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
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef _RUNANDCHECKIONICMODELS_HPP_
00038 #define _RUNANDCHECKIONICMODELS_HPP_
00039
00040 #include <vector>
00041 #include <string>
00042
00043 #include "OdeSolution.hpp"
00044
00045 #include "ColumnDataWriter.hpp"
00046 #include "ColumnDataReader.hpp"
00047
00048 #include "AbstractCardiacCell.hpp"
00049 #include "HeartConfig.hpp"
00050
00051 void RunOdeSolverWithIonicModel(AbstractCardiacCellInterface* pOdeSystem,
00052 double endTime,
00053 std::string filename,
00054 int stepPerRow=100,
00055 bool doComputeExceptVoltage=true,
00056 bool useSamplingInterval=false);
00057
00058 void CheckCellModelResults(const std::string& rBaseResultsFilename,
00059 std::string validResultsBasename = "",
00060 double tolerance = 1e-3);
00061
00062 std::vector<double> GetVoltages(ColumnDataReader& rReader);
00063
00064 void CompareCellModelResults(std::string baseResultsFilename1, std::string baseResultsFilename2,
00065 double tolerance, bool vOnly=false, std::string folderName="TestIonicModels");
00066
00067
00068
00069
00070
00071
00072
00073
00074 #include <cmath>
00075
00076 void RunOdeSolverWithIonicModel(AbstractCardiacCellInterface* pOdeSystem,
00077 double endTime,
00078 std::string filename,
00079 int stepPerRow,
00080 bool doComputeExceptVoltage,
00081 bool useSamplingInterval)
00082 {
00083 double start_time = 0.0;
00084
00085 if (doComputeExceptVoltage)
00086 {
00087
00088 std::vector<double> state_variables_copy = pOdeSystem->GetStdVecStateVariables();
00089
00090
00091 double v_init = pOdeSystem->GetVoltage();
00092 pOdeSystem->ComputeExceptVoltage(start_time, start_time + 100.0 );
00093 double v_end = pOdeSystem->GetVoltage();
00094 TS_ASSERT_DELTA(v_init, v_end, 1e-6);
00095
00096
00097 pOdeSystem->SetVoltage(1e6);
00098 TS_ASSERT_DELTA(pOdeSystem->GetVoltage(), 1e6, 1e-6);
00099
00100
00101 pOdeSystem->SetStateVariables(state_variables_copy);
00102 }
00103
00104
00105 if (useSamplingInterval)
00106 {
00107 OdeSolution solution = pOdeSystem->Compute(start_time, endTime, HeartConfig::Instance()->GetOdeTimeStep() * stepPerRow);
00108 solution.WriteToFile("TestIonicModels", filename, "ms", 1, false, 4);
00109 }
00110 else
00111 {
00112 OdeSolution solution = pOdeSystem->Compute(start_time, endTime);
00113 solution.WriteToFile("TestIonicModels", filename, "ms", stepPerRow, false, 4);
00114 }
00115 }
00116
00117 std::vector<double> GetVoltages(ColumnDataReader& rReader)
00118 {
00119
00120
00121 std::vector<double> voltages;
00122 if (rReader.HasValues("V"))
00123 {
00124 voltages = rReader.GetValues("V");
00125 }
00126 else if (rReader.HasValues("membrane_voltage"))
00127 {
00128 voltages = rReader.GetValues("membrane_voltage");
00129 }
00130 else if (rReader.HasValues("membrane__V"))
00131 {
00132 voltages = rReader.GetValues("membrane__V");
00133 }
00134 else
00135 {
00136 EXCEPTION("Model membrane voltage not recognised.");
00137 }
00138 return voltages;
00139 }
00140
00141 std::vector<double> GetIntracellularCalcium(ColumnDataReader& rReader)
00142 {
00143
00144
00145 std::vector<double> cai;
00146 if (rReader.HasValues("CaI"))
00147 {
00148 cai = rReader.GetValues("CaI");
00149 }
00150 else if (rReader.HasValues("cytosolic_calcium_concentration"))
00151 {
00152 cai = rReader.GetValues("cytosolic_calcium_concentration");
00153 }
00154 else if (rReader.HasValues("Cai"))
00155 {
00156 cai = rReader.GetValues("Cai");
00157 }
00158 else
00159 {
00160 EXCEPTION("Model intracellular calcium is not recognised.");
00161 }
00162 return cai;
00163 }
00164
00165 std::vector<double> GetHGate(ColumnDataReader& rReader)
00166 {
00167
00168
00169 std::vector<double> h_values;
00170 if (rReader.HasValues("h"))
00171 {
00172 h_values = rReader.GetValues("h");
00173 }
00174 else if (rReader.HasValues("fast_sodium_current_h_gate__h"))
00175 {
00176 h_values = rReader.GetValues("fast_sodium_current_h_gate__h");
00177 }
00178 else if (rReader.HasValues("membrane_fast_sodium_current_h_gate"))
00179 {
00180 h_values = rReader.GetValues("membrane_fast_sodium_current_h_gate");
00181 }
00182 else
00183 {
00184 EXCEPTION("Model h gate is not recognised.");
00185 }
00186 return h_values;
00187 }
00188
00189
00190
00191
00192
00193 void CheckCellModelResults(const std::string& rBaseResultsFilename,
00194 std::string validResultsBasename,
00195 double tolerance)
00196 {
00197
00198 ColumnDataReader data_reader("TestIonicModels", rBaseResultsFilename);
00199 std::vector<double> times = data_reader.GetValues("Time");
00200 std::vector<double> voltages = GetVoltages(data_reader);
00201
00202 if (validResultsBasename == "")
00203 {
00204 validResultsBasename = rBaseResultsFilename;
00205 }
00206
00207 ColumnDataReader valid_reader("heart/test/data/ionicmodels", validResultsBasename + "ValidData",
00208 false);
00209 std::vector<double> valid_times = valid_reader.GetValues("Time");
00210 std::vector<double> valid_voltages = GetVoltages(valid_reader);
00211
00212 TS_ASSERT_EQUALS(times.size(), valid_times.size());
00213 for (unsigned i=0; i<valid_times.size(); i++)
00214 {
00215 TS_ASSERT_DELTA(times[i], valid_times[i], 1e-12);
00216 TS_ASSERT_DELTA(voltages[i], valid_voltages[i], tolerance);
00217 }
00218 }
00219
00220 void CompareCellModelResults(std::string baseResultsFilename1, std::string baseResultsFilename2,
00221 double tolerance, bool vOnly, std::string folderName)
00222 {
00223
00224
00225 ColumnDataReader data_reader1(folderName, baseResultsFilename1);
00226 std::vector<double> times1 = data_reader1.GetValues("Time");
00227 std::vector<double> voltages1 = GetVoltages(data_reader1);
00228 std::vector<double> calcium1;
00229 std::vector<double> h1;
00230
00231 ColumnDataReader data_reader2(folderName, baseResultsFilename2);
00232 std::vector<double> times2 = data_reader2.GetValues("Time");
00233 std::vector<double> voltages2 = GetVoltages(data_reader2);
00234 std::vector<double> calcium2;
00235 std::vector<double> h2;
00236
00237 if (!vOnly)
00238 {
00239 calcium1 = GetIntracellularCalcium(data_reader1);
00240 h1 = GetHGate(data_reader1);
00241 calcium2 = GetIntracellularCalcium(data_reader2);
00242 h2 = GetHGate(data_reader2);
00243 }
00244
00245 TS_ASSERT(times1.size() >= times2.size());
00246 double last_v = voltages2[0];
00247 double tol = tolerance;
00248 for (unsigned i=0, j=0; i<times2.size(); i++)
00249 {
00250
00251 while (j<times1.size() && times1[j] < times2[i] - 1e-12)
00252 {
00253 j++;
00254 }
00255
00256
00257 if (fabs(voltages2[i] - last_v) > 0.05)
00258 {
00259 tol = tolerance * 25;
00260 }
00261 else
00262 {
00263 tol = tolerance;
00264 }
00265 last_v = voltages2[i];
00266
00267 TS_ASSERT_DELTA(times1[j], times2[i], 1e-12);
00268
00269 TS_ASSERT_DELTA(voltages1[j], voltages2[i], tol);
00270 if (!vOnly)
00271 {
00272 TS_ASSERT_DELTA(calcium1[j], calcium2[i], tol/100);
00273 TS_ASSERT_DELTA(h1[j], h2[i], tol/10);
00274 }
00275 }
00276 }
00277
00278
00279 #endif //_RUNANDCHECKIONICMODELS_HPP_