Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
CardiacNewtonSolver.hpp
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#ifndef CARDIACNEWTONSOLVER_HPP_
36#define CARDIACNEWTONSOLVER_HPP_
37
38#include <cmath>
39
41#include "AbstractBackwardEulerCardiacCell.hpp"
42#include "Warnings.hpp"
43
56template<unsigned SIZE, typename CELLTYPE>
58{
59public:
66 {
68 return &inst;
69 }
70
78 void Solve(CELLTYPE &rCell,
79 double time,
80 double rCurrentGuess[SIZE])
81 {
82 unsigned counter = 0;
83 const double eps = 1e-6; // JonW tolerance
84
85 // check that the initial guess that was given gives a valid residual
86 rCell.ComputeResidual(time, rCurrentGuess, mResidual.data());
87 double norm_of_residual = norm_inf(mResidual);
88 assert(!std::isnan(norm_of_residual));
89 double norm_of_update = 0.0; //Properly initialised in the loop
90 do
91 {
92 // Calculate Jacobian for current guess
93 rCell.ComputeJacobian(time, rCurrentGuess, mJacobian);
94
95 // Solve Newton linear system for mUpdate, given mJacobian and mResidual
97
98 // Update norm (JonW style)
99 norm_of_update = norm_inf(mUpdate);
100
101 // Update current guess and recalculate residual
102 for (unsigned i=0; i<SIZE; i++)
103 {
104 rCurrentGuess[i] -= mUpdate[i];
105 }
106 double norm_of_previous_residual = norm_of_residual;
107 rCell.ComputeResidual(time, rCurrentGuess, mResidual.data());
108 norm_of_residual = norm_inf(mResidual);
109 if (norm_of_residual > norm_of_previous_residual && norm_of_update > eps)
110 {
111 //Second part of guard:
112 //Note that if norm_of_update < eps (converged) then it's
113 //likely that both the residual and the previous residual were
114 //close to the root.
115
116 //Work out where the biggest change in the guess has happened.
117 double relative_change_max = 0.0;
118 unsigned relative_change_direction = 0;
119 for (unsigned i=0; i<SIZE; i++)
120 {
121 double relative_change = fabs(mUpdate[i]/rCurrentGuess[i]);
122 if (relative_change > relative_change_max)
123 {
124 relative_change_max = relative_change;
125 relative_change_direction = i;
126 }
127 }
128
129 if (relative_change_max > 1.0)
130 {
131 //Only walk 0.2 of the way in that direction (put back 0.8)
132 rCurrentGuess[relative_change_direction] += 0.8*mUpdate[relative_change_direction];
133 rCell.ComputeResidual(time, rCurrentGuess, mResidual.data());
134 norm_of_residual = norm_inf(mResidual);
135 WARNING("Residual increasing and one direction changing radically - back tracking in that direction");
136 }
137 }
138 counter++;
139
140 // avoid infinite loops
141 if (counter > 15)
142 {
143// LCOV_EXCL_START
144 EXCEPTION("Newton method diverged in CardiacNewtonSolver::Solve()");
145// LCOV_EXCL_STOP
146 }
147 }
148 while (norm_of_update > eps);
149
150// LCOV_EXCL_START
151#ifndef NDEBUG
152 if (norm_of_residual > 2e-10)
153 { //This line is for correlation - in case we use norm_of_residual as convergence criterion
154 WARN_ONCE_ONLY("Newton iteration terminated because update vector norm is small, but residual norm is not small.");
155 }
156#endif // NDEBUG
157// LCOV_EXCL_STOP
158 }
159
160protected:
168
179 {
180 for (unsigned i=0; i<SIZE; i++)
181 {
182 for (unsigned ii=i+1; ii<SIZE; ii++)
183 {
184 double fact = mJacobian[ii][i]/mJacobian[i][i];
185 for (unsigned j=i; j<SIZE; j++)
186 {
187 mJacobian[ii][j] -= fact*mJacobian[i][j];
188 }
189 mResidual[ii] -= fact*mResidual[i];
190 }
191 }
192 for (unsigned i=SIZE; i-- > 0; )
193 {
194 mUpdate[i] = mResidual[i];
195 for (unsigned j=i+1; j<SIZE; j++)
196 {
197 mUpdate[i] -= mJacobian[i][j]*mUpdate[j];
198 }
199 mUpdate[i] /= mJacobian[i][i];
200 }
201 }
202
203private:
205 c_vector<double, SIZE> mResidual;
207 double mJacobian[SIZE][SIZE];
209 c_vector<double, SIZE> mUpdate;
210};
211
212#endif /*CARDIACNEWTONSOLVER_HPP_*/
#define EXCEPTION(message)
void Solve(CELLTYPE &rCell, double time, double rCurrentGuess[SIZE])
CardiacNewtonSolver< SIZE, CELLTYPE > & operator=(const CardiacNewtonSolver< SIZE, CELLTYPE > &)
double mJacobian[SIZE][SIZE]
static CardiacNewtonSolver< SIZE, CELLTYPE > * Instance()
c_vector< double, SIZE > mResidual
c_vector< double, SIZE > mUpdate
CardiacNewtonSolver(const CardiacNewtonSolver< SIZE, CELLTYPE > &)