Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_TimeIntegrator.H
Go to the documentation of this file.
1#ifndef AMREX_TIME_INTEGRATOR_H
2#define AMREX_TIME_INTEGRATOR_H
3#include <AMReX_REAL.H>
4#include <AMReX_Vector.H>
5#include <AMReX_ParmParse.H>
9
10#ifdef AMREX_USE_SUNDIALS
12#endif
13
14#include <functional>
15
16namespace amrex {
17
18enum struct IntegratorTypes {
19 ForwardEuler = 0,
22};
23
24template<class T>
26{
27private:
28 std::unique_ptr<IntegratorBase<T> > integrator_ptr;
29
30 IntegratorTypes read_parameters ()
31 {
32 amrex::ParmParse pp("integration");
33
34 int integrator_type;
35 std::string integrator_str;
36 pp.get("type", integrator_str);
37
38 if (integrator_str == "ForwardEuler") {
39 integrator_type = static_cast<int>(IntegratorTypes::ForwardEuler);
40 } else if (integrator_str == "RungeKutta") {
41 integrator_type = static_cast<int>(IntegratorTypes::ExplicitRungeKutta);
42 } else if (integrator_str == "SUNDIALS") {
43 integrator_type = static_cast<int>(IntegratorTypes::Sundials);
44 } else {
45 try {
46 integrator_type = std::stoi(integrator_str, nullptr);
47 } catch (const std::invalid_argument& ia) {
48 Print() << "Invalid integration.type: " << ia.what() << '\n';
49 Error("Failed to initialize AMReX TimeIntegrator class.");
50 }
51
52 AMREX_ALWAYS_ASSERT(integrator_type >= static_cast<int>(IntegratorTypes::ForwardEuler) &&
53 integrator_type <= static_cast<int>(IntegratorTypes::Sundials));
54 }
55
56#ifndef AMREX_USE_SUNDIALS
57 if (integrator_type == static_cast<int>(IntegratorTypes::Sundials)) {
58 Error("AMReX has not been compiled with SUNDIALS. Recompile with USE_SUNDIALS=TRUE.");
59 }
60#endif
61
62 return static_cast<IntegratorTypes>(integrator_type);
63 }
64
65 void set_default_functions ()
66 {
67 // Abort if the RHS function is called but was not set
68 set_rhs([](T& /* S_rhs */, T& /* S_data */, const amrex::Real /* time */) { amrex::Abort("RHS function not set!"); });
69 set_imex_rhs([](T& /* S_rhs */, T& /* S_data */, const amrex::Real /* time */) { amrex::Abort("Implicit RHS function not set!"); },
70 [](T& /* S_rhs */, T& /* S_data */, const amrex::Real /* time */) { amrex::Abort("Explicit RHS function not set!"); } );
71 set_fast_rhs([](T& /* S_rhs */, T& /* S_data */, const amrex::Real /* time */) { amrex::Abort("Fast RHS function not set!"); } );
72
73 // In general, the following functions can be used to fill BCs. Which
74 // function to set will depend on the method type and intended use case
75
76 // By default, do nothing after a stage or step
77 set_post_stage_action([](T& /* S_data */, const amrex::Real /* time */){});
78 set_post_step_action([](T& /* S_data */, const amrex::Real /* time */){});
79
80 // By default, do nothing after a stage or step
81 set_post_fast_stage_action([](T& /* S_data */, const amrex::Real /* time */){});
82 set_post_fast_step_action([](T& /* S_data */, const amrex::Real /* time */){});
83 }
84
85public:
86
87 TimeIntegrator () = default;
88
89 TimeIntegrator (IntegratorTypes integrator_type, const T& S_data, const amrex::Real time = 0.0)
90 {
91 // initialize the integrator class corresponding to the desired type
92 initialize_integrator(integrator_type, S_data, time);
93 }
94
95 TimeIntegrator (const T& S_data, const amrex::Real time = 0.0)
96 {
97 // initialize the integrator class corresponding to the input parameter selection
98 IntegratorTypes integrator_type = read_parameters();
99 initialize_integrator(integrator_type, S_data, time);
100 }
101
102 virtual ~TimeIntegrator () {}
103
109 void initialize_integrator (IntegratorTypes integrator_type, const T& S_data, const amrex::Real time = 0.0)
110 {
111 switch (integrator_type)
112 {
114 integrator_ptr = std::make_unique<FEIntegrator<T> >(S_data, time);
115 break;
117 integrator_ptr = std::make_unique<RKIntegrator<T> >(S_data, time);
118 break;
119#ifdef AMREX_USE_SUNDIALS
121 integrator_ptr = std::make_unique<SundialsIntegrator<T> >(S_data, time);
122 break;
123#endif
124 default:
125 amrex::Error("integrator type did not match a valid integrator type.");
126 break;
127 }
128
129 // initialize functions to do nothing
130 set_default_functions();
131 }
132
133 void set_rhs (std::function<void(T&, T&, const amrex::Real)> F)
134 {
135 integrator_ptr->set_rhs(F);
136 }
137
138 void set_imex_rhs (std::function<void(T&, T&, const amrex::Real)> Fi,
139 std::function<void(T&, T&, const amrex::Real)> Fe)
140 {
141 integrator_ptr->set_imex_rhs(Fi, Fe);
142 }
143
144 void set_fast_rhs (std::function<void(T&, T&, const amrex::Real)> F)
145 {
146 integrator_ptr->set_fast_rhs(F);
147 }
148
149 void set_post_stage_action (std::function<void (T&, amrex::Real)> A)
150 {
151 integrator_ptr->set_post_stage_action(A);
152 }
153
154 void set_post_step_action (std::function<void (T&, amrex::Real)> A)
155 {
156 integrator_ptr->set_post_step_action(A);
157 }
158
159 void set_post_fast_stage_action (std::function<void (T&, amrex::Real)> A)
160 {
161 integrator_ptr->set_post_fast_stage_action(A);
162 }
163
164 void set_post_fast_step_action (std::function<void (T&, amrex::Real)> A)
165 {
166 integrator_ptr->set_post_fast_step_action(A);
167 }
168
170 {
171 return integrator_ptr->get_time_step();
172 }
173
175 {
176 integrator_ptr->set_time_step(dt);
177 }
178
180 {
181 integrator_ptr->set_adaptive_step();
182 }
183
185 {
186 integrator_ptr->set_fast_time_step(dt);
187 }
188
190 {
191 integrator_ptr->set_adaptive_fast_step();
192 }
193
194 void set_max_steps (int steps)
195 {
196 integrator_ptr->set_max_steps(steps);
197 }
198
200 {
201 integrator_ptr->set_tolerances(rtol, atol);
202 }
203
205 {
206 integrator_ptr->set_fast_tolerances(rtol, atol);
207 }
208
209 void advance (T& S_old, T& S_new, amrex::Real time, const amrex::Real dt)
210 {
211 integrator_ptr->advance(S_old, S_new, time, dt);
212 }
213
214 void evolve (T& S_out, const amrex::Real time_out)
215 {
216 integrator_ptr->evolve(S_out, time_out);
217 }
218
219 void integrate (T& S_old, T& S_new, amrex::Real start_time, const amrex::Real start_timestep,
220 const amrex::Real end_time, const int start_step, const int max_steps)
221 {
222 amrex::Real m_time = start_time;
223 amrex::Real m_timestep = start_timestep;
224 bool stop = false;
225 for (int m_step_number = start_step; m_step_number < max_steps && !stop; ++m_step_number)
226 {
227 if (end_time - m_time < m_timestep) {
228 m_timestep = end_time - m_time;
229 stop = true;
230 }
231
232 if (m_step_number > start_step) {
233 std::swap(S_old, S_new);
234 }
235
236 // Call the time integrator advance
237 integrator_ptr->advance(S_old, S_new, m_time, m_timestep);
238
239 // Update our time variable
240 m_time += m_timestep;
241 }
242 }
243
244 void time_interpolate (const T& S_new, const T& S_old, amrex::Real timestep_fraction, T& data)
245 {
246 integrator_ptr->time_interpolate(S_new, S_old, timestep_fraction, data);
247 }
248
249 void map_data (std::function<void(T&)> Map)
250 {
251 integrator_ptr->map_data(Map);
252 }
253};
254
255}
256
257#endif
#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
Parse Parameters From Command Line and Input Files.
Definition AMReX_ParmParse.H:353
void get(std::string_view name, bool &ref, int ival=FIRST) const
Same as getkth() but searches for the last occurrence of name.
Definition AMReX_ParmParse.cpp:1959
This class provides the user with a few print options.
Definition AMReX_Print.H:35
Definition AMReX_TimeIntegrator.H:26
void set_adaptive_step()
Definition AMReX_TimeIntegrator.H:179
void set_adaptive_fast_step()
Definition AMReX_TimeIntegrator.H:189
void integrate(T &S_old, T &S_new, amrex::Real start_time, const amrex::Real start_timestep, const amrex::Real end_time, const int start_step, const int max_steps)
Definition AMReX_TimeIntegrator.H:219
TimeIntegrator(const T &S_data, const amrex::Real time=0.0)
Definition AMReX_TimeIntegrator.H:95
void set_max_steps(int steps)
Definition AMReX_TimeIntegrator.H:194
void set_post_step_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_TimeIntegrator.H:154
void map_data(std::function< void(T &)> Map)
Definition AMReX_TimeIntegrator.H:249
void set_imex_rhs(std::function< void(T &, T &, const amrex::Real)> Fi, std::function< void(T &, T &, const amrex::Real)> Fe)
Definition AMReX_TimeIntegrator.H:138
void set_fast_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_TimeIntegrator.H:144
void evolve(T &S_out, const amrex::Real time_out)
Definition AMReX_TimeIntegrator.H:214
void set_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_TimeIntegrator.H:199
void set_post_fast_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_TimeIntegrator.H:159
void initialize_integrator(IntegratorTypes integrator_type, const T &S_data, const amrex::Real time=0.0)
Create an integrator of the given type. This resets the right-hand side and post stage/step functions...
Definition AMReX_TimeIntegrator.H:109
void set_post_fast_step_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_TimeIntegrator.H:164
void set_post_stage_action(std::function< void(T &, amrex::Real)> A)
Definition AMReX_TimeIntegrator.H:149
void set_rhs(std::function< void(T &, T &, const amrex::Real)> F)
Definition AMReX_TimeIntegrator.H:133
void time_interpolate(const T &S_new, const T &S_old, amrex::Real timestep_fraction, T &data)
Definition AMReX_TimeIntegrator.H:244
void set_time_step(amrex::Real dt)
Definition AMReX_TimeIntegrator.H:174
amrex::Real get_time_step()
Definition AMReX_TimeIntegrator.H:169
virtual ~TimeIntegrator()
Definition AMReX_TimeIntegrator.H:102
void advance(T &S_old, T &S_new, amrex::Real time, const amrex::Real dt)
Definition AMReX_TimeIntegrator.H:209
TimeIntegrator(IntegratorTypes integrator_type, const T &S_data, const amrex::Real time=0.0)
Definition AMReX_TimeIntegrator.H:89
void set_fast_tolerances(amrex::Real rtol, amrex::Real atol)
Definition AMReX_TimeIntegrator.H:204
void set_fast_time_step(amrex::Real dt)
Definition AMReX_TimeIntegrator.H:184
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:80
Definition AMReX_Amr.cpp:50
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:236
IntegratorTypes
Definition AMReX_TimeIntegrator.H:18
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:242