Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
time_harmonic_maxwell.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/multiphysics.h>
5
7
8#include <deal.II/base/work_stream.h>
9
10#include <deal.II/dofs/dof_renumbering.h>
11
12#include <deal.II/fe/fe_system.h>
13
14#include <deal.II/lac/lapack_support.h>
15#include <deal.II/lac/sparsity_tools.h>
16#include <deal.II/lac/trilinos_solver.h>
17
18#include <deal.II/numerics/vector_tools.h>
19
20template <int dim>
22 MultiphysicsInterface<dim> *multiphysics_interface,
23 const SimulationParameters<dim> &p_simulation_parameters,
24 const std::shared_ptr<parallel::DistributedTriangulationBase<dim>>
25 &p_triangulation,
26 std::shared_ptr<SimulationControl> p_simulation_control)
28 , multiphysics(multiphysics_interface)
29 , computing_timer(p_triangulation->get_mpi_communicator(),
30 this->pcout,
31 TimerOutput::never,
32 TimerOutput::wall_times)
33 , simulation_parameters(p_simulation_parameters)
34 , triangulation(p_triangulation)
35 , simulation_control(std::move(p_simulation_control))
37 std::make_shared<DoFHandler<dim>>(*triangulation))
39 std::make_shared<DoFHandler<dim>>(*triangulation))
40 , dof_handler_test(std::make_shared<DoFHandler<dim>>(*triangulation))
42 , extractor_E_imag(dim)
43 , extractor_H_real(2 * dim)
44 , extractor_H_imag(3 * dim)
45{
46 this->pcout << std::setprecision(simulation_control->get_log_precision())
47 << std::scientific;
48
49 if (simulation_parameters.mesh.simplex)
50 {
51 // for simplex meshes
52 AssertThrow(
53 false,
54 ExcMessage(
55 "TimeHarmonicMaxwell solver not yet implemented for simplex meshes."));
56 }
57 else
58 {
59 AssertThrow(dim == 3, TimeHarmonicMaxwellDimensionNotSupported(dim));
60
61 AssertThrow(
62 simulation_parameters.fem_parameters.electromagnetics_trial_degree <
63 simulation_parameters.fem_parameters.electromagnetics_test_degree,
64 ExcMessage(
65 "The DPG method requires the test space to be of higher order than the trial space."));
66
67 // Usual case, for quad/hex meshes
68 fe_trial_interior = std::make_shared<FESystem<dim>>(
69 FE_DGQ<dim>(
70 simulation_parameters.fem_parameters.electromagnetics_trial_degree) ^
71 dim,
72 FE_DGQ<dim>(
73 simulation_parameters.fem_parameters.electromagnetics_trial_degree) ^
74 dim,
75 FE_DGQ<dim>(
76 simulation_parameters.fem_parameters.electromagnetics_trial_degree) ^
77 dim,
78 FE_DGQ<dim>(
79 simulation_parameters.fem_parameters.electromagnetics_trial_degree) ^
80 dim);
81 fe_trial_skeleton = std::make_shared<FESystem<dim>>(
82 FE_Nedelec<dim>(
83 simulation_parameters.fem_parameters.electromagnetics_trial_degree),
84 FE_Nedelec<dim>(
85 simulation_parameters.fem_parameters.electromagnetics_trial_degree),
86 FE_Nedelec<dim>(
87 simulation_parameters.fem_parameters.electromagnetics_trial_degree),
88 FE_Nedelec<dim>(
89 simulation_parameters.fem_parameters.electromagnetics_trial_degree));
90 fe_test = std::make_shared<FESystem<dim>>(
91 FE_Nedelec<dim>(
92 simulation_parameters.fem_parameters.electromagnetics_test_degree),
93 FE_Nedelec<dim>(
94 simulation_parameters.fem_parameters.electromagnetics_test_degree),
95 FE_Nedelec<dim>(
96 simulation_parameters.fem_parameters.electromagnetics_test_degree),
97 FE_Nedelec<dim>(
98 simulation_parameters.fem_parameters.electromagnetics_test_degree));
99 mapping = std::make_shared<MappingQ<dim>>(fe_trial_interior->degree);
100 cell_quadrature = std::make_shared<QGauss<dim>>(fe_test->degree + 1);
101 face_quadrature = std::make_shared<QGauss<dim - 1>>(fe_test->degree + 1);
102 }
103
104 // Initialize solutions shared_ptr
105 present_solution = std::make_shared<GlobalVectorType>();
106 present_solution_skeleton = std::make_shared<GlobalVectorType>();
107
108 // Allocate solution transfer
109 solution_transfer = std::make_shared<SolutionTransfer<dim, GlobalVectorType>>(
111
112 // We may need the temperature field for the physical properties. If so,
113 // we create the corresponding FEValues object to evaluate the temperature at
114 // the quadrature points and its container to store the values.
115 const PhysicalPropertiesManager &physical_properties_manager =
116 simulation_parameters.physical_properties_manager;
117 unsigned int number_of_material_ids =
118 physical_properties_manager.get_number_of_fluids() +
119 physical_properties_manager.get_number_of_solids() - 1;
120
121 // The DPG error estimator is only needed when it is the error estimator of
122 // the electromagnetic fields.
123 const auto &adaptation_variables =
124 this->simulation_parameters.mesh_adaptation.variables;
125 const auto electromagnetic_adaptation =
126 adaptation_variables.find(Variable::electromagnetic_fields);
128 (electromagnetic_adaptation != adaptation_variables.end()) &&
129 (electromagnetic_adaptation->second.error_estimator ==
131
132 needs_temperature = false;
133 needs_void_fraction = false;
134 for (unsigned int material_id = 0; material_id <= number_of_material_ids;
135 ++material_id)
136 {
137 const bool material_needs_temperature =
138 electromagnetic_properties_depend_on(physical_properties_manager,
139 material_id,
141
142 // The temperature of an effective medium (e.g., the temperature of the
143 // particles in unresolved CFD-DEM simulations) is not available yet, so
144 // its electromagnetic properties must not depend on the temperature.
145 AssertThrow(
146 !(material_needs_temperature &&
147 physical_properties_manager.is_effective_medium(0, material_id)),
148 ExcMessage(
149 "Temperature-dependent electromagnetic properties are not supported yet for an effective medium (fluid-dispersed phase interaction). Use temperature-independent electromagnetic property models for the fluid and the unresolved dispersed phase."));
150
151 needs_temperature = needs_temperature || material_needs_temperature;
154 electromagnetic_properties_depend_on(physical_properties_manager,
155 material_id,
157 }
158
160 {
161 // If temperature-dependent electromagnetic properties are requested,
162 // heat transfer must be enabled.
163 const std::vector<PhysicsID> active_physics_ids =
164 multiphysics->get_active_physics();
165 AssertThrow(
166 std::ranges::find(active_physics_ids, PhysicsID::heat_transfer) !=
167 active_physics_ids.end(),
168 ExcMessage(
169 "User-defined temperature-dependent electromagnetic properties requested without heat transfer physics. Enable heat transfer in the multiphysics subsection."));
170 }
171
173 {
174 // The void fraction is only available with the volume-averaged
175 // Navier-Stokes (VANS) formulation of the fluid dynamics. It is never an
176 // active physics, so it cannot be checked in the list of active physics.
177 // Instead, we check the formulation of the fluid dynamics solver.
178 AssertThrow(
179 multiphysics->get_fluid_dynamics_formulation() ==
181 ExcMessage(
182 "Void fraction-dependent electromagnetic properties (fluid-dispersed phase interaction) require the volume-averaged Navier-Stokes (VANS) formulation, which provides the void fraction."));
183
184 // The threshold coupling strategy compares the electromagnetic
185 // properties with those of the last solve, which are only stored for
186 // the temperature field.
187 AssertThrow(
188 simulation_parameters.multiphysics.time_harmonic_maxwell_parameters
189 .time_coupling_strategy !=
191 ExcMessage(
192 "The threshold time coupling strategy of the time-harmonic Maxwell solver is not supported with void fraction-dependent electromagnetic properties at the moment."));
193 }
194}
195
196template <int dim>
197void
199 const TrilinosWrappers::SparsityPattern &sparsity_pattern)
200{
201 auto mpi_communicator = triangulation->get_mpi_communicator();
202 const auto this_mpi_process =
203 Utilities::MPI::this_mpi_process(mpi_communicator);
204 constexpr double bytes_to_gb = 1.0 / (1024.0 * 1024.0 * 1024.0);
205
206 // Fetch memory consumption information on each process
207 const auto present_solution_memory =
208 this->present_solution->memory_consumption() * bytes_to_gb;
209 const auto present_solution_skeleton_memory =
210 this->present_solution_skeleton->memory_consumption() * bytes_to_gb;
211 const auto system_rhs_memory =
212 this->system_rhs.memory_consumption() * bytes_to_gb;
213 const auto sparsity_pattern_memory =
214 sparsity_pattern.n_nonzero_elements() *
215 sizeof(TrilinosWrappers::types::int_type) *
216 bytes_to_gb; // We use a proxy for the memory consumption of the
217 // sparsity pattern based on the number of non-zero
218 // elements and the size of the integer type used to store
219 // the sparsity pattern, since the
220 // TrilinosWrappers::SparsityPattern class does not have a
221 // memory_consumption() function implemented.
222 const auto system_matrix_memory =
223 this->system_matrix.memory_consumption() * bytes_to_gb;
224 const auto dof_handler_trial_interior_memory =
225 this->dof_handler_trial_interior->memory_consumption() * bytes_to_gb;
226 const auto dof_handler_trial_skeleton_memory =
227 this->dof_handler_trial_skeleton->memory_consumption() * bytes_to_gb;
228 const auto dof_handler_test_memory =
229 this->dof_handler_test->memory_consumption() * bytes_to_gb;
230
231 // Gather memory consumption information from all ranks to rank 0
232 const auto present_solution_memory_by_rank =
233 Utilities::MPI::gather(mpi_communicator, present_solution_memory, 0);
234 const auto present_solution_skeleton_memory_by_rank =
235 Utilities::MPI::gather(mpi_communicator,
236 present_solution_skeleton_memory,
237 0);
238 const auto system_rhs_memory_by_rank =
239 Utilities::MPI::gather(mpi_communicator, system_rhs_memory, 0);
240 const auto sparsity_pattern_memory_by_rank =
241 Utilities::MPI::gather(mpi_communicator, sparsity_pattern_memory, 0);
242 const auto system_matrix_memory_by_rank =
243 Utilities::MPI::gather(mpi_communicator, system_matrix_memory, 0);
244 const auto dof_handler_trial_interior_memory_by_rank =
245 Utilities::MPI::gather(mpi_communicator,
246 dof_handler_trial_interior_memory,
247 0);
248 const auto dof_handler_trial_skeleton_memory_by_rank =
249 Utilities::MPI::gather(mpi_communicator,
250 dof_handler_trial_skeleton_memory,
251 0);
252 const auto dof_handler_test_memory_by_rank =
253 Utilities::MPI::gather(mpi_communicator, dof_handler_test_memory, 0);
254
255 // Sum memory consumption across all ranks to get total memory usage
256 const auto present_solution_memory_total =
257 Utilities::MPI::sum(present_solution_memory, mpi_communicator);
258 const auto present_solution_skeleton_memory_total =
259 Utilities::MPI::sum(present_solution_skeleton_memory, mpi_communicator);
260 const auto system_rhs_memory_total =
261 Utilities::MPI::sum(system_rhs_memory, mpi_communicator);
262 const auto sparsity_pattern_memory_total =
263 Utilities::MPI::sum(sparsity_pattern_memory, mpi_communicator);
264 const auto system_matrix_memory_total =
265 Utilities::MPI::sum(system_matrix_memory, mpi_communicator);
266 const auto dof_handler_trial_interior_memory_total =
267 Utilities::MPI::sum(dof_handler_trial_interior_memory, mpi_communicator);
268 const auto dof_handler_trial_skeleton_memory_total =
269 Utilities::MPI::sum(dof_handler_trial_skeleton_memory, mpi_communicator);
270 const auto dof_handler_test_memory_total =
271 Utilities::MPI::sum(dof_handler_test_memory, mpi_communicator);
272
273 // Print memory consumption information on rank 0
274 if (this_mpi_process == 0)
275 {
276 const auto total_memory =
277 present_solution_memory_total + present_solution_skeleton_memory_total +
278 system_rhs_memory_total + sparsity_pattern_memory_total +
279 system_matrix_memory_total + dof_handler_trial_interior_memory_total +
280 dof_handler_trial_skeleton_memory_total + dof_handler_test_memory_total;
281
283 "Time-Harmonic Maxwell Memory Diagnostics",
284 65,
285 '=');
286
287 // When debugging memory issues if rank is needed turn this parameter
288 // to true to have the memory consumption of each rank printed,
289 // otherwise only the total memory consumption across all ranks will
290 // be printed.
291 bool print_memory_by_rank = true;
292 if (print_memory_by_rank)
293 {
294 this->pcout << " *** By rank memory *** " << std::endl;
295
297 "present_solution",
298 present_solution_memory_by_rank.size(),
299 present_solution_memory_by_rank);
301 this->pcout,
302 "present_solution_skeleton",
303 present_solution_skeleton_memory_by_rank.size(),
304 present_solution_skeleton_memory_by_rank);
306 "system_rhs",
307 system_rhs_memory_by_rank.size(),
308 system_rhs_memory_by_rank);
309
311 "sparsity_pattern",
312 sparsity_pattern_memory_by_rank.size(),
313 sparsity_pattern_memory_by_rank);
315 "system_matrix",
316 system_matrix_memory_by_rank.size(),
317 system_matrix_memory_by_rank);
319 this->pcout,
320 "dof_handler_trial_interior",
321 dof_handler_trial_interior_memory_by_rank.size(),
322 dof_handler_trial_interior_memory_by_rank);
324 this->pcout,
325 "dof_handler_trial_skeleton",
326 dof_handler_trial_skeleton_memory_by_rank.size(),
327 dof_handler_trial_skeleton_memory_by_rank);
329 "dof_handler_test",
330 dof_handler_test_memory_by_rank.size(),
331 dof_handler_test_memory_by_rank);
332
333 this->pcout
334 << "=================================================================="
335 << std::endl;
336 }
337
338 this->pcout << " *** Total memory consumption (GB) *** " << std::endl;
339 this->pcout << " present_solution : " << present_solution_memory_total
340 << std::endl;
341 this->pcout << " present_solution_skeleton : "
342 << present_solution_skeleton_memory_total << std::endl;
343 this->pcout << " system_rhs : " << system_rhs_memory_total << std::endl;
344 this->pcout << " sparsity_pattern : " << sparsity_pattern_memory_total
345 << std::endl;
346 this->pcout << " system_matrix : " << system_matrix_memory_total
347 << std::endl;
348 this->pcout << " dof_handler_trial_interior : "
349 << dof_handler_trial_interior_memory_total << std::endl;
350 this->pcout << " dof_handler_trial_skeleton : "
351 << dof_handler_trial_skeleton_memory_total << std::endl;
352 this->pcout << " dof_handler_test : " << dof_handler_test_memory_total
353 << std::endl;
354 this->pcout << " Total DPG solver memory consumption : " << total_memory
355 << std::endl;
356 this->pcout
357 << " =================================================================="
358 << std::endl;
359 }
360}
361
362template <int dim>
363void
365 const PhysicalPropertiesManager &physical_properties_manager)
366{
367 // Define reusable variables for the computation of the scaling factor.
368 auto mpi_communicator = this->triangulation->get_mpi_communicator();
369 const auto &electromagnetic_parameters =
370 this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters;
371 constexpr double void_impedance =
372 4 * numbers::PI *
373 29.9792458; // Void impedance in Ohms, the unit conversion of this value is
374 // done when computing the scaling factor. The factor 29.9792458
375 // is the speed of light in cm/ns and is used to make it
376 // convenient when multiplying by the void permeability to
377 // obtain the void impedance in the desired units (e.g., Ohms).
378
379 // Initialize the amplitude. The default value is put to zero.
380 // If there are no waveguide inlets, the maximum of amplitude will be taken
381 // between 0 and a strictly positive value ensuring that the strictly positive
382 // value remains.
383 double max_port_electric_amplitude = 0.0;
384
385 // If there are waveguide inlets we need to compute their scaling from the
386 // input power provided by the user.
387 if (electromagnetic_parameters.number_of_waveguide_inlets > 0)
388 {
389 const auto &waveguide_powers = electromagnetic_parameters.waveguide_power;
390
391 // The following vector will store the modal power for each waveguide
392 // inlet boundary condition computed from integrating the Poynting vector
393 // of the incident wave over the inlet face.
394 std::vector<double> waveguide_modal_powers(
395 electromagnetic_parameters.number_of_waveguide_inlets, 0.);
396
397 // Resize the vector that will store the waveguide port electric field
398 // amplitudes. Those are obtain by computing the square root of the ratio
399 // between the input power provided by the user and the modal power
400 // computed from integrating the Poynting vector of the incident wave over
401 // the inlet face.
402 this->waveguide_ports_electric_amplitudes = std::vector<double>(
403 electromagnetic_parameters.number_of_waveguide_inlets, 0.);
404
405 // We need to define a FEFaceValues object to perform the integral of the
406 // Poynting vector on faces
407 FEFaceValues<dim> fe_face_values_trial_skeleton(*this->mapping,
408 *this->fe_trial_skeleton,
409 *this->face_quadrature,
410 update_quadrature_points |
411 update_normal_vectors |
412 update_JxW_values);
413 const unsigned int n_face_q_points =
414 fe_face_values_trial_skeleton.n_quadrature_points;
416
417 // Containers for electromagnetic quantities at the quadrature points
418 Tensor<1, dim, std::complex<double>> E_inc;
419 Tensor<1, dim, std::complex<double>> H_inc;
420 std::vector<std::complex<double>> effective_electric_permittivities(
421 n_face_q_points);
422 std::vector<std::complex<double>> effective_magnetic_permeabilities(
423 n_face_q_points);
424 unsigned int material_id;
425 bool cell_material_needs_temperature = false;
426 bool cell_material_needs_void_fraction = false;
427
428 // The temperature field is only needed when at least one electromagnetic
429 // property depends on temperature.
430
431 std::unique_ptr<FEFaceValues<dim>> fe_face_values_temperature;
432 const DoFHandler<dim> *dof_handler_temperature = nullptr;
433 const GlobalVectorType *temperature_solution = nullptr;
434 std::vector<double> temperature_face_values(n_face_q_points);
435 std::map<field, std::vector<double>> field_values_vector;
436
438 {
439 dof_handler_temperature =
440 &this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
441 fe_face_values_temperature = std::make_unique<FEFaceValues<dim>>(
442 *this->mapping,
443 dof_handler_temperature->get_fe(),
444 *this->face_quadrature,
445 update_values | update_quadrature_points);
446 temperature_solution =
447 &this->multiphysics->get_solution(PhysicsID::heat_transfer);
448 }
449
450 // Likewise, the void fraction field is only needed when at least one
451 // electromagnetic property depends on the void fraction.
452 std::unique_ptr<FEFaceValues<dim>> fe_face_values_void_fraction;
453 const DoFHandler<dim> *dof_handler_void_fraction = nullptr;
454 const GlobalVectorType *void_fraction_solution = nullptr;
455 std::vector<double> void_fraction_face_values(n_face_q_points, 1.);
456
458 {
459 dof_handler_void_fraction =
460 &this->multiphysics->get_dof_handler(PhysicsID::void_fraction);
461 fe_face_values_void_fraction = std::make_unique<FEFaceValues<dim>>(
462 *this->mapping,
463 dof_handler_void_fraction->get_fe(),
464 *this->face_quadrature,
465 update_values | update_quadrature_points);
466 void_fraction_solution =
467 &this->multiphysics->get_solution(PhysicsID::void_fraction);
468 }
469
470
471 // Here we perform the Poynting vector integration over the waveguide
472 // inlet faces.
473 for (const auto &cell : triangulation->active_cell_iterators())
474 {
475 if (cell->is_locally_owned())
476 {
477 material_id = cell->material_id();
478
479 // We check if the physical properties depend on the temperature
480 // and void fraction fields. If so, we will need to evaluate them
481 // at the quadrature points.
482 cell_material_needs_temperature =
484 physical_properties_manager, material_id, field::temperature);
485 cell_material_needs_void_fraction =
487 physical_properties_manager,
488 material_id,
490
491 for (const auto &face : cell->face_iterators())
492 {
493 if (!(face->at_boundary()))
494 continue;
495
496 fe_face_values_trial_skeleton.reinit(cell, face);
497
498 if (cell_material_needs_temperature)
499 {
500 const typename DoFHandler<dim>::active_cell_iterator
501 cell_temperature = cell->as_dof_handler_iterator(
502 *dof_handler_temperature);
503 fe_face_values_temperature->reinit(cell_temperature,
504 face);
505 fe_face_values_temperature->get_function_values(
506 *temperature_solution, temperature_face_values);
507 }
508 else
509 {
510 std::ranges::fill(temperature_face_values, 0.);
511 }
512
513 if (cell_material_needs_void_fraction)
514 {
515 const typename DoFHandler<dim>::active_cell_iterator
516 cell_void_fraction = cell->as_dof_handler_iterator(
517 *dof_handler_void_fraction);
518 fe_face_values_void_fraction->reinit(cell_void_fraction,
519 face);
520 fe_face_values_void_fraction->get_function_values(
521 *void_fraction_solution, void_fraction_face_values);
522 }
523 else
524 {
525 std::ranges::fill(void_fraction_face_values, 1.);
526 }
527
528 bc_type =
530 .boundary_conditions_time_harmonic_electromagnetics.type
531 .at(face->boundary_id());
532
533 // We only compute the modal power if its a port boundary
534 if (!(bc_type ==
536 waveguide_port))
537 continue;
538
539 // Get the index of the waveguide port boundary
540 // condition to apply the correct mode parameters
541 const unsigned int waveguide_port_index =
542 get_waveguide_port_index(electromagnetic_parameters,
543 face->boundary_id());
544
545 // We update the material properties for the current face
546 // quadrature points.
547 field_values_vector[field::temperature] =
548 temperature_face_values;
549 field_values_vector[field::void_fraction] =
550 void_fraction_face_values;
551
553 physical_properties_manager,
554 field_values_vector,
555 material_id,
556 effective_electric_permittivities,
557 effective_magnetic_permeabilities);
558
559 // Loop over all face quadrature points
560 for (unsigned int q_point = 0; q_point < n_face_q_points;
561 ++q_point)
562 {
563 // Initialize reusable variables
564 const auto &position =
565 fe_face_values_trial_skeleton.quadrature_point(q_point);
566 const auto &normal =
567 fe_face_values_trial_skeleton.normal_vector(q_point);
568 const double JxW_face =
569 fe_face_values_trial_skeleton.JxW(q_point);
570
571 std::tie(E_inc, H_inc) =
573 electromagnetic_parameters,
574 position,
575 normal,
576 effective_electric_permittivities[q_point],
577 effective_magnetic_permeabilities[q_point],
578 waveguide_port_index);
579
580 // The Poynting vector is given by S = 0.5 * Re(E
581 // x H*), where H* is the complex conjugate of H.
582 // The normal points outward from the domain, so
583 // the power entering the domain is given by the
584 // negative of the flux of the Poynting vector
585 // through the face (that's why we have a negative
586 // sign in front of the integral). The cross product
587 // is written explicitly in 3D, so it is only compiled
588 // for dim = 3 (the waveguide port fields are not
589 // defined in 2D).
590 if constexpr (dim == 3)
591 {
592 waveguide_modal_powers[waveguide_port_index] -=
593 0.5 *
594 std::real(
595 normal[0] * (E_inc[1] * std::conj(H_inc[2]) -
596 E_inc[2] * std::conj(H_inc[1])) +
597 normal[1] * (E_inc[2] * std::conj(H_inc[0]) -
598 E_inc[0] * std::conj(H_inc[2])) +
599 normal[2] * (E_inc[0] * std::conj(H_inc[1]) -
600 E_inc[1] * std::conj(H_inc[0]))) *
601 JxW_face;
602 }
603 else
604 {
605 AssertThrow(false,
606 TimeHarmonicMaxwellDimensionNotSupported(
607 dim));
608 }
609 }
610 }
611 }
612 }
613
614 // Now we need to aggregate from all the MPI processes to get the global
615 // modal power for each waveguide inlet
616 for (unsigned int inlets = 0;
617 inlets < electromagnetic_parameters.number_of_waveguide_inlets;
618 ++inlets)
619 {
620 // The power amplitude comes from the non-dimensionalization of power:
621 // P = 0.5 * int(ExH*)= E_0 * H_0 * P_modal. So we can isolate the
622 // electric field amplitude E_0. We don't need to multiply by the
623 // dimensionality because we assume user input in W and impedance in
624 // Ohms so the resulting units of this integral will always be
625 // Volt/length unit (of the mesh).
627 std::sqrt(waveguide_powers[inlets] * void_impedance /
628 Utilities::MPI::sum(waveguide_modal_powers[inlets],
629 mpi_communicator));
630 }
631
632 max_port_electric_amplitude =
633 *std::ranges::max_element(this->waveguide_ports_electric_amplitudes);
634 }
635
636 // Now we need to define the electromagnetic scaling factor according to the
637 // type chosen by the user.
638 if (electromagnetic_parameters.electromagnetic_scaling_type ==
640 {
641 // We assume that the electric field amplitude provided by the user is MKS
642 // (V/m).
644 electromagnetic_parameters.electric_field_amplitude;
645 }
646 else if (electromagnetic_parameters.electromagnetic_scaling_type ==
648 {
649 // We assume that the magnetic field amplitude provided by the user is in
650 // MKS (A/m). We convert it to the electric field for convenience to not
651 // carry multiple conversion factors when applying the scaling to the
652 // solution.
654 void_impedance * electromagnetic_parameters.magnetic_field_amplitude;
655 }
656 else if (electromagnetic_parameters.electromagnetic_scaling_type ==
658 { // From the Poynting vector integration and assumptions made in the
659 // computation of the waveguide port excitation, the unit here are in V/L,
660 // with L being the length unit of the mesh. Therefore, we need to divide
661 // by the length unit to get the correct scaling factor in V/m.
663 max_port_electric_amplitude /
664 this->simulation_parameters.dimensionality.length;
665 }
666 else if (electromagnetic_parameters.electromagnetic_scaling_type ==
668 {
669 // The solution is not rescaled. If there are waveguide inlets, their
670 // relative powers are already accounted for in the excitation, which is
671 // scaled by the amplitude of each inlet relative to the maximum amplitude
672 // of all the inlets (see TimeHarmonicMaxwellAssemblerRobinBC). The
673 // strongest inlet is therefore excited with the unit-amplitude mode.
674 this->electromagnetic_scaling = 1.0;
675 }
676 else
677 {
678 AssertThrow(false, ExcMessage("Unknown electromagnetic scaling type."));
679 }
680}
681
682template <int dim>
683void
685 const DoFHandler<dim> &dof_handler,
686 GlobalVectorType &solution)
687{
688 const auto &fe = dof_handler.get_fe();
689
690 if (this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters
691 .electromagnetic_scaling_type ==
693 return;
694
695 constexpr double void_impedance = 4 * numbers::PI * 29.9792458;
696 // Here, everything is in MKS units, so we need to convert it to the user
697 // desired units.
698 const double electric_scale =
700 this->simulation_parameters.dimensionality.electric_amplitude_scaling;
701 const double magnetic_scale =
702 this->electromagnetic_scaling / void_impedance *
703 this->simulation_parameters.dimensionality.magnetic_amplitude_scaling;
704
705 const IndexSet &locally_owned_dofs = solution.locally_owned_elements();
706 std::vector<bool> dof_scaled(locally_owned_dofs.n_elements(), false);
707 std::vector<types::global_dof_index> local_dof_indices(fe.n_dofs_per_cell());
708
709 // We loop over all the cells and apply the correct scaling to the electric
710 // and magnetic field components of the solution. We need to do so because the
711 // solution is stored as a single vector but contains different physical
712 // fields (electric and magnetic) that need to be scaled differently and the
713 // system_to_base_index function which allows us to identify which
714 // component of the solution corresponds to which dof is based on the local
715 // dof index and not the global one. Therefore, we need to loop over all the
716 // cells and their local dofs to apply the correct scaling to each component
717 // of the solution.
718 for (const auto &cell : dof_handler.active_cell_iterators())
719 {
720 if (cell->is_locally_owned())
721 {
722 // Get the global dof indices for the current cell
723 cell->get_dof_indices(local_dof_indices);
724
725 // Loop over the local dofs of the cell
726 for (unsigned int local_dof = 0; local_dof < local_dof_indices.size();
727 ++local_dof)
728 {
729 // Get the global dof index corresponding to the local dof index
730 const types::global_dof_index global_dof =
731 local_dof_indices[local_dof];
732
733 // Check if the global dof index is locally owned. If not, we skip
734 // it since we only want to scale the locally owned part of the
735 // solution vector.
736 if (!locally_owned_dofs.is_element(global_dof))
737 continue;
738
739 const unsigned int locally_owned_position =
740 locally_owned_dofs.index_within_set(global_dof);
741
742 // Since we loop over all the cells, we will encounter each dof
743 // multiple times (once for each cell it belongs to). We only want
744 // to apply the scaling once for each dof, so we keep track of
745 // which dofs have already been scaled using the dof_scaled
746 // vector. If a dof has already been scaled, we skip it.
747 if (dof_scaled[locally_owned_position])
748 continue;
749
750 dof_scaled[locally_owned_position] = true;
751
752 // Get the base element of the current dof. Remember that the
753 // convention for the FE system we are using is [E_real, E_imag,
754 // H_real, H_imag].
755 const unsigned int base_element =
756 fe.system_to_base_index(local_dof).first.first;
757
758 if (base_element == 0 || base_element == 1)
759 solution[global_dof] *= electric_scale;
760 else if (base_element == 2 || base_element == 3)
761 solution[global_dof] *= magnetic_scale;
762 }
763 }
764 }
765}
766
767template <int dim>
768std::vector<OutputStruct<dim, GlobalVectorType>>
770{
771 std::vector<OutputStruct<dim, GlobalVectorType>> solution_output_structs;
772
773 // Interior output setup
774 std::vector<std::string> solution_interior_names(dim, "E_real");
775 for (int i = 0; i < dim; ++i)
776 {
777 solution_interior_names.emplace_back("E_imag");
778 }
779 for (int i = 0; i < dim; ++i)
780 {
781 solution_interior_names.emplace_back("H_real");
782 }
783 for (int i = 0; i < dim; ++i)
784 {
785 solution_interior_names.emplace_back("H_imag");
786 }
787
788 std::vector<DataComponentInterpretation::DataComponentInterpretation>
789 solution_interior_data_component_interpretation(
790 4 * dim, DataComponentInterpretation::component_is_part_of_vector);
791
792 solution_output_structs.emplace_back(
795 *this->present_solution,
796 solution_interior_names,
797 solution_interior_data_component_interpretation);
798
799 // We also want to output the DPG estimator as a separate field for
800 // visualization and postprocessing purposes when computed.
802 {
803 solution_output_structs.emplace_back(
804 std::in_place_type<OutputStructCellVector>,
806 std::string("dpg_error_norm"));
807 }
808
809 // Skeleton output setup
810 // TODO: it will need its own writer probably because we need to use
811 // DataOutFaces object, add the skeleton output to all physics when required?
812
813 return solution_output_structs;
814}
815
816template <int dim>
817std::vector<double>
819{
820 auto mpi_communicator = this->triangulation->get_mpi_communicator();
821
822 // Interior L2 error
823 FEValues<dim> fe_values_trial_interior(*this->mapping,
824 *this->fe_trial_interior,
825 *this->cell_quadrature,
826 update_values |
827 update_quadrature_points |
828 update_JxW_values);
829
830 const unsigned int n_q_points = this->cell_quadrature->size();
831
832 // The exact solution will be defined by the user but will need to be on all
833 // possible fields of the ultraweak formulation so we need 4*dim components.
834 std::vector<Vector<double>> exact_solution_values(n_q_points,
835 Vector<double>(4 * dim));
836 auto &exact_solution_function =
837 simulation_parameters.analytical_solution->electromagnetics;
838
839 // When looping on each cell we will extract the different field
840 // solution obtained numerically. The containers used to store the
841 // interpolated solution at the quadrature points are declared below.
842 std::vector<Tensor<1, dim>> local_E_values_real(n_q_points);
843 std::vector<Tensor<1, dim>> local_E_values_imag(n_q_points);
844 std::vector<Tensor<1, dim>> local_H_values_real(n_q_points);
845 std::vector<Tensor<1, dim>> local_H_values_imag(n_q_points);
846
847 // We create variables that will store all the integration results we are
848 // interested in.
849 double L2_error_E_real = 0;
850 double L2_error_E_imag = 0;
851 double L2_error_H_real = 0;
852 double L2_error_H_imag = 0;
853
854 for (const auto &cell : dof_handler_trial_interior->active_cell_iterators())
855 {
856 if (cell->is_locally_owned())
857 {
858 fe_values_trial_interior.reinit(cell);
859
860 // Get the simulated solution at quadrature points
861 fe_values_trial_interior[extractor_E_real].get_function_values(
862 *present_solution, local_E_values_real);
863 fe_values_trial_interior[extractor_E_imag].get_function_values(
864 *present_solution, local_E_values_imag);
865 fe_values_trial_interior[extractor_H_real].get_function_values(
866 *present_solution, local_H_values_real);
867 fe_values_trial_interior[extractor_H_imag].get_function_values(
868 *present_solution, local_H_values_imag);
869
870 // Get the exact solution at quadrature points
871 exact_solution_function.vector_value_list(
872 fe_values_trial_interior.get_quadrature_points(),
873 exact_solution_values);
874
875 // Loop on quadrature points to compute the L2 error contributions
876 for (unsigned int q = 0; q < n_q_points; ++q)
877 {
878 const double JxW = fe_values_trial_interior.JxW(q);
879
880 // Loop on dimensions to compute the squared error
881 for (int d = 0; d < dim; ++d)
882 {
883 // E real part
884 L2_error_E_real +=
885 Utilities::fixed_power<2>(local_E_values_real[q][d] -
886 exact_solution_values[q][d]) *
887 JxW;
888
889 // E imag part
890 L2_error_E_imag += Utilities::fixed_power<2>(
891 local_E_values_imag[q][d] -
892 exact_solution_values[q][d + dim]) *
893 JxW;
894
895 // H real part
896 L2_error_H_real += Utilities::fixed_power<2>(
897 local_H_values_real[q][d] -
898 exact_solution_values[q][d + 2 * dim]) *
899 JxW;
900
901 // H imag part
902 L2_error_H_imag += Utilities::fixed_power<2>(
903 local_H_values_imag[q][d] -
904 exact_solution_values[q][d + 3 * dim]) *
905 JxW;
906 }
907 }
908 }
909 }
910 // Skeleton L2 error
911 // TODO
912
913 L2_error_E_real = Utilities::MPI::sum(L2_error_E_real, mpi_communicator);
914 L2_error_E_imag = Utilities::MPI::sum(L2_error_E_imag, mpi_communicator);
915 L2_error_H_real = Utilities::MPI::sum(L2_error_H_real, mpi_communicator);
916 L2_error_H_imag = Utilities::MPI::sum(L2_error_H_imag, mpi_communicator);
917
918 return {std::sqrt(L2_error_E_real),
919 std::sqrt(L2_error_E_imag),
920 std::sqrt(L2_error_H_real),
921 std::sqrt(L2_error_H_imag)};
922}
923
924
925template <int dim>
926void
928{
929 auto mpi_communicator = this->triangulation->get_mpi_communicator();
930 unsigned int this_mpi_process(
931 Utilities::MPI::this_mpi_process(mpi_communicator));
932
933 if (this_mpi_process == 0 &&
934 simulation_parameters.analytical_solution->verbosity !=
936 {
937 ConvergenceTable &error_table = this->error_table;
938
939 error_table.omit_column_from_convergence_rate_evaluation("cells");
940
941 error_table.evaluate_all_convergence_rates(
942 ConvergenceTable::reduction_rate_log2);
943
944 error_table.set_scientific("error_E_real", true);
945 error_table.set_scientific("error_E_imag", true);
946 error_table.set_scientific("error_H_real", true);
947 error_table.set_scientific("error_H_imag", true);
948 error_table.set_precision("error_E_real",
949 this->simulation_control->get_log_precision());
950 error_table.set_precision("error_E_imag",
951 this->simulation_control->get_log_precision());
952 error_table.set_precision("error_H_real",
953 this->simulation_control->get_log_precision());
954 error_table.set_precision("error_H_imag",
955 this->simulation_control->get_log_precision());
956 error_table.write_text(std::cout);
957 }
958
960 {
961 announce_string(this->pcout, "Time Harmonic Electromagnetics");
962 this->pcout << std::defaultfloat;
963 this->computing_timer.print_summary();
964 this->pcout << std::scientific;
965 }
966}
967
968template <int dim>
969void
978
979template <int dim>
980void
982{
983 // No time-dependent vectors to percolate in time-harmonic Maxwell
984}
985
986template <int dim>
987void
989{
990 // No modification of the solution is required at the moment
991}
992
993template <int dim>
994void
996{
997 if (!this->simulation_parameters
998 .boundary_conditions_time_harmonic_electromagnetics.time_dependent)
999 return;
1000
1001 AssertThrow(
1002 false,
1003 ExcMessage(
1004 "Time-dependent boundary conditions not yet implemented for TimeHarmonicMaxwell."));
1005}
1006
1007template <int dim>
1008void
1010{
1011 if (simulation_parameters.analytical_solution->calculate_error() == true &&
1012 !first_iteration)
1013 {
1014 std::vector<double> errors = calculate_L2_error();
1015 double E_real_error = errors[0];
1016 double E_imag_error = errors[1];
1017 double H_real_error = errors[2];
1018 double H_imag_error = errors[3];
1019
1020 this->error_table.add_value("cells",
1021 this->triangulation->n_global_active_cells());
1022 this->error_table.add_value("error_E_real", E_real_error);
1023 this->error_table.add_value("error_E_imag", E_imag_error);
1024 this->error_table.add_value("error_H_real", H_real_error);
1025 this->error_table.add_value("error_H_imag", H_imag_error);
1026
1027 this->error_table.set_scientific("error_E_real", true);
1028 this->error_table.set_scientific("error_E_imag", true);
1029 this->error_table.set_scientific("error_H_real", true);
1030 this->error_table.set_scientific("error_H_imag", true);
1031
1032 if (Utilities::MPI::this_mpi_process(
1033 triangulation->get_mpi_communicator()) == 0)
1034 {
1035 const std::string filename =
1036 simulation_parameters.simulation_control.output_folder +
1037 simulation_parameters.analytical_solution->get_filename() +
1038 "_time_harmonic_maxwell.dat";
1039 std::ofstream output(filename.c_str());
1040 this->error_table.write_text(output);
1041 }
1042
1043 if (simulation_parameters.analytical_solution->verbosity !=
1045 {
1046 this->pcout << "L2 error E real: " << E_real_error << std::endl;
1047 this->pcout << "L2 error E imag: " << E_imag_error << std::endl;
1048 this->pcout << "L2 error H real: " << H_real_error << std::endl;
1049 this->pcout << "L2 error H imag: " << H_imag_error << std::endl;
1050 }
1051 }
1052}
1053
1054template <int dim>
1055void
1057{
1058 announce_string(this->pcout, "Time Harmonic Electromagnetics");
1059 this->pcout << std::defaultfloat;
1060 this->computing_timer.print_summary();
1061 this->pcout << std::scientific;
1062 this->computing_timer.reset();
1063}
1064
1065template <int dim>
1066void
1068{
1069 this->solution_transfer->prepare_for_coarsening_and_refinement(
1070 *this->present_solution);
1071}
1072
1073template <int dim>
1074void
1076{
1077 auto mpi_communicator = this->triangulation->get_mpi_communicator();
1078
1079 // Set up the vectors for the transfer
1081 mpi_communicator);
1082
1083 // Interpolate the solution at time and previous time
1084 this->solution_transfer->interpolate(tmp);
1085
1086 // Distribute constraints does not need to be call for the DPG method transfer
1087 // as they live on the skeleton only, but we transfer the interior solution
1088 // only for multiphysics purposes.
1089
1090 // Fix on the new mesh
1091 *this->present_solution = tmp;
1092}
1093
1094template <int dim>
1095void
1097{
1098 auto mpi_communicator = this->triangulation->get_mpi_communicator();
1099
1100 // Checkpoint interior
1101 std::vector<const GlobalVectorType *> sol_set_transfer;
1102 solution_transfer = std::make_shared<SolutionTransfer<dim, GlobalVectorType>>(
1104
1105 sol_set_transfer.emplace_back(&(*present_solution));
1106
1107 solution_transfer->prepare_for_serialization(sol_set_transfer);
1108
1109 // When temperature-dependent electromagnetic properties are used by the
1110 // simulation, we need to checkpoint the last temperature solution vector
1112 {
1113 std::vector<const GlobalVectorType *> temperature_sol_set_transfer;
1114
1116 std::make_shared<SolutionTransfer<dim, GlobalVectorType>>(
1117 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer));
1118
1119 temperature_sol_set_transfer.emplace_back(
1121
1122 temperature_last_solved_solution_transfer->prepare_for_serialization(
1123 temperature_sol_set_transfer);
1124 }
1125
1126 // Serialize all post-processing tables that are currently used with the
1127 // TimeHarmonicMaxwell solver
1128 const std::vector<OutputStructTableHandler> &table_output_structs =
1129 this->gather_tables();
1130 serialize_tables_vector(table_output_structs, mpi_communicator);
1131}
1132
1133template <int dim>
1134void
1136{
1137 auto mpi_communicator = triangulation->get_mpi_communicator();
1138 this->pcout << "Reading time-harmonic Maxwell checkpoint" << std::endl;
1139
1140 // Time-harmonic Maxwell solution interior
1141 std::vector<GlobalVectorType *> input_vectors(1);
1143 mpi_communicator);
1144 input_vectors[0] = &distributed_system;
1145
1146 solution_transfer = std::make_shared<SolutionTransfer<dim, GlobalVectorType>>(
1148 solution_transfer->deserialize(input_vectors);
1149
1150 *present_solution = distributed_system;
1151
1152 // Temperature solution
1154 {
1155 const auto &temperature_dof_handler =
1156 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
1157
1158 const IndexSet locally_relevant_temperature_dofs =
1159 DoFTools::extract_locally_relevant_dofs(temperature_dof_handler);
1161 temperature_dof_handler.locally_owned_dofs(),
1162 locally_relevant_temperature_dofs,
1163 mpi_communicator);
1164
1165 GlobalVectorType distributed_temperature_last_solved(
1166 temperature_dof_handler.locally_owned_dofs(), mpi_communicator);
1167
1169 std::make_shared<SolutionTransfer<dim, GlobalVectorType>>(
1170 temperature_dof_handler);
1171
1172 std::vector<GlobalVectorType *> temperature_input_vectors(1);
1173
1174 temperature_input_vectors[0] = &distributed_temperature_last_solved;
1175
1177 temperature_input_vectors);
1178
1179 temperature_last_solved_solution = distributed_temperature_last_solved;
1180 }
1181
1182 // Deserialize all post-processing tables that are currently used with the
1183 // TimeHarmonicMaxwell solver
1184 std::vector<OutputStructTableHandler> table_output_structs =
1185 this->gather_tables();
1186 deserialize_tables_vector(table_output_structs, mpi_communicator);
1187}
1188
1189template <int dim>
1190std::vector<OutputStructTableHandler>
1192{
1193 std::vector<OutputStructTableHandler> table_output_structs;
1194
1195 std::string prefix =
1196 this->simulation_parameters.simulation_control.output_folder;
1197 std::string suffix = ".checkpoint";
1198
1199 if (this->simulation_parameters.analytical_solution->calculate_error())
1200 table_output_structs.emplace_back(
1201 this->error_table,
1202 prefix + this->simulation_parameters.analytical_solution->get_filename() +
1203 "_THM" + suffix);
1204
1205
1206 return table_output_structs;
1207}
1208
1209template <int dim>
1210void
1212 const std::pair<const Variable, Parameters::MultipleAdaptationParameters>
1213 &ivar,
1214 dealii::Vector<float> &estimated_error_per_cell)
1215{
1216 if (ivar.first == Variable::electric_field)
1217 {
1218 AssertThrow(
1219 ivar.second.error_estimator ==
1221 ExcMessage(
1222 "Only the Kelly error estimator is currently implemented for the "
1223 "<electric field> field."));
1224
1225 ComponentMask electric_field_mask =
1226 this->fe_trial_interior->component_mask(extractor_E_real) |
1227 this->fe_trial_interior->component_mask(extractor_E_imag);
1228 compute_kelly(estimated_error_per_cell, electric_field_mask);
1229 }
1230 else if (ivar.first == Variable::magnetic_field)
1231 {
1232 AssertThrow(
1233 ivar.second.error_estimator ==
1235 ExcMessage(
1236 "Only the Kelly error estimator is currently implemented for the "
1237 "<magnetic field> field."));
1238
1239 ComponentMask magnetic_field_mask =
1240 this->fe_trial_interior->component_mask(extractor_H_real) |
1241 this->fe_trial_interior->component_mask(extractor_H_imag);
1242 compute_kelly(estimated_error_per_cell, magnetic_field_mask);
1243 }
1244 else if (ivar.first == Variable::electromagnetic_fields)
1245 {
1246 if (ivar.second.error_estimator ==
1248 {
1249 ComponentMask electromagnetics_mask =
1250 this->fe_trial_interior->component_mask(extractor_E_real) |
1251 this->fe_trial_interior->component_mask(extractor_E_imag) |
1252 this->fe_trial_interior->component_mask(extractor_H_real) |
1253 this->fe_trial_interior->component_mask(extractor_H_imag);
1254 compute_kelly(estimated_error_per_cell, electromagnetics_mask);
1255 }
1256
1257 else if (ivar.second.error_estimator ==
1259 {
1260 compute_dpg_error(estimated_error_per_cell);
1261 }
1262 else
1263 {
1264 AssertThrow(
1265 false,
1266 ExcMessage(
1267 "Unknown error estimator type for variable <electromagnetic fields>."));
1268 }
1269 }
1270}
1271
1272template <int dim>
1273void
1275 dealii::Vector<float> &estimated_error_per_cell,
1276 const ComponentMask &component_mask)
1277{
1278 KellyErrorEstimator<dim>::estimate(
1279 *this->mapping,
1281 *this->face_quadrature,
1282 typename std::map<types::boundary_id, const Function<dim, double> *>(),
1283 *this->present_solution,
1284 estimated_error_per_cell,
1285 component_mask);
1286}
1287
1288template <int dim>
1289void
1291 dealii::Vector<float> &estimated_error_per_cell)
1292{
1293 // For efficiency, the DPG error estimator is computed in the same loop as the
1294 // reconstruction of the interior solution, so we do not implement it here as
1295 // a separate loop. If it is activated, the estimated error per cell is
1296 // computed and stored in the member variable `local_estimated_error_per_cell`
1297 // during the reconstruction and then used for marking the cells for
1298 // refinement here.
1299 auto mpi_communicator = this->triangulation->get_mpi_communicator();
1300
1301 // Here we add a flag to be sure if the local_estimated_error_per_cell has
1302 // been computed before calling this function. If not, all entry of the
1303 // local_estimated_error_per_cell vector will still be 0.
1304 bool dpg_error_computed = false;
1305
1306 for (const auto &cell :
1307 this->dof_handler_trial_interior->active_cell_iterators())
1308 {
1309 if (cell->is_locally_owned())
1310 {
1311 if (this->local_estimated_error_per_cell[cell->active_cell_index()] !=
1312 0)
1313 dpg_error_computed = true;
1314
1315 const unsigned int cell_index = cell->active_cell_index();
1316 estimated_error_per_cell[cell_index] =
1317 this->local_estimated_error_per_cell[cell_index];
1318 }
1319 }
1320
1321 // Reduce the flag across all MPI ranks so that processes that don't own
1322 // any cells don't falsely triggers the assertion.
1323 const bool is_global_dpg_error_computed =
1324 Utilities::MPI::max(static_cast<unsigned int>(dpg_error_computed),
1325 mpi_communicator) != 0;
1326
1327 AssertThrow(
1328 is_global_dpg_error_computed,
1329 ExcMessage(
1330 "The DPG error estimator has not been computed before calling compute_dpg_error. Please make sure that the system assembly has been performed and the local_estimated_error_per_cell vector has been filled with the DPG error estimates for each cell before calling this function."));
1331}
1332
1333template <int dim>
1334void
1336{
1338
1339 auto mpi_communicator = triangulation->get_mpi_communicator();
1340
1341 // Setup each dof handlers
1342 this->dof_handler_trial_interior->distribute_dofs(*this->fe_trial_interior);
1343 DoFRenumbering::Cuthill_McKee(*this->dof_handler_trial_interior);
1344 this->dof_handler_trial_skeleton->distribute_dofs(*this->fe_trial_skeleton);
1345 DoFRenumbering::Cuthill_McKee(*this->dof_handler_trial_skeleton);
1346 this->dof_handler_test->distribute_dofs(*this->fe_test);
1347 DoFRenumbering::Cuthill_McKee(*this->dof_handler_test);
1348
1349 // Get the locally owned dofs
1351 this->dof_handler_trial_interior->locally_owned_dofs();
1353 this->dof_handler_trial_skeleton->locally_owned_dofs();
1354
1355 // Get the locally relevant dofs
1357 DoFTools::extract_locally_relevant_dofs(*this->dof_handler_trial_interior);
1359 DoFTools::extract_locally_relevant_dofs(*this->dof_handler_trial_skeleton);
1360
1361 // Initialize the solution vectors
1364 mpi_communicator);
1365 this->present_solution_skeleton->reinit(
1368 mpi_communicator);
1369
1370 // We reinitialize the local estimated error per cell vector to store the
1371 // DPG error estimator for each cell if necessary.
1373 {
1374 this->local_estimated_error_per_cell.reinit(
1375 triangulation->n_active_cells());
1376 }
1377 // We reinitialize the system rhs with the skeleton dofs because we have
1378 // performed a static condensation of the interior dofs using the Schur
1379 // complement.
1381 mpi_communicator);
1382
1383 // We reinitialize the initial guess vector
1384 this->initial_guess_iterative_solver.reinit(
1385 this->locally_owned_dofs_trial_skeleton, mpi_communicator);
1386
1387 // Define constraints
1389
1390 // Sparse matrices initialization
1391 // In DPG, the sparse matrix and the dynamic sparsity pattern are very
1392 // expensive so we recast the dynamic sparsity pattern to a
1393 // sparsity pattern before initializing the system matrix to save memory
1394 // because the dynamic sparsity pattern is more expensive in terms of
1395 // memory consumption than the static sparsity pattern.
1396 TrilinosWrappers::SparsityPattern
1397 sparsity_pattern; // This needs to be defined outside the following block
1398 // because it is used in extra_verbose to report the
1399 // memory consumption of the sparsity pattern.
1400
1401 {
1402 DynamicSparsityPattern dsp(this->locally_relevant_dofs_trial_skeleton);
1403 DoFTools::make_sparsity_pattern(*this->dof_handler_trial_skeleton,
1404 dsp,
1405 this->nonzero_constraints,
1406 /*keep_constrained_dofs = */ false);
1407 SparsityTools::distribute_sparsity_pattern(
1408 dsp,
1410 mpi_communicator,
1412
1413 sparsity_pattern.reinit(this->locally_owned_dofs_trial_skeleton,
1415 dsp,
1416 mpi_communicator);
1417 sparsity_pattern.compress();
1418 }
1419
1420 this->system_matrix.reinit(sparsity_pattern);
1421
1422 if (this->simulation_parameters.linear_solver.at(PhysicsID::electromagnetics)
1424 {
1425 print_THM_setup_memory(sparsity_pattern);
1426 }
1427 this->pcout << " DPG system for Time-Harmonic Maxwell Equations:"
1428 << std::endl;
1429 this->pcout
1430 << " Number of skeleton degrees of freedom for Time-Harmonic Maxwell: "
1431 << this->dof_handler_trial_skeleton->n_dofs() << std::endl;
1432 this->pcout
1433 << " Number of interior degrees of freedom for Time-Harmonic Maxwell: "
1434 << this->dof_handler_trial_interior->n_dofs() << std::endl;
1435
1436 // Update the multiphysics interface with the TimeHarmonicMaxwell dof_handler,
1437 // mapping, and solution. Note that we provide the interior dof_handler and
1438 // interior solution as it is the one useful for the other physics (we do not
1439 // provide the skeleton dof_handler and skeleton solution).
1444 this->present_solution);
1445}
1446
1447
1448template <int dim>
1449void
1451{
1452 // This tmp vector is used instead of the newton update vector as they don't
1453 // exist for this solver.
1455 this->triangulation->get_mpi_communicator());
1456
1457 VectorTools::interpolate(
1458 *this->mapping,
1460 simulation_parameters.initial_condition->electromagnetics,
1461 tmp,
1462 fe_trial_interior->component_mask(extractor_E_real));
1463
1464 VectorTools::interpolate(
1465 *this->mapping,
1467 simulation_parameters.initial_condition->electromagnetics,
1468 tmp,
1469 fe_trial_interior->component_mask(extractor_E_imag));
1470
1471 VectorTools::interpolate(
1472 *this->mapping,
1474 simulation_parameters.initial_condition->electromagnetics,
1475 tmp,
1476 fe_trial_interior->component_mask(extractor_H_real));
1477
1478 VectorTools::interpolate(
1479 *this->mapping,
1481 simulation_parameters.initial_condition->electromagnetics,
1482 tmp,
1483 fe_trial_interior->component_mask(extractor_H_imag));
1484
1485 // Note that we don't apply the constraints as the initial condition only
1486 // gives values on the interior dofs and not on the skeleton dofs.
1487
1488 // No percolation of time vectors is needed as this is always a steady-state
1489 // solver.
1490
1491 *this->present_solution = tmp;
1492}
1493
1494template <int dim>
1495void
1497{
1498 preconditioner = std::make_shared<TrilinosWrappers::PreconditionIdentity>();
1499}
1500
1501template <int dim>
1502void
1504{
1505 // Clear previous constraints
1506 this->nonzero_constraints.clear();
1509
1510 DoFTools::make_hanging_node_constraints(*this->dof_handler_trial_skeleton,
1511 this->nonzero_constraints);
1512
1513 // Loop over all defined boundary conditions
1514 for (const auto &[id, type] :
1516 .boundary_conditions_time_harmonic_electromagnetics.type)
1517 {
1519 {
1520 // Perfect electric conductor (PEC) boundary condition
1521 // Real
1522 VectorTools::project_boundary_values_curl_conforming_l2(
1524 0,
1525 Functions::ZeroFunction<dim>(4 * dim),
1526 id,
1527 this->nonzero_constraints);
1528
1529 // Imaginary
1530 VectorTools::project_boundary_values_curl_conforming_l2(
1532 dim,
1533 Functions::ZeroFunction<dim>(4 * dim),
1534 id,
1535 this->nonzero_constraints);
1536 }
1538 {
1539 // Perfect magnetic conductor (PMC) boundary condition
1540 // Real
1541 VectorTools::project_boundary_values_curl_conforming_l2(
1543 2 * dim,
1544 Functions::ZeroFunction<dim>(4 * dim),
1545 id,
1546 this->nonzero_constraints);
1547
1548 // Imaginary
1549 VectorTools::project_boundary_values_curl_conforming_l2(
1551 3 * dim,
1552 Functions::ZeroFunction<dim>(4 * dim),
1553 id,
1554 this->nonzero_constraints);
1555 }
1556 if (type ==
1558 {
1559 // Imposed electric field boundary condition
1560
1561 // Real
1562 VectorTools::project_boundary_values_curl_conforming_l2(
1564 0,
1566 &this->simulation_parameters
1567 .boundary_conditions_time_harmonic_electromagnetics
1568 .imposed_electromagnetic_fields.at(id)
1569 ->e_x_real,
1570 &this->simulation_parameters
1571 .boundary_conditions_time_harmonic_electromagnetics
1572 .imposed_electromagnetic_fields.at(id)
1573 ->e_y_real,
1574 &this->simulation_parameters
1575 .boundary_conditions_time_harmonic_electromagnetics
1576 .imposed_electromagnetic_fields.at(id)
1577 ->e_z_real,
1578 &this->simulation_parameters
1579 .boundary_conditions_time_harmonic_electromagnetics
1580 .imposed_electromagnetic_fields.at(id)
1581 ->e_x_imag,
1582 &this->simulation_parameters
1583 .boundary_conditions_time_harmonic_electromagnetics
1584 .imposed_electromagnetic_fields.at(id)
1585 ->e_y_imag,
1586 &this->simulation_parameters
1587 .boundary_conditions_time_harmonic_electromagnetics
1588 .imposed_electromagnetic_fields.at(id)
1589 ->e_z_imag),
1590 id,
1591 this->nonzero_constraints);
1592
1593 // Imaginary
1594 VectorTools::project_boundary_values_curl_conforming_l2(
1596 dim,
1598 &this->simulation_parameters
1599 .boundary_conditions_time_harmonic_electromagnetics
1600 .imposed_electromagnetic_fields.at(id)
1601 ->e_x_real,
1602 &this->simulation_parameters
1603 .boundary_conditions_time_harmonic_electromagnetics
1604 .imposed_electromagnetic_fields.at(id)
1605 ->e_y_real,
1606 &this->simulation_parameters
1607 .boundary_conditions_time_harmonic_electromagnetics
1608 .imposed_electromagnetic_fields.at(id)
1609 ->e_z_real,
1610 &this->simulation_parameters
1611 .boundary_conditions_time_harmonic_electromagnetics
1612 .imposed_electromagnetic_fields.at(id)
1613 ->e_x_imag,
1614 &this->simulation_parameters
1615 .boundary_conditions_time_harmonic_electromagnetics
1616 .imposed_electromagnetic_fields.at(id)
1617 ->e_y_imag,
1618 &this->simulation_parameters
1619 .boundary_conditions_time_harmonic_electromagnetics
1620 .imposed_electromagnetic_fields.at(id)
1621 ->e_z_imag),
1622 id,
1623 this->nonzero_constraints);
1624 }
1625
1626 if (type ==
1628 {
1629 // Imposed magnetic field boundary condition
1630
1631 // Real
1632 VectorTools::project_boundary_values_curl_conforming_l2(
1634 2 * dim,
1636 &this->simulation_parameters
1637 .boundary_conditions_time_harmonic_electromagnetics
1638 .imposed_electromagnetic_fields.at(id)
1639 ->h_x_real,
1640 &this->simulation_parameters
1641 .boundary_conditions_time_harmonic_electromagnetics
1642 .imposed_electromagnetic_fields.at(id)
1643 ->h_y_real,
1644 &this->simulation_parameters
1645 .boundary_conditions_time_harmonic_electromagnetics
1646 .imposed_electromagnetic_fields.at(id)
1647 ->h_z_real,
1648 &this->simulation_parameters
1649 .boundary_conditions_time_harmonic_electromagnetics
1650 .imposed_electromagnetic_fields.at(id)
1651 ->h_x_imag,
1652 &this->simulation_parameters
1653 .boundary_conditions_time_harmonic_electromagnetics
1654 .imposed_electromagnetic_fields.at(id)
1655 ->h_y_imag,
1656 &this->simulation_parameters
1657 .boundary_conditions_time_harmonic_electromagnetics
1658 .imposed_electromagnetic_fields.at(id)
1659 ->h_z_imag),
1660 id,
1661 this->nonzero_constraints);
1662
1663 // Imaginary
1664 VectorTools::project_boundary_values_curl_conforming_l2(
1666 3 * dim,
1668 &this->simulation_parameters
1669 .boundary_conditions_time_harmonic_electromagnetics
1670 .imposed_electromagnetic_fields.at(id)
1671 ->h_x_real,
1672 &this->simulation_parameters
1673 .boundary_conditions_time_harmonic_electromagnetics
1674 .imposed_electromagnetic_fields.at(id)
1675 ->h_y_real,
1676 &this->simulation_parameters
1677 .boundary_conditions_time_harmonic_electromagnetics
1678 .imposed_electromagnetic_fields.at(id)
1679 ->h_z_real,
1680 &this->simulation_parameters
1681 .boundary_conditions_time_harmonic_electromagnetics
1682 .imposed_electromagnetic_fields.at(id)
1683 ->h_x_imag,
1684 &this->simulation_parameters
1685 .boundary_conditions_time_harmonic_electromagnetics
1686 .imposed_electromagnetic_fields.at(id)
1687 ->h_y_imag,
1688 &this->simulation_parameters
1689 .boundary_conditions_time_harmonic_electromagnetics
1690 .imposed_electromagnetic_fields.at(id)
1691 ->h_z_imag),
1692 id,
1693 this->nonzero_constraints);
1694 }
1695 }
1696
1697 // The DPG method requires the use of skeleton elements and shape functions.
1698 // Because we want to use high-order Nedelec elements for the face, but dealii
1699 // does not support a FE_FaceNedelec, we hack our way around this by using
1700 // full cell elements, but freezing the interior dofs using the function
1701 // constrain_dof_to_zero. To do so, we create a container for all dof indices
1702 // and a container for the face dof indices and we loop on all the faces of
1703 // each cell to find which dofs are on the face and flag them. The remaining
1704 // dofs are then constrained to zero.
1705
1706 std::vector<types::global_dof_index> cell_dof_indices(
1707 this->fe_trial_skeleton->n_dofs_per_cell());
1708 std::vector<types::global_dof_index> face_dof_indices(
1709 this->fe_trial_skeleton->n_dofs_per_face());
1710 std::vector<bool> is_dof_on_face(this->fe_trial_skeleton->n_dofs_per_cell(),
1711 false);
1712
1713 // Loop on all skeleton dofs and set the interior constraints to zero
1714 for (const auto &cell :
1715 this->dof_handler_trial_skeleton->active_cell_iterators())
1716 {
1717 if (cell->is_locally_owned())
1718 {
1719 // Get all dof indices on the cell
1720 cell->get_dof_indices(cell_dof_indices);
1721
1722 // Loop on all the faces of the cell
1723 for (const auto &face : cell->face_iterators())
1724 {
1725 face->get_dof_indices(face_dof_indices);
1726
1727 // Loop on all dofs on the face
1728 for (const auto &face_dof : face_dof_indices)
1729 {
1730 // Find the first iterator in the cell dof indices that
1731 // matches the face dof (find where the face dof is in the
1732 // cell dof indices)
1733 const auto it = std::ranges::find(cell_dof_indices, face_dof);
1734
1735 // If the dof is on the face (find returns the second
1736 // iterator if no match is found), set the corresponding
1737 // flag to true
1738 if (it != cell_dof_indices.end())
1739 {
1740 is_dof_on_face[std::distance(cell_dof_indices.begin(),
1741 it)] = true;
1742 }
1743 }
1744 }
1745
1746 // Loop on all dofs on the cell and constrain the interior ones to
1747 // zero
1748 for (unsigned int index = 0; index < cell_dof_indices.size(); ++index)
1749 {
1750 // If the dof is not on a face, then it is an interior dof
1751 if (!is_dof_on_face[index])
1752 {
1753 this->nonzero_constraints.constrain_dof_to_zero(
1754 cell_dof_indices[index]);
1755 }
1756 }
1757 }
1758 }
1759 this->nonzero_constraints.make_consistent_in_parallel(
1762 this->triangulation->get_mpi_communicator());
1763}
1764
1765template <int dim>
1766void
1768{
1769 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
1770
1771 AssertThrow(
1774 ExcMessage(
1775 "The time harmonic electromagnetism physics does not support a preconditioner. Please set: subsection linear solver -> subsection electromagnetics -> set preconditioner = none."));
1776
1777 // Define the linear solver tolerance
1778 const double absolute_residual =
1780 .minimum_residual;
1781 const double relative_residual =
1783 .relative_residual;
1784
1785 const double rescale_metric = this->get_residual_rescale_metric();
1786 const double rescaled_residual = this->system_rhs.l2_norm() / rescale_metric;
1787 const double linear_solver_tolerance =
1788 std::max(relative_residual * rescaled_residual, absolute_residual);
1789
1790 if (this->simulation_parameters.linear_solver.at(PhysicsID::electromagnetics)
1791 .verbosity != Parameters::Verbosity::quiet)
1792 {
1793 this->pcout << " -Tolerance of iterative solver is : "
1794 << linear_solver_tolerance << std::endl;
1795 }
1796
1797 // Set up the solver control
1798 SolverControl solver_control(this->dof_handler_trial_skeleton->n_dofs(),
1799 linear_solver_tolerance);
1800
1801 // Solve
1802 TrilinosWrappers::SolverCG solver(solver_control);
1803
1804 solver.solve(this->system_matrix,
1806 this->system_rhs,
1807 *this->preconditioner);
1808
1810 .verbosity != Parameters::Verbosity::quiet)
1811 {
1812 this->pcout << " -CG iterative solver took : "
1813 << solver_control.last_step()
1814 << " steps to reach a residual norm of "
1815 << solver_control.last_value() / rescale_metric << std::endl;
1816 }
1817
1818 // Update the solution vector for the skeleton. We keep the solution in the
1819 // initial_guess_iterative_solver vector as it is used for the next iteration
1820 // of the solver and the present_solution_skeleton vector is used for
1821 // everything else (output, multiphysics coupling, etc.). The major difference
1822 // is that the present_solution_skeleton vector may be renormalized which
1823 // would break the iterative solver if it was used for the next iteration.
1826
1827 // Reconstruct the interior solution from the skeleton solution
1829
1830 // We need to apply the scaling to the interior solution as it is the one
1831 // used for the multiphysics coupling and output.
1833 *this->present_solution);
1834
1835 // We also apply the scaling to the skeleton solution for
1836 // consistency, even if it is not used for the multiphysics coupling nor
1837 // outputted at the moment.
1840
1842}
1843
1844template <int dim>
1845bool
1847{
1848 const Parameters::TimeHarmonicMaxwell<dim> &thm_parameters =
1849 this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters;
1850 // For steady simulations, each outer iteration (e.g., mesh adaptation
1851 // cycles) should solve electromagnetics regardless of time-coupling settings.
1852 if (this->simulation_control->get_assembly_method() ==
1854 {
1855 return true;
1856 }
1857
1858 // Always solve at the first step of the simulation (simulation start as 0
1859 // when the set up is performed, and then it is incremented before solving the
1860 // physics for the first time, so we want this function to return true in both
1861 // cases.
1862 if (this->simulation_control->get_iteration_number() <= 1)
1863 {
1864 return true;
1865 }
1866 else
1867 {
1868 switch (thm_parameters.time_coupling_strategy)
1869 {
1871 return false;
1872
1873
1875 // Solve only if we are at a multiple of the specified iteration
1876 // frequency. We subtract 1 since the step number starts at 1.
1877 if ((this->simulation_control->get_iteration_number() - 1) %
1878 thm_parameters.coupling_iteration ==
1879 0)
1880 return true;
1881 else
1882 return false;
1884 {
1885 // Solve only if the current time has passed a multiple of
1886 // the specified time frequency since the last time the
1887 // electromagnetics were solved. This is done by comparing the
1888 // floor of the current time divided by the time coupling
1889 // parameter to the floor of the previous time divided by the time
1890 // coupling parameter. If they are different, it means we have
1891 // passed a multiple of the time coupling parameter and we should
1892 // solve the electromagnetics.
1893 const int difference_in_time_steps = static_cast<int>(
1894 std::floor(this->simulation_control->get_current_time() /
1895 thm_parameters.coupling_time) -
1896 std::floor(this->simulation_control->get_previous_time() /
1897 thm_parameters.coupling_time));
1898 if (difference_in_time_steps == 1)
1899 return true;
1900 else if (difference_in_time_steps > 1)
1901 {
1902 AssertThrow(
1903 false,
1904 ExcMessage(
1905 "The time coupling strategy for the time-harmonic Maxwell solver is set to 'time' with a coupling time of " +
1906 std::to_string(thm_parameters.coupling_time) +
1907 ", but the simulation time has advanced by " +
1908 std::to_string(
1909 this->simulation_control->get_current_time() -
1910 this->simulation_control->get_previous_time()) +
1911 " since the last time the electromagnetics were solved. Please either reduce the simulation time step or increase the coupling time to avoid missing coupling points."));
1912 return true;
1913 }
1914 else
1915 return false;
1916 }
1918 {
1919 auto mpi_communicator = triangulation->get_mpi_communicator();
1920 // Initialize the temporary variables to determine if the physical
1921 // properties have changed by more than the specified threshold
1922 // since the last time the electromagnetics were solved.
1923 unsigned int material_id;
1924 const PhysicalPropertiesManager &physical_properties_manager =
1925 this->simulation_parameters.physical_properties_manager;
1926 std::vector<std::complex<double>>
1927 effective_electric_permittivities_last_solved;
1928 std::vector<std::complex<double>>
1929 effective_magnetic_permeabilities_last_solved;
1930 std::map<field, std::vector<double>>
1931 field_values_vector_last_solved;
1932 std::vector<double> temperature_last_solved_values;
1933 std::vector<std::complex<double>>
1934 effective_electric_permittivities_current;
1935 std::vector<std::complex<double>>
1936 effective_magnetic_permeabilities_current;
1937 std::map<field, std::vector<double>> field_values_vector_current;
1938 std::vector<double> temperature_current_values;
1939 double max_relative_change = 0.0;
1940 unsigned int n_q_points = 0;
1941
1942 // The temperature field is only needed when at least one
1943 // electromagnetic property depends on temperature.
1944 std::unique_ptr<FEValues<dim>> fe_values_temperature;
1945 const DoFHandler<dim> *dof_handler_temperature = nullptr;
1946 const GlobalVectorType *temperature_current_solution = nullptr;
1947
1948
1950 {
1951 dof_handler_temperature =
1952 &this->multiphysics->get_dof_handler(
1954 const QGauss<dim> temperature_quadrature(
1955 dof_handler_temperature->get_fe().degree + 1);
1956 n_q_points = temperature_quadrature.size();
1957 temperature_current_solution =
1958 &this->multiphysics->get_solution(PhysicsID::heat_transfer);
1959
1960 fe_values_temperature = std::make_unique<FEValues<dim>>(
1961 *this->mapping,
1962 dof_handler_temperature->get_fe(),
1963 temperature_quadrature,
1964 update_values | update_quadrature_points);
1965
1966 temperature_last_solved_values.resize(n_q_points);
1967 temperature_current_values.resize(n_q_points);
1968
1969 // Loop over all cell quadrature points and check if the
1970 // physical properties have changed by more than the specified
1971 // threshold since the last time the electromagnetics were
1972 // solved according to the temperature field. If so, we need
1973 // to solve the electromagnetics again.
1974 for (const auto &cell :
1975 dof_handler_temperature->active_cell_iterators())
1976 {
1977 if (cell->is_locally_owned())
1978 {
1979 fe_values_temperature->reinit(cell);
1980 fe_values_temperature->get_function_values(
1981 *temperature_current_solution,
1982 temperature_current_values);
1983
1984 fe_values_temperature->get_function_values(
1986 temperature_last_solved_values);
1987
1988 field_values_vector_last_solved[field::temperature] =
1989 temperature_last_solved_values;
1990 field_values_vector_current[field::temperature] =
1991 temperature_current_values;
1992
1993 material_id = cell->material_id();
1994
1996 physical_properties_manager,
1997 field_values_vector_last_solved,
1998 material_id,
1999 effective_electric_permittivities_last_solved,
2000 effective_magnetic_permeabilities_last_solved);
2001
2003 physical_properties_manager,
2004 field_values_vector_current,
2005 material_id,
2006 effective_electric_permittivities_current,
2007 effective_magnetic_permeabilities_current);
2008
2009 for (unsigned int q = 0; q < n_q_points; ++q)
2010 {
2011 // Here we check if the magnitude of the complex
2012 // property difference normalized by the magnitude
2013 // of the last solved property is greater than the
2014 // threshold. We start by computing only the
2015 // absolute square to do the comparison, and then
2016 // we perform the square root to get the actual
2017 // threshold value to have a faster computation.
2018 double relative_change_electric_permittivity =
2019 std::norm(
2020 effective_electric_permittivities_current[q] -
2021 effective_electric_permittivities_last_solved
2022 [q]) /
2023 std::norm(
2024 effective_electric_permittivities_last_solved
2025 [q]);
2026 double relative_change_magnetic_permeability =
2027 std::norm(
2028 effective_magnetic_permeabilities_current[q] -
2029 effective_magnetic_permeabilities_last_solved
2030 [q]) /
2031 std::norm(
2032 effective_magnetic_permeabilities_last_solved
2033 [q]);
2034 max_relative_change =
2035 std::max({relative_change_electric_permittivity,
2036 relative_change_magnetic_permeability,
2037 max_relative_change});
2038 }
2039 }
2040 }
2041 }
2042
2043
2044
2045 // Reduce the maximum relative change across all MPI ranks
2046 max_relative_change =
2047 Utilities::MPI::max(max_relative_change, mpi_communicator);
2048
2049 if (simulation_parameters.linear_solver
2051 .verbosity != Parameters::Verbosity::quiet)
2052 {
2053 this->pcout
2054 << " - The maximum relative change in the electromagnetics physical properties since the last time the time-harmonic Maxwell equations were solved is "
2055 << std::sqrt(max_relative_change) << std::endl;
2056 }
2057
2058 // Return the threshold comparison result. We take the square
2059 // of the coupling_threshold to get the actual relative change,
2060 // because max_relative_change is already square and it is more
2061 // efficient to elevate to the power 2 rather than computing a
2062 // square root.
2063 if (max_relative_change >
2064 Utilities::fixed_power<2>(thm_parameters.coupling_threshold))
2065 {
2066 return true;
2067 }
2068 else
2069 {
2070 return false;
2071 }
2072 }
2073
2074
2075 default:
2076 AssertThrow(false, ExcMessage("Unknown time coupling strategy."));
2077 return false;
2078 }
2079 }
2080}
2081
2082template <int dim>
2083void
2085{
2086 this->assemblers.clear();
2087 this->face_assemblers.clear();
2088
2089 const Parameters::TimeHarmonicMaxwell<dim> &time_harmonic_maxwell_parameters =
2090 this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters;
2092 boundary_conditions = this->simulation_parameters
2093 .boundary_conditions_time_harmonic_electromagnetics;
2094
2095 // Cell terms: Gram matrix, interior bilinear form and imposed current
2096 // density.
2097 this->assemblers.emplace_back(
2098 std::make_shared<TimeHarmonicMaxwellAssemblerCore<dim>>(
2099 time_harmonic_maxwell_parameters));
2100
2101 // Skeleton terms: skeleton bilinear form.
2102 this->face_assemblers.emplace_back(
2104 boundary_conditions));
2105
2106 // Robin boundary conditions. The waveguide port amplitudes must have been
2107 // computed by `compute_electromagnetic_scaling` before this call. It is done
2108 // in `assemble_system_matrix` before the assemblers are set up.
2109 bool has_robin_boundary = false;
2110 for (const auto &bc : boundary_conditions.type)
2111 // bc is a pair of boundary id and boundary type
2112 if (is_robin_boundary_type(bc.second))
2113 has_robin_boundary = true;
2114 if (has_robin_boundary)
2115 this->face_assemblers.emplace_back(
2117 time_harmonic_maxwell_parameters,
2118 boundary_conditions,
2120}
2121
2122template <int dim>
2123void
2125{
2126 // We always need to compute the scaling for the electromagnetic fields even
2127 // if the user does not want to apply it to the solution because it is used to
2128 // normalize the waveguide inlets relative input power
2130 this->simulation_parameters.physical_properties_manager);
2131
2132 TimerOutput::Scope t(this->computing_timer, "Assemble matrix and RHS");
2133
2134 // The time-harmonic Maxwell system is assembled again every time it is
2135 // solved (i.e., at each coupling step), so the global system needs to be
2136 // reset before the assembly, even when the setup_dofs() is not called again.
2137 this->system_matrix = 0;
2138 this->system_rhs = 0;
2139
2141
2142 // Angular frequency of the electromagnetic fields, used by the scratch data
2143 // to compute the coefficients of the cell terms
2144 const double omega =
2145 2.0 * numbers::PI *
2146 this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters
2147 .electromagnetic_frequency;
2148
2149 auto scratch_data = TimeHarmonicMaxwellScratchData<dim>(
2150 this->simulation_parameters.physical_properties_manager,
2151 omega,
2152 *this->fe_trial_interior,
2153 *this->fe_trial_skeleton,
2154 *this->fe_test,
2155 *this->cell_quadrature,
2156 *this->face_quadrature,
2157 *this->mapping);
2158
2159 // We may need the temperature field for the physical properties.
2161 {
2162 const DoFHandler<dim> &dof_handler_ht =
2163 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
2164 scratch_data.enable_temperature(dof_handler_ht.get_fe(),
2165 *this->cell_quadrature,
2166 *this->face_quadrature,
2167 *this->mapping);
2168 }
2169
2170 // We may also need the void fraction field for the physical properties of
2171 // an effective medium. The mapping of the time-harmonic Maxwell solver is
2172 // used so the void fraction is evaluated at the same quadrature points.
2174 {
2175 const DoFHandler<dim> &dof_handler_void_fraction =
2176 this->multiphysics->get_dof_handler(PhysicsID::void_fraction);
2177 scratch_data.enable_void_fraction(dof_handler_void_fraction.get_fe(),
2178 *this->cell_quadrature,
2179 *this->face_quadrature,
2180 *this->mapping);
2181 }
2182 // As it is standard, we loop over the cells of the triangulation. We use
2183 // the DoFHandler associated with the interior trial space for this loop.
2184 WorkStream::run(this->dof_handler_trial_interior->begin_active(),
2185 this->dof_handler_trial_interior->end(),
2186 *this,
2189 scratch_data,
2190 DPGCopyData(this->fe_test->n_dofs_per_cell(),
2191 this->fe_trial_interior->n_dofs_per_cell(),
2192 this->fe_trial_skeleton->n_dofs_per_cell()));
2193
2194 // After the loop over the cells, we finalize the assembly by compressing
2195 // the vectors because of the MPI parallelization.
2196 this->system_matrix.compress(VectorOperation::add);
2197 this->system_rhs.compress(VectorOperation::add);
2198}
2199
2200template <int dim>
2201void
2203 const typename DoFHandler<dim>::active_cell_iterator &cell,
2205 DPGCopyData &copy_data)
2206{
2207 // We get the same cell for the test and skeleton trial spaces to make sure
2208 // that all the FEValues objects are reinitialized on the same physical cell.
2209 const typename DoFHandler<dim>::active_cell_iterator cell_test =
2210 cell->as_dof_handler_iterator(*this->dof_handler_test);
2211 const typename DoFHandler<dim>::active_cell_iterator cell_skeleton =
2212 cell->as_dof_handler_iterator(*this->dof_handler_trial_skeleton);
2213
2214 scratch_data.reinit(cell, cell_test);
2215
2216 // The temperature field is only evaluated at the quadrature points if the
2217 // physical properties of the cell material depend on it.
2218 typename DoFHandler<dim>::active_cell_iterator cell_temperature;
2219 const GlobalVectorType *temperature_solution = nullptr;
2220 if (scratch_data.cell_material_needs_temperature)
2221 {
2222 cell_temperature = cell->as_dof_handler_iterator(
2223 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer));
2224 temperature_solution =
2225 &this->multiphysics->get_solution(PhysicsID::heat_transfer);
2226 scratch_data.reinit_temperature(cell_temperature, *temperature_solution);
2227 }
2228
2229 // Likewise, the void fraction field is only evaluated at the quadrature
2230 // points if the physical properties of the cell material depend on it.
2231 typename DoFHandler<dim>::active_cell_iterator cell_void_fraction;
2232 const GlobalVectorType *void_fraction_solution = nullptr;
2233 if (scratch_data.cell_material_needs_void_fraction)
2234 {
2235 cell_void_fraction = cell->as_dof_handler_iterator(
2236 this->multiphysics->get_dof_handler(PhysicsID::void_fraction));
2237 void_fraction_solution =
2238 &this->multiphysics->get_solution(PhysicsID::void_fraction);
2239 scratch_data.reinit_void_fraction(cell_void_fraction,
2240 *void_fraction_solution);
2241 }
2242 scratch_data.calculate_physical_properties();
2243 scratch_data.calculate_cell_coefficients();
2244
2245 // We reset the local matrices and vector where we are aggregating the
2246 // information of the current cell.
2247 copy_data.reset();
2248
2249 // Cell contributions to the local DPG system
2250 for (auto &assembler : this->assemblers)
2251 {
2252 assembler->assemble_matrix(scratch_data, copy_data);
2253 assembler->assemble_rhs(scratch_data, copy_data);
2254 }
2255
2256 // Skeleton contributions to the local DPG system. All the faces of the cell
2257 // belong to the skeleton.
2258 for (const unsigned int face_no : cell->face_indices())
2259 {
2260 scratch_data.reinit_face(cell_skeleton, cell_test, face_no);
2261
2262 // The physical properties on the faces are only used by the Robin
2263 // boundary condition, so they are only computed on the boundary faces.
2264 if (scratch_data.face_at_boundary)
2265 {
2266 if (scratch_data.cell_material_needs_temperature)
2267 scratch_data.reinit_face_temperature(cell_temperature,
2268 face_no,
2269 *temperature_solution);
2270 if (scratch_data.cell_material_needs_void_fraction)
2271 scratch_data.reinit_face_void_fraction(cell_void_fraction,
2272 face_no,
2273 *void_fraction_solution);
2275 }
2276
2277 for (auto &assembler : this->face_assemblers)
2278 {
2279 assembler->assemble_matrix(scratch_data, copy_data);
2280 assembler->assemble_rhs(scratch_data, copy_data);
2281 }
2282 }
2283
2284 // After having assembled all the matrices and vectors, we compute the
2285 // condensation operators that are required both for the assembly of the
2286 // skeleton system and for the reconstruction of the interior solution.
2287
2288 // The Gram matrix $G$ is symmetric positive definite, so we compute its
2289 // Cholesky factorization. Its inverse is never formed: it is only applied
2290 // through solves with the Cholesky factors, which overwrite their argument.
2291 // The symmetric property has to be set on every cell, because WorkStream
2292 // copies the copy data and the copy constructor of LAPACKFullMatrix resets
2293 // the property to general.
2294 copy_data.G_matrix.set_property(LAPACKSupport::symmetric);
2295 copy_data.G_matrix.compute_cholesky_factorization();
2296
2297 // We compute $G^{-1} B$ by solving with a copy of $B$.
2298 copy_data.G_inverse_B = copy_data.B_matrix;
2299 copy_data.G_matrix.solve(copy_data.G_inverse_B);
2300
2301 // Then we compute $M_1 = B^\dagger G^{-1} B$.
2302 copy_data.B_matrix.Tmmult(copy_data.M1_matrix, copy_data.G_inverse_B);
2303
2304 // Finally, $M_1$ is also symmetric positive definite, so we compute its
2305 // Cholesky factorization in the same way.
2306 copy_data.M1_matrix.set_property(LAPACKSupport::symmetric);
2307 copy_data.M1_matrix.compute_cholesky_factorization();
2308}
2309
2310template <int dim>
2311void
2313 const typename DoFHandler<dim>::active_cell_iterator &cell,
2315 DPGCopyData &copy_data)
2316{
2317 copy_data.cell_is_local = cell->is_locally_owned();
2318 if (!cell->is_locally_owned())
2319 return;
2320
2321 assemble_local_dpg_system(cell, scratch_data, copy_data);
2322
2323 // Since $G$ is symmetric, we compute $M_2 = B^\dagger G^{-1} \hat{B} =
2324 // (G^{-1} B)^\dagger \hat{B}$ and the vector $M_4 l = B^\dagger G^{-1} l =
2325 // (G^{-1} B)^\dagger l$.
2326 copy_data.G_inverse_B.Tmmult(copy_data.M2_matrix, copy_data.B_hat_matrix);
2327 copy_data.G_inverse_B.Tvmult(copy_data.M4_l, copy_data.l_vector);
2328
2329 // The matrix $G^{-1} \hat{B}$ is used to construct the matrix $M_3 =
2330 // \hat{B}^\dagger G^{-1} \hat{B}$ and, since $G$ is symmetric, the vector
2331 // $M_5 l = \hat{B}^\dagger G^{-1} l = (G^{-1} \hat{B})^\dagger l$.
2332 copy_data.G_inverse_B_hat = copy_data.B_hat_matrix;
2333 copy_data.G_matrix.solve(copy_data.G_inverse_B_hat);
2334 copy_data.B_hat_matrix.Tmmult(copy_data.M3_matrix, copy_data.G_inverse_B_hat);
2335 copy_data.G_inverse_B_hat.Tvmult(copy_data.M5_l, copy_data.l_vector);
2336
2337 // We compute the matrix $M_1^{-1} M_2$
2338 copy_data.M1_inverse_M2 = copy_data.M2_matrix;
2339 copy_data.M1_matrix.solve(copy_data.M1_inverse_M2);
2340
2341 // Now, we have to compute the local matrix and the local RHS for the
2342 // condensed system.
2343
2344 // The cell matrix is obtained with the formula
2345 // $M_3 - M_2^\dagger (M_1^{-1} M_2)$.
2346 copy_data.M2_matrix.Tmmult(copy_data.tmp_matrix_M2M1M2,
2347 copy_data.M1_inverse_M2);
2348 copy_data.M3_matrix.add(-1.0, copy_data.tmp_matrix_M2M1M2);
2349
2350 // This line is used to convert the LAPACK matrix to a full matrix so we can
2351 // perform the distribution to the global system.
2352 copy_data.local_matrix = copy_data.M3_matrix;
2353
2354 // Then we compute the cell RHS using $M_5 l - M_2^\dagger M_1^{-1} M_4 l$.
2355 // Since $M_1$ is symmetric, $M_2^\dagger M_1^{-1} M_4 l =
2356 // (M_1^{-1} M_2)^\dagger M_4 l$.
2357 copy_data.M1_inverse_M2.Tvmult(copy_data.local_rhs, copy_data.M4_l);
2358 // The sadd function computes local_rhs = -1 * local_rhs + 1 * M5_l.
2359 copy_data.local_rhs.sadd(-1.0, 1.0, copy_data.M5_l);
2360
2361 // Get the local dof indices for the skeleton trial space to be able to
2362 // distribute the local matrix and RHS to the global system. Cannot use
2363 // cell->get_dof_indices() because the skeleton trial space is not the same as
2364 // the interior trial space so we need to use the as_dof_handler_iterator()
2365 // function to change the cell iterator to the skeleton trial space and then
2366 // get the dof indices from that.
2367 cell->as_dof_handler_iterator(*this->dof_handler_trial_skeleton)
2368 ->get_dof_indices(copy_data.local_dof_indices);
2369}
2370
2371template <int dim>
2372void
2374 const DPGCopyData &copy_data)
2375{
2376 if (!copy_data.cell_is_local)
2377 return;
2378
2379 this->nonzero_constraints.distribute_local_to_global(
2380 copy_data.local_matrix,
2381 copy_data.local_rhs,
2382 copy_data.local_dof_indices,
2383 this->system_matrix,
2384 this->system_rhs);
2385}
2386
2387template <int dim>
2388void
2390{
2391 // Because of the static condensation, the condensed right-hand side
2392 // requires the full local DPG system. It is therefore assembled with the
2393 // matrix in assemble_system_matrix().
2394}
2395
2396template <int dim>
2397void
2399 const typename DoFHandler<dim>::active_cell_iterator &cell,
2401 DPGCopyData &copy_data)
2402{
2403 copy_data.cell_is_local = cell->is_locally_owned();
2404 if (!cell->is_locally_owned())
2405 return;
2406
2407 assemble_local_dpg_system(cell, scratch_data, copy_data);
2408
2409 // Now, we already have the solution on the skeleton and only need to
2410 // compute $u_h = M_1^{-1} (M_4 l - M_2 \hat{u}_h)$ on each cell. Since $G$
2411 // is symmetric, $M_4 l - M_2 \hat{u}_h = (G^{-1} B)^\dagger (l -
2412 // \hat{B}\hat{u}_h)$, so the interior right-hand side is obtained with two
2413 // matrix-vector products and the matrix $M_2$ is never computed. When $u_h$
2414 // is obtained, we can compute at the same time the error indicator
2415 // $\Psi = G^{-1}(l - B u_h - \hat{B}\hat{u}_h)$ if the dpg error estimator
2416 // is activated.
2417
2418 // We first get the skeleton solution vector for this cell.
2419 cell->as_dof_handler_iterator(*this->dof_handler_trial_skeleton)
2420 ->get_dof_values(*this->present_solution_skeleton,
2421 copy_data.local_skeleton_solution);
2422
2423 // Then we compute $l - \hat{B}\hat{u}_h$, which is stored in l_vector, and
2424 // the interior right-hand side $(G^{-1} B)^\dagger (l - \hat{B}\hat{u}_h)$.
2425 // The interior unknowns are obtained with a single solve with the Cholesky
2426 // factorization of $M_1$. The solve overwrites its argument, so it is done on
2427 // a copy of the right-hand side.
2428 copy_data.B_hat_matrix.vmult(copy_data.tmp_vector_reconstruction,
2429 copy_data.local_skeleton_solution);
2430 copy_data.l_vector -= copy_data.tmp_vector_reconstruction;
2431 copy_data.G_inverse_B.Tvmult(copy_data.local_interior_rhs,
2432 copy_data.l_vector);
2433 copy_data.local_interior_solution = copy_data.local_interior_rhs;
2434 copy_data.M1_matrix.solve(copy_data.local_interior_solution);
2435
2436 // We can also compute the error indicator on this cell if the dpg error
2437 // estimator is activated. The residual R = l - B u_h - \hat{B}\hat{u}_h is
2438 // obtained by subtracting B u_h from l_vector, which already contains
2439 // l - \hat{B}\hat{u}_h. Its Riesz representation Psi = G^{-1} R is stored in
2440 // local_residual, and the squared energy norm of the residual is
2441 // ||R||^2_V = R^T G^-1 R = R^T Psi.
2443 {
2444 copy_data.B_matrix.vmult(copy_data.tmp_vector_reconstruction,
2445 copy_data.local_interior_solution);
2446 copy_data.l_vector -= copy_data.tmp_vector_reconstruction;
2447 // G_matrix holds the Cholesky factorization of G, so Psi is obtained by
2448 // solving with a copy of R, since the solve overwrites its argument.
2449 copy_data.local_residual = copy_data.l_vector;
2450 copy_data.G_matrix.solve(copy_data.local_residual);
2451
2452 copy_data.local_residual_norm_squared =
2453 copy_data.l_vector * copy_data.local_residual;
2454
2455 // Update the active cell index to know which cell the error indicator is
2456 // for.
2457 copy_data.active_cell_index = cell->active_cell_index();
2458 }
2459
2460 cell->get_dof_indices(copy_data.local_dof_indices_trial_interior);
2461}
2462
2463template <int dim>
2464void
2466 const DPGCopyData &copy_data)
2467{
2468 if (!copy_data.cell_is_local)
2469 return;
2470
2471 // We map the cell interior solution to the global interior solution. Adding
2472 // the values at the dof indices of the cell is what
2473 // cell->distribute_local_to_global(local_vector, global_vector) does, which
2474 // cannot be used here since the copier does not have access to the cell.
2475 // Since the interior trial space is discontinuous, each dof belongs to a
2476 // single cell and adding the value is equivalent to setting it.
2479 copy_data.local_interior_solution);
2480
2481 // Store the error indicator of the cell if the dpg error estimator is
2482 // activated
2484 {
2486 std::sqrt(copy_data.local_residual_norm_squared);
2488 }
2489}
2490
2491template <int dim>
2492void
2494{
2495 MPI_Comm mpi_communicator = this->triangulation->get_mpi_communicator();
2496
2497 // The interior solution is assembled cell by cell. However,
2498 // present_solution is a ghosted vector (it also stores the locally relevant
2499 // dofs owned by the other processes, which are needed by the other physics
2500 // and the output) and a ghosted vector can only be read. Therefore, the
2501 // contributions of the locally owned cells are first added to the following
2502 // vector, which only stores the locally owned dofs. After the loop on the
2503 // cells, compress(VectorOperation::add) finalizes it, and the assignment to
2504 // the ghosted vector updates its ghost values. This vector is only needed
2505 // during the reconstruction, so it is allocated here and released at the end
2506 // of the function to avoid keeping it in memory between the solves.
2508 this->locally_owned_dofs_trial_interior, mpi_communicator);
2509
2510 // Squared L2 norm of the residual for the dpg error indicator, accumulated
2511 // over the locally owned cells
2512 this->squared_residual_L2_norm = 0.0;
2513
2514 // Angular frequency of the electromagnetic fields, used by the scratch data
2515 // to compute the coefficients of the cell terms
2516 const double omega =
2517 2.0 * numbers::PI *
2518 this->simulation_parameters.multiphysics.time_harmonic_maxwell_parameters
2519 .electromagnetic_frequency;
2520
2521 auto scratch_data = TimeHarmonicMaxwellScratchData<dim>(
2522 this->simulation_parameters.physical_properties_manager,
2523 omega,
2524 *this->fe_trial_interior,
2525 *this->fe_trial_skeleton,
2526 *this->fe_test,
2527 *this->cell_quadrature,
2528 *this->face_quadrature,
2529 *this->mapping);
2530
2532 {
2533 const DoFHandler<dim> &dof_handler_ht =
2534 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
2535 scratch_data.enable_temperature(dof_handler_ht.get_fe(),
2536 *this->cell_quadrature,
2537 *this->face_quadrature,
2538 *this->mapping);
2539 }
2540
2542 {
2543 const DoFHandler<dim> &dof_handler_void_fraction =
2544 this->multiphysics->get_dof_handler(PhysicsID::void_fraction);
2545 scratch_data.enable_void_fraction(dof_handler_void_fraction.get_fe(),
2546 *this->cell_quadrature,
2547 *this->face_quadrature,
2548 *this->mapping);
2549 }
2550
2551 WorkStream::run(this->dof_handler_trial_interior->begin_active(),
2552 this->dof_handler_trial_interior->end(),
2553 *this,
2556 scratch_data,
2557 DPGCopyData(this->fe_test->n_dofs_per_cell(),
2558 this->fe_trial_interior->n_dofs_per_cell(),
2559 this->fe_trial_skeleton->n_dofs_per_cell()));
2560
2561 // After the loop over the cells, we finalize the assembly by compressing
2562 // the vector because of the MPI parallelization.
2563 this->locally_owned_solution_interior.compress(VectorOperation::add);
2564
2566
2567 // The non-ghosted vector is not needed anymore, so we release its memory by
2568 // swapping it with an empty vector, which is deallocated at the end of the
2569 // scope. This is used instead of clear(), which is not available for all the
2570 // types of GlobalVectorType.
2571 {
2572 GlobalVectorType empty_vector;
2573 this->locally_owned_solution_interior.swap(empty_vector);
2574 }
2575
2576 // We also output the global error indicator if the dpg error estimator is
2577 // activated and in verbose mode
2579 (this->simulation_parameters.linear_solver.at(PhysicsID::electromagnetics)
2580 .verbosity != Parameters::Verbosity::quiet))
2581 {
2582 this->pcout << " Time-Harmonic Maxwell DPG residual: "
2583 << std::sqrt(
2584 Utilities::MPI::sum(this->squared_residual_L2_norm,
2585 mpi_communicator))
2586 << std::endl;
2587 }
2588}
2589
2590
2591template class TimeHarmonicMaxwell<2>;
2592template class TimeHarmonicMaxwell<3>;
AuxiliaryPhysics(const Parameters::NonLinearSolver non_linear_solver_parameters)
AuxiliaryPhysics - Base constructor for Auxiliary physics. This constructor builds an auxiliary physi...
std::map< types::boundary_id, BoundaryType > type
Map containing the boundary id and the boundary type.
This class manages the boundary conditions for Time Harmonic Maxwell solver. It introduces the bounda...
Class responsible for storing the local system of an ultraweak Discontinuous Petrov-Galerkin (DPG) di...
Definition copy_data.h:232
LAPACKFullMatrix< double > M3_matrix
Definition copy_data.h:312
void reset()
Resets the uncondensed local system to zero. The matrix is also reset since LAPACKFullMatrix keeps t...
Definition copy_data.h:284
FullMatrix< double > local_matrix
Definition copy_data.h:323
LAPACKFullMatrix< double > G_inverse_B_hat
Definition copy_data.h:305
Vector< double > l_vector
Definition copy_data.h:300
Vector< double > local_residual
Definition copy_data.h:339
LAPACKFullMatrix< double > B_hat_matrix
Definition copy_data.h:298
LAPACKFullMatrix< double > M1_matrix
Definition copy_data.h:308
Vector< double > tmp_vector_reconstruction
Definition copy_data.h:337
LAPACKFullMatrix< double > M2_matrix
Definition copy_data.h:310
LAPACKFullMatrix< double > G_inverse_B
Definition copy_data.h:303
Vector< double > local_skeleton_solution
Definition copy_data.h:330
std::vector< types::global_dof_index > local_dof_indices
Definition copy_data.h:327
bool cell_is_local
Definition copy_data.h:352
Vector< double > local_interior_rhs
Definition copy_data.h:332
std::vector< types::global_dof_index > local_dof_indices_trial_interior
Definition copy_data.h:341
Vector< double > local_interior_solution
Definition copy_data.h:334
Vector< double > M5_l
Definition copy_data.h:316
double local_residual_norm_squared
Definition copy_data.h:344
LAPACKFullMatrix< double > M1_inverse_M2
Definition copy_data.h:318
LAPACKFullMatrix< double > G_matrix
Definition copy_data.h:294
LAPACKFullMatrix< double > B_matrix
Definition copy_data.h:296
unsigned int active_cell_index
Definition copy_data.h:346
LAPACKFullMatrix< double > tmp_matrix_M2M1M2
Definition copy_data.h:320
Vector< double > M4_l
Definition copy_data.h:314
Vector< double > local_rhs
Definition copy_data.h:325
Class that manages the physical properties model which are required to calculate the various physical...
ConditionalOStream pcout
Class that assembles the cell terms of the ultraweak DPG formulation of the time-harmonic Maxwell equ...
Class that assembles the Robin boundary conditions of the time-harmonic Maxwell equations (silver_mul...
Class that assembles the skeleton terms of the ultraweak DPG formulation of the time-harmonic Maxwell...
This class implements a boundary condition type for the Time Harmonic Maxwell equation where the elec...
This class implements a boundary condition type for the Time Harmonic Maxwell equation where the magn...
Class that stores the information required by the assembly of the Discontinuous Petrov-Galerkin (DPG)...
void calculate_face_physical_properties()
Compute the effective electromagnetic properties at the face quadrature points from the fields gather...
void reinit_temperature(const typename DoFHandler< dim >::active_cell_iterator &cell_temperature, const VectorType &temperature_solution)
Evaluate the temperature field at the cell quadrature points.
void reinit_face(const typename DoFHandler< dim >::active_cell_iterator &cell_skeleton, const typename DoFHandler< dim >::active_cell_iterator &cell_test, const unsigned int face_no)
Reinitialize the content of the scratch for a face of the current cell. It evaluates the test functio...
void reinit_face_temperature(const typename DoFHandler< dim >::active_cell_iterator &cell_temperature, const unsigned int face_no, const VectorType &temperature_solution)
Evaluate the temperature field at the quadrature points of a face of the current cell.
void calculate_physical_properties()
Compute the effective electromagnetic properties at the cell quadrature points from the fields gather...
void calculate_cell_coefficients()
Compute the coefficients of the time-harmonic Maxwell equations of the cell at the quadrature points.
void reinit_void_fraction(const typename DoFHandler< dim >::active_cell_iterator &cell_void_fraction, const VectorType &void_fraction_solution)
Evaluate the void fraction field at the cell quadrature points.
void reinit_face_void_fraction(const typename DoFHandler< dim >::active_cell_iterator &cell_void_fraction, const unsigned int face_no, const VectorType &void_fraction_solution)
Evaluate the void fraction field at the quadrature points of a face of the current cell.
void reinit(const typename DoFHandler< dim >::active_cell_iterator &cell, const typename DoFHandler< dim >::active_cell_iterator &cell_test)
Reinitialize the content of the scratch for a cell. It evaluates the shape functions of the test and ...
MultiphysicsInterface< dim > * multiphysics
std::shared_ptr< TrilinosWrappers::PreconditionIdentity > preconditioner
virtual void finish_simulation() override
Carry out the operations required to finish a simulation correctly.
void compute_electromagnetic_scaling(const PhysicalPropertiesManager &physical_properties_manager)
Compute the electromagnetic scaling factor used to non-dimensionalize the time-harmonic Maxwell syste...
std::shared_ptr< DoFHandler< dim > > dof_handler_test
IndexSet locally_relevant_dofs_trial_interior
void copy_local_matrix_to_global_matrix(const DPGCopyData &copy_data)
Distribute the condensed skeleton system of a cell in the global system matrix and right-hand side.
double get_residual_rescale_metric() const override
Return the metric for residual rescaling. By default, should return 1. If the rescale_residual_by_vol...
void output_per_iteration_timer() override
Output the per iteration wallclock times.
virtual void pre_mesh_adaptation() override
pre_mesh_adaption Prepares the auxiliary physics variables for a mesh refinement/coarsening
virtual void compute_dpg_error(dealii::Vector< float > &estimated_error_per_cell)
Compute the DPG error estimator based on the energy norm residual used to refine mesh of the TimeHarm...
void scale_solution_components(const DoFHandler< dim > &dof_handler, GlobalVectorType &solution)
Scale the solution components by the electromagnetic scaling factor to recover the physical values of...
virtual void post_mesh_adaptation() override
post_mesh_adaption Interpolates the auxiliary physics variables to the new mesh
void assemble_system_rhs() override
Assemble the rhs associated with the solver.
void setup_preconditioner() override
Sets up preconditioner of the system matrix.
void copy_local_interior_solution_to_global(const DPGCopyData &copy_data)
WorkStream copier of the interior reconstruction. It adds the interior solution of a cell,...
void assemble_local_dpg_system(const typename DoFHandler< dim >::active_cell_iterator &cell, TimeHarmonicMaxwellScratchData< dim > &scratch_data, DPGCopyData &copy_data)
Assemble the uncondensed local DPG system ( , , and ) of a cell with the cell and face assemblers,...
bool should_solve_auxiliary_physics() override
Manage if the auxiliary physics should be solved at the current iteration. By default,...
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
std::shared_ptr< SolutionTransfer< dim, GlobalVectorType > > solution_transfer
IndexSet locally_relevant_dofs_trial_skeleton
void assemble_system_matrix() override
Assemble the matrix associated with the solver.
const SimulationParameters< dim > & simulation_parameters
GlobalVectorType locally_owned_solution_interior
virtual void setup_dofs() override
Sets up the DofHandler and the degree of freedom associated with the physics.
virtual void update_boundary_conditions() override
Updates non zero constraints if the boundary is time-dependent.
virtual void read_checkpoint() override
Set solution vector of Auxiliary Physics using checkpoint.
void compute_error_estimate(const std::pair< const Variable, Parameters::MultipleAdaptationParameters > &ivar, dealii::Vector< float > &estimated_error_per_cell) override
Compute the error estimator selected in the parameters for adaptive mesh refinement.
virtual std::vector< OutputStructTableHandler > gather_tables() override
Returns a vector of references to TableHandler objects that needs to be serialized/deserialized for a...
virtual void postprocess(bool first_iteration) override
Postprocess the auxiliary physics results. Post-processing this case implies the calculation of all d...
void print_THM_setup_memory(const TrilinosWrappers::SparsityPattern &sparsity_pattern)
Print memory usage diagnostics after the THM setup.
std::shared_ptr< Quadrature< dim - 1 > > face_quadrature
std::shared_ptr< FiniteElement< dim > > fe_trial_skeleton
virtual std::vector< OutputStruct< dim, VectorType > > gather_output_hook() override
Gather and return vector of output structs that are particular to some applications.
virtual void percolate_time_vectors() override
Carry out the operations required to rearrange the values of the previous solution at the end of a ti...
const FEValuesExtractors::Vector extractor_E_real
const FEValuesExtractors::Vector extractor_E_imag
std::vector< double > waveguide_ports_electric_amplitudes
std::shared_ptr< SimulationControl > simulation_control
std::shared_ptr< GlobalVectorType > present_solution_skeleton
AffineConstraints< double > nonzero_constraints
GlobalVectorType initial_guess_iterative_solver
void update_material_properties_dependencies()
Updates the containers that store the properties from which the material properties of the time-harmo...
void define_constraints()
Define the constraints for the TimeHarmonicMaxwell solver. This is equivalent to the define_non_zero_...
void print_memory_consumption(const dealii::ConditionalOStream &pcout, const std::string &str, const unsigned int n_rank, const std::vector< double > &memory_object)
This helper function helps to compute the memory consumption of different objects on each rank and pr...
virtual void write_checkpoint() override
Prepares auxiliary physics to write checkpoint.
std::shared_ptr< DoFHandler< dim > > dof_handler_trial_interior
void reconstruct_local_interior_solution(const typename DoFHandler< dim >::active_cell_iterator &cell, TimeHarmonicMaxwellScratchData< dim > &scratch_data, DPGCopyData &copy_data)
WorkStream worker of the interior reconstruction. It assembles the local DPG system of a cell with as...
std::shared_ptr< SolutionTransfer< dim, GlobalVectorType > > temperature_last_solved_solution_transfer
TimeHarmonicMaxwell(MultiphysicsInterface< dim > *multiphysics_interface, const SimulationParameters< dim > &p_simulation_parameters, const std::shared_ptr< parallel::DistributedTriangulationBase< dim > > &p_triangulation, std::shared_ptr< SimulationControl > p_simulation_control)
Constructor.
virtual void set_initial_conditions() override
Sets up the initial conditions associated with the physics. Generally, most physics only support impo...
void assemble_local_system_matrix(const typename DoFHandler< dim >::active_cell_iterator &cell, TimeHarmonicMaxwellScratchData< dim > &scratch_data, DPGCopyData &copy_data)
Assemble the local DPG system of a cell using the function assemble_local_dpg_system and condense it ...
std::shared_ptr< Quadrature< dim > > cell_quadrature
std::shared_ptr< FiniteElement< dim > > fe_test
void compute_kelly(dealii::Vector< float > &estimated_error_per_cell, const ComponentMask &component_mask)
Compute the Kelly error estimator on the electromagnetic variable for mesh refinement....
const FEValuesExtractors::Vector extractor_H_real
Vector< float > local_estimated_error_per_cell
const FEValuesExtractors::Vector extractor_H_imag
void solve_linear_system() override
Solve the linear system using the CG solver.
std::shared_ptr< FiniteElement< dim > > fe_trial_interior
std::vector< double > calculate_L2_error()
Calculates the L2 error of the solution. It returns a vector of doubles containing the L2 errors of t...
std::shared_ptr< DoFHandler< dim > > dof_handler_trial_skeleton
virtual void modify_solution() override
Carry out modifications on the auxiliary physics solution. To be defined for some physics only (eg....
TrilinosWrappers::SparseMatrix system_matrix
void setup_assemblers()
Set up the cell and face assemblers of the DPG system according to the simulation parameters.
std::shared_ptr< GlobalVectorType > present_solution
void verify_consistency_of_boundary_conditions()
Verify consistency of the input parameters for boundary conditions to ensure that for every boundary ...
std::vector< std::shared_ptr< TimeHarmonicMaxwellFaceAssemblerBase< dim > > > face_assemblers
std::vector< std::shared_ptr< TimeHarmonicMaxwellAssemblerBase< dim > > > assemblers
void reconstruct_interior_solution()
Reconstruct the interior solution from the skeleton solution after solving the linear system on the s...
std::shared_ptr< Mapping< dim > > mapping
GlobalVectorType temperature_last_solved_solution
void serialize_tables_vector(const std::vector< OutputStructTableHandler > &table_output_structs, const MPI_Comm mpi_communicator)
Serializes all the TableHandler objects in table_output_structs.
Definition utilities.h:602
void announce_string(const ConditionalOStream &pcout, const std::string &expression, const char delimiter='-')
Prints a string and then adds a line above and below made with dashes containing as many dashes as th...
Definition utilities.h:500
void deserialize_tables_vector(std::vector< OutputStructTableHandler > &table_output_structs, const MPI_Comm mpi_communicator)
Deserializes all the TableHandler objects in table_output_structs.
Definition utilities.h:624
Identifiers for multiphysics modules and their solution fields.
@ electromagnetic_fields
Combination of both electric and magnetic fields from electromagnetics.
@ magnetic_field
Magnetic field vector field from electromagnetics.
@ electric_field
Electric field vector field from electromagnetics.
@ VANS
Volume-averaged Navier-Stokes (VANS) equations.
@ electromagnetics
Time-harmonic electromagnetics (Electric field, Magnetic field).
@ void_fraction
@ heat_transfer
Heat transfer (Temperature).
TimeHarmonicMaxwellBoundaryType
Boundary condition types of the time-harmonic Maxwell equations.
std::pair< double, double > calculate_L2_error(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Function< dim > *exact_solution, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculates the L2 norm of the error on velocity and pressure.
Struct containing information about the solution output. It is used to pass all the information requi...
TimeHarmonicMaxwell - Defines the parameters for time-harmonic Maxwell simulations.
Parameters::TimeHarmonicMaxwellCouplingStrategy time_coupling_strategy
bool is_robin_boundary_type(const BoundaryConditions::TimeHarmonicMaxwellBoundaryType type)
Check if a boundary condition type of the time-harmonic Maxwell equations is a Robin boundary conditi...
std::pair< Tensor< 1, dim, std::complex< double > >, Tensor< 1, dim, std::complex< double > > > compute_waveguide_port_incident_fields(const Parameters::TimeHarmonicMaxwell< dim > &time_harmonic_maxwell_parameters, const Point< dim > &p, const Tensor< 1, dim > &normal, const std::complex< double > &effective_electric_permittivity, const std::complex< double > &effective_magnetic_permeability, const unsigned int waveguide_port_index)
Compute the incident electromagnetic fields of a rectangular waveguide port from the input parameters...
unsigned int get_waveguide_port_index(const Parameters::TimeHarmonicMaxwell< dim > &time_harmonic_maxwell_parameters, const types::boundary_id boundary_id)
Get the index of the waveguide port condition applied on a boundary, that is the index of the wavegui...
bool electromagnetic_properties_depend_on(const PhysicalPropertiesManager &physical_properties_manager, const unsigned int material_id, const field field_id)
Check if at least one electromagnetic property of a material depends on a field. If the material is a...
void compute_effective_electromagnetic_properties(const PhysicalPropertiesManager &physical_properties_manager, const std::map< field, std::vector< double > > &field_values_vectors, const unsigned int material_id, std::vector< std::complex< double > > &effective_electric_permittivities, std::vector< std::complex< double > > &effective_magnetic_permeabilities)
Compute the effective electromagnetic properties of a material at a set of points....
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17