Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
cls_pde_based_interface_reinitialization.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_cls_pde_based_interface_reinitialization_h
5#define lethe_cls_pde_based_interface_reinitialization_h
6
7#include <core/pvd_handler.h>
8
12
13#include <deal.II/base/work_stream.h>
14
15#include <deal.II/distributed/tria_base.h>
16
17#include <deal.II/fe/fe_q.h>
18#include <deal.II/fe/fe_simplex_p.h>
19#include <deal.II/fe/mapping_fe.h>
20#include <deal.II/fe/mapping_q.h>
21
22#include <deal.II/grid/grid_tools.h>
23
24#include <deal.II/lac/trilinos_sparse_matrix.h>
25#include <deal.II/lac/trilinos_vector.h>
26
27#include <utility>
28
29/**
30 * @brief CLS PDE-based reinitialization solver.
31 *
32 * @tparam dim Number of dimensions of the problem.
33 */
34template <int dim>
36 : public PhysicsNonlinearSubequationsSolver<dim, GlobalVectorType>
37{
38public:
39 /**
40 * @brief Constructor of the CLS PDE-based interface reinitialization.
41 *
42 * @param[in] p_simulation_parameters Simulation parameters.
43 *
44 * @param[in] p_pcout Parallel cout used to print the information.
45 *
46 * @param[in] p_triangulation Distributed mesh information.
47 *
48 * @param[in] p_simulation_control SimulationControl object.
49 *
50 * @param[in, out] p_subequations_interface Subequations interface object used
51 * to get information from other subequations and store information from the
52 * current one.
53 */
55 const SimulationParameters<dim> &p_simulation_parameters,
56 const ConditionalOStream &p_pcout,
57 std::shared_ptr<parallel::DistributedTriangulationBase<dim>>
58 p_triangulation,
59 const std::shared_ptr<SimulationControl> &p_simulation_control,
60 CLSSubequationsInterface<dim> &p_subequations_interface)
62 p_simulation_parameters.cls_subequations_non_linear_solvers.at(
64 p_pcout)
66 , subequations_interface(p_subequations_interface)
67 , simulation_parameters(p_simulation_parameters)
68 , simulation_control(p_simulation_control)
69 , triangulation(std::move(p_triangulation))
70 , dof_handler(std::make_shared<DoFHandler<dim>>(*this->triangulation))
71 , subequation_verbosity(p_simulation_parameters.multiphysics.cls_parameters
72 .reinitialization_method.verbosity)
73 {
74 this->pcout << std::setprecision(simulation_control->get_log_precision())
75 << std::scientific;
76
77 if (this->simulation_parameters.mesh.simplex)
78 {
79 // For simplex meshes
80 this->fe = std::make_shared<FE_SimplexP<dim>>(
81 this->simulation_parameters.fem_parameters.CLS_degree);
82 this->mapping = std::make_shared<MappingFE<dim>>(*this->fe);
83 this->cell_quadrature =
84 std::make_shared<QGaussSimplex<dim>>(this->fe->degree + 1);
85 }
86 else
87 {
88 // Usual case, for quad/hex meshes
89 this->fe = std::make_shared<FE_Q<dim>>(
90 this->simulation_parameters.fem_parameters.CLS_degree);
91 this->mapping = std::make_shared<MappingQ<dim>>(this->fe->degree);
92 this->cell_quadrature =
93 std::make_shared<QGauss<dim>>(this->fe->degree + 1);
94 }
95
96 // Ensure that the shared pointer is properly allocated
97 this->present_solution = std::make_shared<GlobalVectorType>();
98 }
99
100 /**
101 * @brief Default destructor.
102 */
104
105 /**
106 * @brief Set up the DofHandler and the degree of freedom associated with
107 * the subequation.
108 */
109 void
110 setup_dofs() override;
111
112 /**
113 * @brief Solve interface PDE-based reinitialization process until one of the
114 * stop criteria is met.
115 */
116 void
117 solve() override;
118
119 /**
120 * @brief Getter method to access the private attribute evaluation_point for
121 * the subequation currently solved.
122 *
123 * @return The vector in which the evaluation is performed.
124 */
127 {
128 return this->evaluation_point;
129 }
130
131 /**
132 * @brief Getter method to access the private attribute
133 * local_evaluation_point for the subequation currently solved.
134 *
135 * @return The local evaluation point. Ghosts cells are not considered in
136 * this evaluation.
137 */
140 {
141 return this->local_evaluation_point;
142 }
143
144 /**
145 * @brief Getter method to access the private attribute
146 * newton_update for the subequation currently solved.
147 *
148 * @return The direction used to perform the newton iteration.
149 */
152 {
153 return this->newton_update;
154 }
155
156 /**
157 * @brief Getter method to access the private attribute
158 * present_solution for the subequation currently solved.
159 *
160 * @return Vector containing all the values of the solution.
161 */
164 {
165 return *this->present_solution;
166 }
167
168 /**
169 * @brief Getter method to access the private attribute
170 * system_rhs for the subequation currently solved.
171 *
172 * @return Right-hand side vector.
173 */
175 get_system_rhs() override
176 {
177 return this->system_rhs;
178 }
179
180 /**
181 * @brief Getter method to access the private attribute
182 * nonzero_constraints for the subequation currently solved.
183 *
184 * @return Container of nonzero constraints that arise from several sources such
185 * as boundary conditions and hanging nodes in the mesh. See the deal.II
186 * documentation on constraints on degrees of freedom for more information.
187 */
188 AffineConstraints<double> &
190 {
191 return this->nonzero_constraints;
192 }
193
194
195 /**
196 * @brief Output the \f$L_2\f$ and \f$L_\infty\f$ norms of the correction vector.
197 */
198 void
200 {
201 this->pcout << "\t ||dphi_reinit||_L2 = " << std::setw(6)
202 << this->newton_update.l2_norm() << std::setw(6)
203 << "\t ||dphi_reinit||_Linfty = "
204 << this->newton_update.linfty_norm() << std::endl;
205 }
206
207 /**
208 * @brief Return the metric for residual rescaling. By default, should return 1.
209 * If the rescale_residual_by_volume is set to true, the method
210 * returns the square root of the global volume of the triangulation.
211 *
212 * @return Rescale metric.
213 */
214 double
216 {
217 return simulation_parameters.linear_solver.at(PhysicsID::CLS)
218 .rescale_residual_by_volume ?
219 std::sqrt(
220 GridTools::volume(*this->triangulation, *this->mapping)) :
221 1.;
222 }
223
224private:
225 /**
226 * @brief Write parallel VTU files of quantities of interest for
227 * the PDE-based interface reinitialization process.
228 *
229 * @param[in] step Integer indicating the reinitialization step number.
230 *
231 * @note Only the reinitialization steps of the last global time step
232 * (simulation time step) ran are outputted.
233 */
234 void
235 write_output_results(const unsigned int step);
236
237 /**
238 * @brief Computes mesh-dependant diffusivity coefficient value.
239 *
240 * @param[in] min_cell_size Smallest cell's measure.
241 *
242 * @return Constant diffusivity coefficient value for a given mesh.
243 */
244 inline double
245 compute_diffusivity(const double min_cell_size) const
246 {
247 const double multiplier =
248 this->simulation_parameters.multiphysics.cls_parameters
249 .reinitialization_method.pde_based_interface_reinitialization
250 .diffusivity_multiplier;
251 const double power =
252 this->simulation_parameters.multiphysics.cls_parameters
253 .reinitialization_method.pde_based_interface_reinitialization
254 .diffusivity_power;
255 return multiplier * std::pow(min_cell_size, power);
256 }
257
258 /**
259 * @brief Computes PDE-based reinitialization time step value with
260 * user-imposed CFL and minimum cell size.
261 *
262 * @return PDE-based reinitialization time step.
263 */
264 inline double
266 {
267 // Get artificial time-step factor
268 const double dtau_factor =
269 this->simulation_parameters.multiphysics.cls_parameters
270 .reinitialization_method.pde_based_interface_reinitialization
271 .dtau_factor;
272
273 // Get the minimum cell size
274 const double h_min =
276 *this->dof_handler,
277 *this->cell_quadrature,
278 this->triangulation->get_mpi_communicator());
279
280 // Compute diffusivity
281 const double diffusivity_inv = 1.0 / compute_diffusivity(h_min);
282
283 // Compute artificial time step
284 const double dtau = h_min * h_min * diffusivity_inv * dtau_factor;
285
286 // Use the smallest of the computed artificial time step and the simulation
287 // time step for the reinitialization scheme
288 return std::min(dtau, this->simulation_control->get_time_step());
289 }
290
291 /**
292 * @brief Define the zero constraints used to solve the problem.
293 */
294 void
296
297 /**
298 * @brief Define the non zero constraints used to solve the problem.
299 */
300 void
302
303 /**
304 * @brief Sets-up the initial conditions associated with the subequation.
305 */
306 void
308
309 /**
310 * @brief Assemble the matrix.
311 */
312 void
313 assemble_system_matrix() override;
314
315 /**
316 * @brief Assemble the right-hand side (rhs).
317 */
318 void
319 assemble_system_rhs() override;
320
321 /**
322 * @brief Solve the linear system.
323 */
324 void
325 solve_linear_system() override;
326
327 /**
328 * @brief Indicate if the PDE-based reinitialization should continue by
329 * checking if at least one of the two stop criteria (steady-state criterion
330 * or maximum number of reinitialization steps) is met.
331 *
332 * @param[in] step_number PDE-based interface reinitialization step number.
333 *
334 * @return Boolean indicating if the PDE-based reinitialization should
335 * continue
336 */
337 inline bool
338 continue_iterating(const unsigned int step_number)
339 {
340 if (step_number == 1) // Initial condition
341 {
342 return (step_number <
343 this->simulation_parameters.multiphysics.cls_parameters
344 .reinitialization_method
345 .pde_based_interface_reinitialization.max_steps_number +
346 1);
347 }
348 else
349 {
350 // Get the stop criterion of the artificial time-stepping scheme
351 double steady_state_criterion =
352 this->simulation_parameters.multiphysics.cls_parameters
353 .reinitialization_method.pde_based_interface_reinitialization
354 .steady_state_criterion;
355
356 // Evaluate the solution difference between the 2 last solutions
357 auto solution_diff = local_evaluation_point;
358 solution_diff -= previous_local_evaluation_point;
359
360 // Evaluate the current steady-state criterion value
361 double stop_criterion =
362 solution_diff.l2_norm() /
363 (previous_local_evaluation_point.l2_norm() + 1e-14);
364
366 {
367 this->pcout
368 << "PDE-based reinitialization solution norm difference = "
369 << solution_diff.l2_norm() << std::endl;
370 this->pcout
371 << "PDE-based reinitialization steady-state criterion value = "
372 << stop_criterion << std::endl;
373 this->pcout
374 << "PDE-based reinitialization fixed steady-state criterion = "
375 << steady_state_criterion << std::endl;
376 }
377
378 return ((stop_criterion > steady_state_criterion) &&
379 (step_number <
380 this->simulation_parameters.multiphysics.cls_parameters
381 .reinitialization_method
382 .pde_based_interface_reinitialization.max_steps_number +
383 1));
384 }
385 }
386
387 /**
388 * @brief Check if the phase gradient and curvature L2 projections have been
389 * solved.
390 */
391 void
393
396
397 // Parameters
399
400 // Simulation control object for simulation iteration number
401 std::shared_ptr<SimulationControl> simulation_control;
402
403 // Handler to output PDE-based reinitialization steps
405
406 // Time-stepping with BDF1
408 std::vector<double> time_step_vector = {0.0};
409
410 // Core elements
411 std::shared_ptr<parallel::DistributedTriangulationBase<dim>> triangulation;
412 std::shared_ptr<DoFHandler<dim>> dof_handler;
413 std::shared_ptr<FiniteElement<dim>> fe;
414
415 // Mapping and Quadrature
416 std::shared_ptr<Mapping<dim>> mapping;
417 std::shared_ptr<Quadrature<dim>> cell_quadrature;
418
419 // Solution storage
426 std::shared_ptr<GlobalVectorType> present_solution;
427 GlobalVectorType previous_solution; // Only used with bdf1
429 AffineConstraints<double> zero_constraints;
430 AffineConstraints<double> nonzero_constraints;
431 TrilinosWrappers::SparseMatrix system_matrix;
432
433 // Verbosity
435};
436
437#endif
GlobalVectorType & get_local_evaluation_point() override
Getter method to access the private attribute local_evaluation_point for the subequation currently so...
double get_residual_rescale_metric() const override
Return the metric for residual rescaling. By default, should return 1. If the rescale_residual_by_vol...
~CLSPDEBasedInterfaceReinitialization()=default
Default destructor.
AffineConstraints< double > & get_nonzero_constraints() override
Getter method to access the private attribute nonzero_constraints for the subequation currently solve...
void define_zero_constraints()
Define the zero constraints used to solve the problem.
void setup_dofs() override
Set up the DofHandler and the degree of freedom associated with the subequation.
GlobalVectorType & get_present_solution() override
Getter method to access the private attribute present_solution for the subequation currently solved.
void set_initial_conditions()
Sets-up the initial conditions associated with the subequation.
void output_newton_update_norms() override
Output the and norms of the correction vector.
void assemble_system_rhs() override
Assemble the right-hand side (rhs).
void define_non_zero_constraints()
Define the non zero constraints used to solve the problem.
double compute_time_step()
Computes PDE-based reinitialization time step value with user-imposed CFL and minimum cell size.
bool continue_iterating(const unsigned int step_number)
Indicate if the PDE-based reinitialization should continue by checking if at least one of the two sto...
void check_dependencies_validity()
Check if the phase gradient and curvature L2 projections have been solved.
double compute_diffusivity(const double min_cell_size) const
Computes mesh-dependant diffusivity coefficient value.
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
void solve() override
Solve interface PDE-based reinitialization process until one of the stop criteria is met.
GlobalVectorType & get_evaluation_point() override
Getter method to access the private attribute evaluation_point for the subequation currently solved.
GlobalVectorType & get_system_rhs() override
Getter method to access the private attribute system_rhs for the subequation currently solved.
void write_output_results(const unsigned int step)
Write parallel VTU files of quantities of interest for the PDE-based interface reinitialization proce...
CLSPDEBasedInterfaceReinitialization(const SimulationParameters< dim > &p_simulation_parameters, const ConditionalOStream &p_pcout, std::shared_ptr< parallel::DistributedTriangulationBase< dim > > p_triangulation, const std::shared_ptr< SimulationControl > &p_simulation_control, CLSSubequationsInterface< dim > &p_subequations_interface)
Constructor of the CLS PDE-based interface reinitialization.
GlobalVectorType & get_newton_update() override
Getter method to access the private attribute newton_update for the subequation currently solved.
void solve_linear_system() override
Solve the linear system.
Interface for secondary equations (subequations) solved within the CLS auxiliary physics.
The PVDHandler class manages the storage of the information required to write pvd file....
Definition pvd_handler.h:17
PhysicsNonlinearSubequationsSolver(const Parameters::NonLinearSolver non_linear_solver_parameters, const ConditionalOStream &pcout)
CLSSubequationsID
IDs associated to the different subequations solved in Lethe.
@ pde_based_interface_reinitialization
CLS PDE-based interface reinitialization.
double identify_minimum_cell_size(const Mapping< dim > &mapping, const DoFHandler< dim > &dof_handler, const Quadrature< dim > &cell_quadrature, const MPI_Comm &mpi_communicator)
Identify minimum cell size. The cell size corresponds to the diameter of a disk (2D) or sphere (3D) o...
Definition utilities.h:1044
@ CLS
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17