Smith  0.1
Smith is an implicit thermal structural mechanics simulation code.
nonlinear_block_solver.hpp
Go to the documentation of this file.
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Smith 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 <functional>
17 #include <optional>
18 #include <vector>
19 #include <mpi.h>
20 
22 #include "smith/numerics/nonlinear_convergence.hpp"
23 
24 namespace mfem {
25 class Solver;
26 class Vector;
27 class HypreParMatrix;
28 class BlockOperator;
29 } // namespace mfem
30 
31 namespace smith {
32 
33 class EquationSolver;
34 class BoundaryConditionManager;
35 class FiniteElementState;
36 class FiniteElementDual;
37 class Mesh;
38 struct NonlinearSolverOptions;
39 struct LinearSolverOptions;
40 
43  public:
46 
48  using FieldPtr = std::shared_ptr<FieldT>;
50  using DualPtr = std::shared_ptr<FieldD>;
51  using MatrixPtr = std::unique_ptr<mfem::HypreParMatrix>;
52 
59  virtual std::vector<FieldPtr> solve(
60  const std::vector<FieldPtr>& u_guesses,
61  std::function<std::vector<mfem::Vector>(const std::vector<FieldPtr>&)> residuals,
62  std::function<std::vector<std::vector<MatrixPtr>>(const std::vector<FieldPtr>&)> jacobians) const = 0;
63 
68  virtual std::vector<FieldPtr> solveAdjoint(const std::vector<DualPtr>& u_bars,
69  std::vector<std::vector<MatrixPtr>>& jacobian_transposed) const = 0;
70 
72  ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector<mfem::Vector>& residuals) const
73  {
74  return convergenceStatus(tolerance_multiplier, residuals, convergence_context_);
75  }
76 
78  virtual ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector<mfem::Vector>& residuals,
79  NonlinearConvergenceContext& context) const = 0;
80 
82  virtual void primeConvergenceContext(const std::vector<mfem::Vector>& residuals,
83  NonlinearConvergenceContext& context) const = 0;
84 
86  bool checkConvergence(double tolerance_multiplier, const std::vector<mfem::Vector>& residuals) const
87  {
88  return convergenceStatus(tolerance_multiplier, residuals).converged;
89  }
90 
93  virtual void resetConvergenceState() const { convergence_context_.reset(); }
94 
96  virtual void clearMemory() const {}
97 
99  virtual void setInnerToleranceMultiplier(double multiplier) = 0;
100 
101  protected:
102  mutable bool is_setup_ = false;
104 };
105 
108  public:
110 
115  NonlinearBlockSolver(std::unique_ptr<EquationSolver> s, MPI_Comm comm, double abs_tol = 1e-12, double rel_tol = 1e-8,
116  std::optional<NonlinearSolverOptions> retained_nonlinear_options = std::nullopt,
117  std::optional<LinearSolverOptions> retained_linear_options = std::nullopt);
118 
120  ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector<mfem::Vector>& residuals,
121  NonlinearConvergenceContext& context) const override;
122 
124  void primeConvergenceContext(const std::vector<mfem::Vector>& residuals,
125  NonlinearConvergenceContext& context) const override;
126 
128  std::vector<FieldPtr> solve(
129  const std::vector<FieldPtr>& u_guesses,
130  std::function<std::vector<mfem::Vector>(const std::vector<FieldPtr>&)> residuals,
131  std::function<std::vector<std::vector<MatrixPtr>>(const std::vector<FieldPtr>&)> jacobians) const override;
132 
134  std::vector<FieldPtr> solveAdjoint(const std::vector<DualPtr>& u_bars,
135  std::vector<std::vector<MatrixPtr>>& jacobian_transposed) const override;
136 
138  void completeSetup(const std::vector<FieldPtr>& us) const;
139 
141  void setInnerToleranceMultiplier(double multiplier) override { inner_tol_multiplier_ = multiplier; }
142 
144  std::shared_ptr<NonlinearBlockSolver> cloneFresh() const;
145 
146  mutable std::unique_ptr<mfem::BlockOperator>
148  mutable std::vector<std::vector<MatrixPtr>>
151 
152  mutable std::unique_ptr<EquationSolver>
154 
155  MPI_Comm comm_;
156  double abs_tol_;
157  double rel_tol_;
158  double inner_tol_multiplier_ = 1.0;
159  std::optional<NonlinearSolverOptions> retained_nonlinear_options_ = std::nullopt;
160  std::optional<LinearSolverOptions> retained_linear_options_ = std::nullopt;
161 };
162 
167 std::shared_ptr<NonlinearBlockSolver> buildNonlinearBlockSolver(NonlinearSolverOptions nonlinear_opts,
168  LinearSolverOptions linear_opts,
169  const smith::Mesh& mesh);
170 
171 } // namespace smith
Class for encapsulating the dual vector space of a finite element space (i.e. the space of linear for...
Class for encapsulating the critical MFEM components of a primal finite element field.
Helper class for constructing a mesh consistent with Smith.
Definition: mesh.hpp:37
Abstract interface for nonlinear block solvers that provide both forward and adjoint solves.
bool checkConvergence(double tolerance_multiplier, const std::vector< mfem::Vector > &residuals) const
Check whether the current residuals satisfy the convergence criterion.
bool is_setup_
Records if this block solver has its preconditioner initialized.
ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector< mfem::Vector > &residuals) const
Evaluate convergence for this solver's configured tolerance using solver-owned convergence state.
virtual void primeConvergenceContext(const std::vector< mfem::Vector > &residuals, NonlinearConvergenceContext &context) const =0
Initialize a convergence context from a residual snapshot without relying on a fake convergence test.
virtual std::vector< FieldPtr > solveAdjoint(const std::vector< DualPtr > &u_bars, std::vector< std::vector< MatrixPtr >> &jacobian_transposed) const =0
Solve the (linear) adjoint set of equations with a vector of FiniteElementState as unknown.
std::shared_ptr< FieldD > DualPtr
using
virtual ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector< mfem::Vector > &residuals, NonlinearConvergenceContext &context) const =0
Evaluate convergence with externally owned convergence state.
std::shared_ptr< FieldT > FieldPtr
using
std::unique_ptr< mfem::HypreParMatrix > MatrixPtr
using
virtual void resetConvergenceState() const
Reset internal convergence tracking state (e.g. the stored initial residual norm used for relative to...
NonlinearConvergenceContext convergence_context_
Solver-owned convergence state for one solve.
virtual void clearMemory() const
Interface option to clear memory between solves to avoid high-water mark memory usage.
virtual std::vector< FieldPtr > solve(const std::vector< FieldPtr > &u_guesses, std::function< std::vector< mfem::Vector >(const std::vector< FieldPtr > &)> residuals, std::function< std::vector< std::vector< MatrixPtr >>(const std::vector< FieldPtr > &)> jacobians) const =0
Solve a set of equations with a vector of FiniteElementState as unknown.
virtual void setInnerToleranceMultiplier(double multiplier)=0
Set an inner-solve tolerance multiplier, e.g. for staggered solves.
Nonlinear block solver backed by an EquationSolver forward solve and linear adjoint solves.
double abs_tol_
absolute residual tolerance for convergence check
ConvergenceStatus convergenceStatus(double tolerance_multiplier, const std::vector< mfem::Vector > &residuals) const
Evaluate convergence for this solver's configured tolerance using solver-owned convergence state.
std::vector< FieldPtr > solveAdjoint(const std::vector< DualPtr > &u_bars, std::vector< std::vector< MatrixPtr >> &jacobian_transposed) const override
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::unique_ptr< EquationSolver > nonlinear_solver_
the nonlinear equation solver used for the forward pass
std::vector< FieldPtr > solve(const std::vector< FieldPtr > &u_guesses, std::function< std::vector< mfem::Vector >(const std::vector< FieldPtr > &)> residuals, std::function< std::vector< std::vector< MatrixPtr >>(const std::vector< FieldPtr > &)> jacobians) const override
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::optional< LinearSolverOptions > retained_linear_options_
retained linear config
void primeConvergenceContext(const std::vector< mfem::Vector > &residuals, NonlinearConvergenceContext &context) const override
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::unique_ptr< mfem::BlockOperator > block_jac_
Need to hold an instance of a block operator to work with the mfem solver interface.
MPI_Comm comm_
MPI communicator for parallel norm computation.
NonlinearBlockSolver(std::unique_ptr< EquationSolver > s, MPI_Comm comm, double abs_tol=1e-12, double rel_tol=1e-8, std::optional< NonlinearSolverOptions > retained_nonlinear_options=std::nullopt, std::optional< LinearSolverOptions > retained_linear_options=std::nullopt)
Construct from a nonlinear equation solver.
void setInnerToleranceMultiplier(double multiplier) override
Set the inner tolerance multiplier.
std::optional< NonlinearSolverOptions > retained_nonlinear_options_
retained nonlinear config
void completeSetup(const std::vector< FieldPtr > &us) const
Initialize the preconditioner in case of vector problems.
double inner_tol_multiplier_
multiplier for tolerances during inner solves
double rel_tol_
relative residual tolerance for convergence check
std::shared_ptr< NonlinearBlockSolver > cloneFresh() const
Build a fresh solver instance from retained config.
std::vector< std::vector< MatrixPtr > > matrix_of_jacs_
Accelerator functionality.
Definition: smith.cpp:36
std::shared_ptr< NonlinearBlockSolver > buildNonlinearBlockSolver(NonlinearSolverOptions nonlinear_opts, LinearSolverOptions linear_opts, const smith::Mesh &mesh)
Create an equation-backed nonlinear block solver.
This file contains enumerations and record types for physics solver configuration.
Detailed status from evaluating nonlinear residual convergence.
bool converged
True when the scalar global residual criterion passes.
Parameters for an iterative linear solution scheme.
Stores initial residual norms used for relative nonlinear convergence checks.
void reset()
Clear all stored initial norms for a new solve. The next convergence evaluation seeds a fresh relativ...
Nonlinear solution scheme parameters.