Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_IntegratorBase.H
Go to the documentation of this file.
1#ifndef AMREX_INTEGRATOR_BASE_H
2#define AMREX_INTEGRATOR_BASE_H
3#include <AMReX_Config.H>
4#include <AMReX_REAL.H>
5#include <AMReX_Vector.H>
6#include <AMReX_MultiFab.H>
7#include <AMReX_ParmParse.H>
8
9#if defined(AMREX_PARTICLES)
10#include <AMReX_Particles.H>
11#endif
12
13#include <concepts>
14#include <functional>
15
16namespace amrex {
17
18template<class T> struct IntegratorOps;
19
20#if defined(AMREX_PARTICLES)
21template<class T>
22requires (std::is_base_of_v<ParticleContainerBase, T>)
24{
25
26 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other, bool /* Grow */ = false)
27 {
28 // Emplace a new T in V with the same size as Other and get a reference
29 V.emplace_back(std::make_unique<T>(Other.Geom(0), Other.ParticleDistributionMap(0), Other.ParticleBoxArray(0)));
30 T& pc = *V[V.size()-1];
31
32 // We want the particles to have all the same position, cpu, etc.
33 // as in Other, so do a copy from Other to our new particle container.
34 Copy(pc, Other);
35 }
36
37 static void Copy (T& Y, const T& Other)
38 {
39 // Copy the contents of Other into Y
40 const bool local = true;
41 Y.copyParticles(Other, local);
42 }
43
44 static void Saxpy (T& Y, const amrex::Real a, T& X)
45 {
46 // Calculate Y += a * X using a particle-level saxpy function supplied by the particle container T
49
50 int lev = 0;
51 TParIter pty(Y, lev);
52 TParIter ptx(X, lev);
53
54 auto checkValid = [&]() -> bool {
55 bool pty_v = pty.isValid();
56 bool ptx_v = ptx.isValid();
57 AMREX_ASSERT(pty_v == ptx_v);
58 return pty_v && ptx_v;
59 };
60
61 auto ptIncrement = [&](){ ++pty; ++ptx; };
62
63#ifdef AMREX_USE_OMP
64#pragma omp parallel if (Gpu::notInLaunchRegion())
65#endif
66 for (; checkValid(); ptIncrement())
67 {
68 const int npy = pty.numParticles();
69 const int npx = ptx.numParticles();
70 AMREX_ALWAYS_ASSERT(npy == npx);
71
72 ParticleType* psy = &(pty.GetArrayOfStructs()[0]);
73 ParticleType* psx = &(ptx.GetArrayOfStructs()[0]);
74
75 auto particle_apply_rhs = T::particle_apply_rhs;
76
77 amrex::ParallelFor ( npy, [=] AMREX_GPU_DEVICE (int i) {
78 ParticleType& py = psy[i];
79 const ParticleType& px = psx[i];
80 particle_apply_rhs(py, a, px);
81 });
82 }
83 }
84
85};
86#endif
87
88template<class T>
89requires (std::same_as<T, Vector<MultiFab>>)
90struct IntegratorOps<T>
91{
92
93 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other, bool Grow = false)
94 {
95 // Emplace a new T in V with the same size as Other
96 V.emplace_back(std::make_unique<T>());
97 for (auto const& other_mf : Other) {
98 IntVect nGrow = Grow ? other_mf.nGrowVect() : IntVect(0);
99 V.back()->push_back(amrex::MultiFab(other_mf.boxArray(), other_mf.DistributionMap(), other_mf.nComp(), nGrow));
100 }
101 }
102
103 static void Copy (T& Y, const T& Other, const Vector<int>& scomp={}, const Vector<int>& ncomp={}, bool Grow = true)
104 {
105 // Copy the contents of Other into Y
106 const int size = Y.size();
107 bool specify_components = !scomp.empty() && ncomp.size() == scomp.size();
108 for (int i = 0; i < size; ++i) {
109 IntVect nGrow = Grow ? Other[i].nGrowVect() : IntVect(0);
110 const int iscomp = specify_components ? scomp[i] : 0;
111 const int incomp = specify_components ? ncomp[i] : Other[i].nComp();
112 if (incomp > 0) {
113 amrex::MultiFab::Copy(Y[i], Other[i], iscomp, iscomp, incomp, nGrow);
114 }
115 }
116 }
117
118 static void Saxpy (T& Y, const amrex::Real a, const T& X, const Vector<int>& scomp={}, const Vector<int>& ncomp={}, bool Grow = false)
119 {
120 // Calculate Y += a * X
121 const int size = Y.size();
122 bool specify_components = !scomp.empty() && ncomp.size() == scomp.size();
123 for (int i = 0; i < size; ++i) {
124 IntVect nGrow = Grow ? X[i].nGrowVect() : IntVect(0);
125 const int iscomp = specify_components ? scomp[i] : 0;
126 const int incomp = specify_components ? ncomp[i] : X[i].nComp();
127 if (incomp > 0) {
128 amrex::MultiFab::Saxpy(Y[i], a, X[i], iscomp, iscomp, incomp, nGrow);
129 }
130 }
131 }
132
133};
134
135template<class T>
136requires (std::same_as<T, MultiFab>)
137struct IntegratorOps<T>
138{
139
140 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other, bool Grow = false)
141 {
142 // Emplace a new T in V with the same size as Other
143 IntVect nGrow = Grow ? Other.nGrowVect() : IntVect(0);
144 V.emplace_back(std::make_unique<T>(Other.boxArray(), Other.DistributionMap(), Other.nComp(), nGrow));
145 }
146
147 static void Copy (T& Y, const T& Other, const int scomp=0, const int ncomp=-1, bool Grow = true)
148 {
149 // Copy the contents of Other into Y
150 IntVect nGrow = Grow ? Other.nGrowVect() : IntVect(0);
151 const int mf_ncomp = ncomp > 0 ? ncomp : Other.nComp();
152 amrex::MultiFab::Copy(Y, Other, scomp, scomp, mf_ncomp, nGrow);
153 }
154
155 static void Saxpy (T& Y, const amrex::Real a, const T& X, const int scomp=0, const int ncomp=-1, bool Grow = false)
156 {
157 // Calculate Y += a * X
158 IntVect nGrow = Grow ? X.nGrowVect() : IntVect(0);
159 const int mf_ncomp = ncomp > 0 ? ncomp : X.nComp();
160 amrex::MultiFab::Saxpy(Y, a, X, scomp, scomp, mf_ncomp, nGrow);
161 }
162
163};
164
165template<class T>
167{
168private:
169 void initialize_base_parameters ()
170 {
171 amrex::ParmParse pp("integration");
172 pp.query("use_adaptive_time_step", use_adaptive_time_step);
173 pp.query("time_step", time_step);
174 pp.query("use_adaptive_fast_time_step", use_adaptive_fast_time_step);
175 pp.query("fast_time_step", fast_time_step);
176 pp.query("rel_tol", rel_tol);
177 pp.query("abs_tol", abs_tol);
178 pp.query("fast_rel_tol", fast_rel_tol);
179 pp.query("fast_abs_tol", fast_abs_tol);
180 }
181protected:
185 std::function<void(T& rhs, T& state, const amrex::Real time)> Rhs;
186
191 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsIm;
192
197 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsEx;
198
203 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsFast;
204
209 std::function<void (T&, amrex::Real)> post_stage_action;
210
215 std::function<void (T&, amrex::Real)> post_step_action;
216
221 std::function<void (T&, amrex::Real)> post_fast_stage_action;
222
227 std::function<void (T&, amrex::Real)> post_fast_step_action;
228
234
239
244
250
256
261
265 int max_steps = 500;
266
271
276
282
288
289
290public:
292 initialize_base_parameters();
293 }
294
295 IntegratorBase (const T& /* S_data */) {
296 initialize_base_parameters();
297 }
298
299 virtual ~IntegratorBase () = default;
300
301 void set_rhs (std::function<void(T&, T&, const amrex::Real)> F)
302 {
303 Rhs = F;
304 }
305
306 void set_imex_rhs (std::function<void(T&, T&, const amrex::Real)> Fi,
307 std::function<void(T&, T&, const amrex::Real)> Fe)
308 {
309 RhsIm = Fi;
310 RhsEx = Fe;
311 }
312
313 void set_fast_rhs (std::function<void(T&, T&, const amrex::Real)> F)
314 {
315 RhsFast = F;
316 }
317
318 void set_post_stage_action (std::function<void (T&, amrex::Real)> A)
319 {
321 }
322
323 void set_post_step_action (std::function<void (T&, amrex::Real)> A)
324 {
326 }
327
328 void set_post_fast_stage_action (std::function<void (T&, amrex::Real)> A)
329 {
331 }
332
333 void set_post_fast_step_action (std::function<void (T&, amrex::Real)> A)
334 {
336 }
337
339 {
340 return time_step;
341 }
342
344 {
345 time_step = dt;
347 }
348
350 {
352 }
353
355 {
356 fast_time_step = dt;
358 }
359
364
365 void set_max_steps (int steps)
366 {
367 max_steps = steps;
368 }
369
371 {
372 rel_tol = rtol;
373 abs_tol = atol;
374 }
375
377 {
378 fast_rel_tol = rtol;
379 fast_abs_tol = atol;
380 }
381
386 virtual amrex::Real advance (T& S_old, T& S_new, amrex::Real time,
387 amrex::Real dt) = 0;
388
392 virtual void evolve (T& S_out, const amrex::Real time_out) = 0;
393
394 virtual void time_interpolate (const T& S_new, const T& S_old,
395 amrex::Real timestep_fraction, T& data) = 0;
396
397 virtual void map_data (std::function<void(T&)> Map) = 0;
398};
399
400}
401
402#endif
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
amrex::ParmParse pp
Input file parser instance for the given namespace.
Definition AMReX_HypreIJIface.cpp:15
Definition AMReX_IntegratorBase.H:167
void set_post_step_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:323
void set_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_IntegratorBase.H:301
void set_adaptive_step()
Definition AMReX_IntegratorBase.H:349
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
void set_fast_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_IntegratorBase.H:376
void set_post_fast_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:328
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
virtual void time_interpolate(const T &S_new, const T &S_old, amrex::Real timestep_fraction, T &data)=0
void set_adaptive_fast_step()
Definition AMReX_IntegratorBase.H:360
virtual amrex::Real advance(T &S_old, T &S_new, amrex::Real time, amrex::Real dt)=0
Take a single time step from (time, S_old) to (time + dt, S_new) with the given step size.
amrex::Real get_time_step()
Definition AMReX_IntegratorBase.H:338
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
void set_post_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:318
IntegratorBase()
Definition AMReX_IntegratorBase.H:291
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
int max_steps
Max number of internal steps before an error is returned (Long)
Definition AMReX_IntegratorBase.H:265
virtual void evolve(T &S_out, const amrex::Real time_out)=0
Evolve the current (internal) integrator state to time_out.
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
virtual void map_data(std::function< void(T &)> Map)=0
void set_post_fast_step_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:333
void set_imex_rhs(std::function< void(T &, T &, const amrex::Real)> Fi, std::function< void(T &, T &, const amrex::Real)> Fe)
Definition AMReX_IntegratorBase.H:306
void set_max_steps(int steps)
Definition AMReX_IntegratorBase.H:365
void set_time_step(amrex::Real dt)
Definition AMReX_IntegratorBase.H:343
void set_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_IntegratorBase.H:370
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
virtual ~IntegratorBase()=default
amrex::Long num_steps
Number of integrator time steps (Long)
Definition AMReX_IntegratorBase.H:260
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
void set_fast_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_IntegratorBase.H:313
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
void set_fast_time_step(amrex::Real dt)
Definition AMReX_IntegratorBase.H:354
IntegratorBase(const T &)
Definition AMReX_IntegratorBase.H:295
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 previous_time_step
Step size of the last completed step (Real)
Definition AMReX_IntegratorBase.H:243
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 Saxpy(MultiFab &dst, Real a, const MultiFab &src, int srccomp, int dstcomp, int numcomp, int nghost)
dst += a*src
Definition AMReX_MultiFab.cpp:292
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
Definition AMReX_ParIter.H:118
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:351
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:1948
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:79
amrex_long Long
Definition AMReX_INT.H:30
Definition AMReX_Amr.cpp:50
void Copy(FabArray< DFAB > &dst, FabArray< SFAB > const &src, int srccomp, int dstcomp, int numcomp, int nghost)
Definition AMReX_FabArray.H:180
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
static void Copy(T &Y, const T &Other, const Vector< int > &scomp={}, const Vector< int > &ncomp={}, bool Grow=true)
Definition AMReX_IntegratorBase.H:103
static void Saxpy(T &Y, const amrex::Real a, const T &X, const int scomp=0, const int ncomp=-1, bool Grow=false)
Definition AMReX_IntegratorBase.H:155
static void Saxpy(T &Y, const amrex::Real a, T &X)
Definition AMReX_IntegratorBase.H:44
static void Copy(T &Y, const T &Other)
Definition AMReX_IntegratorBase.H:37
static void CreateLike(amrex::Vector< std::unique_ptr< T > > &V, const T &Other, bool Grow=false)
Definition AMReX_IntegratorBase.H:93
static void Copy(T &Y, const T &Other, const int scomp=0, const int ncomp=-1, bool Grow=true)
Definition AMReX_IntegratorBase.H:147
static void CreateLike(amrex::Vector< std::unique_ptr< T > > &V, const T &Other, bool=false)
Definition AMReX_IntegratorBase.H:26
static void Saxpy(T &Y, const amrex::Real a, const T &X, const Vector< int > &scomp={}, const Vector< int > &ncomp={}, bool Grow=false)
Definition AMReX_IntegratorBase.H:118
Definition AMReX_IntegratorBase.H:18
The struct used to store particles.
Definition AMReX_Particle.H:405