OGS  master
NumLib::PETScNonlinearSolver Class Referencefinal

Detailed Description

Definition at line 24 of file PETScNonlinearSolver.h.

#include <PETScNonlinearSolver.h>

Inheritance diagram for NumLib::PETScNonlinearSolver:
Collaboration diagram for NumLib::PETScNonlinearSolver:

Public Types

using System = NonlinearSystem< NonlinearSolverTag::Newton >
 Type of the nonlinear equation system to be solved. More...
 

Public Member Functions

 PETScNonlinearSolver (GlobalLinearSolver &linear_solver, int maxiter, std::string prefix)
 
void setEquationSystem (System &eq, ConvergenceCriterion &conv_crit)
 Set the nonlinear equation system that will be solved. More...
 
void compensateNonEquilibriumInitialResiduum (bool const value)
 
void calculateNonEquilibriumInitialResiduum (std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id) override
 
NonlinearSolverStatus solve (std::vector< GlobalVector * > &x, std::vector< GlobalVector * > const &x_prev, std::function< void(int, std::vector< GlobalVector * > const &)> const &postIterationCallback, int const process_id) override
 
- Public Member Functions inherited from NumLib::NonlinearSolverBase
virtual ~NonlinearSolverBase ()=default
 

Private Attributes

SNES _snes_solver
 
System_equation_system = nullptr
 
ConvergenceCriterion_convergence_criterion = nullptr
 
std::size_t _residual_id = 0u
 ID of the residual vector. More...
 
std::size_t _jacobian_id = 0u
 ID of the Jacobian matrix. More...
 
std::size_t _petsc_x_id = 0u
 
std::size_t _petsc_jacobian_id = 0u
 
std::size_t _petsc_residual_id = 0u
 
bool _compensate_non_equilibrium_initial_residuum = false
 

Member Typedef Documentation

◆ System

Type of the nonlinear equation system to be solved.

Definition at line 28 of file PETScNonlinearSolver.h.

Constructor & Destructor Documentation

◆ PETScNonlinearSolver()

NumLib::PETScNonlinearSolver::PETScNonlinearSolver ( GlobalLinearSolver linear_solver,
int  maxiter,
std::string  prefix 
)

Constructs a new instance.

Parameters
linear_solverthe linear solver used by this nonlinear solver.
maxiterthe maximum number of iterations used to solve the equation.
prefixused to set the SNES options.

Definition at line 80 of file PETScNonlinearSolver.cpp.

83 {
84  SNESCreate(PETSC_COMM_WORLD, &_snes_solver);
85  if (!prefix.empty())
86  {
87  prefix = prefix + "_";
88  SNESSetOptionsPrefix(_snes_solver, prefix.c_str());
89  }
90  // force SNESSolve() to take at least one iteration regardless of the
91  // initial residual norm
92  SNESSetForceIteration(_snes_solver, PETSC_TRUE);
93 
94  // Set the maximum iterations.
95  PetscReal atol, rtol, stol;
96  PetscInt maxf;
97  SNESGetTolerances(_snes_solver, &atol, &rtol, &stol, nullptr, &maxf);
98  SNESSetTolerances(_snes_solver, atol, rtol, stol, maxiter, maxf);
99 
100  SNESSetFromOptions(_snes_solver);
101 #ifndef NDEBUG
102  PetscOptionsView(nullptr, PETSC_VIEWER_STDOUT_WORLD);
103 #endif // NDEBUG
104 }

References _snes_solver.

Member Function Documentation

◆ calculateNonEquilibriumInitialResiduum()

void NumLib::PETScNonlinearSolver::calculateNonEquilibriumInitialResiduum ( std::vector< GlobalVector * > const &  x,
std::vector< GlobalVector * > const &  x_prev,
int const  process_id 
)
overridevirtual

Implements NumLib::NonlinearSolverBase.

Definition at line 119 of file PETScNonlinearSolver.cpp.

122 {
124  {
125  return;
126  }
127 
128  OGS_FATAL(
129  "Non-equilibrium initial residuum is not implemented for the "
130  "PETScNonlinearSolver.");
131 }
#define OGS_FATAL(...)
Definition: Error.h:25

References _compensate_non_equilibrium_initial_residuum, and OGS_FATAL.

◆ compensateNonEquilibriumInitialResiduum()

void NumLib::PETScNonlinearSolver::compensateNonEquilibriumInitialResiduum ( bool const  value)

◆ setEquationSystem()

void NumLib::PETScNonlinearSolver::setEquationSystem ( System eq,
ConvergenceCriterion conv_crit 
)

Set the nonlinear equation system that will be solved.

Definition at line 106 of file PETScNonlinearSolver.cpp.

108 {
109  _equation_system = &eq;
110  _convergence_criterion = &conv_crit;
111 }
ConvergenceCriterion * _convergence_criterion

References _convergence_criterion, and _equation_system.

◆ solve()

NonlinearSolverStatus NumLib::PETScNonlinearSolver::solve ( std::vector< GlobalVector * > &  x,
std::vector< GlobalVector * > const &  x_prev,
std::function< void(int, std::vector< GlobalVector * > const &)> const &  postIterationCallback,
int const  process_id 
)
overridevirtual

Assemble and solve the equation system.

Parameters
xin: the initial guess, out: the solution.
x_prevprevious time step solution.
postIterationCallbackcalled after each iteration if set.
process_idusually used in staggered schemes.
Return values
trueif the equation system could be solved
falseotherwise

Implements NumLib::NonlinearSolverBase.

Definition at line 133 of file PETScNonlinearSolver.cpp.

140 {
141  DBUG("PETScNonlinearSolver: solve()");
142  using TimeDiscretizedSystem =
143  TimeDiscretizedODESystem<ODESystemTag::FirstOrderImplicitQuasilinear,
145 
146  auto* system = static_cast<TimeDiscretizedSystem*>(_equation_system);
147 
148  DBUG("PETScNonlinearSolver: create vectors");
149  // r and J on which the ogs assembly operates.
151  system->getMatrixSpecifications(process_id), _jacobian_id);
153  system->getMatrixSpecifications(process_id), _residual_id);
154 
155  // Vectors and matrices used by SNES for solutions. These will be locked
156  // during the SNES' solve call.
158  system->getMatrixSpecifications(process_id), _petsc_jacobian_id);
159  BaseLib::RunTime timer_dirichlet;
160  double time_dirichlet = 0.0;
161 
162  timer_dirichlet.start();
163  system->computeKnownSolutions(*x[process_id], process_id);
164  system->applyKnownSolutions(*x[process_id]);
165  time_dirichlet += timer_dirichlet.elapsed();
166  INFO("[time] Applying Dirichlet BCs took {} s.", time_dirichlet);
167 
169  system->getMatrixSpecifications(process_id), _petsc_residual_id);
171  system->getMatrixSpecifications(process_id), _petsc_x_id);
172  MathLib::LinAlg::copy(*x[process_id], x_snes); // Initial guess.
173 
174  PetscContext petsc_context{_equation_system, x, x_prev, &r, &J, process_id};
175 
176  DBUG("PETScNonlinearSolver: set function");
177  SNESSetFunction(_snes_solver, r_snes.getRawVector(), updateResidual,
178  &petsc_context);
179 
180  DBUG("PETScNonlinearSolver: set jacobian");
181  // The jacobian and the preconditioner matrices point to the same location.
182  SNESSetJacobian(_snes_solver, J_snes.getRawMatrix(), J_snes.getRawMatrix(),
183  updateJacobian, &petsc_context);
184 
185  std::unique_ptr<GlobalVector> xl = nullptr;
186  std::unique_ptr<GlobalVector> xu = nullptr;
187 
188  SNESType snes_type;
189  SNESGetType(_snes_solver, &snes_type);
190  if ((std::strcmp(snes_type, SNESVINEWTONRSLS) == 0) ||
191  (std::strcmp(snes_type, SNESVINEWTONSSLS) == 0))
192  {
193  // Set optional constraints via callback.
194  DBUG("PETScNonlinearSolver: set constraints");
196  system->getMatrixSpecifications(process_id));
198  system->getMatrixSpecifications(process_id));
199 
200  system->updateConstraints(*xl, *xu, process_id);
203 
204  SNESVISetVariableBounds(_snes_solver, xl->getRawVector(),
205  xu->getRawVector());
206  }
207 
208  DBUG("PETScNonlinearSolver: call SNESSolve");
209  SNESSolve(_snes_solver, nullptr, x_snes.getRawVector());
210 
211  SNESConvergedReason reason = SNES_CONVERGED_ITERATING;
212  SNESGetConvergedReason(_snes_solver, &reason);
213  INFO("PETSsSNES convergence reason {}.", reason);
214 
215  PetscInt iterations;
216  SNESGetIterationNumber(_snes_solver, &iterations);
217  INFO("PETScSNES used {} iterations.", iterations);
218 
219  // Copy back the solution.
220  MathLib::LinAlg::copy(x_snes, *x[process_id]);
221 
227 
228  return {reason >= 0, iterations};
229 }
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
Count the running time.
Definition: RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition: RunTime.h:42
void start()
Start the timer.
Definition: RunTime.h:32
virtual GlobalMatrix & getMatrix(std::size_t &id)=0
Get an uninitialized matrix with the given id.
virtual void releaseMatrix(GlobalMatrix const &A)=0
std::size_t _jacobian_id
ID of the Jacobian matrix.
std::size_t _residual_id
ID of the residual vector.
virtual GlobalVector & getVector(std::size_t &id)=0
Get an uninitialized vector with the given id.
virtual void releaseVector(GlobalVector const &x)=0
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.
static const double r
PetscErrorCode updateResidual(SNES, Vec x, Vec petsc_r, void *petsc_context)
PetscErrorCode updateJacobian(SNES, Vec, Mat J, Mat, void *petsc_context)
static NUMLIB_EXPORT MatrixProvider & provider
static NUMLIB_EXPORT VectorProvider & provider

References _equation_system, _jacobian_id, _petsc_jacobian_id, _petsc_residual_id, _petsc_x_id, _residual_id, _snes_solver, MathLib::LinAlg::copy(), DBUG(), BaseLib::RunTime::elapsed(), MathLib::finalizeVectorAssembly(), NumLib::FirstOrderImplicitQuasilinear, NumLib::MatrixProvider::getMatrix(), NumLib::VectorProvider::getVector(), INFO(), NumLib::Newton, NumLib::GlobalVectorProvider::provider, NumLib::GlobalMatrixProvider::provider, MathLib::r, NumLib::MatrixProvider::releaseMatrix(), NumLib::VectorProvider::releaseVector(), BaseLib::RunTime::start(), anonymous_namespace{PETScNonlinearSolver.cpp}::updateJacobian(), and anonymous_namespace{PETScNonlinearSolver.cpp}::updateResidual().

Member Data Documentation

◆ _compensate_non_equilibrium_initial_residuum

bool NumLib::PETScNonlinearSolver::_compensate_non_equilibrium_initial_residuum = false
private

Enables computation of the non-equilibrium initial residuum \( r_{\rm neq} \) before the first time step. The forces are zero if the external forces are in equilibrium with the initial state/initial conditions. During the simulation the new residuum reads \( \tilde r = r - r_{\rm neq} \).

Definition at line 73 of file PETScNonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), and compensateNonEquilibriumInitialResiduum().

◆ _convergence_criterion

ConvergenceCriterion* NumLib::PETScNonlinearSolver::_convergence_criterion = nullptr
private

Definition at line 62 of file PETScNonlinearSolver.h.

Referenced by setEquationSystem().

◆ _equation_system

System* NumLib::PETScNonlinearSolver::_equation_system = nullptr
private

Definition at line 61 of file PETScNonlinearSolver.h.

Referenced by setEquationSystem(), and solve().

◆ _jacobian_id

std::size_t NumLib::PETScNonlinearSolver::_jacobian_id = 0u
private

ID of the Jacobian matrix.

Definition at line 65 of file PETScNonlinearSolver.h.

Referenced by solve().

◆ _petsc_jacobian_id

std::size_t NumLib::PETScNonlinearSolver::_petsc_jacobian_id = 0u
private

Definition at line 68 of file PETScNonlinearSolver.h.

Referenced by solve().

◆ _petsc_residual_id

std::size_t NumLib::PETScNonlinearSolver::_petsc_residual_id = 0u
private

Definition at line 69 of file PETScNonlinearSolver.h.

Referenced by solve().

◆ _petsc_x_id

std::size_t NumLib::PETScNonlinearSolver::_petsc_x_id = 0u
private

Definition at line 67 of file PETScNonlinearSolver.h.

Referenced by solve().

◆ _residual_id

std::size_t NumLib::PETScNonlinearSolver::_residual_id = 0u
private

ID of the residual vector.

Definition at line 64 of file PETScNonlinearSolver.h.

Referenced by solve().

◆ _snes_solver

SNES NumLib::PETScNonlinearSolver::_snes_solver
private

Definition at line 59 of file PETScNonlinearSolver.h.

Referenced by PETScNonlinearSolver(), and solve().


The documentation for this class was generated from the following files: