Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
tracer_assemblers.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021-2024, 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
7#include <solvers/copy_data.h>
9
10
11template <int dim>
12void
14 const TracerScratchData<dim> &scratch_data,
16{
17 // Scheme and physical properties
18 const std::vector<double> &diffusivity_vector =
19 scratch_data.tracer_diffusivity;
20 const auto method = this->simulation_control->get_assembly_method();
21
22 // Loop and quadrature informations
23 const auto &JxW_vec = scratch_data.JxW;
24 const unsigned int n_q_points = scratch_data.n_q_points;
25 const unsigned int n_dofs = scratch_data.n_dofs;
26 const double h = scratch_data.cell_size;
27
28 // Time steps and inverse time steps which is used for stabilization constant
29 std::vector<double> time_steps_vector =
30 this->simulation_control->get_time_steps_vector();
31 const double dt = time_steps_vector[0];
32 const double sdt = 1. / dt;
33
34 // Copy data elements
35 auto &strong_jacobian_vec = copy_data.strong_jacobian;
36 auto &local_matrix = copy_data.local_matrix;
37
38 // assembling local matrix and right hand side
39 for (unsigned int q = 0; q < n_q_points; ++q)
40 {
41 // Gather into local variables the relevant fields
42 const double diffusivity = diffusivity_vector[q];
43 const Tensor<1, dim> &tracer_gradient = scratch_data.tracer_gradients[q];
44 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
45
46 // Store JxW in local variable for faster access;
47 const double JxW = JxW_vec[q];
48
49 // Implementation of a Discontinuity-Capturing Directional Dissipation
50 // (DCDD) shock capturing scheme. For more information see Tezduyar,
51 // T. E. (2003). Computation of moving boundaries and interfaces and
52 // stabilization parameters. International Journal for Numerical
53 // Methods in Fluids, 43(5), 555-575. Our implementation is based on
54 // equations (70) and (79), which are adapted for the tracer solver.
55
56 // We use the previous concentration for the shock capture if the
57 // simulation is transient
58 const Tensor<1, dim> tracer_gradient_for_dcdd =
59 is_steady(method) ? tracer_gradient :
60 scratch_data.previous_tracer_gradients[q];
61 const double tracer_gradient_for_dcdd_norm =
62 tracer_gradient_for_dcdd.norm();
63
64 // Shock capturing viscosity term
65 const double order = scratch_data.fe_values_tracer.get_fe().degree;
66
67 const double vdcdd = (0.5 * h) * (velocity.norm() * velocity.norm()) *
68 pow(tracer_gradient_for_dcdd.norm() * h, order);
69
70 const double tolerance = 1e-12;
71 Tensor<1, dim> s = velocity / (velocity.norm() + tolerance);
72 Tensor<1, dim> r =
73 tracer_gradient_for_dcdd / (tracer_gradient_for_dcdd_norm + tolerance);
74
75 const Tensor<2, dim> k_corr = (r * s) * outer_product(s, s);
76 const Tensor<2, dim> rr = outer_product(r, r);
77 const Tensor<2, dim> dcdd_factor = rr - k_corr;
78
79 // If the method is not steady, we use the previous gradient to calculate
80 // the DCDD schock capture term and consequently we do not need an
81 // estimation of the derivative of the vdcdd term.
82 const double d_vdcdd =
83 is_steady(method) ?
84 order * (0.5 * h * h) * (velocity.norm() * velocity.norm()) *
85 pow(tracer_gradient_for_dcdd_norm * h, order - 1) :
86 0;
87
88 // Calculation of the magnitude of the velocity for the
89 // stabilization parameter
90 const double u_mag = std::max(velocity.norm(), tolerance);
91
92 // Calculation of the GLS stabilization parameter. The
93 // stabilization parameter used is different if the simulation is
94 // steady or unsteady. In the unsteady case it includes the value
95 // of the time step
96 const double tau =
97 is_steady(method) ?
98 1. / std::sqrt(std::pow(2. * u_mag / h, 2) +
99 9 * std::pow(4 * diffusivity / (h * h), 2)) :
100 1. / std::sqrt(std::pow(sdt, 2) + std::pow(2. * u_mag / h, 2) +
101 9 * std::pow(4 * diffusivity / (h * h), 2));
102
103 for (unsigned int j = 0; j < n_dofs; ++j)
104 {
105 const Tensor<1, dim> &grad_phi_C_j = scratch_data.grad_phi[q][j];
106 const double &laplacian_phi_C_j = scratch_data.laplacian_phi[q][j];
107 strong_jacobian_vec[q][j] +=
108 velocity * grad_phi_C_j - diffusivity * laplacian_phi_C_j;
109 }
110
111 for (unsigned int i = 0; i < n_dofs; ++i)
112 {
113 const double &phi_C_i = scratch_data.phi[q][i];
114 const Tensor<1, dim> &grad_phi_C_i = scratch_data.grad_phi[q][i];
115
116 for (unsigned int j = 0; j < n_dofs; ++j)
117 {
118 const Tensor<1, dim> &grad_phi_C_j = scratch_data.grad_phi[q][j];
119
120 // Weak form : - D * laplacian C + u * gradC - f = 0
121 local_matrix(i, j) += (diffusivity * grad_phi_C_i * grad_phi_C_j +
122 phi_C_i * velocity * grad_phi_C_j) *
123 JxW;
124
125 // GLS stabilization
126 local_matrix(i, j) += tau * strong_jacobian_vec[q][j] *
127 (grad_phi_C_i * velocity) * JxW;
128
129 // DCDD shock-capturing
130 local_matrix(i, j) +=
131 (vdcdd *
132 scalar_product(grad_phi_C_i, dcdd_factor * grad_phi_C_j) +
133 d_vdcdd * grad_phi_C_j.norm() *
134 scalar_product(grad_phi_C_i,
135 dcdd_factor * tracer_gradient)) *
136 JxW;
137 }
138 }
139 } // end loop on quadrature points
140}
141
142
143template <int dim>
144void
146 const TracerScratchData<dim> &scratch_data,
147 StabilizedMethodsCopyData &copy_data)
148{
149 // Scheme and physical properties
150 const std::vector<double> &diffusivity_vector =
151 scratch_data.tracer_diffusivity;
152 const auto method = this->simulation_control->get_assembly_method();
153
154 // Loop and quadrature information
155 const auto &JxW_vec = scratch_data.JxW;
156 const unsigned int n_q_points = scratch_data.n_q_points;
157 const unsigned int n_dofs = scratch_data.n_dofs;
158 const double h = scratch_data.cell_size;
159
160 // Time steps and inverse time steps which is used for stabilization constant
161 std::vector<double> time_steps_vector =
162 this->simulation_control->get_time_steps_vector();
163 const double dt = time_steps_vector[0];
164 const double sdt = 1. / dt;
165
166 // Copy data elements
167 auto &strong_residual_vec = copy_data.strong_residual;
168 auto &local_rhs = copy_data.local_rhs;
169
170 // assembling local matrix and right hand side
171 for (unsigned int q = 0; q < n_q_points; ++q)
172 {
173 // Gather into local variables the relevant fields
174 const double diffusivity = diffusivity_vector[q];
175 const Tensor<1, dim> &tracer_gradient = scratch_data.tracer_gradients[q];
176 const Tensor<1, dim> &previous_tracer_gradient =
177 scratch_data.previous_tracer_gradients[q];
178 const double &tracer_laplacian = scratch_data.tracer_laplacians[q];
179 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
180
181 // Store JxW in local variable for faster access;
182 const double JxW = JxW_vec[q];
183
184 // We use the previous concentration for the shock capture if the
185 // simulation is transient
186 const Tensor<1, dim> tracer_gradient_for_dcdd =
187 is_steady(method) ? tracer_gradient : previous_tracer_gradient;
188 const double tracer_gradient_for_dcdd_norm =
189 tracer_gradient_for_dcdd.norm();
190
191 // Shock capturing viscosity term
192 const double order = scratch_data.fe_values_tracer.get_fe().degree;
193
194
195 const double vdcdd = (0.5 * h) * (velocity.norm() * velocity.norm()) *
196 pow(tracer_gradient_for_dcdd_norm * h, order);
197
198 const double tolerance = 1e-12;
199 Tensor<1, dim> s = velocity / (velocity.norm() + tolerance);
200 Tensor<1, dim> r =
201 tracer_gradient_for_dcdd / (tracer_gradient_for_dcdd_norm + tolerance);
202
203 const Tensor<2, dim> k_corr = (r * s) * outer_product(s, s);
204 const Tensor<2, dim> rr = outer_product(r, r);
205 const Tensor<2, dim> dcdd_factor = rr - k_corr;
206
207 // Calculation of the magnitude of the velocity for the
208 // stabilization parameter
209 const double u_mag = std::max(velocity.norm(), tolerance);
210
211 // Calculation of the GLS stabilization parameter. The
212 // stabilization parameter used is different if the simulation is
213 // steady or unsteady. In the unsteady case it includes the value
214 // of the time step
215 const double tau =
216 is_steady(method) ?
217 1. / std::sqrt(std::pow(2. * u_mag / h, 2) +
218 9 * std::pow(4 * diffusivity / (h * h), 2)) :
219 1. / std::sqrt(std::pow(sdt, 2) + std::pow(2. * u_mag / h, 2) +
220 9 * std::pow(4 * diffusivity / (h * h), 2));
221
222 // Calculate the strong residual for GLS stabilization
223 strong_residual_vec[q] +=
224 velocity * tracer_gradient - diffusivity * tracer_laplacian;
225
226 for (unsigned int i = 0; i < n_dofs; ++i)
227 {
228 const double &phi_C_i = scratch_data.phi[q][i];
229 const Tensor<1, dim> &grad_phi_C_i = scratch_data.grad_phi[q][i];
230
231 // RHS for: - D * laplacian C + u * grad C - f = 0
232 local_rhs(i) -= (diffusivity * grad_phi_C_i * tracer_gradient +
233 phi_C_i * velocity * tracer_gradient -
234 scratch_data.source[q] * phi_C_i) *
235 JxW;
236
237 // GLS stabilization
238 local_rhs(i) -=
239 tau * (strong_residual_vec[q] * (grad_phi_C_i * velocity)) * JxW;
240
241 // DCDD shock-capturing
242 local_rhs(i) -=
243 vdcdd *
244 scalar_product(grad_phi_C_i, dcdd_factor * tracer_gradient) * JxW;
245 }
246 } // end loop on quadrature points
247}
248
249template class TracerAssemblerCore<2>;
250template class TracerAssemblerCore<3>;
251
252template <int dim>
253void
255 const TracerScratchData<dim> &scratch_data,
256 StabilizedMethodsCopyData &copy_data)
257{
258 // Scheme and physical properties
259 const std::vector<double> &diffusivity_vector =
260 scratch_data.tracer_diffusivity;
261
262 // Loop and quadrature informations
263 const auto &JxW_vec = scratch_data.JxW;
264 const unsigned int n_q_points = scratch_data.n_q_points;
265 const unsigned int n_dofs = scratch_data.n_dofs;
266
267 // Copy data elements
268 auto &local_matrix = copy_data.local_matrix;
269
270 // assembling local matrix and right hand side
271 for (unsigned int q = 0; q < n_q_points; ++q)
272 {
273 // Gather into local variables the relevant fields
274 const double diffusivity = diffusivity_vector[q];
275 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
276 const double &velocity_divergence = scratch_data.velocity_divergences[q];
277
278 // Store JxW in local variable for faster access;
279 const double JxW = JxW_vec[q];
280
281 for (unsigned int i = 0; i < n_dofs; ++i)
282 {
283 const double &phi_C_i = scratch_data.phi[q][i];
284 const Tensor<1, dim> &grad_phi_C_i = scratch_data.grad_phi[q][i];
285
286 for (unsigned int j = 0; j < n_dofs; ++j)
287 {
288 const double &phi_C_j = scratch_data.phi[q][j];
289 const Tensor<1, dim> &grad_phi_C_j = scratch_data.grad_phi[q][j];
290
291 // Weak form of: - D * laplacian C + u * gradC - f = 0
292 // Note that the advection term has been weakened for it to appear
293 // explicitly in the weak form as a boundary term.
294 local_matrix(i, j) += (diffusivity * grad_phi_C_i * grad_phi_C_j -
295 grad_phi_C_i * velocity * phi_C_j) *
296 JxW;
297
298 // This term is added to correct for the influence of the
299 // non-divergence-free velocity field on the concentration.
300 local_matrix(i, j) +=
301 -phi_C_i * velocity_divergence * phi_C_j * JxW;
302 }
303 }
304 } // end loop on quadrature points
305}
306
307
308template <int dim>
309void
311 const TracerScratchData<dim> &scratch_data,
312 StabilizedMethodsCopyData &copy_data)
313{
314 // Scheme and physical properties
315 const std::vector<double> &diffusivity_vector =
316 scratch_data.tracer_diffusivity;
317
318 // Loop and quadrature informations
319 const auto &JxW_vec = scratch_data.JxW;
320 const unsigned int n_q_points = scratch_data.n_q_points;
321 const unsigned int n_dofs = scratch_data.n_dofs;
322
323 // Copy data elements
324 auto &local_rhs = copy_data.local_rhs;
325
326 // Assembling local right hand side
327 for (unsigned int q = 0; q < n_q_points; ++q)
328 {
329 // Gather into local variables the relevant fields
330 const double diffusivity = diffusivity_vector[q];
331 const double &tracer_value = scratch_data.tracer_values[q];
332 const Tensor<1, dim> &tracer_gradient = scratch_data.tracer_gradients[q];
333 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
334 const double &velocity_divergence = scratch_data.velocity_divergences[q];
335
336 // Store JxW in local variable for faster access;
337 const double JxW = JxW_vec[q];
338
339 for (unsigned int i = 0; i < n_dofs; ++i)
340 {
341 const double &phi_C_i = scratch_data.phi[q][i];
342 const Tensor<1, dim> &grad_phi_C_i = scratch_data.grad_phi[q][i];
343
344 // RHS for: - D * laplacian C + u * grad C - f = 0
345 local_rhs(i) -= (diffusivity * grad_phi_C_i * tracer_gradient -
346 grad_phi_C_i * velocity * tracer_value -
347 scratch_data.source[q] * phi_C_i) *
348 JxW;
349
350 // This term is added to correct for the influence of the
351 // non-divergence-free velocity field on the concentration.
352 local_rhs(i) -= -phi_C_i * velocity_divergence * tracer_value * JxW;
353 }
354 } // end loop on quadrature points
355}
356
357template class TracerAssemblerDGCore<2>;
358template class TracerAssemblerDGCore<3>;
359
360
361template <int dim>
362void
364 const TracerScratchData<dim> &scratch_data,
365 StabilizedMethodsCopyData &copy_data)
366{
367 // Loop and quadrature informations
368 const auto &JxW = scratch_data.JxW;
369 const unsigned int n_q_points = scratch_data.n_q_points;
370 const unsigned int n_dofs = scratch_data.n_dofs;
371
372 // Copy data elements
373 auto &strong_residual = copy_data.strong_residual;
374 auto &strong_jacobian = copy_data.strong_jacobian;
375 auto &local_matrix = copy_data.local_matrix;
376
377 // Time stepping information
378 const auto method = this->simulation_control->get_assembly_method();
379 // Vector for the BDF coefficients
380 const Vector<double> &bdf_coefs =
381 this->simulation_control->get_bdf_coefficients();
382 std::vector<double> tracer(1 + number_of_previous_solutions(method));
383
384 // Loop over the quadrature points
385 for (unsigned int q = 0; q < n_q_points; ++q)
386 {
387 tracer[0] = scratch_data.tracer_values[q];
388 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
389 tracer[p + 1] = scratch_data.previous_tracer_values[p][q];
390
391 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
392 ++p)
393 {
394 strong_residual[q] += bdf_coefs[p] * tracer[p];
395 }
396
397 for (unsigned int j = 0; j < n_dofs; ++j)
398 {
399 strong_jacobian[q][j] += bdf_coefs[0] * scratch_data.phi[q][j];
400 }
401
402
403 for (unsigned int i = 0; i < n_dofs; ++i)
404 {
405 const double phi_u_i = scratch_data.phi[q][i];
406 for (unsigned int j = 0; j < n_dofs; ++j)
407 {
408 const double phi_u_j = scratch_data.phi[q][j];
409
410 local_matrix(i, j) += phi_u_j * phi_u_i * bdf_coefs[0] * JxW[q];
411 }
412 }
413 }
414}
415
416template <int dim>
417void
419 const TracerScratchData<dim> &scratch_data,
420 StabilizedMethodsCopyData &copy_data)
421{
422 // Loop and quadrature informations
423 const auto &JxW = scratch_data.JxW;
424 const unsigned int n_q_points = scratch_data.n_q_points;
425 const unsigned int n_dofs = scratch_data.n_dofs;
426
427 // Copy data elements
428 auto &strong_residual = copy_data.strong_residual;
429 auto &local_rhs = copy_data.local_rhs;
430
431 // Time stepping information
432 const auto method = this->simulation_control->get_assembly_method();
433 // Vector for the BDF coefficients
434 const Vector<double> &bdf_coefs =
435 this->simulation_control->get_bdf_coefficients();
436 std::vector<double> tracer(1 + number_of_previous_solutions(method));
437
438 // Loop over the quadrature points
439 for (unsigned int q = 0; q < n_q_points; ++q)
440 {
441 tracer[0] = scratch_data.tracer_values[q];
442 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
443 tracer[p + 1] = scratch_data.previous_tracer_values[p][q];
444
445 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
446 ++p)
447 {
448 strong_residual[q] += (bdf_coefs[p] * tracer[p]);
449 }
450
451 for (unsigned int i = 0; i < n_dofs; ++i)
452 {
453 const double phi_u_i = scratch_data.phi[q][i];
454 double local_rhs_i = 0;
455 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
456 ++p)
457 {
458 local_rhs_i -= bdf_coefs[p] * (tracer[p] * phi_u_i);
459 }
460 local_rhs(i) += local_rhs_i * JxW[q];
461 }
462 }
463}
464
465template class TracerAssemblerBDF<2>;
466template class TracerAssemblerBDF<3>;
467
468
469
470template <int dim>
471void
473 const TracerScratchData<dim> &scratch_data,
475{
476 const double penalty_factor = scratch_data.penalty_factor;
477 const FEInterfaceValues<dim> &fe_iv = scratch_data.fe_interface_values_tracer;
478 const auto &q_points = fe_iv.get_quadrature_points();
479 auto &copy_data_face = copy_data.face_data.back();
480 const unsigned int n_dofs = fe_iv.n_current_interface_dofs();
481
482
483 const std::vector<double> &JxW = fe_iv.get_JxW_values();
484 const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
485
486 for (unsigned int q = 0; q < q_points.size(); ++q)
487 {
488 const double velocity_dot_n =
489 scratch_data.face_velocity_values[q] * normals[q];
490
491 // Shape functions of the upwind cell
492 const auto &phi_upwind = (velocity_dot_n > 0.) ?
493 scratch_data.face_phi_here[q] :
494 scratch_data.face_phi_there[q];
495 const auto &jump_phi = scratch_data.face_jump_phi[q];
496 const auto &avg_grad_phi_n = scratch_data.face_average_grad_phi_dot_n[q];
497
498 const double advection_factor = velocity_dot_n * JxW[q];
499 const double diffusion_factor =
500 scratch_data.tracer_diffusivity_face[q] * JxW[q];
501 const double penalty_diffusion_factor = penalty_factor * diffusion_factor;
502
503 for (unsigned int i = 0; i < n_dofs; ++i)
504 {
505 const double jump_phi_i = jump_phi[i];
506 const double avg_grad_phi_n_i = avg_grad_phi_n[i];
507
508 for (unsigned int j = 0; j < n_dofs; ++j)
509 {
510 // Advection term with upwinding:
511 // [\phi_i] * \phi_j^{upwind} * (u . n)
512 copy_data_face.face_matrix(i, j) +=
513 jump_phi_i * phi_upwind[j] * advection_factor;
514
515 // Assemble the diffusion term using Nitsche
516 // symmetric interior penalty method. See Larson Chap. 14. P.362
517 copy_data_face.face_matrix(i, j) +=
518 diffusion_factor * (-avg_grad_phi_n[j] * jump_phi_i -
519 avg_grad_phi_n_i * jump_phi[j]) +
520 penalty_diffusion_factor * jump_phi[j] * jump_phi_i;
521 }
522 }
523 }
524}
525
526template <int dim>
527void
529 const TracerScratchData<dim> &scratch_data,
531{
532 const double penalty_factor = scratch_data.penalty_factor;
533 const FEInterfaceValues<dim> &fe_iv = scratch_data.fe_interface_values_tracer;
534 const auto &q_points = fe_iv.get_quadrature_points();
535 const unsigned int n_dofs = fe_iv.n_current_interface_dofs();
536
537 auto &copy_data_face = copy_data.face_data.back();
538
539 const std::vector<double> &JxW = fe_iv.get_JxW_values();
540 const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
541
542 for (unsigned int q = 0; q < q_points.size(); ++q)
543 {
544 const double velocity_dot_n =
545 scratch_data.face_velocity_values[q] * normals[q];
546
547 // Tracer value of the upwind cell
548 const double upwind_value = (velocity_dot_n > 0) ?
549 scratch_data.values_here[q] :
550 scratch_data.values_there[q];
551
552 const auto &jump_phi = scratch_data.face_jump_phi[q];
553 const auto &avg_grad_phi_n = scratch_data.face_average_grad_phi_dot_n[q];
554
555 const double advective_flux = upwind_value * velocity_dot_n * JxW[q];
556 const double diffusion_factor =
557 scratch_data.tracer_diffusivity_face[q] * JxW[q];
558 const double tracer_jump = scratch_data.tracer_value_jump[q];
559 const double tracer_average_gradient_n =
560 scratch_data.tracer_average_gradient[q] * normals[q];
561
562 for (unsigned int i = 0; i < n_dofs; ++i)
563 {
564 // Assemble advection terms with upwinding:
565 // [\phi_i] * C^{upwind} * (u . n)
566 copy_data_face.face_rhs(i) -= jump_phi[i] * advective_flux;
567
568 // Assemble the diffusion term using Nitsche symmetric interior
569 // penalty method. See Larson Chap. 14. P.362
570 copy_data_face.face_rhs(i) -=
571 diffusion_factor * (-tracer_average_gradient_n * jump_phi[i] -
572 tracer_jump * avg_grad_phi_n[i] +
573 penalty_factor * tracer_jump * jump_phi[i]);
574 }
575 }
576}
577
578template class TracerAssemblerSIPG<2>;
579template class TracerAssemblerSIPG<3>;
580
581
582template <int dim>
583void
585 const TracerScratchData<dim> &scratch_data,
587{
588 const unsigned int boundary_index = scratch_data.boundary_index;
589 const FEFaceValuesBase<dim> &fe_face =
590 scratch_data.fe_interface_values_tracer.get_fe_face_values(0);
591 const auto &q_points = fe_face.get_quadrature_points();
592
593 const double beta = scratch_data.penalty_factor;
594
595 const unsigned int n_facet_dofs = fe_face.get_fe().n_dofs_per_cell();
596 const std::vector<double> &JxW = fe_face.get_JxW_values();
597 const std::vector<Tensor<1, dim>> &normals = fe_face.get_normal_vectors();
598
599 const auto boundary_type = tracer_boundary_conditions.type[boundary_index];
600
601 // Only outlet and Dirichlet boundaries contribute to the matrix
604 return;
605
606 const bool is_dirichlet =
608
609 // Normal gradient of the shape functions at a quadrature point
610 std::vector<double> grad_phi_n(n_facet_dofs);
611
612 for (unsigned int point = 0; point < q_points.size(); ++point)
613 {
614 const double velocity_dot_n =
615 scratch_data.face_velocity_values[point] * normals[point];
616
617 // The advection term is only applied to the outflow part of the boundary
618 const double advection_factor =
619 (velocity_dot_n > 0) ? velocity_dot_n * JxW[point] : 0.;
620
621 if (!is_dirichlet)
622 {
623 if (velocity_dot_n > 0)
624 for (unsigned int i = 0; i < n_facet_dofs; ++i)
625 {
626 const double phi_i = fe_face.shape_value(i, point);
627 for (unsigned int j = 0; j < n_facet_dofs; ++j)
628 copy_data.local_matrix(i, j) +=
629 phi_i * fe_face.shape_value(j, point) * advection_factor;
630 }
631 continue;
632 }
633
634 const double diffusion_factor =
635 scratch_data.tracer_diffusivity_face[point] * JxW[point];
636
637 for (unsigned int j = 0; j < n_facet_dofs; ++j)
638 grad_phi_n[j] = fe_face.shape_grad(j, point) * normals[point];
639
640 for (unsigned int i = 0; i < n_facet_dofs; ++i)
641 {
642 const double phi_i = fe_face.shape_value(i, point);
643 for (unsigned int j = 0; j < n_facet_dofs; ++j)
644 {
645 const double phi_j = fe_face.shape_value(j, point);
646
647 copy_data.local_matrix(i, j) += phi_i * phi_j * advection_factor;
648
649 copy_data.local_matrix(i, j) +=
650 diffusion_factor *
651 (-phi_i * grad_phi_n[j] - phi_j * grad_phi_n[i] +
652 beta * phi_i * phi_j);
653 }
654 }
655 }
656}
657
658template <int dim>
659void
661 const TracerScratchData<dim> &scratch_data,
663{
664 const unsigned int boundary_index = scratch_data.boundary_index;
665 const FEFaceValuesBase<dim> &fe_face =
666 scratch_data.fe_interface_values_tracer.get_fe_face_values(0);
667 const auto &q_points = fe_face.get_quadrature_points();
668
669 const double beta = scratch_data.penalty_factor;
670
671 const unsigned int n_facet_dofs = fe_face.get_fe().n_dofs_per_cell();
672 const std::vector<double> &JxW = fe_face.get_JxW_values();
673 const std::vector<Tensor<1, dim>> &normals = fe_face.get_normal_vectors();
674
675 // If the boundary condition is an outlet, assumes that advection comes
676 // out since there is no inflow
677 if (tracer_boundary_conditions.type[boundary_index] ==
679 {
680 for (unsigned int point = 0; point < q_points.size(); ++point)
681 {
682 const double velocity_dot_n =
683 scratch_data.face_velocity_values[point] * normals[point];
684
685 for (unsigned int i = 0; i < n_facet_dofs; ++i)
686 copy_data.local_rhs(i) -= fe_face.shape_value(i, point) // \phi_i
687 * scratch_data.values_here[point] *
688 velocity_dot_n // u . n
689 * JxW[point]; // dx
690 }
691 }
692 // Else the boundary condition is a dirichlet. Evaluate the function and
693 // process it accordingly
694 else if (tracer_boundary_conditions.type[boundary_index] ==
696 {
697 std::vector<double> function_value(q_points.size());
698 tracer_boundary_conditions.tracer[boundary_index]->value_list(
699 q_points, function_value);
700
701 for (unsigned int point = 0; point < q_points.size(); ++point)
702 {
703 const double velocity_dot_n =
704 scratch_data.face_velocity_values[point] * normals[point];
705
706 for (unsigned int i = 0; i < n_facet_dofs; ++i)
707 {
708 if (velocity_dot_n < 0)
709 copy_data.local_rhs(i) -= fe_face.shape_value(i, point) *
710 function_value[point] *
711 velocity_dot_n * JxW[point];
712
713 if (velocity_dot_n > 0)
714 copy_data.local_rhs(i) -= fe_face.shape_value(i, point) *
715 scratch_data.values_here[point] *
716 velocity_dot_n * JxW[point];
717
718 copy_data.local_rhs(i) -=
719 scratch_data.tracer_diffusivity_face[point] *
720 (-fe_face.shape_value(i, point) *
721 scratch_data.gradients_here[point] * normals[point] -
722 (scratch_data.values_here[point] - function_value[point]) *
723 fe_face.shape_grad(i, point) * normals[point] +
724 beta * fe_face.shape_value(i, point) *
725 (scratch_data.values_here[point] - function_value[point])) *
726 JxW[point];
727 }
728 }
729 }
730 // Since the advection term is weakened, a boundary without contribution
731 // imposes a zero total (advective and diffusive) flux. This is the natural
732 // boundary condition, which is the expected behavior for walls.
733 else if (tracer_boundary_conditions.type[boundary_index] ==
735 {
736 return;
737 }
738 else if (tracer_boundary_conditions.type[boundary_index] ==
740 tracer_boundary_conditions.type[boundary_index] ==
742 {
743 AssertThrow(
744 false,
745 ExcMessage(
746 "Periodic boundary conditions cannot be used with the tracer physics when discontinous Galerkin elements are used."));
747 }
748 // If it is an unknown boundary condition, throw an exception. This case
749 // should never occur.
750 else
751 {
752 AssertThrow(false,
753 ExcMessage(
754 "No valid boundary conditions types were identified."));
755 }
756}
757
760
761template <int dim>
762void
764 const TracerScratchData<dim> &scratch_data,
765 StabilizedMethodsCopyData &copy_data)
766{
767 // Scheme and physical properties
768 const std::vector<double> &k = scratch_data.tracer_reaction_prefactor;
769
770 // Loop and quadrature information
771 const auto &JxW_vec = scratch_data.JxW;
772 const unsigned int n_q_points = scratch_data.n_q_points;
773 const unsigned int n_dofs = scratch_data.n_dofs;
774
775 auto &strong_jacobian_vec = copy_data.strong_jacobian;
776 auto &local_matrix = copy_data.local_matrix;
777
778 // Assembling local matrix
779 for (unsigned int q = 0; q < n_q_points; ++q)
780 {
781 const double reaction_coeff = k[q];
782 const double C = scratch_data.tracer_values[q];
783
784 // Update the strong Jacobian with the reaction term contribution:
785 // It comes from the derivative -d/dC (k * C) = -(k + C*dk/dC).
786 for (unsigned int j = 0; j < n_dofs; ++j)
787 {
788 const double &phi_C_j = scratch_data.phi[q][j];
789 strong_jacobian_vec[q][j] +=
790 -(reaction_coeff +
791 scratch_data.grad_tracer_reaction_prefactor[q] * C) *
792 phi_C_j;
793 }
794
795 // Store JxW in a local variable for faster access.
796 const double JxW = JxW_vec[q];
797
798 for (unsigned int i = 0; i < n_dofs; ++i)
799 {
800 const auto &phi_C_i = scratch_data.phi[q][i];
801
802 for (unsigned int j = 0; j < n_dofs; ++j)
803 {
804 const auto &phi_C_j = scratch_data.phi[q][j];
805
806 local_matrix(i, j) += reaction_coeff * phi_C_i * phi_C_j * JxW;
807 }
808 }
809 } // end loop on quadrature points
810}
811
812template <int dim>
813void
815 const TracerScratchData<dim> &scratch_data,
816 StabilizedMethodsCopyData &copy_data)
817{
818 // Scheme and physical properties
819 const std::vector<double> &k = scratch_data.tracer_reaction_prefactor;
820
821 // Loop and quadrature information
822 const auto &JxW_vec = scratch_data.JxW;
823 const unsigned int n_q_points = scratch_data.n_q_points;
824 const unsigned int n_dofs = scratch_data.n_dofs;
825
826 // Copy data elements
827 auto &strong_residual_vec = copy_data.strong_residual;
828 auto &local_rhs = copy_data.local_rhs;
829
830 // Assembling right-hand side
831 for (unsigned int q = 0; q < n_q_points; ++q)
832 {
833 // Store JxW in local variable for faster access
834 const double JxW = JxW_vec[q];
835
836 // Get the tracer concentration value at this quadrature point
837 const double C = scratch_data.tracer_values[q];
838
839 // Calculate the strong residual for GLS stabilization
840 strong_residual_vec[q] += -k[q] * C;
841
842 for (unsigned int i = 0; i < n_dofs; ++i)
843 {
844 const auto &phi_C_i = scratch_data.phi[q][i];
845
846 // Add reaction term to the RHS
847 local_rhs(i) -= k[q] * phi_C_i * C * JxW;
848 }
849 } // end loop on quadrature points
850}
851
852template class TracerAssemblerReaction<2>;
853template class TracerAssemblerReaction<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
Responsible for storing the information calculated using the discontinuous galerkin (DG) assembly of ...
Definition copy_data.h:127
std::vector< CopyDataFace > face_data
Definition copy_data.h:149
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
Vector< double > strong_residual
Definition copy_data.h:107
FullMatrix< double > local_matrix
Definition copy_data.h:104
std::vector< Vector< double > > strong_jacobian
Definition copy_data.h:108
Class that assembles the transient time arising from BDF time integration for the Tracer equations....
const std::shared_ptr< SimulationControl > simulation_control
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the rhs.
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the matrix.
Assembles Nitsche's method for boundary faces. This assembler is only required when solving the Trace...
BoundaryConditions::TracerBoundaryConditions< dim > tracer_boundary_conditions
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedDGMethodsCopyData &copy_data) override
Interface for the call to rhs.
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedDGMethodsCopyData &copy_data) override
Interface for the call to matrix assembly.
Class that assembles the core of the Tracer equation. This class assembles the weak form of: with an...
const std::shared_ptr< SimulationControl > simulation_control
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the rhs.
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the matrix.
Class that assembles the core (cells) of the Tracer equation for DG elements. This class assembles th...
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the rhs.
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the matrix.
Class that assembles the reaction source term of the Tracer equation. It assembles the term: Where C...
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the matrix.
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedMethodsCopyData &copy_data) override
Assembles the rhs.
Assembles the symmetric interior penalty (SIPG) method (or Nitsche's method) for internal faces....
virtual void assemble_matrix(const TracerScratchData< dim > &scratch_data, StabilizedDGMethodsCopyData &copy_data) override
Interface for the call to matrix assembly.
virtual void assemble_rhs(const TracerScratchData< dim > &scratch_data, StabilizedDGMethodsCopyData &copy_data) override
Interface for the call to rhs.
Class that stores the information required by the assembly procedure for a Tracer advection-diffusion...
std::vector< Tensor< 1, dim > > face_velocity_values
std::vector< double > tracer_values
std::vector< Tensor< 1, dim > > previous_tracer_gradients
Table< 2, double > face_average_grad_phi_dot_n
std::vector< double > values_there
std::vector< Tensor< 1, dim > > velocity_values
std::vector< double > grad_tracer_reaction_prefactor
std::vector< double > velocity_divergences
std::vector< std::vector< double > > previous_tracer_values
std::vector< double > tracer_diffusivity
Table< 1, double > JxW
FEInterfaceValues< dim > fe_interface_values_tracer
std::vector< Tensor< 1, dim > > tracer_average_gradient
std::vector< double > tracer_reaction_prefactor
Table< 2, double > laplacian_phi
std::vector< Tensor< 1, dim > > tracer_gradients
std::vector< double > source
std::vector< double > tracer_value_jump
std::vector< double > values_here
Table< 2, double > face_jump_phi
Table< 2, double > face_phi_there
Table< 2, double > face_phi_here
std::vector< double > tracer_laplacians
Table< 2, Tensor< 1, dim > > grad_phi
Table< 2, double > phi
std::vector< double > tracer_diffusivity_face
std::vector< Tensor< 1, dim > > gradients_here
FEValues< dim > fe_values_tracer
@ velocity
Velocity vector field from fluid dynamics.
@ tracer
Passive scalar tracer (Tracer).
bool is_steady(const Parameters::SimulationControl::TimeSteppingMethod method)
Determines if the time integration method is a steady-state method.