00001 /* 00002 00003 Copyright (C) University of Oxford, 2005-2009 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 00068 void Solve(AbstractBackwardEulerCardiacCell<SIZE> &rCell, 00069 double rCurrentGuess[SIZE]) 00070 { 00071 unsigned counter = 0; 00072 // const double eps = 1e-6 * rCurrentGuess[0]; // Our tolerance (should use min(guess) perhaps?) 00073 const double eps = 1e-6; // JonW tolerance 00074 double norm = 2*eps; 00075 00076 // check that the initial guess that was given gives a valid residual 00077 rCell.ComputeResidual(rCurrentGuess, mResidual); 00078 for (unsigned i=0; i<SIZE; i++) 00079 { 00080 assert(!isnan(mResidual[i])); 00081 } 00082 00083 while (norm > eps) 00084 { 00085 // Calculate Jacobian for current guess 00086 rCell.ComputeJacobian(rCurrentGuess, mJacobian); 00087 00088 // // Update norm (our style) 00089 // norm = ComputeNorm(mResidual); 00090 00091 // Solve Newton linear system 00092 SolveLinearSystem(); 00093 00094 // Update norm (JonW style) 00095 norm = ComputeNorm(mUpdate); 00096 00097 // Update current guess and recalculate residual 00098 for (unsigned i=0; i<SIZE; i++) 00099 { 00100 rCurrentGuess[i] -= mUpdate[i]; 00101 } 00102 rCell.ComputeResidual(rCurrentGuess, mResidual); 00103 00104 counter++; 00105 assert(counter < 15); // avoid infinite loops 00106 } 00107 } 00108 00183 00184 00185 protected: 00186 CardiacNewtonSolver() 00187 {} 00188 CardiacNewtonSolver(const CardiacNewtonSolver<SIZE>&); 00189 CardiacNewtonSolver<SIZE>& operator= (const CardiacNewtonSolver<SIZE>&); 00190 00194 double ComputeNorm(double vector[SIZE]) 00195 { 00196 double norm = 0.0; 00197 for (unsigned i=0; i<SIZE; i++) 00198 { 00199 if (fabs(vector[i]) > norm) 00200 { 00201 norm = fabs(vector[i]); 00202 } 00203 } 00204 return norm; 00205 } 00206 00210 void SolveLinearSystem() 00211 { 00212 double fact; 00213 for (unsigned i=0; i<SIZE; i++) 00214 { 00215 for (unsigned ii=i+1; ii<SIZE; ii++) 00216 { 00217 fact = mJacobian[ii][i]/mJacobian[i][i]; 00218 for (unsigned j=i; j<SIZE; j++) 00219 { 00220 mJacobian[ii][j] -= fact*mJacobian[i][j]; 00221 } 00222 mResidual[ii] -= fact*mResidual[i]; 00223 } 00224 } 00225 /*This must be int, since an unsigned down-loop wouldn't terminate*/ 00226 for (int i=SIZE-1; i>=0; i--) 00227 { 00228 mUpdate[i] = mResidual[i]; 00229 for (unsigned j=i+1; j<SIZE; j++) 00230 { 00231 mUpdate[i] -= mJacobian[i][j]*mUpdate[j]; 00232 } 00233 mUpdate[i] /= mJacobian[i][i]; 00234 } 00235 } 00236 00237 private: 00239 double mResidual[SIZE]; 00241 double mJacobian[SIZE][SIZE]; 00243 double mUpdate[SIZE]; 00244 }; 00245 00246 #endif /*CARDIACNEWTONSOLVER_HPP_*/