deal.II version GIT relicensing-2392-g6e04ac39aa 2025-01-20 09: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
dof_accessor_set.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2013 - 2023 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
18
19#include <deal.II/fe/fe.h>
20
22
33#include <deal.II/lac/vector.h>
34
35#include <limits>
36#include <vector>
37
39
40template <typename Number>
42 Number,
43 Number,
44 << "Called set_dof_values_by_interpolation(), but"
45 << " the element to be set, value " << std::setprecision(16)
46 << arg1 << ", does not match with the non-zero value "
47 << std::setprecision(16) << arg2 << " already set before.");
48
49namespace internal
50{
51#ifdef DEBUG
57 template <typename Number>
58 std::enable_if_t<!std::is_unsigned_v<Number>,
60 get_abs(const Number a)
61 {
62 return std::abs(a);
63 }
64
65 template <typename Number>
66 std::enable_if_t<std::is_unsigned_v<Number>, Number>
67 get_abs(const Number a)
68 {
69 return a;
70 }
71
75 template <typename VectorType>
76 constexpr bool is_dealii_vector =
77 std::is_same_v<VectorType,
79 std::is_same_v<VectorType,
81 std::is_same_v<VectorType,
83 typename VectorType::value_type>> ||
84 std::is_same_v<VectorType,
86 typename VectorType::value_type>>;
87
92 template <typename T>
94 decltype(std::declval<const T>().set_ghost_state(std::declval<bool>()));
95
96 template <typename T>
97 constexpr bool has_set_ghost_state =
98 is_supported_operation<set_ghost_state_t, T>;
99
100 template <
101 typename VectorType,
102 std::enable_if_t<has_set_ghost_state<VectorType>, VectorType> * = nullptr>
103 void
104 set_ghost_state(VectorType &vector, const bool ghosted)
105 {
106 vector.set_ghost_state(ghosted);
107 }
108
109 template <
110 typename VectorType,
111 std::enable_if_t<!has_set_ghost_state<VectorType>, VectorType> * = nullptr>
112 void
113 set_ghost_state(VectorType &, const bool)
114 {
115 // serial vector: nothing to do
116 }
117#endif
118
123 template <int dim,
124 int spacedim,
125 bool lda,
126 class OutputVector,
127 typename number>
128 void
130 const Vector<number> &local_values,
131 OutputVector &values,
132 const bool perform_check)
133 {
134 (void)perform_check;
135
136#ifdef DEBUG
137 if (perform_check && is_dealii_vector<OutputVector>)
138 {
139 const bool old_ghost_state = values.has_ghost_elements();
140 set_ghost_state(values, true);
141
142 Vector<number> local_values_old(cell.get_fe().n_dofs_per_cell());
143 cell.get_dof_values(values, local_values_old);
144
145 for (unsigned int i = 0; i < cell.get_fe().n_dofs_per_cell(); ++i)
146 {
147 // a check consistent with the one in
148 // Utilities::MPI::Partitioner::import_from_ghosted_array_finish()
149 Assert(local_values_old[i] == number() ||
150 get_abs(local_values_old[i] - local_values[i]) <=
151 get_abs(local_values_old[i] + local_values[i]) *
152 100000. *
153 std::numeric_limits<typename numbers::NumberTraits<
154 number>::real_type>::epsilon(),
155 ExcNonMatchingElementsSetDofValuesByInterpolation<number>(
156 local_values[i], local_values_old[i]));
157 }
158
159 set_ghost_state(values, old_ghost_state);
160 }
161#endif
162
163 cell.set_dof_values(local_values, values);
164 }
165
166
167 template <int dim,
168 int spacedim,
169 bool lda,
170 class OutputVector,
171 typename number>
172 void
175 const Vector<number> &local_values,
176 OutputVector &values,
177 const types::fe_index fe_index_,
178 const std::function<void(const DoFCellAccessor<dim, spacedim, lda> &cell,
179 const Vector<number> &local_values,
180 OutputVector &values)> &processor)
181 {
182 const types::fe_index fe_index =
183 (cell.get_dof_handler().has_hp_capabilities() == false &&
184 fe_index_ == numbers::invalid_fe_index) ?
186 fe_index_;
187
188 if (cell.is_active() && !cell.is_artificial())
189 {
190 if ((cell.get_dof_handler().has_hp_capabilities() == false) ||
191 // for hp-DoFHandlers, we need to require that on
192 // active cells, you either don't specify an fe_index,
193 // or that you specify the correct one
194 (fe_index == cell.active_fe_index()) ||
195 (fe_index == numbers::invalid_fe_index))
196 // simply set the values on this cell
197 processor(cell, local_values, values);
198 else
199 {
200 Assert(local_values.size() ==
201 cell.get_dof_handler().get_fe(fe_index).n_dofs_per_cell(),
202 ExcMessage("Incorrect size of local_values vector."));
203
204 FullMatrix<double> interpolation(
205 cell.get_fe().n_dofs_per_cell(),
206 cell.get_dof_handler().get_fe(fe_index).n_dofs_per_cell());
207
208 cell.get_fe().get_interpolation_matrix(
209 cell.get_dof_handler().get_fe(fe_index), interpolation);
210
211 // do the interpolation to the target space. for historical
212 // reasons, matrices are set to size 0x0 internally even if
213 // we reinit as 4x0, so we have to treat this case specially
214 Vector<number> tmp(cell.get_fe().n_dofs_per_cell());
215 if ((tmp.size() > 0) && (local_values.size() > 0))
216 interpolation.vmult(tmp, local_values);
217
218 // now set the dof values in the global vector
219 processor(cell, tmp, values);
220 }
221 }
222 else
223 // otherwise distribute them to the children
224 {
225 Assert((cell.get_dof_handler().has_hp_capabilities() == false) ||
226 (fe_index != numbers::invalid_fe_index),
228 "You cannot call this function on non-active cells "
229 "of DoFHandler objects unless you provide an explicit "
230 "finite element index because they do not have naturally "
231 "associated finite element spaces associated: degrees "
232 "of freedom are only distributed on active cells for which "
233 "the active FE index has been set."));
234
236 cell.get_dof_handler().get_fe(fe_index);
237 const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
238
239 Assert(local_values.size() == dofs_per_cell,
241 ExcVectorDoesNotMatch()));
242 Assert(values.size() == cell.get_dof_handler().n_dofs(),
244 ExcVectorDoesNotMatch()));
245
246 Vector<number> tmp(dofs_per_cell);
247
248 for (unsigned int child = 0; child < cell.n_children(); ++child)
249 {
250 if (tmp.size() > 0)
251 fe.get_prolongation_matrix(child, cell.refinement_case())
252 .vmult(tmp, local_values);
254 *cell.child(child), tmp, values, fe_index, processor);
255 }
256 }
257 }
258
259} // namespace internal
260
261
262
263template <int dim, int spacedim, bool lda>
264template <class OutputVector, typename number>
265void
267 const Vector<number> &local_values,
268 OutputVector &values,
269 const types::fe_index fe_index_,
270 const bool perform_check) const
271{
272 internal::process_by_interpolation<dim, spacedim, lda, OutputVector, number>(
273 *this,
274 local_values,
275 values,
276 fe_index_,
277 [perform_check](const DoFCellAccessor<dim, spacedim, lda> &cell,
278 const Vector<number> &local_values,
279 OutputVector &values) {
280 internal::set_dof_values(cell, local_values, values, perform_check);
281 });
282}
283
284
285template <int dim, int spacedim, bool lda>
286template <class OutputVector, typename number>
287void
290 const Vector<number> &local_values,
291 OutputVector &values,
292 const types::fe_index fe_index_) const
293{
294 internal::process_by_interpolation<dim, spacedim, lda, OutputVector, number>(
295 *this,
296 local_values,
297 values,
298 fe_index_,
300 const Vector<number> &local_values,
301 OutputVector &values) {
302 std::vector<types::global_dof_index> dof_indices(
303 cell.get_fe().n_dofs_per_cell());
304 cell.get_dof_indices(dof_indices);
306 dof_indices,
307 values);
308 });
309}
310
311
312// --------------------------------------------------------------------------
313// explicit instantiations
314#include "dof_accessor_set.inst"
315
void distribute_local_to_global(const InVector &local_vector, const std::vector< size_type > &local_dof_indices, OutVector &global_vector) const
const DoFHandler< dim, spacedim > & get_dof_handler() const
void get_dof_values(const InputVector &values, Vector< number > &local_values) const
const FiniteElement< dimension_, space_dimension_ > & get_fe() const
TriaIterator< DoFCellAccessor< dimension_, space_dimension_, level_dof_access > > child(const unsigned int i) const
void distribute_local_to_global_by_interpolation(const Vector< number > &local_values, OutputVector &values, const types::fe_index fe_index=numbers::invalid_fe_index) const
void set_dof_values(const Vector< number > &local_values, OutputVector &values) const
void set_dof_values_by_interpolation(const Vector< number > &local_values, OutputVector &values, const types::fe_index fe_index=numbers::invalid_fe_index, const bool perform_check=false) const
void get_dof_indices(std::vector< types::global_dof_index > &dof_indices) const
types::fe_index active_fe_index() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
bool has_hp_capabilities() const
types::global_dof_index n_dofs() const
unsigned int n_dofs_per_cell() const
virtual const FullMatrix< double > & get_prolongation_matrix(const unsigned int child, const RefinementCase< dim > &refinement_case=RefinementCase< dim >::isotropic_refinement) const
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
virtual size_type size() const override
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition exceptions.h:534
static ::ExceptionBase & ExcNonMatchingElementsSetDofValuesByInterpolation(Number arg1, Number arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
decltype(std::declval< const T >().set_ghost_state(std::declval< bool >())) set_ghost_state_t
constexpr bool has_set_ghost_state
constexpr bool is_dealii_vector
std::enable_if_t<!std::is_unsigned_v< Number >, typename numbers::NumberTraits< Number >::real_type > get_abs(const Number a)
void set_ghost_state(VectorType &vector, const bool ghosted)
void set_dof_values(const DoFCellAccessor< dim, spacedim, lda > &cell, const Vector< number > &local_values, OutputVector &values, const bool perform_check)
void process_by_interpolation(const DoFCellAccessor< dim, spacedim, lda > &cell, const Vector< number > &local_values, OutputVector &values, const types::fe_index fe_index_, const std::function< void(const DoFCellAccessor< dim, spacedim, lda > &cell, const Vector< number > &local_values, OutputVector &values)> &processor)
const types::fe_index invalid_fe_index
Definition types.h:255
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned short int fe_index
Definition types.h:68