Serac  0.1
Serac is an implicit thermal strucural mechanics simulation code.
equation_solver.hpp
Go to the documentation of this file.
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Serac Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
13 #pragma once
14 
15 #include <memory>
16 #include <optional>
17 #include <variant>
18 #include <utility>
19 
20 #include "mpi.h"
21 #include "mfem.hpp"
22 
25 #include "serac/numerics/petsc_solvers.hpp"
26 
27 namespace serac {
28 
46  public:
47  // _equationsolver_constructor_start
55  EquationSolver(std::unique_ptr<mfem::NewtonSolver> nonlinear_solver, std::unique_ptr<mfem::Solver> linear_solver,
56  std::unique_ptr<mfem::Solver> preconditioner = nullptr);
57  // _equationsolver_constructor_end
58 
59  // _build_equationsolver_start
67  EquationSolver(NonlinearSolverOptions nonlinear_opts = {}, LinearSolverOptions lin_opts = {},
68  MPI_Comm comm = MPI_COMM_WORLD);
69  // _build_equationsolver_end
70 
77  void setOperator(const mfem::Operator& op);
78 
84  void solve(mfem::Vector& x) const;
85 
90  mfem::NewtonSolver& nonlinearSolver() { return *nonlin_solver_; }
91 
95  const mfem::NewtonSolver& nonlinearSolver() const { return *nonlin_solver_; }
96 
101  mfem::Solver& linearSolver() { return *lin_solver_; }
102 
106  const mfem::Solver& linearSolver() const { return *lin_solver_; }
107 
113  mfem::Solver& preconditioner() { return *preconditioner_; }
114 
118  const mfem::Solver& preconditioner() const { return *preconditioner_; }
119 
123  static void defineInputFileSchema(axom::inlet::Container& container);
124 
125  private:
129  std::unique_ptr<mfem::Solver> preconditioner_;
130 
134  std::unique_ptr<mfem::Solver> lin_solver_;
135 
139  std::unique_ptr<mfem::NewtonSolver> nonlin_solver_;
140 
146  bool nonlin_solver_set_solver_called_ = false;
147 };
148 
152 class SuperLUSolver : public mfem::Solver {
153  public:
159  SuperLUSolver(int print_level, MPI_Comm comm) : superlu_solver_(comm)
160  {
161  superlu_solver_.SetColumnPermutation(mfem::superlu::PARMETIS);
162  if (print_level == 0) {
163  superlu_solver_.SetPrintStatistics(false);
164  }
165  }
166 
173  void Mult(const mfem::Vector& input, mfem::Vector& output) const;
174 
183  void SetOperator(const mfem::Operator& op);
184 
185  private:
190  mutable std::unique_ptr<mfem::SuperLURowLocMatrix> superlu_mat_;
191 
197  mfem::SuperLUSolver superlu_solver_;
198 };
199 
200 #ifdef MFEM_USE_STRUMPACK
204 class StrumpackSolver : public mfem::Solver {
205  public:
211  StrumpackSolver(int print_level, MPI_Comm comm) : strumpack_solver_(comm)
212  {
213  strumpack_solver_.SetKrylovSolver(strumpack::KrylovSolver::DIRECT);
214  strumpack_solver_.SetReorderingStrategy(strumpack::ReorderingStrategy::METIS);
215 
216  if (print_level == 1) {
217  strumpack_solver_.SetPrintFactorStatistics(true);
218  strumpack_solver_.SetPrintSolveStatistics(true);
219  }
220  }
221 
228  void Mult(const mfem::Vector& input, mfem::Vector& output) const;
229 
236  void SetOperator(const mfem::Operator& op);
237 
238  private:
243  mutable std::unique_ptr<mfem::STRUMPACKRowLocMatrix> strumpack_mat_;
244 
250  mfem::STRUMPACKSolver strumpack_solver_;
251 };
252 
253 #endif
254 
263 std::unique_ptr<mfem::HypreParMatrix> buildMonolithicMatrix(const mfem::BlockOperator& block_operator);
264 
274 std::unique_ptr<mfem::NewtonSolver> buildNonlinearSolver(const NonlinearSolverOptions& nonlinear_opts,
275  const LinearSolverOptions& linear_opts,
276  mfem::Solver& preconditioner, MPI_Comm comm = MPI_COMM_WORLD);
277 
285 std::pair<std::unique_ptr<mfem::Solver>, std::unique_ptr<mfem::Solver>> buildLinearSolverAndPreconditioner(
286  LinearSolverOptions linear_opts = {}, MPI_Comm comm = MPI_COMM_WORLD);
287 
294 std::unique_ptr<mfem::Solver> buildPreconditioner(LinearSolverOptions linear_opts,
295  [[maybe_unused]] MPI_Comm comm = MPI_COMM_WORLD);
296 
297 #ifdef MFEM_USE_AMGX
305 std::unique_ptr<mfem::AmgXSolver> buildAMGX(const AMGXOptions& options, const MPI_Comm comm);
306 #endif
307 
308 } // namespace serac
309 
315 template <>
316 struct FromInlet<serac::LinearSolverOptions> {
318  serac::LinearSolverOptions operator()(const axom::inlet::Container& base);
319 };
320 
326 template <>
327 struct FromInlet<serac::NonlinearSolverOptions> {
329  serac::NonlinearSolverOptions operator()(const axom::inlet::Container& base);
330 };
331 
337 template <>
338 struct FromInlet<serac::EquationSolver> {
340  serac::EquationSolver operator()(const axom::inlet::Container& base);
341 };
This class manages the objects typically required to solve a nonlinear set of equations arising from ...
mfem::NewtonSolver & nonlinearSolver()
const mfem::Solver & linearSolver() const
void setOperator(const mfem::Operator &op)
void solve(mfem::Vector &x) const
mfem::Solver & preconditioner()
static void defineInputFileSchema(axom::inlet::Container &container)
EquationSolver(std::unique_ptr< mfem::NewtonSolver > nonlinear_solver, std::unique_ptr< mfem::Solver > linear_solver, std::unique_ptr< mfem::Solver > preconditioner=nullptr)
const mfem::Solver & preconditioner() const
const mfem::NewtonSolver & nonlinearSolver() const
mfem::Solver & linearSolver()
A wrapper class for using the MFEM SuperLU solver with a HypreParMatrix.
void Mult(const mfem::Vector &input, mfem::Vector &output) const
Factor and solve the linear system y = Op^{-1} x using DSuperLU.
SuperLUSolver(int print_level, MPI_Comm comm)
Constructs a wrapper over an mfem::SuperLUSolver.
void SetOperator(const mfem::Operator &op)
Set the underlying matrix operator to use in the solution algorithm.
This file contains the all the necessary functions for reading input files.
Accelerator functionality.
Definition: serac.cpp:36
std::pair< std::unique_ptr< mfem::Solver >, std::unique_ptr< mfem::Solver > > buildLinearSolverAndPreconditioner(LinearSolverOptions linear_opts, MPI_Comm comm)
Build the linear solver and its associated preconditioner given a linear options struct.
std::unique_ptr< mfem::Solver > buildPreconditioner(LinearSolverOptions linear_opts, [[maybe_unused]] MPI_Comm comm)
Build a preconditioner from the available options.
std::unique_ptr< mfem::NewtonSolver > buildNonlinearSolver(const NonlinearSolverOptions &nonlinear_opts, const LinearSolverOptions &linear_opts, mfem::Solver &prec, MPI_Comm comm)
Build a nonlinear solver using the nonlinear option struct.
std::unique_ptr< mfem::HypreParMatrix > buildMonolithicMatrix(const mfem::BlockOperator &block_operator)
Function for building a monolithic parallel Hypre matrix from a block system of smaller Hypre matrice...
This file contains enumerations and record types for physics solver configuration.
Parameters for an iterative linear solution scheme.
Nonlinear solution scheme parameters.