https://mooseframework.inl.gov
ComplexEquationSystem.C
Go to the documentation of this file.
1 #ifdef MOOSE_MFEM_ENABLED
2 
4 #include "libmesh/int_range.h"
5 
6 namespace Moose::MFEM
7 {
8 
9 void
11  ComplexGridFunctions & cmplx_gridfunctions,
12  mfem::AssemblyLevel assembly_level)
13 {
14  _assembly_level = assembly_level;
15 
16  if (gridfunctions.size())
17  mooseError("Mixing real and complex variables is currently not supported.");
18 
19  for (auto & test_var_name : _test_var_names)
20  {
21  if (!cmplx_gridfunctions.Has(test_var_name))
22  {
23  mooseError("MFEM complex variable ",
24  test_var_name,
25  " requested by equation system during initialization was "
26  "not found in gridfunctions");
27  }
28  // Store pointers to test FESpaces
29  _test_pfespaces.push_back(cmplx_gridfunctions.Get(test_var_name)->ParFESpace());
30  // Create auxiliary gridfunctions for storing essential constraints from Dirichlet conditions
31  _cmplx_var_ess_constraints.emplace_back(std::make_unique<mfem::ParComplexGridFunction>(
32  cmplx_gridfunctions.Get(test_var_name)->ParFESpace()));
33  }
34 
35  // Store pointers to FESpaces of all coupled variables
36  for (auto & coupled_var_name : _coupled_var_names)
37  _coupled_pfespaces.push_back(cmplx_gridfunctions.Get(coupled_var_name)->ParFESpace());
38 
39  // Extract which coupled variables are to be trivially eliminated and which are trial variables
41 
42  // Store pointers to coupled variable ComplexGridFunctions that are to be eliminated prior to
43  // forming the jacobian
44  for (auto & eliminated_var_name : _eliminated_var_names)
45  _cmplx_eliminated_variables.Register(eliminated_var_name,
46  cmplx_gridfunctions.GetShared(eliminated_var_name));
47 
48  // Get a reference to the complex GridFunctions
49  _complex_gfuncs = &cmplx_gridfunctions;
50 }
51 
52 void
54 {
57 }
58 
59 void
61 {
62  // Register linear forms
63  for (const auto i : index_range(_test_var_names))
64  {
65  auto test_var_name = _test_var_names.at(i);
66  _clfs.Register(test_var_name,
67  std::make_shared<mfem::ParComplexLinearForm>(_test_pfespaces.at(i)));
68  _clfs.GetRef(test_var_name) = 0.0;
69  }
70  // Apply boundary conditions
72 
73  for (auto & test_var_name : _test_var_names)
74  {
75  // Apply kernels
76  auto clf = _clfs.GetShared(test_var_name);
77  ApplyDomainLFIntegrators(test_var_name, clf, _cmplx_kernels_map);
79  clf->Assemble();
80  }
81 }
82 
83 void
85 {
86  // Register bilinear forms
87  for (const auto i : index_range(_test_var_names))
88  {
89  auto test_var_name = _test_var_names.at(i);
90  _slfs.Register(test_var_name,
91  std::make_shared<mfem::ParSesquilinearForm>(_test_pfespaces.at(i)));
92 
93  // Apply kernels
94  auto slf = _slfs.GetShared(test_var_name);
95  slf->SetAssemblyLevel(_assembly_level);
96  ApplyBoundaryBLFIntegrators<mfem::ParSesquilinearForm>(
97  test_var_name, test_var_name, slf, _cmplx_integrated_bc_map);
98  ApplyDomainBLFIntegrators<mfem::ParSesquilinearForm>(
99  test_var_name, test_var_name, slf, _cmplx_kernels_map);
100  // Assemble
101  slf->Assemble();
102  }
103 }
104 
105 void
107  mfem::ParComplexGridFunction & trial_gf,
108  mfem::Array<int> & global_ess_markers)
109 {
110  if (_cmplx_essential_bc_map.Has(var_name))
111  {
112  auto & bcs = _cmplx_essential_bc_map.GetRef(var_name);
113  for (auto & bc : bcs)
114  {
115  // Set constrained DoFs values on essential boundaries
116  bc->ApplyBC(trial_gf);
117  // Fetch marker array labelling essential boundaries of current BC
118  mfem::Array<int> ess_bdrs(bc->getBoundaryMarkers());
119  // Add these boundary markers to the set of markers labelling all essential boundaries
120  for (const auto i : make_range(trial_gf.ParFESpace()->GetParMesh()->bdr_attributes.Max()))
121  global_ess_markers[i] = std::max(global_ess_markers[i], ess_bdrs[i]);
122  }
123  }
124 }
125 
126 void
128 {
129  _ess_tdof_lists.resize(_trial_var_names.size());
130  for (const auto i : index_range(_trial_var_names))
131  {
132  const auto & trial_var_name = _trial_var_names.at(i);
133  mfem::ParComplexGridFunction & trial_gf = *_cmplx_var_ess_constraints.at(i);
134 
135  // Make sure we update the size, if this mesh has changed recently for instance
136  trial_gf.Update();
137 
138  // Initial guess for non-linear problems (initial condition or the previous time step solution)
139  static_cast<mfem::Vector &>(trial_gf) = _complex_gfuncs->GetRef(trial_var_name);
140 
141  mfem::Array<int> global_ess_markers(trial_gf.ParFESpace()->GetParMesh()->bdr_attributes.Max());
142  global_ess_markers = 0;
143  // Set strongly constrained DoFs of trial_gf on essential boundaries and add markers for all
144  // essential boundaries to the global_ess_markers array
145  ApplyComplexEssentialBC(trial_var_name, trial_gf, global_ess_markers);
146  trial_gf.FESpace()->GetEssentialTrueDofs(global_ess_markers, _ess_tdof_lists.at(i));
147  }
148 }
149 
150 void
151 ComplexEquationSystem::AddComplexKernel(std::shared_ptr<MFEMComplexKernel> kernel)
152 {
153  const auto & trial_var_name = kernel->getTrialVariableName();
154  const auto & test_var_name = kernel->getTestVariableName();
155  AddCoupledVariableNameIfMissing(trial_var_name);
156  AddTestVariableNameIfMissing(test_var_name);
157  // Register new complex kernels map if not present for the test variable
158  if (!_cmplx_kernels_map.Has(test_var_name))
159  {
160  auto kernel_field_map =
161  std::make_shared<NamedFieldsMap<std::vector<std::shared_ptr<MFEMComplexKernel>>>>();
162  _cmplx_kernels_map.Register(test_var_name, std::move(kernel_field_map));
163  }
164  // Register new complex kernels map if not present for the test/trial variable pair
165  if (!_cmplx_kernels_map.Get(test_var_name)->Has(trial_var_name))
166  {
167  auto kernels = std::make_shared<std::vector<std::shared_ptr<MFEMComplexKernel>>>();
168  _cmplx_kernels_map.Get(test_var_name)->Register(trial_var_name, std::move(kernels));
169  }
170  _cmplx_kernels_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(kernel));
171 }
172 
173 void
174 ComplexEquationSystem::AddComplexIntegratedBC(std::shared_ptr<MFEMComplexIntegratedBC> bc)
175 {
176  const auto & trial_var_name = bc->getTrialVariableName();
177  const auto & test_var_name = bc->getTestVariableName();
178  AddCoupledVariableNameIfMissing(trial_var_name);
179  AddTestVariableNameIfMissing(test_var_name);
180  // Register new complex integrated bc map if not present for the test variable
181  if (!_cmplx_integrated_bc_map.Has(test_var_name))
182  {
183  auto integrated_bc_field_map =
184  std::make_shared<NamedFieldsMap<std::vector<std::shared_ptr<MFEMComplexIntegratedBC>>>>();
185  _cmplx_integrated_bc_map.Register(test_var_name, std::move(integrated_bc_field_map));
186  }
187  // Register new complex integrated bc map if not present for the test/trial variable pair
188  if (!_cmplx_integrated_bc_map.Get(test_var_name)->Has(trial_var_name))
189  {
190  auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMComplexIntegratedBC>>>();
191  _cmplx_integrated_bc_map.Get(test_var_name)->Register(trial_var_name, std::move(bcs));
192  }
193  _cmplx_integrated_bc_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(bc));
194 }
195 
196 void
197 ComplexEquationSystem::AddComplexEssentialBCs(std::shared_ptr<MFEMComplexEssentialBC> bc)
198 {
199  const auto & test_var_name = bc->getTestVariableName();
200  AddTestVariableNameIfMissing(test_var_name);
201  // Register new complex essential bc map if not present for the test variable
202  if (!_cmplx_essential_bc_map.Has(test_var_name))
203  {
204  auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMComplexEssentialBC>>>();
205  _cmplx_essential_bc_map.Register(test_var_name, std::move(bcs));
206  }
207  _cmplx_essential_bc_map.GetRef(test_var_name).push_back(std::move(bc));
208 }
209 
210 void
212  mfem::BlockVector & trueX,
213  mfem::BlockVector & trueRHS)
214 {
215  auto & test_var_name = _test_var_names.at(0);
216  mfem::Vector aux_x, aux_rhs;
217  mfem::OperatorPtr aux_a;
218 
219  auto slf = _slfs.Get(test_var_name);
220  slf->FormLinearSystem(_ess_tdof_lists.at(0),
222  *_clfs.Get(test_var_name),
223  aux_a,
224  aux_x,
225  aux_rhs,
226  /*copy_interior=*/true);
227 
228  trueX.GetBlock(0) = aux_x;
229  trueRHS.GetBlock(0) = aux_rhs;
230  trueX.SyncFromBlocks();
231  trueRHS.SyncFromBlocks();
232 
233  op.Reset(aux_a.Ptr());
234  aux_a.SetOperatorOwner(false);
235 }
236 
237 void
238 ComplexEquationSystem::FormSystemMatrix(mfem::OperatorHandle & op,
239  mfem::BlockVector & trueX,
240  mfem::BlockVector & trueRHS)
241 {
242 
243  // Allocate block operator
244  DeleteHBlocks();
245  _h_blocks.SetSize(_test_var_names.size(), _trial_var_names.size());
246  _h_blocks = nullptr;
247  // Zero out RHS and sync memory
248  trueRHS = 0.0;
249  trueRHS.SyncToBlocks();
250 
251  // Form diagonal blocks.
252  for (const auto i : index_range(_test_var_names))
253  {
254  auto & test_var_name = _test_var_names.at(i);
255 
256  mfem::Vector aux_x, aux_rhs;
257  mfem::OperatorHandle aux_a;
258 
259  auto slf = _slfs.Get(test_var_name);
260  slf->FormLinearSystem(_ess_tdof_lists.at(i),
262  *_clfs.Get(test_var_name),
263  aux_a,
264  aux_x,
265  aux_rhs,
266  /*copy_interior=*/true);
267  trueX.GetBlock(i) = aux_x;
268  trueRHS.GetBlock(i) = aux_rhs;
269  _h_blocks(i, i) = aux_a.As<mfem::ComplexHypreParMatrix>()->GetSystemMatrix();
270  }
271  // Sync memory
272  trueX.SyncFromBlocks();
273  trueRHS.SyncFromBlocks();
274 
275  // Create monolithic matrix
276  op.Reset(mfem::HypreParMatrixFromBlocks(_h_blocks));
277 }
278 
279 // Equation system Mult
280 void
281 ComplexEquationSystem::Mult(const mfem::Vector & x, mfem::Vector & residual) const
282 {
283  _linear_operator->Mult(x, residual);
284  x.HostRead();
285  residual.HostRead();
286 }
287 
288 void
289 ComplexEquationSystem::SetTrialVariablesFromTrueVectors(const mfem::BlockVector & trueX) const
290 {
291  for (const auto i : index_range(_trial_var_names))
292  {
293  auto & trial_var_name = _trial_var_names.at(i);
294  trueX.GetBlock(i).SyncMemory(trueX);
295  _complex_gfuncs->Get(trial_var_name)->Distribute(&(trueX.GetBlock(i)));
296  }
297 }
298 
299 }
300 
301 #endif
virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const override
Nonlinear Mult (Used by Newton-solver not necessarily nonlinear)
virtual void SetTrialVariablesFromTrueVectors(const mfem::BlockVector &trueX) const override
Update variable from solution vector after solve.
std::vector< std::unique_ptr< mfem::ParComplexGridFunction > > _cmplx_var_ess_constraints
Complex Gridfunctions holding essential constraints from Dirichlet BCs.
virtual void AddTestVariableNameIfMissing(const std::string &test_var_name)
Add test variable to EquationSystem.
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
std::vector< mfem::ParFiniteElementSpace * > _coupled_pfespaces
Pointers to finite element spaces associated with coupled variables.
virtual void BuildLinearForms() override
Build linear forms and eliminate constrained DoFs.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
NamedFieldsMap< mfem::ParComplexLinearForm > _clfs
virtual void FormSystemOperator(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS) override
Form matrix-free representation of system operator.
std::vector< std::string > _eliminated_var_names
Names of all coupled variables without a corresponding test variable.
ComplexGridFunctions _cmplx_eliminated_variables
Pointers to coupled variables not part of the reduced EquationSystem.
virtual void ApplyComplexEssentialBC(const std::string &var_name, mfem::ParComplexGridFunction &trial_gf, mfem::Array< int > &global_ess_markers)
Apply essential BC(s) associated with var_name to set true DoFs of trial_gf and update markers of all...
std::vector< mfem::Array< int > > _ess_tdof_lists
mfem::AssemblyLevel _assembly_level
auto max(const L &left, const R &right)
std::vector< mfem::ParFiniteElementSpace * > _test_pfespaces
Pointers to finite element spaces associated with test variables.
void AddComplexEssentialBCs(std::shared_ptr< MFEMComplexEssentialBC > bc)
Add complex essential BCs.
virtual void ApplyEssentialBCs() override
Update all essentially constrained true DoF markers and values on boundaries.
std::vector< std::string > _trial_var_names
Subset of _coupled_var_names of all variables corresponding to gridfunctions with degrees of freedom ...
NamedFieldsMap< std::vector< std::shared_ptr< MFEMComplexEssentialBC > > > _cmplx_essential_bc_map
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...
virtual void BuildEquationSystem() override
Build all forms comprising this EquationSystem.
std::shared_ptr< T > GetShared(const std::string &field_name) const
Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer...
virtual void FormSystemMatrix(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS) override
Form matrix representation of system operator as a HypreParMatrix.
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMComplexKernel > > > > _cmplx_kernels_map
void ApplyDomainLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParComplexLinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMComplexKernel >>>> &kernels_map)
Method for applying LinearFormIntegrators on domains from kernels to a ParComplexLinearForm.
virtual void BuildBilinearForms() override
Build bilinear forms (diagonal Jacobian contributions)
std::vector< std::string > _test_var_names
Names of all test variables corresponding to linear forms in this equation system.
void AddComplexIntegratedBC(std::shared_ptr< MFEMComplexIntegratedBC > bc)
Add complex integrated BCs.
mfem::Array2D< const mfem::HypreParMatrix * > _h_blocks
virtual void SetTrialVariableNames()
Set trial variable names from subset of coupled variables that have an associated test variable...
virtual void AddCoupledVariableNameIfMissing(const std::string &coupled_var_name)
Add coupled variable to EquationSystem.
void DeleteHBlocks()
Deletes the HypreParMatrix associated with any pointer stored in _h_blocks, and then proceeds to dele...
std::vector< std::string > _coupled_var_names
Names of all trial variables of kernels and boundary conditions added to this EquationSystem.
void Register(const std::string &field_name, FieldArgs &&... args)
Construct new field with name field_name and register.
virtual void Init(GridFunctions &gridfunctions, ComplexGridFunctions &cmplx_gridfunctions, mfem::AssemblyLevel assembly_level) override
Initialise.
NamedFieldsMap< mfem::ParSesquilinearForm > _slfs
Moose::MFEM::ComplexGridFunctions * _complex_gfuncs
IntRange< T > make_range(T beg, T end)
void ApplyBoundaryLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParComplexLinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMComplexIntegratedBC >>>> &integrated_bc_map)
Method for applying LinearFormIntegrators on boundaries from kernels to a ParComplexLinearForm.
int size()
Returns the number of elements in the map.
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
mfem::OperatorHandle _linear_operator
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMComplexIntegratedBC > > > > _cmplx_integrated_bc_map
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
auto index_range(const T &sizable)
void AddComplexKernel(std::shared_ptr< MFEMComplexKernel > kernel)
Add complex kernels.