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

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