1 #ifndef AMREX_SUNDIALS_INTEGRATOR_H
2 #define AMREX_SUNDIALS_INTEGRATOR_H
6 #include <AMReX_Config.H>
14 #include <nvector/nvector_manyvector.h>
15 #include <sunlinsol/sunlinsol_spgmr.h>
16 #include <arkode/arkode_arkstep.h>
17 #include <arkode/arkode_mristep.h>
24 std::function<
int(amrex::Real, N_Vector, N_Vector,
void*)>
f;
28 std::function<
int(amrex::Real, N_Vector, N_Vector,
void*)>
fi;
29 std::function<
int(amrex::Real, N_Vector, N_Vector,
void*)>
fe;
32 std::function<
int(amrex::Real, N_Vector, N_Vector,
void*)>
ff;
43 namespace SundialsUserFun {
44 static int f (amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
46 return udata->
f(t, y_data, y_rhs, user_data);
49 static int fi (amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
51 return udata->
fi(t, y_data, y_rhs, user_data);
54 static int fe (amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
56 return udata->
fe(t, y_data, y_rhs, user_data);
59 static int ff (amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
61 return udata->
ff(t, y_data, y_rhs, user_data);
64 static int post_stage (amrex::Real t, N_Vector y_data,
void *user_data) {
66 return udata->
post_stage(t, y_data, user_data);
69 static int post_step (amrex::Real t, N_Vector y_data,
void *user_data) {
71 return udata->
post_step(t, y_data, user_data);
79 static int post_fast_step (amrex::Real t, N_Vector y_data,
void *user_data) {
118 SUNLinearSolver
LS =
nullptr;
137 if (
type ==
"ERK" ||
type ==
"DIRK" ||
type ==
"IMEX-RK") {
140 else if (
type ==
"EX-MRI" ||
type ==
"IM-MRI" ||
type ==
"IMEX-MRI") {
144 std::string msg(
"Unknown method type: ");
150 void SetupRK (amrex::Real time, N_Vector y_data)
157 if (
method !=
"DEFAULT") {
162 else if (
type ==
"DIRK") {
166 if (
method !=
"DEFAULT") {
171 else if (
type ==
"IMEX-RK") {
190 if (
type ==
"DIRK" ||
type ==
"IMEX-RK") {
191 LS = SUNLinSol_SPGMR(y_data, SUN_PREC_NONE, 0,
sunctx);
207 if (
method !=
"DEFAULT") {
216 if (
method !=
"DEFAULT") {
239 if (
type ==
"EX-MRI") {
244 else if (
type ==
"IM-MRI") {
249 else if (
type ==
"IMEX-MRI") {
256 if (
method !=
"DEFAULT") {
257 MRIStepCoupling MRIC = MRIStepCoupling_LoadTableByName(
method.c_str());
259 MRIStepCoupling_Free(MRIC);
269 if (
type ==
"IM-MRI" ||
type ==
"IMEX-MRI") {
270 LS = SUNLinSol_SPGMR(y_data, SUN_PREC_NONE, 0,
sunctx);
286 const int num_vecs = N_VGetNumSubvectors_ManyVector(y_data);
287 S_data.resize(num_vecs);
289 for(
int i = 0; i < num_vecs; i++)
301 auto get_length = [&](
int index) -> sunindextype {
302 auto* p_mf = &S_data[index];
303 return p_mf->nComp() * (p_mf->boxArray()).numPts();
306 sunindextype NV_len = S_data.
size();
307 N_Vector* NV_array =
new N_Vector[NV_len];
309 for (
int i = 0; i < NV_len; ++i) {
314 N_Vector y_data = N_VNew_ManyVector(NV_len, NV_array,
sunctx);
324 auto get_length = [&](
int index) -> sunindextype {
325 auto* p_mf = &S_data[index];
326 return p_mf->nComp() * (p_mf->boxArray()).numPts();
329 sunindextype NV_len = S_data.
size();
330 N_Vector* NV_array =
new N_Vector[NV_len];
332 for (
int i = 0; i < NV_len; ++i) {
348 N_Vector y_data = N_VNew_ManyVector(NV_len, NV_array,
sunctx);
403 void initialize (
const T& S_data,
const amrex::Real time = 0.0)
407 #if defined(SUNDIALS_VERSION_MAJOR) && (SUNDIALS_VERSION_MAJOR < 7)
408 # ifdef AMREX_USE_MPI
409 sunctx = ::sundials::Context(&mpi_comm);
411 sunctx = ::sundials::Context(
nullptr);
414 # ifdef AMREX_USE_MPI
415 sunctx = ::sundials::Context(mpi_comm);
417 sunctx = ::sundials::Context(SUN_COMM_NULL);
422 udata.
f = [&](amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
436 udata.
fi = [&](amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
450 udata.
fe = [&](amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
464 udata.
ff = [&](amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
544 amrex::Real
advance (T& S_old, T& S_new, amrex::Real time,
const amrex::Real dt)
override
546 amrex::Real tout = time + dt;
555 int flag = ARKStepEvolve(
arkode_mem, tout, y_new, &tret, ARK_ONE_STEP);
561 int flag = MRIStepEvolve(
arkode_mem, tout, y_new, &tret, ARK_ONE_STEP);
564 Error(
"SUNDIALS integrator type not specified.");
573 void evolve (T& S_out,
const amrex::Real time_out)
override
576 amrex::Real time_ret;
584 flag = ARKStepEvolve(
arkode_mem, time_out, y_out, &time_ret, ARK_NORMAL);
594 flag = MRIStepEvolve(
arkode_mem, time_out, y_out, &time_ret, ARK_NORMAL);
597 Error(
"SUNDIALS integrator type not specified.");
605 void map_data (std::function<
void(T&)> )
override {}
#define AMREX_ALWAYS_ASSERT(EX)
Definition: AMReX_BLassert.H:50
amrex::ParmParse pp
Input file parser instance for the given namespace.
Definition: AMReX_HypreIJIface.cpp:15
int MPI_Comm
Definition: AMReX_ccse-mpi.H:47
Long numPts() const noexcept
Returns the total number of cells contained in all boxes in the BoxArray.
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition: AMReX_FabArrayBase.H:94
int nGrow(int direction=0) const noexcept
Return the grow factor that defines the region of definition.
Definition: AMReX_FabArrayBase.H:77
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition: AMReX_FabArrayBase.H:130
int nComp() const noexcept
Return number of variables (aka components) associated with each point.
Definition: AMReX_FabArrayBase.H:82
Definition: AMReX_IntegratorBase.H:163
bool use_adaptive_fast_time_step
Flag to enable/disable adaptive time stepping at the fast time scale in multirate methods (bool)
Definition: AMReX_IntegratorBase.H:232
amrex::Real fast_rel_tol
Relative tolerance for adaptive time stepping at the fast time scale (Real)
Definition: AMReX_IntegratorBase.H:264
amrex::Real rel_tol
Relative tolerance for adaptive time stepping (Real)
Definition: AMReX_IntegratorBase.H:253
std::function< void(T &, amrex::Real)> post_fast_stage_action
The post_stage_action function is called by the integrator on the computed stage just after it is com...
Definition: AMReX_IntegratorBase.H:204
amrex::Real fast_abs_tol
Absolute tolerance for adaptive time stepping at the fast time scale (Real)
Definition: AMReX_IntegratorBase.H:270
amrex::Real fast_time_step
Current integrator fast time scale time step size with multirate methods (Real)
Definition: AMReX_IntegratorBase.H:238
std::function< void(T &rhs, T &state, const amrex::Real time)> RhsEx
RhsEx is the explicit right-hand-side function an ImEx integrator will use.
Definition: AMReX_IntegratorBase.H:180
std::function< void(T &, amrex::Real)> post_step_action
The post_step_action function is called by the integrator on the computed state just after it is comp...
Definition: AMReX_IntegratorBase.H:198
std::function< void(T &, amrex::Real)> post_fast_step_action
The post_step_action function is called by the integrator on the computed state just after it is comp...
Definition: AMReX_IntegratorBase.H:210
std::function< void(T &rhs, T &state, const amrex::Real time)> RhsIm
RhsIm is the implicit right-hand-side function an ImEx integrator will use.
Definition: AMReX_IntegratorBase.H:174
std::function< void(T &rhs, T &state, const amrex::Real time)> Rhs
Rhs is the right-hand-side function the integrator will use.
Definition: AMReX_IntegratorBase.H:168
bool use_adaptive_time_step
Flag to enable/disable adaptive time stepping in single rate methods or at the slow time scale in mul...
Definition: AMReX_IntegratorBase.H:216
std::function< void(T &, amrex::Real)> post_stage_action
The post_stage_action function is called by the integrator on the computed stage just after it is com...
Definition: AMReX_IntegratorBase.H:192
amrex::Real time_step
Current integrator time step size (Real)
Definition: AMReX_IntegratorBase.H:221
std::function< void(T &rhs, T &state, const amrex::Real time)> RhsFast
RhsFast is the fast timescale right-hand-side function a multirate integrator will use.
Definition: AMReX_IntegratorBase.H:186
amrex::Real abs_tol
Absolute tolerance for adaptive time stepping (Real)
Definition: AMReX_IntegratorBase.H:258
A collection (stored as an array) of FArrayBox objects.
Definition: AMReX_MultiFab.H:38
static void Copy(MultiFab &dst, const MultiFab &src, int srccomp, int dstcomp, int numcomp, int nghost)
Copy from src to dst including nghost ghost cells. The two MultiFabs MUST have the same underlying Bo...
Definition: AMReX_MultiFab.cpp:193
Parse Parameters From Command Line and Input Files.
Definition: AMReX_ParmParse.H:320
int query(const char *name, bool &ref, int ival=FIRST) const
Same as querykth() but searches for the last occurrence of name.
Definition: AMReX_ParmParse.cpp:1309
This class provides the user with a few print options.
Definition: AMReX_Print.H:35
Definition: AMReX_SundialsIntegrator.H:87
SUNLinearSolver fast_LS
Definition: AMReX_SundialsIntegrator.H:123
bool use_mri
Definition: AMReX_SundialsIntegrator.H:105
void time_interpolate(const T &, const T &, amrex::Real, T &) override
Definition: AMReX_SundialsIntegrator.H:603
SundialsIntegrator()
Definition: AMReX_SundialsIntegrator.H:396
void unpack_vector(N_Vector y_data, amrex::MultiFab &S_data)
Definition: AMReX_SundialsIntegrator.H:360
N_Vector copy_data(const amrex::MultiFab &S_data)
Definition: AMReX_SundialsIntegrator.H:376
std::string type
Definition: AMReX_SundialsIntegrator.H:92
void initialize(const T &S_data, const amrex::Real time=0.0)
Definition: AMReX_SundialsIntegrator.H:403
::sundials::Context sunctx
Definition: AMReX_SundialsIntegrator.H:114
MRIStepInnerStepper fast_stepper
Definition: AMReX_SundialsIntegrator.H:122
void unpack_vector(N_Vector y_data, amrex::Vector< amrex::MultiFab > &S_data)
Definition: AMReX_SundialsIntegrator.H:284
N_Vector copy_data(const amrex::Vector< amrex::MultiFab > &S_data)
Definition: AMReX_SundialsIntegrator.H:322
void SetupRK(amrex::Real time, N_Vector y_data)
Definition: AMReX_SundialsIntegrator.H:150
std::string method_e
Definition: AMReX_SundialsIntegrator.H:96
void * arkode_mem
Definition: AMReX_SundialsIntegrator.H:117
std::string method_i
Definition: AMReX_SundialsIntegrator.H:97
std::string fast_method
Definition: AMReX_SundialsIntegrator.H:101
amrex::Real advance(T &S_old, T &S_new, amrex::Real time, const amrex::Real dt) override
Take a single time step from (time, S_old) to (time + dt, S_new) with the given step size.
Definition: AMReX_SundialsIntegrator.H:544
void * arkode_fast_mem
Definition: AMReX_SundialsIntegrator.H:121
void evolve(T &S_out, const amrex::Real time_out) override
Evolve the current (internal) integrator state to time_out.
Definition: AMReX_SundialsIntegrator.H:573
N_Vector wrap_data(amrex::MultiFab &S_data)
Definition: AMReX_SundialsIntegrator.H:369
bool use_ark
Definition: AMReX_SundialsIntegrator.H:104
virtual ~SundialsIntegrator()
Definition: AMReX_SundialsIntegrator.H:535
SundialsUserData udata
Definition: AMReX_SundialsIntegrator.H:108
void initialize_parameters()
Definition: AMReX_SundialsIntegrator.H:125
N_Vector wrap_data(amrex::Vector< amrex::MultiFab > &S_data)
Definition: AMReX_SundialsIntegrator.H:299
std::string method
Definition: AMReX_SundialsIntegrator.H:95
SUNLinearSolver LS
Definition: AMReX_SundialsIntegrator.H:118
SundialsIntegrator(const T &S_data, const amrex::Real time=0.0)
Definition: AMReX_SundialsIntegrator.H:398
void SetupMRI(amrex::Real time, N_Vector y_data)
Definition: AMReX_SundialsIntegrator.H:200
std::string fast_type
Definition: AMReX_SundialsIntegrator.H:100
void map_data(std::function< void(T &)>) override
Definition: AMReX_SundialsIntegrator.H:605
Long size() const noexcept
Definition: AMReX_Vector.H:50
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition: AMReX_ParallelContext.H:70
static int fi(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:49
static int fe(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:54
static int post_fast_step(amrex::Real t, N_Vector y_data, void *user_data)
Definition: AMReX_SundialsIntegrator.H:79
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
static int post_step(amrex::Real t, N_Vector y_data, void *user_data)
Definition: AMReX_SundialsIntegrator.H:69
static int ff(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:59
static int post_stage(amrex::Real t, N_Vector y_data, void *user_data)
Definition: AMReX_SundialsIntegrator.H:64
static int post_fast_stage(amrex::Real t, N_Vector y_data, void *user_data)
Definition: AMReX_SundialsIntegrator.H:74
amrex::MultiFab *& getMFptr(N_Vector v)
Definition: AMReX_NVector_MultiFab.cpp:228
N_Vector N_VMake_MultiFab(sunindextype length, amrex::MultiFab *v_mf, ::sundials::Context *sunctx)
Definition: AMReX_NVector_MultiFab.cpp:103
N_Vector N_VNew_MultiFab(sunindextype length, const amrex::BoxArray &ba, const amrex::DistributionMapping &dm, sunindextype nComp, sunindextype nGhost, ::sundials::Context *sunctx)
Definition: AMReX_NVector_MultiFab.cpp:78
Definition: AMReX_Amr.cpp:49
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
@ make_alias
Definition: AMReX_MakeType.H:7
int nComp(FabArrayBase const &fa)
BoxArray const & boxArray(FabArrayBase const &fa)
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition: AMReX.cpp:219
const int[]
Definition: AMReX_BLProfiler.cpp:1664
Definition: AMReX_SundialsIntegrator.H:21
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> fi
Definition: AMReX_SundialsIntegrator.H:28
std::function< int(amrex::Real, N_Vector, void *)> post_fast_stage
Definition: AMReX_SundialsIntegrator.H:39
std::function< int(amrex::Real, N_Vector, void *)> post_step
Definition: AMReX_SundialsIntegrator.H:36
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> f
Definition: AMReX_SundialsIntegrator.H:24
std::function< int(amrex::Real, N_Vector, void *)> post_stage
Definition: AMReX_SundialsIntegrator.H:35
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> ff
Definition: AMReX_SundialsIntegrator.H:32
std::function< int(amrex::Real, N_Vector, void *)> post_fast_step
Definition: AMReX_SundialsIntegrator.H:40
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> fe
Definition: AMReX_SundialsIntegrator.H:29