00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "NhsModelWithBackwardSolver.hpp"
00030 #include <iostream>
00031 #include <cmath>
00032 #include "LogFile.hpp"
00033 #include "Exception.hpp"
00034 #include "TimeStepper.hpp"
00035
00036 const double NhsModelWithBackwardSolver::mTolerance = 1e-10;
00037
00038 double NhsModelWithBackwardSolver::ImplicitSolveForQ()
00039 {
00040 mTemporaryStateVariables[2] = (mTemporaryStateVariables[2] + mDt*mA1*mDLambdaDt)/(1 + mAlpha1*mDt);
00041 mTemporaryStateVariables[3] = (mTemporaryStateVariables[3] + mDt*mA2*mDLambdaDt)/(1 + mAlpha2*mDt);
00042 mTemporaryStateVariables[4] = (mTemporaryStateVariables[4] + mDt*mA3*mDLambdaDt)/(1 + mAlpha3*mDt);
00043
00044 return mTemporaryStateVariables[2] + mTemporaryStateVariables[3] + mTemporaryStateVariables[4];
00045 }
00046
00047 void NhsModelWithBackwardSolver::CalculateCaTropAndZDerivatives(double calciumTroponin, double z, double Q,
00048 double& dCaTrop, double& dz)
00049 {
00050
00051 #define COVERAGE_IGNORE
00052 if(calciumTroponin < 0)
00053 {
00054 EXCEPTION("CalciumTrop concentration went negative");
00055 }
00056 if(z<0)
00057 {
00058 EXCEPTION("z went negative");
00059 }
00060 if(z>1)
00061 {
00062 EXCEPTION("z became greater than 1");
00063 }
00064 #undef COVERAGE_IGNORE
00065
00066 double T0 = CalculateT0(z);
00067
00068 double Ta;
00069 if(Q>0)
00070 {
00071 Ta = T0*(1+(2+mA)*Q)/(1+Q);
00072 }
00073 else
00074 {
00075 Ta = T0*(1+mA*Q)/(1-Q);
00076 }
00077
00078 dCaTrop = mKon * mCalciumI * ( mCalciumTroponinMax - calciumTroponin)
00079 - mKrefoff * (1-Ta/(mGamma*mTref)) * calciumTroponin;
00080
00081 double ca_trop_to_ca_trop50_ratio_to_n = pow(calciumTroponin/mCalciumTrop50, mN);
00082
00083 dz = mAlpha0 * ca_trop_to_ca_trop50_ratio_to_n * (1-z)
00084 - mAlphaR1 * z
00085 - mAlphaR2 * pow(z,mNr) / (pow(z,mNr) + pow(mKZ,mNr));
00086 }
00087
00088
00089
00090 void NhsModelWithBackwardSolver::CalculateBackwardEulerResidual(double calciumTroponin, double z, double Q,
00091 double& residualComponent1, double& residualComponent2)
00092 {
00093 double dcatrop;
00094 double dz;
00095 CalculateCaTropAndZDerivatives(calciumTroponin,z,Q,dcatrop,dz);
00096
00097 residualComponent1 = calciumTroponin - mDt*dcatrop - mTemporaryStateVariables[0];
00098 residualComponent2 = z - mDt*dz - mTemporaryStateVariables[1];
00099 }
00100
00101
00102
00103 NhsModelWithBackwardSolver::NhsModelWithBackwardSolver()
00104 {
00105 mTemporaryStateVariables.resize(5);
00106 }
00107
00108
00109
00110 void NhsModelWithBackwardSolver::RunDoNotUpdate(double startTime, double endTime, double timestep)
00111 {
00112 assert(startTime < endTime);
00113
00114 mDt = timestep;
00115
00116 mTemporaryStateVariables = mStateVariables;
00117
00118
00119 TimeStepper stepper(startTime, endTime, timestep);
00120
00121 while ( !stepper.IsTimeAtEnd() )
00122 {
00124
00126 double new_Q = ImplicitSolveForQ();
00127
00129
00131
00132
00133 double catrop_guess = mTemporaryStateVariables[0];
00134 double z_guess = mTemporaryStateVariables[1];
00135 double f1,f2;
00136
00137 CalculateBackwardEulerResidual(catrop_guess, z_guess, new_Q, f1, f2);
00138 double norm_resid = sqrt(f1*f1+f2*f2);
00139
00140
00141
00142 unsigned counter = 0;
00143 while ((norm_resid>mTolerance) && (counter++<15))
00144 {
00145
00146 double j11,j12,j21,j22;
00147 double temp1,temp2;
00148
00149 double h = std::max(fabs(catrop_guess/100),1e-8);
00150 CalculateBackwardEulerResidual(catrop_guess+h, z_guess, new_Q, temp1, temp2);
00151 j11 = (temp1-f1)/h;
00152 j21 = (temp2-f2)/h;
00153
00154 h = std::max(fabs(z_guess/100),1e-8);
00155 CalculateBackwardEulerResidual(catrop_guess, z_guess+h, new_Q, temp1, temp2);
00156 j12 = (temp1-f1)/h;
00157 j22 = (temp2-f2)/h;
00158
00159
00160 double one_over_det = 1.0/(j11*j22-j12*j21);
00161 double u1 = one_over_det*(j22*f1 - j12*f2);
00162 double u2 = one_over_det*(-j21*f1 + j11*f2);
00163
00164 catrop_guess -= u1;
00165 z_guess -= u2;
00166
00167 CalculateBackwardEulerResidual(catrop_guess, z_guess, new_Q, f1, f2);
00168 norm_resid = sqrt(f1*f1+f2*f2);
00169 }
00170 assert(counter<15);
00171
00172 mTemporaryStateVariables[0] = catrop_guess;
00173 mTemporaryStateVariables[1] = z_guess;
00174
00175 stepper.AdvanceOneTimeStep();
00176 }
00177 }
00178
00179 double NhsModelWithBackwardSolver::GetNextActiveTension()
00180 {
00181 double T0 = CalculateT0(mTemporaryStateVariables[1]);
00182 double Q = mTemporaryStateVariables[2]+mTemporaryStateVariables[3]+mTemporaryStateVariables[4];
00183
00184 if(Q>0)
00185 {
00186 return T0*(1+(2+mA)*Q)/(1+Q);
00187 }
00188 else
00189 {
00190 return T0*(1+mA*Q)/(1-Q);
00191 }
00192 }
00193
00194 void NhsModelWithBackwardSolver::RunAndUpdate(double startTime, double endTime, double timestep)
00195 {
00196 RunDoNotUpdate(startTime, endTime, timestep);
00197 UpdateStateVariables();
00198 }
00199