MercuryTime.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef TIME_H
6 #define TIME_H
7 
8 #include <ctime>
9 #include <ctime>
10 #include <cstring>
11 #include <sstream>
12 #include <chrono>
13 #include <cmath>
14 #include "GeneralDefine.h"
15 #include "Logger.h"
16 
24 class Time
25 {
26 public:
27 
28  Time () {
29  tic();
30  start = 0;
31  finish = 0;
32  }
33 
38  void tic()
39  {
40  start = clock(); //clock tics
41  wallClockStart = std::chrono::high_resolution_clock::now();
42  }
43 
50  {
51  finish = clock();
52  wallClockFinish = std::chrono::high_resolution_clock::now();
53  return getWallTime();
54  }
55 
60  {
61  return (Mdouble(finish) - Mdouble(start)) / CLOCKS_PER_SEC;
62  }
63 
68  {
69  return std::chrono::duration<double>(wallClockFinish-wallClockStart).count();
70  }
71 
76  {
77  Mdouble tocTime = toc();
78  start = finish;
79  return tocTime;
80  }
81 
82 private:
86  clock_t start;
87  std::chrono::time_point<std::chrono::high_resolution_clock> wallClockStart;
88 
92  clock_t finish;
93  std::chrono::time_point<std::chrono::high_resolution_clock> wallClockFinish;
94 };
95 
109 {
110 public:
111 
119  {
120  startTime_ = clock();
121  time_ = t;
122  timeMax_ = tMax;
123  }
124 
139  {
140  clock_t finish = clock();
141  Mdouble elapsedTime = (Mdouble(finish) - Mdouble(startTime_)) / CLOCKS_PER_SEC;
142 
143  if (fabs(time_ - t) < 1.e-9)
144  {
145  logger(WARN, "Choose an other value for t");
146  return 0;
147  }
148  else
149  {
150  Mdouble time2Finish = elapsedTime * (timeMax_ - time_) / (t - time_);
151  startTime_ = finish;
152  time_ = t;
153  return time2Finish;
154  }
155  }
156 
163  {
164  // gets the estimated time left to finish.
165  Mdouble time2Finish = getTime2Finish(t);
166 
167  // adds to the estimated time to current time and also type-casting Mdouble to time_t.
168  time_t finish = time(nullptr) + time2Finish;
169 
170  std::stringstream ss;
171 
172  //write estimated end time
173  ss << ctime(&finish);
174 
175  //decrement put pointer by one to avoid line break
176  ss.seekp((long) ss.tellp() - 1);
177 
178  //write time to finish
179  ss << " (" << time2Finish / 3600 << "h)";
180  return ss.str();
181  }
182 
183 private:
185  clock_t startTime_;
186 
189 
192 
193 };
194 
195 #endif
double Mdouble
Definition: GeneralDefine.h:13
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ WARN
Estimates the total time, in seconds, left to reach the end of any simulation. First,...
Definition: MercuryTime.h:109
std::string getFinishTime(Mdouble t)
Returns the estimated finish time based on the amount of time left to finish.
Definition: MercuryTime.h:162
Mdouble timeMax_
Stores the total simulation time (DPM units)
Definition: MercuryTime.h:191
clock_t startTime_
Stores the current number of clock ticks at the start.
Definition: MercuryTime.h:185
Time2Finish(Mdouble t, Mdouble tMax)
Initialises the variable start with the current value of clock ticks, the current time and the final ...
Definition: MercuryTime.h:118
Mdouble getTime2Finish(Mdouble t)
Estimates the total time, in seconds, left to reach the end of any simulation. After the class is ini...
Definition: MercuryTime.h:138
Mdouble time_
Stores the simulation time (DPM units)
Definition: MercuryTime.h:188
Allows for timing the algorithms; accurate up to 0.01 sec.
Definition: MercuryTime.h:25
std::chrono::time_point< std::chrono::high_resolution_clock > wallClockStart
Definition: MercuryTime.h:87
clock_t finish
Stores the number of clock ticks, called by Time::toc().
Definition: MercuryTime.h:92
std::chrono::time_point< std::chrono::high_resolution_clock > wallClockFinish
Definition: MercuryTime.h:93
Mdouble toc()
This is like a stop button of a stopwatch. Assigns the variable finish to the current value of ticks ...
Definition: MercuryTime.h:49
Time()
Definition: MercuryTime.h:28
Mdouble getWallTime()
Definition: MercuryTime.h:67
clock_t start
Stores the number of clock ticks, called by Time::tic().
Definition: MercuryTime.h:86
Mdouble toctic()
Outputs the toc value and resets the start time.
Definition: MercuryTime.h:75
Mdouble getCPUTime() const
Definition: MercuryTime.h:59
void tic()
This is like a start button of a stopwatch. Assigns the variable start with the current number of clo...
Definition: MercuryTime.h:38
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:117
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
t
Definition: plotPSD.py:36