Chaste Release::3.1
SimulationTime.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include <cassert>
00037 #include <cmath>
00038 #include "SimulationTime.hpp"
00039 
00041 SimulationTime* SimulationTime::mpInstance = NULL;
00042 
00044 boost::shared_ptr<TimeStepper> SimulationTime::mpTimeStepper;
00045 
00046 SimulationTime* SimulationTime::Instance()
00047 {
00048     if (mpInstance == NULL)
00049     {
00050         mpInstance = new SimulationTime;
00051         mpTimeStepper.reset();
00052         std::atexit(Destroy);
00053     }
00054     return mpInstance;
00055 }
00056 
00057 SimulationTime::SimulationTime()
00058     :
00059       mStartTime(DOUBLE_UNSET)
00060 {
00061     // Make sure there's only one instance - enforces correct serialization
00062     assert(mpInstance == NULL);
00063 }
00064 
00065 void SimulationTime::Destroy()
00066 {
00067     if (mpInstance)
00068     {
00069         delete mpInstance;
00070         mpInstance = NULL;
00071     }
00072 }
00073 
00074 double SimulationTime::GetTimeStep() const
00075 {
00076     assert(mpTimeStepper);
00077     return mpTimeStepper->GetIdealTimeStep();
00078 }
00079 
00080 void SimulationTime::IncrementTimeOneStep()
00081 {
00082     assert(mpTimeStepper);
00083     mpTimeStepper->AdvanceOneTimeStep(); //This can now throw if the end time has been reached
00084 }
00085 
00086 unsigned SimulationTime::GetTimeStepsElapsed() const
00087 {
00088     assert(mpTimeStepper);
00089     return mpTimeStepper->GetTotalTimeStepsTaken();
00090 }
00091 
00092 double SimulationTime::GetTime() const
00093 {
00094     // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
00095     assert(mStartTime != DOUBLE_UNSET);
00096     //Check if the time stepping has started
00097     if (mpTimeStepper)
00098     {
00099         return mpTimeStepper->GetTime();
00100     }
00101     //If time stepping hasn't started then we are still at start time
00102     return mStartTime;
00103 }
00104 
00105 void SimulationTime::SetStartTime(double startTime)
00106 {
00107     assert(mStartTime == DOUBLE_UNSET);
00108     mStartTime = startTime;
00109 }
00110 
00111 void SimulationTime::SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
00112 {
00113     // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
00114     assert(mStartTime != DOUBLE_UNSET);
00115     assert(!mpTimeStepper);
00116     assert(endTime > mStartTime);
00117 
00118     mpTimeStepper.reset(new TimeStepper(mStartTime, endTime, (endTime-mStartTime)/totalTimeStepsInSimulation, true));
00119 }
00120 
00121 void SimulationTime::ResetEndTimeAndNumberOfTimeSteps(const double& rEndTime, const unsigned& rNumberOfTimeStepsInThisRun)
00122 {
00123     // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
00124     assert(mStartTime != DOUBLE_UNSET);
00125     // NOTE: If this assertion fails, you should be using set rather than reset
00126     assert(mpTimeStepper);
00127     mStartTime = mpTimeStepper->GetTime();
00128 
00129     assert(rEndTime > mStartTime);
00130 
00131     // Reset the machinery that works out the time
00132     mpTimeStepper.reset(new TimeStepper(mStartTime, rEndTime, (rEndTime-mStartTime)/rNumberOfTimeStepsInThisRun, true));
00133 }
00134 
00135 bool SimulationTime::IsStartTimeSetUp() const
00136 {
00137     return (mStartTime != DOUBLE_UNSET);
00138 }
00139 
00140 bool SimulationTime::IsEndTimeAndNumberOfTimeStepsSetUp() const
00141 {
00142     if (mpTimeStepper)
00143     {
00144         return true;
00145     }
00146     else
00147     {
00148         return false;
00149     }
00150 }
00151 
00152 bool SimulationTime::IsFinished() const
00153 {
00154     // assert(mpTimeStepper);
00155     return(mpTimeStepper->IsTimeAtEnd());
00156 }
00157