-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
350 lines (307 loc) · 12.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
// Genetic Algorithm
#include "lib/Genetic_Algorithm/include/main_ga.hpp"
#include "lib/Genetic_Algorithm/include/random.hpp"
#include "lib/Genetic_Algorithm/include/globalSetup.hpp"
// Livshitz Rudy 2009 Model
#include "lib/LivRudy2009/include/LivRudy2009.hpp"
#include "lib/LivR_SteadyState_Prediction/LivR_SS_Prediction.hpp"
// Global variables
GlobalSetup *globalSetup;
Random myRandom;
std::vector< std::vector<double> > objectiveTraces; // Acquired at 10 kHz
std::vector< std::vector<double> > protocolTraces; // Acquired at 10 kHz
#define PROTOCOLDT 0.1 // (ms) Time step of protocol data
#define MINDT 0.001 // (ms) minimum dt, 1000 kHz integration
#define MAXDT 0.1 // (ms) maximum dt, 10kHz integration, should be <= PROTOCOLDT
// Model simulation and objective evaluation
void globalEvaluate(double *parameter, double *error,
double *constraintViolation, double *penalty,
int *noOfViolations) {
LivRudy2009 model;
// Each thread will make a copy of objectives and protocols. While this does
// have an overhead, it is neglible compared to running the simulation and
// makes the code cleaner while protecting the shared variable.
std::vector< std::vector<double> > objectives, protocols;
#pragma omp critical
{
objectives = objectiveTraces;
protocols = protocolTraces;
}
// Scale model parameters
model.setGNa(model.getGNa() * parameter[0]);
model.setGNab(model.getGNab() * parameter[1]);
model.setGCaL(model.getGCaL() * parameter[2]);
model.setGCaT(model.getGCaT() * parameter[3]);
model.setGCab(model.getGCab() * parameter[4]);
model.setGK1(model.getGK1() * parameter[5]);
model.setGKr(model.getGKr() * parameter[6]);
model.setGKs(model.getGKs() * parameter[7]);
model.setGKp(model.getGKp() * parameter[8]);
model.setGNaK(model.getGNaK() * parameter[9]);
model.setGNCX(model.getGNCX() * parameter[10]);
model.setGpCa(model.getGpCa() * parameter[11]);
model.setGserca(model.getGserca() * parameter[12]);
//Predict and set initial concentrations
LivR_SS_Prediction intialConcentrations;
model.setNai(
intialConcentrations.predict_Nai(
parameter[0], parameter[1], parameter[2], parameter[3], parameter[4],
parameter[5], parameter[6], parameter[7], parameter[8], parameter[9],
parameter[10], parameter[11], parameter[12]));
model.setKi(
intialConcentrations.predict_Ki(
parameter[0], parameter[1], parameter[2], parameter[3], parameter[4],
parameter[5], parameter[6], parameter[7], parameter[8], parameter[9],
parameter[10], parameter[11], parameter[12]));
model.setCai(
intialConcentrations.predict_Cai(
parameter[0], parameter[1], parameter[2], parameter[3], parameter[4],
parameter[5], parameter[6], parameter[7], parameter[8], parameter[9],
parameter[10], parameter[11], parameter[12]));
model.setCaJSR(
intialConcentrations.predict_CaJSR(
parameter[0], parameter[1], parameter[2], parameter[3], parameter[4],
parameter[5], parameter[6], parameter[7], parameter[8], parameter[9],
parameter[10], parameter[11], parameter[12]));
model.setCaNSR(
intialConcentrations.predict_CaNSR(
parameter[0], parameter[1], parameter[2], parameter[3], parameter[4],
parameter[5], parameter[6], parameter[7], parameter[8], parameter[9],
parameter[10], parameter[11], parameter[12]));
double dt = MAXDT; // Adaptive timestep, starting at max
double dVdt; // dVdt is used to modify timestep
const double dVdtThresh = MAXDT * 2; // If dVdt is less than this, reduce dt
double v0 = model.getVm(); // Voltage of previous timestep to calculate dVdt
int steps = dt / PROTOCOLDT; // Number of integration steps
const int maxSteps = PROTOCOLDT / MINDT; // Max number of integration steps
// Static pacing beats before each perturbation to elminate transients
int numPrelimBeats = 25;
int numPerturbBeats = 5;
double cm = protocols.back().at(0) * 1e-12;
protocols.resize(protocols.size() - 1); // Remove cm once extracted
// Vector for voltage differences between simulation and objective
std::vector< std::vector<double> >
vmDiff(protocols.size(),
std::vector<double>(protocols.at(0).size()));
// Static pacing, using static pacing current
// Conditions are saved, and model is set to those initial conditions before
// each perturbation.
for (int z = 0; z < numPrelimBeats; z++) {
auto it = protocols.at(0).begin();
// Extract current from protocol and scale by cm each loop
// Loop will exit if model crashes
double v0 = model.getVm(); // Get initial voltage for dVdt evaluation
// Steps through each step of the protocol and inject current
while (it != protocols.at(0).end()) {
dVdt = std::abs(model.getVm() - v0) / PROTOCOLDT;
v0 = model.getVm();
// Adaptive dt calculation
// Voltage is changing less than threshold, so use max dt
if (dVdt < dVdtThresh) {
dt = MAXDT;
model.setDt(dt);
steps = PROTOCOLDT / dt;
}
else { // Voltage is changing quickly, so reduce dt up to minimum
steps = std::ceil(dVdt / dVdtThresh); // Round up to an integer
if (steps > maxSteps)
steps = maxSteps;
dt = PROTOCOLDT / steps;
model.setDt(dt);
}
int idx = 0;
// Integrate using adaptive dt, loop breaks if model crashes
while (idx < steps && model.iClamp(*it / cm * -1)) {
idx++;
}
if (model.getStatus())
it++; // Increment to next protocol step if model did not crash
else
break; // if model crashed, exit loop
}
}
std::vector<double> conditions(model.getConditions());
// Run each protocol
for (int i = 0; i < protocols.size(); i++) {
// Set conditions to after static pacing beats
model.setConditions(conditions);
// Current perturbation for 10 beats
// First set of 5 beats are not recorded
for (int z = 0; z < numPerturbBeats; z++) {
auto it = protocols.at(i).begin();
// Extract current from protocol and scale by cm each loop
// Loop will exit if model crashes
double v0 = model.getVm(); // Get initial voltage for dVdt evaluation
// Steps through each step of the protocol and inject current
while (it != protocols.at(i).end()) {
dVdt = std::abs(model.getVm() - v0) / PROTOCOLDT;
v0 = model.getVm();
// Adaptive dt calculation
// Voltage is changing less than threshold, so use max dt
if (dVdt < dVdtThresh) {
dt = MAXDT;
model.setDt(dt);
steps = PROTOCOLDT / dt;
}
else { // Voltage is changing quickly, so reduce dt up to minimum
steps = std::ceil(dVdt / dVdtThresh); // Round up to an integer
if (steps > maxSteps)
steps = maxSteps;
dt = PROTOCOLDT / steps;
model.setDt(dt);
}
int idx = 0;
// Integrate using adaptive dt, loop breaks if model crashes
while (idx < steps && model.iClamp(*it / cm * -1)) {
idx++;
}
if (model.getStatus())
it++; // Increment to next protocol step if model did not crash
else
break; // if model crashed, exit loop
}
}
// Second set of 5 beats are summated
// Initialize vector for voltage summation
std::vector<double> vmData(protocols.at(i).size(), 0.0);
for (int z = 0; z < numPerturbBeats; z++) {
auto it = protocols.at(i).begin();
auto ot = vmData.begin();
// Extract current from protocol and scale by cm each loop
// Loop will exit if model crashes
double v0 = model.getVm(); // Get initial voltage for dVdt evaluation
// Steps through each step of the protocol and inject current
while (it != protocols.at(i).end()) {
dVdt = std::abs(model.getVm() - v0) / PROTOCOLDT;
v0 = model.getVm();
// Adaptive dt calculation
// Voltage is changing less than threshold, so use max dt
if (dVdt < dVdtThresh) {
dt = MAXDT;
model.setDt(dt);
steps = PROTOCOLDT / dt;
}
else { // Voltage is changing quickly, so reduce dt up to minimum
steps = std::ceil(dVdt / dVdtThresh); // Round up to an integer
if (steps > maxSteps)
steps = maxSteps;
dt = PROTOCOLDT / steps;
model.setDt(dt);
}
int idx = 0;
// Integrate using adaptive dt, loop breaks if model crashes
while (idx < steps && model.iClamp(*it / cm * -1)) {
idx++;
}
if (model.getStatus()) {
*ot += model.getVm(); // Save running sum of voltage
it++; // Increment to next protocol step if model did not crash
ot++;
}
else
break; // if model crashed, exit loop
}
}
// Divide by number of beats for average
std::transform(vmData.begin(), vmData.end(), vmData.begin(),
[=] (double sumVm) {
return sumVm / numPerturbBeats;
});
// Calculate difference between average voltage and objective and
// save to vmDiff vector.
std::transform(vmData.begin(), vmData.end(),
objectives.at(i).begin(),
vmDiff.at(i).begin(),
[] (double modelVm, double objectiveVm) {
return std::abs(modelVm - objectiveVm);
});
}
// If model crashed, set error to arbitrarily high value
if (!model.getStatus()) {
error[0] = 400 * 5000 * 8;
}
else { // Perform normal error calculation
// Calculate total error
double totalError = 0;
std::for_each(vmDiff.begin(), vmDiff.end(),
// Summate differences between simulation and objective
// and add to total error
[&totalError] (std::vector<double> simVsObj) {
totalError += std::accumulate(simVsObj.begin(),
simVsObj.end(),
0.0);
});
error[0] = totalError;
}
#pragma omp ordered
{
if (globalSetup->savePopulation) {
FILE *outEvals = fopen(globalSetup->saveEvalSolutions, "a");
for (int i = 0; i < globalSetup->noOfDecisionVariables; i++) {
fprintf(outEvals, "%f\t", parameter[i]);
}
for (int i = 0; i < globalSetup->finalNoOfObjectives; i++) {
fprintf(outEvals, "%f\t", error[i]);
}
if (globalSetup->finalNoOfConstraints > 0) {
for (int i = 0; i < globalSetup->finalNoOfConstraints; i++) {
fprintf(outEvals, "%f\t", constraintViolation[i]);
}
fprintf(outEvals, "%f", *penalty);
}
fprintf(outEvals, "\n");
fflush(outEvals);
fclose(outEvals);
}
}
}
int main(int argc, char *argv[]) {
// Input objective and protocol traces from data files
std::string line;
// Objective
std::ifstream objFile(argv[1]);
if (!objFile.good()) {
std::cout << "Error: unable to open objective file" << std::endl;
return EXIT_FAILURE;
}
while (std::getline(objFile, line, '\n')) {
std::vector<double> lineData;
std::stringstream lineStream(line);
double value;
while (lineStream >> value)
lineData.push_back(value);
objectiveTraces.push_back(lineData);
}
// Protocol
std::ifstream proFile(argv[2]);
if (!proFile.good()) {
std::cout << "Error: unable to open protocol file" << std::endl;
return EXIT_FAILURE;
}
while (std::getline(proFile, line, '\n')) {
std::vector<double> lineData;
std::stringstream lineStream(line);
double value;
while (lineStream >> value)
lineData.push_back(value);
protocolTraces.push_back(lineData);
}
// Double check protocol and objective matrixes match desired dimensions
// Protocol should have one extra row, since it contains membrane capacitance
if (protocolTraces.size() - 1 != objectiveTraces.size()) {
std::cout <<
"Error: Protocol and objective sizes do not match" << std::endl <<
"Protocol size - 1: " << protocolTraces.size() - 1 << std::endl <<
"Protocol file: " << argv[1] << std::endl <<
"Objective size: " << objectiveTraces.size() << std::endl <<
"Objective file: " << argv[2] << std::endl;
return EXIT_FAILURE;
}
run_GA(argv[3]);
return EXIT_SUCCESS;
}