Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
cahn_hilliard_scratch_data.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-2025 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_cahn_hilliard_scratch_data_h
5#define lethe_cahn_hilliard_scratch_data_h
6
7
11
12#include <deal.II/fe/fe_system.h>
13
14using namespace dealii;
15/**
16 * @brief Class that stores the information required by the assembly procedure
17 * for the Cahn-Hilliard equations. Consequently, this class
18 * calculates the phase field parameter Phi (values, gradients, laplacians),
19 * the chemical potential eta (values, gradients, laplacians) and the shape
20 * function (values, gradients, laplacians) at all the Gauss points for all
21 * degrees of freedom and stores it into arrays. Additionally, the user can
22 * request that this class gathers additional fields for physics which are
23 * coupled to the Cahn-Hilliard equations, such as the velocity which is
24 * required. This class serves as a separation between the evaluation at the
25 * Gauss point of the variables of interest and their use in the assembly, which
26 * is carried out by the assembler functions. For more information on this
27 * design, the reader can consult deal.II step-9
28 * "https://www.dealii.org/current/doxygen/deal.II/step_9.html". In this latter
29 * example, the scratch is a struct instead of a templated class because of the
30 * simplicity of step-9.
31 *
32 * @tparam dim An integer that denotes the dimension of the space in which
33 * the flow is solved
34 *
35 * @ingroup solvers
36 **/
37
38template <int dim>
40{
41public:
42 /**
43 * @brief Constructor. The constructor creates the fe_values that will be used
44 * to fill the member variables of the scratch. It also allocated the
45 * necessary memory for all member variables. However, it does not do any
46 * evaluation, since this needs to be done at the cell level.
47 *
48 * @param properties_manager The physical properties manager (see physical_properties_manager.h)
49 * @param fe_cahn_hilliard The FESystem used to solve the Cahn-Hilliard equations
50 * @param quadrature The quadrature to use for the assembly
51 * @param mapping The mapping of the domain in which the Cahn-Hilliard equations are solved
52 * @param fe_fd The FESystem used to solve the Fluid Dynamics equations
53 *
54 */
56 const FESystem<dim> &fe_cahn_hilliard,
57 const Quadrature<dim> &quadrature,
58 const Mapping<dim> &mapping,
59 const FiniteElement<dim> &fe_fd,
60 const Quadrature<dim - 1> &face_quadrature)
63 fe_cahn_hilliard,
64 quadrature,
65 update_values | update_quadrature_points |
66 update_JxW_values | update_gradients |
67 update_hessians)
68 , fe_values_fd(mapping, fe_fd, quadrature, update_values)
70 fe_cahn_hilliard,
71 face_quadrature,
72 update_values | update_quadrature_points |
73 update_JxW_values | update_gradients |
74 update_normal_vectors)
75 {
76 allocate();
77 }
78
79 /**
80 * @brief Copy Constructor. Same as the main constructor.
81 * This constructor only uses the other scratch to build the FeValues, it
82 * does not copy the content of the other scratch into itself since, by
83 * definition of the WorkStream mechanism it is assumed that the content of
84 * the scratch will be reset on a cell basis.
85 *
86 * @param sd
87 */
91 sd.fe_values_cahn_hilliard.get_fe(),
92 sd.fe_values_cahn_hilliard.get_quadrature(),
93 update_values | update_quadrature_points |
94 update_JxW_values | update_gradients |
95 update_hessians)
96 , fe_values_fd(sd.fe_values_fd.get_mapping(),
97 sd.fe_values_fd.get_fe(),
98 sd.fe_values_fd.get_quadrature(),
99 update_values)
101 sd.fe_face_values_cahn_hilliard.get_mapping(),
103 sd.fe_face_values_cahn_hilliard.get_quadrature(),
104 update_values | update_quadrature_points | update_JxW_values |
105 update_gradients | update_normal_vectors)
106 {
107 allocate();
108 }
109
110
111 /** @brief Allocates the necessary memory for all members of the scratch
112 */
113 void
114 allocate() override;
115
116 /** @brief Reinitializes the content of the scratch.
117 *
118 * Using the FeValues and the content of the solutions and previous solutions,
119 * fills all of the class member of the scratch.
120 *
121 * @param[in] cell The cell over which the assembly is being carried.
122 * This cell must be compatible with the fe which is used to fill the
123 * FeValues.
124 *
125 * @param[in] current_solution The present value of the solution for [Phi,eta]
126 *
127 * @param[in] previous_solutions The solutions at the previous time steps.
128 *
129 * @param[in] source_function The function describing the source term in
130 * Cahn-Hilliard equations.
131 *
132 */
133
134 template <typename VectorType>
135 void
136 reinit(const typename DoFHandler<dim>::active_cell_iterator &cell,
137 const VectorType &current_solution,
138 const std::vector<VectorType> &previous_solutions,
139 Function<dim> *source_function)
140 {
141 this->phase_order.component = 0;
142 this->chemical_potential.component = 1;
143
144 this->fe_values_cahn_hilliard.reinit(cell);
145
146 quadrature_points = this->fe_values_cahn_hilliard.get_quadrature_points();
147 auto &fe_cahn_hilliard = this->fe_values_cahn_hilliard.get_fe();
148
149 source_function->value_list(quadrature_points, source_phase_order, 0);
150 source_function->value_list(quadrature_points,
152 1);
153
154 // Compute cell diameter
155 double cell_measure = compute_cell_measure_with_JxW(
156 this->fe_values_cahn_hilliard.get_JxW_values());
157 this->cell_size =
158 compute_cell_diameter<dim>(cell_measure, fe_cahn_hilliard.degree);
159
160 // Gather Phi and eta (values, gradient and laplacian)
161 this->fe_values_cahn_hilliard[phase_order].get_function_values(
162 current_solution, this->phase_order_values);
163 this->fe_values_cahn_hilliard[phase_order].get_function_gradients(
164 current_solution, this->phase_order_gradients);
165 this->fe_values_cahn_hilliard[phase_order].get_function_laplacians(
166 current_solution, this->phase_order_laplacians);
167
168 this->fe_values_cahn_hilliard[chemical_potential].get_function_values(
169 current_solution, this->chemical_potential_values);
170 this->fe_values_cahn_hilliard[chemical_potential].get_function_gradients(
171 current_solution, this->chemical_potential_gradients);
172 this->fe_values_cahn_hilliard[chemical_potential].get_function_laplacians(
173 current_solution, this->chemical_potential_laplacians);
174
175 // Gather previous phase order values
176 for (unsigned int p = 0; p < previous_solutions.size(); ++p)
177 {
178 this->fe_values_cahn_hilliard[phase_order].get_function_values(
179 previous_solutions[p], previous_phase_order_values[p]);
180 }
181
182 // Gather previous chemical potential values
183 for (unsigned int p = 0; p < previous_solutions.size(); ++p)
184 {
185 this->fe_values_cahn_hilliard[chemical_potential].get_function_values(
186 previous_solutions[p], previous_chemical_potential_values[p]);
187 }
188
189 for (unsigned int q = 0; q < n_q_points; ++q)
190 {
191 this->JxW[q] = this->fe_values_cahn_hilliard.JxW(q);
192
193 for (unsigned int k = 0; k < n_dofs; ++k)
194 {
195 // Shape functions for the phase order
196 this->phi_phase[q][k] =
197 this->fe_values_cahn_hilliard[phase_order].value(k, q);
198 this->grad_phi_phase[q][k] =
199 this->fe_values_cahn_hilliard[phase_order].gradient(k, q);
200 this->hess_phi_phase[q][k] =
201 this->fe_values_cahn_hilliard[phase_order].hessian(k, q);
202 this->laplacian_phi_phase[q][k] = trace(this->hess_phi_phase[q][k]);
203
204 // Shape functions for the chemical potential
205 this->phi_potential[q][k] =
207 this->grad_phi_potential[q][k] =
208 this->fe_values_cahn_hilliard[chemical_potential].gradient(k, q);
209 this->hess_phi_potential[q][k] =
210 this->fe_values_cahn_hilliard[chemical_potential].hessian(k, q);
211 this->laplacian_phi_potential[q][k] =
212 trace(this->hess_phi_potential[q][k]);
213 }
214 }
215
216 this->is_boundary_cell =
217 cell->at_boundary(); // The attribute needs to be updated because the
218 // assembler for the angle of contact boundary
219 // condition needs to know if the cell is at the
220 // boundary
221 if (this->is_boundary_cell)
222 {
223 n_faces = cell->n_faces();
224 is_boundary_face = std::vector<bool>(n_faces, false);
225 n_faces_q_points = fe_face_values_cahn_hilliard.get_quadrature().size();
226 boundary_face_id = std::vector<unsigned int>(n_faces);
227
230
231 this->face_phase_grad_values = std::vector<std::vector<Tensor<1, dim>>>(
232 n_faces, std::vector<Tensor<1, dim>>(n_faces_q_points));
233
234 this->face_normal = std::vector<std::vector<Tensor<1, dim>>>(
235 n_faces, std::vector<Tensor<1, dim>>(n_faces_q_points));
236
237 for (const auto face : cell->face_indices())
238 {
239 this->is_boundary_face[face] = cell->face(face)->at_boundary();
240 if (this->is_boundary_face[face])
241 {
242 fe_face_values_cahn_hilliard.reinit(cell, face);
243 boundary_face_id[face] = cell->face(face)->boundary_id();
244
246 .get_function_gradients(current_solution,
247 this->face_phase_grad_values[face]);
248
249 for (unsigned int q = 0; q < n_faces_q_points; ++q)
250 {
251 face_JxW[face][q] = fe_face_values_cahn_hilliard.JxW(q);
252 this->face_normal[face][q] =
253 this->fe_face_values_cahn_hilliard.normal_vector(q);
254 for (const unsigned int k :
255 fe_face_values_cahn_hilliard.dof_indices())
256 {
257 this->grad_phi_face_phase[face][q][k] =
259 .gradient(k, q);
260 }
261 }
262 }
263 }
264 }
265 }
266
267
268 template <typename VectorType>
269 void
270 reinit_velocity(const typename DoFHandler<dim>::active_cell_iterator &cell,
271 const VectorType &current_solution,
272 const Parameters::ALE<dim> &ale)
273 {
274 FEValuesExtractors::Vector velocities(0);
275 this->fe_values_fd.reinit(cell);
276
277 this->fe_values_fd[velocities].get_function_values(current_solution,
279
280 if (!ale.enabled())
281 return;
282
283
284 // ALE enabled, so extract the ALE velocity and subtract it from the
285 // velocity obtained from the fluid dynamics
286 Tensor<1, dim> velocity_ale;
287 std::shared_ptr<Functions::ParsedFunction<dim>> velocity_ale_function =
288 ale.velocity;
289 Vector<double> velocity_ale_vector(dim);
290
291 for (unsigned int q = 0; q < n_q_points; ++q)
292 {
293 velocity_ale_function->vector_value(quadrature_points[q],
294 velocity_ale_vector);
295 for (int d = 0; d < dim; ++d)
296 velocity_ale[d] = velocity_ale_vector[d];
297
298 velocity_values[q] -= velocity_ale;
299 }
300 }
301
302 /**
303 * @brief Calculates the physical properties that may be required by the
304 * Cahn-Hilliard problem. Namely the surface tension and the Cahn-Hilliard
305 * mobility.
306 */
307 void
309
310
311 // Physical properties
313 std::map<field, std::vector<double>> fields;
314 dealii::types::material_id material_id;
315 std::vector<double> density;
316 std::vector<double> kinematic_viscosity;
317 std::vector<double> surface_tension;
318 std::vector<double> mobility_cahn_hilliard;
320
321
322 FEValuesExtractors::Scalar phase_order;
323 FEValuesExtractors::Scalar chemical_potential;
324
325 // FEValues for the Cahn-Hilliard problem
327 unsigned int n_dofs;
328 unsigned int n_q_points;
329 double cell_size;
330
331 // Quadrature
332 Table<1, double> JxW;
333 std::vector<Point<dim>> quadrature_points;
334
335 // Phase order and chemical potential values
336 std::vector<double> phase_order_values;
337 std::vector<Tensor<1, dim>> phase_order_gradients;
338 std::vector<double> phase_order_laplacians;
339 std::vector<std::vector<double>> previous_phase_order_values;
340 std::vector<double> chemical_potential_values;
341 std::vector<Tensor<1, dim>> chemical_potential_gradients;
342 std::vector<double> chemical_potential_laplacians;
343 std::vector<std::vector<double>> previous_chemical_potential_values;
344
345 // Source term
346 std::vector<double> source_phase_order;
347 std::vector<double> source_chemical_potential;
348
349 // Shape functions for the phase order and the chemical potential
350 Table<2, double> phi_phase;
351 Table<2, Tensor<2, dim>> hess_phi_phase;
352 Table<2, double> laplacian_phi_phase;
353 Table<2, Tensor<1, dim>> grad_phi_phase;
354 Table<2, double> phi_potential;
355 Table<2, Tensor<2, dim>> hess_phi_potential;
356 Table<2, double> laplacian_phi_potential;
357 Table<2, Tensor<1, dim>> grad_phi_potential;
358
359 /**
360 * Scratch component for the Navier-Stokes component
361 */
362 FEValuesExtractors::Vector velocities;
363 // This FEValues must be instantiated for the velocity
364 FEValues<dim> fe_values_fd;
365 std::vector<Tensor<1, dim>> velocity_values;
366 std::vector<std::vector<Tensor<1, dim>>> previous_velocity_values;
367 std::vector<Tensor<2, dim>> velocity_gradient_values;
368
369 // Scratch for the face boundary condition
371 Table<2, double> face_JxW;
372
373 unsigned int n_faces;
374 unsigned int n_faces_q_points;
375
376 // Is boundary cell indicator
378 std::vector<bool> is_boundary_face;
379 std::vector<unsigned int> boundary_face_id;
380
381 // First is face number, second quadrature point, third DOF
382 Table<3, Tensor<1, dim>> grad_phi_face_phase;
383 // First vector is face number, second quadrature point
384 std::vector<std::vector<Tensor<1, dim>>> face_phase_grad_values;
385 // The normal vector is necessary for the free angle boundary condition
386 std::vector<std::vector<Tensor<1, dim>>> face_normal;
387};
388
389#endif
std::map< field, std::vector< double > > fields
std::vector< double > source_chemical_potential
PhysicalPropertiesManager properties_manager
std::vector< double > surface_tension
std::vector< double > phase_order_laplacians
Table< 2, Tensor< 2, dim > > hess_phi_potential
std::vector< double > mobility_cahn_hilliard_gradient
std::vector< double > chemical_potential_laplacians
std::vector< double > source_phase_order
std::vector< unsigned int > boundary_face_id
std::vector< double > kinematic_viscosity
std::vector< std::vector< Tensor< 1, dim > > > face_normal
std::vector< std::vector< Tensor< 1, dim > > > previous_velocity_values
Table< 2, Tensor< 2, dim > > hess_phi_phase
dealii::types::material_id material_id
std::vector< double > mobility_cahn_hilliard
FEValuesExtractors::Scalar phase_order
std::vector< Tensor< 2, dim > > velocity_gradient_values
std::vector< Tensor< 1, dim > > chemical_potential_gradients
std::vector< Tensor< 1, dim > > velocity_values
Table< 2, Tensor< 1, dim > > grad_phi_phase
std::vector< double > chemical_potential_values
void allocate() override
Allocates the necessary memory for all members of the scratch.
FEFaceValues< dim > fe_face_values_cahn_hilliard
FEValuesExtractors::Scalar chemical_potential
std::vector< Point< dim > > quadrature_points
std::vector< std::vector< Tensor< 1, dim > > > face_phase_grad_values
std::vector< std::vector< double > > previous_chemical_potential_values
CahnHilliardScratchData(const PhysicalPropertiesManager &properties_manager, const FESystem< dim > &fe_cahn_hilliard, const Quadrature< dim > &quadrature, const Mapping< dim > &mapping, const FiniteElement< dim > &fe_fd, const Quadrature< dim - 1 > &face_quadrature)
Constructor. The constructor creates the fe_values that will be used to fill the member variables of ...
Table< 3, Tensor< 1, dim > > grad_phi_face_phase
void calculate_physical_properties()
Calculates the physical properties that may be required by the Cahn-Hilliard problem....
void reinit_velocity(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const Parameters::ALE< dim > &ale)
Table< 2, Tensor< 1, dim > > grad_phi_potential
std::vector< Tensor< 1, dim > > phase_order_gradients
std::vector< double > phase_order_values
FEValuesExtractors::Vector velocities
CahnHilliardScratchData(const CahnHilliardScratchData< dim > &sd)
Copy Constructor. Same as the main constructor. This constructor only uses the other scratch to build...
std::vector< std::vector< double > > previous_phase_order_values
void reinit(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const std::vector< VectorType > &previous_solutions, Function< dim > *source_function)
Reinitializes the content of the scratch.
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