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 <functional>
14#include <type_traits>
15
16namespace amrex {
17
18template<class T, typename Tv = void> struct IntegratorOps;
19
20#if defined(AMREX_PARTICLES)
21template<class T>
22struct IntegratorOps<T, std::enable_if_t<std::is_base_of_v<amrex::ParticleContainerBase, T> > >
23{
24
25 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other)
26 {
27 // Emplace a new T in V with the same size as Other and get a reference
28 V.emplace_back(std::make_unique<T>(Other.Geom(0), Other.ParticleDistributionMap(0), Other.ParticleBoxArray(0)));
29 T& pc = *V[V.size()-1];
30
31 // We want the particles to have all the same position, cpu, etc.
32 // as in Other, so do a copy from Other to our new particle container.
33 Copy(pc, Other);
34 }
35
36 static void Copy (T& Y, const T& Other)
37 {
38 // Copy the contents of Other into Y
39 const bool local = true;
40 Y.copyParticles(Other, local);
41 }
42
43 static void Saxpy (T& Y, const amrex::Real a, T& X)
44 {
45 // Calculate Y += a * X using a particle-level saxpy function supplied by the particle container T
48
49 int lev = 0;
50 TParIter pty(Y, lev);
51 TParIter ptx(X, lev);
52
53 auto checkValid = [&]() -> bool {
54 bool pty_v = pty.isValid();
55 bool ptx_v = ptx.isValid();
56 AMREX_ASSERT(pty_v == ptx_v);
57 return pty_v && ptx_v;
58 };
59
60 auto ptIncrement = [&](){ ++pty; ++ptx; };
61
62#ifdef AMREX_USE_OMP
63#pragma omp parallel if (Gpu::notInLaunchRegion())
64#endif
65 for (; checkValid(); ptIncrement())
66 {
67 const int npy = pty.numParticles();
68 const int npx = ptx.numParticles();
69 AMREX_ALWAYS_ASSERT(npy == npx);
70
71 ParticleType* psy = &(pty.GetArrayOfStructs()[0]);
72 ParticleType* psx = &(ptx.GetArrayOfStructs()[0]);
73
74 auto particle_apply_rhs = T::particle_apply_rhs;
75
76 amrex::ParallelFor ( npy, [=] AMREX_GPU_DEVICE (int i) {
77 ParticleType& py = psy[i];
78 const ParticleType& px = psx[i];
79 particle_apply_rhs(py, a, px);
80 });
81 }
82 }
83
84};
85#endif
86
87template<class T>
88struct IntegratorOps<T, std::enable_if_t<std::is_same_v<amrex::Vector<amrex::MultiFab>, T> > >
89{
90
91 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other, bool Grow = false)
92 {
93 // Emplace a new T in V with the same size as Other
94 V.emplace_back(std::make_unique<T>());
95 for (auto const& other_mf : Other) {
96 IntVect nGrow = Grow ? other_mf.nGrowVect() : IntVect(0);
97 V.back()->push_back(amrex::MultiFab(other_mf.boxArray(), other_mf.DistributionMap(), other_mf.nComp(), nGrow));
98 }
99 }
100
101 static void Copy (T& Y, const T& Other, const Vector<int>& scomp={}, const Vector<int>& ncomp={}, bool Grow = true)
102 {
103 // Copy the contents of Other into Y
104 const int size = Y.size();
105 bool specify_components = !scomp.empty() && ncomp.size() == scomp.size();
106 for (int i = 0; i < size; ++i) {
107 IntVect nGrow = Grow ? Other[i].nGrowVect() : IntVect(0);
108 const int iscomp = specify_components ? scomp[i] : 0;
109 const int incomp = specify_components ? ncomp[i] : Other[i].nComp();
110 if (incomp > 0) {
111 amrex::MultiFab::Copy(Y[i], Other[i], iscomp, iscomp, incomp, nGrow);
112 }
113 }
114 }
115
116 static void Saxpy (T& Y, const amrex::Real a, const T& X, const Vector<int>& scomp={}, const Vector<int>& ncomp={}, bool Grow = false)
117 {
118 // Calculate Y += a * X
119 const int size = Y.size();
120 bool specify_components = !scomp.empty() && ncomp.size() == scomp.size();
121 for (int i = 0; i < size; ++i) {
122 IntVect nGrow = Grow ? X[i].nGrowVect() : IntVect(0);
123 const int iscomp = specify_components ? scomp[i] : 0;
124 const int incomp = specify_components ? ncomp[i] : X[i].nComp();
125 if (incomp > 0) {
126 amrex::MultiFab::Saxpy(Y[i], a, X[i], iscomp, iscomp, incomp, nGrow);
127 }
128 }
129 }
130
131};
132
133template<class T>
134struct IntegratorOps<T, std::enable_if_t<std::is_same_v<amrex::MultiFab, T> > >
135{
136
137 static void CreateLike (amrex::Vector<std::unique_ptr<T> >& V, const T& Other, bool Grow = false)
138 {
139 // Emplace a new T in V with the same size as Other
140 IntVect nGrow = Grow ? Other.nGrowVect() : IntVect(0);
141 V.emplace_back(std::make_unique<T>(Other.boxArray(), Other.DistributionMap(), Other.nComp(), nGrow));
142 }
143
144 static void Copy (T& Y, const T& Other, const int scomp=0, const int ncomp=-1, bool Grow = true)
145 {
146 // Copy the contents of Other into Y
147 IntVect nGrow = Grow ? Other.nGrowVect() : IntVect(0);
148 const int mf_ncomp = ncomp > 0 ? ncomp : Other.nComp();
149 amrex::MultiFab::Copy(Y, Other, scomp, scomp, mf_ncomp, nGrow);
150 }
151
152 static void Saxpy (T& Y, const amrex::Real a, const T& X, const int scomp=0, const int ncomp=-1, bool Grow = false)
153 {
154 // Calculate Y += a * X
155 IntVect nGrow = Grow ? X.nGrowVect() : IntVect(0);
156 const int mf_ncomp = ncomp > 0 ? ncomp : X.nComp();
157 amrex::MultiFab::Saxpy(Y, a, X, scomp, scomp, mf_ncomp, nGrow);
158 }
159
160};
161
162template<class T>
164{
165private:
167 {
168 amrex::ParmParse pp("integration");
169 pp.query("use_adaptive_time_step", use_adaptive_time_step);
170 pp.query("time_step", time_step);
171 pp.query("use_adaptive_fast_time_step", use_adaptive_fast_time_step);
172 pp.query("fast_time_step", fast_time_step);
173 pp.query("rel_tol", rel_tol);
174 pp.query("abs_tol", abs_tol);
175 pp.query("fast_rel_tol", fast_rel_tol);
176 pp.query("fast_abs_tol", fast_abs_tol);
177 }
178protected:
182 std::function<void(T& rhs, T& state, const amrex::Real time)> Rhs;
183
188 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsIm;
189
194 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsEx;
195
200 std::function<void(T& rhs, T& state, const amrex::Real time)> RhsFast;
201
206 std::function<void (T&, amrex::Real)> post_stage_action;
207
212 std::function<void (T&, amrex::Real)> post_step_action;
213
218 std::function<void (T&, amrex::Real)> post_fast_stage_action;
219
224 std::function<void (T&, amrex::Real)> post_fast_step_action;
225
231
235 amrex::Real time_step;
236
241
247
252 amrex::Real fast_time_step;
253
257 amrex::Long num_steps = 0;
258
262 int max_steps = 500;
263
267 amrex::Real rel_tol = 1.0e-4;
268
272 amrex::Real abs_tol = 1.0e-9;
273
278 amrex::Real fast_rel_tol = 1.0e-4;
279
284 amrex::Real fast_abs_tol = 1.0e-9;
285
286
287public:
291
292 IntegratorBase (const T& /* S_data */) {
294 }
295
296 virtual ~IntegratorBase () = default;
297
298 void set_rhs (std::function<void(T&, T&, const amrex::Real)> F)
299 {
300 Rhs = F;
301 }
302
303 void set_imex_rhs (std::function<void(T&, T&, const amrex::Real)> Fi,
304 std::function<void(T&, T&, const amrex::Real)> Fe)
305 {
306 RhsIm = Fi;
307 RhsEx = Fe;
308 }
309
310 void set_fast_rhs (std::function<void(T&, T&, const amrex::Real)> F)
311 {
312 RhsFast = F;
313 }
314
315 void set_post_stage_action (std::function<void (T&, amrex::Real)> A)
316 {
318 }
319
320 void set_post_step_action (std::function<void (T&, amrex::Real)> A)
321 {
323 }
324
325 void set_post_fast_stage_action (std::function<void (T&, amrex::Real)> A)
326 {
328 }
329
330 void set_post_fast_step_action (std::function<void (T&, amrex::Real)> A)
331 {
333 }
334
335 amrex::Real get_time_step ()
336 {
337 return time_step;
338 }
339
340 void set_time_step (amrex::Real dt)
341 {
342 time_step = dt;
344 }
345
347 {
349 }
350
351 void set_fast_time_step (amrex::Real dt)
352 {
353 fast_time_step = dt;
355 }
356
361
362 void set_max_steps (int steps)
363 {
364 max_steps = steps;
365 }
366
367 void set_tolerances (amrex::Real rtol, amrex::Real atol)
368 {
369 rel_tol = rtol;
370 abs_tol = atol;
371 }
372
373 void set_fast_tolerances (amrex::Real rtol, amrex::Real atol)
374 {
375 fast_rel_tol = rtol;
376 fast_abs_tol = atol;
377 }
378
383 virtual amrex::Real advance (T& S_old, T& S_new, amrex::Real time,
384 amrex::Real dt) = 0;
385
389 virtual void evolve (T& S_out, const amrex::Real time_out) = 0;
390
391 virtual void time_interpolate (const T& S_new, const T& S_old,
392 amrex::Real timestep_fraction, T& data) = 0;
393
394 virtual void map_data (std::function<void(T&)> Map) = 0;
395};
396
397}
398
399#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:164
void set_post_step_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:320
void set_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_IntegratorBase.H:298
void set_adaptive_step()
Definition AMReX_IntegratorBase.H:346
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:246
amrex::Real fast_rel_tol
Relative tolerance for adaptive time stepping at the fast time scale (Real)
Definition AMReX_IntegratorBase.H:278
amrex::Real rel_tol
Relative tolerance for adaptive time stepping (Real)
Definition AMReX_IntegratorBase.H:267
void initialize_base_parameters()
Definition AMReX_IntegratorBase.H:166
void set_fast_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_IntegratorBase.H:373
void set_post_fast_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:325
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:218
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:357
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:335
amrex::Real fast_abs_tol
Absolute tolerance for adaptive time stepping at the fast time scale (Real)
Definition AMReX_IntegratorBase.H:284
amrex::Real fast_time_step
Current integrator fast time scale time step size with multirate methods (Real)
Definition AMReX_IntegratorBase.H:252
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:194
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:212
void set_post_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_IntegratorBase.H:315
IntegratorBase()
Definition AMReX_IntegratorBase.H:288
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:224
int max_steps
Max number of internal steps before an error is returned (Long)
Definition AMReX_IntegratorBase.H:262
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:188
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:330
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:303
void set_max_steps(int steps)
Definition AMReX_IntegratorBase.H:362
void set_time_step(amrex::Real dt)
Definition AMReX_IntegratorBase.H:340
void set_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_IntegratorBase.H:367
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:182
virtual ~IntegratorBase()=default
amrex::Long num_steps
Number of integrator time steps (Long)
Definition AMReX_IntegratorBase.H:257
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:230
void set_fast_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_IntegratorBase.H:310
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:206
void set_fast_time_step(amrex::Real dt)
Definition AMReX_IntegratorBase.H:351
IntegratorBase(const T &)
Definition AMReX_IntegratorBase.H:292
amrex::Real time_step
Current integrator time step size (Real)
Definition AMReX_IntegratorBase.H:235
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:200
amrex::Real previous_time_step
Step size of the last completed step (Real)
Definition AMReX_IntegratorBase.H:240
amrex::Real abs_tol
Absolute tolerance for adaptive time stepping (Real)
Definition AMReX_IntegratorBase.H:272
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:38
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:115
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:343
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:1393
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
Long size() const noexcept
Definition AMReX_Vector.H:53
Definition AMReX_Amr.cpp:49
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:191
void Copy(FabArray< DFAB > &dst, FabArray< SFAB > const &src, int srccomp, int dstcomp, int numcomp, int nghost)
Definition AMReX_FabArray.H:180
IntVectND< 3 > IntVect
Definition AMReX_BaseFwd.H:30
static void CreateLike(amrex::Vector< std::unique_ptr< T > > &V, const T &Other)
Definition AMReX_IntegratorBase.H:25
static void Saxpy(T &Y, const amrex::Real a, T &X)
Definition AMReX_IntegratorBase.H:43
static void Copy(T &Y, const T &Other)
Definition AMReX_IntegratorBase.H:36
static void CreateLike(amrex::Vector< std::unique_ptr< T > > &V, const T &Other, bool Grow=false)
Definition AMReX_IntegratorBase.H:137
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:152
static void Copy(T &Y, const T &Other, const int scomp=0, const int ncomp=-1, bool Grow=true)
Definition AMReX_IntegratorBase.H:144
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:116
static void CreateLike(amrex::Vector< std::unique_ptr< T > > &V, const T &Other, bool Grow=false)
Definition AMReX_IntegratorBase.H:91
static void Copy(T &Y, const T &Other, const Vector< int > &scomp={}, const Vector< int > &ncomp={}, bool Grow=true)
Definition AMReX_IntegratorBase.H:101
Definition AMReX_IntegratorBase.H:18
The struct used to store particles.
Definition AMReX_Particle.H:402