https://mooseframework.inl.gov
Loading...
Searching...
No Matches
EquationSystem.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_MFEM_ENABLED
11
12#pragma once
13
14#include "libmesh/ignore_warnings.h"
15#include "mfem/miniapps/common/pfem_extras.hpp"
16#include "libmesh/restore_warnings.h"
17#include "MFEMIntegratedBC.h"
18#include "MFEMEssentialBC.h"
20#include "MFEMContainers.h"
21#include "MFEMKernel.h"
23#include "ScaleIntegrator.h"
24#include "NLScaleIntegrator.h"
25
26namespace Moose::MFEM
27{
28class CoefficientManager;
29
45class EquationSystem : public mfem::Operator
46{
47
48public:
49 EquationSystem() = default;
50 ~EquationSystem() override;
51
53 virtual void AddKernel(std::shared_ptr<MFEMKernel> kernel);
54 virtual void AddIntegratedBC(std::shared_ptr<MFEMIntegratedBC> kernel);
56 virtual void AddEssentialBC(std::shared_ptr<MFEMEssentialBC> bc);
58 virtual void AddEssentialConstraint(std::shared_ptr<MFEMEssentialConstraint> constraint);
59
61 virtual void Init(GridFunctions & gridfunctions,
62 ComplexGridFunctions & cmplx_gridfunctions,
63 mfem::AssemblyLevel assembly_level);
75 void FormSystem(mfem::BlockVector & trueX, mfem::BlockVector & trueRHS);
77 void Mult(const mfem::Vector & u, mfem::Vector & residual) const override;
79 virtual void ComputeNonlinearResidual(const mfem::Vector & u, mfem::Vector & residual) const;
81 mfem::Operator & GetGradient(const mfem::Vector & u) const override;
83 mfem::OperatorHandle & GetLinearOperator() const { return _linear_operator; };
84
86 virtual void SetTrialVariablesFromTrueVectors(const mfem::BlockVector & trueX) const;
87
89 void SetGradientRequired(bool requires_gradient) { _gradient_required = requires_gradient; }
90
94 {
95 _coefficient_manager = &coefficients;
96 }
97
98 // Test variables are associated with linear forms,
99 // whereas trial variables are associated with gridfunctions.
100 const std::vector<std::string> & GetTrialVarNames() const { return _trial_var_names; }
101 const std::vector<std::string> & GetTestVarNames() const { return _test_var_names; }
102
104 const mfem::Array<int> & GetBlockOffsets() const { return _block_true_offsets; }
105
109 mfem::ParBilinearForm & GetBilinearForm(const std::string & test_var_name)
110 {
111 return _blfs.GetRef(test_var_name);
112 }
113
117 mfem::ParGridFunction & GetGridFunction(const std::string & trial_var_name)
118 {
119 return _gfuncs->GetRef(trial_var_name);
120 }
121
123 const mfem::Vector & GetLinearizationPoint() const;
124
129 std::shared_ptr<mfem::ParBilinearForm>
130 BuildBilinearFormForFESpace(const std::string & var_name,
131 mfem::ParFiniteElementSpace & fespace,
132 mfem::AssemblyLevel assembly_level);
133
138 std::shared_ptr<mfem::ParNonlinearForm>
139 BuildNonlinearFormForFESpace(const std::string & var_name,
140 mfem::ParFiniteElementSpace & fespace,
141 mfem::AssemblyLevel assembly_level);
142
147 mfem::Array<int> & GetEssentialBoundaryMarkers(const std::string & var_name);
148
153 const mfem::Array<int> & GetEssentialTrueDofs(const std::string & var_name) const;
154
156 virtual bool HasEssentialConstraints(const std::string & var_name) const;
157
159 virtual bool IsComplex() const { return false; }
161 virtual bool IsEigen() const { return false; }
163 virtual bool IsTimeDependent() const { return false; }
165 bool IsMultivariate() const { return _test_var_names.size() > 1; }
167 bool IsNonlinear() const { return _non_linear; }
168
170 virtual void BuildEquationSystem();
171
172protected:
174 virtual void AddCoupledVariableNameIfMissing(const std::string & coupled_var_name);
176 virtual void AddEliminatedVariableNameIfMissing(const std::string & eliminated_var_name);
178 virtual void AddTestVariableNameIfMissing(const std::string & test_var_name);
180 virtual void SetTrialVariableNames();
181
187 template <typename T>
189 const Moose::MFEM::NamedFieldsMap<std::vector<std::shared_ptr<T>>> & map) const;
190
194 void DeleteHBlocks();
195
200
201 bool VectorContainsName(const std::vector<std::string> & the_vector,
202 const std::string & name) const;
203
206 virtual void ApplyEssentialConstraint(const std::string & var_name,
207 mfem::ParGridFunction & trial_gf,
208 mfem::Array<int> & global_bdr_markers,
209 mfem::Array<int> & global_ess_tdofs);
211 virtual void ApplyEssentialConstraints();
213 virtual void EliminateCoupledVariables();
215 virtual void BuildLinearForms();
217 virtual void BuildNonlinearForms();
219 virtual void BuildBilinearForms();
221 virtual void BuildMixedBilinearForms();
222
225 virtual void FormLinearSystem(mfem::OperatorHandle & op,
226 mfem::BlockVector & trueX,
227 mfem::BlockVector & trueRHS);
228 using mfem::Operator::FormSystemOperator;
231 virtual void FormSystemOperator(mfem::OperatorHandle & op,
232 mfem::BlockVector & trueX,
233 mfem::BlockVector & trueRHS);
236 virtual void FormSystemMatrix(mfem::OperatorHandle & op,
237 mfem::BlockVector & trueX,
238 mfem::BlockVector & trueRHS);
240 void FormJacobianMatrix(const mfem::Vector & u);
241
246 template <class FormType>
248 const std::string & trial_var_name,
249 const std::string & test_var_name,
250 std::shared_ptr<FormType> form,
251 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>> & kernels_map,
252 std::optional<mfem::real_t> scale_factor = std::nullopt);
253
259 const std::string & test_var_name,
260 std::shared_ptr<mfem::ParLinearForm> form,
261 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>> & kernels_map);
262
268 const std::string & test_var_name,
269 std::shared_ptr<mfem::ParNonlinearForm> form,
270 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>> & kernels_map,
271 std::optional<mfem::real_t> scale_factor = std::nullopt);
272
277 template <class FormType>
279 const std::string & trial_var_name,
280 const std::string & test_var_name,
281 std::shared_ptr<FormType> form,
282 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMIntegratedBC>>>> &
283 integrated_bc_map,
284 std::optional<mfem::real_t> scale_factor = std::nullopt);
285
291 const std::string & test_var_name,
292 std::shared_ptr<mfem::ParLinearForm> form,
293 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMIntegratedBC>>>> &
294 integrated_bc_map);
295
301 const std::string & test_var_name,
302 std::shared_ptr<mfem::ParNonlinearForm> form,
303 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMIntegratedBC>>>> &
304 integrated_bc_map,
305 std::optional<mfem::real_t> scale_factor = std::nullopt);
306
309 std::vector<std::string> _coupled_var_names;
313 std::vector<std::string> _trial_var_names;
315 std::vector<std::string> _eliminated_var_names;
319 std::vector<std::string> _test_var_names;
321 std::vector<mfem::ParFiniteElementSpace *> _test_pfespaces;
323 std::vector<mfem::ParFiniteElementSpace *> _coupled_pfespaces;
324
325 // Components of weak form, named according to test variable
330
332 std::vector<std::unique_ptr<mfem::ParGridFunction>> _var_ess_constraints;
333 std::vector<mfem::Array<int>> _ess_tdof_lists;
334 std::vector<mfem::Array<int>> _ess_bdr_markers;
335
336 mfem::Array2D<const mfem::HypreParMatrix *> _h_blocks, _jacobian_blocks;
349
350 // Operator handle for the jacobian
351 mutable mfem::OperatorHandle _jacobian;
352 // Operator handle for the linear components of the system operator
353 mutable mfem::OperatorHandle _linear_operator;
354 mfem::AssemblyLevel _assembly_level;
355
356 // Pointer to GridFunctions to enable updates during nonlinear iterations
358 // Array storing block offsets of solution and residual vector
359 mfem::Array<int> _block_true_offsets;
360 // Non-owning pointer to the vector passed into the most recent GetGradient() call.
361 mutable const mfem::Vector * _linearization_point = nullptr;
362 // Boolean indicating if EquationSystem contains nonlinear integrators
363 bool _non_linear = false;
364 // Whether an external object (e.g. solver) requires Jacobian/gradient information.
365 bool _gradient_required = false;
366 // Coefficient manager notified when trial variables are updated, so that stored projections
367 // of solution-dependent coefficients are invalidated.
369
370private:
372 using mfem::Operator::RecoverFEMSolution;
373};
374
375template <typename T>
376void
378 const Moose::MFEM::NamedFieldsMap<std::vector<std::shared_ptr<T>>> & map) const
379{
380 for (const auto & [var_name, objects] : map)
381 if (!VectorContainsName(_trial_var_names, var_name))
382 mooseError("MFEM variable '",
383 var_name,
384 "' has an object of type '",
385 MooseUtils::prettyCppType(objects->front().get()),
386 "' applied to it, but it is not a trial variable of the equation system. "
387 "Add a kernel acting on '",
388 var_name,
389 "', or remove it.");
390}
391
392template <class FormType>
393void
395 const std::string & trial_var_name,
396 const std::string & test_var_name,
397 std::shared_ptr<FormType> form,
398 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>> & kernels_map,
399 std::optional<mfem::real_t> scale_factor)
400{
401 if (kernels_map.Has(test_var_name) && kernels_map.Get(test_var_name)->Has(trial_var_name))
402 {
403 auto kernels = kernels_map.GetRef(test_var_name).GetRef(trial_var_name);
404 for (auto & kernel : kernels)
405 {
406 mfem::BilinearFormIntegrator * integ = kernel->createBFIntegrator();
407
408 if (integ)
409 {
410 if (scale_factor.has_value())
411 integ = new ScaleIntegrator(integ, scale_factor.value(), true);
412 kernel->isDGKernel() ? form->AddInteriorFaceIntegrator(std::move(integ))
413 : kernel->isSubdomainRestricted()
414 ? form->AddDomainIntegrator(std::move(integ), kernel->getSubdomainMarkers())
415 : form->AddDomainIntegrator(std::move(integ));
416 }
417 }
418 }
419}
420
421template <class FormType>
422void
424 const std::string & trial_var_name,
425 const std::string & test_var_name,
426 std::shared_ptr<FormType> form,
427 NamedFieldsMap<NamedFieldsMap<std::vector<std::shared_ptr<MFEMIntegratedBC>>>> &
428 integrated_bc_map,
429 std::optional<mfem::real_t> scale_factor)
430{
431 if (integrated_bc_map.Has(test_var_name) &&
432 integrated_bc_map.Get(test_var_name)->Has(trial_var_name))
433 {
434 auto bcs = integrated_bc_map.GetRef(test_var_name).GetRef(trial_var_name);
435 for (auto & bc : bcs)
436 {
437 mfem::BilinearFormIntegrator * integ = bc->createBFIntegrator();
438
439 if (integ)
440 {
441 if (scale_factor.has_value())
442 integ = new ScaleIntegrator(integ, scale_factor.value(), true);
443 bc->isDGBC() ? bc->isBoundaryRestricted()
444 ? form->AddBdrFaceIntegrator(std::move(integ), bc->getBoundaryMarkers())
445 : form->AddBdrFaceIntegrator(std::move(integ))
446 : bc->isBoundaryRestricted()
447 ? form->AddBoundaryIntegrator(std::move(integ), bc->getBoundaryMarkers())
448 : form->AddBoundaryIntegrator(std::move(integ));
449 }
450 }
451 }
452}
453
454} // namespace Moose::MFEM
455
456#endif
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Front-end class for creating and storing MFEM coefficients.
Owns the weak-form mathematics of a MOOSE MFEM problem.
void CheckConstrainedVariablesAreTrialVariables(const Moose::MFEM::NamedFieldsMap< std::vector< std::shared_ptr< T > > > &map) const
Error if any variable that map holds strongly constrained DoFs for is not a trial variable of this sy...
virtual void AddTestVariableNameIfMissing(const std::string &test_var_name)
Add test variable to EquationSystem.
void ApplyDomainBLFIntegrators(const std::string &trial_var_name, const std::string &test_var_name, std::shared_ptr< FormType > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMKernel > > > > &kernels_map, std::optional< mfem::real_t > scale_factor=std::nullopt)
Template method for applying BilinearFormIntegrators on domains from kernels to a BilinearForm,...
const std::vector< std::string > & GetTrialVarNames() const
const std::vector< std::string > & GetTestVarNames() const
virtual bool HasEssentialConstraints(const std::string &var_name) const
std::shared_ptr< mfem::ParBilinearForm > BuildBilinearFormForFESpace(const std::string &var_name, mfem::ParFiniteElementSpace &fespace, mfem::AssemblyLevel assembly_level)
Build a fresh ParBilinearForm on the given FESpace using the same kernels as the main system's biline...
NamedFieldsMap< std::vector< std::shared_ptr< MFEMEssentialConstraint > > > _essential_constraint_map
Arrays to store essential domain constraints to act on each component of weak form.
virtual void BuildBilinearForms()
Build bilinear forms (diagonal Jacobian contributions)
virtual void BuildNonlinearForms()
Build non-linear action forms.
std::vector< std::string > _coupled_var_names
Names of all trial variables of kernels and boundary conditions added to this EquationSystem.
std::vector< mfem::ParFiniteElementSpace * > _test_pfespaces
Pointers to finite element spaces associated with test variables.
mfem::Array< int > _block_true_offsets
void FormSystem(mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Build all weak-form components via BuildEquationSystem(), form the constrained linear part of the sys...
void SetCoefficientManager(CoefficientManager &coefficients)
Set the coefficient manager to notify when trial variables are updated, so that stored projections of...
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMKernel > > > > _kernels_map
Arrays to store kernels to act on each component of weak form.
std::vector< mfem::Array< int > > _ess_bdr_markers
std::vector< mfem::ParFiniteElementSpace * > _coupled_pfespaces
Pointers to finite element spaces associated with coupled variables.
bool VectorContainsName(const std::vector< std::string > &the_vector, const std::string &name) const
const mfem::Array< int > & GetBlockOffsets() const
Getter for block true offsets associated with the EquationSystem operator.
void ApplyDomainNLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParNonlinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMKernel > > > > &kernels_map, std::optional< mfem::real_t > scale_factor=std::nullopt)
Apply domain NonlinearFormIntegrators from kernels to the nonlinear form associated with the supplied...
virtual void EliminateCoupledVariables()
Perform trivial eliminations of coupled variables lacking corresponding test variables.
virtual void ApplyEssentialConstraints()
Update essential boundary markers and all constrained true DoF lists and values.
virtual void ApplyEssentialConstraint(const std::string &var_name, mfem::ParGridFunction &trial_gf, mfem::Array< int > &global_bdr_markers, mfem::Array< int > &global_ess_tdofs)
Apply essential constraints associated with var_name to set true DoFs of trial_gf,...
virtual void BuildEquationSystem()
Build all forms comprising this EquationSystem.
mfem::Operator & GetGradient(const mfem::Vector &u) const override
Get Jacobian at the provided vector of true DoFs of trial variables.
virtual void BuildMixedBilinearForms()
Build mixed bilinear forms (off-diagonal Jacobian contributions)
void ApplyBoundaryBLFIntegrators(const std::string &trial_var_name, const std::string &test_var_name, std::shared_ptr< FormType > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > &integrated_bc_map, std::optional< mfem::real_t > scale_factor=std::nullopt)
Template method for applying BilinearFormIntegrators on boundaries from integrated boundary condition...
mfem::Array2D< const mfem::HypreParMatrix * > _h_blocks
mfem::OperatorHandle & GetLinearOperator() const
Get operator handle for linear component of system operator.
void ApplyBoundaryLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParLinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > &integrated_bc_map)
Apply boundary LinearFormIntegrators from integrated boundary conditions to the linear form associate...
std::vector< std::string > _test_var_names
Names of all test variables corresponding to linear forms in this equation system.
void FormJacobianMatrix(const mfem::Vector &u)
Compute Jacobian matrix at the provided vector of true DoFs of trial variables.
virtual void FormSystemOperator(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Form matrix-free representation of linear components of system operator.
mfem::Array< int > & GetEssentialBoundaryMarkers(const std::string &var_name)
Return the essential boundary attribute marker array for a given trial variable.
void ApplyBoundaryNLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParNonlinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > &integrated_bc_map, std::optional< mfem::real_t > scale_factor=std::nullopt)
Apply boundary NonlinearFormIntegrators from integrated boundary conditions to the nonlinear form ass...
mfem::OperatorHandle _linear_operator
const mfem::Vector * _linearization_point
std::shared_ptr< mfem::ParNonlinearForm > BuildNonlinearFormForFESpace(const std::string &var_name, mfem::ParFiniteElementSpace &fespace, mfem::AssemblyLevel assembly_level)
Build a fresh ParNonlinearForm on the given FESpace using the same kernels as the main system's nonli...
NamedFieldsMap< mfem::ParLinearForm > _lfs
virtual void AddIntegratedBC(std::shared_ptr< MFEMIntegratedBC > kernel)
mfem::AssemblyLevel _assembly_level
virtual void AddKernel(std::shared_ptr< MFEMKernel > kernel)
Add kernels.
virtual void SetTrialVariableNames()
Set trial variable names from subset of coupled variables that have an associated test variable.
mfem::ParGridFunction & GetGridFunction(const std::string &trial_var_name)
mfem::Array2D< const mfem::HypreParMatrix * > _jacobian_blocks
virtual void FormSystemMatrix(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Form matrix representation of linear components of system operator as a HypreParMatrix.
std::vector< mfem::Array< int > > _ess_tdof_lists
CoefficientManager * _coefficient_manager
const mfem::Array< int > & GetEssentialTrueDofs(const std::string &var_name) const
Return the essentially constrained true dofs of a given trial variable: those on essential boundaries...
virtual void SetTrialVariablesFromTrueVectors(const mfem::BlockVector &trueX) const
Update variable from solution vector after solve.
virtual void AddEssentialBC(std::shared_ptr< MFEMEssentialBC > bc)
Add BC associated with essentially constrained DoFs on boundaries.
NamedFieldsMap< mfem::ParBilinearForm > _blfs
virtual void AddEssentialConstraint(std::shared_ptr< MFEMEssentialConstraint > constraint)
Add constraint associated with essentially constrained DoFs on domains.
void DeleteHBlocks()
Deletes the HypreParMatrix associated with any pointer stored in _h_blocks, and then proceeds to dele...
virtual void ComputeNonlinearResidual(const mfem::Vector &u, mfem::Vector &residual) const
Compute the contribution to the residual from nonlinear forms only.
mfem::OperatorHandle _jacobian
virtual void FormLinearSystem(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Form linear components of system based on on- and off-diagonal bilinear form contributions,...
mfem::ParBilinearForm & GetBilinearForm(const std::string &test_var_name)
NamedFieldsMap< std::vector< std::shared_ptr< MFEMEssentialBC > > > _essential_bc_map
Arrays to store essential BCs to act on each component of weak form.
virtual void Init(GridFunctions &gridfunctions, ComplexGridFunctions &cmplx_gridfunctions, mfem::AssemblyLevel assembly_level)
Initialise.
std::vector< std::string > _trial_var_names
Subset of _coupled_var_names of all variables corresponding to gridfunctions with degrees of freedom ...
NamedFieldsMap< NamedFieldsMap< mfem::ParMixedBilinearForm > > _mblfs
std::vector< std::unique_ptr< mfem::ParGridFunction > > _var_ess_constraints
Gridfunctions holding essential constraints from Dirichlet BCs.
void DeleteJacobianBlocks()
Deletes the HypreParMatrix associated with any pointer stored in _jacobian_blocks,...
void ApplyDomainLFIntegrators(const std::string &test_var_name, std::shared_ptr< mfem::ParLinearForm > form, NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMKernel > > > > &kernels_map)
Apply domain LinearFormIntegrators from kernels to the linear form associated with the supplied test ...
virtual void AddEliminatedVariableNameIfMissing(const std::string &eliminated_var_name)
Add eliminated variable to EquationSystem.
void Mult(const mfem::Vector &u, mfem::Vector &residual) const override
Compute residual y = Mu.
NamedFieldsMap< mfem::ParNonlinearForm > _nlfs
Moose::MFEM::GridFunctions _eliminated_variables
Pointers to coupled variables not part of the reduced EquationSystem.
const mfem::Vector & GetLinearizationPoint() const
The true-DoF vector used for the most recent Jacobian linearization.
void SetGradientRequired(bool requires_gradient)
Set whether an external object (such as a nonlinear solver) requires Jacobian information for this Eq...
std::vector< std::string > _eliminated_var_names
Names of all coupled variables without a corresponding test variable.
virtual bool IsComplex() const
Moose::MFEM::GridFunctions * _gfuncs
virtual bool IsEigen() const
virtual void BuildLinearForms()
Build linear forms and eliminate constrained DoFs.
virtual void AddCoupledVariableNameIfMissing(const std::string &coupled_var_name)
Add coupled variable to EquationSystem.
virtual bool IsTimeDependent() const
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > _integrated_bc_map
Arrays to store integrated BCs to act on each component of weak form.
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
Integrator which scales its results by a constant value.
std::string prettyCppType(const std::string &cpp_type)
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).