Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
cahn_hilliard_assemblers.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/bdf.h>
6
8#include <solvers/copy_data.h>
9
10#include <numbers>
11
12template <int dim>
13void
15 const CahnHilliardScratchData<dim> &scratch_data,
17{
18 // Gather physical properties
19 const double epsilon = this->epsilon;
20 const double cell_size = scratch_data.cell_size;
21 const double xi =
22 this->cahn_hilliard_parameters.potential_smoothing_coefficient;
23
24 // Loop and quadrature information
25 const auto &JxW_vec = scratch_data.JxW;
26 const unsigned int n_q_points = scratch_data.n_q_points;
27 const unsigned int n_dofs = scratch_data.n_dofs;
28
29 // Copy data elements
30 auto &local_matrix = copy_data.local_matrix;
31
32 for (unsigned int q = 0; q < n_q_points; ++q)
33 {
34 const double lambda = 3 * epsilon * scratch_data.surface_tension[q] /
35 (2 * std::numbers::sqrt2);
36
37 const double mobility = scratch_data.mobility_cahn_hilliard[q];
38 const double mobility_gradient =
39 scratch_data.mobility_cahn_hilliard_gradient[q];
40
41 // Gather into local variables the relevant fields
42 const Tensor<1, dim> velocity_field = scratch_data.velocity_values[q];
43
44 // Store JxW in local variable for faster access;
45 const double JxW = JxW_vec[q];
46
47 const double phase_order_value = scratch_data.phase_order_values[q];
48 const Tensor<1, dim> potential_gradient =
49 scratch_data.chemical_potential_gradients[q];
50
51 for (unsigned int i = 0; i < n_dofs; ++i)
52 {
53 const double phi_phase_i = scratch_data.phi_phase[q][i];
54 const Tensor<1, dim> grad_phi_phase_i =
55 scratch_data.grad_phi_phase[q][i];
56
57 const double phi_potential_i = scratch_data.phi_potential[q][i];
58 const Tensor<1, dim> grad_phi_potential_i =
59 scratch_data.grad_phi_potential[q][i];
60
61
62 for (unsigned int j = 0; j < n_dofs; ++j)
63 {
64 const double phi_phase_j = scratch_data.phi_phase[q][j];
65 const Tensor<1, dim> grad_phi_phase_j =
66 scratch_data.grad_phi_phase[q][j];
67
68 const double phi_potential_j = scratch_data.phi_potential[q][j];
69 const Tensor<1, dim> grad_phi_potential_j =
70 scratch_data.grad_phi_potential[q][j];
71
72 local_matrix(i, j) +=
73 // Phase order equation
74 (phi_phase_i * (velocity_field * grad_phi_phase_j) +
75 mobility * grad_phi_phase_i * grad_phi_potential_j +
76 mobility_gradient * grad_phi_phase_i * potential_gradient *
77 phi_phase_j
78 // Chemical potential equation
79 + phi_potential_i * phi_potential_j -
80 (lambda / (epsilon * epsilon)) * phi_potential_i *
81 (3 * phase_order_value * phase_order_value - 1.0) *
82 phi_phase_j -
83 lambda * grad_phi_potential_i * grad_phi_phase_j
84 // Chemical potential smoothing
85 + xi * cell_size * cell_size * grad_phi_potential_i *
86 grad_phi_potential_j) *
87 JxW;
88 }
89 }
90 }
91}
92
93
94
95template <int dim>
96void
98 const CahnHilliardScratchData<dim> &scratch_data,
100{
101 // Gather physical properties
102 const double epsilon = this->epsilon;
103 const double cell_size = scratch_data.cell_size;
104 const double xi =
105 this->cahn_hilliard_parameters.potential_smoothing_coefficient;
106
107
108 // Loop and quadrature information
109 const auto &JxW_vec = scratch_data.JxW;
110 const unsigned int n_q_points = scratch_data.n_q_points;
111 const unsigned int n_dofs = scratch_data.n_dofs;
112
113 // Copy data elements
114 auto &local_rhs = copy_data.local_rhs;
115
116 for (unsigned int q = 0; q < n_q_points; ++q)
117 {
118 const double lambda = 3 * epsilon * scratch_data.surface_tension[q] /
119 (2 * std::numbers::sqrt2);
120 const double mobility = scratch_data.mobility_cahn_hilliard[q];
121
122
123 // Gather into local variables the relevant fields
124 const Tensor<1, dim> velocity_field = scratch_data.velocity_values[q];
125
126 // Store JxW in local variable for faster access;
127 const double JxW = JxW_vec[q];
128 const double phase_order_value = scratch_data.phase_order_values[q];
129 const Tensor<1, dim> phase_order_gradient =
130 scratch_data.phase_order_gradients[q];
131 const double potential_value = scratch_data.chemical_potential_values[q];
132 const Tensor<1, dim> potential_gradient =
133 scratch_data.chemical_potential_gradients[q];
134 const double source_phase_order = scratch_data.source_phase_order[q];
135 const double source_chemical_potential =
136 scratch_data.source_chemical_potential[q];
137
138
139 for (unsigned int i = 0; i < n_dofs; ++i)
140 {
141 const double phi_phase_i = scratch_data.phi_phase[q][i];
142 const Tensor<1, dim> grad_phi_phase_i =
143 scratch_data.grad_phi_phase[q][i];
144 const double phi_potential_i = scratch_data.phi_potential[q][i];
145 const Tensor<1, dim> grad_phi_potential_i =
146 scratch_data.grad_phi_potential[q][i];
147
148 local_rhs(i) +=
149 // Phase order equation
150 (-phi_phase_i * (velocity_field * phase_order_gradient) -
151 mobility * grad_phi_phase_i * potential_gradient
152 // Chemical potential equation
153 - phi_potential_i * potential_value +
154 (lambda / (epsilon * epsilon)) * phi_potential_i *
155 (phase_order_value * phase_order_value - 1) * phase_order_value +
156 lambda * grad_phi_potential_i * phase_order_gradient
157 // Chemical potential smoothing
158 - xi * cell_size * cell_size * grad_phi_potential_i *
159 potential_gradient
160 // Source term
161 + source_phase_order * phi_phase_i +
162 source_chemical_potential * phi_potential_i) *
163 JxW;
164 }
165 }
166}
167
168template class CahnHilliardAssemblerCore<2>;
169template class CahnHilliardAssemblerCore<3>;
170
171template <int dim>
172void
174 const CahnHilliardScratchData<dim> &scratch_data,
175 StabilizedMethodsCopyData &copy_data)
176{
177 if (!scratch_data.is_boundary_cell)
178 {
179 return;
180 }
181 const double epsilon = this->epsilon;
182
183 auto &local_matrix = copy_data.local_matrix;
184
185
186 for (unsigned int f = 0; f < scratch_data.n_faces; f++)
187 {
188 // Check if the face is on a boundary
189 if (scratch_data.is_boundary_face[f])
190 {
191 if (this->cahn_hilliard_boundary_conditions.type.at(
192 scratch_data.boundary_face_id[f]) ==
194 {
195 const double angle_of_contact =
196 this->cahn_hilliard_boundary_conditions.angle_of_contact.at(
197 scratch_data.boundary_face_id[f]);
198 for (unsigned int q = 0; q < scratch_data.n_faces_q_points; ++q)
199 {
200 const Tensor<1, dim> face_phase_grad_value =
201 scratch_data.face_phase_grad_values[f][q];
202 const double lambda = 3 * epsilon *
203 scratch_data.surface_tension[q] /
204 (2 * std::numbers::sqrt2);
205
206 const double JxW_face = scratch_data.face_JxW[f][q];
207
208 for (unsigned int i = 0; i < scratch_data.n_dofs; ++i)
209 {
210 const double phi_potential_i =
211 scratch_data.phi_potential[q][i];
212
213 for (unsigned int j = 0; j < scratch_data.n_dofs; ++j)
214 {
215 const Tensor<1, dim> grad_phi_face_phase_j =
216 scratch_data.grad_phi_face_phase[f][q][j];
217
218 local_matrix(i, j) +=
219 -lambda * -phi_potential_i * grad_phi_face_phase_j *
220 face_phase_grad_value *
221 std::cos(angle_of_contact * M_PI / 180.0) *
222 (1.0 / (face_phase_grad_value.norm() +
223 std::numeric_limits<double>::min())) *
224 JxW_face;
225 }
226 }
227 }
228 }
229 }
230 }
231}
232
233template <int dim>
234void
236 const CahnHilliardScratchData<dim> &scratch_data,
237 StabilizedMethodsCopyData &copy_data)
238{
239 if (!scratch_data.is_boundary_cell)
240 return;
241
242 const double epsilon = this->epsilon;
243
244 auto &local_rhs = copy_data.local_rhs;
245
246
247 for (unsigned int f = 0; f < scratch_data.n_faces; f++)
248 {
249 // Check if the face is on a boundary
250 if (scratch_data.is_boundary_face[f])
251 {
252 if (this->cahn_hilliard_boundary_conditions.type.at(
253 scratch_data.boundary_face_id[f]) ==
255 {
256 const double angle_of_contact =
257 this->cahn_hilliard_boundary_conditions.angle_of_contact.at(
258 scratch_data.boundary_face_id[f]);
259 for (unsigned int q = 0; q < scratch_data.n_faces_q_points; ++q)
260 {
261 const Tensor<1, dim> face_phase_grad_value =
262 scratch_data.face_phase_grad_values[f][q];
263
264 const double lambda = 3 * epsilon *
265 scratch_data.surface_tension[q] /
266 (2 * std::numbers::sqrt2);
267
268 const double JxW_face = scratch_data.face_JxW[f][q];
269
270 for (unsigned int i = 0; i < scratch_data.n_dofs; ++i)
271 {
272 const double phi_potential_i =
273 scratch_data.phi_potential[q][i];
274
275 local_rhs(i) +=
276 lambda * phi_potential_i *
277 std::cos(angle_of_contact * M_PI / 180.0) *
278 (face_phase_grad_value.norm()) * JxW_face;
279 }
280 }
281 }
282 }
283 }
284}
285
288
289template <int dim>
290void
292 const CahnHilliardScratchData<dim> &scratch_data,
293 StabilizedMethodsCopyData &copy_data)
294{
295 if (!scratch_data.is_boundary_cell)
296 return;
297
298 const double epsilon = this->epsilon;
299
300
301 auto &local_matrix = copy_data.local_matrix;
302
303 for (unsigned int f = 0; f < scratch_data.n_faces; f++)
304 {
305 // Check if the face is on a boundary
306 if (scratch_data.is_boundary_face[f])
307 {
309 scratch_data.boundary_face_id[f]) ==
311 {
312 for (unsigned int q = 0; q < scratch_data.n_faces_q_points; ++q)
313 {
314 const Tensor<1, dim> face_normal =
315 scratch_data.face_normal[f][q];
316 const double JxW_face = scratch_data.face_JxW[f][q];
317
318 const double lambda = 3 * epsilon *
319 scratch_data.surface_tension[q] /
320 (2 * std::numbers::sqrt2);
321
322 for (unsigned int i = 0; i < scratch_data.n_dofs; ++i)
323 {
324 const double phi_potential_i =
325 scratch_data.phi_potential[q][i];
326
327 for (unsigned int j = 0; j < scratch_data.n_dofs; ++j)
328 {
329 const Tensor<1, dim> grad_phi_face_phase_j =
330 scratch_data.grad_phi_face_phase[f][q][j];
331
332 local_matrix(i, j) -= lambda * phi_potential_i *
333 grad_phi_face_phase_j *
334 face_normal * JxW_face;
335 }
336 }
337 }
338 }
339 }
340 }
341}
342
343template <int dim>
344void
346 const CahnHilliardScratchData<dim> &scratch_data,
347 StabilizedMethodsCopyData &copy_data)
348{
349 if (!scratch_data.is_boundary_cell)
350 return;
351
352 const double epsilon = this->epsilon;
353
354 auto &local_rhs = copy_data.local_rhs;
355
356 for (unsigned int f = 0; f < scratch_data.n_faces; f++)
357 {
358 // Check if the face is on a boundary
359 if (scratch_data.is_boundary_face[f])
360 {
361 if (this->cahn_hilliard_boundary_conditions.type.at(
362 scratch_data.boundary_face_id[f]) ==
364 {
365 for (unsigned int q = 0; q < scratch_data.n_faces_q_points; ++q)
366 {
367 const Tensor<1, dim> face_phase_grad_value =
368 scratch_data.face_phase_grad_values[f][q];
369 const Tensor<1, dim> face_normal =
370 scratch_data.face_normal[f][q];
371 const double JxW_face = scratch_data.face_JxW[f][q];
372
373 const double lambda = 3 * epsilon *
374 scratch_data.surface_tension[q] /
375 (2 * std::numbers::sqrt2);
376
377 for (unsigned int i = 0; i < scratch_data.n_dofs; ++i)
378 {
379 const double phi_potential_i =
380 scratch_data.phi_potential[q][i];
381
382 local_rhs(i) += lambda * phi_potential_i *
383 face_phase_grad_value * face_normal *
384 JxW_face;
385 }
386 }
387 }
388 }
389 }
390}
391
394
395
396template <int dim>
397void
399 const CahnHilliardScratchData<dim> &scratch_data,
400 StabilizedMethodsCopyData &copy_data)
401{
402 // Loop and quadrature information
403 const auto &JxW = scratch_data.JxW;
404 const unsigned int n_q_points = scratch_data.n_q_points;
405 const unsigned int n_dofs = scratch_data.n_dofs;
406
407 // Copy data elements
408 auto &local_matrix = copy_data.local_matrix;
409
410 // Time stepping information
411 const auto method = this->simulation_control->get_assembly_method();
412 // Vector for the BDF coefficients
413 const Vector<double> &bdf_coefs =
414 this->simulation_control->get_bdf_coefficients();
415 std::vector<double> phase_order(1 + number_of_previous_solutions(method));
416
417 // Loop over the quadrature points
418 for (unsigned int q = 0; q < n_q_points; ++q)
419 {
420 phase_order[0] = scratch_data.phase_order_values[q];
421 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
422 phase_order[p + 1] = scratch_data.previous_phase_order_values[p][q];
423
424 for (unsigned int i = 0; i < n_dofs; ++i)
425 {
426 const double phi_phase_i = scratch_data.phi_phase[q][i];
427 for (unsigned int j = 0; j < n_dofs; ++j)
428 {
429 const double phi_phase_j = scratch_data.phi_phase[q][j];
430
431 local_matrix(i, j) +=
432 phi_phase_j * phi_phase_i * bdf_coefs[0] * JxW[q];
433 }
434 }
435 }
436}
437
438template <int dim>
439void
441 const CahnHilliardScratchData<dim> &scratch_data,
442 StabilizedMethodsCopyData &copy_data)
443{
444 // Loop and quadrature information
445 const auto &JxW = scratch_data.JxW;
446 const unsigned int n_q_points = scratch_data.n_q_points;
447 const unsigned int n_dofs = scratch_data.n_dofs;
448
449 // Copy data elements
450 auto &local_rhs = copy_data.local_rhs;
451
452 // Time stepping information
453 const auto method = this->simulation_control->get_assembly_method();
454 // Vector for the BDF coefficients
455 const Vector<double> &bdf_coefs =
456 this->simulation_control->get_bdf_coefficients();
457 std::vector<double> phase_order(1 + number_of_previous_solutions(method));
458
459 // Loop over the quadrature points
460 for (unsigned int q = 0; q < n_q_points; ++q)
461 {
462 phase_order[0] = scratch_data.phase_order_values[q];
463 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
464 phase_order[p + 1] = scratch_data.previous_phase_order_values[p][q];
465
466 for (unsigned int i = 0; i < n_dofs; ++i)
467 {
468 const double phi_phase_i = scratch_data.phi_phase[q][i];
469 double local_rhs_i = 0;
470 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
471 ++p)
472 {
473 local_rhs_i -= bdf_coefs[p] * (phase_order[p] * phi_phase_i);
474 }
475 local_rhs(i) += local_rhs_i * JxW[q];
476 }
477 }
478}
479
480template class CahnHilliardAssemblerBDF<2>;
481template class CahnHilliardAssemblerBDF<3>;
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
Class that assembles the boundary condition on the angle of contact in the Cahn-Hilliard equations.
virtual void assemble_matrix(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_matrix Assembles the matrix
const BoundaryConditions::CahnHilliardBoundaryConditions< dim > & cahn_hilliard_boundary_conditions
virtual void assemble_rhs(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_rhs Assembles the rhs
Class that assembles the transient time arising from BDF time integration for the Cahn-Hilliard equat...
const std::shared_ptr< SimulationControl > simulation_control
virtual void assemble_matrix(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_matrix Assembles the matrix
virtual void assemble_rhs(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_rhs Assembles the rhs
Class that assembles the core of the Cahn-Hilliard equation : .
virtual void assemble_rhs(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_rhs Assembles the rhs
const Parameters::CahnHilliard cahn_hilliard_parameters
virtual void assemble_matrix(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_matrix Assembles the matrix
Class that assembles the boundary condition that allows a free angle of contact, thus adding a degree...
virtual void assemble_matrix(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_matrix Assembles the matrix
virtual void assemble_rhs(const CahnHilliardScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
assemble_rhs Assembles the rhs
const BoundaryConditions::CahnHilliardBoundaryConditions< dim > & cahn_hilliard_boundary_conditions
Class that stores the information required by the assembly procedure for the Cahn-Hilliard equations....
std::vector< double > source_chemical_potential
std::vector< double > surface_tension
std::vector< double > mobility_cahn_hilliard_gradient
std::vector< double > source_phase_order
std::vector< unsigned int > boundary_face_id
std::vector< std::vector< Tensor< 1, dim > > > face_normal
std::vector< double > mobility_cahn_hilliard
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
std::vector< std::vector< Tensor< 1, dim > > > face_phase_grad_values
Table< 3, Tensor< 1, dim > > grad_phi_face_phase
Table< 2, Tensor< 1, dim > > grad_phi_potential
std::vector< Tensor< 1, dim > > phase_order_gradients
std::vector< double > phase_order_values
std::vector< std::vector< double > > previous_phase_order_values
Class responsible for storing the information calculated using the assembly of stabilized scalar equa...
Definition copy_data.h:67
Vector< double > local_rhs
Definition copy_data.h:105
FullMatrix< double > local_matrix
Definition copy_data.h:104