Chaste  Release::2017.1
SimulationTime.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
44 boost::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  :
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 
105 void SimulationTime::SetStartTime(double startTime)
106 {
107  assert(mStartTime == DOUBLE_UNSET);
108  mStartTime = startTime;
109 }
110 
111 void 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 
122 void 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 
bool IsEndTimeAndNumberOfTimeStepsSetUp() const
static SimulationTime * Instance()
bool IsFinished() const
unsigned GetTimeStepsElapsed() const
double GetTimeStep() const
const double DOUBLE_UNSET
Definition: Exception.hpp:56
static void Destroy()
void SetStartTime(double startTime)
void SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
double GetTime() const
static SimulationTime * mpInstance
bool IsStartTimeSetUp() const
void IncrementTimeOneStep()
void ResetEndTimeAndNumberOfTimeSteps(const double &rEndTime, const unsigned &rNumberOfTimeStepsInThisRun)
static boost::shared_ptr< TimeStepper > mpTimeStepper