Reference documentation for deal.II version GIT relicensing-462-g03c82a14bd 2024-04-21 16:20:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
affine_constraints.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_affine_constraints_h
16#define dealii_affine_constraints_h
17
18#include <deal.II/base/config.h>
19
23#include <deal.II/base/table.h>
26
28#include <deal.II/lac/vector.h>
30
31#include <boost/range/iterator_range.hpp>
32
33#include <algorithm>
34#include <set>
35#include <type_traits>
36#include <utility>
37#include <vector>
38
40
41// Forward declarations
42#ifndef DOXYGEN
43template <typename>
44class FullMatrix;
45class SparsityPattern;
49template <typename number>
50class SparseMatrix;
51template <typename number>
53
54namespace internal
55{
56 namespace AffineConstraints
57 {
59
77 struct Distributing
78 {
79 Distributing(const size_type global_row = numbers::invalid_size_type,
80 const size_type local_row = numbers::invalid_size_type);
81
82 Distributing(const Distributing &in);
83
84 Distributing &
85 operator=(const Distributing &in);
86
87 bool
88 operator<(const Distributing &in) const
89 {
90 return global_row < in.global_row;
91 }
92
93 size_type global_row;
94 size_type local_row;
95 mutable size_type constraint_position;
96 };
97
98
99
111 template <typename number>
112 struct DataCache
113 {
114 DataCache();
115
116 void
117 reinit();
118
120 insert_new_index(const std::pair<size_type, number> &pair);
121
122 void
123 append_index(const size_type index,
124 const std::pair<size_type, number> &pair);
125
127 get_size(const size_type index) const;
128
129 const std::pair<size_type, number> *
130 get_entry(const size_type index) const;
131
132 size_type row_length;
133
134 std::vector<std::pair<size_type, number>> data;
135
136 std::vector<size_type> individual_size;
137 };
138
139
140
169 template <typename number>
170 class GlobalRowsFromLocal
171 {
172 public:
176 GlobalRowsFromLocal();
177
178 void
179 reinit(const size_type n_local_rows);
180
181 void
182 insert_index(const size_type global_row,
183 const size_type local_row,
184 const number constraint_value);
185 void
186 sort();
187
188 void
189 print(std::ostream &os);
190
195 size() const;
196
202 size(const size_type counter_index) const;
203
208 global_row(const size_type counter_index) const;
209
213 size_type &
214 global_row(const size_type counter_index);
215
222 local_row(const size_type counter_index) const;
223
227 size_type &
228 local_row(const size_type counter_index);
229
236 local_row(const size_type counter_index,
237 const size_type index_in_constraint) const;
238
243 number
244 constraint_value(const size_type counter_index,
245 const size_type index_in_constraint) const;
246
253 bool
254 have_indirect_rows() const;
255
260 void
261 insert_constraint(const size_type constrained_local_dof);
262
269 n_constraints() const;
270
276 n_inhomogeneities() const;
277
284 void
285 set_ith_constraint_inhomogeneous(const size_type i);
286
292 constraint_origin(size_type i) const;
293
299 std::vector<Distributing> total_row_indices;
300
301 private:
305 DataCache<number> data_cache;
306
311 size_type n_active_rows;
312
317 size_type n_inhomogeneous_rows;
318 };
319
320
321
339 template <typename number>
340 struct ScratchData
341 {
345 ScratchData()
346 : in_use(false)
347 {}
348
352 ScratchData(const ScratchData &)
353 : in_use(false)
354 {}
355
359 bool in_use;
360
364 std::vector<std::pair<size_type, size_type>> new_entries;
365
369 std::vector<size_type> rows;
370
374 std::vector<size_type> columns;
375
379 std::vector<number> values;
380
384 std::vector<size_type> block_starts;
385
389 std::vector<size_type> vector_indices;
390
394 std::vector<number> vector_values;
395
399 GlobalRowsFromLocal<number> global_rows;
400
404 GlobalRowsFromLocal<number> global_columns;
405 };
406 } // namespace AffineConstraints
407} // namespace internal
408
409namespace internal
410{
411 namespace AffineConstraintsImplementation
412 {
413 template <typename VectorType>
414 void
415 set_zero_all(const std::vector<types::global_dof_index> &cm,
416 VectorType &vec);
417
418 template <class T>
419 void
420 set_zero_all(const std::vector<types::global_dof_index> &cm,
421 ::Vector<T> &vec);
422
423 template <class T>
424 void
425 set_zero_all(const std::vector<types::global_dof_index> &cm,
426 ::BlockVector<T> &vec);
427 } // namespace AffineConstraintsImplementation
428} // namespace internal
429#endif
430
431
432
505template <typename number = double>
507{
508public:
513
541
560
583 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
584 "Use the constructor with two index set arguments.")
585 explicit AffineConstraints(const IndexSet &locally_stored_constraints);
586
619 const IndexSet &locally_stored_constraints);
620
624 explicit AffineConstraints(const AffineConstraints &affine_constraints);
625
629 AffineConstraints(AffineConstraints &&affine_constraints) noexcept =
630 default; // NOLINT
631
642 operator=(const AffineConstraints &) = delete;
643
648 operator=(AffineConstraints &&affine_constraints) noexcept =
649 default; // NOLINT
650
657 template <typename other_number>
658 void
659 copy_from(const AffineConstraints<other_number> &other);
660
669 void
671
680 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
681 "Use the reinit() function with two index set arguments.")
682 void
683 reinit(const IndexSet &locally_stored_constraints);
684
693 void
695 const IndexSet &locally_stored_constraints);
696
705 bool
706 can_store_line(const size_type line_n) const;
707
716 const IndexSet &
718
725 const IndexSet &
727
756 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT("Use get_view() and merge() instead.")
757 void
759 const IndexSet &filter);
760
811 void
813 const size_type constrained_dof,
814 const ArrayView<const std::pair<size_type, number>> &dependencies,
815 const number inhomogeneity = 0);
816
834 void
835 constrain_dof_to_zero(const size_type constrained_dof);
836
841 void
842 add_line(const size_type line_n);
843
856 void
857 add_lines(const std::vector<bool> &lines);
858
871 void
873
886 void
888
904 void
905 add_entry(const size_type constrained_dof_index,
906 const size_type column,
907 const number weight);
908
914 void
916 const size_type constrained_dof_index,
917 const std::vector<std::pair<size_type, number>> &col_weight_pairs);
918
930 void
931 set_inhomogeneity(const size_type constrained_dof_index, const number value);
932
954 void
956
962 bool
963 is_closed() const;
964
970 bool
971 is_closed(const MPI_Comm comm) const;
972
997 template <typename other_number>
998 void
1000 const AffineConstraints<other_number> &other_constraints,
1001 const MergeConflictBehavior merge_conflict_behavior = no_conflicts_allowed,
1002 const bool allow_different_local_lines = false);
1003
1016 void
1017 shift(const size_type offset);
1018
1068 get_view(const IndexSet &mask) const;
1069
1077 void
1079
1092 size_type
1094
1099 size_type
1101
1107 size_type
1109
1119 bool
1120 is_constrained(const size_type line_n) const;
1121
1133 bool
1135
1142 bool
1144 const size_type line_n_2) const;
1145
1156 size_type
1158
1163 bool
1165
1171 bool
1173
1178 const std::vector<std::pair<size_type, number>> *
1180
1185 number
1186 get_inhomogeneity(const size_type line_n) const;
1187
1208 void
1209 print(std::ostream &out) const;
1210
1223 void
1224 write_dot(std::ostream &) const;
1225
1230 std::size_t
1232
1239 void
1240 resolve_indices(std::vector<types::global_dof_index> &indices) const;
1241
1267 void
1268 condense(SparsityPattern &sparsity) const;
1269
1273 void
1275
1280 void
1282
1287 void
1289
1297 void
1298 condense(SparseMatrix<number> &matrix) const;
1299
1303 void
1304 condense(BlockSparseMatrix<number> &matrix) const;
1305
1317 template <typename VectorType>
1318 void
1319 condense(VectorType &vec) const;
1320
1327 template <typename VectorType>
1328 void
1329 condense(const VectorType &vec_ghosted, VectorType &output) const;
1330
1343 template <typename VectorType>
1344 void
1345 condense(SparseMatrix<number> &matrix, VectorType &vector) const;
1346
1351 template <typename BlockVectorType>
1352 void
1353 condense(BlockSparseMatrix<number> &matrix, BlockVectorType &vector) const;
1354
1361 template <typename VectorType>
1362 void
1363 set_zero(VectorType &vec) const;
1364
1420 template <class InVector, class OutVector>
1421 void
1422 distribute_local_to_global(const InVector &local_vector,
1423 const std::vector<size_type> &local_dof_indices,
1424 OutVector &global_vector) const;
1425
1473 template <typename VectorType>
1474 void
1475 distribute_local_to_global(const Vector<number> &local_vector,
1476 const std::vector<size_type> &local_dof_indices,
1477 VectorType &global_vector,
1478 const FullMatrix<number> &local_matrix) const;
1479
1499 template <typename VectorType>
1500 void
1502 const Vector<number> &local_vector,
1503 const std::vector<size_type> &local_dof_indices_row,
1504 const std::vector<size_type> &local_dof_indices_col,
1505 VectorType &global_vector,
1506 const FullMatrix<number> &local_matrix,
1507 bool diagonal = false) const;
1508
1512 template <typename VectorType>
1513 void
1515 const number value,
1516 VectorType &global_vector) const;
1517
1550 template <typename ForwardIteratorVec,
1551 typename ForwardIteratorInd,
1552 typename VectorType>
1553 void
1554 distribute_local_to_global(ForwardIteratorVec local_vector_begin,
1555 ForwardIteratorVec local_vector_end,
1556 ForwardIteratorInd local_indices_begin,
1557 VectorType &global_vector) const;
1558
1610 template <typename MatrixType>
1611 void
1612 distribute_local_to_global(const FullMatrix<number> &local_matrix,
1613 const std::vector<size_type> &local_dof_indices,
1614 MatrixType &global_matrix) const;
1615
1643 template <typename MatrixType>
1644 void
1645 distribute_local_to_global(const FullMatrix<number> &local_matrix,
1646 const std::vector<size_type> &row_indices,
1647 const std::vector<size_type> &col_indices,
1648 MatrixType &global_matrix) const;
1649
1666 template <typename MatrixType>
1667 void
1668 distribute_local_to_global(const FullMatrix<number> &local_matrix,
1669 const std::vector<size_type> &row_indices,
1670 const AffineConstraints &column_affine_constraints,
1671 const std::vector<size_type> &column_indices,
1672 MatrixType &global_matrix) const;
1673
1694 template <typename MatrixType, typename VectorType>
1695 void
1696 distribute_local_to_global(const FullMatrix<number> &local_matrix,
1697 const Vector<number> &local_vector,
1698 const std::vector<size_type> &local_dof_indices,
1699 MatrixType &global_matrix,
1700 VectorType &global_vector,
1701 bool use_inhomogeneities_for_rhs = false) const;
1702
1756 void
1758 const std::vector<size_type> &local_dof_indices,
1759 SparsityPatternBase &sparsity_pattern,
1760 const bool keep_constrained_entries = true,
1761 const Table<2, bool> &dof_mask = Table<2, bool>()) const;
1762
1766 void
1768 const std::vector<size_type> &row_indices,
1769 const std::vector<size_type> &col_indices,
1770 SparsityPatternBase &sparsity_pattern,
1771 const bool keep_constrained_entries = true,
1772 const Table<2, bool> &dof_mask = Table<2, bool>()) const;
1773
1778 void
1780 const std::vector<size_type> &row_indices,
1781 const AffineConstraints<number> &col_constraints,
1782 const std::vector<size_type> &col_indices,
1783 SparsityPatternBase &sparsity_pattern,
1784 const bool keep_constrained_entries = true,
1785 const Table<2, bool> &dof_mask = Table<2, bool>()) const;
1786
1806 template <typename ForwardIteratorVec,
1807 typename ForwardIteratorInd,
1808 typename VectorType>
1809 void
1810 get_dof_values(const VectorType &global_vector,
1811 ForwardIteratorInd local_indices_begin,
1812 ForwardIteratorVec local_vector_begin,
1813 ForwardIteratorVec local_vector_end) const;
1814
1836 template <typename VectorType>
1837 void
1838 distribute(VectorType &vec) const;
1839
1848 {
1853 using Entries = std::vector<std::pair<size_type, number>>;
1854
1861
1870
1875
1879 ConstraintLine(const size_type &index = numbers::invalid_dof_index,
1880 const typename AffineConstraints<
1881 number>::ConstraintLine::Entries &entries = {},
1882 const number inhomogeneity = 0.0);
1883
1887 ConstraintLine(const ConstraintLine &other) = default;
1888
1892 ConstraintLine(ConstraintLine &&other) noexcept = default;
1893
1898 operator=(const ConstraintLine &other) = default;
1899
1904 operator=(ConstraintLine &&other) noexcept = default;
1905
1910 std::size_t
1912
1918 template <class Archive>
1919 void
1920 serialize(Archive &ar, const unsigned int)
1921 {
1922 ar &index &entries &inhomogeneity;
1923 }
1924
1928 friend void
1930 {
1931 std::swap(l1.index, l2.index);
1932 std::swap(l1.entries, l2.entries);
1933 std::swap(l1.inhomogeneity, l2.inhomogeneity);
1934 }
1935 };
1936
1940 using const_iterator = typename std::vector<ConstraintLine>::const_iterator;
1941
1945 using LineRange = boost::iterator_range<const_iterator>;
1946
1955 LineRange
1956 get_lines() const;
1957
1986 bool
1987 is_consistent_in_parallel(const std::vector<IndexSet> &locally_owned_dofs,
1988 const IndexSet &locally_active_dofs,
1989 const MPI_Comm mpi_communicator,
1990 const bool verbose = false) const;
1991
1997 void
1999 const IndexSet &locally_relevant_dofs,
2000 const MPI_Comm mpi_communicator);
2001
2009 "You are attempting an operation on an AffineConstraints object "
2010 "that requires the object to not be 'closed', i.e., for which you "
2011 "must not already have called the close() member function. But the "
2012 "object is already closed, and so the operation can not be "
2013 "performed.");
2021 "You are attempting an operation on an AffineConstraints object "
2022 "that requires the object to be 'closed', i.e., for which you "
2023 "needed to call the close() member function. But the object "
2024 "is not currently closed, and so the operation can not be "
2025 "performed.");
2032 size_type,
2033 << "The specified line " << arg1 << " does not exist.");
2040 size_type,
2041 size_type,
2042 number,
2043 number,
2044 << "The entry for the indices " << arg1 << " and " << arg2
2045 << " already exists, but the values " << arg3 << " (old) and "
2046 << arg4 << " (new) differ "
2047 << "by " << (arg4 - arg3) << '.');
2054 int,
2055 int,
2056 << "You tried to constrain DoF " << arg1 << " to DoF " << arg2
2057 << ", but that one is also constrained. This is not allowed!");
2064 size_type,
2065 << "Degree of freedom " << arg1
2066 << " is constrained from both object in a merge operation.");
2073 size_type,
2074 << "In the given argument a degree of freedom is constrained "
2075 << "to another DoF with number " << arg1
2076 << ", which however is constrained by this object. This is not"
2077 << " allowed.");
2084 size_type,
2085 << "The index set given to this constraints object indicates "
2086 << "constraints for degree of freedom " << arg1
2087 << " should not be stored by this object, but a constraint "
2088 << "is being added.");
2089
2096 size_type,
2097 size_type,
2098 << "The index set given to this constraints object indicates "
2099 << "constraints using degree of freedom " << arg2
2100 << " should not be stored by this object, but a constraint "
2101 << "for degree of freedom " << arg1 << " uses it.");
2102
2109 int,
2110 int,
2111 << "While distributing the constraint for DoF " << arg1
2112 << ", it turns out that one of the processors "
2113 << "who own the " << arg2 << " degrees of freedom that x_"
2114 << arg1 << " is constrained against does not know about "
2115 << "the constraint on x_" << arg1
2116 << ". Did you not initialize the AffineConstraints container "
2117 << "with the appropriate locally_relevant set so "
2118 << "that every processor who owns a DoF that constrains "
2119 << "another DoF also knows about this constraint?");
2120
2121 template <typename>
2122 friend class AffineConstraints;
2123
2124private:
2136 std::vector<ConstraintLine> lines;
2137
2170 std::vector<size_type> lines_cache;
2171
2179
2186
2196
2201
2203 internal::AffineConstraints::ScratchData<number>>
2205
2210 size_type
2211 calculate_line_index(const size_type line_n) const;
2212
2217 template <typename MatrixType, typename VectorType>
2218 void
2220 const Vector<number> &local_vector,
2221 const std::vector<size_type> &local_dof_indices,
2222 MatrixType &global_matrix,
2223 VectorType &global_vector,
2224 const bool use_inhomogeneities_for_rhs,
2225 const std::bool_constant<false>) const;
2226
2231 template <typename MatrixType, typename VectorType>
2232 void
2234 const Vector<number> &local_vector,
2235 const std::vector<size_type> &local_dof_indices,
2236 MatrixType &global_matrix,
2237 VectorType &global_vector,
2238 const bool use_inhomogeneities_for_rhs,
2239 const std::bool_constant<true>) const;
2240
2248 void
2249 make_sorted_row_list(const std::vector<size_type> &local_dof_indices,
2250 internal::AffineConstraints::GlobalRowsFromLocal<number>
2251 &global_rows) const;
2252
2260 void
2261 make_sorted_row_list(const std::vector<size_type> &local_dof_indices,
2262 std::vector<size_type> &active_dofs) const;
2263
2267 template <typename MatrixScalar, typename VectorScalar>
2270 const size_type i,
2271 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2272 const Vector<VectorScalar> &local_vector,
2273 const std::vector<size_type> &local_dof_indices,
2274 const FullMatrix<MatrixScalar> &local_matrix) const;
2275};
2276
2277/* ---------------- template and inline functions ----------------- */
2278
2279template <typename number>
2283
2284
2285
2286template <typename number>
2288 const IndexSet &locally_stored_constraints)
2289 : AffineConstraints<number>(locally_stored_constraints,
2290 locally_stored_constraints)
2291{}
2292
2293
2294
2295template <typename number>
2298 const IndexSet &locally_stored_constraints)
2299 : lines()
2301 , local_lines(locally_stored_constraints)
2302 , sorted(false)
2303{
2304 Assert(locally_owned_dofs.is_subset_of(locally_stored_constraints),
2305 ExcMessage("The set of locally stored constraints needs to be a "
2306 "superset of the locally owned DoFs."));
2307
2308 // make sure the IndexSet is compressed. Otherwise this can lead to crashes
2309 // that are hard to find (only happen in release mode).
2310 // see tests/mpi/affine_constraints_crash_01
2312}
2313
2314
2315
2316template <typename number>
2318 const AffineConstraints &affine_constraints)
2319 : Subscriptor()
2320 , lines(affine_constraints.lines)
2321 , lines_cache(affine_constraints.lines_cache)
2322 , locally_owned_dofs(affine_constraints.locally_owned_dofs)
2323 , local_lines(affine_constraints.local_lines)
2325 affine_constraints.needed_elements_for_distribute)
2326 , sorted(affine_constraints.sorted)
2327{}
2328
2329
2330
2331template <typename number>
2332inline void
2334{
2335 Assert(sorted == false, ExcMatrixIsClosed());
2336
2337 // the following can happen when we compute with distributed meshes and dof
2338 // handlers and we constrain a degree of freedom whose number we don't have
2339 // locally. if we don't abort here the program will try to allocate several
2340 // terabytes of memory to resize the various arrays below :-)
2342 const size_type line_index = calculate_line_index(line_n);
2343
2344 // check whether line already exists; it may, in which case we can just quit
2345 if (is_constrained(line_n))
2346 return;
2347
2348 // if necessary enlarge vector of existing entries for cache
2349 if (line_index >= lines_cache.size())
2350 lines_cache.resize(std::max(2 * static_cast<size_type>(lines_cache.size()),
2351 line_index + 1),
2353
2354 // push a new line to the end of the list
2355 lines.emplace_back();
2356 lines.back().index = line_n;
2357 lines.back().inhomogeneity = 0.;
2358 lines_cache[line_index] = lines.size() - 1;
2359}
2360
2361
2362
2363template <typename number>
2364inline void
2366 const size_type column,
2367 const number weight)
2368{
2369 Assert(sorted == false, ExcMatrixIsClosed());
2370 Assert(constrained_dof_index != column,
2371 ExcMessage("Can't constrain a degree of freedom to itself"));
2372
2373 // Ensure that the current line is present in the cache:
2374 const size_type line_index = calculate_line_index(constrained_dof_index);
2375 Assert(line_index < lines_cache.size(),
2376 ExcMessage("The current AffineConstraints does not contain the line "
2377 "for the current entry. Call AffineConstraints::add_line "
2378 "before calling this function."));
2379
2380 // if in debug mode, check whether an entry for this column already exists
2381 // and if it's the same as the one entered at present
2382 //
2383 // in any case: exit the function if an entry for this column already
2384 // exists, since we don't want to enter it twice
2388 ExcColumnNotStoredHere(constrained_dof_index, column));
2389 ConstraintLine *line_ptr = &lines[lines_cache[line_index]];
2390 Assert(line_ptr->index == constrained_dof_index, ExcInternalError());
2391 for (const auto &p : line_ptr->entries)
2392 if (p.first == column)
2393 {
2394 Assert(std::abs(p.second - weight) < 1.e-14,
2396 constrained_dof_index, column, p.second, weight));
2397 return;
2398 }
2399
2400 line_ptr->entries.emplace_back(column, weight);
2401}
2402
2403
2404
2405template <typename number>
2406inline void
2408 const size_type constrained_dof_index,
2409 const number value)
2410{
2411 const size_type line_index = calculate_line_index(constrained_dof_index);
2412 Assert(line_index < lines_cache.size() &&
2414 ExcMessage("call add_line() before calling set_inhomogeneity()"));
2415 Assert(lines_cache[line_index] < lines.size(), ExcInternalError());
2416 ConstraintLine *line_ptr = &lines[lines_cache[line_index]];
2417 line_ptr->inhomogeneity = value;
2418}
2419
2420
2421
2422template <typename number>
2423template <typename VectorType>
2424inline void
2426{
2427 // since lines is a private member, we cannot pass it to the functions
2428 // above. therefore, copy the content which is cheap
2429 std::vector<size_type> constrained_lines(lines.size());
2430 for (unsigned int i = 0; i < lines.size(); ++i)
2431 constrained_lines[i] = lines[i].index;
2432 internal::AffineConstraintsImplementation::set_zero_all(constrained_lines,
2433 vec);
2434}
2435
2436template <typename number>
2439{
2440 return lines.size();
2441}
2442
2443template <typename number>
2446{
2447 return std::count_if(lines.begin(),
2448 lines.end(),
2449 [](const ConstraintLine &line) {
2450 return (line.entries.size() == 1) &&
2451 (line.entries[0].second == number(1.));
2452 });
2453}
2454
2455template <typename number>
2458{
2459 return std::count_if(lines.begin(),
2460 lines.end(),
2461 [](const ConstraintLine &line) {
2462 return (line.inhomogeneity != number(0.));
2463 });
2464}
2465
2466template <typename number>
2467inline bool
2469{
2470 if (lines.empty())
2471 return false;
2472
2473 const size_type line_index = calculate_line_index(index);
2474 return ((line_index < lines_cache.size()) &&
2475 (lines_cache[line_index] != numbers::invalid_size_type));
2476}
2477
2478template <typename number>
2479inline bool
2481 const size_type line_n) const
2482{
2483 // check whether the entry is constrained. could use is_constrained, but
2484 // that means computing the line index twice
2485 const size_type line_index = calculate_line_index(line_n);
2486 if (line_index >= lines_cache.size() ||
2488 return false;
2489 else
2490 {
2491 Assert(lines_cache[line_index] < lines.size(), ExcInternalError());
2492 return (lines[lines_cache[line_index]].inhomogeneity != number(0.));
2493 }
2494}
2495
2496template <typename number>
2497inline const std::vector<std::pair<types::global_dof_index, number>> *
2499{
2500 if (lines.empty())
2501 return nullptr;
2502
2503 // check whether the entry is constrained. could use is_constrained, but
2504 // that means computing the line index twice
2505 const size_type line_index = calculate_line_index(line_n);
2506 if (line_index >= lines_cache.size() ||
2508 return nullptr;
2509 else
2510 return &lines[lines_cache[line_index]].entries;
2511}
2512
2513template <typename number>
2514inline number
2516{
2517 // check whether the entry is constrained. could use is_constrained, but
2518 // that means computing the line index twice
2519 const size_type line_index = calculate_line_index(line_n);
2520 if (line_index >= lines_cache.size() ||
2522 return 0;
2523 else
2524 return lines[lines_cache[line_index]].inhomogeneity;
2525}
2526
2527template <typename number>
2530{
2531 // IndexSet is unused (serial case)
2532 if (local_lines.size() == 0)
2533 return line_n;
2534
2536
2537 return local_lines.index_within_set(line_n);
2538}
2539
2540
2541
2542template <typename number>
2543inline bool
2545{
2546 return local_lines.size() == 0 || local_lines.is_element(line_n);
2547}
2548
2549
2550
2551template <typename number>
2552inline const IndexSet &
2557
2558
2559
2560template <typename number>
2561inline const IndexSet &
2566
2567
2568
2569template <typename number>
2570template <typename VectorType>
2571inline void
2573 const size_type index,
2574 const number value,
2575 VectorType &global_vector) const
2576{
2577 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2578
2579 if (is_constrained(index) == false)
2580 global_vector(index) += value;
2581 else
2582 {
2583 const ConstraintLine &position =
2585 for (size_type j = 0; j < position.entries.size(); ++j)
2586 global_vector(position.entries[j].first) +=
2587 value * position.entries[j].second;
2588 }
2589}
2590
2591template <typename number>
2592template <typename ForwardIteratorVec,
2593 typename ForwardIteratorInd,
2594 typename VectorType>
2595inline void
2597 ForwardIteratorVec local_vector_begin,
2598 ForwardIteratorVec local_vector_end,
2599 ForwardIteratorInd local_indices_begin,
2600 VectorType &global_vector) const
2601{
2602 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2603 for (; local_vector_begin != local_vector_end;
2604 ++local_vector_begin, ++local_indices_begin)
2605 {
2606 if (is_constrained(*local_indices_begin) == false)
2607 internal::ElementAccess<VectorType>::add(*local_vector_begin,
2608 *local_indices_begin,
2609 global_vector);
2610 else
2611 {
2612 const ConstraintLine &position =
2613 lines[lines_cache[calculate_line_index(*local_indices_begin)]];
2614 for (size_type j = 0; j < position.entries.size(); ++j)
2616 (*local_vector_begin) * position.entries[j].second,
2617 position.entries[j].first,
2618 global_vector);
2619 }
2620 }
2621}
2622
2623template <typename number>
2624template <class InVector, class OutVector>
2625inline void
2627 const InVector &local_vector,
2628 const std::vector<size_type> &local_dof_indices,
2629 OutVector &global_vector) const
2630{
2631 Assert(global_vector.has_ghost_elements() == false, ExcGhostsPresent());
2632 Assert(local_vector.size() == local_dof_indices.size(),
2633 ExcDimensionMismatch(local_vector.size(), local_dof_indices.size()));
2634 distribute_local_to_global(local_vector.begin(),
2635 local_vector.end(),
2636 local_dof_indices.begin(),
2637 global_vector);
2638}
2639
2640template <typename number>
2641template <typename ForwardIteratorVec,
2642 typename ForwardIteratorInd,
2643 typename VectorType>
2644inline void
2646 const VectorType &global_vector,
2647 ForwardIteratorInd local_indices_begin,
2648 ForwardIteratorVec local_vector_begin,
2649 ForwardIteratorVec local_vector_end) const
2650{
2651 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2652 for (; local_vector_begin != local_vector_end;
2653 ++local_vector_begin, ++local_indices_begin)
2654 {
2655 if (is_constrained(*local_indices_begin) == false)
2656 *local_vector_begin = global_vector(*local_indices_begin);
2657 else
2658 {
2659 const ConstraintLine &position =
2660 lines[lines_cache[calculate_line_index(*local_indices_begin)]];
2661 typename VectorType::value_type value = position.inhomogeneity;
2662 for (size_type j = 0; j < position.entries.size(); ++j)
2663 value += (global_vector(position.entries[j].first) *
2664 position.entries[j].second);
2665 *local_vector_begin = value;
2666 }
2667 }
2668}
2669
2670// Forward declarations
2671#ifndef DOXYGEN
2672template <typename MatrixType>
2673class BlockMatrixBase;
2674template <typename SparsityPatternType>
2676template <typename number>
2678
2679namespace internal
2680{
2681 namespace AffineConstraints
2682 {
2700 template <typename MatrixType>
2701 struct IsBlockMatrix
2702 {
2703 private:
2709 template <typename T>
2710 static std::true_type
2711 check(const BlockMatrixBase<T> *);
2712
2717 template <typename T>
2718 static std::true_type
2719 check(const BlockSparseMatrixEZ<T> *);
2720
2725 static std::false_type
2726 check(...);
2727
2728 public:
2735 static const bool value =
2736 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2737 std::true_type>;
2738 };
2739
2740 // instantiation of the static member
2741 template <typename MatrixType>
2742 const bool IsBlockMatrix<MatrixType>::value;
2743
2744 } // namespace AffineConstraints
2745} // namespace internal
2746#endif
2747
2748
2749
2750template <typename number>
2751template <typename other_number>
2752inline void
2755{
2756 lines.clear();
2757 lines.reserve(other.lines.size());
2758
2759 for (const auto &l : other.lines)
2760 lines.emplace_back(l.index,
2761 typename ConstraintLine::Entries(l.entries.begin(),
2762 l.entries.end()),
2763 l.inhomogeneity);
2764
2765 lines_cache = other.lines_cache;
2766 local_lines = other.local_lines;
2767 sorted = other.sorted;
2768
2769 locally_owned_dofs = other.locally_owned_dofs;
2770 needed_elements_for_distribute = other.needed_elements_for_distribute;
2771}
2772
2773
2774template <typename number>
2775template <typename other_number>
2776void
2778 const AffineConstraints<other_number> &other_constraints,
2779 const MergeConflictBehavior merge_conflict_behavior,
2780 const bool allow_different_local_lines)
2781{
2782 (void)allow_different_local_lines;
2783 Assert(allow_different_local_lines ||
2784 local_lines == other_constraints.local_lines,
2785 ExcMessage(
2786 "local_lines for this and the other objects are not the same "
2787 "although allow_different_local_lines is false."));
2788
2789 // store the previous state with respect to sorting
2790 const bool object_was_sorted = sorted;
2791 sorted = false;
2792
2793 // first action is to fold into the present object possible constraints
2794 // in the second object. we don't strictly need to do this any more since
2795 // the AffineConstraints container has learned to deal with chains of
2796 // constraints in the close() function, but we have traditionally done
2797 // this and it's not overly hard to do.
2798 //
2799 // for this, loop over all constraints and replace the constraint lines
2800 // with a new one where constraints are replaced if necessary.
2801 typename ConstraintLine::Entries tmp;
2802 for (ConstraintLine &line : lines)
2803 {
2804 tmp.clear();
2805 for (const std::pair<size_type, number> &entry : line.entries)
2806 {
2807 // if the present dof is not stored, or not constrained, or if we
2808 // won't take the constraint from the other object, then simply copy
2809 // it over
2810 if ((other_constraints.local_lines.size() != 0. &&
2811 other_constraints.local_lines.is_element(entry.first) ==
2812 false) ||
2813 other_constraints.is_constrained(entry.first) == false ||
2814 ((merge_conflict_behavior != right_object_wins) &&
2815 other_constraints.is_constrained(entry.first) &&
2816 this->is_constrained(entry.first)))
2817 tmp.push_back(entry);
2818 else
2819 // otherwise resolve further constraints by replacing the old
2820 // entry by a sequence of new entries taken from the other
2821 // object, but with multiplied weights
2822 {
2823 const auto *other_entries =
2824 other_constraints.get_constraint_entries(entry.first);
2825 Assert(other_entries != nullptr, ExcInternalError());
2826
2827 const number weight = entry.second;
2828
2829 for (const auto &other_entry : *other_entries)
2830 tmp.emplace_back(other_entry.first,
2831 other_entry.second * weight);
2832
2833 line.inhomogeneity +=
2834 other_constraints.get_inhomogeneity(entry.first) * weight;
2835 }
2836 }
2837 // finally exchange old and newly resolved line
2838 line.entries.swap(tmp);
2839 }
2840
2841 if (local_lines.size() != 0)
2842 local_lines.add_indices(other_constraints.local_lines);
2843
2844 {
2845 // do not bother to resize the lines cache exactly since it is pretty
2846 // cheap to adjust it along the way.
2847 std::fill(lines_cache.begin(),
2848 lines_cache.end(),
2850
2851 // reset lines_cache for our own constraints
2852 size_type index = 0;
2853 for (const ConstraintLine &line : lines)
2854 {
2855 const size_type local_line_no = calculate_line_index(line.index);
2856 if (local_line_no >= lines_cache.size())
2857 lines_cache.resize(local_line_no + 1, numbers::invalid_size_type);
2858 lines_cache[local_line_no] = index++;
2859 }
2860
2861 // Add other_constraints to lines cache and our list of constraints
2862 for (const auto &line : other_constraints.lines)
2863 {
2864 const size_type local_line_no = calculate_line_index(line.index);
2865 if (local_line_no >= lines_cache.size())
2866 {
2867 lines_cache.resize(local_line_no + 1, numbers::invalid_size_type);
2868 lines.emplace_back(line.index,
2869 typename ConstraintLine::Entries(
2870 line.entries.begin(), line.entries.end()),
2871 line.inhomogeneity);
2872 lines_cache[local_line_no] = index++;
2873 }
2874 else if (lines_cache[local_line_no] == numbers::invalid_size_type)
2875 {
2876 // there are no constraints for that line yet
2877 lines.emplace_back(line.index,
2878 typename ConstraintLine::Entries(
2879 line.entries.begin(), line.entries.end()),
2880 line.inhomogeneity);
2881 AssertIndexRange(local_line_no, lines_cache.size());
2882 lines_cache[local_line_no] = index++;
2883 }
2884 else
2885 {
2886 // we already store that line
2887 switch (merge_conflict_behavior)
2888 {
2889 case no_conflicts_allowed:
2890 AssertThrow(false,
2891 ExcDoFIsConstrainedFromBothObjects(line.index));
2892 break;
2893
2894 case left_object_wins:
2895 // ignore this constraint
2896 break;
2897
2898 case right_object_wins:
2899 AssertIndexRange(local_line_no, lines_cache.size());
2900 lines[lines_cache[local_line_no]] = {
2901 line.index,
2902 typename ConstraintLine::Entries(line.entries.begin(),
2903 line.entries.end()),
2904 static_cast<number>(line.inhomogeneity)};
2905 break;
2906
2907 default:
2909 }
2910 }
2911 }
2912
2913 // check that we set the pointers correctly
2914 for (size_type i = 0; i < lines_cache.size(); ++i)
2915 if (lines_cache[i] != numbers::invalid_size_type)
2916 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
2918 }
2919
2920 // if the object was sorted before, then make sure it is so afterward as
2921 // well. otherwise leave everything in the unsorted state
2922 if (object_was_sorted == true)
2923 close();
2924}
2925
2926
2927
2928template <typename number>
2929template <typename MatrixType>
2930inline void
2932 const FullMatrix<number> &local_matrix,
2933 const std::vector<size_type> &local_dof_indices,
2934 MatrixType &global_matrix) const
2935{
2936 // create a dummy and hand on to the function actually implementing this
2937 // feature in the cm.templates.h file.
2939 distribute_local_to_global(
2940 local_matrix,
2941 dummy,
2942 local_dof_indices,
2943 global_matrix,
2944 dummy,
2945 false,
2946 std::integral_constant<
2947 bool,
2948 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
2949}
2950
2951
2952
2953template <typename number>
2954template <typename MatrixType, typename VectorType>
2955inline void
2957 const FullMatrix<number> &local_matrix,
2958 const Vector<number> &local_vector,
2959 const std::vector<size_type> &local_dof_indices,
2960 MatrixType &global_matrix,
2961 VectorType &global_vector,
2962 bool use_inhomogeneities_for_rhs) const
2963{
2964 // enter the internal function with the respective block information set,
2965 // the actual implementation follows in the cm.templates.h file.
2966 distribute_local_to_global(
2967 local_matrix,
2968 local_vector,
2969 local_dof_indices,
2970 global_matrix,
2971 global_vector,
2972 use_inhomogeneities_for_rhs,
2973 std::integral_constant<
2974 bool,
2975 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
2976}
2977
2978
2979
2980template <typename number>
2982 const size_type &index,
2984 const number inhomogeneity)
2985 : index(index)
2986 , entries(entries)
2987 , inhomogeneity(inhomogeneity)
2988{}
2989
2990
2991
2993
2994#endif
void add_entries(const size_type constrained_dof_index, const std::vector< std::pair< size_type, number > > &col_weight_pairs)
bool is_closed() const
size_type n_inhomogeneities() const
bool has_inhomogeneities() const
LineRange get_lines() const
void add_line(const size_type line_n)
const IndexSet & get_locally_owned_indices() const
void add_selected_constraints(const AffineConstraints &constraints_in, const IndexSet &filter)
bool can_store_line(const size_type line_n) const
void add_constraint(const size_type constrained_dof, const ArrayView< const std::pair< size_type, number > > &dependencies, const number inhomogeneity=0)
Threads::ThreadLocalStorage< internal::AffineConstraints::ScratchData< number > > scratch_data
friend class AffineConstraints
void merge(const AffineConstraints< other_number > &other_constraints, const MergeConflictBehavior merge_conflict_behavior=no_conflicts_allowed, const bool allow_different_local_lines=false)
void add_entry(const size_type constrained_dof_index, const size_type column, const number weight)
void make_consistent_in_parallel(const IndexSet &locally_owned_dofs, const IndexSet &locally_relevant_dofs, const MPI_Comm mpi_communicator)
number get_inhomogeneity(const size_type line_n) const
void make_sorted_row_list(const std::vector< size_type > &local_dof_indices, std::vector< size_type > &active_dofs) const
void get_dof_values(const VectorType &global_vector, ForwardIteratorInd local_indices_begin, ForwardIteratorVec local_vector_begin, ForwardIteratorVec local_vector_end) const
void distribute_local_to_global(const InVector &local_vector, const std::vector< size_type > &local_dof_indices, OutVector &global_vector) const
void make_sorted_row_list(const std::vector< size_type > &local_dof_indices, internal::AffineConstraints::GlobalRowsFromLocal< number > &global_rows) const
const IndexSet & get_local_lines() const
void add_entries_local_to_global(const std::vector< size_type > &local_dof_indices, SparsityPatternBase &sparsity_pattern, const bool keep_constrained_entries=true, const Table< 2, bool > &dof_mask=Table< 2, bool >()) const
void shift(const size_type offset)
size_type n_identities() const
std::size_t memory_consumption() const
typename std::vector< ConstraintLine >::const_iterator const_iterator
void set_inhomogeneity(const size_type constrained_dof_index, const number value)
void condense(SparsityPattern &sparsity) const
size_type max_constraint_indirections() const
ProductType< VectorScalar, MatrixScalar >::type resolve_vector_entry(const size_type i, const internal::AffineConstraints::GlobalRowsFromLocal< number > &global_rows, const Vector< VectorScalar > &local_vector, const std::vector< size_type > &local_dof_indices, const FullMatrix< MatrixScalar > &local_matrix) const
void distribute(VectorType &vec) const
void resolve_indices(std::vector< types::global_dof_index > &indices) const
std::vector< ConstraintLine > lines
bool is_constrained(const size_type line_n) const
AffineConstraints get_view(const IndexSet &mask) const
IndexSet needed_elements_for_distribute
const std::vector< std::pair< size_type, number > > * get_constraint_entries(const size_type line_n) const
size_type calculate_line_index(const size_type line_n) const
void distribute_local_to_global(const FullMatrix< number > &local_matrix, const Vector< number > &local_vector, const std::vector< size_type > &local_dof_indices, MatrixType &global_matrix, VectorType &global_vector, const bool use_inhomogeneities_for_rhs, const std::bool_constant< true >) const
bool is_inhomogeneously_constrained(const size_type index) const
void copy_from(const AffineConstraints< other_number > &other)
std::vector< size_type > lines_cache
void constrain_dof_to_zero(const size_type constrained_dof)
void distribute_local_to_global(const FullMatrix< number > &local_matrix, const Vector< number > &local_vector, const std::vector< size_type > &local_dof_indices, MatrixType &global_matrix, VectorType &global_vector, const bool use_inhomogeneities_for_rhs, const std::bool_constant< false >) const
bool is_consistent_in_parallel(const std::vector< IndexSet > &locally_owned_dofs, const IndexSet &locally_active_dofs, const MPI_Comm mpi_communicator, const bool verbose=false) const
bool is_identity_constrained(const size_type line_n) const
size_type n_constraints() const
void print(std::ostream &out) const
void write_dot(std::ostream &) const
void set_zero(VectorType &vec) const
void add_lines(const std::vector< bool > &lines)
boost::iterator_range< const_iterator > LineRange
bool are_identity_constrained(const size_type line_n_1, const size_type line_n_2) const
bool is_subset_of(const IndexSet &other) const
Definition index_set.cc:703
size_type size() const
Definition index_set.h:1765
size_type index_within_set(const size_type global_index) const
Definition index_set.h:1990
bool is_element(const size_type index) const
Definition index_set.h:1883
void compress() const
Definition index_set.h:1773
A class that provides a separate storage location on each thread that accesses the object.
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
static ::ExceptionBase & ExcMatrixNotClosed()
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition exceptions.h:585
static ::ExceptionBase & ExcDoFIsConstrainedFromBothObjects(size_type arg1)
static ::ExceptionBase & ExcDoFConstrainedToConstrainedDoF(int arg1, int arg2)
#define Assert(cond, exc)
static ::ExceptionBase & ExcMatrixIsClosed()
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:539
static ::ExceptionBase & ExcDoFIsConstrainedToConstrainedDoF(size_type arg1)
static ::ExceptionBase & ExcColumnNotStoredHere(size_type arg1, size_type arg2)
static ::ExceptionBase & ExcRowNotStoredHere(size_type arg1)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
Definition exceptions.h:494
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcIncorrectConstraint(int arg1, int arg2)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:516
static ::ExceptionBase & ExcEntryAlreadyExists(size_type arg1, size_type arg2, number arg3, number arg4)
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcLineInexistent(size_type arg1)
#define AssertThrow(cond, exc)
#define DEAL_II_NOT_IMPLEMENTED()
types::global_dof_index size_type
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
const types::global_dof_index invalid_dof_index
Definition types.h:252
const types::global_dof_index invalid_size_type
Definition types.h:233
STL namespace.
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
Definition types.h:32
unsigned int global_dof_index
Definition types.h:81
*braid_SplitCommworld & comm
void serialize(Archive &ar, const unsigned int)
friend void swap(ConstraintLine &l1, ConstraintLine &l2)
ConstraintLine & operator=(const ConstraintLine &other)=default
ConstraintLine(const ConstraintLine &other)=default
ConstraintLine(const size_type &index=numbers::invalid_dof_index, const typename AffineConstraints< number >::ConstraintLine::Entries &entries={}, const number inhomogeneity=0.0)
std::vector< std::pair< size_type, number > > Entries
ConstraintLine(ConstraintLine &&other) noexcept=default
ConstraintLine & operator=(ConstraintLine &&other) noexcept=default
std::size_t memory_consumption() const
typename internal::ProductTypeImpl< std::decay_t< T >, std::decay_t< U > >::type type
static void add(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V)
bool operator<(const SynchronousIterators< Iterators > &a, const SynchronousIterators< Iterators > &b)