Chaste  Release::3.4
AbstractOneStepIvpOdeSolver.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 "AbstractOneStepIvpOdeSolver.hpp"
37 #include "TimeStepper.hpp"
38 #include "Exception.hpp"
39 #include <cmath>
40 
42  std::vector<double>& rYValues,
43  double startTime,
44  double endTime,
45  double timeStep,
46  double timeSampling)
47 {
48  assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
49  assert(endTime > startTime);
50  assert(timeStep > 0.0);
51  assert(timeSampling >= timeStep);
52 
53  mStoppingEventOccurred = false;
54  if ( pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true )
55  {
56  EXCEPTION("(Solve with sampling) Stopping event is true for initial condition");
57  }
58  TimeStepper stepper(startTime, endTime, timeSampling);
59 
60  // setup solutions if output is required
61  OdeSolution solutions;
62  solutions.SetNumberOfTimeSteps(stepper.EstimateTimeSteps());
63  solutions.rGetSolutions().push_back(rYValues);
64  solutions.rGetTimes().push_back(startTime);
65  solutions.SetOdeSystemInformation(pOdeSystem->GetSystemInformation());
66  solutions.SetSolverName( GetIdentifier() );
67 
68  mWorkingMemory.resize(rYValues.size());
69 
70  // Solve the ODE system
71  while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
72  {
73  InternalSolve(pOdeSystem, rYValues, mWorkingMemory, stepper.GetTime(), stepper.GetNextTime(), timeStep);
74  stepper.AdvanceOneTimeStep();
75  // write current solution into solutions
76  solutions.rGetSolutions().push_back(rYValues);
77  // Push back new time into the time solution vector
79  {
80  solutions.rGetTimes().push_back(mStoppingTime);
81  }
82  else
83  {
84  solutions.rGetTimes().push_back(stepper.GetTime());
85  }
86  }
87 
88  // stepper.EstimateTimeSteps may have been an overestimate...
89  solutions.SetNumberOfTimeSteps(stepper.GetTotalTimeStepsTaken());
90  return solutions;
91 }
92 
94  std::vector<double>& rYValues,
95  double startTime,
96  double endTime,
97  double timeStep)
98 {
99  assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
100  assert(endTime > startTime);
101  assert(timeStep > 0.0);
102 
103  mStoppingEventOccurred = false;
104  if ( pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true )
105  {
106  EXCEPTION("(Solve without sampling) Stopping event is true for initial condition");
107  }
108 
109  // Perhaps resize working memory
110  mWorkingMemory.resize(rYValues.size());
111  // And solve...
112  InternalSolve(pOdeSystem, rYValues, mWorkingMemory, startTime, endTime, timeStep);
113 }
114 
116  std::vector<double>& rYValues,
117  std::vector<double>& rWorkingMemory,
118  double startTime,
119  double endTime,
120  double timeStep)
121 {
122  TimeStepper stepper(startTime, endTime, timeStep);
123  // Solve the ODE system
124 
125  // Which of our vectors holds the current solution?
126  // If this is true, it's in rYValues, otherwise it's in rWorkingMemory.
127  bool curr_is_curr = false;
128 
129  // should never get here if this bool has been set to true;
130  assert(!mStoppingEventOccurred);
131  while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
132  {
133  curr_is_curr = !curr_is_curr;
134  // Function that calls the appropriate one-step solver
135  CalculateNextYValue(pOdeSystem,
136  stepper.GetNextTimeStep(),
137  stepper.GetTime(),
138  curr_is_curr ? rYValues : rWorkingMemory,
139  curr_is_curr ? rWorkingMemory : rYValues);
140  stepper.AdvanceOneTimeStep();
141  if ( pOdeSystem->CalculateStoppingEvent(stepper.GetTime(),
142  curr_is_curr ? rWorkingMemory : rYValues) == true )
143  {
144  mStoppingTime = stepper.GetTime();
145  mStoppingEventOccurred = true;
146  }
147  }
148  // Final answer must be in rYValues
149  if (curr_is_curr)
150  {
151  rYValues.assign(rWorkingMemory.begin(), rWorkingMemory.end());
152  }
153 }
double GetNextTimeStep()
std::vector< std::vector< double > > & rGetSolutions()
double GetTime() const
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual OdeSolution Solve(AbstractOdeSystem *pAbstractOdeSystem, std::vector< double > &rYValues, double startTime, double endTime, double timeStep, double timeSampling)
void AdvanceOneTimeStep()
bool IsTimeAtEnd() const
void SetOdeSystemInformation(boost::shared_ptr< const AbstractOdeSystemInformation > pOdeSystemInfo)
Definition: OdeSolution.cpp:66
unsigned GetTotalTimeStepsTaken() const
boost::shared_ptr< const AbstractOdeSystemInformation > GetSystemInformation() const
std::vector< double > & rGetTimes()
unsigned EstimateTimeSteps() const
virtual void InternalSolve(AbstractOdeSystem *pAbstractOdeSystem, std::vector< double > &rCurrentYValues, std::vector< double > &rWorkingMemory, double startTime, double endTime, double timeStep)
std::string GetIdentifier() const
virtual void CalculateNextYValue(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rNextYValues)=0
double GetNextTime() const
void SetSolverName(std::string solverName)
void SetNumberOfTimeSteps(unsigned numTimeSteps)
Definition: OdeSolution.cpp:58
virtual bool CalculateStoppingEvent(double time, const std::vector< double > &rY)