libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode_base.h
Go to the documentation of this file.
1
166#ifndef ODE_BASE_H_
167#define ODE_BASE_H_
168
169#include <string>
170#include <vector>
171#include <cstdio>
172#include <cstdlib>
173#include <cmath>
174
175#include "ode_io.h"
176#include "ode_util.h"
177
178namespace ode {
179
181
184class OdeBase {
185
186 public:
188
192 OdeBase (unsigned long neq, bool need_jac);
194 virtual ~OdeBase ();
195
196 //-------------------
197 //getters and setters
198
200 const char *get_name () { return(name_.c_str()); }
202 const char *get_method () { return(method_.c_str()); }
204 const char *get_dirout () { return(dirout_.c_str()); }
206 bool get_quiet () { return(quiet_); }
208 bool get_silent_snap () { return(silent_snap_); }
210 unsigned long get_neq () { return(neq_); }
212
215 double get_t () { return(t_); }
217 double get_dt () { return(dt_); }
219 double *get_sol () { return(sol_); }
221
222 double get_sol (unsigned long i) { return(sol_[i]); }
224 long unsigned get_nstep () { return(nstep_); }
226 long unsigned get_neval () { return(neval_); }
228 long unsigned get_icheck () { return(icheck_); }
230 long unsigned get_nJac () { return(nJac_); }
231
233
234 void set_t (double t) { t_ = t; }
236
240 void set_sol (unsigned long i, double x) { sol_[i] = x; }
242
243 void set_sol (double *sol) { for(unsigned long i=0; i<neq_; i++) sol_[i] = sol[i]; }
245 void set_name (std::string name) { name_ = name; }
247 void set_name (const char *name) { name_ = name; }
249 void set_quiet (bool quiet) { quiet_ = quiet; }
251 void set_silent_snap (bool silent_snap) { silent_snap_ = silent_snap; }
253 void set_icheck (unsigned long icheck) { icheck_ = icheck; }
254
255 //----------------
256 //solver functions
257
259 void step (double dt, bool extra=true);
260
262
267 void solve_fixed (double tint, double dt, bool extras=true);
268
270
276 void solve_fixed (double tint, double dt, const char *dirout, int inter=1);
277
279
285 void solve_fixed (double tint, double dt, unsigned long nsnap, const char *dirout);
286
288
294 void solve_fixed (double dt, double *tsnap, unsigned long nsnap, const char *dirout);
295
297
301 void reset (double t, double *sol);
302
303 protected:
304
306
311 void solve_fixed_ (double tint, double dt, bool extra=true);
312
313 //----------------------
314 //basic solver variables
315
317 std::string name_;
319 std::string method_;
321 std::string dirout_;
323 bool quiet_;
327 unsigned long neq_;
329 double t_;
331 double dt_;
333 double *sol_;
335 long unsigned nstep_;
337 long unsigned neval_;
339 long unsigned icheck_;
341 double **Jac_;
343 long unsigned nJac_;
348
349 //---------------------------
350 //essential virtual functions
351
353
358 virtual void ode_fun (double *solin, double *fout) = 0;
359
361
366 virtual void ode_jac (double *solin, double **Jout);
367
369
373 virtual void step_ (double dt) = 0;
374
375 //---------------------------------------
376 //wrappers of essential virtual functions
377
379
384 void ode_fun_ (double *solin, double *fout);
386
391 void ode_jac_ (double *solin, double **Jout);
392
393 //------
394 //extras
395
397 virtual void before_solve ();
399
402 virtual void after_step (double t);
404
407 virtual void after_capture (double t);
409
414 virtual void after_snap (std::string dirout, long isnap, double t);
415
417 virtual void after_solve ();
418
419 //--------------
420 //solver support
421
423
428 void snap (std::string dirout, long isnap, double tsnap);
429
431
436 bool solve_done (double dt, double tend);
437
439 void check_sol_integrity ();
440
442
446 void check_pre_solve (double tint, double dt);
447
449
454 void check_pre_snaps (double dt, double *tsnap, unsigned long nsnap);
455
456 private:
457
458 //flag for whether the Jacobian is being used
459 bool need_jac_;
460 //arrays for evaluating numerical jacobian
461 double *f_, *g_;
462};
463
464} // namespace ode
465
466#endif
Lowest base class for all solvers.
Definition ode_base.h:184
void ode_jac_(double *solin, double **Jout)
wrapper, calls ode_jac() and increments nJac;
Definition ode_base.cc:95
virtual void after_capture(double t)
does any extra stuff only when a step is captured
Definition ode_base.cc:330
double reljacdel_
relative adjustment fraction for numerical Jacobian, if needed
Definition ode_base.h:347
OdeBase(unsigned long neq, bool need_jac)
constructs
Definition ode_base.cc:7
long unsigned icheck_
interval of steps after which to check for nans and infs (zero to ignore)
Definition ode_base.h:339
double get_sol(unsigned long i)
gets an element of the solution array
Definition ode_base.h:222
unsigned long neq_
number of equations in the system of ODEs
Definition ode_base.h:327
const char * get_method()
gets the name of the solver/method
Definition ode_base.h:202
void step(double dt, bool extra=true)
increments the step counter and the time, checks the solution integrity if needed,...
Definition ode_base.cc:103
void solve_fixed(double tint, double dt, bool extras=true)
integrates for a specified duration of independent variable without output
Definition ode_base.cc:192
bool get_silent_snap()
gets whether to skip writing the solution vector to file when snapping
Definition ode_base.h:208
bool solve_done(double dt, double tend)
checks if the solution is within a single time step of the end point
Definition ode_base.cc:141
double t_
time, initialized to zero
Definition ode_base.h:329
const char * get_name()
gets the name of the ODE system
Definition ode_base.h:200
long unsigned get_neval()
gets the total number of ODE system evaluation
Definition ode_base.h:226
long unsigned nJac_
counter for jacobian evaluations
Definition ode_base.h:343
std::string name_
the "name" of the system, which is used for output
Definition ode_base.h:317
virtual void ode_jac(double *solin, double **Jout)
evaluates the system's Jacobian matrix, also in autonomous form, and can either be defined in a deriv...
Definition ode_base.cc:60
virtual void after_snap(std::string dirout, long isnap, double t)
does any extra stuff after each snap
Definition ode_base.cc:335
virtual void before_solve()
does any extra stuff before starting a solve
Definition ode_base.cc:323
double get_dt()
gets the most recent or current time step size
Definition ode_base.h:217
void set_quiet(bool quiet)
sets the boolean determining if updates are printed during solves
Definition ode_base.h:249
virtual void after_solve()
does any extra stuff after completing a solve
Definition ode_base.cc:342
double dt_
time step is stored and updated during solves
Definition ode_base.h:331
void snap(std::string dirout, long isnap, double tsnap)
writes the current value of the solution to a binary file
Definition ode_base.cc:122
virtual ~OdeBase()
destructs
Definition ode_base.cc:47
long unsigned nstep_
number of time steps
Definition ode_base.h:335
double * get_sol()
gets a pointer to the whole solution array
Definition ode_base.h:219
virtual void step_(double dt)=0
advances a single time step (without changing counters or the time) and must be defined in the derive...
bool quiet_
whether stuff should be printed during a solve
Definition ode_base.h:323
virtual void ode_fun(double *solin, double *fout)=0
evaluates the system of ODEs in autonomous form and must be defined by a derived class
void check_sol_integrity()
checks solution for nans and infs, exiting the program if they're found
Definition ode_base.cc:151
void set_name(const char *name)
sets the name of the ODE system
Definition ode_base.h:247
bool get_quiet()
gets the boolean determining if updates are printed during solves
Definition ode_base.h:206
double ** Jac_
storage for the ODE system's Jacobian matrix, only allocated for the methods that need it
Definition ode_base.h:341
void set_silent_snap(bool silent_snap)
sets whether to skip writing the solution vector to file when snapping
Definition ode_base.h:251
void reset(double t, double *sol)
reset to a specified time and initial condition array
Definition ode_base.cc:314
long unsigned get_icheck()
gets the number of steps after which the solution is checked for integrity
Definition ode_base.h:228
void set_icheck(unsigned long icheck)
sets the number of steps after which the solution is checked for integrity
Definition ode_base.h:253
void solve_fixed_(double tint, double dt, bool extra=true)
integrates without output or any counters, trackers, extra functions...
Definition ode_base.cc:182
void set_sol(double *sol)
copies an array into the solution array
Definition ode_base.h:243
bool silent_snap_
whether to skip writing the solution vector to file when snapping but still execute after_snap()
Definition ode_base.h:325
double absjacdel_
absolute adjustment fraction for numerical Jacobian, if needed
Definition ode_base.h:345
std::string method_
the "name" of the solver/method, as in "Euler" or "RK4"
Definition ode_base.h:319
void set_name(std::string name)
sets the name of the ODE system
Definition ode_base.h:245
double get_t()
gets the current value of the independent variable
Definition ode_base.h:215
void check_pre_snaps(double dt, double *tsnap, unsigned long nsnap)
checks that snap times are monotonically increasing and > current time
Definition ode_base.cc:169
const char * get_dirout()
gets output directory string if one has been set
Definition ode_base.h:204
long unsigned get_nstep()
gets the total number of steps taken
Definition ode_base.h:224
double * sol_
array for the solution, changing over time
Definition ode_base.h:333
void ode_fun_(double *solin, double *fout)
wrapper, calls ode_fun() and increases the neval counter by one
Definition ode_base.cc:87
void set_t(double t)
sets the "time," or independent variable used to track progress
Definition ode_base.h:234
virtual void after_step(double t)
does any extra stuff after each step
Definition ode_base.cc:325
long unsigned neval_
function evaluation counter, must be incremented in step() when defined
Definition ode_base.h:337
void set_sol(unsigned long i, double x)
sets an element of the solution array
Definition ode_base.h:240
void check_pre_solve(double tint, double dt)
checks that a solve can be performed with given tend and dt values
Definition ode_base.cc:161
std::string dirout_
output directory if one is being used by a solver
Definition ode_base.h:321
long unsigned get_nJac()
gets the total number of Jacobian evaluations performed
Definition ode_base.h:230
unsigned long get_neq()
gets the size of the ODE system
Definition ode_base.h:210