deal.II version GIT relicensing-3718-gc13d52846a 2025-07-09 19:30:01+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 - 2025 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
34#include <deal.II/lac/vector.h>
35
36#include <iomanip>
37#include <limits>
38#include <vector>
39
41
42template <typename Number>
44 Number,
45 Number,
46 << "Called set_dof_values_by_interpolation(), but"
47 << " the element to be set, value " << std::setprecision(16)
48 << arg1 << ", does not match with the non-zero value "
49 << std::setprecision(16) << arg2 << " already set before.");
50
51namespace internal
52{
53#ifdef DEBUG
59 template <typename Number>
60 std::enable_if_t<!std::is_unsigned_v<Number>,
62 get_abs(const Number a)
63 {
64 return std::abs(a);
65 }
66
67 template <typename Number>
68 std::enable_if_t<std::is_unsigned_v<Number>, Number>
69 get_abs(const Number a)
70 {
71 return a;
72 }
73
74
79 template <typename T>
81 decltype(std::declval<const T>().set_ghost_state(std::declval<bool>()));
82
83 template <typename T>
84 constexpr bool has_set_ghost_state =
85 is_supported_operation<set_ghost_state_t, T>;
86
87 template <
88 typename VectorType,
89 std::enable_if_t<has_set_ghost_state<VectorType>, VectorType> * = nullptr>
90 void
91 set_ghost_state(VectorType &vector, const bool ghosted)
92 {
93 vector.set_ghost_state(ghosted);
94 }
95
96 template <
97 typename VectorType,
98 std::enable_if_t<!has_set_ghost_state<VectorType>, VectorType> * = nullptr>
99 void
100 set_ghost_state(VectorType &, const bool)
101 {
102 // serial vector: nothing to do
103 }
104#endif
105
106
111 template <int dim,
112 int spacedim,
113 bool lda,
114 class OutputVector,
115 typename number>
116 void
118 const Vector<number> &local_values,
119 OutputVector &values,
120 const bool perform_check)
121 {
122 (void)perform_check;
123
124 if constexpr (running_in_debug_mode())
125 {
126 using VectorNumber = typename OutputVector::value_type;
127 constexpr bool is_dealii_vector =
128 std::is_same_v<OutputVector, Vector<VectorNumber>> ||
129 std::is_same_v<OutputVector, BlockVector<VectorNumber>> ||
130 std::is_same_v<OutputVector,
132 std::is_same_v<OutputVector,
134 if (perform_check && is_dealii_vector)
135 {
136 const bool old_ghost_state = values.has_ghost_elements();
137 set_ghost_state(values, true);
138
139 Vector<number> local_values_old(cell.get_fe().n_dofs_per_cell());
140 cell.get_dof_values(values, local_values_old);
141
142 for (unsigned int i = 0; i < cell.get_fe().n_dofs_per_cell(); ++i)
143 {
144 // a check consistent with the one in
145 // Utilities::MPI::Partitioner::import_from_ghosted_array_finish()
146 Assert(
147 local_values_old[i] == number() ||
148 get_abs(local_values_old[i] - local_values[i]) <=
149 get_abs(local_values_old[i] + local_values[i]) * 100000. *
150 std::numeric_limits<typename numbers::NumberTraits<
151 number>::real_type>::epsilon(),
152 ExcNonMatchingElementsSetDofValuesByInterpolation<number>(
153 local_values[i], local_values_old[i]));
154 }
155
156 set_ghost_state(values, old_ghost_state);
157 }
158 }
159
160 cell.set_dof_values(local_values, values);
161 }
162
163
164 template <int dim,
165 int spacedim,
166 bool lda,
167 class OutputVector,
168 typename number>
169 void
172 const Vector<number> &local_values,
173 OutputVector &values,
174 const types::fe_index fe_index_,
175 const std::function<void(const DoFCellAccessor<dim, spacedim, lda> &cell,
176 const Vector<number> &local_values,
177 OutputVector &values)> &processor)
178 {
179 const types::fe_index fe_index =
180 (cell.get_dof_handler().has_hp_capabilities() == false &&
181 fe_index_ == numbers::invalid_fe_index) ?
183 fe_index_;
184
185 if (cell.is_active() && !cell.is_artificial())
186 {
187 if ((cell.get_dof_handler().has_hp_capabilities() == false) ||
188 // for hp-DoFHandlers, we need to require that on
189 // active cells, you either don't specify an fe_index,
190 // or that you specify the correct one
191 (fe_index == cell.active_fe_index()) ||
192 (fe_index == numbers::invalid_fe_index))
193 // simply set the values on this cell
194 processor(cell, local_values, values);
195 else
196 {
197 Assert(local_values.size() ==
198 cell.get_dof_handler().get_fe(fe_index).n_dofs_per_cell(),
199 ExcMessage("Incorrect size of local_values vector."));
200
201 FullMatrix<double> interpolation(
202 cell.get_fe().n_dofs_per_cell(),
203 cell.get_dof_handler().get_fe(fe_index).n_dofs_per_cell());
204
205 cell.get_fe().get_interpolation_matrix(
206 cell.get_dof_handler().get_fe(fe_index), interpolation);
207
208 // do the interpolation to the target space. for historical
209 // reasons, matrices are set to size 0x0 internally even if
210 // we reinit as 4x0, so we have to treat this case specially
211 Vector<number> tmp(cell.get_fe().n_dofs_per_cell());
212 if ((tmp.size() > 0) && (local_values.size() > 0))
213 interpolation.vmult(tmp, local_values);
214
215 // now set the dof values in the global vector
216 processor(cell, tmp, values);
217 }
218 }
219 else
220 // otherwise distribute them to the children
221 {
222 Assert((cell.get_dof_handler().has_hp_capabilities() == false) ||
223 (fe_index != numbers::invalid_fe_index),
225 "You cannot call this function on non-active cells "
226 "of DoFHandler objects unless you provide an explicit "
227 "finite element index because they do not have naturally "
228 "associated finite element spaces associated: degrees "
229 "of freedom are only distributed on active cells for which "
230 "the active FE index has been set."));
231
233 cell.get_dof_handler().get_fe(fe_index);
234 const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
235
236 Assert(local_values.size() == dofs_per_cell,
238 ExcVectorDoesNotMatch()));
239 Assert(values.size() == cell.get_dof_handler().n_dofs(),
241 ExcVectorDoesNotMatch()));
242
243 Vector<number> tmp(dofs_per_cell);
244
245 for (unsigned int child = 0; child < cell.n_children(); ++child)
246 {
247 if (tmp.size() > 0)
248 fe.get_prolongation_matrix(child, cell.refinement_case())
249 .vmult(tmp, local_values);
251 *cell.child(child), tmp, values, fe_index, processor);
252 }
253 }
254 }
255
256} // namespace internal
257
258
259
260template <int dim, int spacedim, bool lda>
261template <class OutputVector, typename number>
262void
264 const Vector<number> &local_values,
265 OutputVector &values,
266 const types::fe_index fe_index_,
267 const bool perform_check) const
268{
269 internal::process_by_interpolation<dim, spacedim, lda, OutputVector, number>(
270 *this,
271 local_values,
272 values,
273 fe_index_,
274 [perform_check](const DoFCellAccessor<dim, spacedim, lda> &cell,
275 const Vector<number> &local_values,
276 OutputVector &values) {
277 internal::set_dof_values(cell, local_values, values, perform_check);
278 });
279}
280
281
282template <int dim, int spacedim, bool lda>
283template <class OutputVector, typename number>
284void
287 const Vector<number> &local_values,
288 OutputVector &values,
289 const types::fe_index fe_index_) const
290{
291 internal::process_by_interpolation<dim, spacedim, lda, OutputVector, number>(
292 *this,
293 local_values,
294 values,
295 fe_index_,
297 const Vector<number> &local_values,
298 OutputVector &values) {
299 std::vector<types::global_dof_index> dof_indices(
300 cell.get_fe().n_dofs_per_cell());
301 cell.get_dof_indices(dof_indices);
303 dof_indices,
304 values);
305 });
306}
307
308
309// --------------------------------------------------------------------------
310// explicit instantiations
311#include "dofs/dof_accessor_set.inst"
312
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:35
constexpr bool running_in_debug_mode()
Definition config.h:73
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:36
#define Assert(cond, exc)
#define DeclException2(Exception2, type1, type2, outsequence)
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
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)
constexpr types::fe_index invalid_fe_index
Definition types.h:260
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned short int fe_index
Definition types.h:72