https://mooseframework.inl.gov
MFEMPetscNonlinearSolver.C
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 
13 #include "MooseError.h"
14 #include "PetscSupport.h"
15 #include "MFEMProblem.h"
16 
17 #ifdef MFEM_USE_PETSC
18 
20 
23 {
25  params.addClassDescription("MFEM PETSc-backed nonlinear solver using SNES.");
26  params.set<bool>("use_initial_guess", /*quiet_mode=*/true) = true;
27  params.addParam<MultiMooseEnum>(
28  "petsc_options", Moose::PetscSupport::getCommonPetscFlags(), "Singleton PETSc options");
29  params.addParam<MultiMooseEnum>("petsc_options_iname",
31  "Names of PETSc name/value pairs");
32  params.addParam<std::vector<std::string>>(
33  "petsc_options_value",
34  "Values of PETSc name/value pairs (must correspond with \"petsc_options_iname\")");
35  params.addParam<std::string>(
36  "petsc_options_prefix", "", "PETSc options prefix used for this nonlinear solver.");
37  return params;
38 }
39 
41  : Moose::MFEM::NonlinearSolverBase(parameters)
42 {
44 }
45 
46 void
48 {
49  const auto & prefix = getParam<std::string>("petsc_options_prefix");
50  const auto normalized_prefix = !prefix.empty() && prefix.back() != '_' ? prefix + "_" : prefix;
51 
54  getParam<MultiMooseEnum>("petsc_options"), normalized_prefix, *this, petsc_options);
56  getParam<MooseEnumItem, std::string>("petsc_options_iname", "petsc_options_value"),
57  getMFEMProblem().mesh().dimension(),
58  normalized_prefix,
59  *this,
60  petsc_options);
61 
62  for (const auto & flag : petsc_options.flags)
63  Moose::PetscSupport::setSinglePetscOption(flag.rawName().c_str());
64  for (const auto & option : petsc_options.pairs)
65  Moose::PetscSupport::setSinglePetscOption(option.first, option.second);
66 
67  auto solver =
68  std::make_unique<mfem::PetscNonlinearSolver>(getMFEMProblem().getComm(), normalized_prefix);
69  solver->iterative_mode = getParam<bool>("use_initial_guess");
70  solver->SetRelTol(getParam<mfem::real_t>("rel_tol"));
71  solver->SetAbsTol(getParam<mfem::real_t>("abs_tol"));
72  solver->SetMaxIter(getParam<unsigned int>("max_its"));
73  solver->SetPrintLevel(getParam<unsigned int>("print_level"));
74  solver->SetJacobianType(mfem::Operator::PETSC_MATAIJ);
75  _solver = std::move(solver);
76 }
77 
78 void
79 MFEMPetscNonlinearSolver::SetOperator(const mfem::Operator & op)
80 {
81  GetSolver().SetOperator(op);
82 }
83 
84 void
86 {
87  mooseError("MFEMPetscNonlinearSolver does not support an external MFEM linear solver. "
88  "Configure PETSc KSP/PC behavior through PETSc options instead.");
89 }
90 
91 void
92 MFEMPetscNonlinearSolver::Mult(const mfem::Vector & rhs, mfem::Vector & x)
93 {
94  GetSolver().Mult(rhs, x);
95 }
96 #endif
97 
98 #endif
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
void addPetscFlagsToPetscOptions(const MultiMooseEnum &petsc_flags, std::string prefix, const ParallelParamObject &param_object, PetscOptions &petsc_options)
Populate flags in a given PetscOptions object using a vector of input arguments.
Definition: PetscSupport.C:767
MFEMProblem & getMFEMProblem()
Return the owning MFEM problem.
Definition: MFEMObject.h:45
mfem::Solver & GetSolver()
Returns the wrapped MFEM solver.
void addPetscPairsToPetscOptions(const std::vector< std::pair< MooseEnumItem, std::string >> &petsc_pair_options, const unsigned int mesh_dimension, std::string prefix, const ParallelParamObject &param_object, PetscOptions &petsc_options)
Populate name and value pairs in a given PetscOptions object using vectors of input arguments...
Definition: PetscSupport.C:831
A struct for storing the various types of petsc options and values.
Definition: PetscSupport.h:44
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MultiMooseEnum flags
Single value PETSc options (flags)
Definition: PetscSupport.h:56
MeshBase & mesh
std::unique_ptr< mfem::Solver > _solver
Solver to be used for the problem.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
registerMooseObject("MooseApp", MFEMPetscNonlinearSolver)
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
Definition: PetscSupport.h:53
MFEMPetscNonlinearSolver(const InputParameters &parameters)
MooseObject wrapper for mfem::PetscNonlinearSolver-backed nonlinear solves.
void Mult(const mfem::Vector &rhs, mfem::Vector &x) override
Solve the nonlinear system for the provided right-hand side and solution vector.
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags) ...
void SetOperator(const mfem::Operator &op) override
Configure the nonlinear solver with the residual/Jacobian operator.
void ConstructSolver() override
Override in derived classes to construct and set the solver options.
void SetLinearSolver(mfem::Solver &solver) override
Configure the linear solver used inside the nonlinear solve.
MPI_Comm getComm()
Return the MPI communicator associated with this FE problem&#39;s mesh.
Definition: MFEMProblem.h:264
static InputParameters validParams()
void setSinglePetscOption(const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
A wrapper function for dealing with different versions of PetscOptionsSetValue.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...