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