MercuryDPM  Beta
 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-2014, 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 
40 class Time
41 {
42 public:
43 
48  void tic()
49  {
50  start = clock(); //clock tics
51  }
52 
59  {
60  finish = clock();
61  return (Mdouble(finish) - Mdouble(start)) / CLOCKS_PER_SEC;
62  }
63 
64 private:
68  clock_t start;
69 
73  clock_t finish;
74 };
75 
89 {
90 public:
91 
98  void set(Mdouble t, Mdouble tMax)
99  {
100  startTime_ = clock();
101  time_ = t;
102  timeMax_ = tMax;
103  }
104 
119  {
120  clock_t finish = clock();
121  Mdouble elapsedTime = (Mdouble(finish) - Mdouble(startTime_)) / CLOCKS_PER_SEC;
122 
123  if (fabs(time_ - t) < 1.e-9)
124  {
125  std::cout << "Choose an other value for t" << std::endl;
126  return 0;
127  }
128  else
129  {
130  Mdouble time2Finish = elapsedTime * (timeMax_ - time_) / (t - time_);
131  startTime_ = finish;
132  time_ = t;
133  return time2Finish;
134  }
135  }
136 
142  std::string getFinishTime(Mdouble t)
143  {
144  // gets the estimated time left to finish.
145  Mdouble time2Finish = getTime2Finish(t);
146 
147  // adds to the estimated time to current time and also type-casting Mdouble to time_t.
148  time_t finish = time(nullptr) + time2Finish;
149 
150  std::stringstream ss;
151 
152  //write estimated end time
153  ss << ctime(&finish);
154 
155  //decrement put pointer by one to avoid line break
156  ss.seekp((long) ss.tellp() - 1);
157 
158  //write time to finish
159  ss << " (" << time2Finish / 3600 << "h)";
160  return ss.str();
161  }
162 
163 private:
165  clock_t startTime_;
166 
169 
172 
173 };
174 
175 #endif
void set(Mdouble t, Mdouble tMax)
Initialises the variable start with the current value of clock ticks, the current time and the final ...
Definition: MercuryTime.h:98
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:118
clock_t startTime_
Stores the current number of clock ticks at the start.
Definition: MercuryTime.h:165
clock_t start
Stores the number of clock ticks, called by Time::tic().
Definition: MercuryTime.h:68
Allows for timing the algorithms; accurate up to 0.01 sec.
Definition: MercuryTime.h:40
Mdouble time_
Stores the simulation time (DPM units)
Definition: MercuryTime.h:168
std::string getFinishTime(Mdouble t)
Returns the estimated finish time based on the amount of time left to finish.
Definition: MercuryTime.h:142
double Mdouble
void tic()
This is like a start button of a stopwatch. Assigns the variable start with the current number of clo...
Definition: MercuryTime.h:48
clock_t finish
Stores the number of clock ticks, called by Time::toc().
Definition: MercuryTime.h:73
Mdouble toc()
This is like a stop button of a stopwatch. Assigns the variable finish to the current value of ticks ...
Definition: MercuryTime.h:58
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:88
Mdouble timeMax_
Stores the total simulation time (DPM units)
Definition: MercuryTime.h:171