Line data Source code
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 : #include "MFEMPetscNonlinearSolver.h" 13 : #include "MooseError.h" 14 : #include "PetscSupport.h" 15 : #include "MFEMProblem.h" 16 : 17 : #ifdef MFEM_USE_PETSC 18 : 19 : registerMooseObject("MooseApp", MFEMPetscNonlinearSolver); 20 : 21 : InputParameters 22 2112 : MFEMPetscNonlinearSolver::validParams() 23 : { 24 2112 : InputParameters params = Moose::MFEM::NonlinearSolverBase::validParams(); 25 4224 : params.addClassDescription("MFEM PETSc-backed nonlinear solver using SNES."); 26 4224 : params.set<bool>("use_initial_guess", /*quiet_mode=*/true) = true; 27 6336 : params.addParam<MultiMooseEnum>( 28 4224 : "petsc_options", Moose::PetscSupport::getCommonPetscFlags(), "Singleton PETSc options"); 29 6336 : params.addParam<MultiMooseEnum>("petsc_options_iname", 30 4224 : Moose::PetscSupport::getCommonPetscKeys(), 31 : "Names of PETSc name/value pairs"); 32 8448 : params.addParam<std::vector<std::string>>( 33 : "petsc_options_value", 34 : "Values of PETSc name/value pairs (must correspond with \"petsc_options_iname\")"); 35 6336 : params.addParam<std::string>( 36 : "petsc_options_prefix", "", "PETSc options prefix used for this nonlinear solver."); 37 2112 : return params; 38 0 : } 39 : 40 7 : MFEMPetscNonlinearSolver::MFEMPetscNonlinearSolver(const InputParameters & parameters) 41 7 : : Moose::MFEM::NonlinearSolverBase(parameters) 42 : { 43 7 : ConstructSolver(); 44 7 : } 45 : 46 : void 47 7 : MFEMPetscNonlinearSolver::ConstructSolver() 48 : { 49 14 : const auto & prefix = getParam<std::string>("petsc_options_prefix"); 50 7 : const auto normalized_prefix = !prefix.empty() && prefix.back() != '_' ? prefix + "_" : prefix; 51 : 52 7 : Moose::PetscSupport::PetscOptions & petsc_options = getMFEMProblem().getPetscOptions(); 53 21 : Moose::PetscSupport::addPetscFlagsToPetscOptions( 54 : getParam<MultiMooseEnum>("petsc_options"), normalized_prefix, *this, petsc_options); 55 14 : Moose::PetscSupport::addPetscPairsToPetscOptions( 56 42 : getParam<MooseEnumItem, std::string>("petsc_options_iname", "petsc_options_value"), 57 7 : getMFEMProblem().mesh().dimension(), 58 : normalized_prefix, 59 : *this, 60 : petsc_options); 61 : 62 7 : for (const auto & flag : petsc_options.flags) 63 0 : Moose::PetscSupport::setSinglePetscOption(flag.rawName().c_str()); 64 14 : for (const auto & option : petsc_options.pairs) 65 7 : Moose::PetscSupport::setSinglePetscOption(option.first, option.second); 66 : 67 : auto solver = 68 7 : std::make_unique<mfem::PetscNonlinearSolver>(getMFEMProblem().getComm(), normalized_prefix); 69 14 : solver->iterative_mode = getParam<bool>("use_initial_guess"); 70 14 : solver->SetRelTol(getParam<mfem::real_t>("rel_tol")); 71 14 : solver->SetAbsTol(getParam<mfem::real_t>("abs_tol")); 72 14 : solver->SetMaxIter(getParam<unsigned int>("max_its")); 73 14 : solver->SetPrintLevel(getParam<unsigned int>("print_level")); 74 7 : solver->SetJacobianType(mfem::Operator::PETSC_MATAIJ); 75 7 : _solver = std::move(solver); 76 7 : } 77 : 78 : void 79 7 : MFEMPetscNonlinearSolver::SetOperator(const mfem::Operator & op) 80 : { 81 7 : GetSolver().SetOperator(op); 82 7 : } 83 : 84 : void 85 0 : MFEMPetscNonlinearSolver::SetLinearSolver(mfem::Solver &) 86 : { 87 0 : 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 7 : MFEMPetscNonlinearSolver::Mult(const mfem::Vector & rhs, mfem::Vector & x) 93 : { 94 7 : GetSolver().Mult(rhs, x); 95 7 : } 96 : #endif 97 : 98 : #endif