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 <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 }

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5