TimeStepper.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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)
00039     : mStart(startTime),
00040       mEnd(endTime),
00041       mDt(dt),
00042       mTimeStep(0),
00043       mTime(startTime)
00044 {
00045     if (startTime > endTime)
00046     {
00047         EXCEPTION("The simulation duration must be positive");
00048     }
00049 
00061     mNextTime = CalculateNextTime();
00062     
00063     // if enforceConstantTimeStep check whether the times are such that we won't have a variable dt
00064     if (enforceConstantTimeStep)
00065     {
00066         if ( fabs(mDt*EstimateTimeSteps()-mEnd+mStart) > SMIDGE )
00067         {
00068             //PRINT_4_VARIABLES(mDt, EstimateTimeSteps(), mDt*EstimateTimeSteps(), mEnd-mStart);
00069             EXCEPTION("TimeStepper estimate non-constant timesteps will need to be used: check timestep divides (end_time-start_time) (or divides printing timestep)");
00070         }
00071     }
00072 }
00073 
00074 double TimeStepper::CalculateNextTime() const
00075 {
00076     double next_time = mStart + (mTimeStep+1) * mDt;
00077     if ((next_time) + SMIDGE*(mDt) >= mEnd)
00078     {
00079         next_time = mEnd;
00080     }
00081     return next_time;
00082 }
00083 
00084 void TimeStepper::AdvanceOneTimeStep()
00085 {
00086     mTimeStep++;
00087     if (mTimeStep == 0)
00088     {
00089         EXCEPTION("Time step counter has overflowed.");
00090     }
00091     mTime = mNextTime;
00092     mNextTime = CalculateNextTime();
00093 }
00094 
00095 double TimeStepper::GetTime() const
00096 {
00097     return mTime;
00098 }
00099 
00100 double TimeStepper::GetNextTime() const
00101 {
00102     return mNextTime;
00103 }
00104 
00105 double TimeStepper::GetNextTimeStep() 
00106 {
00107     double dt = mDt;
00108     if (mNextTime == mEnd)
00109     {
00110         dt = mEnd - mTime;
00111     }
00112 
00113     return dt;
00114 }
00115 
00116 bool TimeStepper::IsTimeAtEnd() const
00117 {
00118     return mTime >= mEnd;
00119 }
00120 
00121 unsigned TimeStepper::EstimateTimeSteps() const
00122 {
00123     return (unsigned) round((mEnd - mStart)/mDt);
00124 }
00125 
00126 unsigned TimeStepper::GetTimeStepsElapsed() const
00127 {
00128     return mTimeStep;
00129 }

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5