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(!std::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: 00187 CardiacNewtonSolver() 00188 {} 00190 CardiacNewtonSolver(const CardiacNewtonSolver<SIZE>&); 00192 CardiacNewtonSolver<SIZE>& operator= (const CardiacNewtonSolver<SIZE>&); 00193 00199 double ComputeNorm(double vector[SIZE]) 00200 { 00201 double norm = 0.0; 00202 for (unsigned i=0; i<SIZE; i++) 00203 { 00204 if (fabs(vector[i]) > norm) 00205 { 00206 norm = fabs(vector[i]); 00207 } 00208 } 00209 return norm; 00210 } 00211 00215 void SolveLinearSystem() 00216 { 00217 double fact; 00218 for (unsigned i=0; i<SIZE; i++) 00219 { 00220 for (unsigned ii=i+1; ii<SIZE; ii++) 00221 { 00222 fact = mJacobian[ii][i]/mJacobian[i][i]; 00223 for (unsigned j=i; j<SIZE; j++) 00224 { 00225 mJacobian[ii][j] -= fact*mJacobian[i][j]; 00226 } 00227 mResidual[ii] -= fact*mResidual[i]; 00228 } 00229 } 00230 /*This must be int, since an unsigned down-loop wouldn't terminate*/ 00231 for (int i=SIZE-1; i>=0; i--) 00232 { 00233 mUpdate[i] = mResidual[i]; 00234 for (unsigned j=i+1; j<SIZE; j++) 00235 { 00236 mUpdate[i] -= mJacobian[i][j]*mUpdate[j]; 00237 } 00238 mUpdate[i] /= mJacobian[i][i]; 00239 } 00240 } 00241 00242 private: 00244 double mResidual[SIZE]; 00246 double mJacobian[SIZE][SIZE]; 00248 double mUpdate[SIZE]; 00249 }; 00250 00251 #endif /*CARDIACNEWTONSOLVER_HPP_*/