Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
RunAndCheckIonicModels.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#ifndef _RUNANDCHECKIONICMODELS_HPP_
38#define _RUNANDCHECKIONICMODELS_HPP_
39
40#include <vector>
41#include <string>
42
43#include "OdeSolution.hpp"
44
45#include "ColumnDataWriter.hpp"
46#include "ColumnDataReader.hpp"
47
48#include "AbstractCardiacCell.hpp"
49#include "HeartConfig.hpp"
50
51#include <cxxtest/TestSuite.h>
52
53void RunOdeSolverWithIonicModel(AbstractCardiacCellInterface* pOdeSystem,
54 double endTime,
55 std::string filename,
56 int stepPerRow=100,
57 bool doComputeExceptVoltage=true,
58 bool useSamplingInterval=false);
59
60void CheckCellModelResults(const std::string& rBaseResultsFilename,
61 std::string validResultsBasename = "",
62 double tolerance = 1e-3,
63 std::string referenceFolder = "heart/test/data/ionicmodels");
64
65std::vector<double> GetVoltages(ColumnDataReader& rReader);
66
67void CompareCellModelResults(std::string baseResultsFilename1, std::string baseResultsFilename2,
68 double tolerance, bool vOnly=false, std::string folderName="TestIonicModels");
69
70
71/*
72 * Note: we have to have the function definitions here, rather than in a .cpp file, if we're
73 * to include them in heart/src, as otherwise linking of the heart library fails because
74 * CxxTest routines are not defined.
75 */
76
77#include <cmath>
78
79void RunOdeSolverWithIonicModel(AbstractCardiacCellInterface* pOdeSystem,
80 double endTime,
81 std::string filename,
82 int stepPerRow,
83 bool doComputeExceptVoltage,
84 bool useSamplingInterval)
85{
86 double start_time = 0.0;
87
88 if (doComputeExceptVoltage)
89 {
90 // Store the current system state
91 std::vector<double> state_variables_copy = pOdeSystem->GetStdVecStateVariables();
92
93 // Test ComputeExceptVoltage
94 double v_init = pOdeSystem->GetVoltage();
95 pOdeSystem->ComputeExceptVoltage(start_time, start_time + 100.0 /*ms*/);
96 double v_end = pOdeSystem->GetVoltage();
97 TS_ASSERT_DELTA(v_init, v_end, 1e-6);
98
99 // Test SetVoltage
100 pOdeSystem->SetVoltage(1e6);
101 TS_ASSERT_DELTA(pOdeSystem->GetVoltage(), 1e6, 1e-6);
102
103 // Reset the system
104 pOdeSystem->SetStateVariables(state_variables_copy);
105 }
106
107 // Solve and write to file
108 if (useSamplingInterval)
109 {
110 OdeSolution solution = pOdeSystem->Compute(start_time, endTime, HeartConfig::Instance()->GetOdeTimeStep() * stepPerRow);
111 solution.WriteToFile("TestIonicModels", filename, "ms", 1, false, 4);
112 }
113 else
114 {
115 OdeSolution solution = pOdeSystem->Compute(start_time, endTime);
116 solution.WriteToFile("TestIonicModels", filename, "ms", stepPerRow, false, 4);
117 }
118}
119
120std::vector<double> GetVoltages(ColumnDataReader& rReader)
121{
122 //Rather Ugly, we can't currently guarantee what the name of the voltage column is,
123 //hence we try to cover the most common possibilities
124 std::vector<double> voltages;
125 if (rReader.HasValues("V"))
126 {
127 voltages = rReader.GetValues("V");
128 }
129 else if (rReader.HasValues("membrane_voltage"))
130 {
131 voltages = rReader.GetValues("membrane_voltage");
132 }
133 else if (rReader.HasValues("membrane__V"))
134 {
135 voltages = rReader.GetValues("membrane__V");
136 }
137 else
138 {
139 EXCEPTION("Model membrane voltage not recognised.");
140 }
141 return voltages;
142}
143
144std::vector<double> GetIntracellularCalcium(ColumnDataReader& rReader)
145{
146 //Rather Ugly, we can't currently guarantee what the name of the calcium column is,
147 //hence we try to cover the most common possibilities
148 std::vector<double> cai;
149 if (rReader.HasValues("CaI"))
150 {
151 cai = rReader.GetValues("CaI");
152 }
153 else if (rReader.HasValues("cytosolic_calcium_concentration"))
154 {
155 cai = rReader.GetValues("cytosolic_calcium_concentration");
156 }
157 else if (rReader.HasValues("Cai"))
158 {
159 cai = rReader.GetValues("Cai");
160 }
161 else
162 {
163 EXCEPTION("Model intracellular calcium is not recognised.");
164 }
165 return cai;
166}
167
168std::vector<double> GetHGate(ColumnDataReader& rReader)
169{
170 //Rather Ugly, we can't currently guarantee what the name of the h gate column is,
171 //hence we try to cover the most common possibilities
172 std::vector<double> h_values;
173 if (rReader.HasValues("h"))
174 {
175 h_values = rReader.GetValues("h");
176 }
177 else if (rReader.HasValues("fast_sodium_current_h_gate__h"))
178 {
179 h_values = rReader.GetValues("fast_sodium_current_h_gate__h");
180 }
181 else if (rReader.HasValues("membrane_fast_sodium_current_h_gate"))
182 {
183 h_values = rReader.GetValues("membrane_fast_sodium_current_h_gate");
184 }
185 else
186 {
187 EXCEPTION("Model h gate is not recognised.");
188 }
189 return h_values;
190}
191
192/*
193 * Check the cell model against a previous version
194 * or another source e.g. Alan's COR
195 */
196void CheckCellModelResults(const std::string& rBaseResultsFilename,
197 std::string validResultsBasename,
198 double tolerance,
199 std::string referenceFolder)
200{
201 // read data entries for the new file
202 ColumnDataReader data_reader("TestIonicModels", rBaseResultsFilename);
203 std::vector<double> times = data_reader.GetValues("Time");
204 std::vector<double> voltages = GetVoltages(data_reader);
205
206 if (validResultsBasename == "")
207 {
208 validResultsBasename = rBaseResultsFilename;
209 }
210
211 ColumnDataReader valid_reader(referenceFolder, validResultsBasename + "ValidData",
212 false);
213 std::vector<double> valid_times = valid_reader.GetValues("Time");
214 std::vector<double> valid_voltages = GetVoltages(valid_reader);
215
216 TS_ASSERT_EQUALS(times.size(), valid_times.size());
217 for (unsigned i=0; i<valid_times.size(); i++)
218 {
219 TS_ASSERT_DELTA(times[i], valid_times[i], 1e-12);
220 TS_ASSERT_DELTA(voltages[i], valid_voltages[i], tolerance);
221 }
222}
223
224void CompareCellModelResults(std::string baseResultsFilename1, std::string baseResultsFilename2,
225 double tolerance, bool vOnly, std::string folderName)
226{
227 // Compare 2 sets of results, e.g. from 2 different solvers for the same model.
228 // If the time series differ, the finer resolution must be given first.
229 ColumnDataReader data_reader1(folderName, baseResultsFilename1);
230 std::vector<double> times1 = data_reader1.GetValues("Time");
231 std::vector<double> voltages1 = GetVoltages(data_reader1);
232 std::vector<double> calcium1;
233 std::vector<double> h1;
234
235 ColumnDataReader data_reader2(folderName, baseResultsFilename2);
236 std::vector<double> times2 = data_reader2.GetValues("Time");
237 std::vector<double> voltages2 = GetVoltages(data_reader2);
238 std::vector<double> calcium2;
239 std::vector<double> h2;
240
241 if (!vOnly)
242 {
243 calcium1 = GetIntracellularCalcium(data_reader1);
244 h1 = GetHGate(data_reader1);
245 calcium2 = GetIntracellularCalcium(data_reader2);
246 h2 = GetHGate(data_reader2);
247 }
248
249 TS_ASSERT(times1.size() >= times2.size());
250 double last_v = voltages2[0];
251 double tol = tolerance;
252 for (unsigned i=0, j=0; i<times2.size(); i++)
253 {
254 // Find corresponding time index
255 while (j<times1.size() && times1[j] < times2[i] - 1e-12)
256 {
257 j++;
258 }
259
260 // Set tolerance higher in upstroke
261 if (fabs(voltages2[i] - last_v) > 0.05)
262 {
263 tol = tolerance * 25;
264 }
265 else
266 {
267 tol = tolerance;
268 }
269 last_v = voltages2[i];
270
271 TS_ASSERT_DELTA(times1[j], times2[i], 1e-12);
272 // adjust tol to data
273 TS_ASSERT_DELTA(voltages1[j], voltages2[i], tol);
274 if (!vOnly)
275 {
276 TS_ASSERT_DELTA(calcium1[j], calcium2[i], tol/100);
277 TS_ASSERT_DELTA(h1[j], h2[i], tol/10);
278 }
279 }
280}
281
282
283#endif //_RUNANDCHECKIONICMODELS_HPP_
#define EXCEPTION(message)
virtual void SetStateVariables(const std::vector< double > &rVariables)=0
virtual void ComputeExceptVoltage(double tStart, double tEnd)=0
virtual double GetVoltage()=0
virtual std::vector< double > GetStdVecStateVariables()=0
virtual OdeSolution Compute(double tStart, double tEnd, double tSamp=0.0)=0
virtual void SetVoltage(double voltage)=0
bool HasValues(const std::string &rVariableName)
std::vector< double > GetValues(const std::string &rVariableName)
static HeartConfig * Instance()
void WriteToFile(std::string directoryName, std::string baseResultsFilename, std::string timeUnits, unsigned stepsPerRow=1, bool cleanDirectory=true, unsigned precision=8, bool includeDerivedQuantities=false)