15#ifndef dealii_affine_constraints_h
16#define dealii_affine_constraints_h
31#include <boost/range/iterator_range.hpp>
49template <
typename number>
51template <
typename number>
82 Distributing(
const Distributing &in);
85 operator=(
const Distributing &in);
90 return global_row < in.global_row;
111 template <
typename number>
120 insert_new_index(
const std::pair<size_type, number> &pair);
123 append_index(
const size_type index,
124 const std::pair<size_type, number> &pair);
127 get_size(
const size_type index)
const;
129 const std::pair<size_type, number> *
130 get_entry(
const size_type index)
const;
134 std::vector<std::pair<size_type, number>> data;
136 std::vector<size_type> individual_size;
169 template <
typename number>
170 class GlobalRowsFromLocal
176 GlobalRowsFromLocal();
179 reinit(
const size_type n_local_rows);
182 insert_index(
const size_type global_row,
183 const size_type local_row,
184 const number constraint_value);
189 print(std::ostream &os);
202 size(
const size_type counter_index)
const;
208 global_row(
const size_type counter_index)
const;
214 global_row(
const size_type counter_index);
222 local_row(
const size_type counter_index)
const;
228 local_row(
const size_type counter_index);
236 local_row(
const size_type counter_index,
237 const size_type index_in_constraint)
const;
244 constraint_value(
const size_type counter_index,
245 const size_type index_in_constraint)
const;
254 have_indirect_rows()
const;
261 insert_constraint(
const size_type constrained_local_dof);
269 n_constraints()
const;
276 n_inhomogeneities()
const;
285 set_ith_constraint_inhomogeneous(
const size_type i);
292 constraint_origin(size_type i)
const;
299 std::vector<Distributing> total_row_indices;
305 DataCache<number> data_cache;
339 template <
typename number>
352 ScratchData(
const ScratchData &)
364 std::vector<std::pair<size_type, size_type>> new_entries;
369 std::vector<size_type> rows;
374 std::vector<size_type> columns;
379 std::vector<number>
values;
384 std::vector<size_type> block_starts;
389 std::vector<size_type> vector_indices;
394 std::vector<number> vector_values;
399 GlobalRowsFromLocal<number> global_rows;
404 GlobalRowsFromLocal<number> global_columns;
411 namespace AffineConstraintsImplementation
413 template <
typename VectorType>
415 set_zero_all(
const std::vector<types::global_dof_index> &cm,
420 set_zero_all(
const std::vector<types::global_dof_index> &cm,
425 set_zero_all(
const std::vector<types::global_dof_index> &cm,
505template <
typename number =
double>
583 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
584 "Use the constructor with two index set arguments.")
619 const
IndexSet &locally_stored_constraints);
657 template <typename other_number>
680 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
681 "Use the
reinit() function with two index set arguments.")
695 const
IndexSet &locally_stored_constraints);
756 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT("Use
get_view() and
merge() instead.")
815 const number inhomogeneity = 0);
907 const number weight);
997 template <typename other_number>
1002 const
bool allow_different_local_lines = false);
1317 template <typename VectorType>
1327 template <typename VectorType>
1329 condense(const VectorType &vec_ghosted, VectorType &output) const;
1343 template <typename VectorType>
1351 template <typename BlockVectorType>
1361 template <typename VectorType>
1420 template <class InVector, class OutVector>
1424 OutVector &global_vector) const;
1473 template <typename VectorType>
1477 VectorType &global_vector,
1478 const
FullMatrix<number> &local_matrix) const;
1499 template <typename VectorType>
1502 const
Vector<number> &local_vector,
1505 VectorType &global_vector,
1507 bool diagonal = false) const;
1512 template <typename VectorType>
1516 VectorType &global_vector) const;
1550 template <typename ForwardIteratorVec,
1551 typename ForwardIteratorInd,
1552 typename VectorType>
1555 ForwardIteratorVec local_vector_end,
1556 ForwardIteratorInd local_indices_begin,
1557 VectorType &global_vector) const;
1610 template <typename MatrixType>
1614 MatrixType &global_matrix) const;
1643 template <typename MatrixType>
1648 MatrixType &global_matrix) const;
1666 template <typename MatrixType>
1672 MatrixType &global_matrix) const;
1694 template <typename MatrixType, typename VectorType>
1697 const
Vector<number> &local_vector,
1699 MatrixType &global_matrix,
1700 VectorType &global_vector,
1701 bool use_inhomogeneities_for_rhs = false) const;
1760 const
bool keep_constrained_entries = true,
1761 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1771 const
bool keep_constrained_entries = true,
1772 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1784 const
bool keep_constrained_entries = true,
1785 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1806 template <typename ForwardIteratorVec,
1807 typename ForwardIteratorInd,
1808 typename VectorType>
1811 ForwardIteratorInd local_indices_begin,
1812 ForwardIteratorVec local_vector_begin,
1813 ForwardIteratorVec local_vector_end) const;
1836 template <typename VectorType>
1853 using Entries = std::vector<std::pair<size_type, number>>;
1882 const number inhomogeneity = 0.0);
1918 template <
class Archive>
1922 ar &index &entries &inhomogeneity;
1988 const IndexSet &locally_active_dofs,
1990 const bool verbose =
false)
const;
1999 const IndexSet &locally_relevant_dofs,
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 "
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 "
2033 <<
"The specified line " << arg1 <<
" does not exist.");
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) <<
'.');
2056 <<
"You tried to constrain DoF " << arg1 <<
" to DoF " << arg2
2057 <<
", but that one is also constrained. This is not allowed!");
2065 <<
"Degree of freedom " << arg1
2066 <<
" is constrained from both object in a merge operation.");
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"
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.");
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.");
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?");
2203 internal::AffineConstraints::ScratchData<number>>
2217 template <
typename MatrixType,
typename VectorType>
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;
2231 template <
typename MatrixType,
typename VectorType>
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;
2250 internal::AffineConstraints::GlobalRowsFromLocal<number>
2251 &global_rows)
const;
2262 std::vector<size_type> &active_dofs)
const;
2267 template <
typename MatrixScalar,
typename VectorScalar>
2271 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2273 const std::vector<size_type> &local_dof_indices,
2279template <
typename number>
2286template <
typename number>
2288 const IndexSet &locally_stored_constraints)
2290 locally_stored_constraints)
2295template <
typename number>
2298 const IndexSet &locally_stored_constraints)
2305 ExcMessage(
"The set of locally stored constraints needs to be a "
2306 "superset of the locally owned DoFs."));
2316template <
typename number>
2331template <
typename number>
2355 lines.emplace_back();
2356 lines.back().index = line_n;
2357 lines.back().inhomogeneity = 0.;
2363template <
typename number>
2367 const number weight)
2370 Assert(constrained_dof_index != column,
2371 ExcMessage(
"Can't constrain a degree of freedom to itself"));
2376 ExcMessage(
"The current AffineConstraints does not contain the line "
2377 "for the current entry. Call AffineConstraints::add_line "
2378 "before calling this function."));
2391 for (
const auto &p : line_ptr->
entries)
2392 if (p.first == column)
2396 constrained_dof_index, column, p.second, weight));
2400 line_ptr->
entries.emplace_back(column, weight);
2405template <
typename number>
2414 ExcMessage(
"call add_line() before calling set_inhomogeneity()"));
2422template <
typename number>
2423template <
typename VectorType>
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,
2436template <
typename number>
2440 return lines.size();
2443template <
typename number>
2447 return std::count_if(
lines.begin(),
2450 return (line.entries.size() == 1) &&
2451 (line.entries[0].second == number(1.));
2455template <
typename number>
2459 return std::count_if(
lines.begin(),
2462 return (line.inhomogeneity != number(0.));
2466template <
typename number>
2478template <
typename number>
2496template <
typename number>
2497inline const std::vector<std::pair<types::global_dof_index, number>> *
2513template <
typename number>
2527template <
typename number>
2542template <
typename number>
2551template <
typename number>
2560template <
typename number>
2569template <
typename number>
2570template <
typename VectorType>
2575 VectorType &global_vector)
const
2580 global_vector(index) += value;
2586 global_vector(position.
entries[j].first) +=
2587 value * position.
entries[j].second;
2591template <
typename number>
2592template <
typename ForwardIteratorVec,
2593 typename ForwardIteratorInd,
2594 typename VectorType>
2597 ForwardIteratorVec local_vector_begin,
2598 ForwardIteratorVec local_vector_end,
2599 ForwardIteratorInd local_indices_begin,
2600 VectorType &global_vector)
const
2603 for (; local_vector_begin != local_vector_end;
2604 ++local_vector_begin, ++local_indices_begin)
2608 *local_indices_begin,
2616 (*local_vector_begin) * position.
entries[j].second,
2623template <
typename number>
2624template <
class InVector,
class OutVector>
2627 const InVector &local_vector,
2628 const std::vector<size_type> &local_dof_indices,
2629 OutVector &global_vector)
const
2632 Assert(local_vector.size() == local_dof_indices.size(),
2636 local_dof_indices.begin(),
2640template <
typename number>
2641template <
typename ForwardIteratorVec,
2642 typename ForwardIteratorInd,
2643 typename VectorType>
2646 const VectorType &global_vector,
2647 ForwardIteratorInd local_indices_begin,
2648 ForwardIteratorVec local_vector_begin,
2649 ForwardIteratorVec local_vector_end)
const
2652 for (; local_vector_begin != local_vector_end;
2653 ++local_vector_begin, ++local_indices_begin)
2656 *local_vector_begin = global_vector(*local_indices_begin);
2661 typename VectorType::value_type value = position.
inhomogeneity;
2663 value += (global_vector(position.
entries[j].first) *
2665 *local_vector_begin = value;
2672template <
typename MatrixType>
2674template <
typename SparsityPatternType>
2676template <
typename number>
2700 template <
typename MatrixType>
2701 struct IsBlockMatrix
2709 template <
typename T>
2710 static std::true_type
2717 template <
typename T>
2718 static std::true_type
2725 static std::false_type
2735 static const bool value =
2736 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2741 template <
typename MatrixType>
2742 const bool IsBlockMatrix<MatrixType>::value;
2750template <
typename number>
2751template <
typename other_number>
2757 lines.reserve(other.
lines.size());
2759 for (
const auto &l : other.
lines)
2760 lines.emplace_back(l.index,
2774template <
typename number>
2775template <
typename other_number>
2780 const bool allow_different_local_lines)
2782 (void)allow_different_local_lines;
2783 Assert(allow_different_local_lines ||
2786 "local_lines for this and the other objects are not the same "
2787 "although allow_different_local_lines is false."));
2790 const bool object_was_sorted = sorted;
2805 for (
const std::pair<size_type, number> &entry : line.entries)
2814 ((merge_conflict_behavior != right_object_wins) &&
2816 this->is_constrained(entry.first)))
2817 tmp.push_back(entry);
2823 const auto *other_entries =
2827 const number weight = entry.second;
2829 for (
const auto &other_entry : *other_entries)
2830 tmp.emplace_back(other_entry.first,
2831 other_entry.second * weight);
2833 line.inhomogeneity +=
2838 line.entries.swap(tmp);
2841 if (local_lines.size() != 0)
2842 local_lines.add_indices(other_constraints.
local_lines);
2847 std::fill(lines_cache.begin(),
2855 const size_type local_line_no = calculate_line_index(line.index);
2856 if (local_line_no >= lines_cache.size())
2858 lines_cache[local_line_no] = index++;
2862 for (
const auto &line : other_constraints.
lines)
2864 const size_type local_line_no = calculate_line_index(line.index);
2865 if (local_line_no >= lines_cache.size())
2868 lines.emplace_back(line.index,
2870 line.entries.begin(), line.entries.end()),
2871 line.inhomogeneity);
2872 lines_cache[local_line_no] = index++;
2877 lines.emplace_back(line.index,
2879 line.entries.begin(), line.entries.end()),
2880 line.inhomogeneity);
2882 lines_cache[local_line_no] = index++;
2887 switch (merge_conflict_behavior)
2889 case no_conflicts_allowed:
2891 ExcDoFIsConstrainedFromBothObjects(line.index));
2894 case left_object_wins:
2898 case right_object_wins:
2900 lines[lines_cache[local_line_no]] = {
2903 line.entries.end()),
2904 static_cast<number
>(line.inhomogeneity)};
2914 for (
size_type i = 0; i < lines_cache.size(); ++i)
2916 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
2922 if (object_was_sorted ==
true)
2928template <
typename number>
2929template <
typename MatrixType>
2933 const std::vector<size_type> &local_dof_indices,
2934 MatrixType &global_matrix)
const
2939 distribute_local_to_global(
2946 std::integral_constant<
2948 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
2953template <
typename number>
2954template <
typename MatrixType,
typename VectorType>
2959 const std::vector<size_type> &local_dof_indices,
2960 MatrixType &global_matrix,
2961 VectorType &global_vector,
2962 bool use_inhomogeneities_for_rhs)
const
2966 distribute_local_to_global(
2972 use_inhomogeneities_for_rhs,
2973 std::integral_constant<
2975 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
2980template <
typename number>
2984 const number inhomogeneity)
2987 , inhomogeneity(inhomogeneity)
IndexSet locally_owned_dofs
void add_entries(const size_type constrained_dof_index, const std::vector< std::pair< size_type, number > > &col_weight_pairs)
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
size_type index_within_set(const size_type global_index) const
bool is_element(const size_type index) const
A class that provides a separate storage location on each thread that accesses the object.
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcMatrixNotClosed()
static ::ExceptionBase & ExcLineInexistant(size_type arg1)
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
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)
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)
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)
static ::ExceptionBase & ExcEntryAlreadyExists(size_type arg1, size_type arg2, number arg3, number arg4)
static ::ExceptionBase & ExcMessage(std::string 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
const types::global_dof_index invalid_size_type
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index
*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)