Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
SimulationTime.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include <cassert>
37#include <cmath>
38#include "SimulationTime.hpp"
39
42
44boost::shared_ptr<TimeStepper> SimulationTime::mpTimeStepper;
45
47{
48 if (mpInstance == nullptr)
49 {
51 mpTimeStepper.reset();
52 std::atexit(Destroy);
53 }
54 return mpInstance;
55}
56
58 :
59 mStartTime(DOUBLE_UNSET)
60{
61 // Make sure there's only one instance - enforces correct serialization
62 assert(mpInstance == nullptr);
63}
64
66{
67 if (mpInstance)
68 {
69 delete mpInstance;
70 mpInstance = nullptr;
71 }
72}
73
75{
76 assert(mpTimeStepper);
77 return mpTimeStepper->GetIdealTimeStep();
78}
79
81{
82 assert(mpTimeStepper);
83 mpTimeStepper->AdvanceOneTimeStep(); //This can now throw if the end time has been reached
84}
85
87{
88 assert(mpTimeStepper);
89 return mpTimeStepper->GetTotalTimeStepsTaken();
90}
91
93{
94 // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
95 assert(mStartTime != DOUBLE_UNSET);
96 //Check if the time stepping has started
97 if (mpTimeStepper)
98 {
99 return mpTimeStepper->GetTime();
100 }
101 //If time stepping hasn't started then we are still at start time
102 return mStartTime;
103}
104
105void SimulationTime::SetStartTime(double startTime)
106{
107 assert(mStartTime == DOUBLE_UNSET);
108 mStartTime = startTime;
109}
110
111void SimulationTime::SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
112{
113 // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
114 assert(mStartTime != DOUBLE_UNSET);
115 assert(!mpTimeStepper);
116 assert(endTime > mStartTime);
117 assert(totalTimeStepsInSimulation != 0u);
118
119 mpTimeStepper.reset(new TimeStepper(mStartTime, endTime, (endTime-mStartTime)/totalTimeStepsInSimulation, true));
120}
121
122void SimulationTime::ResetEndTimeAndNumberOfTimeSteps(const double& rEndTime, const unsigned& rNumberOfTimeStepsInThisRun)
123{
124 // NOTE: if this assertion fails, it may be because Destroy() wasn't called in the previous test
125 assert(mStartTime != DOUBLE_UNSET);
126 // NOTE: If this assertion fails, you should be using set rather than reset
127 assert(mpTimeStepper);
128 mStartTime = mpTimeStepper->GetTime();
129
130 assert(rEndTime > mStartTime);
131
132 // Reset the machinery that works out the time
133 mpTimeStepper.reset(new TimeStepper(mStartTime, rEndTime, (rEndTime-mStartTime)/rNumberOfTimeStepsInThisRun, true));
134}
135
137{
138 return (mStartTime != DOUBLE_UNSET);
139}
140
142{
143 if (mpTimeStepper)
144 {
145 return true;
146 }
147 else
148 {
149 return false;
150 }
151}
152
154{
155 // assert(mpTimeStepper);
156 return(mpTimeStepper->IsTimeAtEnd());
157}
158
const double DOUBLE_UNSET
Definition Exception.hpp:57
bool IsStartTimeSetUp() const
bool IsFinished() const
double GetTime() const
void SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
static SimulationTime * mpInstance
bool IsEndTimeAndNumberOfTimeStepsSetUp() const
static void Destroy()
void SetStartTime(double startTime)
double GetTimeStep() const
void IncrementTimeOneStep()
static SimulationTime * Instance()
void ResetEndTimeAndNumberOfTimeSteps(const double &rEndTime, const unsigned &rNumberOfTimeStepsInThisRun)
static boost::shared_ptr< TimeStepper > mpTimeStepper
unsigned GetTimeStepsElapsed() const