MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MercuryTime.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, 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 <time.h>
31 #include <string.h>
32 #include <sstream>
33 #include "GeneralDefine.h"
34 
42 class Time
43 {
44 public:
45 
46  Time () {
47  tic();
48  }
49 
54  void tic()
55  {
56  start = clock(); //clock tics
57  }
58 
65  {
66  finish = clock();
67  return (Mdouble(finish) - Mdouble(start)) / CLOCKS_PER_SEC;
68  }
69 
74  {
75  Mdouble tocTime = toc();
76  start = finish;
77  return tocTime;
78  }
79 
80 private:
84  clock_t start;
85 
89  clock_t finish;
90 };
91 
105 {
106 public:
107 
115  {
116  startTime_ = clock();
117  time_ = t;
118  timeMax_ = tMax;
119  }
120 
135  {
136  clock_t finish = clock();
137  Mdouble elapsedTime = (Mdouble(finish) - Mdouble(startTime_)) / CLOCKS_PER_SEC;
138 
139  if (fabs(time_ - t) < 1.e-9)
140  {
141  std::cout << "Choose an other value for t" << std::endl;
142  return 0;
143  }
144  else
145  {
146  Mdouble time2Finish = elapsedTime * (timeMax_ - time_) / (t - time_);
147  startTime_ = finish;
148  time_ = t;
149  return time2Finish;
150  }
151  }
152 
158  std::string getFinishTime(Mdouble t)
159  {
160  // gets the estimated time left to finish.
161  Mdouble time2Finish = getTime2Finish(t);
162 
163  // adds to the estimated time to current time and also type-casting Mdouble to time_t.
164  time_t finish = time(nullptr) + time2Finish;
165 
166  std::stringstream ss;
167 
168  //write estimated end time
169  ss << ctime(&finish);
170 
171  //decrement put pointer by one to avoid line break
172  ss.seekp((long) ss.tellp() - 1);
173 
174  //write time to finish
175  ss << " (" << time2Finish / 3600 << "h)";
176  return ss.str();
177  }
178 
179 private:
181  clock_t startTime_;
182 
185 
188 
189 };
190 
191 #endif
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:134
clock_t startTime_
Stores the current number of clock ticks at the start.
Definition: MercuryTime.h:181
clock_t start
Stores the number of clock ticks, called by Time::tic().
Definition: MercuryTime.h:84
double Mdouble
Definition: GeneralDefine.h:34
Allows for timing the algorithms; accurate up to 0.01 sec.
Definition: MercuryTime.h:42
Mdouble time_
Stores the simulation time (DPM units)
Definition: MercuryTime.h:184
std::string getFinishTime(Mdouble t)
Returns the estimated finish time based on the amount of time left to finish.
Definition: MercuryTime.h:158
void tic()
This is like a start button of a stopwatch. Assigns the variable start with the current number of clo...
Definition: MercuryTime.h:54
Time()
Definition: MercuryTime.h:46
clock_t finish
Stores the number of clock ticks, called by Time::toc().
Definition: MercuryTime.h:89
Mdouble toc()
This is like a stop button of a stopwatch. Assigns the variable finish to the current value of ticks ...
Definition: MercuryTime.h:64
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:114
Mdouble toctic()
Outputs the toc value and resets the start time.
Definition: MercuryTime.h:73
Estimates the total time, in seconds, left to reach the end of any simulation. First, the class needs to be initialized by calling set. After the class is initialized, an estimate of the total remaining time of the simulation can be found by calling getTime2Finish. The estimate is based on rate at which the simulation time progressed since initialization.
Definition: MercuryTime.h:104
Mdouble timeMax_
Stores the total simulation time (DPM units)
Definition: MercuryTime.h:187