ExaDG
Loading...
Searching...
No Matches
solution_projection_between_triangulations.h
1/* ______________________________________________________________________
2 *
3 * ExaDG - High-Order Discontinuous Galerkin for the Exa-Scale
4 *
5 * Copyright (C) 2025 by the ExaDG authors
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * ______________________________________________________________________
20 */
21
22#ifndef EXADG_OPERATORS_SOLUTION_PROJECTION_BETWEEN_TRIANGULATIONS_H_
23#define EXADG_OPERATORS_SOLUTION_PROJECTION_BETWEEN_TRIANGULATIONS_H_
24
25// C/C++
26#include <algorithm>
27
28// deal.II
29#include <deal.II/base/conditional_ostream.h>
30#include <deal.II/base/mpi_remote_point_evaluation.h>
31#include <deal.II/base/timer.h>
32#include <deal.II/matrix_free/fe_point_evaluation.h>
33#include <deal.II/matrix_free/matrix_free.h>
34#include <deal.II/numerics/vector_tools.h>
35
36// ExaDG
37#include <exadg/grid/grid_data.h>
38#include <exadg/matrix_free/integrators.h>
39#include <exadg/matrix_free/matrix_free_data.h>
40#include <exadg/operators/inverse_mass_operator.h>
41#include <exadg/operators/inverse_mass_parameters.h>
42#include <exadg/operators/mapping_flags.h>
43#include <exadg/operators/quadrature.h>
44#include <exadg/postprocessor/write_output.h>
45
46namespace ExaDG
47{
48namespace GridToGridProjection
49{
50// Parameters controlling the grid-to-grid projection.
51template<int dim>
52struct GridToGridProjectionData
53{
54 GridToGridProjectionData()
55 : rpe_data(),
56 solver_data(),
57 preconditioner(PreconditionerMass::PointJacobi),
58 additional_quadrature_points(1)
59 {
60 }
61
62 void
63 print(dealii::ConditionalOStream const & pcout) const
64 {
65 print_parameter(pcout, "RPE tolerance", rpe_data.tolerance);
66 print_parameter(pcout, "RPE enforce unique mapping", rpe_data.enforce_unique_mapping);
67 print_parameter(pcout, "RPE rtree level", rpe_data.rtree_level);
68 }
69
70 typename dealii::Utilities::MPI::RemotePointEvaluation<dim>::AdditionalData::AdditionalData
71 rpe_data;
72 SolverData solver_data;
73 PreconditionerMass preconditioner;
74 InverseMassType inverse_mass_type;
75
76 // Number of additional integration points used for sampling the source grid.
77 // The default `additional_quadrature_points = 1` considers `fe_degree + 1` quadrature points in
78 // 1D using the `fe_degree` of the target grid's finite element.
79 unsigned int additional_quadrature_points;
80};
81
86template<int dim, int n_components, typename Number>
87std::vector<dealii::Point<dim>>
88collect_integration_points(
89 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & matrix_free,
90 unsigned int const dof_index,
91 unsigned int const quad_index)
92{
93 CellIntegrator<dim, n_components, Number> fe_eval(matrix_free, dof_index, quad_index);
94
95 // Conservative estimate for the number of points.
96 std::vector<dealii::Point<dim>> integration_points;
97 integration_points.reserve(
98 matrix_free.get_dof_handler(dof_index).get_triangulation().n_active_cells() *
99 fe_eval.n_q_points);
100
101 for(unsigned int cell_batch_idx = 0; cell_batch_idx < matrix_free.n_cell_batches();
102 ++cell_batch_idx)
103 {
104 fe_eval.reinit(cell_batch_idx);
105 for(const unsigned int q : fe_eval.quadrature_point_indices())
106 {
107 dealii::Point<dim, dealii::VectorizedArray<Number>> const cell_batch_points =
108 fe_eval.quadrature_point(q);
109 for(unsigned int i = 0; i < dealii::VectorizedArray<Number>::size(); ++i)
110 {
111 dealii::Point<dim> p;
112 for(unsigned int d = 0; d < dim; ++d)
113 {
114 p[d] = cell_batch_points[d][i];
115 }
116 integration_points.push_back(p);
117 }
118 }
119 }
120
121 return integration_points;
122}
123
128template<int dim, int n_components, typename Number, typename VectorType>
129VectorType
130assemble_projection_rhs(
131 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & matrix_free,
132 std::vector<
133 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type> const &
134 values_source_in_q_points_target,
135 unsigned int const dof_index,
136 unsigned int const quad_index)
137{
138 VectorType system_rhs;
139 matrix_free.initialize_dof_vector(system_rhs, dof_index);
140
141 CellIntegrator<dim, n_components, Number> fe_eval(matrix_free, dof_index, quad_index);
142
143 unsigned int idx_q_point = 0;
144
145 for(unsigned int cell_batch_idx = 0; cell_batch_idx < matrix_free.n_cell_batches();
146 ++cell_batch_idx)
147 {
148 fe_eval.reinit(cell_batch_idx);
149 for(unsigned int const q : fe_eval.quadrature_point_indices())
150 {
151 dealii::Tensor<1, n_components, dealii::VectorizedArray<Number>> tmp;
152
153 for(unsigned int i = 0; i < dealii::VectorizedArray<Number>::size(); ++i)
154 {
155 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type const
156 values = values_source_in_q_points_target[idx_q_point];
157
158 // Increment index into `values_source_in_q_points_target`, meaning that the sequence of
159 // function values in integration points need to match the particular sequence here.
160 ++idx_q_point;
161
162 if constexpr(n_components == 1)
163 {
164 tmp[0][i] = values;
165 }
166 else
167 {
168 for(unsigned int c = 0; c < n_components; ++c)
169 {
170 tmp[c][i] = values[c];
171 }
172 }
173 }
174
175 fe_eval.submit_value(tmp, q);
176 }
177 fe_eval.integrate(dealii::EvaluationFlags::values);
178 fe_eval.distribute_local_to_global(system_rhs);
179 }
180 system_rhs.compress(dealii::VectorOperation::add);
181
182 return system_rhs;
183}
184
189template<int dim, typename Number, int n_components, typename VectorType>
190void
191project_vectors(
192 std::vector<VectorType *> const & source_vectors,
193 dealii::DoFHandler<dim> const & source_dof_handler,
194 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
195 std::vector<VectorType *> const & target_vectors,
196 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & target_matrix_free,
197 unsigned int const dof_index,
198 unsigned int const quad_index,
200{
201 // Setup inverse mass operator.
202 InverseMassOperatorData<Number> inverse_mass_operator_data;
203 inverse_mass_operator_data.dof_index = dof_index;
204 inverse_mass_operator_data.quad_index = quad_index;
205 inverse_mass_operator_data.parameters.preconditioner = PreconditionerMass::PointJacobi;
206 inverse_mass_operator_data.parameters.solver_data = data.solver_data;
207 inverse_mass_operator_data.parameters.implementation_type =
208 InverseMassOperatorData<Number>::template get_optimal_inverse_mass_type<dim>(
209 target_matrix_free.get_dof_handler(dof_index).get_fe(),
210 get_element_type(target_matrix_free.get_dof_handler(dof_index).get_triangulation()));
211
212 InverseMassOperator<dim, n_components, Number> inverse_mass_operator;
213 inverse_mass_operator.initialize(target_matrix_free, inverse_mass_operator_data);
214
215 dealii::Utilities::MPI::RemotePointEvaluation<dim> rpe_source(data.rpe_data);
216
217 // The sequence of integration points follows from the sequence of points as encountered during
218 // cell batch loop.
219 std::vector<dealii::Point<dim>> integration_points_target =
220 collect_integration_points<dim, n_components, Number>(target_matrix_free,
221 dof_index,
222 quad_index);
223
224 rpe_source.reinit(integration_points_target,
225 source_dof_handler.get_triangulation(),
226 *source_mapping);
227
228 if(not rpe_source.all_points_found())
229 {
230 write_points_in_dummy_triangulation(
231 integration_points_target, "./", "all_points", 0, source_dof_handler.get_mpi_communicator());
232
233 std::vector<dealii::Point<dim>> points_not_found;
234 points_not_found.reserve(integration_points_target.size());
235 for(unsigned int i = 0; i < integration_points_target.size(); ++i)
236 {
237 if(not rpe_source.point_found(i))
238 {
239 points_not_found.push_back(integration_points_target[i]);
240 }
241 }
242
243 write_points_in_dummy_triangulation(
244 points_not_found, "./", "points_not_found", 0, source_dof_handler.get_mpi_communicator());
245
246 AssertThrow(rpe_source.all_points_found(),
247 dealii::ExcMessage(
248 "Could not interpolate source grid vector in target grid. "
249 "Points exported to `./all_points.pvtu` and `./points_not_found.pvtu`"));
250 }
251
252 // Loop over vectors and project.
253 for(unsigned int i = 0; i < target_vectors.size(); ++i)
254 {
255 // Evaluate the source vector at the target integration points.
256 VectorType const & source_vector = *source_vectors.at(i);
257 source_vector.update_ghost_values();
258
259 std::vector<
260 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type> const
261 values_source_in_q_points_target = dealii::VectorTools::point_values<n_components>(
262 rpe_source, source_dof_handler, source_vector, dealii::VectorTools::EvaluationFlags::avg);
263
264 // Assemble right hand side vector for the projection.
265 VectorType system_rhs = assemble_projection_rhs<dim, n_components, Number, VectorType>(
266 target_matrix_free, values_source_in_q_points_target, dof_index, quad_index);
267
268 // Solve linear system starting from zero initial guess.
269 VectorType sol;
270 sol.reinit(system_rhs, false /* omit_zeroing_entries */);
271
272 inverse_mass_operator.apply(sol, system_rhs);
273
274 // Copy solution to target vector.
275 *target_vectors[i] = sol;
276 }
277}
278
284template<int dim, typename Number, typename VectorType>
285void
286do_grid_to_grid_projection(
287 std::vector<std::vector<VectorType *>> const & source_vectors_per_dof_handler,
288 std::vector<dealii::DoFHandler<dim> const *> const & source_dof_handlers,
289 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
290 std::vector<std::vector<VectorType *>> & target_vectors_per_dof_handler,
291 std::vector<dealii::DoFHandler<dim> const *> const & target_dof_handlers,
292 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & matrix_free,
294{
295 // Check input dimensions.
296 AssertThrow(source_vectors_per_dof_handler.size() == source_dof_handlers.size(),
297 dealii::ExcMessage("First dimension of source vector of vectors "
298 "has to match source DoFHandler count."));
299 AssertThrow(target_vectors_per_dof_handler.size() == target_dof_handlers.size(),
300 dealii::ExcMessage("First dimension of target vector of vectors "
301 "has to match target DoFHandler count."));
302 AssertThrow(source_dof_handlers.size() == target_dof_handlers.size(),
303 dealii::ExcMessage("Target and source DoFHandler counts have to match"));
304 AssertThrow(source_vectors_per_dof_handler.size() > 0,
305 dealii::ExcMessage("Vector of source vectors empty."));
306 for(unsigned int i = 0; i < source_vectors_per_dof_handler.size(); ++i)
307 {
308 AssertThrow(source_vectors_per_dof_handler[i].size() ==
309 target_vectors_per_dof_handler.at(i).size(),
310 dealii::ExcMessage("Vectors of source and target vectors need to have same size."));
311 }
312
313 // Project vectors per `dealii::DoFHandler`.
314 for(unsigned int i = 0; i < target_dof_handlers.size(); ++i)
315 {
316 unsigned int const n_components = target_dof_handlers[i]->get_fe().n_components();
317 if(n_components == 1)
318 {
319 project_vectors<dim, Number, 1 /* n_components */, VectorType>(
320 source_vectors_per_dof_handler.at(i),
321 *source_dof_handlers.at(i),
322 source_mapping,
323 target_vectors_per_dof_handler.at(i),
324 matrix_free,
325 i /* dof_index */,
326 i /* quad_index */,
327 data);
328 }
329 else if(n_components == dim)
330 {
331 project_vectors<dim, Number, dim /* n_components */, VectorType>(
332 source_vectors_per_dof_handler.at(i),
333 *source_dof_handlers.at(i),
334 source_mapping,
335 target_vectors_per_dof_handler.at(i),
336 matrix_free,
337 i /* dof_index */,
338 i /* quad_index */,
339 data);
340 }
341 else if(n_components == dim + 2)
342 {
343 project_vectors<dim, Number, dim + 2 /* n_components */, VectorType>(
344 source_vectors_per_dof_handler.at(i),
345 *source_dof_handlers.at(i),
346 source_mapping,
347 target_vectors_per_dof_handler.at(i),
348 matrix_free,
349 i /* dof_index */,
350 i /* quad_index */,
351 data);
352 }
353 else
354 {
355 AssertThrow(n_components == 1 or n_components == dim,
356 dealii::ExcMessage("The requested number of components is not"
357 "supported in `grid_to_grid_projection()`."));
358 }
359 }
360}
361
365template<int dim, typename Number, typename VectorType>
366void
367grid_to_grid_projection(
368 std::vector<std::vector<VectorType *>> const & source_vectors_per_dof_handler,
369 std::vector<dealii::DoFHandler<dim> const *> const & source_dof_handlers,
370 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
371 std::vector<std::vector<VectorType *>> & target_vectors_per_dof_handler,
372 std::vector<dealii::DoFHandler<dim> const *> const & target_dof_handlers,
373 std::shared_ptr<dealii::Mapping<dim> const> const & target_mapping,
375{
376 // Setup a single `dealii::MatrixFree` object with multiple `dealii::DoFHandler`s.
377 MatrixFreeData<dim, Number> matrix_free_data;
378
379 MappingFlags mapping_flags;
380 mapping_flags.cells =
381 dealii::update_quadrature_points | dealii::update_values | dealii::update_JxW_values;
382 matrix_free_data.append_mapping_flags(mapping_flags);
383
384 dealii::AffineConstraints<Number> empty_constraints;
385 empty_constraints.clear();
386 empty_constraints.close();
387 for(unsigned int i = 0; i < target_dof_handlers.size(); ++i)
388 {
389 matrix_free_data.insert_dof_handler(target_dof_handlers[i], std::to_string(i));
390 matrix_free_data.insert_constraint(&empty_constraints, std::to_string(i));
391
392 ElementType element_type = get_element_type(target_dof_handlers[i]->get_triangulation());
393
394 std::shared_ptr<dealii::Quadrature<dim>> quadrature = create_quadrature<dim>(
395 element_type, target_dof_handlers[i]->get_fe().degree + data.additional_quadrature_points);
396
397 matrix_free_data.insert_quadrature(*quadrature, std::to_string(i));
398 }
399
400 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> matrix_free;
401 matrix_free.reinit(*target_mapping,
402 matrix_free_data.get_dof_handler_vector(),
403 matrix_free_data.get_constraint_vector(),
404 matrix_free_data.get_quadrature_vector(),
405 matrix_free_data.data);
406
407 do_grid_to_grid_projection<dim, Number, VectorType>(source_vectors_per_dof_handler,
408 source_dof_handlers,
409 source_mapping,
410 target_vectors_per_dof_handler,
411 target_dof_handlers,
412 matrix_free,
413 data);
414}
415
416} // namespace GridToGridProjection
417} // namespace ExaDG
418
419#endif /* EXADG_OPERATORS_SOLUTION_PROJECTION_BETWEEN_TRIANGULATIONS_H_ */
Definition driver.cpp:33
std::shared_ptr< dealii::Quadrature< dim > > create_quadrature(ElementType const &element_type, unsigned int const n_q_points_1d)
Definition quadrature.h:40
ElementType get_element_type(dealii::Triangulation< dim > const &tria)
Definition grid_data.h:70
Definition solution_projection_between_triangulations.h:53
Definition solver_data.h:34