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 amg_data(AMGData()),
59 additional_quadrature_points(1)
60 {
61 }
62
63 void
64 print(dealii::ConditionalOStream const & pcout) const
65 {
66 print_parameter(pcout, "RPE tolerance", rpe_data.tolerance);
67 print_parameter(pcout, "RPE enforce unique mapping", rpe_data.enforce_unique_mapping);
68 print_parameter(pcout, "RPE rtree level", rpe_data.rtree_level);
69
70 // These parameters play only a role if an iterative scheme is used for projection.
71 // That is for `InverseMassType != InverseMassType::MatrixfreeOperator` determined at runtime.
72 solver_data.print(pcout);
73 print_parameter(pcout, "Preconditioner", preconditioner);
74 amg_data.print(pcout);
75 }
76
77 typename dealii::Utilities::MPI::RemotePointEvaluation<dim>::AdditionalData rpe_data;
78 SolverData solver_data;
79 PreconditionerMass preconditioner;
80 AMGData amg_data;
81
82 // Number of additional integration points used for sampling the source grid.
83 // The default `additional_quadrature_points = 1` considers `fe_degree + 1` quadrature points in
84 // 1D using the `fe_degree` of the target grid's finite element.
85 unsigned int additional_quadrature_points;
86};
87
92template<int dim, int n_components, typename Number>
93std::vector<dealii::Point<dim>>
94collect_integration_points(
95 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & matrix_free,
96 unsigned int const dof_index,
97 unsigned int const quad_index)
98{
99 CellIntegrator<dim, n_components, Number> fe_eval(matrix_free, dof_index, quad_index);
100
101 // Conservative estimate for the number of points.
102 std::vector<dealii::Point<dim>> integration_points;
103 integration_points.reserve(
104 matrix_free.get_dof_handler(dof_index).get_triangulation().n_active_cells() *
105 fe_eval.n_q_points);
106
107 for(unsigned int cell_batch_idx = 0; cell_batch_idx < matrix_free.n_cell_batches();
108 ++cell_batch_idx)
109 {
110 fe_eval.reinit(cell_batch_idx);
111 for(const unsigned int q : fe_eval.quadrature_point_indices())
112 {
113 dealii::Point<dim, dealii::VectorizedArray<Number>> const cell_batch_points =
114 fe_eval.quadrature_point(q);
115 for(unsigned int i = 0; i < dealii::VectorizedArray<Number>::size(); ++i)
116 {
117 dealii::Point<dim> p;
118 for(unsigned int d = 0; d < dim; ++d)
119 {
120 p[d] = cell_batch_points[d][i];
121 }
122 integration_points.push_back(p);
123 }
124 }
125 }
126
127 return integration_points;
128}
129
134template<int dim, int n_components, typename Number, typename VectorType>
135VectorType
136assemble_projection_rhs(
137 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & matrix_free,
138 std::vector<
139 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type> const &
140 values_source_in_q_points_target,
141 unsigned int const dof_index,
142 unsigned int const quad_index)
143{
144 VectorType system_rhs;
145 matrix_free.initialize_dof_vector(system_rhs, dof_index);
146
147 CellIntegrator<dim, n_components, Number> fe_eval(matrix_free, dof_index, quad_index);
148
149 unsigned int idx_q_point = 0;
150
151 for(unsigned int cell_batch_idx = 0; cell_batch_idx < matrix_free.n_cell_batches();
152 ++cell_batch_idx)
153 {
154 fe_eval.reinit(cell_batch_idx);
155 for(unsigned int const q : fe_eval.quadrature_point_indices())
156 {
157 dealii::Tensor<1, n_components, dealii::VectorizedArray<Number>> tmp;
158
159 for(unsigned int i = 0; i < dealii::VectorizedArray<Number>::size(); ++i)
160 {
161 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type const
162 values = values_source_in_q_points_target[idx_q_point];
163
164 // Increment index into `values_source_in_q_points_target`, meaning that the sequence of
165 // function values in integration points need to match the particular sequence here.
166 ++idx_q_point;
167
168 if constexpr(n_components == 1)
169 {
170 tmp[0][i] = values;
171 }
172 else
173 {
174 for(unsigned int c = 0; c < n_components; ++c)
175 {
176 tmp[c][i] = values[c];
177 }
178 }
179 }
180
181 fe_eval.submit_value(tmp, q);
182 }
183 fe_eval.integrate(dealii::EvaluationFlags::values);
184 fe_eval.distribute_local_to_global(system_rhs);
185 }
186 system_rhs.compress(dealii::VectorOperation::add);
187
188 return system_rhs;
189}
190
195template<int dim, typename Number, int n_components, typename VectorType>
196void
197project_vectors(
198 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
199 dealii::DoFHandler<dim> const & source_dof_handler,
200 std::vector<VectorType *> const & source_vectors,
201 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & target_matrix_free,
202 std::vector<VectorType *> const & target_vectors,
203 unsigned int const dof_index,
204 unsigned int const quad_index,
206{
207 // Setup inverse mass operator.
208 InverseMassOperatorData<Number> inverse_mass_operator_data;
209 inverse_mass_operator_data.dof_index = dof_index;
210 inverse_mass_operator_data.quad_index = quad_index;
211 inverse_mass_operator_data.parameters.preconditioner = data.preconditioner;
212 inverse_mass_operator_data.parameters.solver_data = data.solver_data;
213 inverse_mass_operator_data.parameters.amg_data = data.amg_data;
214 inverse_mass_operator_data.parameters.implementation_type =
215 InverseMassOperatorData<Number>::get_optimal_inverse_mass_type(
216 target_matrix_free.get_dof_handler(dof_index).get_fe());
217
218 InverseMassOperator<dim, n_components, Number> inverse_mass_operator;
219 inverse_mass_operator.initialize(target_matrix_free, inverse_mass_operator_data);
220
221 dealii::Utilities::MPI::RemotePointEvaluation<dim> rpe_source(data.rpe_data);
222
223 // The sequence of integration points follows from the sequence of points as encountered during
224 // cell batch loop.
225 std::vector<dealii::Point<dim>> integration_points_target =
226 collect_integration_points<dim, n_components, Number>(target_matrix_free,
227 dof_index,
228 quad_index);
229
230 rpe_source.reinit(integration_points_target,
231 source_dof_handler.get_triangulation(),
232 *source_mapping);
233
234 if(not rpe_source.all_points_found())
235 {
236 write_points_in_dummy_triangulation(
237 integration_points_target, "./", "points_all", 0, source_dof_handler.get_mpi_communicator());
238
239 std::vector<dealii::Point<dim>> points_not_found;
240 points_not_found.reserve(integration_points_target.size());
241 for(unsigned int i = 0; i < integration_points_target.size(); ++i)
242 {
243 if(not rpe_source.point_found(i))
244 {
245 points_not_found.push_back(integration_points_target[i]);
246 }
247 }
248
249 write_points_in_dummy_triangulation(
250 points_not_found, "./", "points_not_found", 0, source_dof_handler.get_mpi_communicator());
251
252 AssertThrow(
253 rpe_source.all_points_found(),
254 dealii::ExcMessage(
255 "Could not interpolate source grid vector in target grid. "
256 "Points exported to `./points_all_points.pvtu` and `./points_not_found_points.pvtu`"));
257 }
258
259 // Loop over vectors and project.
260 for(unsigned int i = 0; i < target_vectors.size(); ++i)
261 {
262 // Evaluate the source vector at the target integration points.
263 VectorType const & source_vector = *source_vectors.at(i);
264 source_vector.update_ghost_values();
265
266 std::vector<
267 typename dealii::FEPointEvaluation<n_components, dim, dim, Number>::value_type> const
268 values_source_in_q_points_target = dealii::VectorTools::point_values<n_components>(
269 rpe_source, source_dof_handler, source_vector, dealii::VectorTools::EvaluationFlags::avg);
270
271 // Assemble right hand side vector for the projection.
272 VectorType system_rhs = assemble_projection_rhs<dim, n_components, Number, VectorType>(
273 target_matrix_free, values_source_in_q_points_target, dof_index, quad_index);
274
275 // Solve linear system starting from zero initial guess.
276 VectorType sol;
277 sol.reinit(system_rhs, false /* omit_zeroing_entries */);
278
279 inverse_mass_operator.apply(sol, system_rhs);
280
281 // Copy solution to target vector.
282 *target_vectors[i] = sol;
283 }
284}
285
291template<int dim, typename Number, typename VectorType>
292void
293do_grid_to_grid_projection(
294 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
295 std::vector<dealii::DoFHandler<dim> const *> const & source_dof_handlers,
296 std::vector<std::vector<VectorType *>> const & source_vectors_per_dof_handler,
297 std::vector<dealii::DoFHandler<dim> const *> const & target_dof_handlers,
298 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> const & target_matrix_free,
299 std::vector<std::vector<VectorType *>> & target_vectors_per_dof_handler,
301{
302 // Check input dimensions.
303 AssertThrow(source_vectors_per_dof_handler.size() == source_dof_handlers.size(),
304 dealii::ExcMessage("First dimension of source vector of vectors "
305 "has to match source DoFHandler count."));
306 AssertThrow(target_vectors_per_dof_handler.size() == target_dof_handlers.size(),
307 dealii::ExcMessage("First dimension of target vector of vectors "
308 "has to match target DoFHandler count."));
309 AssertThrow(source_dof_handlers.size() == target_dof_handlers.size(),
310 dealii::ExcMessage("Target and source DoFHandler counts have to match"));
311 AssertThrow(source_vectors_per_dof_handler.size() > 0,
312 dealii::ExcMessage("Vector of source vectors empty."));
313 for(unsigned int i = 0; i < source_vectors_per_dof_handler.size(); ++i)
314 {
315 AssertThrow(source_vectors_per_dof_handler[i].size() ==
316 target_vectors_per_dof_handler.at(i).size(),
317 dealii::ExcMessage("Vectors of source and target vectors need to have same size."));
318 }
319
320 // Project vectors per `dealii::DoFHandler`.
321 for(unsigned int i = 0; i < target_dof_handlers.size(); ++i)
322 {
323 unsigned int const n_components = target_dof_handlers[i]->get_fe().n_components();
324 if(n_components == 1)
325 {
326 project_vectors<dim, Number, 1 /* n_components */, VectorType>(
327 source_mapping,
328 *source_dof_handlers.at(i),
329 source_vectors_per_dof_handler.at(i),
330 target_matrix_free,
331 target_vectors_per_dof_handler.at(i),
332 i /* dof_index */,
333 i /* quad_index */,
334 data);
335 }
336 else if(n_components == dim)
337 {
338 project_vectors<dim, Number, dim /* n_components */, VectorType>(
339 source_mapping,
340 *source_dof_handlers.at(i),
341 source_vectors_per_dof_handler.at(i),
342 target_matrix_free,
343 target_vectors_per_dof_handler.at(i),
344 i /* dof_index */,
345 i /* quad_index */,
346 data);
347 }
348 else if(n_components == dim + 2)
349 {
350 project_vectors<dim, Number, dim + 2 /* n_components */, VectorType>(
351 source_mapping,
352 *source_dof_handlers.at(i),
353 source_vectors_per_dof_handler.at(i),
354 target_matrix_free,
355 target_vectors_per_dof_handler.at(i),
356 i /* dof_index */,
357 i /* quad_index */,
358 data);
359 }
360 else
361 {
362 AssertThrow(n_components == 1 or n_components == dim,
363 dealii::ExcMessage("The requested number of components is not"
364 "supported in `grid_to_grid_projection()`."));
365 }
366 }
367}
368
372template<int dim, typename Number, typename VectorType>
373void
374grid_to_grid_projection(
375 std::shared_ptr<dealii::Mapping<dim> const> const & source_mapping,
376 std::vector<dealii::DoFHandler<dim> const *> const & source_dof_handlers,
377 std::vector<std::vector<VectorType *>> const & source_vectors_per_dof_handler,
378 std::shared_ptr<dealii::Mapping<dim> const> const & target_mapping,
379 std::vector<dealii::DoFHandler<dim> const *> const & target_dof_handlers,
380 std::vector<std::vector<VectorType *>> & target_vectors_per_dof_handler,
382{
383 // Setup a single `dealii::MatrixFree` object with multiple `dealii::DoFHandler`s.
384 MatrixFreeData<dim, Number> target_matrix_free_data;
385
386 MappingFlags mapping_flags;
387 mapping_flags.cells =
388 dealii::update_quadrature_points | dealii::update_values | dealii::update_JxW_values;
389 target_matrix_free_data.append_mapping_flags(mapping_flags);
390
391 dealii::AffineConstraints<Number> empty_constraints;
392 empty_constraints.clear();
393 empty_constraints.close();
394 for(unsigned int i = 0; i < target_dof_handlers.size(); ++i)
395 {
396 target_matrix_free_data.insert_dof_handler(target_dof_handlers[i], std::to_string(i));
397 target_matrix_free_data.insert_constraint(&empty_constraints, std::to_string(i));
398
399 ElementType element_type = get_element_type(target_dof_handlers[i]->get_triangulation());
400
401 std::shared_ptr<dealii::Quadrature<dim>> quadrature = create_quadrature<dim>(
402 element_type, target_dof_handlers[i]->get_fe().degree + data.additional_quadrature_points);
403
404 target_matrix_free_data.insert_quadrature(*quadrature, std::to_string(i));
405 }
406
407 dealii::MatrixFree<dim, Number, dealii::VectorizedArray<Number>> target_matrix_free;
408
409 target_matrix_free.reinit(*target_mapping,
410 target_matrix_free_data.get_dof_handler_vector(),
411 target_matrix_free_data.get_constraint_vector(),
412 target_matrix_free_data.get_quadrature_vector(),
413 target_matrix_free_data.data);
414
415 do_grid_to_grid_projection<dim, Number, VectorType>(source_mapping,
416 source_dof_handlers,
417 source_vectors_per_dof_handler,
418 target_dof_handlers,
419 target_matrix_free,
420 target_vectors_per_dof_handler,
421 data);
422}
423
424} // namespace GridToGridProjection
425} // namespace ExaDG
426
427#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 multigrid_parameters.h:98
Definition solution_projection_between_triangulations.h:53
Definition solver_data.h:34