Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
AbstractOneStepIvpOdeSolver.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 "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
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...
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
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();
146 }
147 }
148 // Final answer must be in rYValues
149 if (curr_is_curr)
150 {
151 rYValues.assign(rWorkingMemory.begin(), rWorkingMemory.end());
152 }
153}
#define EXCEPTION(message)
virtual bool CalculateStoppingEvent(double time, const std::vector< double > &rY)
virtual void CalculateNextYValue(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rNextYValues)=0
virtual OdeSolution Solve(AbstractOdeSystem *pAbstractOdeSystem, std::vector< double > &rYValues, double startTime, double endTime, double timeStep, double timeSampling)
virtual void InternalSolve(AbstractOdeSystem *pAbstractOdeSystem, std::vector< double > &rCurrentYValues, std::vector< double > &rWorkingMemory, double startTime, double endTime, double timeStep)
boost::shared_ptr< const AbstractOdeSystemInformation > GetSystemInformation() const
std::string GetIdentifier() const
void SetNumberOfTimeSteps(unsigned numTimeSteps)
std::vector< std::vector< double > > & rGetSolutions()
std::vector< double > & rGetTimes()
void SetSolverName(std::string solverName)
void SetOdeSystemInformation(boost::shared_ptr< const AbstractOdeSystemInformation > pOdeSystemInfo)
double GetNextTimeStep()
bool IsTimeAtEnd() const
unsigned GetTotalTimeStepsTaken() const
double GetTime() const
void AdvanceOneTimeStep()
double GetNextTime() const
unsigned EstimateTimeSteps() const