15 #include "../config/config.hpp"
17 #ifdef MFEM_USE_SUNDIALS
27 #include <sundials/sundials_config.h>
29 #if !defined(SUNDIALS_VERSION_MAJOR) || (SUNDIALS_VERSION_MAJOR < 5)
30 #error MFEM requires SUNDIALS version 5.0.0 or newer!
32 #if defined(MFEM_USE_CUDA) && ((SUNDIALS_VERSION_MAJOR == 5) && (SUNDIALS_VERSION_MINOR < 4))
33 #error MFEM requires SUNDIALS version 5.4.0 or newer when MFEM_USE_CUDA=TRUE!
35 #include <sundials/sundials_matrix.h>
36 #include <sundials/sundials_linearsolver.h>
37 #include <arkode/arkode_arkstep.h>
38 #include <cvodes/cvodes.h>
39 #include <kinsol/kinsol.h>
41 #include <sunmemory/sunmemory_cuda.h>
46 #if (SUNDIALS_VERSION_MAJOR < 6)
60 #endif // SUNDIALS_VERSION_MAJOR < 6
99 operator SUNMemoryHelper()
const {
return h; }
102 size_t memsize, SUNMemoryType mem_type
103 #
if (SUNDIALS_VERSION_MAJOR >= 6)
109 #
if (SUNDIALS_VERSION_MAJOR >= 6)
116 #else // MFEM_USE_CUDA
121 class SundialsMemHelper
133 #endif // MFEM_USE_CUDA
212 SundialsNVector(MPI_Comm comm,
double *data_,
int loc_size,
long glob_size);
226 inline N_Vector_ID
GetNVectorID(N_Vector x_)
const {
return N_VGetVectorID(x_); }
230 inline MPI_Comm
GetComm()
const {
return *
static_cast<MPI_Comm*
>(N_VGetCommunicator(
x)); }
237 void SetSize(
int s,
long glob_size = 0);
269 operator N_Vector()
const {
return x; }
283 using Vector::operator=;
300 static N_Vector
MakeNVector(MPI_Comm comm,
bool use_device);
382 static int RHS(realtype
t,
const N_Vector y, N_Vector ydot,
void *user_data);
385 static int LinSysSetup(realtype
t, N_Vector y, N_Vector fy, SUNMatrix
A,
386 booleantype jok, booleantype *jcur,
387 realtype gamma,
void *user_data, N_Vector tmp1,
388 N_Vector tmp2, N_Vector tmp3);
391 static int LinSysSolve(SUNLinearSolver LS, SUNMatrix
A, N_Vector x,
392 N_Vector
b, realtype tol);
395 static int root(realtype
t, N_Vector y, realtype *gout,
void *user_data);
398 typedef std::function<int(realtype t, Vector y, Vector gout, CVODESolver *)>
405 typedef std::function<int(Vector y, Vector w, CVODESolver*)>
EWTFunction;
452 virtual void Step(
Vector &x,
double &
t,
double &dt);
517 static int RHSQ(realtype
t,
const N_Vector y, N_Vector qdot,
void *user_data);
520 static int RHSB(realtype
t, N_Vector y,
521 N_Vector
yB, N_Vector yBdot,
void *user_dataB);
524 static int RHSQB(realtype
t, N_Vector y, N_Vector
yB,
525 N_Vector qBdot,
void *user_dataB);
528 static int ewt(N_Vector y, N_Vector w,
void *user_data);
578 virtual void Step(
Vector &x,
double &
t,
double &dt);
588 double reltolQ = 1e-3,
589 double abstolQ = 1e-8);
593 double abstolQB = 1e-8);
640 static int LinSysSetupB(realtype
t, N_Vector y, N_Vector
yB, N_Vector fyB,
642 booleantype jok, booleantype *jcur,
643 realtype gamma,
void *user_data, N_Vector tmp1,
644 N_Vector tmp2, N_Vector tmp3);
647 static int LinSysSolveB(SUNLinearSolver LS, SUNMatrix
A, N_Vector x,
648 N_Vector
b, realtype tol);
681 static int RHS1(realtype
t,
const N_Vector y, N_Vector ydot,
void *user_data);
682 static int RHS2(realtype
t,
const N_Vector y, N_Vector ydot,
void *user_data);
686 static int LinSysSetup(realtype
t, N_Vector y, N_Vector fy, SUNMatrix
A,
687 SUNMatrix
M, booleantype jok, booleantype *jcur,
688 realtype gamma,
void *user_data, N_Vector tmp1,
689 N_Vector tmp2, N_Vector tmp3);
692 static int LinSysSolve(SUNLinearSolver LS, SUNMatrix
A, N_Vector x,
693 N_Vector
b, realtype tol);
697 N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
700 static int MassSysSolve(SUNLinearSolver LS, SUNMatrix
M, N_Vector x,
701 N_Vector
b, realtype tol);
704 static int MassMult1(SUNMatrix
M, N_Vector x, N_Vector v);
707 static int MassMult2(N_Vector x, N_Vector v, realtype
t,
755 virtual void Step(
Vector &x,
double &
t,
double &dt);
849 static int Mult(
const N_Vector
u, N_Vector fu,
void *user_data);
853 booleantype *new_u,
void *user_data);
856 static int LinSysSetup(N_Vector
u, N_Vector fu, SUNMatrix J,
857 void *user_data, N_Vector tmp1, N_Vector tmp2);
860 static int LinSysSolve(SUNLinearSolver LS, SUNMatrix J, N_Vector
u,
861 N_Vector
b, realtype tol);
888 KINSolver(
int strategy,
bool oper_grad =
true);
898 KINSolver(MPI_Comm comm,
int strategy,
bool oper_grad =
true);
993 #endif // MFEM_USE_SUNDIALS
995 #endif // MFEM_SUNDIALS
static int RHSQ(realtype t, const N_Vector y, N_Vector qdot, void *user_data)
Wrapper to compute the ODE RHS Quadrature function.
void SetSize(int s, long glob_size=0)
Resize the vector to size s.
void Init(TimeDependentOperator &f_)
Initialize CVODE: calls CVodeCreate() to create the CVODE memory and set some defaults.
void SetJFNKSolver(Solver &solver)
int GetOwnership() const
Gets ownership of the internal N_Vector.
SundialsNVector * q
Quadrature vector.
void GetForwardSolution(double tB, mfem::Vector &yy)
Get Interpolated Forward solution y at backward integration time tB.
virtual void StepB(Vector &w, double &t, double &dt)
Solve one adjoint time step.
static int SundialsMemHelper_Alloc(SUNMemoryHelper helper, SUNMemory *memptr, size_t memsize, SUNMemoryType mem_type#if(SUNDIALS_VERSION_MAJOR >=6), void *queue#endif)
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
std::function< int(Vector y, Vector w, CVODESolver *)> EWTFunction
Typedef declaration for error weight functions.
static int LinSysSolveB(SUNLinearSolver LS, SUNMatrix A, N_Vector x, N_Vector b, realtype tol)
Solve the linear system A x = b.
SundialsMemHelper()=default
Default constructor – object must be moved to.
~SundialsNVector()
Calls SUNDIALS N_VDestroy function if the N_Vector is owned by 'this'.
void SetScaledStepTol(double sstol)
Set KINSOL's scaled step tolerance.
RootFunction root_func
A class member to facilitate pointing to a user-specified root function.
SundialsNVector * Y
State vector.
void SetStepMode(int itask)
Select the ARKode step mode: ARK_NORMAL (default) or ARK_ONE_STEP.
void SetERKTableNum(ARKODE_ERKTableID table_id)
Choose a specific Butcher table for an explicit RK method.
void SetIRKTableNum(ARKODE_DIRKTableID table_id)
Choose a specific Butcher table for a diagonally implicit RK method.
Base abstract class for first order time dependent operators.
virtual void Step(Vector &x, double &t, double &dt)
Integrate the ODE with CVODE using the specified step mode.
static constexpr double default_abs_tolB
Default scalar backward absolute tolerance.
SundialsNVector * f_scale
scaling vectors
void SetSVtolerances(double reltol, Vector abstol)
Set the scalar relative and vector of absolute tolerances.
static int RHSB(realtype t, N_Vector y, N_Vector yB, N_Vector yBdot, void *user_dataB)
Wrapper to compute the ODE RHS backward function.
void MakeRef(Vector &base, int offset, int s)
Reset the Vector to be a reference to a sub-vector of base.
void * GetMem() const
Access the SUNDIALS memory structure.
int indexB
backward problem index
virtual ~CVODESolver()
Destroy the associated CVODE memory and SUNDIALS objects.
bool use_oper_grad
use the Jv prod function
void EvalQuadIntegration(double t, Vector &q)
Evaluate Quadrature.
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
virtual void SetPrintLevel(int print_lvl)
Set the print level for the KINSetPrintLevel function.
static int LinSysSetup(realtype t, N_Vector y, N_Vector fy, SUNMatrix A, SUNMatrix M, booleantype jok, booleantype *jcur, realtype gamma, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Setup the linear system .
void SetMaxStep(double dt_max)
Set the maximum time step.
void SetOrder(int order)
Chooses integration order for all explicit / implicit / IMEX methods.
int GetFlag() const
Returns the last flag returned by a call to a SUNDIALS function.
void * sundials_mem
SUNDIALS mem structure.
static int PrecSolve(N_Vector uu, N_Vector uscale, N_Vector fval, N_Vector fscale, N_Vector vv, void *user_data)
Solve the preconditioner equation .
long saved_global_size
Global vector length on last initialization.
virtual void Step(Vector &x, double &t, double &dt)
SundialsNVector()
Creates an empty SundialsNVector.
void UseSundialsLinearSolverB()
Use built in SUNDIALS Newton solver.
static int MassSysSetup(realtype t, SUNMatrix M, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Setup the linear system .
void SetSStolerancesB(double reltol, double abstol)
Tolerance specification functions for the adjoint problem.
int print_level
(DEPRECATED) Legacy print level definition, which is left for compatibility with custom iterative sol...
void SetMaxSetupCalls(int max_calls)
Set maximum number of nonlinear iterations without a Jacobian update.
void _SetDataAndSize_()
Set data and length from the internal N_Vector x.
ARKStepSolver(Type type=EXPLICIT)
Construct a serial wrapper to SUNDIALS' ARKode integrator.
void SetIMEXTableNum(ARKODE_ERKTableID etable_id, ARKODE_DIRKTableID itable_id)
Choose a specific Butcher table for an IMEX RK method.
void InitAdjointSolve(int steps, int interpolation)
Initialize Adjoint.
Interface to ARKode's ARKStep module – additive Runge-Kutta methods.
int global_strategy
KINSOL solution strategy.
SUNLinearSolver LSA
Linear solver for A.
static int MassMult1(SUNMatrix M, N_Vector x, N_Vector v)
Compute the matrix-vector product .
N_Vector_ID GetNVectorID(N_Vector x_) const
Returns the N_Vector_ID for the N_Vector x_.
SundialsSolver()
Protected constructor: objects of this type should be constructed only as part of a derived class...
SUNMatrix M
Mass matrix M.
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
void SetSVtolerancesB(double reltol, Vector abstol)
Tolerance specification functions for the adjoint problem.
int lmm_type
Linear multistep method type.
Interface to the CVODE library – linear multi-step methods.
void InitQuadIntegration(mfem::Vector &q0, double reltolQ=1e-3, double abstolQ=1e-8)
void InitQuadIntegrationB(mfem::Vector &qB0, double reltolQB=1e-3, double abstolQB=1e-8)
Initialize Quadrature Integration (Adjoint)
Interface to the KINSOL library – nonlinear solver methods.
static MemoryType GetDeviceMemoryType()
Get the current Device MemoryType. This is the MemoryType used by most MFEM classes when allocating m...
static constexpr double default_rel_tolB
Default scalar backward relative tolerance.
SundialsNVector * yy
State vector.
Vector interface for SUNDIALS N_Vectors.
static constexpr double default_abs_tolQB
Default scalar backward absolute quadrature tolerance.
void SetMaxStep(double dt_max)
Set the maximum time step.
Type
Types of ARKODE solvers.
Type rk_type
Runge-Kutta type.
virtual ~ARKStepSolver()
Destroy the associated ARKode memory and SUNDIALS objects.
Newton's method for solving F(x)=b for a given operator F.
SundialsNVector * qB
State vector.
bool use_implicit
True for implicit or imex integration.
void SetJFNK(bool use_jfnk)
Set the Jacobian Free Newton Krylov flag. The default is false.
SUNNonlinearSolver NLS
Nonlinear solver.
constexpr ARKODE_DIRKTableID ARKODE_DIRK_NONE
CVODESolver(int lmm)
Construct a serial wrapper to SUNDIALS' CVODE integrator.
N_Vector StealNVector()
Changes the ownership of the the vector.
static int Mult(const N_Vector u, N_Vector fu, void *user_data)
Wrapper to compute the nonlinear residual .
int step_mode
ARKStep step mode (ARK_NORMAL or ARK_ONE_STEP).
SundialsNVector * y_scale
Wrapper for hypre's parallel vector class.
static int LinSysSetup(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2)
Setup the linear system .
SUNLinearSolver LSM
Linear solver for M.
int ncheck
number of checkpoints used so far
static int RHS1(realtype t, const N_Vector y, N_Vector ydot, void *user_data)
N_Vector x
The actual SUNDIALS object.
Base class for interfacing with SUNDIALS packages.
void UseMFEMLinearSolver()
Attach the linear system setup and solve methods from the TimeDependentOperator i.e., SUNImplicitSetup() and SUNImplicitSolve() to CVODE.
virtual void SetOperator(const Operator &op)
Set the nonlinear Operator of the system and initialize KINSOL.
virtual ~KINSolver()
Destroy the associated KINSOL memory.
SundialsMemHelper(SUNContext context)
void MakeRef(Vector &base, int offset)
Reset the Vector to be a reference to a sub-vector of base without changing its current size...
void SetRootFinder(int components, RootFunction func)
Initialize Root Finder.
Vector wrk
Work vector needed for the JFNK PC.
EWTFunction ewt_func
A class member to facilitate pointing to a user-specified error weight function.
void SetFixedStep(double dt)
Use a fixed time step size (disable temporal adaptivity).
static int root(realtype t, N_Vector y, realtype *gout, void *user_data)
Prototype to define root finding for CVODE.
void SetLSMaxIter(int m)
Set the maximum number of linear solver iterations.
SundialsMemHelper & operator=(const SundialsMemHelper &)=delete
Disable copy assignment.
bool reinit
Flag to signal memory reinitialization is need.
int maa
number of acceleration vectors
static N_Vector MakeNVector(bool use_device)
Create a N_Vector.
void UseMFEMLinearSolverB()
Set Linear Solver for the backward problem.
constexpr ARKODE_ERKTableID ARKODE_FEHLBERG_13_7_8
constexpr ARKODE_ERKTableID ARKODE_ERK_NONE
std::function< int(realtype t, Vector y, Vector gout, CVODESolver *)> RootFunction
Typedef for root finding functions.
void _SetNvecDataAndSize_(long glob_size=0)
Set data and length of internal N_Vector x from 'this'.
void SetMAA(int maa)
Set the number of acceleration vectors to use with KIN_FP or KIN_PICARD.
int maxli
Maximum linear iterations.
void SetOwnership(int own)
Sets ownership of the internal N_Vector.
long GlobalSize() const
Returns the MPI global length for the internal N_Vector x.
long GetNumSteps()
Get the number of internal steps taken so far.
MPI_Comm GetComm() const
Returns the MPI communicator for the internal N_Vector x.
void SetDataAndSize(double *d, int s, long glob_size=0)
Set the vector data and size.
static int LinSysSetup(realtype t, N_Vector y, N_Vector fy, SUNMatrix A, booleantype jok, booleantype *jcur, realtype gamma, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Setup the linear system .
void PrintInfo() const
Print various ARKStep statistics.
N_Vector_ID GetNVectorID() const
Returns the N_Vector_ID for the internal N_Vector.
static int RHS2(realtype t, const N_Vector y, N_Vector ydot, void *user_data)
static int GradientMult(N_Vector v, N_Vector Jv, N_Vector u, booleantype *new_u, void *user_data)
Wrapper to compute the Jacobian-vector product .
void AllocateEmptyNVector(N_Vector &y)
const Operator * jacobian
stores oper->GetGradient()
void InitB(TimeDependentAdjointOperator &f_)
Initialize the adjoint problem.
static int LinSysSetupB(realtype t, N_Vector y, N_Vector yB, N_Vector fyB, SUNMatrix A, booleantype jok, booleantype *jcur, realtype gamma, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
Setup the linear system A x = b.
SUNLinearSolver LSB
Linear solver for A.
Singleton class for SUNContext and SundialsMemHelper objects.
SundialsNVector * yB
State vector.
static constexpr double default_rel_tol
Default scalar relative tolerance.
void SetWFTolerances(EWTFunction func)
Set multiplicative error weights.
SUNMatrix AB
Linear system A = I - gamma J, M - gamma J, or J.
static bool UseManagedMemory()
int step_mode
CVODE step mode (CV_NORMAL or CV_ONE_STEP).
static int MassMult2(N_Vector x, N_Vector v, realtype t, void *mtimes_data)
Compute the matrix-vector product at time t.
KINSolver(int strategy, bool oper_grad=true)
Construct a serial wrapper to SUNDIALS' KINSOL nonlinear solver.
Implicit-explicit ARK method.
static constexpr double default_abs_tol
Default scalar absolute tolerance.
int flag
Last flag returned from a call to SUNDIALS.
static SundialsMemHelper & GetMemHelper()
Provides access to the SundialsMemHelper object.
void UseMFEMLinearSolver()
Attach the linear system setup and solve methods from the TimeDependentOperator i.e., SUNImplicitSetup() and SUNImplicitSolve() to ARKode.
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
static int LinSysSolve(SUNLinearSolver LS, SUNMatrix A, N_Vector x, N_Vector b, realtype tol)
Solve the linear system .
static SUNContext & GetContext()
Provides access to the SUNContext object.
static int LinSysSolve(SUNLinearSolver LS, SUNMatrix A, N_Vector x, N_Vector b, realtype tol)
Solve the linear system .
void PrintInfo() const
Print various CVODE statistics.
void Init(TimeDependentAdjointOperator &f_)
static int PrecSetup(N_Vector uu, N_Vector uscale, N_Vector fval, N_Vector fscale, void *user_data)
Setup the preconditioner.
void SetMaxOrder(int max_order)
Set the maximum method order.
virtual ~CVODESSolver()
Destroy the associated CVODES memory and SUNDIALS objects.
static int ewt(N_Vector y, N_Vector w, void *user_data)
Error control function.
void SetLSMaxRestarts(int m)
Set the maximum number of linear solver restarts.
double u(const Vector &xvec)
void UseMFEMMassLinearSolver(int tdep)
Attach mass matrix linear system setup, solve, and matrix-vector product methods from the TimeDepende...
void SetMaxNStepsB(int mxstepsB)
Set the maximum number of backward steps.
void Init(TimeDependentOperator &f_)
Initialize ARKode: calls ARKStepCreate() to create the ARKStep memory and set some defaults...
virtual void SetPreconditioner(Solver &solver)
Equivalent to SetSolver(solver).
static int RHS(realtype t, const N_Vector y, N_Vector ydot, void *user_data)
Number of components in gout.
int maxlrs
Maximum linear solver restarts.
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
void UseSundialsLinearSolver()
Attach a SUNDIALS GMRES linear solver to ARKode.
long GetNumSteps()
Get Number of Steps for ForwardSolve.
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
static int SundialsMemHelper_Dealloc(SUNMemoryHelper helper, SUNMemory sunmem#if(SUNDIALS_VERSION_MAJOR >=6), void *queue#endif)
static int LinSysSolve(SUNLinearSolver LS, SUNMatrix J, N_Vector u, N_Vector b, realtype tol)
Solve the linear system .
void operator=(const Sundials &other)=delete
Disable copy assignment.
static int MassSysSolve(SUNLinearSolver LS, SUNMatrix M, N_Vector x, N_Vector b, realtype tol)
Solve the linear system .
virtual void Step(Vector &x, double &t, double &dt)
Integrate the ODE with ARKode using the specified step mode.
static int RHSQB(realtype t, N_Vector y, N_Vector yB, N_Vector qBdot, void *user_dataB)
Wrapper to compute the ODE RHS Backwards Quadrature function.
void EvalQuadIntegrationB(double t, Vector &dG_dp)
Evaluate Quadrature solution.
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
void SetMaxNSteps(int steps)
Set the maximum number of time steps.
void SetStepMode(int itask)
Select the CVODE step mode: CV_NORMAL (default) or CV_ONE_STEP.
void UseSundialsMassLinearSolver(int tdep)
Attach the SUNDIALS GMRES linear solver and the mass matrix matrix-vector product method from the Tim...