Time class to handle simulation time stepping and output intervals.
Time class to handle simulation time stepping and output intervalsThis class provides functionalities to manage time in simulations. It supports time intervals, time increments, and data saving at specific intervals.
The Time class is used by Simulator to orchestrate the time integration loop. It tracks the current simulation time based on time step size (dt) and the number of steps taken (increment), and determines when results should be saved based on the saveat interval.
#ifndef PFC_TIME_HPP
#define PFC_TIME_HPP
#include <array>
#include <cmath>
#include <iostream>
namespace pfc {
class Time {
private:
double m_t0;
double m_t1;
double m_dt;
int m_increment;
double m_saveat;
public:
Time(const std::array<double, 3> &time, double saveat)
: m_t0(time[0]), m_t1(time[1]), m_dt(time[2]), m_increment(0),
m_saveat(saveat) {
if (m_t0 < 0) {
throw std::invalid_argument("Start time cannot be negative: " +
std::to_string(m_t0));
}
if (m_dt <= 0) {
throw std::invalid_argument("Time step (dt) must be greater than zero: " +
std::to_string(m_dt));
}
if (m_dt < 1e-9) {
throw std::invalid_argument("Time step (dt) is too small: " +
std::to_string(m_dt));
}
if (std::abs(m_t0 - m_t1) < 1e-9) {
throw std::invalid_argument("Start time cannot equal end time: t0 == t1");
}
if (m_saveat > m_t1) {
throw std::invalid_argument(
"Save interval cannot exceed end time: " + std::to_string(m_saveat) +
" > " + std::to_string(m_t1));
}
}
Time(const std::array<double, 3> &time) : Time(time, time[2]) {}
double get_t0()
const {
return m_t0; }
double get_t1()
const {
return m_t1; }
double get_dt()
const {
return m_dt; }
double get_current() const {
double current_time = m_t0 + m_increment * m_dt;
return (current_time > m_t1) ? m_t1
: current_time;
}
void set_saveat(
double saveat) { m_saveat = saveat; }
bool done() const {
return (get_current() >= m_t1 - 1e-9);
}
void next() { m_increment += 1; }
bool do_save() const {
if (m_saveat <= 0) {
return false;
}
return (std::fmod(get_current() + 1.0e-9, m_saveat) < 1.e-6) || done() ||
(m_increment == 0);
}
operator double() const { return get_current(); }
friend std::ostream &
operator<<(std::ostream &os,
const Time &t) {
os << "(t0 = " << t.m_t0 << ", t1 = " << t.m_t1 << ", dt = " << t.m_dt;
os << ", saveat = " << t.m_saveat << ", t_current = " << t.get_current()
<< ")\n";
return os;
};
};
}
#endif
void set_increment(int increment)
Set the current time increment.
Definition time.hpp:471
void set_saveat(double saveat)
Set the time interval for saving data.
Definition time.hpp:478
double get_dt() const
Get the time step.
Definition time.hpp:390
double get_saveat() const
Get the time interval for saving data.
Definition time.hpp:464
int get_increment() const
Get the current time increment.
Definition time.hpp:397
double get_t0() const
Get the start time.
Definition time.hpp:376
friend std::ostream & operator<<(std::ostream &os, const Time &t)
Overloaded stream insertion operator to print the Time object.
Definition time.hpp:734
double get_t1() const
Get the end time.
Definition time.hpp:383