Chaste Commit::f841a6fa79bd6f7a205054452b95ddf6d10aae23
BackwardEulerIvpOdeSolver.cpp
1/*
2
3Copyright (c) 2005-2026, 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
37#include "BackwardEulerIvpOdeSolver.hpp"
38#include <cmath>
39
41 double timeStep,
42 double time,
43 std::vector<double>& rCurrentYValues,
44 std::vector<double>& rCurrentGuess)
45{
46 std::vector<double> dy(mSizeOfOdeSystem); //For JC to optimize
47 pAbstractOdeSystem->EvaluateYDerivatives(time+timeStep, rCurrentGuess, dy);
48 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
49 {
50 mResidual[i] = rCurrentGuess[i] - timeStep * dy[i] - rCurrentYValues[i];
51 }
52}
53
55 double timeStep,
56 double time,
57 std::vector<double>& rCurrentYValues,
58 std::vector<double>& rCurrentGuess)
59{
60 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
61 {
62 for (unsigned j=0; j<mSizeOfOdeSystem; j++)
63 {
64 mJacobian[i][j] = 0.0;
65 }
66 }
67
68 if (pAbstractOdeSystem->GetUseAnalyticJacobian() && !mForceUseOfNumericalJacobian)
69 {
70 // The ODE system has an analytic jacobian, so use that
71 auto* p_ode_system = static_cast<AbstractOdeSystemWithAnalyticJacobian*>(pAbstractOdeSystem);
72 p_ode_system->AnalyticJacobian(rCurrentGuess, mJacobian, time, timeStep);
73 }
74 else
75 {
76 ComputeNumericalJacobian(pAbstractOdeSystem,
77 timeStep,
78 time,
79 rCurrentYValues,
80 rCurrentGuess);
81 }
82}
83
85{
86 double fact;
87 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
88 {
89 for (unsigned ii=i+1; ii<mSizeOfOdeSystem; ii++)
90 {
91 fact = mJacobian[ii][i]/mJacobian[i][i];
92 for (unsigned j=i; j<mSizeOfOdeSystem; j++)
93 {
94 mJacobian[ii][j] -= fact*mJacobian[i][j];
95 }
96 mResidual[ii] -= fact*mResidual[i];
97 }
98 }
99 // This needs to int, since a downloop in unsigned won't terminate properly
100 for (int i=mSizeOfOdeSystem-1; i>=0; i--)
101 {
102 mUpdate[i] = mResidual[i];
103 for (unsigned j=i+1; j<mSizeOfOdeSystem; j++)
104 {
105 mUpdate[i] -= mJacobian[i][j]*mUpdate[j];
106 }
107 mUpdate[i] /= mJacobian[i][i];
108 }
109}
110
112{
113 double norm = 0.0;
114 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
115 {
116 if (fabs(pVector[i]) > norm)
117 {
118 norm = fabs(pVector[i]);
119 }
120 }
121 return norm;
122}
123
125 double timeStep,
126 double time,
127 std::vector<double>& rCurrentYValues,
128 std::vector<double>& rCurrentGuess)
129{
130 std::vector<double> residual(mSizeOfOdeSystem);
131 std::vector<double> residual_perturbed(mSizeOfOdeSystem);
132 std::vector<double> guess_perturbed(mSizeOfOdeSystem);
133
134 double epsilon = mNumericalJacobianEpsilon;
135
136 ComputeResidual(pAbstractOdeSystem, timeStep, time, rCurrentYValues, rCurrentGuess);
137 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
138 {
139 residual[i] = mResidual[i];
140 }
141
142 for (unsigned global_column=0; global_column<mSizeOfOdeSystem; global_column++)
143 {
144 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
145 {
146 guess_perturbed[i] = rCurrentGuess[i];
147 }
148
149 guess_perturbed[global_column] += epsilon;
150
151 ComputeResidual(pAbstractOdeSystem, timeStep, time, rCurrentYValues, guess_perturbed);
152 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
153 {
154 residual_perturbed[i] = mResidual[i];
155 }
156
157 // Compute residual_perturbed - residual
158 double one_over_eps = 1.0/epsilon;
159 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
160 {
161 mJacobian[i][global_column] = one_over_eps*(residual_perturbed[i] - residual[i]);
162 }
163 }
164}
165
167 double timeStep,
168 double time,
169 std::vector<double>& rCurrentYValues,
170 std::vector<double>& rNextYValues)
171{
172 // Check the size of the ODE system matches the solvers expected
173 assert(mSizeOfOdeSystem == pAbstractOdeSystem->GetNumberOfStateVariables());
174
175 const double eps = 1e-6; // JonW tolerance
176 double norm = 2*eps;
177
178 std::vector<double> current_guess(mSizeOfOdeSystem);
179 current_guess.assign(rCurrentYValues.begin(), rCurrentYValues.end());
180
181 // Ensure no infinite loops by keeping a counter
182 // TODO should this limit of 20 be a function param?
183 unsigned counter = 0u;
184 const unsigned iter_limit = 20u;
185 while (norm > eps && counter < iter_limit)
186 {
187 // Calculate Jacobian and mResidual for current guess
188 ComputeResidual(pAbstractOdeSystem, timeStep, time, rCurrentYValues, current_guess);
189 ComputeJacobian(pAbstractOdeSystem, timeStep, time, rCurrentYValues, current_guess);
190
191 // Solve Newton linear system
193
194 // Update norm (JonW style)
195 norm = ComputeNorm(mUpdate);
196
197 // Update current guess
198 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
199 {
200 current_guess[i] -= mUpdate[i];
201 }
202
203 counter++;
204 }
205 assert(counter < iter_limit); // avoid infinite loops
206
207 rNextYValues.assign(current_guess.begin(), current_guess.end());
208}
209
211{
212 mSizeOfOdeSystem = sizeOfOdeSystem;
213
214 // default epsilon
217
218 // allocate memory
219 mResidual = new double[mSizeOfOdeSystem];
220 mUpdate = new double[mSizeOfOdeSystem];
221
222 mJacobian = new double*[mSizeOfOdeSystem];
223 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
224 {
225 mJacobian[i] = new double[mSizeOfOdeSystem];
226 }
227}
228
230{
231 // Delete pointers
232 delete[] mResidual;
233 delete[] mUpdate;
234
235 for (unsigned i=0; i<mSizeOfOdeSystem; i++)
236 {
237 delete[] mJacobian[i];
238 }
239 delete[] mJacobian;
240}
241
243{
244 assert(epsilon > 0);
246}
247
252
253
254// Serialization for Boost >= 1.36
#define CHASTE_CLASS_EXPORT(T)
virtual void AnalyticJacobian(const std::vector< double > &rSolutionGuess, double **jacobian, double time, double timeStep)=0
virtual void EvaluateYDerivatives(double time, const std::vector< double > &rY, std::vector< double > &rDY)=0
void ComputeNumericalJacobian(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rCurrentGuess)
void CalculateNextYValue(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rNextYValues)
void ComputeResidual(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rCurrentGuess)
void SetEpsilonForNumericalJacobian(double epsilon)
void ComputeJacobian(AbstractOdeSystem *pAbstractOdeSystem, double timeStep, double time, std::vector< double > &rCurrentYValues, std::vector< double > &rCurrentGuess)
BackwardEulerIvpOdeSolver(unsigned sizeOfOdeSystem)