10 #ifdef MOOSE_MFEM_ENABLED 14 #include "libmesh/int_range.h" 50 const std::string & name)
const 52 return std::find(the_vector.begin(), the_vector.end(),
name) != the_vector.end();
94 const auto & trial_var_name = kernel->getTrialVariableName();
95 const auto & test_var_name = kernel->getTestVariableName();
101 auto kernel_field_map =
102 std::make_shared<Moose::MFEM::NamedFieldsMap<std::vector<std::shared_ptr<MFEMKernel>>>>();
103 _kernels_map.Register(test_var_name, std::move(kernel_field_map));
106 if (!
_kernels_map.Get(test_var_name)->Has(trial_var_name))
108 auto kernels = std::make_shared<std::vector<std::shared_ptr<MFEMKernel>>>();
109 _kernels_map.Get(test_var_name)->Register(trial_var_name, std::move(kernels));
111 _kernels_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(kernel));
117 const auto & trial_var_name = bc->getTrialVariableName();
118 const auto & test_var_name = bc->getTestVariableName();
124 auto integrated_bc_field_map = std::make_shared<
131 auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMIntegratedBC>>>();
134 _integrated_bc_map.GetRef(test_var_name).Get(trial_var_name)->push_back(std::move(bc));
140 const auto & test_var_name = bc->getTestVariableName();
145 auto bcs = std::make_shared<std::vector<std::shared_ptr<MFEMEssentialBC>>>();
154 mfem::AssemblyLevel assembly_level)
158 if (cmplx_gridfunctions.
size())
159 mooseError(
"Complex variables have been created but the executioner numeric type has not been " 160 "set to complex. Please set Executioner/numeric_type = complex.");
167 if (!gridfunctions.
Has(test_var_name))
171 " requested by equation system during initialization was " 172 "not found in gridfunctions");
180 if (!gridfunctions.
Has(trial_var_name))
184 " requested by equation system during initialization was " 185 "not found in gridfunctions");
189 std::make_unique<mfem::ParGridFunction>(gridfunctions.
Get(trial_var_name)->ParFESpace()));
200 gridfunctions.
GetShared(eliminated_var_name));
208 mfem::ParGridFunction & trial_gf,
209 mfem::Array<int> & global_ess_markers)
214 for (
auto & bc : bcs)
217 bc->ApplyBC(trial_gf);
219 mfem::Array<int> ess_bdrs(bc->getBoundaryMarkers());
221 for (
const auto i :
make_range(trial_gf.ParFESpace()->GetParMesh()->bdr_attributes.Max()))
222 global_ess_markers[i] =
std::max(global_ess_markers[i], ess_bdrs[i]);
242 mfem::Array<int> global_ess_markers(trial_gf.ParFESpace()->GetParMesh()->bdr_attributes.Max());
243 global_ess_markers = 0;
247 trial_gf.ParFESpace()->GetEssentialTrueDofs(global_ess_markers,
_ess_tdof_lists.at(i));
256 if (
_mblfs.Has(test_var_name) &&
_mblfs.Get(test_var_name)->Has(eliminated_var_name) &&
259 auto & mblf = *
_mblfs.Get(test_var_name)->Get(eliminated_var_name);
266 mfem::BlockVector & trueX,
267 mfem::BlockVector & trueRHS)
270 "Number of test and trial variables must be the same for block matrix assembly.");
277 "Non-legacy assembly is only supported for single test and trial variable systems");
284 mfem::BlockVector & trueX,
285 mfem::BlockVector & trueRHS)
288 mfem::Vector aux_x, aux_rhs;
289 mfem::OperatorPtr aux_a;
291 auto blf =
_blfs.
Get(test_var_name);
300 trueX.GetBlock(0) = aux_x;
301 trueRHS.GetBlock(0) = aux_rhs;
302 trueX.SyncFromBlocks();
303 trueRHS.SyncFromBlocks();
305 op.Reset(aux_a.Ptr());
306 aux_a.SetOperatorOwner(
false);
311 mfem::BlockVector & trueX,
312 mfem::BlockVector & trueRHS)
320 trueRHS.SyncToBlocks();
330 mfem::Vector aux_x, aux_rhs;
332 mfem::HypreParMatrix * aux_a =
new mfem::HypreParMatrix;
334 if (test_var_name == trial_var_name)
336 mooseAssert(i == j,
"Trial and test variables must have the same ordering.");
337 auto blf =
_blfs.
Get(test_var_name);
345 trueX.GetBlock(j) = aux_x;
347 else if (
_mblfs.Has(test_var_name) &&
_mblfs.Get(test_var_name)->Has(trial_var_name))
349 auto mblf =
_mblfs.Get(test_var_name)->Get(trial_var_name);
361 trueRHS.GetBlock(i) += aux_rhs;
366 trueX.SyncFromBlocks();
367 trueRHS.SyncFromBlocks();
370 op.Reset(mfem::HypreParMatrixFromBlocks(
_h_blocks));
376 height = trueX.Size();
377 width = trueRHS.Size();
408 mooseAssert(
_non_linear,
"Should not be calling this method if our forms are not nonlinear");
411 const mfem::BlockVector block_solution(const_cast<mfem::Vector &>(sol),
_block_true_offsets);
419 nlf->Mult(block_solution.GetBlock(i), block_residual.GetBlock(i));
420 block_residual.GetBlock(i).SyncAliasMemory(block_residual);
437 auto nlf =
_nlfs.
Get(test_var_name);
438 mfem::HypreParMatrix * nlf_jac =
439 dynamic_cast<mfem::HypreParMatrix *
>(&nlf->GetGradient(update_vector.GetBlock(i)));
441 "Jacobian contribution of nonlinear form associated with " + test_var_name +
442 " is not castable into a HypreParMatrix");
461 mooseError(
"MFEM nonlinear solvers that require GetGradient() currently require legacy " 462 "assembly in EquationSystem.");
477 trueX.GetBlock(i).SyncMemory(trueX);
478 _gfuncs->
Get(trial_var_name)->Distribute(&(trueX.GetBlock(i)));
537 ApplyBoundaryBLFIntegrators<mfem::ParBilinearForm>(
539 ApplyDomainBLFIntegrators<mfem::ParBilinearForm>(
558 auto test_mblfs = std::make_shared<Moose::MFEM::NamedFieldsMap<mfem::ParMixedBilinearForm>>();
566 _kernels_map.Get(test_var_name)->Has(coupled_var_name) &&
567 test_var_name != coupled_var_name)
571 ApplyDomainBLFIntegrators<mfem::ParMixedBilinearForm>(
577 test_mblfs->Register(coupled_var_name, mblf);
581 _mblfs.Register(test_var_name, test_mblfs);
596 const std::string & test_var_name,
597 std::shared_ptr<mfem::ParLinearForm> form,
600 if (kernels_map.Has(test_var_name) && kernels_map.Get(test_var_name)->Has(test_var_name))
602 auto kernels = kernels_map.GetRef(test_var_name).GetRef(test_var_name);
603 for (
auto & kernel : kernels)
605 mfem::LinearFormIntegrator * integ = kernel->createLFIntegrator();
609 kernel->isSubdomainRestricted()
610 ? form->AddDomainIntegrator(std::move(integ), kernel->getSubdomainMarkers())
611 : form->AddDomainIntegrator(std::move(integ));
619 const std::string & test_var_name,
620 std::shared_ptr<mfem::ParNonlinearForm> form,
622 std::optional<mfem::real_t> scale_factor)
624 if (kernels_map.Has(test_var_name))
625 for (
const auto & [trial_var_name, kernels] : kernels_map.GetRef(test_var_name))
626 for (
auto & kernel : *kernels)
627 if (
auto * integ = kernel->createNLIntegrator())
630 mooseError(
"Support for off-diagonal MFEM nonlinear domain integrators in conjunction " 631 "with a nonlinear solver that requires a gradient is not currently " 632 "implemented. Kernel '",
634 "' contributes to test variable '",
636 "' from trial variable '",
641 if (scale_factor.has_value())
643 kernel->isSubdomainRestricted()
644 ? form->AddDomainIntegrator(std::move(integ), kernel->getSubdomainMarkers())
645 : form->AddDomainIntegrator(std::move(integ));
651 const std::string & test_var_name,
652 std::shared_ptr<mfem::ParLinearForm> form,
656 if (integrated_bc_map.Has(test_var_name) &&
657 integrated_bc_map.Get(test_var_name)->Has(test_var_name))
659 auto bcs = integrated_bc_map.GetRef(test_var_name).GetRef(test_var_name);
660 for (
auto & bc : bcs)
662 mfem::LinearFormIntegrator * integ = bc->createLFIntegrator();
666 bc->isBoundaryRestricted()
667 ? form->AddBoundaryIntegrator(std::move(integ), bc->getBoundaryMarkers())
668 : form->AddBoundaryIntegrator(std::move(integ));
676 const std::string & test_var_name,
677 std::shared_ptr<mfem::ParNonlinearForm> form,
680 std::optional<mfem::real_t> scale_factor)
682 if (integrated_bc_map.Has(test_var_name))
683 for (
const auto & [trial_var_name, bcs] : integrated_bc_map.GetRef(test_var_name))
684 for (
auto & bc : *bcs)
685 if (
auto * integ = bc->createNLIntegrator())
689 "Support for Off-diagonal MFEM nonlinear boundary integrators in conjunction with " 690 "a nonlinear solver that requires a gradient is not currently " 691 "implemented. Boundary condition '",
693 "' contributes to test variable '",
695 "' from trial variable '",
700 if (scale_factor.has_value())
702 bc->isBoundaryRestricted()
703 ? form->AddBoundaryIntegrator(std::move(integ), bc->getBoundaryMarkers())
704 : form->AddBoundaryIntegrator(std::move(integ));
714 mooseError(
"LOR solve is not supported for complex equation systems.");
716 mooseError(
"LOR solve is only supported for single-variable systems");
721 "If we are preparing a linear solver, we better have a linear operator");
std::string name(const ElemQuality q)
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...
NamedFieldsMap< mfem::ParBilinearForm > _blfs
virtual void EliminateCoupledVariables()
Perform trivial eliminations of coupled variables lacking corresponding test variables.
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
NamedFieldsMap< NamedFieldsMap< mfem::ParMixedBilinearForm > > _mblfs
virtual void AddTestVariableNameIfMissing(const std::string &test_var_name)
Add test variable to EquationSystem.
virtual void AddKernel(std::shared_ptr< MFEMKernel > kernel)
Add kernels.
virtual void BuildBilinearForms()
Build bilinear forms (diagonal Jacobian contributions)
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.
void PrepareLinearSolver(LinearSolverBase &solver)
Prepare the provided linear solver.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
virtual void SetTrialVariablesFromTrueVectors(const mfem::BlockVector &trueX) const
Update variable from solution vector after solve.
void DeleteJacobianBlocks()
Deletes the HypreParMatrix associated with any pointer stored in _jacobian_blocks, and then proceeds to delete all dynamically allocated memory for _jacobian_blocks itself, resetting all dimensions to zero.
bool VectorContainsName(const std::vector< std::string > &the_vector, const std::string &name) const
bool IsLOR() const
Returns whether or not this solver (or its preconditioner) uses LOR.
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMIntegratedBC > > > > _integrated_bc_map
Arrays to store integrated BCs to act on each component of weak form.
std::vector< std::string > _eliminated_var_names
Names of all coupled variables without a corresponding test variable.
virtual void SetOperator(mfem::OperatorHandle &op)
Updates the solver at the operator level.
void FormJacobianMatrix(const mfem::Vector &u)
Compute Jacobian matrix at the provided vector of true DoFs of trial variables.
~EquationSystem() override
NamedFieldsMap< mfem::ParNonlinearForm > _nlfs
Class to store weak form components (bilinear and linear forms, and optionally mixed and nonlinear fo...
Moose::MFEM::GridFunctions _eliminated_variables
Pointers to coupled variables not part of the reduced EquationSystem.
virtual void AddEssentialBC(std::shared_ptr< MFEMEssentialBC > bc)
Add BC associated with essentially constrained DoFs on boundaries.
NonlinearFormIntegrator which scales its results by a constant value.
std::vector< mfem::Array< int > > _ess_tdof_lists
Lightweight adaptor over an std::map from strings to pointer to T.
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 FormSystem(mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Assemble the linear part of the operator, assemble the right-hand side, apply essential and eliminate...
void Mult(const mfem::Vector &u, mfem::Vector &residual) const override
Compute residual y = Mu.
mfem::Array< int > _block_true_offsets
std::vector< std::string > _trial_var_names
Subset of _coupled_var_names of all variables corresponding to gridfunctions with degrees of freedom ...
Base class for linear MFEM solvers and preconditioners.
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...
NamedFieldsMap< std::vector< std::shared_ptr< MFEMEssentialBC > > > _essential_bc_map
Arrays to store essential BCs to act on each component of weak form.
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...
mfem::OperatorHandle _jacobian
virtual void SetupLOR(mfem::ParBilinearForm &, mfem::Array< int > &)
Rebuild any Low-Order-Refined components from the unreduced bilinear form.
NamedFieldsMap< NamedFieldsMap< std::vector< std::shared_ptr< MFEMKernel > > > > _kernels_map
Arrays to store kernels to act on each component of weak form.
std::vector< std::string > _test_var_names
Names of all test variables corresponding to linear forms in this equation system.
virtual void BuildEquationSystem()
Build all forms comprising this EquationSystem.
std::vector< std::unique_ptr< mfem::ParGridFunction > > _var_ess_constraints
Gridfunctions holding essential constraints from Dirichlet BCs.
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...
virtual void BuildMixedBilinearForms()
Build mixed bilinear forms (off-diagonal Jacobian contributions)
virtual void FormSystemOperator(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Form matrix-free representation of linear components of system operator.
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 FormSystemMatrix(mfem::OperatorHandle &op, mfem::BlockVector &trueX, mfem::BlockVector &trueRHS)
Form matrix representation of linear components of system operator as a HypreParMatrix.
virtual void AddCoupledVariableNameIfMissing(const std::string &coupled_var_name)
Add coupled variable to EquationSystem.
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, populate solution and RHS vectors of true DoFs, and apply constraints.
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 BuildNonlinearForms()
Build non-linear action forms.
NamedFieldsMap< mfem::ParLinearForm > _lfs
virtual void BuildLinearForms()
Build linear forms and eliminate constrained DoFs.
IntRange< T > make_range(T beg, T end)
int size()
Returns the number of elements in the map.
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...
virtual void ApplyEssentialBCs()
Update all essentially constrained true DoF markers and values on boundaries.
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 ...
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
mfem::OperatorHandle _linear_operator
virtual void AddEliminatedVariableNameIfMissing(const std::string &eliminated_var_name)
Add eliminated variable to EquationSystem.
virtual void Init(GridFunctions &gridfunctions, ComplexGridFunctions &cmplx_gridfunctions, mfem::AssemblyLevel assembly_level)
Initialise.
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
mfem::Operator & GetGradient(const mfem::Vector &u) const override
Get Jacobian at the provided vector of true DoFs of trial variables.
bool _solver_requires_gradient
virtual void ComputeNonlinearResidual(const mfem::Vector &u, mfem::Vector &residual) const
Compute the contribution to the residual from nonlinear forms only.
virtual void ApplyEssentialBC(const std::string &var_name, mfem::ParGridFunction &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...
virtual void AddIntegratedBC(std::shared_ptr< MFEMIntegratedBC > kernel)
auto index_range(const T &sizable)
virtual bool Complex() const
Whether this a complex equation system.
mfem::Array2D< const mfem::HypreParMatrix * > _jacobian_blocks
Moose::MFEM::GridFunctions * _gfuncs