10#ifdef MOOSE_MFEM_ENABLED
15#include "libmesh/int_range.h"
29 for (
const auto i : make_range(
_h_blocks.NumRows()))
30 for (
const auto j : make_range(
_h_blocks.NumCols()))
51 const std::string & name)
const
53 return std::find(the_vector.begin(), the_vector.end(), name) != the_vector.end();
95 const auto & trial_var_name = kernel->getTrialVariableName();
96 const auto & test_var_name = kernel->getTestVariableName();
102 auto kernel_field_map =
103 std::make_shared<Moose::MFEM::NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>>();
104 _kernels_map.Register(test_var_name, std::move(kernel_field_map));
107 if (!
_kernels_map.Get(test_var_name)->Has(trial_var_name))
109 auto kernels = std::make_shared<std::vector<std::shared_ptr<MFEMKernel>>>();
110 _kernels_map.Get(test_var_name)->Register(trial_var_name, std::move(kernels));
112 _kernels_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(kernel));
118 const auto & trial_var_name = bc->getTrialVariableName();
119 const auto & test_var_name = bc->getTestVariableName();
125 auto integrated_bc_field_map = std::make_shared<
132 auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMIntegratedBC>>>();
135 _integrated_bc_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(bc));
145 const auto & trial_var_name = bc->getTrialVariableName();
149 auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMEssentialBC>>>();
158 const auto & trial_var_name = constraint->getTrialVariableName();
162 auto constraints = std::make_shared<std::vector<std::shared_ptr<MFEMEssentialConstraint>>>();
166 for (
const auto & other : constraints)
167 constraint->checkDisjointSubdomains(*other);
168 constraints.push_back(std::move(constraint));
174 mfem::AssemblyLevel assembly_level)
185 if (!gridfunctions.
Has(test_var_name))
189 " requested by equation system during initialization was "
190 "not found in gridfunctions");
198 if (!gridfunctions.
Has(trial_var_name))
202 " requested by equation system during initialization was "
203 "not found in gridfunctions");
207 std::make_unique<mfem::ParGridFunction>(gridfunctions.
Get(trial_var_name)->ParFESpace()));
218 gridfunctions.
GetShared(eliminated_var_name));
226 mfem::ParGridFunction & trial_gf,
227 mfem::Array<int> & global_bdr_markers,
228 mfem::Array<int> & global_ess_tdofs)
231 const auto & bdr_attributes = trial_gf.ParFESpace()->GetParMesh()->bdr_attributes;
232 global_bdr_markers.SetSize(bdr_attributes.Size() ? bdr_attributes.Max() : 0, 0);
233 global_ess_tdofs.SetSize(0);
238 bc->ApplyBC(trial_gf);
240 mfem::Array<int> ess_bdrs(bc->getBoundaryMarkers());
242 for (
const auto i : make_range(ess_bdrs.Size()))
243 global_bdr_markers[i] |= ess_bdrs[i];
246 mfem::Array<int> ess_tdofs;
247 trial_gf.ParFESpace()->GetEssentialTrueDofs(ess_bdrs, ess_tdofs);
248 global_ess_tdofs.Append(ess_tdofs);
256 constraint->ApplyConstraint(trial_gf, global_ess_tdofs);
260 const int num_ess_tdofs = global_ess_tdofs.Size();
261 global_ess_tdofs.Sort();
262 global_ess_tdofs.Unique();
263 if (global_ess_tdofs.Size() != num_ess_tdofs)
264 mooseError(
"Duplicate essential true degrees of freedom for variable '",
266 "'. Check for overlapping essential constraints or boundary conditions.");
296 if (
_mblfs.Has(test_var_name) &&
_mblfs.Get(test_var_name)->Has(eliminated_var_name) &&
299 auto & mblf = *
_mblfs.Get(test_var_name)->Get(eliminated_var_name);
306 mfem::BlockVector & trueX,
307 mfem::BlockVector & trueRHS)
310 "Number of test and trial variables must be the same for block matrix assembly.");
320 mfem::BlockVector & trueX,
321 mfem::BlockVector & trueRHS)
324 "Non-legacy assembly is only supported for single test and trial variable systems");
327 mfem::Vector aux_x, aux_rhs;
328 mfem::OperatorPtr aux_a;
330 auto blf =
_blfs.
Get(test_var_name);
339 trueX.GetBlock(0) = aux_x;
340 trueRHS.GetBlock(0) = aux_rhs;
341 trueX.SyncFromBlocks();
342 trueRHS.SyncFromBlocks();
344 op.Reset(aux_a.Ptr());
345 aux_a.SetOperatorOwner(
false);
350 mfem::BlockVector & trueX,
351 mfem::BlockVector & trueRHS)
359 trueRHS.SyncToBlocks();
369 mfem::Vector aux_x, aux_rhs;
371 mfem::HypreParMatrix * aux_a =
new mfem::HypreParMatrix;
373 if (test_var_name == trial_var_name)
375 mooseAssert(i == j,
"Trial and test variables must have the same ordering.");
376 auto blf =
_blfs.
Get(test_var_name);
384 trueX.GetBlock(j) = aux_x;
386 else if (
_mblfs.Has(test_var_name) &&
_mblfs.Get(test_var_name)->Has(trial_var_name))
388 auto mblf =
_mblfs.Get(test_var_name)->Get(trial_var_name);
400 trueRHS.GetBlock(i) += aux_rhs;
405 trueX.SyncFromBlocks();
406 trueRHS.SyncFromBlocks();
409 op.Reset(mfem::HypreParMatrixFromBlocks(
_h_blocks));
416 height = trueX.Size();
417 width = trueRHS.Size();
439 mooseAssert(
IsNonlinear(),
"Should not be calling this method if our forms are not nonlinear");
441 const mfem::BlockVector block_solution(
const_cast<mfem::Vector &
>(sol),
_block_true_offsets);
449 nlf->AddMult(block_solution.GetBlock(i), block_residual.GetBlock(i));
450 block_residual.GetBlock(i).SyncAliasMemory(block_residual);
467 auto nlf =
_nlfs.
Get(test_var_name);
468 mfem::HypreParMatrix * nlf_jac =
469 dynamic_cast<mfem::HypreParMatrix *
>(&nlf->GetGradient(update_vector.GetBlock(i)));
471 "Jacobian contribution of nonlinear form associated with " + test_var_name +
472 " is not castable into a HypreParMatrix");
493 mooseError(
"MFEM nonlinear solvers that require GetGradient() currently require legacy "
494 "assembly in EquationSystem.");
509 trueX.GetBlock(i).SyncMemory(trueX);
510 _gfuncs->
Get(trial_var_name)->Distribute(&(trueX.GetBlock(i)));
572 ApplyBoundaryBLFIntegrators<mfem::ParBilinearForm>(
574 ApplyDomainBLFIntegrators<mfem::ParBilinearForm>(
594 auto test_mblfs = std::make_shared<Moose::MFEM::NamedFieldsMap<mfem::ParMixedBilinearForm>>();
602 _kernels_map.Get(test_var_name)->Has(coupled_var_name) &&
603 test_var_name != coupled_var_name)
607 ApplyDomainBLFIntegrators<mfem::ParMixedBilinearForm>(
614 test_mblfs->Register(coupled_var_name, mblf);
618 _mblfs.Register(test_var_name, test_mblfs);
633 const std::string & test_var_name,
634 std::shared_ptr<mfem::ParLinearForm> form,
637 if (kernels_map.Has(test_var_name) && kernels_map.Get(test_var_name)->Has(test_var_name))
639 auto kernels = kernels_map.GetRef(test_var_name).GetRef(test_var_name);
640 for (
auto & kernel : kernels)
642 mfem::LinearFormIntegrator * integ = kernel->createLFIntegrator();
646 kernel->isSubdomainRestricted()
647 ? form->AddDomainIntegrator(std::move(integ), kernel->getSubdomainMarkers())
648 : form->AddDomainIntegrator(std::move(integ));
656 const std::string & test_var_name,
657 std::shared_ptr<mfem::ParNonlinearForm> form,
659 std::optional<mfem::real_t> scale_factor)
661 if (kernels_map.Has(test_var_name))
662 for (
const auto & [trial_var_name, kernels] : kernels_map.GetRef(test_var_name))
663 for (
auto & kernel : *kernels)
664 if (
auto * integ = kernel->createNLIntegrator())
667 mooseError(
"Support for off-diagonal MFEM nonlinear domain integrators in conjunction "
668 "with a nonlinear solver that requires a gradient is not currently "
669 "implemented. Kernel '",
671 "' contributes to test variable '",
673 "' from trial variable '",
678 if (scale_factor.has_value())
680 kernel->isSubdomainRestricted()
681 ? form->AddDomainIntegrator(std::move(integ), kernel->getSubdomainMarkers())
682 : form->AddDomainIntegrator(std::move(integ));
688 const std::string & test_var_name,
689 std::shared_ptr<mfem::ParLinearForm> form,
693 if (integrated_bc_map.Has(test_var_name) &&
694 integrated_bc_map.Get(test_var_name)->Has(test_var_name))
696 auto bcs = integrated_bc_map.GetRef(test_var_name).GetRef(test_var_name);
697 for (
auto & bc : bcs)
699 mfem::LinearFormIntegrator * integ = bc->createLFIntegrator();
703 bc->isDGBC() ? bc->isBoundaryRestricted()
704 ? form->AddBdrFaceIntegrator(std::move(integ), bc->getBoundaryMarkers())
705 : form->AddBdrFaceIntegrator(std::move(integ))
706 : bc->isBoundaryRestricted()
707 ? form->AddBoundaryIntegrator(std::move(integ), bc->getBoundaryMarkers())
708 : form->AddBoundaryIntegrator(std::move(integ));
716 const std::string & test_var_name,
717 std::shared_ptr<mfem::ParNonlinearForm> form,
720 std::optional<mfem::real_t> scale_factor)
722 if (integrated_bc_map.Has(test_var_name))
723 for (
const auto & [trial_var_name, bcs] : integrated_bc_map.GetRef(test_var_name))
724 for (
auto & bc : *bcs)
725 if (
auto * integ = bc->createNLIntegrator())
729 "Support for Off-diagonal MFEM nonlinear boundary integrators in conjunction with "
730 "a nonlinear solver that requires a gradient is not currently "
731 "implemented. Boundary condition '",
733 "' contributes to test variable '",
735 "' from trial variable '",
740 if (scale_factor.has_value())
742 bc->isBoundaryRestricted()
743 ? form->AddBoundaryIntegrator(std::move(integ), bc->getBoundaryMarkers())
744 : form->AddBoundaryIntegrator(std::move(integ));
752 mooseError(
"EquationSystem::GetLinearizationPoint() called before GetGradient().");
756std::shared_ptr<mfem::ParBilinearForm>
758 mfem::ParFiniteElementSpace & fespace,
759 mfem::AssemblyLevel assembly_level)
761 auto blf = std::make_shared<mfem::ParBilinearForm>(&fespace);
762 blf->SetAssemblyLevel(assembly_level);
763 ApplyBoundaryBLFIntegrators<mfem::ParBilinearForm>(var_name, var_name, blf,
_integrated_bc_map);
764 ApplyDomainBLFIntegrators<mfem::ParBilinearForm>(var_name, var_name, blf,
_kernels_map);
770std::shared_ptr<mfem::ParNonlinearForm>
772 mfem::ParFiniteElementSpace & fespace,
773 mfem::AssemblyLevel )
775 auto nlf = std::make_shared<mfem::ParNonlinearForm>(&fespace);
788 mooseError(
"No essential boundary markers found for variable '", var_name,
"'.");
791const mfem::Array<int> &
798 mooseError(
"No essential true dofs found for variable '", var_name,
"'.");
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
void markSolutionChanged()
Notify quadrature function coefficients that solution variables have changed, marking the stored valu...
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.
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...
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
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)
mfem::Array2D< const mfem::HypreParMatrix * > _h_blocks
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.
~EquationSystem() override
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::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,...
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.
std::vector< std::string > _eliminated_var_names
Names of all coupled variables without a corresponding test variable.
Moose::MFEM::GridFunctions * _gfuncs
virtual void BuildLinearForms()
Build linear forms and eliminate constrained DoFs.
virtual void AddCoupledVariableNameIfMissing(const std::string &coupled_var_name)
Add coupled variable to EquationSystem.
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > _integrated_bc_map
Arrays to store integrated BCs to act on each component of weak form.
NonlinearFormIntegrator which scales its results by a constant value.
Lightweight adaptor over an std::map from strings to pointer to T.
void Register(const std::string &field_name, FieldArgs &&... args)
Construct new field with name field_name and register.
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
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.
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.
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).