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>
584 "Use the constructor with two index set arguments.")
619 const
IndexSet &locally_stored_constraints);
657 template <typename other_number>
681 "Use the
reinit() function with two index set arguments.")
695 const
IndexSet &locally_stored_constraints);
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;
1931 std::swap(l1.index, l2.index);
1932 std::swap(l1.entries, l2.entries);
1933 std::swap(l1.inhomogeneity, l2.inhomogeneity);
1988 const IndexSet &locally_active_dofs,
1990 const bool verbose =
false)
const;
2053 const IndexSet &constraints_to_make_consistent,
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 "
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 "
2087 <<
"The specified line " << arg1 <<
" does not exist.");
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) <<
'.');
2110 <<
"You tried to constrain DoF " << arg1 <<
" to DoF " << arg2
2111 <<
", but that one is also constrained. This is not allowed!");
2119 <<
"Degree of freedom " << arg1
2120 <<
" is constrained from both object in a merge operation.");
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"
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.");
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.");
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?");
2257 internal::AffineConstraints::ScratchData<number>>
2271 template <
typename MatrixType,
typename VectorType>
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;
2285 template <
typename MatrixType,
typename VectorType>
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;
2304 internal::AffineConstraints::GlobalRowsFromLocal<number>
2305 &global_rows)
const;
2316 std::vector<size_type> &active_dofs)
const;
2321 template <
typename MatrixScalar,
typename VectorScalar>
2325 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2327 const std::vector<size_type> &local_dof_indices,
2333template <
typename number>
2340template <
typename number>
2342 const IndexSet &locally_stored_constraints)
2344 locally_stored_constraints)
2349template <
typename number>
2352 const IndexSet &locally_stored_constraints)
2359 ExcMessage(
"The set of locally stored constraints needs to be a "
2360 "superset of the locally owned DoFs."));
2370template <
typename number>
2385template <
typename number>
2409 lines.emplace_back();
2410 lines.back().index = line_n;
2411 lines.back().inhomogeneity = 0.;
2417template <
typename number>
2421 const number weight)
2424 Assert(constrained_dof_index != column,
2425 ExcMessage(
"Can't constrain a degree of freedom to itself"));
2430 ExcMessage(
"The current AffineConstraints does not contain the line "
2431 "for the current entry. Call AffineConstraints::add_line "
2432 "before calling this function."));
2445 for (
const auto &p : line_ptr->
entries)
2446 if (p.first == column)
2450 constrained_dof_index, column, p.second, weight));
2454 line_ptr->
entries.emplace_back(column, weight);
2459template <
typename number>
2468 ExcMessage(
"call add_line() before calling set_inhomogeneity()"));
2476template <
typename number>
2477template <
typename VectorType>
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,
2490template <
typename number>
2494 return lines.size();
2497template <
typename number>
2501 return std::count_if(
lines.begin(),
2504 return (line.entries.size() == 1) &&
2505 (line.entries[0].second == number(1.));
2509template <
typename number>
2513 return std::count_if(
lines.begin(),
2516 return (line.inhomogeneity != number(0.));
2520template <
typename number>
2532template <
typename number>
2550template <
typename number>
2551inline const std::vector<std::pair<types::global_dof_index, number>> *
2567template <
typename number>
2581template <
typename number>
2596template <
typename number>
2605template <
typename number>
2614template <
typename number>
2623template <
typename number>
2624template <
typename VectorType>
2629 VectorType &global_vector)
const
2634 global_vector(index) += value;
2640 global_vector(position.
entries[j].first) +=
2641 value * position.
entries[j].second;
2645template <
typename number>
2646template <
typename ForwardIteratorVec,
2647 typename ForwardIteratorInd,
2648 typename VectorType>
2651 ForwardIteratorVec local_vector_begin,
2652 ForwardIteratorVec local_vector_end,
2653 ForwardIteratorInd local_indices_begin,
2654 VectorType &global_vector)
const
2657 for (; local_vector_begin != local_vector_end;
2658 ++local_vector_begin, ++local_indices_begin)
2662 *local_indices_begin,
2670 (*local_vector_begin) * position.
entries[j].second,
2677template <
typename number>
2678template <
class InVector,
class OutVector>
2681 const InVector &local_vector,
2682 const std::vector<size_type> &local_dof_indices,
2683 OutVector &global_vector)
const
2686 Assert(local_vector.size() == local_dof_indices.size(),
2690 local_dof_indices.begin(),
2694template <
typename number>
2695template <
typename ForwardIteratorVec,
2696 typename ForwardIteratorInd,
2697 typename VectorType>
2700 const VectorType &global_vector,
2701 ForwardIteratorInd local_indices_begin,
2702 ForwardIteratorVec local_vector_begin,
2703 ForwardIteratorVec local_vector_end)
const
2706 for (; local_vector_begin != local_vector_end;
2707 ++local_vector_begin, ++local_indices_begin)
2710 *local_vector_begin = global_vector(*local_indices_begin);
2715 typename VectorType::value_type value = position.
inhomogeneity;
2717 value += (global_vector(position.
entries[j].first) *
2719 *local_vector_begin = value;
2726template <
typename MatrixType>
2728template <
typename SparsityPatternType>
2730template <
typename number>
2754 template <
typename MatrixType>
2755 struct IsBlockMatrix
2763 template <
typename T>
2764 static std::true_type
2771 template <
typename T>
2772 static std::true_type
2779 static std::false_type
2789 static const bool value =
2790 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2795 template <
typename MatrixType>
2796 const bool IsBlockMatrix<MatrixType>::value;
2804template <
typename number>
2805template <
typename other_number>
2811 lines.reserve(other.
lines.size());
2813 for (
const auto &l : other.
lines)
2814 lines.emplace_back(l.index,
2828template <
typename number>
2829template <
typename other_number>
2834 const bool allow_different_local_lines)
2836 (void)allow_different_local_lines;
2837 Assert(allow_different_local_lines ||
2840 "local_lines for this and the other objects are not the same "
2841 "although allow_different_local_lines is false."));
2844 const bool object_was_sorted = sorted;
2859 for (
const std::pair<size_type, number> &entry : line.entries)
2868 ((merge_conflict_behavior != right_object_wins) &&
2870 this->is_constrained(entry.first)))
2871 tmp.push_back(entry);
2877 const auto *other_entries =
2881 const number weight = entry.second;
2883 for (
const auto &other_entry : *other_entries)
2884 tmp.emplace_back(other_entry.first,
2885 other_entry.second * weight);
2887 line.inhomogeneity +=
2892 line.entries.swap(tmp);
2895 if (local_lines.size() != 0)
2896 local_lines.add_indices(other_constraints.
local_lines);
2901 std::fill(lines_cache.begin(),
2909 const size_type local_line_no = calculate_line_index(line.index);
2910 if (local_line_no >= lines_cache.size())
2912 lines_cache[local_line_no] = index++;
2916 for (
const auto &line : other_constraints.
lines)
2918 const size_type local_line_no = calculate_line_index(line.index);
2919 if (local_line_no >= lines_cache.size())
2922 lines.emplace_back(line.index,
2924 line.entries.begin(), line.entries.end()),
2925 line.inhomogeneity);
2926 lines_cache[local_line_no] = index++;
2931 lines.emplace_back(line.index,
2933 line.entries.begin(), line.entries.end()),
2934 line.inhomogeneity);
2936 lines_cache[local_line_no] = index++;
2941 switch (merge_conflict_behavior)
2943 case no_conflicts_allowed:
2945 ExcDoFIsConstrainedFromBothObjects(line.index));
2948 case left_object_wins:
2952 case right_object_wins:
2954 lines[lines_cache[local_line_no]] = {
2957 line.entries.end()),
2958 static_cast<number
>(line.inhomogeneity)};
2968 for (
size_type i = 0; i < lines_cache.size(); ++i)
2970 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
2976 if (object_was_sorted ==
true)
2982template <
typename number>
2983template <
typename MatrixType>
2987 const std::vector<size_type> &local_dof_indices,
2988 MatrixType &global_matrix)
const
2993 distribute_local_to_global(
3000 std::integral_constant<
3002 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3007template <
typename number>
3008template <
typename MatrixType,
typename VectorType>
3013 const std::vector<size_type> &local_dof_indices,
3014 MatrixType &global_matrix,
3015 VectorType &global_vector,
3016 bool use_inhomogeneities_for_rhs)
const
3020 distribute_local_to_global(
3026 use_inhomogeneities_for_rhs,
3027 std::integral_constant<
3029 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3034template <
typename number>
3038 const number inhomogeneity)
3041 , 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)
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
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_DEPRECATED_WITH_COMMENT(comment)
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcMatrixNotClosed()
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)
static ::ExceptionBase & ExcLineInexistent(size_type arg1)
#define AssertThrow(cond, exc)
#define DEAL_II_NOT_IMPLEMENTED()
std::vector< index_type > data
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
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)