deal.II version GIT relicensing-2084-gab787ef0dd 2024-11-04 03:10:00+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
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
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_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
2051 void
2053 const IndexSet &constraints_to_make_consistent,
2054 const MPI_Comm mpi_communicator);
2055
2063 "You are attempting an operation on an AffineConstraints object "
2064 "that requires the object to not be 'closed', i.e., for which you "
2065 "must not already have called the close() member function. But the "
2066 "object is already closed, and so the operation can not be "
2067 "performed.");
2075 "You are attempting an operation on an AffineConstraints object "
2076 "that requires the object to be 'closed', i.e., for which you "
2077 "needed to call the close() member function. But the object "
2078 "is not currently closed, and so the operation can not be "
2079 "performed.");
2086 size_type,
2087 << "The specified line " << arg1 << " does not exist.");
2094 size_type,
2095 size_type,
2096 number,
2097 number,
2098 << "The entry for the indices " << arg1 << " and " << arg2
2099 << " already exists, but the values " << arg3 << " (old) and "
2100 << arg4 << " (new) differ "
2101 << "by " << (arg4 - arg3) << '.');
2108 int,
2109 int,
2110 << "You tried to constrain DoF " << arg1 << " to DoF " << arg2
2111 << ", but that one is also constrained. This is not allowed!");
2118 size_type,
2119 << "Degree of freedom " << arg1
2120 << " is constrained from both object in a merge operation.");
2127 size_type,
2128 << "In the given argument a degree of freedom is constrained "
2129 << "to another DoF with number " << arg1
2130 << ", which however is constrained by this object. This is not"
2131 << " allowed.");
2138 size_type,
2139 << "The index set given to this constraints object indicates "
2140 << "constraints for degree of freedom " << arg1
2141 << " should not be stored by this object, but a constraint "
2142 << "is being added.");
2143
2150 size_type,
2151 size_type,
2152 << "The index set given to this constraints object indicates "
2153 << "constraints using degree of freedom " << arg2
2154 << " should not be stored by this object, but a constraint "
2155 << "for degree of freedom " << arg1 << " uses it.");
2156
2163 int,
2164 int,
2165 << "While distributing the constraint for DoF " << arg1
2166 << ", it turns out that one of the processors "
2167 << "who own the " << arg2 << " degrees of freedom that x_"
2168 << arg1 << " is constrained against does not know about "
2169 << "the constraint on x_" << arg1
2170 << ". Did you not initialize the AffineConstraints container "
2171 << "with the appropriate locally_relevant set so "
2172 << "that every processor who owns a DoF that constrains "
2173 << "another DoF also knows about this constraint?");
2174
2175 template <typename>
2176 friend class AffineConstraints;
2177
2178private:
2190 std::vector<ConstraintLine> lines;
2191
2224 std::vector<size_type> lines_cache;
2225
2233
2240
2250
2255
2257 internal::AffineConstraints::ScratchData<number>>
2259
2264 size_type
2265 calculate_line_index(const size_type line_n) const;
2266
2271 template <typename MatrixType, typename VectorType>
2272 void
2274 const Vector<number> &local_vector,
2275 const std::vector<size_type> &local_dof_indices,
2276 MatrixType &global_matrix,
2277 VectorType &global_vector,
2278 const bool use_inhomogeneities_for_rhs,
2279 const std::bool_constant<false>) const;
2280
2285 template <typename MatrixType, typename VectorType>
2286 void
2288 const Vector<number> &local_vector,
2289 const std::vector<size_type> &local_dof_indices,
2290 MatrixType &global_matrix,
2291 VectorType &global_vector,
2292 const bool use_inhomogeneities_for_rhs,
2293 const std::bool_constant<true>) const;
2294
2302 void
2303 make_sorted_row_list(const std::vector<size_type> &local_dof_indices,
2304 internal::AffineConstraints::GlobalRowsFromLocal<number>
2305 &global_rows) const;
2306
2314 void
2315 make_sorted_row_list(const std::vector<size_type> &local_dof_indices,
2316 std::vector<size_type> &active_dofs) const;
2317
2321 template <typename MatrixScalar, typename VectorScalar>
2324 const size_type i,
2325 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2326 const Vector<VectorScalar> &local_vector,
2327 const std::vector<size_type> &local_dof_indices,
2328 const FullMatrix<MatrixScalar> &local_matrix) const;
2329};
2330
2331/* ---------------- template and inline functions ----------------- */
2332
2333template <typename number>
2337
2338
2339
2340template <typename number>
2342 const IndexSet &locally_stored_constraints)
2343 : AffineConstraints<number>(locally_stored_constraints,
2344 locally_stored_constraints)
2345{}
2346
2347
2348
2349template <typename number>
2352 const IndexSet &locally_stored_constraints)
2353 : lines()
2355 , local_lines(locally_stored_constraints)
2356 , sorted(false)
2357{
2358 Assert(locally_owned_dofs.is_subset_of(locally_stored_constraints),
2359 ExcMessage("The set of locally stored constraints needs to be a "
2360 "superset of the locally owned DoFs."));
2361
2362 // make sure the IndexSet is compressed. Otherwise this can lead to crashes
2363 // that are hard to find (only happen in release mode).
2364 // see tests/mpi/affine_constraints_crash_01
2366}
2367
2368
2369
2370template <typename number>
2372 const AffineConstraints &affine_constraints)
2374 , lines(affine_constraints.lines)
2375 , lines_cache(affine_constraints.lines_cache)
2376 , locally_owned_dofs(affine_constraints.locally_owned_dofs)
2377 , local_lines(affine_constraints.local_lines)
2379 affine_constraints.needed_elements_for_distribute)
2380 , sorted(affine_constraints.sorted)
2381{}
2382
2383
2384
2385template <typename number>
2386inline void
2388{
2389 Assert(sorted == false, ExcMatrixIsClosed());
2390
2391 // the following can happen when we compute with distributed meshes and dof
2392 // handlers and we constrain a degree of freedom whose number we don't have
2393 // locally. if we don't abort here the program will try to allocate several
2394 // terabytes of memory to resize the various arrays below :-)
2396 const size_type line_index = calculate_line_index(line_n);
2397
2398 // check whether line already exists; it may, in which case we can just quit
2399 if (is_constrained(line_n))
2400 return;
2401
2402 // if necessary enlarge vector of existing entries for cache
2403 if (line_index >= lines_cache.size())
2404 lines_cache.resize(std::max(2 * static_cast<size_type>(lines_cache.size()),
2405 line_index + 1),
2407
2408 // push a new line to the end of the list
2409 lines.emplace_back();
2410 lines.back().index = line_n;
2411 lines.back().inhomogeneity = 0.;
2412 lines_cache[line_index] = lines.size() - 1;
2413}
2414
2415
2416
2417template <typename number>
2418inline void
2420 const size_type column,
2421 const number weight)
2422{
2423 Assert(sorted == false, ExcMatrixIsClosed());
2424 Assert(constrained_dof_index != column,
2425 ExcMessage("Can't constrain a degree of freedom to itself"));
2426
2427 // Ensure that the current line is present in the cache:
2428 const size_type line_index = calculate_line_index(constrained_dof_index);
2429 Assert(line_index < lines_cache.size(),
2430 ExcMessage("The current AffineConstraints does not contain the line "
2431 "for the current entry. Call AffineConstraints::add_line "
2432 "before calling this function."));
2433
2434 // if in debug mode, check whether an entry for this column already exists
2435 // and if it's the same as the one entered at present
2436 //
2437 // in any case: exit the function if an entry for this column already
2438 // exists, since we don't want to enter it twice
2442 ExcColumnNotStoredHere(constrained_dof_index, column));
2443 ConstraintLine *line_ptr = &lines[lines_cache[line_index]];
2444 Assert(line_ptr->index == constrained_dof_index, ExcInternalError());
2445 for (const auto &p : line_ptr->entries)
2446 if (p.first == column)
2447 {
2448 Assert(std::abs(p.second - weight) < 1.e-14,
2450 constrained_dof_index, column, p.second, weight));
2451 return;
2452 }
2453
2454 line_ptr->entries.emplace_back(column, weight);
2455}
2456
2457
2458
2459template <typename number>
2460inline void
2462 const size_type constrained_dof_index,
2463 const number value)
2464{
2465 const size_type line_index = calculate_line_index(constrained_dof_index);
2466 Assert(line_index < lines_cache.size() &&
2468 ExcMessage("call add_line() before calling set_inhomogeneity()"));
2469 Assert(lines_cache[line_index] < lines.size(), ExcInternalError());
2470 ConstraintLine *line_ptr = &lines[lines_cache[line_index]];
2471 line_ptr->inhomogeneity = value;
2472}
2473
2474
2475
2476template <typename number>
2477template <typename VectorType>
2478inline void
2480{
2481 // since lines is a private member, we cannot pass it to the functions
2482 // above. therefore, copy the content which is cheap
2483 std::vector<size_type> constrained_lines(lines.size());
2484 for (unsigned int i = 0; i < lines.size(); ++i)
2485 constrained_lines[i] = lines[i].index;
2486 internal::AffineConstraintsImplementation::set_zero_all(constrained_lines,
2487 vec);
2488}
2489
2490template <typename number>
2493{
2494 return lines.size();
2495}
2496
2497template <typename number>
2500{
2501 return std::count_if(lines.begin(),
2502 lines.end(),
2503 [](const ConstraintLine &line) {
2504 return (line.entries.size() == 1) &&
2505 (line.entries[0].second == number(1.));
2506 });
2507}
2508
2509template <typename number>
2512{
2513 return std::count_if(lines.begin(),
2514 lines.end(),
2515 [](const ConstraintLine &line) {
2516 return (line.inhomogeneity != number(0.));
2517 });
2518}
2519
2520template <typename number>
2521inline bool
2523{
2524 if (lines.empty())
2525 return false;
2526
2527 const size_type line_index = calculate_line_index(index);
2528 return ((line_index < lines_cache.size()) &&
2529 (lines_cache[line_index] != numbers::invalid_size_type));
2530}
2531
2532template <typename number>
2533inline bool
2535 const size_type line_n) const
2536{
2537 // check whether the entry is constrained. could use is_constrained, but
2538 // that means computing the line index twice
2539 const size_type line_index = calculate_line_index(line_n);
2540 if (line_index >= lines_cache.size() ||
2542 return false;
2543 else
2544 {
2545 Assert(lines_cache[line_index] < lines.size(), ExcInternalError());
2546 return (lines[lines_cache[line_index]].inhomogeneity != number(0.));
2547 }
2548}
2549
2550template <typename number>
2551inline const std::vector<std::pair<types::global_dof_index, number>> *
2553{
2554 if (lines.empty())
2555 return nullptr;
2556
2557 // check whether the entry is constrained. could use is_constrained, but
2558 // that means computing the line index twice
2559 const size_type line_index = calculate_line_index(line_n);
2560 if (line_index >= lines_cache.size() ||
2562 return nullptr;
2563 else
2564 return &lines[lines_cache[line_index]].entries;
2565}
2566
2567template <typename number>
2568inline number
2570{
2571 // check whether the entry is constrained. could use is_constrained, but
2572 // that means computing the line index twice
2573 const size_type line_index = calculate_line_index(line_n);
2574 if (line_index >= lines_cache.size() ||
2576 return 0;
2577 else
2578 return lines[lines_cache[line_index]].inhomogeneity;
2579}
2580
2581template <typename number>
2584{
2585 // IndexSet is unused (serial case)
2586 if (local_lines.size() == 0)
2587 return line_n;
2588
2590
2591 return local_lines.index_within_set(line_n);
2592}
2593
2594
2595
2596template <typename number>
2597inline bool
2599{
2600 return local_lines.size() == 0 || local_lines.is_element(line_n);
2601}
2602
2603
2604
2605template <typename number>
2606inline const IndexSet &
2611
2612
2613
2614template <typename number>
2615inline const IndexSet &
2620
2621
2622
2623template <typename number>
2624template <typename VectorType>
2625inline void
2627 const size_type index,
2628 const number value,
2629 VectorType &global_vector) const
2630{
2631 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2632
2633 if (is_constrained(index) == false)
2634 global_vector(index) += value;
2635 else
2636 {
2637 const ConstraintLine &position =
2639 for (size_type j = 0; j < position.entries.size(); ++j)
2640 global_vector(position.entries[j].first) +=
2641 value * position.entries[j].second;
2642 }
2643}
2644
2645template <typename number>
2646template <typename ForwardIteratorVec,
2647 typename ForwardIteratorInd,
2648 typename VectorType>
2649inline void
2651 ForwardIteratorVec local_vector_begin,
2652 ForwardIteratorVec local_vector_end,
2653 ForwardIteratorInd local_indices_begin,
2654 VectorType &global_vector) const
2655{
2656 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2657 for (; local_vector_begin != local_vector_end;
2658 ++local_vector_begin, ++local_indices_begin)
2659 {
2660 if (is_constrained(*local_indices_begin) == false)
2661 internal::ElementAccess<VectorType>::add(*local_vector_begin,
2662 *local_indices_begin,
2663 global_vector);
2664 else
2665 {
2666 const ConstraintLine &position =
2667 lines[lines_cache[calculate_line_index(*local_indices_begin)]];
2668 for (size_type j = 0; j < position.entries.size(); ++j)
2670 (*local_vector_begin) * position.entries[j].second,
2671 position.entries[j].first,
2672 global_vector);
2673 }
2674 }
2675}
2676
2677template <typename number>
2678template <class InVector, class OutVector>
2679inline void
2681 const InVector &local_vector,
2682 const std::vector<size_type> &local_dof_indices,
2683 OutVector &global_vector) const
2684{
2685 Assert(global_vector.has_ghost_elements() == false, ExcGhostsPresent());
2686 Assert(local_vector.size() == local_dof_indices.size(),
2687 ExcDimensionMismatch(local_vector.size(), local_dof_indices.size()));
2688 distribute_local_to_global(local_vector.begin(),
2689 local_vector.end(),
2690 local_dof_indices.begin(),
2691 global_vector);
2692}
2693
2694template <typename number>
2695template <typename ForwardIteratorVec,
2696 typename ForwardIteratorInd,
2697 typename VectorType>
2698inline void
2700 const VectorType &global_vector,
2701 ForwardIteratorInd local_indices_begin,
2702 ForwardIteratorVec local_vector_begin,
2703 ForwardIteratorVec local_vector_end) const
2704{
2705 Assert(lines.empty() || sorted == true, ExcMatrixNotClosed());
2706 for (; local_vector_begin != local_vector_end;
2707 ++local_vector_begin, ++local_indices_begin)
2708 {
2709 if (is_constrained(*local_indices_begin) == false)
2710 *local_vector_begin = global_vector(*local_indices_begin);
2711 else
2712 {
2713 const ConstraintLine &position =
2714 lines[lines_cache[calculate_line_index(*local_indices_begin)]];
2715 typename VectorType::value_type value = position.inhomogeneity;
2716 for (size_type j = 0; j < position.entries.size(); ++j)
2717 value += (global_vector(position.entries[j].first) *
2718 position.entries[j].second);
2719 *local_vector_begin = value;
2720 }
2721 }
2722}
2723
2724// Forward declarations
2725#ifndef DOXYGEN
2726template <typename MatrixType>
2727class BlockMatrixBase;
2728template <typename SparsityPatternType>
2730template <typename number>
2732
2733namespace internal
2734{
2735 namespace AffineConstraints
2736 {
2754 template <typename MatrixType>
2755 struct IsBlockMatrix
2756 {
2757 private:
2763 template <typename T>
2764 static std::true_type
2765 check(const BlockMatrixBase<T> *);
2766
2771 template <typename T>
2772 static std::true_type
2773 check(const BlockSparseMatrixEZ<T> *);
2774
2779 static std::false_type
2780 check(...);
2781
2782 public:
2789 static const bool value =
2790 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2791 std::true_type>;
2792 };
2793
2794 // instantiation of the static member
2795 template <typename MatrixType>
2796 const bool IsBlockMatrix<MatrixType>::value;
2797
2798 } // namespace AffineConstraints
2799} // namespace internal
2800#endif
2801
2802
2803
2804template <typename number>
2805template <typename other_number>
2806inline void
2809{
2810 lines.clear();
2811 lines.reserve(other.lines.size());
2812
2813 for (const auto &l : other.lines)
2814 lines.emplace_back(l.index,
2815 typename ConstraintLine::Entries(l.entries.begin(),
2816 l.entries.end()),
2817 l.inhomogeneity);
2818
2819 lines_cache = other.lines_cache;
2820 local_lines = other.local_lines;
2821 sorted = other.sorted;
2822
2823 locally_owned_dofs = other.locally_owned_dofs;
2824 needed_elements_for_distribute = other.needed_elements_for_distribute;
2825}
2826
2827
2828template <typename number>
2829template <typename other_number>
2830void
2832 const AffineConstraints<other_number> &other_constraints,
2833 const MergeConflictBehavior merge_conflict_behavior,
2834 const bool allow_different_local_lines)
2835{
2836 (void)allow_different_local_lines;
2837 Assert(allow_different_local_lines ||
2838 local_lines == other_constraints.local_lines,
2839 ExcMessage(
2840 "local_lines for this and the other objects are not the same "
2841 "although allow_different_local_lines is false."));
2842
2843 // store the previous state with respect to sorting
2844 const bool object_was_sorted = sorted;
2845 sorted = false;
2846
2847 // first action is to fold into the present object possible constraints
2848 // in the second object. we don't strictly need to do this any more since
2849 // the AffineConstraints container has learned to deal with chains of
2850 // constraints in the close() function, but we have traditionally done
2851 // this and it's not overly hard to do.
2852 //
2853 // for this, loop over all constraints and replace the constraint lines
2854 // with a new one where constraints are replaced if necessary.
2855 typename ConstraintLine::Entries tmp;
2856 for (ConstraintLine &line : lines)
2857 {
2858 tmp.clear();
2859 for (const std::pair<size_type, number> &entry : line.entries)
2860 {
2861 // if the present dof is not stored, or not constrained, or if we
2862 // won't take the constraint from the other object, then simply copy
2863 // it over
2864 if ((other_constraints.local_lines.size() != 0. &&
2865 other_constraints.local_lines.is_element(entry.first) ==
2866 false) ||
2867 other_constraints.is_constrained(entry.first) == false ||
2868 ((merge_conflict_behavior != right_object_wins) &&
2869 other_constraints.is_constrained(entry.first) &&
2870 this->is_constrained(entry.first)))
2871 tmp.push_back(entry);
2872 else
2873 // otherwise resolve further constraints by replacing the old
2874 // entry by a sequence of new entries taken from the other
2875 // object, but with multiplied weights
2876 {
2877 const auto *other_entries =
2878 other_constraints.get_constraint_entries(entry.first);
2879 Assert(other_entries != nullptr, ExcInternalError());
2880
2881 const number weight = entry.second;
2882
2883 for (const auto &other_entry : *other_entries)
2884 tmp.emplace_back(other_entry.first,
2885 other_entry.second * weight);
2886
2887 line.inhomogeneity +=
2888 other_constraints.get_inhomogeneity(entry.first) * weight;
2889 }
2890 }
2891 // finally exchange old and newly resolved line
2892 line.entries.swap(tmp);
2893 }
2894
2895 if (local_lines.size() != 0)
2896 local_lines.add_indices(other_constraints.local_lines);
2897
2898 {
2899 // do not bother to resize the lines cache exactly since it is pretty
2900 // cheap to adjust it along the way.
2901 std::fill(lines_cache.begin(),
2902 lines_cache.end(),
2904
2905 // reset lines_cache for our own constraints
2906 size_type index = 0;
2907 for (const ConstraintLine &line : lines)
2908 {
2909 const size_type local_line_no = calculate_line_index(line.index);
2910 if (local_line_no >= lines_cache.size())
2911 lines_cache.resize(local_line_no + 1, numbers::invalid_size_type);
2912 lines_cache[local_line_no] = index++;
2913 }
2914
2915 // Add other_constraints to lines cache and our list of constraints
2916 for (const auto &line : other_constraints.lines)
2917 {
2918 const size_type local_line_no = calculate_line_index(line.index);
2919 if (local_line_no >= lines_cache.size())
2920 {
2921 lines_cache.resize(local_line_no + 1, numbers::invalid_size_type);
2922 lines.emplace_back(line.index,
2923 typename ConstraintLine::Entries(
2924 line.entries.begin(), line.entries.end()),
2925 line.inhomogeneity);
2926 lines_cache[local_line_no] = index++;
2927 }
2928 else if (lines_cache[local_line_no] == numbers::invalid_size_type)
2929 {
2930 // there are no constraints for that line yet
2931 lines.emplace_back(line.index,
2932 typename ConstraintLine::Entries(
2933 line.entries.begin(), line.entries.end()),
2934 line.inhomogeneity);
2935 AssertIndexRange(local_line_no, lines_cache.size());
2936 lines_cache[local_line_no] = index++;
2937 }
2938 else
2939 {
2940 // we already store that line
2941 switch (merge_conflict_behavior)
2942 {
2943 case no_conflicts_allowed:
2944 AssertThrow(false,
2945 ExcDoFIsConstrainedFromBothObjects(line.index));
2946 break;
2947
2948 case left_object_wins:
2949 // ignore this constraint
2950 break;
2951
2952 case right_object_wins:
2953 AssertIndexRange(local_line_no, lines_cache.size());
2954 lines[lines_cache[local_line_no]] = {
2955 line.index,
2956 typename ConstraintLine::Entries(line.entries.begin(),
2957 line.entries.end()),
2958 static_cast<number>(line.inhomogeneity)};
2959 break;
2960
2961 default:
2963 }
2964 }
2965 }
2966
2967 // check that we set the pointers correctly
2968 for (size_type i = 0; i < lines_cache.size(); ++i)
2969 if (lines_cache[i] != numbers::invalid_size_type)
2970 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
2972 }
2973
2974 // if the object was sorted before, then make sure it is so afterward as
2975 // well. otherwise leave everything in the unsorted state
2976 if (object_was_sorted == true)
2977 close();
2978}
2979
2980
2981
2982template <typename number>
2983template <typename MatrixType>
2984inline void
2986 const FullMatrix<number> &local_matrix,
2987 const std::vector<size_type> &local_dof_indices,
2988 MatrixType &global_matrix) const
2989{
2990 // create a dummy and hand on to the function actually implementing this
2991 // feature in the cm.templates.h file.
2993 distribute_local_to_global(
2994 local_matrix,
2995 dummy,
2996 local_dof_indices,
2997 global_matrix,
2998 dummy,
2999 false,
3000 std::integral_constant<
3001 bool,
3002 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3003}
3004
3005
3006
3007template <typename number>
3008template <typename MatrixType, typename VectorType>
3009inline void
3011 const FullMatrix<number> &local_matrix,
3012 const Vector<number> &local_vector,
3013 const std::vector<size_type> &local_dof_indices,
3014 MatrixType &global_matrix,
3015 VectorType &global_vector,
3016 bool use_inhomogeneities_for_rhs) const
3017{
3018 // enter the internal function with the respective block information set,
3019 // the actual implementation follows in the cm.templates.h file.
3020 distribute_local_to_global(
3021 local_matrix,
3022 local_vector,
3023 local_dof_indices,
3024 global_matrix,
3025 global_vector,
3026 use_inhomogeneities_for_rhs,
3027 std::integral_constant<
3028 bool,
3029 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3030}
3031
3032
3033
3034template <typename number>
3036 const size_type &index,
3038 const number inhomogeneity)
3039 : index(index)
3040 , entries(entries)
3041 , inhomogeneity(inhomogeneity)
3042{}
3043
3044
3045
3047
3048#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)
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
void make_consistent_in_parallel(const IndexSet &locally_owned_dofs, const IndexSet &constraints_to_make_consistent, const MPI_Comm mpi_communicator)
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:706
size_type size() const
Definition index_set.h:1776
size_type index_within_set(const size_type global_index) const
Definition index_set.h:2001
bool is_element(const size_type index) const
Definition index_set.h:1894
void compress() const
Definition index_set.h:1784
A class that provides a separate storage location on each thread that accesses the object.
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:206
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
static ::ExceptionBase & ExcMatrixNotClosed()
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition exceptions.h:580
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:534
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:489
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:511
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()
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)
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
friend void swap(ConstraintLine &l1, ConstraintLine &l2) noexcept
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)