1#ifndef AMREX_SUNDIALS_INTEGRATOR_H
2#define AMREX_SUNDIALS_INTEGRATOR_H
7#include <AMReX_Config.H>
15#include <nvector/nvector_manyvector.h>
16#include <sunnonlinsol/sunnonlinsol_fixedpoint.h>
17#include <sunlinsol/sunlinsol_spgmr.h>
18#include <arkode/arkode_arkstep.h>
19#include <arkode/arkode_mristep.h>
47namespace SundialsUserFun {
48 static int f (
amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
50 return udata->
f(t, y_data, y_rhs, user_data);
53 static int fi (
amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
55 return udata->
fi(t, y_data, y_rhs, user_data);
58 static int fe (
amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
60 return udata->
fe(t, y_data, y_rhs, user_data);
63 static int ff (
amrex::Real t, N_Vector y_data, N_Vector y_rhs,
void *user_data) {
65 return udata->
ff(t, y_data, y_rhs, user_data);
70 return udata->
post_stage(t, y_data, user_data);
75 return udata->
post_step(t, y_data, user_data);
101 std::string type =
"ERK";
104 std::string method =
"DEFAULT";
105 std::string method_e =
"DEFAULT";
106 std::string method_i =
"DEFAULT";
109 std::string fast_type =
"ERK";
110 std::string fast_method =
"DEFAULT";
113 std::string nonlinear_solver =
"Newton";
114 int max_nonlinear_iters = 0;
116 std::string fast_nonlinear_solver =
"Newton";
117 int fast_max_nonlinear_iters = 0;
120 std::string linear_solver =
"GMRES";
121 int max_linear_iters = 0;
123 std::string fast_linear_solver =
"GMRES";
124 int fast_max_linear_iters = 0;
127 bool use_ark =
false;
128 bool use_mri =
false;
137 ::sundials::Context sunctx;
140 void *arkode_mem =
nullptr;
141 SUNLinearSolver LS =
nullptr;
142 SUNNonlinearSolver NLS =
nullptr;
145 void *arkode_fast_mem =
nullptr;
146 MRIStepInnerStepper fast_stepper =
nullptr;
147 SUNLinearSolver fast_LS =
nullptr;
148 SUNNonlinearSolver fast_NLS =
nullptr;
151 bool set_stop_time =
false;
161 MRIStepInnerStepper_Free(&fast_stepper);
162 ARKStepFree(&arkode_fast_mem);
163 MRIStepFree(&arkode_mem);
164 }
else if (use_ark) {
165 ARKStepFree(&arkode_mem);
169 SUNLinSolFree(fast_LS);
171 SUNNonlinSolFree(NLS);
173 SUNNonlinSolFree(fast_NLS);
179 void initialize_parameters ()
185 pp.
query(
"method_e", method_e);
186 pp.
query(
"method_i", method_i);
188 pp.
query(
"fast_type", fast_type);
189 pp.
query(
"fast_method", fast_method);
191 if (type ==
"ERK" || type ==
"DIRK" || type ==
"IMEX-RK") {
194 else if (type ==
"EX-MRI" || type ==
"IM-MRI" || type ==
"IMEX-MRI") {
198 std::string msg(
"Unknown method type: ");
203 pp.
query(
"nonlinear_solver", nonlinear_solver);
204 pp.
query(
"max_nonlinear_iters", max_nonlinear_iters);
206 pp.
query(
"fast_nonlinear_solver", fast_nonlinear_solver);
207 pp.
query(
"fast_max_nonlinear_iters", fast_max_nonlinear_iters);
209 pp.
query(
"linear_solver", linear_solver);
210 pp.
query(
"max_linear_iters", max_linear_iters);
212 pp.
query(
"fast_linear_solver", fast_linear_solver);
213 pp.
query(
"fast_max_linear_iters", fast_max_linear_iters);
215 set_stop_time =
pp.
query(
"stop_time", stop_time);
217 pp.
query(
"max_num_steps", max_num_steps);
230 if (method !=
"DEFAULT") {
231 flag = ARKStepSetTableName(arkode_mem,
"ARKODE_DIRK_NONE", method.c_str());
235 else if (type ==
"DIRK") {
239 if (method !=
"DEFAULT") {
240 flag = ARKStepSetTableName(arkode_mem, method.c_str(),
"ARKODE_ERK_NONE");
244 else if (type ==
"IMEX-RK") {
246 << method_e <<
"\n"; }
249 if (method_e !=
"DEFAULT" && method_i !=
"DEFAULT")
251 flag = ARKStepSetTableName(arkode_mem, method_i.c_str(), method_e.c_str());
257 flag = ARKStepSetUserData(arkode_mem, &udata);
271 if (type ==
"DIRK" || type ==
"IMEX-RK") {
273 amrex::Print() <<
"Nonlinear solver: " << nonlinear_solver <<
"\n";
274 amrex::Print() <<
"Max nonlinear iters: " << max_nonlinear_iters <<
"\n";
276 if (nonlinear_solver ==
"fixed-point") {
277 NLS = SUNNonlinSol_FixedPoint(y_data, 0, sunctx);
279 flag = ARKStepSetNonlinearSolver(arkode_mem, NLS);
282 flag = ARKStepSetMaxNonlinIters(arkode_mem, max_nonlinear_iters);
285 if (nonlinear_solver ==
"Newton") {
287 amrex::Print() <<
"Linear solver: " << linear_solver <<
"\n";
288 amrex::Print() <<
"Max linear iters: " << max_linear_iters <<
"\n";
290 LS = SUNLinSol_SPGMR(y_data, SUN_PREC_NONE, max_linear_iters, sunctx);
292 flag = ARKStepSetLinearSolver(arkode_mem, LS,
nullptr);
306 flag = ARKStepSetStopTime(arkode_mem, stop_time);
311 flag = ARKStepSetMaxNumSteps(arkode_mem, max_num_steps);
321 if (fast_type ==
"ERK") {
325 if (fast_method !=
"DEFAULT") {
326 flag = ARKStepSetTableName(arkode_fast_mem,
"ARKODE_DIRK_NONE", fast_method.c_str());
330 else if (fast_type ==
"DIRK") {
334 if (fast_method !=
"DEFAULT") {
335 flag = ARKStepSetTableName(arkode_fast_mem, fast_method.c_str(),
"ARKODE_ERK_NONE");
340 amrex::Print() <<
"Fast nonlinear solver: " << fast_nonlinear_solver <<
"\n";
341 amrex::Print() <<
"Fast max nonlinear iters: " << fast_max_nonlinear_iters <<
"\n";
343 if (fast_nonlinear_solver ==
"fixed-point") {
344 fast_NLS = SUNNonlinSol_FixedPoint(y_data, 0, sunctx);
346 flag = ARKStepSetNonlinearSolver(arkode_fast_mem, fast_NLS);
349 flag = ARKStepSetMaxNonlinIters(arkode_fast_mem, fast_max_nonlinear_iters);
352 if (fast_nonlinear_solver ==
"Newton") {
354 amrex::Print() <<
"Linear solver: " << fast_linear_solver <<
"\n";
355 amrex::Print() <<
"Max linear iters: " << fast_max_linear_iters <<
"\n";
357 fast_LS = SUNLinSol_SPGMR(y_data, SUN_PREC_NONE, fast_max_linear_iters, sunctx);
359 flag = ARKStepSetLinearSolver(arkode_fast_mem, fast_LS,
nullptr);
365 flag = ARKStepSetUserData(arkode_fast_mem, &udata);
385 flag = ARKStepSetMaxNumSteps(arkode_fast_mem, max_num_steps);
389 flag = ARKStepCreateMRIStepInnerStepper(arkode_fast_mem, &fast_stepper);
393 if (type ==
"EX-MRI") {
396 fast_stepper, sunctx);
399 else if (type ==
"IM-MRI") {
402 fast_stepper, sunctx);
405 else if (type ==
"IMEX-MRI") {
408 time, y_data, fast_stepper, sunctx);
413 if (method !=
"DEFAULT") {
414 MRIStepCoupling MRIC = MRIStepCoupling_LoadTableByName(method.c_str());
416 flag = MRIStepSetCoupling(arkode_mem, MRIC);
418 MRIStepCoupling_Free(MRIC);
422 flag = MRIStepSetUserData(arkode_mem, &udata);
436 if (type ==
"IM-MRI" || type ==
"IMEX-MRI") {
438 amrex::Print() <<
"Nonlinear solver: " << nonlinear_solver <<
"\n";
439 amrex::Print() <<
"Max nonlinear iters: " << max_nonlinear_iters <<
"\n";
441 if (nonlinear_solver ==
"fixed-point") {
442 NLS = SUNNonlinSol_FixedPoint(y_data, 0, sunctx);
444 flag = MRIStepSetNonlinearSolver(arkode_mem, NLS);
447 flag = MRIStepSetMaxNonlinIters(arkode_mem, max_nonlinear_iters);
450 if (nonlinear_solver ==
"Newton") {
452 amrex::Print() <<
"Linear solver: " << linear_solver <<
"\n";
453 amrex::Print() <<
"Max linear iters: " << max_linear_iters <<
"\n";
455 LS = SUNLinSol_SPGMR(y_data, SUN_PREC_NONE, max_linear_iters, sunctx);
457 flag = MRIStepSetLinearSolver(arkode_mem, LS,
nullptr);
471 flag = MRIStepSetStopTime(arkode_mem, stop_time);
476 flag = MRIStepSetMaxNumSteps(arkode_mem, max_num_steps);
487 const int num_vecs = N_VGetNumSubvectors_ManyVector(y_data);
488 S_data.resize(num_vecs);
490 for(
int i = 0; i < num_vecs; i++)
502 auto get_length = [&](
int index) -> sunindextype {
503 auto* p_mf = &S_data[index];
504 return p_mf->nComp() * (p_mf->boxArray()).numPts();
507 sunindextype NV_len = S_data.
size();
508 N_Vector* NV_array =
new N_Vector[NV_len];
510 for (
int i = 0; i < NV_len; ++i) {
512 &S_data[i], &sunctx);
515 N_Vector y_data = N_VNew_ManyVector(NV_len, NV_array, sunctx);
525 auto get_length = [&](
int index) -> sunindextype {
526 auto* p_mf = &S_data[index];
527 return p_mf->nComp() * (p_mf->boxArray()).numPts();
530 sunindextype NV_len = S_data.
size();
531 N_Vector* NV_array =
new N_Vector[NV_len];
533 for (
int i = 0; i < NV_len; ++i) {
549 N_Vector y_data = N_VNew_ManyVector(NV_len, NV_array, sunctx);
625 ::sundials::Context old_sunctx = std::move(sunctx);
628 initialize_parameters();
630#if defined(SUNDIALS_VERSION_MAJOR) && (SUNDIALS_VERSION_MAJOR < 7)
632 sunctx = ::sundials::Context(&mpi_comm);
634 sunctx = ::sundials::Context(
nullptr);
638 sunctx = ::sundials::Context(mpi_comm);
640 sunctx = ::sundials::Context(SUN_COMM_NULL);
645 udata.
f = [&](
amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
649 unpack_vector(y_data, S_data);
652 unpack_vector(y_rhs, S_rhs);
659 udata.
fi = [&](
amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
663 unpack_vector(y_data, S_data);
666 unpack_vector(y_rhs, S_rhs);
673 udata.
fe = [&](
amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
677 unpack_vector(y_data, S_data);
680 unpack_vector(y_rhs, S_rhs);
687 udata.
ff = [&](
amrex::Real rhs_time, N_Vector y_data, N_Vector y_rhs,
691 unpack_vector(y_data, S_data);
694 unpack_vector(y_rhs, S_rhs);
705 unpack_vector(y_data, S_data);
716 unpack_vector(y_data, S_data);
727 unpack_vector(y_data, S_data);
738 unpack_vector(y_data, S_data);
745 N_Vector y_data = copy_data(S_data);
748 SetupRK(time, y_data);
752 SetupMRI(time, y_data);
764 if (type ==
"EX-MRI" || type ==
"IM-MRI" || type ==
"IMEX-MRI") {
767 MRIStepPrintAllStats(arkode_mem, stdout, SUN_OUTPUTFORMAT_TABLE);
771 ARKStepPrintAllStats(arkode_fast_mem, stdout, SUN_OUTPUTFORMAT_TABLE);
776 ARKStepPrintAllStats(arkode_mem, stdout, SUN_OUTPUTFORMAT_TABLE);
799 N_Vector y_old = wrap_data(S_old);
800 N_Vector y_new = wrap_data(S_new);
803 ARKStepReset(arkode_mem, time, y_old);
804 ARKStepSetFixedStep(arkode_mem, dt);
805 int flag = ARKStepEvolve(arkode_mem, tout, y_new, &tret, ARK_ONE_STEP);
809 MRIStepReset(arkode_mem, time, y_old);
810 MRIStepSetFixedStep(arkode_mem, dt);
811 int flag = MRIStepEvolve(arkode_mem, tout, y_new, &tret, ARK_ONE_STEP);
814 Error(
"SUNDIALS integrator type not specified.");
834 N_Vector y_out = wrap_data(S_out);
840 flag = ARKStepEvolve(arkode_mem, time_out, y_out, &time_ret, ARK_NORMAL);
850 flag = MRIStepEvolve(arkode_mem, time_out, y_out, &time_ret, ARK_NORMAL);
853 Error(
"SUNDIALS integrator type not specified.");
867 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
Long numPts() const noexcept
Returns the total number of cells contained in all boxes in the BoxArray.
Definition AMReX_BoxArray.cpp:394
int nGrow(int direction=0) const noexcept
Return the grow factor that defines the region of definition.
Definition AMReX_FabArrayBase.H:83
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:135
int nComp() const noexcept
Return number of variables (aka components) associated with each point.
Definition AMReX_FabArrayBase.H:88
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:100
Definition AMReX_IntegratorBase.H:167
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:249
amrex::Real fast_rel_tol
Relative tolerance for adaptive time stepping at the fast time scale (Real)
Definition AMReX_IntegratorBase.H:281
amrex::Real rel_tol
Relative tolerance for adaptive time stepping (Real)
Definition AMReX_IntegratorBase.H:270
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:221
amrex::Real fast_abs_tol
Absolute tolerance for adaptive time stepping at the fast time scale (Real)
Definition AMReX_IntegratorBase.H:287
amrex::Real fast_time_step
Current integrator fast time scale time step size with multirate methods (Real)
Definition AMReX_IntegratorBase.H:255
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:197
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:215
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:227
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:191
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:185
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:233
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:209
amrex::Real time_step
Current integrator time step size (Real)
Definition AMReX_IntegratorBase.H:238
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:203
amrex::Real abs_tol
Absolute tolerance for adaptive time stepping (Real)
Definition AMReX_IntegratorBase.H:275
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:40
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:353
int query(std::string_view name, bool &ref, int ival=FIRST) const
Same as querykth() but searches for the last occurrence of name.
Definition AMReX_ParmParse.cpp:1976
This class provides the user with a few print options.
Definition AMReX_Print.H:35
IntegratorBase implementation powered by SUNDIALS ARKStep/MRIStep.
Definition AMReX_SundialsIntegrator.H:96
void time_interpolate(const T &, const T &, amrex::Real, T &) override
Interpolate between SUNDIALS stages (not yet implemented for this integrator).
Definition AMReX_SundialsIntegrator.H:862
SundialsIntegrator()
Construct an uninitialized integrator; call initialize() before use.
Definition AMReX_SundialsIntegrator.H:600
void initialize(const T &S_data, const amrex::Real time=0.0)
Configure (or reconfigure) the SUNDIALS integrator for the provided state.
Definition AMReX_SundialsIntegrator.H:619
amrex::Real advance(T &S_old, T &S_new, amrex::Real time, const amrex::Real dt) override
Take a single time step of size dt starting from S_old.
Definition AMReX_SundialsIntegrator.H:794
void evolve(T &S_out, const amrex::Real time_out) override
Evolve the solution in S_out up to time_out using ARKStep/MRIStep.
Definition AMReX_SundialsIntegrator.H:829
virtual ~SundialsIntegrator()
Destroy the integrator, printing summary statistics when verbose.
Definition AMReX_SundialsIntegrator.H:761
SundialsIntegrator(const T &S_data, const amrex::Real time=0.0)
Construct and immediately configure the integrator with S_data at time time.
Definition AMReX_SundialsIntegrator.H:608
void map_data(std::function< void(T &)>) override
Apply a user-supplied mapping to every MultiFab in the integrator (unused placeholder).
Definition AMReX_SundialsIntegrator.H:867
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Long size() const noexcept
Definition AMReX_Vector.H:54
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
amrex_long Long
Definition AMReX_INT.H:30
bool IOProcessor() noexcept
Is this CPU the I/O Processor? To get the rank number, call IOProcessorNumber()
Definition AMReX_ParallelDescriptor.H:289
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:53
static int fe(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition AMReX_SundialsIntegrator.H:58
static int post_fast_step(amrex::Real t, N_Vector y_data, void *user_data)
Definition AMReX_SundialsIntegrator.H:83
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition AMReX_SundialsIntegrator.H:48
static int post_step(amrex::Real t, N_Vector y_data, void *user_data)
Definition AMReX_SundialsIntegrator.H:73
static int ff(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition AMReX_SundialsIntegrator.H:63
static int post_stage(amrex::Real t, N_Vector y_data, void *user_data)
Definition AMReX_SundialsIntegrator.H:68
static int post_fast_stage(amrex::Real t, N_Vector y_data, void *user_data)
Definition AMReX_SundialsIntegrator.H:78
int MPI_Comm
Definition AMReX_ccse-mpi.H:51
N_Vector N_VMake_MultiFab(sunindextype length, amrex::MultiFab *v_mf, ::sundials::Context *sunctx)
Wrap an existing MultiFab mf as an N_Vector without copying.
Definition AMReX_NVector_MultiFab.cpp:105
amrex::MultiFab *& getMFptr(N_Vector v)
Access the MultiFab pointer stored inside v (non-const).
Definition AMReX_NVector_MultiFab.cpp:233
N_Vector N_VNew_MultiFab(sunindextype length, const amrex::BoxArray &ba, const amrex::DistributionMapping &dm, sunindextype nComp, sunindextype nGhost, ::sundials::Context *sunctx)
Allocate a MultiFab-backed N_Vector of length vec_length.
Definition AMReX_NVector_MultiFab.cpp:80
Definition AMReX_Amr.cpp:50
@ make_alias
Definition AMReX_MakeType.H:7
BoxArray const & boxArray(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.boxArray().
Definition AMReX_FabArrayBase.cpp:2870
DistributionMapping const & DistributionMap(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.DistributionMap().
Definition AMReX_FabArrayBase.cpp:2875
int nComp(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.nComp().
Definition AMReX_FabArrayBase.cpp:2860
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:236
int Verbose() noexcept
Return the verbosity level configured via ParmParse or SetVerbose().
Definition AMReX.cpp:182
const int[]
Definition AMReX_BLProfiler.cpp:1665
User-supplied callbacks consumed by the AMReX/SUNDIALS bridge.
Definition AMReX_SundialsIntegrator.H:36
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> fi
Implicit RHS for ImEx schemes.
Definition AMReX_SundialsIntegrator.H:38
std::function< int(amrex::Real, N_Vector, void *)> post_fast_stage
Hook for MRI fast stages.
Definition AMReX_SundialsIntegrator.H:43
std::function< int(amrex::Real, N_Vector, void *)> post_step
Hook invoked after each time step.
Definition AMReX_SundialsIntegrator.H:42
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> f
ERK/DIRK RHS or MRI slow RHS.
Definition AMReX_SundialsIntegrator.H:37
std::function< int(amrex::Real, N_Vector, void *)> post_stage
Hook invoked after each stage.
Definition AMReX_SundialsIntegrator.H:41
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> ff
MRI fast-scale RHS.
Definition AMReX_SundialsIntegrator.H:40
std::function< int(amrex::Real, N_Vector, void *)> post_fast_step
Hook for MRI fast steps.
Definition AMReX_SundialsIntegrator.H:44
std::function< int(amrex::Real, N_Vector, N_Vector, void *)> fe
Explicit RHS for ImEx schemes.
Definition AMReX_SundialsIntegrator.H:39