Advanced Multi-Physics (AMP)
On-Line Documentation
PetscSNESSolver.h
Go to the documentation of this file.
1#ifndef included_AMP_PetscSNESSolver
2#define included_AMP_PetscSNESSolver
3
4#include "AMP/solvers/SolverStrategy.h"
5#include "AMP/solvers/SolverStrategyParameters.h"
6#include "AMP/solvers/petsc/PetscKrylovSolver.h"
7#include "AMP/solvers/petsc/PetscMonitor.h"
8#include "AMP/utils/AMP_MPI.h"
9#include "AMP/vectors/petsc/PetscHelpers.h"
10
11#include <list>
12
13#ifdef MPICH_SKIP_MPICXX
14 #define _FIX_FOR_PETSC_MPI_CXX
15 #undef MPICH_SKIP_MPICXX
16#endif
17
18#ifdef OMPI_SKIP_MPICXX
19 #define _FIX_FOR_PETSC_OMPI_CXX
20 #undef OMPI_SKIP_MPICXX
21#endif
22
23#undef PETSC_VERSION_DATE_GIT
24#undef PETSC_VERSION_GIT
25#include "petsc.h"
26#include "petscmat.h"
27#include "petscsnes.h"
28
29#ifdef _FIX_FOR_PETSC_OMPI_CXX
30 #ifndef OMPI_SKIP_MPICXX
31 #define OMPI_SKIP_MPICXX
32 #endif
33#endif
34
35#ifdef _FIX_FOR_PETSC_MPI_CXX
36 #ifndef MPICH_SKIP_MPICXX
37 #define MPICH_SKIP_MPICXX
38 #endif
39#endif
40
41namespace AMP::LinearAlgebra {
42class PetscVector;
43}
44
45
46namespace AMP::Solver {
47
48
54{
55public:
60
61
102 explicit PetscSNESSolver( std::shared_ptr<SolverStrategyParameters> parameters );
103
108
109 std::string type() const override { return "PetscSNESSolver"; }
110
112 static std::unique_ptr<SolverStrategy>
113 createSolver( std::shared_ptr<SolverStrategyParameters> solverStrategyParameters )
114 {
115 return std::make_unique<PetscSNESSolver>( solverStrategyParameters );
116 }
117
123 void apply( std::shared_ptr<const AMP::LinearAlgebra::Vector> f,
124 std::shared_ptr<AMP::LinearAlgebra::Vector> u ) override;
125
130 void setInitialGuess( std::shared_ptr<AMP::LinearAlgebra::Vector> initialGuess ) override;
131
135 SNES getSNESSolver( void ) { return d_SNESSolver; }
136
141 bool getUsesJacobian( void ) { return d_bUsesJacobian; }
142
146 std::shared_ptr<PetscKrylovSolver> getKrylovSolver( void ) { return d_pKrylovSolver; }
147
148 std::shared_ptr<SolverStrategy> getNestedSolver( void ) override { return getKrylovSolver(); }
149
153 std::shared_ptr<AMP::LinearAlgebra::Vector> getSolution( void ) { return d_pSolutionVector; }
154
158 std::shared_ptr<AMP::LinearAlgebra::Vector> getScratchVector( void )
159 {
160 return d_pScratchVector;
161 }
162
170
175
177 // Note that this will enable the line search functionality also
178 void setLineSearchPreCheck( std::function<int( std::shared_ptr<AMP::LinearAlgebra::Vector>,
179 std::shared_ptr<AMP::LinearAlgebra::Vector>,
180 bool & )> lineSearchPreCheckPtr );
181
182 std::function<int( std::shared_ptr<AMP::LinearAlgebra::Vector>,
183 std::shared_ptr<AMP::LinearAlgebra::Vector>,
184 bool & )>
190
191 void reset( std::shared_ptr<AMP::Solver::SolverStrategyParameters> ) override;
192
194
196 std::ostream &os = AMP::pout ) const override;
197
198protected:
199private:
200 void getFromInput( std::shared_ptr<const AMP::Database> db );
201
202 std::shared_ptr<SolverStrategy>
203 createPreconditioner( std::shared_ptr<AMP::Database> pc_solver_db );
204
205 static PetscErrorCode apply( SNES snes, Vec x, Vec f, void *ctx );
206
207 void preApply( std::shared_ptr<const AMP::LinearAlgebra::Vector> v );
208
209 static PetscErrorCode setJacobian( SNES, Vec x, Mat A, Mat, void *ctx );
210
211 static bool isVectorValid( std::shared_ptr<AMP::Operator::Operator> &op,
213 const AMP_MPI &comm );
214
215 int defaultLineSearchPreCheck( std::shared_ptr<AMP::LinearAlgebra::Vector> x,
216 std::shared_ptr<AMP::LinearAlgebra::Vector> y,
217 bool &changed_y );
218
219 static PetscErrorCode
221
222 // copies of PETSc routines that are not exposed for Eisenstat-Walker
225
227
230
232
233 void createPetscObjects( std::shared_ptr<const SolverStrategyParameters> params );
234 void resetPetscObjects( std::shared_ptr<AMP::Solver::SolverStrategyParameters> params );
237
238 // pointer to the line search precheck function
239 std::function<int( std::shared_ptr<AMP::LinearAlgebra::Vector>,
240 std::shared_ptr<AMP::LinearAlgebra::Vector>,
241 bool & )>
243
244 bool d_bUsesJacobian = false;
251
255
257
258 // strategy to use for MFFD differencing (DS or WP)
260
261 // string prefix for SNES options passed on command line or through petsc options
263 // error in MFFD approximations
265
266 // The next set of options are specific to forcing
267 std::string d_sForcingTermStrategy = "CONSTANT";
269
273
274 // Eisenstat-Walker algo. options
279
282
284
285 std::shared_ptr<AMP::LinearAlgebra::Vector> d_pSolutionVector = nullptr;
286 std::shared_ptr<AMP::LinearAlgebra::Vector> d_pResidualVector = nullptr;
287 std::shared_ptr<AMP::LinearAlgebra::Vector> d_pScratchVector = nullptr;
288
289 std::shared_ptr<PetscMonitor> d_PetscMonitor = nullptr;
290
292
293 Mat d_Jacobian = nullptr;
294
296
297 std::shared_ptr<PetscKrylovSolver> d_pKrylovSolver = nullptr;
298};
299} // namespace AMP::Solver
300
301#endif
struct _p_Mat * Mat
struct _p_PC * PC
struct _p_SNES * SNES
struct _p_KSP * KSP
int PetscErrorCode
Provides C++ wrapper around MPI routines.
Definition AMP_MPI.h:63
std::shared_ptr< Vector > shared_ptr
Shorthand for shared pointer to Vector.
Definition Vector.h:60
std::shared_ptr< SolverStrategy > getNestedSolver(void) override
std::shared_ptr< PetscMonitor > d_PetscMonitor
std::shared_ptr< AMP::LinearAlgebra::Vector > getScratchVector(void)
int d_iForcingTermFlag
string is for input
void setInitialGuess(std::shared_ptr< AMP::LinearAlgebra::Vector > initialGuess) override
void apply(std::shared_ptr< const AMP::LinearAlgebra::Vector > f, std::shared_ptr< AMP::LinearAlgebra::Vector > u) override
std::vector< int > d_iLinearIterationHistory
keeps track of linear iteration statistics over solver lifetime
static PetscErrorCode applyPreconditioner(PC pc, Vec xin, Vec xout)
void createPetscObjects(std::shared_ptr< const SolverStrategyParameters > params)
static PetscErrorCode mffdCheckBounds(void *checkctx, Vec U, Vec a, PetscScalar *h)
double d_dConstantForcingTerm
int is for passing choice to PETSc
static PetscErrorCode wrapperLineSearchPreCheck(SNESLineSearch snes, Vec x, Vec y, PetscBool *changed_y, void *ctx)
static PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes)
void printConvergenceStatus(SolverStrategy::SolverStatus status, std::ostream &os=AMP::pout) const override
static PetscErrorCode setJacobian(SNES, Vec x, Mat A, Mat, void *ctx)
void reset(std::shared_ptr< AMP::Solver::SolverStrategyParameters >) override
pointer to line search function
void getFromInput(std::shared_ptr< const AMP::Database > db)
std::function< int(std::shared_ptr< AMP::LinearAlgebra::Vector >, std::shared_ptr< AMP::LinearAlgebra::Vector >, bool &)> d_lineSearchPreCheckPtr
PetscSNESSolver(std::shared_ptr< SolverStrategyParameters > parameters)
static PetscErrorCode setupPreconditioner(PC pc)
std::string type() const override
Return the name of the solver.
std::shared_ptr< PetscKrylovSolver > getKrylovSolver(void)
void setLineSearchPreCheck(std::function< int(std::shared_ptr< AMP::LinearAlgebra::Vector >, std::shared_ptr< AMP::LinearAlgebra::Vector >, bool &)> lineSearchPreCheckPtr)
set the pointer to the user function that does the line search pre-check if provided
int getTotalNumberOfLinearIterations(void) const
std::shared_ptr< AMP::LinearAlgebra::Vector > d_pSolutionVector
void resetPetscObjects(std::shared_ptr< AMP::Solver::SolverStrategyParameters > params)
static bool isVectorValid(std::shared_ptr< AMP::Operator::Operator > &op, AMP::LinearAlgebra::Vector::shared_ptr &v, const AMP_MPI &comm)
std::shared_ptr< AMP::LinearAlgebra::Vector > d_pScratchVector
int defaultLineSearchPreCheck(std::shared_ptr< AMP::LinearAlgebra::Vector > x, std::shared_ptr< AMP::LinearAlgebra::Vector > y, bool &changed_y)
std::shared_ptr< SolverStrategy > createPreconditioner(std::shared_ptr< AMP::Database > pc_solver_db)
static PetscErrorCode apply(SNES snes, Vec x, Vec f, void *ctx)
std::shared_ptr< AMP::LinearAlgebra::Vector > d_pResidualVector
static PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes)
std::shared_ptr< PetscKrylovSolver > d_pKrylovSolver
std::function< int(std::shared_ptr< AMP::LinearAlgebra::Vector >, std::shared_ptr< AMP::LinearAlgebra::Vector >, bool &)> getLineSearchPreCheckAdaptor()
static std::unique_ptr< SolverStrategy > createSolver(std::shared_ptr< SolverStrategyParameters > solverStrategyParameters)
static create routine that is used by SolverFactory
std::shared_ptr< AMP::LinearAlgebra::Vector > getSolution(void)
SNESConvergedReason d_SNES_completion_code
void preApply(std::shared_ptr< const AMP::LinearAlgebra::Vector > v)
std::shared_ptr< AMP::Solver::SolverStrategy > shared_ptr
#define AMP_ASSERT(EXP)
Assert error.
std::ostream pout
PETSc vector.



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page