https://mooseframework.inl.gov
MFEMGeometricMultigridSolver.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 "MFEMProblem.h"
14 #include "EquationSystem.h"
15 
17 
19 {
20  // MGProxy is installed as a preconditioner, so Mult() should overwrite its output vector rather
21  // than treating it as an initial iterate. This matches MFEM's
22  // IterativeSolver::SetPreconditioner() convention, which sets the preconditioner's iterative_mode
23  // to false.
24  iterative_mode = false;
25 }
26 
27 void
28 MFEMGeometricMultigridSolver::MGProxy::setMG(mfem::GeometricMultigrid & mg)
29 {
30  _mg = &mg;
31  height = mg.Height();
32  width = mg.Width();
33 }
34 
35 void
37 {
38  _owner.BuildMultigrid(op);
39 }
40 
41 void
42 MFEMGeometricMultigridSolver::MGProxy::Mult(const mfem::Vector & x, mfem::Vector & y) const
43 {
44  MFEM_VERIFY(_mg, "MGProxy: GeometricMultigrid not yet built");
45  _mg->Mult(x, y);
46 }
47 
50 {
52  params.addClassDescription(
53  "Geometric (p-)multigrid preconditioner backed by mfem::GeometricMultigrid. "
54  "Requires a linear equation system, an MFEMFESpaceHierarchy, and per-level smoother "
55  "objects.");
56 
57  params.addRequiredParam<std::string>("variable",
58  "Name of the trial variable this preconditioner acts on.");
59  params.addRequiredParam<std::string>(
60  "fespace_hierarchy", "Name of the MFEMFESpaceHierarchy that defines the level structure.");
61  params.addRequiredParam<std::vector<MFEMSolverName>>(
62  "smoothers",
63  "Names of LinearSolverBase objects used as smoothers on the interior levels "
64  "(levels 1 to N-1). May have length 1 (used on all interior levels) or "
65  "N-1 (one per interior level, ordered coarse-to-fine).");
66  params.addRequiredParam<MFEMSolverName>(
67  "coarse_solver", "Name of the LinearSolverBase used on the coarsest level.");
68  params.addRequiredParam<std::vector<std::string>>(
69  "assembly_levels",
70  "Assembly level for each level in the hierarchy. Valid values: 'legacy', 'full', "
71  "'element', 'partial', 'none'. May have length 1 (used on all N levels) or N.");
72  return params;
73 }
74 
77  _var_name(getParam<std::string>("variable")),
78  _smoother_names(getParam<std::vector<MFEMSolverName>>("smoothers")),
79  _coarse_solver_name(getParam<MFEMSolverName>("coarse_solver"))
80 {
81  // Co-own the hierarchy so it outlives this solver.
82  const auto & hierarchy_name = getParam<std::string>("fespace_hierarchy");
84 
85  // Parse assembly levels, optionally expanding a single input value to all levels.
86  const int N = _hierarchy->GetNumLevels();
87  const auto & asm_strs = getParam<std::vector<std::string>>("assembly_levels");
88  const int n_asm = asm_strs.size();
89  if (n_asm != 1 && n_asm != N)
90  paramError(
91  "assembly_levels", "must have length 1 or N = ", N, " (total levels), got ", n_asm, ".");
92 
93  _assembly_levels.resize(N);
94  for (const auto i : make_range(N))
95  _assembly_levels[i] = ParseAssemblyLevel(n_asm == 1 ? asm_strs[0] : asm_strs[i]);
96 
98 }
99 
100 void
102 {
103  _mg.reset();
104  _level_ops.clear();
105  _level_blfs.clear();
106 
107  auto proxy = std::make_unique<MGProxy>(*this);
108  _mg_proxy = proxy.get();
109  _solver = std::move(proxy);
110 }
111 
112 mfem::AssemblyLevel
114 {
115  if (s == "legacy")
116  return mfem::AssemblyLevel::LEGACY;
117  if (s == "full")
118  return mfem::AssemblyLevel::FULL;
119  if (s == "element")
120  return mfem::AssemblyLevel::ELEMENT;
121  if (s == "partial")
122  return mfem::AssemblyLevel::PARTIAL;
123  if (s == "none")
124  return mfem::AssemblyLevel::NONE;
125  paramError("assembly_levels",
126  "unknown assembly level '",
127  s,
128  "'. Valid values: legacy, full, element, partial, none.");
129  return mfem::AssemblyLevel::LEGACY;
130 }
131 
132 void
134 {
135  BuildMultigrid(op);
136 }
137 
138 void
140 {
141  auto & problem = getMFEMProblem();
142  auto & pd = problem.getProblemData();
143 
144  auto * eq_sys = dynamic_cast<Moose::MFEM::EquationSystem *>(pd.eqn_system.get());
145  if (!eq_sys)
146  mooseError("GeometricMultigridSolver '",
147  name(),
148  "': requires a standard (non-complex, non-time-dependent) EquationSystem.");
149 
150  if (eq_sys->Nonlinear())
151  mooseError("GeometricMultigridSolver '",
152  name(),
153  "': nonlinear equation systems are not currently supported.");
154 
155  if (eq_sys->HasMixedBilinearForms(_var_name))
156  paramError("variable",
157  "mixed bilinear form contributions are not supported for variable '",
158  _var_name,
159  "'. Block multigrid is required for saddle-point / mixed-field problems.");
160 
161  const int N = _hierarchy->GetNumLevels();
162  if (N < 1)
163  paramError("fespace_hierarchy", "hierarchy must contain at least one level.");
164  const int finest_level = N - 1;
165 
166  // Validate smoother vector length (levels 1 to N-1 each need a smoother).
167  const int n_smooth = _smoother_names.size();
168  if (n_smooth != 1 && n_smooth != N - 1)
169  paramError("smoothers", "must have length 1 or N-1 = ", N - 1, ", got ", n_smooth, ".");
170 
171  auto get_smoother = [&](int level) -> Moose::MFEM::LinearSolverBase &
172  {
173  if (level == 0)
174  return problem.getMFEMObject<Moose::MFEM::LinearSolverBase>("Moose::MFEM::SolverBase",
176  const std::string & sname = (n_smooth == 1) ? _smoother_names[0] : _smoother_names[level - 1];
177  return problem.getMFEMObject<Moose::MFEM::LinearSolverBase>("Moose::MFEM::SolverBase", sname);
178  };
179 
180  // Obtain essential boundary attribute markers from the equation system.
181  mfem::Array<int> ess_bdr = eq_sys->BuildEssentialBoundaryMarkers(_var_name);
182 
183  auto & finest_fespace =
184  static_cast<mfem::ParFiniteElementSpace &>(_hierarchy->GetFESpaceAtLevel(finest_level));
185  const int finest_size = finest_fespace.GetTrueVSize();
186  if (op.Height() != finest_size || op.Width() != finest_size)
187  mooseError("GeometricMultigridSolver '",
188  name(),
189  "': incoming fine operator has size ",
190  op.Height(),
191  " x ",
192  op.Width(),
193  ", but the finest hierarchy space has true size ",
194  finest_size,
195  ".");
196 
197  // Build new levels' forms; accumulate before touching _mg / _level_*.
198  std::vector<std::shared_ptr<mfem::ParBilinearForm>> new_blfs;
199  std::vector<std::unique_ptr<mfem::OperatorHandle>> new_level_ops;
200  new_level_ops.reserve(N - 1);
201 
202  auto mg = std::make_unique<mfem::GeometricMultigrid>(*_hierarchy, ess_bdr);
203  auto * mg_ptr = mg.get();
204 
205  for (const auto level : make_range(N))
206  {
207  auto & level_fespace =
208  static_cast<mfem::ParFiniteElementSpace &>(_hierarchy->GetFESpaceAtLevel(level));
209 
210  // Compute essential true DoFs for this level.
211  mfem::Array<int> level_tdofs;
212  level_fespace.GetEssentialTrueDofs(ess_bdr, level_tdofs);
213 
214  // Build level operator.
215  mfem::Operator * level_op = nullptr;
216  bool own_op = false;
217 
218  if (level == finest_level)
219  {
220  level_op = const_cast<mfem::Operator *>(&op);
221  own_op = false;
222  }
223  else
224  {
225  auto blf =
226  eq_sys->BuildBilinearFormForFESpace(_var_name, level_fespace, _assembly_levels[level]);
227 
228  auto level_op_handle = std::make_unique<mfem::OperatorHandle>();
229  blf->FormSystemMatrix(level_tdofs, *level_op_handle);
230  level_op = level_op_handle->Ptr();
231  own_op = false; // owned by level_op_handle or blf
232  new_level_ops.push_back(std::move(level_op_handle));
233  new_blfs.push_back(std::move(blf));
234  }
235 
236  // Configure the smoother / coarse solver with this level's operator.
237  // Each smoother's SetOperator() owns full initialization.
238  auto & level_smoother = get_smoother(level);
239  level_smoother.SetOperator(*level_op);
240 
241  mg_ptr->AddLevel(level_op, &level_smoother.GetSolver(), own_op, /*ownSmoother=*/false);
242  }
243 
244  // Atomically replace:
245  // 1. Old MG freed, dropping raw pointers into level operators.
246  // 2. Old operator handles freed before old forms they may wrap.
247  // 3. Proxy updated to point at the new MG and level data.
248  _mg = std::move(mg);
249  _level_ops = std::move(new_level_ops);
250  _level_blfs = std::move(new_blfs);
251  _mg_proxy->setMG(*_mg);
252 }
253 #endif
void ConstructSolver() override
Creates a stable proxy solver; the real multigrid is built when the proxy gets an operator...
std::shared_ptr< mfem::ParFiniteElementSpaceHierarchy > _hierarchy
Finite element space hierarchy defining the multigrid levels.
MFEMGeometricMultigridSolver(const InputParameters &parameters)
MFEMProblem & getMFEMProblem()
Return the owning MFEM problem.
Definition: MFEMObject.h:45
MFEMProblemData & getProblemData()
Method to get the current MFEMProblemData object storing the current data specifying the FE problem...
Definition: MFEMProblem.h:266
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:467
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
void BuildMultigrid(const mfem::Operator &op)
Rebuild the multigrid object and per-level operators for the supplied finest-level operator...
const std::string _var_name
Trial variable whose operator is preconditioned by this solver.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
LinearSolverBase(const InputParameters &parameters)
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...
Owns the weak-form mathematics of a MOOSE MFEM problem.
MGProxy(MFEMGeometricMultigridSolver &owner)
Constructs a proxy that delegates multigrid rebuilding to the owning MOOSE solver.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
static InputParameters validParams()
std::vector< mfem::AssemblyLevel > _assembly_levels
Assembly level requested for each multigrid level after optional single-value expansion.
const MFEMSolverName _coarse_solver_name
Name of the solver used on the coarsest multigrid level.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
Base class for linear MFEM solvers and preconditioners.
void SetOperator(mfem::Operator &op) override
Rebuilds the multigrid hierarchy for the supplied finest-level operator.
void Mult(const mfem::Vector &x, mfem::Vector &y) const override
Applies the current concrete MFEM multigrid preconditioner.
Moose::MFEM::FESpaceHierarchies fespace_hierarchies
std::vector< std::shared_ptr< mfem::ParBilinearForm > > _level_blfs
Rediscretized bilinear forms kept alive for the active linear coarse-level operators.
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...
const std::vector< MFEMSolverName > _smoother_names
Names of solvers used as smoothers on interior multigrid levels.
std::unique_ptr< mfem::GeometricMultigrid > _mg
Concrete MFEM multigrid preconditioner rebuilt on each SetOperator() call.
mfem::AssemblyLevel ParseAssemblyLevel(const std::string &s) const
Map assembly-level string ("legacy", "full", "element", "partial", "none") to the corresponding mfem:...
MGProxy * _mg_proxy
Non-owning pointer to the proxy solver stored in _solver.
IntRange< T > make_range(T beg, T end)
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
P-multigrid / geometric multigrid preconditioner backed by mfem::GeometricMultigrid.
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...
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
void setMG(mfem::GeometricMultigrid &mg)
Updates the concrete MFEM multigrid object used by Mult().
std::vector< std::unique_ptr< mfem::OperatorHandle > > _level_ops
Constrained linear coarse-level operators; destroyed before the forms that own their data...
registerMooseObject("MooseApp", MFEMGeometricMultigridSolver)
void SetOperator(const mfem::Operator &op) override
Rebuilds the owner&#39;s multigrid hierarchy for the new outer-solver operator.