Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
cls_scratch_data.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021-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_scratch_data_h
5#define lethe_cls_scratch_data_h
6
8
11
12#include <deal.II/fe/fe_interface_values.h>
13#include <deal.II/fe/fe_system.h>
14
15using namespace dealii;
16
17/**
18 * @brief Store the information required by the assembly procedure
19 * for a CLS free surface equation. Consequently, this class
20 * calculates the phase values (values, gradients, laplacians) and the shape
21 * method (values, gradients, laplacians) at all the gauss points for all
22 * degrees of freedom and stores it into arrays.
23 * This class serves as a separation between the evaluation at the gauss point
24 * of the variables of interest and their use in the assembly, which is carried
25 * out by the assembler methods.
26 *
27 * @tparam dim An integer that denotes the dimension of the space in which
28 * the flow is solved
29 *
30 * @ingroup solvers
31 **/
32
33template <int dim>
35{
36public:
37 /**
38 * @brief Constructor. The constructor creates the fe_values that will be used
39 * to fill the member variables of the scratch. It also allocated the
40 * necessary memory for all member variables. However, it does not do any
41 * evaluation, since this needs to be done at the cell level.
42 *
43 * @param simulation_control The SimulationControl object that holds
44 * information related to the control of the steady-state or transient
45 * simulation. This is used to extrapolate velocity solutions in time
46 * for transient simulation.
47 *
48 * @param properties_manager Manager to calculate the physical properties.
49 *
50 * @param fe_cls The FESystem used to solve the CLS equations.
51 *
52 * @param quadrature The quadrature to use for the assembly within the cells.
53 *
54 * @param face_quadrature The quadrature to use for the assembly on faces.
55 *
56 * @param mapping The mapping of the domain in which the Navier-Stokes
57 * equations are solved.
58 *
59 * @param fe_fd The FESystem used to solve the Fluid Dynamics equations.
60 *
61 */
62 CLSScratchData(const std::shared_ptr<SimulationControl> &simulation_control,
64 const FiniteElement<dim> &fe_cls,
65 const Quadrature<dim> &quadrature,
66 const Quadrature<dim - 1> &face_quadrature,
67 const Mapping<dim> &mapping,
68 const FiniteElement<dim> &fe_fd)
71 , fe_values_cls(mapping,
72 fe_cls,
73 quadrature,
74 update_values | update_gradients |
75 update_quadrature_points | update_hessians |
76 update_JxW_values)
78 fe_cls,
79 face_quadrature,
80 update_values | update_quadrature_points |
81 update_JxW_values | update_normal_vectors)
82 , fe_values_fd(mapping, fe_fd, quadrature, update_values | update_gradients)
83 , fe_face_values_fd(mapping, fe_fd, face_quadrature, update_values)
84 {
85 allocate();
86 }
87
88 /**
89 * @brief Copy Constructor. Same as the main constructor.
90 * This constructor only uses the other scratch to build the FeValues, it
91 * does not copy the content of the other scratch into itself since, by
92 * definition of the WorkStream mechanism it is assumed that the content of
93 * the scratch will be reset on a cell basis.
94 *
95 * @param sd The scratch data to be copied
96 */
100 , fe_values_cls(sd.fe_values_cls.get_mapping(),
101 sd.fe_values_cls.get_fe(),
102 sd.fe_values_cls.get_quadrature(),
103 update_values | update_gradients |
104 update_quadrature_points | update_hessians |
105 update_JxW_values)
107 sd.fe_interface_values_cls.get_fe(),
108 sd.fe_interface_values_cls.get_quadrature(),
109 update_values | update_quadrature_points |
110 update_JxW_values | update_normal_vectors)
111 , fe_values_fd(sd.fe_values_fd.get_mapping(),
112 sd.fe_values_fd.get_fe(),
113 sd.fe_values_fd.get_quadrature(),
114 update_values | update_gradients)
115 , fe_face_values_fd(sd.fe_face_values_fd.get_mapping(),
116 sd.fe_face_values_fd.get_fe(),
117 sd.fe_face_values_fd.get_quadrature(),
118 update_values)
119 {
120 allocate();
121 }
122
123
124 /** @brief Allocates the memory for the scratch
125 *
126 * This method allocates the necessary memory for all members of the scratch
127 */
128 void
129 allocate() override;
130
131 /** @brief Reinitialize the content of the scratch
132 *
133 * Using the FeValues and the content of the solutions and previous solutions,
134 * fills all of the class member of the scratch
135 *
136 * @tparam VectorType The Vector type used for the solvers
137 *
138 * @param[in] cell The cell over which the assembly is being carried.
139 * This cell must be compatible with the FE which is used to fill the
140 * FeValues.
141 *
142 * @param[in] current_solution The present value of the solution for the CLS.
143 *
144 * @param[in] previous_solutions The solutions at the previous time steps.
145 *
146 */
147
148 template <typename VectorType>
149 void
150 reinit(const typename DoFHandler<dim>::active_cell_iterator &cell,
151 const VectorType &current_solution,
152 const std::vector<VectorType> &previous_solutions)
153 {
154 fe_values_cls.reinit(cell);
155 this->quadrature_points = fe_values_cls.get_quadrature_points();
156 auto &fe_cls = fe_values_cls.get_fe();
157
158 // Compute cell diameter
159 double cell_measure =
160 compute_cell_measure_with_JxW(this->fe_values_cls.get_JxW_values());
161 this->cell_size = compute_cell_diameter<dim>(cell_measure, fe_cls.degree);
162
163 fe_values_cls.get_function_values(current_solution,
165 fe_values_cls.get_function_gradients(current_solution,
166 this->phase_gradients);
167 fe_values_cls.get_function_laplacians(current_solution,
168 this->phase_laplacians);
169 fe_values_cls.get_function_gradients(previous_solutions[0],
171
172
173 // Gather previous cls values
174 for (unsigned int p = 0; p < previous_solutions.size(); ++p)
175 {
176 fe_values_cls.get_function_values(previous_solutions[p],
177 this->previous_phase_values[p]);
178 }
179
180 for (unsigned int q = 0; q < this->n_q_points; ++q)
181 {
182 this->JxW[q] = fe_values_cls.JxW(q);
183
184 for (unsigned int k = 0; k < this->n_dofs; ++k)
185 {
186 // Shape function
187 this->phi[q][k] = fe_values_cls.shape_value(k, q);
188 this->grad_phi[q][k] = fe_values_cls.shape_grad(k, q);
189 this->hess_phi[q][k] = fe_values_cls.shape_hessian(k, q);
190 this->laplacian_phi[q][k] = trace(this->hess_phi[q][k]);
191 }
192 }
193 }
194
195
196 /** @brief Reinitialize the content of the scratch for the internal faces. This is only used for the DG assemblers.
197 *
198 * @param[in] cell The cell over which the assembly is being carried.
199 *
200 * @param[in] face_no The face index associated with the cell
201 *
202 * @param[in] sub_face_no The subface index associated with the face
203 *
204 * @param[in] neighbor_cell The neighboring cell
205 *
206 * @param[in] neighbor_face_no The face index associated with the neighboring
207 * cell
208 *
209 * @param[in] neighbor_sub_face_no The subface index associated with the
210 * neighboring cell
211 *
212 * @param[in] current_solution The present value of the solution.
213 */
214 template <typename VectorType>
215 void
217 const typename DoFHandler<dim>::active_cell_iterator &cell,
218 const unsigned int &face_no,
219 const unsigned int &sub_face_no,
220 const typename DoFHandler<dim>::active_cell_iterator &neighbor_cell,
221 const unsigned int &neighbor_face_no,
222 const unsigned int &neighbor_sub_face_no,
223 const VectorType &current_solution)
224 {
225 fe_interface_values_cls.reinit(cell,
226 face_no,
227 sub_face_no,
228 neighbor_cell,
229 neighbor_face_no,
230 neighbor_sub_face_no);
231 face_quadrature_points = fe_interface_values_cls.get_quadrature_points();
232
233 n_interface_dofs = fe_interface_values_cls.n_current_interface_dofs();
234
235 // BB TODO : Preallocate memory here
236 values_here.resize(face_quadrature_points.size());
239
240 fe_interface_values_cls.get_fe_face_values(0).get_function_values(
241 current_solution, values_here);
242 fe_interface_values_cls.get_fe_face_values(1).get_function_values(
243 current_solution, values_there);
244
245 fe_interface_values_cls.get_jump_in_function_values(current_solution,
247 }
248
249 /** @brief Reinitialize the velocity, calculated by the Fluid Dynamics
250 *
251 * @tparam VectorType The Vector type used for the solvers
252 *
253 * @param cell The cell for which the velocity is reinitialized
254 * This cell must be compatible with the Fluid Dynamics FE
255 *
256 * @param current_solution The present value of the solution for \f$[u,p]\f$
257 *
258 * @param previous_solutions Vector of \f$n\f$ @p VectorType containers of
259 * previous fluid dynamic solutions (\f$[u,p]\f$). \f$n\f$ depends on the BDF
260 * scheme selected for time-stepping.
261 *
262 * @param ale ALE parameters
263 *
264 */
265
266 template <typename VectorType>
267 void
268 reinit_velocity(const typename DoFHandler<dim>::active_cell_iterator &cell,
269 const VectorType &current_solution,
270 const std::vector<VectorType> &previous_solutions,
271 const Parameters::ALE<dim> &ale)
272 {
273 fe_values_fd.reinit(cell);
274
275 fe_values_fd[velocities_fd].get_function_values(current_solution,
277 fe_values_fd[velocities_fd].get_function_gradients(
278 current_solution, velocity_gradient_values);
279
280 for (unsigned int q = 0; q < this->n_q_points; ++q)
281 {
282 this->velocity_divergences[q] =
283 trace(this->velocity_gradient_values[q]);
284 }
285
286 // Gather previous velocity values
287 for (unsigned int p = 0; p < previous_solutions.size(); ++p)
288 {
289 fe_values_fd[velocities_fd].get_function_values(
290 previous_solutions[p], this->previous_velocity_values[p]);
291 }
292
293 if (ale.enabled())
294 {
295 // ALE enabled, so extract the ALE velocity and subtract it from the
296 // velocity obtained from the fluid dynamics
297 Tensor<1, dim> velocity_ale;
298 std::shared_ptr<Functions::ParsedFunction<dim>> velocity_ale_function =
299 ale.velocity;
300 Vector<double> velocity_ale_vector(dim);
301
302 for (unsigned int q = 0; q < n_q_points; ++q)
303 {
304 velocity_ale_function->vector_value(quadrature_points[q],
305 velocity_ale_vector);
306 for (int d = 0; d < dim; ++d)
307 velocity_ale[d] = velocity_ale_vector[d];
308
309 velocity_values[q] -= velocity_ale;
310
311 for (unsigned int p = 0; p < previous_solutions.size(); ++p)
312 {
313 this->previous_velocity_values[p][q] -= velocity_ale;
314 }
315 }
316 }
317
318 // Extrapolate velocity to t+dt using the BDF scheme if the simulation is
319 // transient
320 const auto method = this->simulation_control->get_assembly_method();
321 if (time_stepping_is_bdf(method))
322 {
323 // Extrapolate velocity
324 std::vector<double> time_vector =
325 this->simulation_control->get_simulation_times();
326 bdf_extrapolate(time_vector,
329 this->velocity_values);
330 }
331 }
332
333
334 /** @brief Reinitialize the content of the scratch regarding the velocity for internal/boundary faces.
335 * The velocity is inherently assumed to have been solved using a CG scheme.
336 *
337 * @param[in] cell The cell over which the assembly is being carried.
338 *
339 * @param[in] face_no The face index associated with the cell
340 *
341 * @param[in] velocity_solution The present value of the velocity solution.
342 */
343 template <typename VectorType>
344 void
346 const typename DoFHandler<dim>::active_cell_iterator &velocity_cell,
347 const unsigned int &face_no,
348 const VectorType &velocity_solution,
349 const Parameters::ALE<dim> &ale)
350 {
351 fe_face_values_fd.reinit(velocity_cell, face_no);
352
353 // BB note : Array could be pre-allocated
355
356 fe_face_values_fd[velocities_fd].get_function_values(velocity_solution,
358
359 if (!ale.enabled())
360 return;
361
362 // ALE enabled, so extract the ALE velocity and subtract it from the
363 // velocity obtained from the fluid dynamics
364 Tensor<1, dim> velocity_ale;
365 std::shared_ptr<Functions::ParsedFunction<dim>> velocity_ale_function =
366 ale.velocity;
367 Vector<double> velocity_ale_vector(dim);
368
369 for (unsigned int q = 0; q < face_quadrature_points.size(); ++q)
370 {
371 velocity_ale_function->vector_value(face_quadrature_points[q],
372 velocity_ale_vector);
373 for (int d = 0; d < dim; ++d)
374 velocity_ale[d] = velocity_ale_vector[d];
375
376 face_velocity_values[q] -= velocity_ale;
377 }
378 }
379
380 // For velocity solution extrapolation
381 const std::shared_ptr<SimulationControl> simulation_control;
382
383 // Physical properties
385 std::map<field, std::vector<double>> fields;
386
387 // FEValues for the CLS problem
388 FEValues<dim> fe_values_cls;
389 FEInterfaceValues<dim> fe_interface_values_cls;
390 unsigned int n_dofs;
391 unsigned int n_interface_dofs;
392 unsigned int n_q_points;
393 double cell_size;
394
395 // Quadrature
396 Table<1, double> JxW;
397 std::vector<Point<dim>> quadrature_points;
398 std::vector<Point<dim>> face_quadrature_points;
399
400 // CLS values
401 std::vector<double> present_phase_values;
402 std::vector<Tensor<1, dim>> phase_gradients;
403 std::vector<Tensor<1, dim>> previous_phase_gradients;
404
405 std::vector<double> phase_laplacians;
406 std::vector<std::vector<double>> previous_phase_values;
407
408 // CLS values at the faces
409 std::vector<double> values_here;
410 std::vector<double> values_there;
411 std::vector<double> phase_value_jump;
412
413 // Shape functions
414 Table<2, double> phi;
415 Table<2, Tensor<1, dim>> grad_phi;
416 Table<2, Tensor<2, dim>> hess_phi;
417 Table<2, double> laplacian_phi;
418
419
420 /**
421 * Scratch component for the Navier-Stokes component
422 */
423 FEValues<dim> fe_values_fd;
424 FEFaceValues<dim> fe_face_values_fd;
425
426 FEValuesExtractors::Vector velocities_fd;
427 // This FEValues must be instantiated for the velocity
428 std::vector<Tensor<1, dim>> velocity_values;
429 std::vector<std::vector<Tensor<1, dim>>> previous_velocity_values;
430 std::vector<Tensor<2, dim>> velocity_gradient_values;
431 std::vector<double> velocity_divergences;
432
433 // Face velocity value for DG
434 std::vector<Tensor<1, dim>> face_velocity_values;
435};
436
437#endif
void bdf_extrapolate(const std::vector< double > &time_vector, const std::vector< std::vector< DataType > > &solution_vector, const unsigned int number_of_previous_solutions, std::vector< DataType > &extrapolated_solution)
Extrapolate vector of solution to time_vector[0] using previous solution times and previous solutions...
Definition bdf.h:133
unsigned int number_of_previous_solutions(const Parameters::SimulationControl::TimeSteppingMethod method)
Get the number of previous time steps for a specific BDF scheme.
Definition bdf.h:97
FEFaceValues< dim > fe_face_values_fd
std::vector< Point< dim > > face_quadrature_points
void reinit(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const std::vector< VectorType > &previous_solutions)
Reinitialize the content of the scratch.
const PhysicalPropertiesManager properties_manager
std::vector< Tensor< 1, dim > > face_velocity_values
std::vector< double > values_there
Table< 2, Tensor< 2, dim > > hess_phi
std::vector< double > velocity_divergences
Table< 2, Tensor< 1, dim > > grad_phi
std::vector< Tensor< 2, dim > > velocity_gradient_values
Table< 1, double > JxW
std::vector< Point< dim > > quadrature_points
CLSScratchData(const std::shared_ptr< SimulationControl > &simulation_control, const PhysicalPropertiesManager &properties_manager, const FiniteElement< dim > &fe_cls, const Quadrature< dim > &quadrature, const Quadrature< dim - 1 > &face_quadrature, const Mapping< dim > &mapping, const FiniteElement< dim > &fe_fd)
Constructor. The constructor creates the fe_values that will be used to fill the member variables of ...
CLSScratchData(const CLSScratchData< dim > &sd)
Copy Constructor. Same as the main constructor. This constructor only uses the other scratch to build...
FEValues< dim > fe_values_cls
FEValuesExtractors::Vector velocities_fd
Table< 2, double > laplacian_phi
const std::shared_ptr< SimulationControl > simulation_control
FEInterfaceValues< dim > fe_interface_values_cls
void reinit_velocity(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const std::vector< VectorType > &previous_solutions, const Parameters::ALE< dim > &ale)
Reinitialize the velocity, calculated by the Fluid Dynamics.
void reinit_internal_face(const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int &face_no, const unsigned int &sub_face_no, const typename DoFHandler< dim >::active_cell_iterator &neighbor_cell, const unsigned int &neighbor_face_no, const unsigned int &neighbor_sub_face_no, const VectorType &current_solution)
Reinitialize the content of the scratch for the internal faces. This is only used for the DG assemble...
std::vector< double > values_here
std::vector< double > phase_laplacians
std::vector< std::vector< double > > previous_phase_values
Table< 2, double > phi
unsigned int n_dofs
std::vector< double > phase_value_jump
std::vector< double > present_phase_values
std::map< field, std::vector< double > > fields
std::vector< Tensor< 1, dim > > previous_phase_gradients
std::vector< Tensor< 1, dim > > velocity_values
FEValues< dim > fe_values_fd
std::vector< std::vector< Tensor< 1, dim > > > previous_velocity_values
void allocate() override
Allocates the memory for the scratch.
unsigned int n_q_points
void reinit_face_velocity(const typename DoFHandler< dim >::active_cell_iterator &velocity_cell, const unsigned int &face_no, const VectorType &velocity_solution, const Parameters::ALE< dim > &ale)
Reinitialize the content of the scratch regarding the velocity for internal/boundary faces....
unsigned int n_interface_dofs
std::vector< Tensor< 1, dim > > phase_gradients
An interface for all the common parameters required to consider a moving frame of reference in a simp...
Definition ale.h:25
bool enabled() const
Return if the ALE module is enabled.
Definition ale.h:64
std::shared_ptr< Functions::ParsedFunction< dim > > velocity
ALE velocity function.
Definition ale.h:56
Class that manages the physical properties model which are required to calculate the various physical...
Base class for scratch data objects of physics and subequations.
double compute_cell_measure_with_JxW(const std::vector< double > &JxW_values)
Computes the area (2D) or volume (3D) of the cell by integrating 1 over the cell, by summing JxW valu...
Definition utilities.h:1017
double compute_cell_diameter(const double cell_measure, const unsigned int fe_degree)
Computes equivalent cell diameter by comparing the area to a disk (2D) or the volume to a sphere (3D)...
Definition utilities.h:989
GlobalVectorType VectorType
bool time_stepping_is_bdf(const Parameters::SimulationControl::TimeSteppingMethod method)
Determines if the time integration method is within the bdf family.