00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "TimeStepper.hpp"
00031 #include "Exception.hpp"
00032 #include <cmath>
00033 #include <cfloat>
00034 #include <iostream>
00035 #include <iomanip>
00036 #include <cassert>
00037
00038 const double smidge=1e-10;
00039
00040 TimeStepper::TimeStepper(double startTime, double endTime, double dt)
00041 : mStart(startTime),
00042 mEnd(endTime),
00043 mDt(dt),
00044 mTimeStep(0),
00045 mTime(startTime)
00046 {
00047 if (startTime > endTime)
00048 {
00049 EXCEPTION("The simulation duration must be positive");
00050 }
00051
00065 mNextTime=CalculateNextTime();
00066 }
00067
00068 double TimeStepper::CalculateNextTime() const
00069 {
00070 double next_time = mStart + (mTimeStep+1) * mDt;
00071 if ((next_time) + smidge*(mDt) >= mEnd)
00072 {
00073 next_time = mEnd;
00074 }
00075 return next_time;
00076 }
00077
00078 void TimeStepper::AdvanceOneTimeStep()
00079 {
00080 mTimeStep++;
00081 mTime = mNextTime;
00082 mNextTime = CalculateNextTime();
00083 }
00084
00085 double TimeStepper::GetTime() const
00086 {
00087 return mTime;
00088 }
00089
00090 double TimeStepper::GetNextTime() const
00091 {
00092 return mNextTime;
00093 }
00094
00095 double TimeStepper::GetNextTimeStep() const
00096 {
00097 double dt=mDt;
00098 if (mNextTime == mEnd)
00099 {
00100 dt = mEnd - mTime;
00101 }
00102 return dt;
00103 }
00104
00105 bool TimeStepper::IsTimeAtEnd() const
00106 {
00107 return mTime >= mEnd;
00108 }
00109
00110 unsigned TimeStepper::EstimateTimeSteps() const
00111 {
00112 return (unsigned) ceil((mEnd - mStart)/mDt);
00113 }
00114
00115 unsigned TimeStepper::GetTimeStepsElapsed() const
00116 {
00117 return mTimeStep;
00118 }