TimeStepper.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #include "TimeStepper.hpp"
00031 #include "Exception.hpp"
00032 #include <cmath>
00033 #include <cfloat>
00034 #include <cassert>
00035 
00036 const double SMIDGE = 1e-10;
00037 
00038 TimeStepper::TimeStepper(double startTime, double endTime, double dt, bool enforceConstantTimeStep, std::vector<double> additionalTimes)
00039     : mStart(startTime),
00040       mEnd(endTime),
00041       mDt(dt),
00042       mTotalTimeStepsTaken(0),
00043       mAdditionalTimesReached(0),
00044       mTime(startTime)
00045 {
00046     if (startTime > endTime)
00047     {
00048         EXCEPTION("The simulation duration must be positive");
00049     }
00050 
00051     // Remove any additionalTimes entries which fall too close to a time when the stepper would stop anyway
00052     for(unsigned i=0;i<additionalTimes.size();i++)
00053     {
00054         if(i>0)
00055         {
00056             if(additionalTimes[i-1] >= additionalTimes[i])
00057             {
00058                 EXCEPTION("The additional times vector should be in ascending numerical order");
00059             }
00060         }        
00061         
00062         double test_value=(additionalTimes[i]-startTime)/mDt;
00063         if(fabs(round(test_value)-test_value)>1e-12)
00064         {
00065             mAdditionalTimes.push_back(additionalTimes[i]);
00066         }
00067     }
00068 
00069     mNextTime = CalculateNextTime();
00070 
00071     // if enforceConstantTimeStep check whether the times are such that we won't have a variable dt
00072     if (enforceConstantTimeStep)
00073     {
00074         if ( fabs(mDt*EstimateTimeSteps()-mEnd+mStart) > SMIDGE )
00075         {
00076             EXCEPTION("TimeStepper estimate non-constant timesteps will need to be used: check timestep divides (end_time-start_time) (or divides printing timestep)");
00077         }
00078     }
00079 }
00080 
00081 double TimeStepper::CalculateNextTime() 
00082 {
00083     double next_time = mStart + (mTotalTimeStepsTaken-mAdditionalTimesReached+1) * mDt;
00084     if ((next_time) + SMIDGE*(mDt) >= mEnd)
00085     {
00086         next_time = mEnd;
00087     }
00088     
00089     if (    (mAdditionalTimes.size()>0)                          // any additional times given  
00090          && (mAdditionalTimesReached<mAdditionalTimes.size())    // not yet done all the additional times
00091          && ((next_time) + SMIDGE*(mDt) >= mAdditionalTimes[mAdditionalTimesReached]) ) // this next step takes us over an additional time
00092     {
00093         next_time = mAdditionalTimes[mAdditionalTimesReached];
00094         mAdditionalTimesReached++;
00095     }
00096     
00097     return next_time;
00098 }
00099 
00100 void TimeStepper::AdvanceOneTimeStep()
00101 {
00102     mTotalTimeStepsTaken++;
00103     if (mTotalTimeStepsTaken == 0)
00104     {
00105         EXCEPTION("Time step counter has overflowed.");
00106     }
00107     mTime = mNextTime;
00108 
00109     mNextTime = CalculateNextTime();
00110 }
00111 
00112 double TimeStepper::GetTime() const
00113 {
00114     return mTime;
00115 }
00116 
00117 double TimeStepper::GetNextTime() const
00118 {
00119     return mNextTime;
00120 }
00121 
00122 double TimeStepper::GetNextTimeStep()
00123 {
00124     double dt = mDt;
00125     
00126     if (mNextTime == mEnd)
00127     {
00128         dt = mEnd - mTime;
00129     }
00130 
00131     // if the next time or the current time is one of the additional times, the timestep will not be mDt    
00132     if ((mAdditionalTimesReached>0)  &&  
00133         ( (mNextTime == mAdditionalTimes[mAdditionalTimesReached-1]) || (mTime == mAdditionalTimes[mAdditionalTimesReached-1]) ) )
00134     {
00135         dt = mNextTime - mTime;
00136         assert(dt>0);
00137     }
00138 
00139     return dt;
00140 }
00141 
00142 bool TimeStepper::IsTimeAtEnd() const
00143 {
00144     return mTime >= mEnd;
00145 }
00146 
00147 unsigned TimeStepper::EstimateTimeSteps() const
00148 {
00149     return (unsigned) round((mEnd - mStart)/mDt) + mAdditionalTimes.size();
00150 }
00151 
00152 unsigned TimeStepper::GetTotalTimeStepsTaken() const
00153 {
00154     return mTotalTimeStepsTaken;
00155 }

Generated by  doxygen 1.6.2