https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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;
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
46void
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
78void
80{
81 mooseError("MFEMPetscNonlinearSolver does not support an external MFEM linear solver. "
82 "Configure PETSc KSP/PC behavior through PETSc options instead.");
83}
84#endif
85
86#endif
registerMooseObject("MooseApp", MFEMPetscNonlinearSolver)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Moose::PetscSupport::PetscOptions & getPetscOptions()
Retrieve a writable reference the PETSc options (used by PetscSupport)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MFEMProblem & getMFEMProblem()
Return the owning MFEM problem.
Definition MFEMObject.h:45
MooseObject wrapper for mfem::PetscNonlinearSolver-backed nonlinear solves.
void SetLinearSolver(mfem::Solver &solver) override
Configure the linear solver used inside the nonlinear solve.
void ConstructSolver() override
Override in derived classes to construct and set the solver options.
MFEMPetscNonlinearSolver(const InputParameters &parameters)
static InputParameters validParams()
std::unique_ptr< mfem::Solver > _solver
Solver to be used for the problem.
A struct for storing the various types of petsc options and values.
std::vector< std::pair< std::string, std::string > > pairs
PETSc key-value pairs.
MultiMooseEnum flags
Single value PETSc options (flags)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
MeshBase & mesh
MultiMooseEnum getCommonPetscFlags()
A helper function to produce a MultiMooseEnum with commonly used PETSc single options (flags)
void setSinglePetscOption(const std::string &name, const std::string &value="", FEProblemBase *const problem=nullptr)
A wrapper function for dealing with different versions of PetscOptionsSetValue.
MultiMooseEnum getCommonPetscKeys()
A helper function to produce a MultiMooseEnum with commonly used PETSc iname options (keys in key-val...
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.
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.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...