00001 /* 00002 00003 Copyright (C) University of Oxford, 2005-2010 00004 00005 University of Oxford means the Chancellor, Masters and Scholars of the 00006 University of Oxford, having an administrative office at Wellington 00007 Square, Oxford OX1 2JD, UK. 00008 00009 This file is part of Chaste. 00010 00011 Chaste is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU Lesser General Public License as published 00013 by the Free Software Foundation, either version 2.1 of the License, or 00014 (at your option) any later version. 00015 00016 Chaste is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00019 License for more details. The offer of Chaste under the terms of the 00020 License is subject to the License being interpreted in accordance with 00021 English Law and subject to any action against the University of Oxford 00022 being under the jurisdiction of the English Courts. 00023 00024 You should have received a copy of the GNU Lesser General Public License 00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>. 00026 00027 */ 00028 #ifndef CARDIACNEWTONSOLVER_HPP_ 00029 #define CARDIACNEWTONSOLVER_HPP_ 00030 00031 #include <cmath> 00032 00033 #include "AbstractBackwardEulerCardiacCell.hpp" 00034 00047 template<unsigned SIZE> 00048 class CardiacNewtonSolver 00049 { 00050 public: 00056 static CardiacNewtonSolver<SIZE>* Instance() 00057 { 00058 static CardiacNewtonSolver<SIZE> inst; 00059 return &inst; 00060 } 00061 00069 void Solve(AbstractBackwardEulerCardiacCell<SIZE> &rCell, 00070 double time, 00071 double rCurrentGuess[SIZE]) 00072 { 00073 unsigned counter = 0; 00074 // const double eps = 1e-6 * rCurrentGuess[0]; // Our tolerance (should use min(guess) perhaps?) 00075 const double eps = 1e-6; // JonW tolerance 00076 double norm = 2*eps; 00077 00078 // check that the initial guess that was given gives a valid residual 00079 rCell.ComputeResidual(time, rCurrentGuess, mResidual); 00080 for (unsigned i=0; i<SIZE; i++) 00081 { 00082 assert(!std::isnan(mResidual[i])); 00083 } 00084 00085 while (norm > eps) 00086 { 00087 // Calculate Jacobian for current guess 00088 rCell.ComputeJacobian(time, rCurrentGuess, mJacobian); 00089 00090 // // Update norm (our style) 00091 // norm = ComputeNorm(mResidual); 00092 00093 // Solve Newton linear system 00094 SolveLinearSystem(); 00095 00096 // Update norm (JonW style) 00097 norm = ComputeNorm(mUpdate); 00098 00099 // Update current guess and recalculate residual 00100 for (unsigned i=0; i<SIZE; i++) 00101 { 00102 rCurrentGuess[i] -= mUpdate[i]; 00103 } 00104 rCell.ComputeResidual(time, rCurrentGuess, mResidual); 00105 00106 counter++; 00107 00108 // avoid infinite loops 00109 if (counter > 15) 00110 { 00111 #define COVERAGE_IGNORE 00112 EXCEPTION("Newton method diverged in CardiacNewtonSolver::Solve()"); 00113 #undef COVERAGE_IGNORE 00114 } 00115 } 00116 } 00117 00193 00194 00195 protected: 00197 CardiacNewtonSolver() 00198 {} 00200 CardiacNewtonSolver(const CardiacNewtonSolver<SIZE>&); 00202 CardiacNewtonSolver<SIZE>& operator= (const CardiacNewtonSolver<SIZE>&); 00203 00209 double ComputeNorm(double vector[SIZE]) 00210 { 00211 double norm = 0.0; 00212 for (unsigned i=0; i<SIZE; i++) 00213 { 00214 if (fabs(vector[i]) > norm) 00215 { 00216 norm = fabs(vector[i]); 00217 } 00218 } 00219 return norm; 00220 } 00221 00225 void SolveLinearSystem() 00226 { 00227 double fact; 00228 for (unsigned i=0; i<SIZE; i++) 00229 { 00230 for (unsigned ii=i+1; ii<SIZE; ii++) 00231 { 00232 fact = mJacobian[ii][i]/mJacobian[i][i]; 00233 for (unsigned j=i; j<SIZE; j++) 00234 { 00235 mJacobian[ii][j] -= fact*mJacobian[i][j]; 00236 } 00237 mResidual[ii] -= fact*mResidual[i]; 00238 } 00239 } 00240 /*This must be int, since an unsigned down-loop wouldn't terminate*/ 00241 for (int i=SIZE-1; i>=0; i--) 00242 { 00243 mUpdate[i] = mResidual[i]; 00244 for (unsigned j=i+1; j<SIZE; j++) 00245 { 00246 mUpdate[i] -= mJacobian[i][j]*mUpdate[j]; 00247 } 00248 mUpdate[i] /= mJacobian[i][i]; 00249 } 00250 } 00251 00252 private: 00254 double mResidual[SIZE]; 00256 double mJacobian[SIZE][SIZE]; 00258 double mUpdate[SIZE]; 00259 }; 00260 00261 #endif /*CARDIACNEWTONSOLVER_HPP_*/