include/meltpooldg/utilities/amr_indicators.templates.hpp Source File

Developer Documentation: include/meltpooldg/utilities/amr_indicators.templates.hpp Source File
Developer Documentation
amr_indicators.templates.hpp
Go to the documentation of this file.
1#include <deal.II/base/exceptions.h>
2#include <deal.II/base/tensor.h>
3#include <deal.II/base/vectorization.h>
4
5#include <deal.II/fe/fe_dgq.h>
6
11
12namespace MeltPoolDG
13{
14 template <int dim, typename number, template <typename> class BinaryOp>
15 void
17 std::unique_ptr<AMRIndicatorBase<dim, number>> &&indicator,
18 number weight)
19 {
20 indicators_and_weights.emplace_back(std::move(indicator), weight);
21 }
22
23 template <int dim, typename number, template <typename> class BinaryOp>
24 auto
26 const dealii::Triangulation<dim> &tria) -> VectorType
27 {
28 AssertThrow(not indicators_and_weights.empty(),
29 dealii::ExcMessage("No AMR indicator has been added to the indicator composite."));
30
31 auto check_partitioning = [&tria](const VectorType &indicator) {
32 Assert(
33 tria.n_active_cells() == indicator.size(),
34 dealii::ExcMessage(
35 "The indicator has returned a vector which does not has the same number of elements as the triangulation has local active cells."));
36 };
37
38 VectorType indicator = indicators_and_weights[0].indicator->compute_indicator(tria);
39 indicator *= indicators_and_weights[0].weight;
40
41 check_partitioning(indicator);
42 for (unsigned i = 1; i < this->indicators_and_weights.size(); ++i)
43 {
44 VectorType temp_indicator = indicators_and_weights[i].indicator->compute_indicator(tria);
45 check_partitioning(temp_indicator);
46 for (unsigned j = 0; j < indicator.size(); ++j)
47 indicator[j] =
48 BinaryOp<number>()(indicator[j], indicators_and_weights[i].weight * temp_indicator[j]);
49 }
50
51 return indicator;
52 }
53
54 template <int dim, typename number, template <typename> class BinaryOp>
55 bool
57 {
58 return indicators_and_weights.empty();
59 }
60
61 template <int dim, typename number, int n_dof_components, int n_indicator_components>
63 const MatrixFreeContext<dim, number> matrix_free_context,
64 const VectorType &solution,
65 const ExtractIndicatorDofValuesFunction extract_indicator_dof_values,
66 const unsigned fe_index,
67 const unsigned fe_degree)
68 : matrix_free_context(matrix_free_context)
69 , solution(solution)
70 , extract_indicator_dof_values(extract_indicator_dof_values)
71 , fe_index(fe_index)
72 {
73 AssertThrow(fe_degree > 1,
74 dealii::ExcMessage("The SSED indicator requires an fe degree greater than one."));
75
76 dealii::FullMatrix<number> transformation_matrix(fe_degree + 1);
77 dealii::FETools::get_projection_matrix(dealii::FE_DGQ<1>(fe_degree),
78 dealii::FE_DGQLegendre<1>(fe_degree),
79 transformation_matrix);
80 linearized_matrix.resize((fe_degree + 1) * (fe_degree + 1));
81 for (unsigned i = 0; i < fe_degree + 1; ++i)
82 for (unsigned j = 0; j < fe_degree + 1; ++j)
83 linearized_matrix[j + ((fe_degree + 1) * i)] = transformation_matrix[j][i];
84 }
85
86 template <int dim, typename number, int n_dof_components, int n_indicator_components>
87 dealii::Vector<number>
89 const dealii::Triangulation<dim> &tria)
90 {
91 const unsigned fe_degree =
92 matrix_free_context.mf.get_dof_handler(matrix_free_context.dof_idx).get_fe(fe_index).degree;
93
94 dealii::AlignedVector<VectorizedArrayType> indicator;
95 matrix_free_context.mf.initialize_cell_data_vector(indicator);
96
97 if (fe_degree > 1)
98 {
99 std::function<void(const dealii::MatrixFree<dim, number> &mf,
100 dealii::AlignedVector<VectorizedArrayType> &indicator,
101 const VectorType &solution,
102 const std::pair<unsigned, unsigned> &cell_range)>
103 cell_op = [&](const dealii::MatrixFree<dim, number> &mf,
104 dealii::AlignedVector<VectorizedArrayType> &indicator,
105 const VectorType &solution,
106 const std::pair<unsigned, unsigned> &cell_range) {
107 local_apply_cell(mf, indicator, solution, cell_range);
108 };
109
110 matrix_free_context.mf.cell_loop(cell_op, indicator, solution);
111 }
112
113 return VectorTools::convert_matrix_free_cell_aligned_vector_to_vector<dim, number, dim + 2>(
114 matrix_free_context, indicator, tria);
115 }
116
117 template <int dim, typename number, int n_dof_components, int n_indicator_components>
118 void
120 const dealii::MatrixFree<dim, number> &,
121 dealii::AlignedVector<VectorizedArrayType> &dst,
122 const VectorType &src,
123 const std::pair<unsigned, unsigned> &cell_range)
124 {
125 FECellIntegrator<dim, n_dof_components, number> phi_dgq(matrix_free_context.mf,
126 matrix_free_context.dof_idx,
127 matrix_free_context.quad_idx);
128 FECellIntegrator<dim, n_dof_components, number> phi_leg(matrix_free_context.mf,
129 matrix_free_context.dof_idx,
130 matrix_free_context.quad_idx);
131
132 for (unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
133 {
134 VectorizedArrayType ssed_error = 0.;
135 phi_dgq.reinit(cell);
136 phi_leg.reinit(cell);
137
138 phi_dgq.read_dof_values(src);
139
140 VectorizedArrayType cell_volume = 0.;
141 const unsigned fe_degree =
142 matrix_free_context.mf.get_dof_handler(matrix_free_context.dof_idx)
143 .get_fe(phi_dgq.get_active_fe_index())
144 .degree;
145 // transform cell solution values from modal basis to modal basis
146 dealii::internal::FEEvaluationImplBasisChange<dealii::internal::evaluate_general,
147 dealii::internal::EvaluatorQuantity::value,
148 dim,
149 0,
150 0>::template do_forward<VectorizedArrayType,
152 n_dof_components,
153 linearized_matrix,
154 phi_dgq.begin_dof_values(),
155 phi_leg.begin_dof_values(),
156 fe_degree + 1,
157 fe_degree + 1);
158
159 // only keep highest modal basis
160 for (unsigned i = 0; i < std::pow(fe_degree, dim); ++i)
161 phi_leg.submit_dof_value(dealii::Tensor<1, n_dof_components, VectorizedArrayType>(), i);
162
163 phi_leg.evaluate(dealii::EvaluationFlags::values);
164
165 // integrate over cell
166 for (const unsigned int q : phi_dgq.quadrature_point_indices())
167 {
168 const dealii::Tensor<1, n_indicator_components, VectorizedArrayType> u_q =
169 extract_indicator_dof_values(fe_evaluation_tensor_value_at_q(phi_leg, q));
170 ssed_error += phi_dgq.JxW(q) * u_q * u_q;
171 cell_volume += phi_dgq.JxW(q);
172 }
173
174 ssed_error /= cell_volume;
175 phi_dgq.set_cell_data(dst, std::sqrt(ssed_error));
176 }
177 }
178
179 template <int dim, typename number, int n_dof_components, int n_indicator_components>
181 const MatrixFreeContext<dim, number> matrix_free_context,
182 const VectorType &solution,
183 const ExtractIndicatorDofValuesFunction extract_indicator_dof_values,
184 const unsigned fe_index)
185 : matrix_free_context(matrix_free_context)
186 , solution(solution)
187 , extract_indicator_dof_values(extract_indicator_dof_values)
188 , fe_index(fe_index)
189 {
190 // Jump indicator only works for DG elements.
191 for (unsigned int sub_fe_idx = 0;
192 sub_fe_idx < matrix_free_context.mf.get_dof_handler(matrix_free_context.dof_idx)
193 .get_fe(fe_index)
194 .n_components();
195 ++sub_fe_idx)
196 AssertThrow(dynamic_cast<const dealii::FE_DGQ<dim> *>(
198 .get_fe(fe_index)
199 .get_sub_fe(sub_fe_idx, 1)),
200 dealii::ExcMessage("The jump indicator only works with DG elements."));
201 }
202
203 template <int dim, typename number, int n_dof_components, int n_indicator_components>
204 dealii::Vector<number>
206 const dealii::Triangulation<dim> &tria)
207 {
208 using LocalOpType = std::function<void(const dealii::MatrixFree<dim, number> &mf,
209 dealii::AlignedVector<VectorizedArrayType> &indicator,
210 const VectorType &solution,
211 const std::pair<unsigned, unsigned> &cell_range)>;
212
213 dealii::AlignedVector<VectorizedArrayType> indicator;
214 matrix_free_context.mf.initialize_cell_data_vector(indicator);
215
216 LocalOpType cell_op = [](const dealii::MatrixFree<dim, number> &,
217 dealii::AlignedVector<VectorizedArrayType> &,
218 const VectorType &,
219 const std::pair<unsigned, unsigned> &) {};
220
221 LocalOpType face_op = std::bind_front(
223 this);
224
225 LocalOpType boundary_face_op = [](const dealii::MatrixFree<dim, number> &,
226 dealii::AlignedVector<VectorizedArrayType> &,
227 const VectorType &,
228 const std::pair<unsigned, unsigned> &) {};
229
230 matrix_free_context.mf.loop(cell_op, face_op, boundary_face_op, indicator, solution);
231
232 return VectorTools::
233 convert_matrix_free_cell_aligned_vector_to_vector<dim, number, n_dof_components>(
234 matrix_free_context, indicator, tria);
235 }
236
237 template <int dim, typename number, int n_dof_components, int n_indicator_components>
238 void
240 const dealii::MatrixFree<dim, number> &,
241 dealii::AlignedVector<VectorizedArrayType> &dst,
242 const VectorType &src,
243 const std::pair<unsigned, unsigned> &face_range) const
244 {
245 FEFaceIntegrator<dim, n_dof_components, number> phi_m(matrix_free_context.mf,
246 true,
247 matrix_free_context.dof_idx,
248 matrix_free_context.quad_idx);
249 FEFaceIntegrator<dim, n_dof_components, number> phi_p(matrix_free_context.mf,
250 false,
251 matrix_free_context.dof_idx,
252 matrix_free_context.quad_idx);
253
254 for (unsigned face = face_range.first; face < face_range.second; ++face)
255 {
256 phi_p.reinit(face);
257 phi_p.gather_evaluate(src, dealii::EvaluationFlags::values);
258 phi_m.reinit(face);
259 phi_m.gather_evaluate(src, dealii::EvaluationFlags::values);
260
261 VectorizedArrayType face_area = 0.;
262 VectorizedArrayType face_jump_error = 0.;
263 for (const unsigned q : phi_m.quadrature_point_indices())
264 {
265 const auto w_p =
266 extract_indicator_dof_values(fe_evaluation_tensor_value_at_q(phi_p, q));
267 const auto w_m =
268 extract_indicator_dof_values(fe_evaluation_tensor_value_at_q(phi_m, q));
269
270 face_jump_error += phi_m.JxW(q) * (w_m - w_p) * (w_m - w_p);
271
272 face_area += phi_m.JxW(q);
273 }
274 constexpr int n_faces = 2 * dim;
275 VectorizedArrayType current_cell_error = phi_m.read_cell_data(dst);
276 current_cell_error += 0.25 / n_faces * std::sqrt(face_jump_error / face_area);
277 phi_m.set_cell_data(dst, current_cell_error);
278 }
279 }
280} // namespace MeltPoolDG
Definition amr_indicators.hpp:30
VectorType compute_indicator(const dealii::Triangulation< dim > &tria) override
Definition amr_indicators.templates.hpp:25
bool empty() const
Definition amr_indicators.templates.hpp:56
dealii::Vector< number > VectorType
Definition amr_indicators.hpp:54
void add_indicator(std::unique_ptr< AMRIndicatorBase< dim, number > > &&indicator, number weight=1.)
Definition amr_indicators.templates.hpp:16
Definition amr_indicators.hpp:214
dealii::Vector< number > compute_indicator(const dealii::Triangulation< dim > &tria) override
Definition amr_indicators.templates.hpp:205
const MatrixFreeContext< dim, number > matrix_free_context
Matrix free object used to compute the solution.
Definition amr_indicators.hpp:266
dealii::LinearAlgebra::distributed::Vector< number > VectorType
Definition amr_indicators.hpp:218
void local_apply_face(const dealii::MatrixFree< dim, number > &, dealii::AlignedVector< VectorizedArrayType > &dst, const VectorType &src, const std::pair< unsigned, unsigned > &face_range) const
Definition amr_indicators.templates.hpp:239
const unsigned fe_index
Relevant fe index for the fe object of the dof handler in the matrix free object.
Definition amr_indicators.hpp:276
dealii::VectorizedArray< number > VectorizedArrayType
Definition amr_indicators.hpp:216
JumpIndicator(const MatrixFreeContext< dim, number > matrix_free_context, const VectorType &solution, const ExtractIndicatorDofValuesFunction extract_indicator_dof_values, const unsigned fe_index=0)
Definition amr_indicators.templates.hpp:180
std::function< dealii::Tensor< 1, n_relevant_components, VectorizedArrayType >(const dealii::Tensor< 1, n_mf_components, VectorizedArrayType > &)> ExtractIndicatorDofValuesFunction
Definition amr_indicators.hpp:221
dealii::Vector< number > compute_indicator(const dealii::Triangulation< dim > &tria) override
Definition amr_indicators.templates.hpp:88
SSEDIndicator(const MatrixFreeContext< dim, number > matrix_free_context, const VectorType &solution, const ExtractIndicatorDofValuesFunction extract_indicator_dof_values, const unsigned fe_index, const unsigned fe_degree)
Definition amr_indicators.templates.hpp:62
dealii::VectorizedArray< number > VectorizedArrayType
Definition amr_indicators.hpp:120
dealii::LinearAlgebra::distributed::Vector< number > VectorType
Definition amr_indicators.hpp:122
void local_apply_cell(const dealii::MatrixFree< dim, number > &, dealii::AlignedVector< VectorizedArrayType > &dst, const VectorType &src, const std::pair< unsigned, unsigned > &cell_range)
Definition amr_indicators.templates.hpp:119
std::function< dealii::Tensor< 1, n_indicator_components, VectorizedArrayType >(const dealii::Tensor< 1, n_dof_components, VectorizedArrayType > &)> ExtractIndicatorDofValuesFunction
Definition amr_indicators.hpp:126
dealii::AlignedVector< VectorizedArrayType > linearized_matrix
Definition amr_indicators.hpp:188
Interface for a general preconditioner.
Definition boundary_condition_functions.hpp:17
dealii::Tensor< 1, FeEval::n_components, dealii::VectorizedArray< typename FeEval::number_type > > fe_evaluation_tensor_value_at_q(const FeEval &fe_eval, const unsigned q_index)
Definition matrix_free_util.hpp:45
dealii::FEFaceEvaluation< dim, -1, 0, n_components, number, VectorizedArrayType > FEFaceIntegrator
Definition fe_integrator.hpp:22
dealii::FEEvaluation< dim, -1, 0, n_components, number, VectorizedArrayType > FECellIntegrator
Definition fe_integrator.hpp:14
Definition matrix_free_util.hpp:21
const dealii::MatrixFree< dim, Number > & mf
Definition matrix_free_util.hpp:22
unsigned int dof_idx
Definition matrix_free_util.hpp:23