MercuryTime.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef TIME_H
27 #define TIME_H
28 
29 #include <ctime>
30 #include <ctime>
31 #include <cstring>
32 #include <sstream>
33 #include <chrono>
34 #include <cmath>
35 #include "GeneralDefine.h"
36 #include "Logger.h"
37 
45 class Time
46 {
47 public:
48 
49  Time () {
50  tic();
51  start = 0;
52  finish = 0;
53  }
54 
59  void tic()
60  {
61  start = clock(); //clock tics
62  wallClockStart = std::chrono::high_resolution_clock::now();
63  }
64 
71  {
72  finish = clock();
73  wallClockFinish = std::chrono::high_resolution_clock::now();
74  return getWallTime();
75  }
76 
81  {
82  return (Mdouble(finish) - Mdouble(start)) / CLOCKS_PER_SEC;
83  }
84 
89  {
90  return std::chrono::duration<double>(wallClockFinish-wallClockStart).count();
91  }
92 
97  {
98  Mdouble tocTime = toc();
99  start = finish;
100  return tocTime;
101  }
102 
103 private:
107  clock_t start;
108  std::chrono::time_point<std::chrono::high_resolution_clock> wallClockStart;
109 
113  clock_t finish;
114  std::chrono::time_point<std::chrono::high_resolution_clock> wallClockFinish;
115 };
116 
130 {
131 public:
132 
140  {
141  startTime_ = clock();
142  time_ = t;
143  timeMax_ = tMax;
144  }
145 
160  {
161  clock_t finish = clock();
162  Mdouble elapsedTime = (Mdouble(finish) - Mdouble(startTime_)) / CLOCKS_PER_SEC;
163 
164  if (fabs(time_ - t) < 1.e-9)
165  {
166  logger(WARN, "Choose an other value for t");
167  return 0;
168  }
169  else
170  {
171  Mdouble time2Finish = elapsedTime * (timeMax_ - time_) / (t - time_);
172  startTime_ = finish;
173  time_ = t;
174  return time2Finish;
175  }
176  }
177 
183  std::string getFinishTime(Mdouble t)
184  {
185  // gets the estimated time left to finish.
186  Mdouble time2Finish = getTime2Finish(t);
187 
188  // adds to the estimated time to current time and also type-casting Mdouble to time_t.
189  time_t finish = time(nullptr) + time2Finish;
190 
191  std::stringstream ss;
192 
193  //write estimated end time
194  ss << ctime(&finish);
195 
196  //decrement put pointer by one to avoid line break
197  ss.seekp((long) ss.tellp() - 1);
198 
199  //write time to finish
200  ss << " (" << time2Finish / 3600 << "h)";
201  return ss.str();
202  }
203 
204 private:
206  clock_t startTime_;
207 
210 
213 
214 };
215 
216 #endif
double Mdouble
Definition: GeneralDefine.h:34
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:130
std::string getFinishTime(Mdouble t)
Returns the estimated finish time based on the amount of time left to finish.
Definition: MercuryTime.h:183
Mdouble timeMax_
Stores the total simulation time (DPM units)
Definition: MercuryTime.h:212
clock_t startTime_
Stores the current number of clock ticks at the start.
Definition: MercuryTime.h:206
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:139
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:159
Mdouble time_
Stores the simulation time (DPM units)
Definition: MercuryTime.h:209
Allows for timing the algorithms; accurate up to 0.01 sec.
Definition: MercuryTime.h:46
std::chrono::time_point< std::chrono::high_resolution_clock > wallClockStart
Definition: MercuryTime.h:108
clock_t finish
Stores the number of clock ticks, called by Time::toc().
Definition: MercuryTime.h:113
std::chrono::time_point< std::chrono::high_resolution_clock > wallClockFinish
Definition: MercuryTime.h:114
Mdouble toc()
This is like a stop button of a stopwatch. Assigns the variable finish to the current value of ticks ...
Definition: MercuryTime.h:70
Time()
Definition: MercuryTime.h:49
Mdouble getWallTime()
Definition: MercuryTime.h:88
clock_t start
Stores the number of clock ticks, called by Time::tic().
Definition: MercuryTime.h:107
Mdouble toctic()
Outputs the toc value and resets the start time.
Definition: MercuryTime.h:96
Mdouble getCPUTime() const
Definition: MercuryTime.h:80
void tic()
This is like a start button of a stopwatch. Assigns the variable start with the current number of clo...
Definition: MercuryTime.h:59