SimulationTime.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 #include "SimulationTime.hpp"
00029 #include "Exception.hpp"
00030 #include <cassert>
00031 
00032 
00034 SimulationTime* SimulationTime::mpInstance = NULL;
00035 
00036 SimulationTime* SimulationTime::Instance()
00037 {
00038     if (mpInstance == NULL)
00039     {
00040         mpInstance = new SimulationTime;
00041     }
00042     return mpInstance;
00043 }
00044 
00045 
00046 SimulationTime::SimulationTime()
00047     : mTimeStepsElapsed(0),
00048       mEndTimeAndNumberOfTimeStepsSet(false),
00049       mCurrentTime(0.0),
00050       mStartTime(0.0),
00051       mStartTimeSet(false)
00052 {
00053     // Make sure there's only one instance - enforces correct serialization
00054     assert(mpInstance == NULL);
00055 }
00056 
00057 
00058 void SimulationTime::Destroy()
00059 {
00060     if (mpInstance)
00061     {
00062         delete mpInstance;
00063         mpInstance = NULL;
00064     }
00065 }
00066 
00067 
00068 void SimulationTime::SetStartTime(double startTime)
00069 {
00070     assert(mStartTimeSet==false);
00071     mStartTime = startTime;
00072     mCurrentTime = startTime;
00073     mStartTimeSet = true;
00074 }
00075 
00076 
00077 double SimulationTime::GetTimeStep() const
00078 {
00079     assert(mStartTimeSet);
00080     assert(mEndTimeAndNumberOfTimeStepsSet);
00081     return mDurationOfSimulation/mTotalTimeStepsInSimulation;
00082 }
00083 
00084 
00085 void SimulationTime::IncrementTimeOneStep()
00086 {
00087     assert(mStartTimeSet);
00088     assert(mEndTimeAndNumberOfTimeStepsSet);
00089     mTimeStepsElapsed++;
00090     mCurrentTime = mStartTime
00091                    + ((double)mTimeStepsElapsed / (double)mTotalTimeStepsInSimulation)
00092                    * mDurationOfSimulation;
00093 }
00094 
00095 
00096 unsigned SimulationTime::GetTimeStepsElapsed() const
00097 {
00098     assert(mEndTimeAndNumberOfTimeStepsSet);
00099     return mTimeStepsElapsed;
00100 }
00101 
00102 
00103 double SimulationTime::GetTime() const
00104 {
00105     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00106     // IMPORTANT NOTE: if this assertion fails, it may be because Destroy   //
00107     // wasn't called in the previous test                                   //
00108     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00109     assert(mStartTimeSet);
00110 
00111     return mCurrentTime;
00112 }
00113 
00114 
00115 void SimulationTime::SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
00116 {
00117     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00118     // IMPORTANT NOTE: if this assertion fails, it may be because Destroy   //
00119     // wasn't called in the previous test                                   //
00120     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00121     assert(mStartTimeSet);
00122     assert(!mEndTimeAndNumberOfTimeStepsSet);
00123     assert(endTime>mCurrentTime);
00124     mEndTime = endTime;
00125     mDurationOfSimulation = mEndTime - mCurrentTime;
00126     mTotalTimeStepsInSimulation = totalTimeStepsInSimulation;
00127     mEndTimeAndNumberOfTimeStepsSet = true;
00128 }
00129 
00130 
00131 void SimulationTime::ResetEndTimeAndNumberOfTimeSteps(const double& rEndTime, const unsigned& rNumberOfTimeStepsInThisRun)
00132 {
00133     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00134     // IMPORTANT NOTE: if this assertion fails, it may be because Destroy   //
00135     // wasn't called in the previous test                                   //
00136     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
00137     assert(mStartTimeSet);
00138     assert(rEndTime>mCurrentTime);
00139     assert(mTimeStepsElapsed>0); // if this throws you should be using set rather than reset.
00140     //reset the machinery that works out the time
00141     mStartTime = mCurrentTime;
00142     mTimeStepsElapsed = 0;
00143     // set up the new end time and stuff
00144     mEndTime = rEndTime;
00145     mDurationOfSimulation = mEndTime - mCurrentTime;
00146     mTotalTimeStepsInSimulation = rNumberOfTimeStepsInThisRun;
00147     mEndTimeAndNumberOfTimeStepsSet = true;
00148 }
00149 
00150 
00151 bool SimulationTime::IsStartTimeSetUp() const
00152 {
00153     return mStartTimeSet;
00154 }
00155 
00156 
00157 bool SimulationTime::IsFinished() const
00158 {
00159     return(mCurrentTime>=mEndTime);
00160 }
00161 
00162 
00163 unsigned SimulationTime::GetTotalNumberOfTimeSteps() const
00164 {
00165     return mTotalTimeStepsInSimulation;
00166 }

Generated by  doxygen 1.6.2