OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
48#ifndef PFC_TIME_HPP
49#define PFC_TIME_HPP
50
51#include <array>
52#include <cmath>
53#include <iostream>
54
55namespace pfc {
56
233class Time {
234private:
235 double m_t0;
236 double m_t1;
237 double m_dt;
238 int m_increment;
239 double m_saveat;
240
241public:
335 Time(const std::array<double, 3> &time, double saveat)
336 : m_t0(time[0]), m_t1(time[1]), m_dt(time[2]), m_increment(0),
337 m_saveat(saveat) {
338 if (m_t0 < 0) {
339 throw std::invalid_argument("Start time cannot be negative: " +
340 std::to_string(m_t0));
341 }
342 if (m_dt <= 0) {
343 throw std::invalid_argument("Time step (dt) must be greater than zero: " +
344 std::to_string(m_dt));
345 }
346 if (m_dt < 1e-9) {
347 throw std::invalid_argument("Time step (dt) is too small: " +
348 std::to_string(m_dt));
349 }
350 if (std::abs(m_t0 - m_t1) < 1e-9) {
351 throw std::invalid_argument("Start time cannot equal end time: t0 == t1");
352 }
353 if (m_saveat > m_t1) {
354 throw std::invalid_argument(
355 "Save interval cannot exceed end time: " + std::to_string(m_saveat) +
356 " > " + std::to_string(m_t1));
357 }
358 }
359
369 Time(const std::array<double, 3> &time) : Time(time, time[2]) {}
370
376 double get_t0() const { return m_t0; }
377
383 double get_t1() const { return m_t1; }
384
390 double get_dt() const { return m_dt; }
391
397 int get_increment() const { return m_increment; }
398
453 double get_current() const {
454 double current_time = m_t0 + m_increment * m_dt;
455 return (current_time > m_t1) ? m_t1
456 : current_time; // Clamp to m_t1 if it exceeds
457 }
458
464 double get_saveat() const { return m_saveat; }
465
471 void set_increment(int increment) { m_increment = increment; }
472
478 void set_saveat(double saveat) { m_saveat = saveat; }
479
542 bool done() const {
543 return (get_current() >= m_t1 - 1e-9); // Adjust for floating-point precision
544 }
545
610 void next() { m_increment += 1; }
611
712 bool do_save() const {
713 if (m_saveat <= 0) {
714 return false; // Save interval of 0 means no saving
715 }
716 return (std::fmod(get_current() + 1.0e-9, m_saveat) < 1.e-6) || done() ||
717 (m_increment == 0);
718 }
719
725 operator double() const { return get_current(); }
726
734 friend std::ostream &operator<<(std::ostream &os, const Time &t) {
735 os << "(t0 = " << t.m_t0 << ", t1 = " << t.m_t1 << ", dt = " << t.m_dt;
736 os << ", saveat = " << t.m_saveat << ", t_current = " << t.get_current()
737 << ")\n";
738 return os;
739 };
740};
741
742} // namespace pfc
743
744#endif
Definition time.hpp:233
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
Time(const std::array< double, 3 > &time)
Construct a new Time object with the specified time interval and default save interval.
Definition time.hpp:369
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
Represents the global simulation domain (the "world").
Definition world.hpp:91