-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexp.cpp
More file actions
1878 lines (1541 loc) · 52.1 KB
/
exp.cpp
File metadata and controls
1878 lines (1541 loc) · 52.1 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------------------------------
NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE
Copyright 2004, The Johns Hopkins University
School of Medicine. All rights reserved.
For research use only; commercial use prohibited.
Distribution without permission of Raimond L. Winslow
not permitted. rwinslow@bme.jhu.edu
Name of Program: Guinea Pig C++ (Coupled) (GPC_Coupled)
Version: Documented Version, version 1.0.1
Date: February 2004, August 2004
-----------------------------------------------------
*/
//Experiment class file
#include <iostream>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <float.h>
#include <gpc.h>
#include <states_index.h>
//Constructor for Experiment
Experiment::Experiment()
{
//physical constants
RT_over_F=(8.314*310.0)/96.5;
success=true;
notdone=true;
/* algebraic membrane potential method**/
extra_charge_myo=0;
extra_charge_SS=0;
extra_charge_NSR=0;
extra_charge_JSR=0;
extra_charge_MITO=0;
extra_q_myo=0;
extra_q_NSR=0;
extra_q_JSR=0;
extra_q_SS=0;
extra_q_MITO=0;
}
// Setup variables for Experiment
void Experiment::Setup(double t,double step)
{
// int k;
//general constants
FFlag=true;
h=step;
start_time=t;
tstep=t+step;
success=true;
notdone=true;
//weights for integrators
errweight[index_V] = 1E-2; //0
errweight[index_mNa] = 1.0; //1
errweight[index_hNa] = 1.0; //2
errweight[index_jNa] = 1.0; //3
errweight[index_xKs] = 1.0; //4
errweight[index_Nai] = 0.2; //5
errweight[index_Ki] = 1.0/132.0; //6
errweight[index_Cai] = 1000.0; //7
errweight[index_CaNSR] = 0.5; //8
errweight[index_CaSS] = 1000.0; //9
errweight[index_CaJSR] = 0.05; //10
errweight[index_C1_RyR] = 1.0; //11
//errweight[index_O1_RyR] = 1.0; //12
errweight[index_O2_RyR] = 1.0; //12
errweight[index_C2_RyR] = 1.0; //13
errweight[index_C0] = 1.0; //14
errweight[index_C1] = 1.0; //15
errweight[index_C2] = 1.0; //16
errweight[index_C3] = 1.0; //17
errweight[index_C4] = 1.0; //18
errweight[index_Open] = 1.0; //19
errweight[index_CCa0] = 1.0; //20
errweight[index_CCa1] = 1.0; //21
errweight[index_CCa2] = 1.0; //22
errweight[index_CCa3] = 1.0; //23
errweight[index_CCa4] = 1.0; //24
errweight[index_OCa] = 1.0; //25
errweight[index_yCa] = 1.0; //26
errweight[index_LTRPNCa] = 1.0; //27
errweight[index_HTRPNCa] = 1.0; //28
errweight[index_N0] = 1.0; //29
errweight[index_N1] = 1.0; //30
errweight[index_P0] = 1.0; //31
errweight[index_P1] = 1.0; //32
errweight[index_P2] = 1.0; //33
errweight[index_P3] = 1.0; //34
/**after ATP mod**/
errweight[index_ATPi] = 1.0/8.0; //35
/**after Mitochondria mod**/
errweight[index_Cam]= 1.0; //not sure
errweight[index_ADPm]= 1.0/10.0;
errweight[index_Dpsi]= 1.0/200.0;
errweight[index_NADH]= 1.0/15.0;
errweight[index_ISOC]= 1.0;
errweight[index_AKG]= 1.0;
errweight[index_SCoA]= 1.0;
errweight[index_Succ]= 1.0;
errweight[index_FUM]=1.0;
errweight[index_MAL]= 1.0;
errweight[index_Oaa]= 1.0;
}
//sets the current clamp
void Experiment::setCurrent(double start_time)
{
if (IF_mode){
//The following code produce the I-F experiment from Rice paper (JT)
Istim=0;
within_first_pulse = start_time>=time_on_Is1 && start_time<=time_off_Is1;
within_second_pulse = start_time>=time_on_Is2 && start_time<=time_off_Is2;
if (stimulusFlag) {
if (within_first_pulse || within_second_pulse)
Istim=Istim+pulse_amplitude;
}
else{
cerr << "Current Stimulus must be on during I-F experiment!" << endl;
exit(1);
}
}
else if (BB_mode){
Istim=0;
if (start_time-shift>=t1 && start_time-shift<=t2){
period=(1.0/high_freq)*1000; //(ms)
}
else{
period=(1.0/norm_freq)*1000; //(ms)
}
time_on_Is1=floor((start_time-shift)/period)*period;
time_off_Is1=time_on_Is1+pulse_duration;
if (stimulusFlag) {
if (((start_time-shift)>=time_on_Is1)&&((start_time-shift)<=time_off_Is1))
Istim=Istim+pulse_amplitude;
}
else{
cerr << "Current Stimulus must be on during BB experiment!" << endl;
exit(1);
}
}
else{
// time_on_Is1=static_cast<int>((start_time-shift)/period)*period;
time_on_Is1=floor((start_time-shift)/period)*period;
time_off_Is1=time_on_Is1+pulse_duration;
Istim=0;
if (stimulusFlag) {
if(((start_time-shift)>=time_on_Is1)&&((start_time-shift)<=time_off_Is1))
Istim=Istim+pulse_amplitude;
}
}
}
/* *************MAIN COMPUTE***************
This routine calls integrators that do the actual calculations
*/
void Experiment::compute(ofstream& outDataFile,ofstream& outCurrentFile,
ofstream& outDerivFile,double states[],double timenow)
{
// save initial conditions every ssiniperiod ms
if ((ssiniFlag)&&(fmod(timenow,ssiniperiod)<stepsize))
create_ini_file(states,statesize,timenow);
#if USE_CVODE==1
CVode_run(h, states); // CVode
update(states); //assigns local variables with values from the state array
printstates(outDataFile,outCurrentFile,outDerivFile,states,timenow); //Prints the data to an outfile
#else
int i;
//assigns the inital values into the array y0
for(i=0;i<statesize;i++)
y0[i]=states[i];
RK4(h, states); //Runge Kutta
update(states); //assigns local variables with values from the state array
printstates(outDataFile,outCurrentFile,outDerivFile,states,timenow); //Prints the data to an outfile
for(i=0;i<statesize;i++) //updating the values in states to their final value y1
states[i]=y1[i];
#endif
}
//Function F, computes values for the differential equations
//and the result is an array F or F1 of the values.
void Experiment::getF(double start_time,double states[],bool FFlag)
{
update(states);
setCurrent(start_time);
VclampMode(start_time);
getReversalPotentials();
getINa();
getIKs();
getIK1();
getINab();
getIKp();
getICa();
getICaK();
getINaK();
getINaCa();
getICab();
getIpCa();
getInsCa();
/*after ATP mod**/
getV_AM();
/**after Mitochondria mod**/
getATPm();
getDmuH();
getNAD();
getVCS();
getVACO();
getVIDH();
getVKGDH();
getVSL();
getVSDH();
getVFH();
getVMDH();
getVAAT();
getVNO_VHNe();
getVFO_VHFe();
getVATPase_Vhu();
getVANT_Vhleak();
// getVuni(); //moved up before getFNai
// getVnaCa();
// getVATP_XB();
getFNa();
getFxKs();
computeInCalFlux();
get_Force();
getF_trpmyo();
getFLTRPNCa();
getFHTRPNCa();
computeJtrpn();
getVuni(); // moved up
getVnaCa(); // from below (in the Mitene folder)
CHF();
getFNai();
getFKi();
getFCai();
getFCaSS();
getFCaJSR();
getFCaNSR();
getFV();
getFRyR();
getFCaL();
getFyCa();
getFOCa();
/**after ATP mod**/
getFATPi();
getF_mitene();
/**derivatives**/
if (membranepot_flag==1)
F[index_V]=0; //algebraic expression
else
F[index_V]=dV;
F[index_mNa]=dmNa;
F[index_hNa]=dhNa;
F[index_jNa]=djNa;
F[index_Nai]=dNai;
F[index_Ki]=dKi;
F[index_Cai]=dCai;
F[index_CaNSR]=dCaNSR;
F[index_CaSS]=dCaSS;
F[index_CaJSR]=dCaJSR;
F[index_C1_RyR]=dC1_RyR;
//F[index_O1_RyR]=dO1_RyR;
F[index_O2_RyR]=dO2_RyR;
F[index_C2_RyR]=dC2_RyR;
F[index_xKs]=dxKs;
F[index_C0]=dC0;
F[index_C1]=dC1;
F[index_C2]=dC2;
F[index_C3]=dC3;
F[index_C4]=dC4;
F[index_Open]=dOpen;
F[index_CCa0]=dCCa0;
F[index_CCa1]=dCCa1;
F[index_CCa2]=dCCa2;
F[index_CCa3]=dCCa3;
F[index_CCa4]=dCCa4;
F[index_yCa]=dyCa;
F[index_OCa]=dOCa;
F[index_LTRPNCa]=dLTRPNCa;
F[index_HTRPNCa]=dHTRPNCa;
F[index_N0] = dN0;
F[index_P0] = dP0;
F[index_P1] = dP1;
F[index_P2] = dP2;
F[index_P3] = dP3;
F[index_N1] = dN1;
/**after ATP mod**/
F[index_ATPi] = dATPi;
/**after Mitochondria mod**/
F[index_Cam]=dCam;
F[index_ADPm]=dADPm;
F[index_Dpsi]=dDpsi;
F[index_NADH]=dNADH;
F[index_ISOC]=dISOC;
F[index_AKG]=dAKG;
F[index_SCoA]=dSCoA;
F[index_Succ]=dSucc;
F[index_FUM]=dFUM;
F[index_MAL]=dMAL;
F[index_Oaa]=dOaa;
// Not really needed with CVode
#if USE_CVODE==0
if(FFlag==true)
{
for(int i=0;i<statesize;i++)
F1[i]=F[i];
}
#endif
}
//updates the local state variables to the current values located in the states array
void Experiment::update(const double states[])
{
mNa=states[index_mNa];
hNa=states[index_hNa];
jNa=states[index_jNa];
Nai=states[index_Nai];
Ki=states[index_Ki];
Cai=states[index_Cai];
CaNSR=states[index_CaNSR];
CaSS=states[index_CaSS];
CaJSR=states[index_CaJSR];
C1_RyR=states[index_C1_RyR];
//O1_RyR=states[index_O1_RyR];
O2_RyR=states[index_O2_RyR];
C2_RyR=states[index_C2_RyR];
O1_RyR=1.0-C1_RyR-C2_RyR-O2_RyR;
xKs=states[index_xKs];
C0=states[index_C0];
C1=states[index_C1];
C2=states[index_C2];
C3=states[index_C3];
C4=states[index_C4];
Open=states[index_Open];
CCa0=states[index_CCa0];
CCa1=states[index_CCa1];
CCa2=states[index_CCa2];
CCa3=states[index_CCa3];
CCa4=states[index_CCa4];
OCa=states[index_OCa];
yCa=states[index_yCa];
LTRPNCa=states[index_LTRPNCa];
HTRPNCa=states[index_HTRPNCa];
N0=states[index_N0];
P0=states[index_P0];
P1=states[index_P1];
P2=states[index_P2];
P3=states[index_P3];
N1=states[index_N1];
/**after ATP mod**/
ATPi=states[index_ATPi];
/**after Mitochondria mod**/
Cam=states[index_Cam];
ADPm=states[index_ADPm];
Dpsi=states[index_Dpsi];
NADH=states[index_NADH];
ISOC=states[index_ISOC];
AKG=states[index_AKG];
SCoA=states[index_SCoA];
Succ=states[index_Succ];
FUM=states[index_FUM];
MAL=states[index_MAL];
Oaa=states[index_Oaa];
if (membranepot_flag==1)
CalcMembranePotential();
else
V=states[index_V];
}
//******************************************** Start Adding code here
//*************************************************************** CHF
void Experiment::CHF()
{
if (chf_flag==1)
{
IK1 = chfsc_IK1*IK1;
Jup = chfsc_Jup*Jup;
INaCa = chfsc_INaCa*INaCa;
}
}
//************************************************************* CVODE
/*
* Integrator.C
*
* This file implements/calls integrators for solving ordinary differential equations
*
*/
#if USE_CVODE==1
/* ----------------------------------------------------
code to call CVode starts here
---------------------------------------------------- */
#include <sundialstypes.h> /* definitions of types realtype and */
/* integertype, and the constant FALSE */
#include <cvode.h> /* prototypes for CVodeMalloc, CVode, and */
/* CVodeFree, constants OPT_SIZE, BDF, NEWTON, */
/* SV, SUCCESS, NST,NFE,NSETUPS, NNI, NCFN, NETF */
#include <cvdense.h> /* prototype for CVDense, constant DENSE_NJE */
#include <nvector_serial.h> /* definitions of type N_Vector and macro */
/* NV_Ith_S, prototypes for N_VNew, N_VFree */
#include <dense.h> /* definitions of type DenseMat, macro DENSE_ELEM*/
/*
CVode specific functions
CVode is a stiff/non-stiff integrator written in C (not in C++)
To work this code needs header files and a cvode library
*/
extern "C" {
static N_Vector cvode_y;
static void *cvode_mem;
static realtype reltol,abstol;
static N_Vector ew;
static double cvode_t;
M_Env MachEnv;
static realtype ropt[OPT_SIZE];
static long int iopt[OPT_SIZE];
}
// Call CVode
void Experiment::CVode_run(double h,double states[])
{
int flag=0,i;
// call CVode to solve ydot(t)=f(y,t) with y(0) given
flag = CVode(cvode_mem, start_time+h, cvode_y, &cvode_t, NORMAL);
if (flag != SUCCESS) {
cerr<<"CVode failed, flag="<<flag<<"."<<endl;
}
// copy vector y to states which is returned to compute
for(i=0;i<statesize;i++)
states[i]=NV_Ith_S(cvode_y,i);
if (vclamp_flag==1) // set V to correct value
states[0]=y0[0];
}
// Extra function that acts as a front to CVode
extern "C" void func_f(long N, realtype time, N_Vector y, N_Vector ydot, void *f_data)
{
Test2->CVode_f(N, time, y, ydot, f_data);
}
// function f(t,y) for CVode, returns derivatives of variables to CVode
void Experiment::CVode_f(int N, double time, N_Vector y, N_Vector ydot, void *f_data)
{
int z;
static double states2[MAXSTATES];
bool flag=true;
// copy vector y to states2
for(z=0;z<N;z++)
states2[z]=NV_Ith_S(y,z);
// send states2 to getF which uses it to produce derivatives
getF(time,states2,flag);
// copy derivatives to vector ydot which is returned to CVode
for(z=0;z<N;z++) {
NV_Ith_S(ydot,z)=F[z];
}
}
// Initializes CVode
void Experiment::CVodeInit(double states[])
{
int i;
MachEnv = M_EnvInit_Serial((int)statesize);
if (MachEnv == NULL) {
cerr<<"Trouble with MachEnv in CVODE"<<endl;
exit(-3);
}
// Allocate memory for solution vector y(t)
cvode_y = N_VNew((int)statesize, MachEnv);
// Allocate memory for solution vector ew(t)
ew = N_VNew((int)statesize, MachEnv);
reltol = tolerance_relative;
abstol = tolerance_absolute;
// initialize vector cvode_y with states
for(i=0;i<statesize;i++)
NV_Ith_S(cvode_y,i)=states[i];
// use default values for options
for(i=0;i<OPT_SIZE;i++) {
ropt[i]=0.0;
iopt[i]=0;
}
iopt[MXSTEP]=1000000; //added by JT, taken from integrator.cpp of Canine model
// except for these
ropt[HMAX]=step_max; // Largest step size
ropt[HMIN]=step_min; // Smallest step size
// scale tolerance based on maximum value of state
for(i=0;i<statesize;i++) {
NV_Ith_S(ew,i)=abstol/errweight[i];
}
/* CVodeMalloc sets up initial settings for CVode. See
Integration method is BDF(Backward Differential Formula)
Other choice would be ADAMS but it is not as stable */
#if 0
// This method of calling CVODE does not pass error weights
cvode_mem = CVodeMalloc(statesize, func_f, start_time, cvode_y, BDF, NEWTON, SS, &reltol, &abstol,
NULL, NULL, TRUE, iopt, ropt, MachEnv);
#else
// We wish to pass errorweight to CVODE
cvode_mem = CVodeMalloc(statesize, func_f, start_time, cvode_y, BDF, NEWTON,
SV, &reltol, ew, NULL, NULL, TRUE, iopt, ropt, MachEnv);
#endif
if (cvode_mem == NULL) {
cerr<<"CVodeMalloc failed."<<endl;
exit(1);
}
/* CVDense is needed by Newton algorithm for solving linear system
The second NULL tells the solver to compute an approximation of the Jacobian. */
CVDense(cvode_mem, NULL, NULL);
}
void Experiment::CVodeExit(void)
{
N_VFree(cvode_y);
CVodeFree(cvode_mem);
M_EnvFree_Serial(MachEnv);
}
#else
/* ----------------------------------------------------
rk4 code starts here
----------------------------------------------------
Merson Modified Runge-Kutta 4th Order ADAPTIVE Step Algorithm
Kubicek, M., Marek, M. (1983). Computational methods in
Bifurcation theory and Dissipative Structures. pg. 84.
Passed Variables:
h timestep
states Contains initial state at time t on entry.
Contains final state at time t + tstep on exit
This routine controls the solution of the system of differential
equations from time=start_time to time=start_time+h by monitoring
the truncation error on each incremental step, and adjusting the
step_size based on the error after each attempted step.
*/
void Experiment::RK4(double h,double states[])
{
int i,z;
while(notdone)
{
if(success)
{ FFlag=true;//FFlag is used to indicate whether or not to send the derivatives to F1
getF(start_time,y0,FFlag);
}
for(z=0;z<statesize;z++)
{
k1[z]=h*F1[z];
y1[z]=y0[z]+k1[z]/3.0;
states[z]=y1[z];
}
FFlag=false;
getF(start_time+h/3.0,states,FFlag);
for(z=0;z<statesize;z++)
{
k2[z]=h*F[z];
y1[z]=y0[z]+(k1[z]+k2[z])/6.0;
states[z]=y1[z];
}
getF(start_time+h/3.0,states,FFlag);
for(z=0;z<statesize;z++)
{
k3[z]=h*F[z];
y1[z]=y0[z]+(k1[z]+3.0*k3[z])/8.0;
states[z]=y1[z];
}
getF(start_time+h/2.0,states,FFlag);
for(z=0;z<statesize;z++)
{
k4[z]=h*F[z];
y4[z]=y0[z]+(.5*k1[z])-(1.5*k3[z])+(2.0*k4[z]);
states[z]=y4[z];
y1[z]=y4[z];
}
getF(start_time+h,states,FFlag);
for(z=0;z<statesize;z++)
{
k5[z]=h*F[z];
y1[z]=y0[z]+(k1[z]+4.0*k4[z]+k5[z])/6.0;
states[z]=y1[z];
}
// Calculating the error
/////////////////////////////////////////////////////////////////
//update(states);
tr_error = 0;
for(i=0;i<statesize;i++)
{
errtmp = fabs((y4[i]-y1[i])*0.2*errweight[i]);
if (errtmp<big)
tr_error = max(errtmp, tr_error);
else
tr_error = big;
}
if (tr_error< tolerance_absolute)
{
for(i=0;i<statesize;i++)
{
// If the absolute size of solution is less than 1 thousanth of
// error margin, set value to zero
if (fabs(y1[i])< tolerance_absolute/1000)
y1[i] = 0.0;
states[i]=y1[i];
y0[i] = y1[i];
}
start_time = start_time+h;
success = true;
if (start_time>=tstep)
{
notdone =false;
}
else
{
h = .85*h*pow((tolerance_absolute/tr_error),.2);
notdone = true;
}
}
else {
if (h <= step_min)
{
for(i=0;i<statesize;i++)
{
if (fabs(y1[i])<tolerance_absolute/1000)
{
y1[i] = 0.0;
}
y0[i] = y1[i];
states[i]=y1[i];
}
start_time = start_time+h;
success = true;
if (start_time >= tstep)
{
notdone = false;
}
else
{
h = step_min;
notdone = true;
}
}
else
{
h = .85*h*pow((tolerance_absolute/tr_error),.2);
success = false;
notdone = true;
}
}
h = min(step_max,max(step_min,h));
h = min(h,tstep-start_time);
if (h < .0000000001)
notdone = false;
}
}
#endif
//******************************************************** MembranPot
//gets the time rate of change for V
void Experiment::getFV()
{
if (vclamp_flag==1 || membranepot_flag==1)
{
CalcMembranePotential();
dV=0;
y0[0]=V;
}
else
{
dV=(-(INa+ICa+ICaK+IKs+IK1+IKp+INaCa+INaK+InsCa+IpCa+ICab+INab+Istim)/C_m);
}
}
//sets the voltage clamp
void Experiment::VclampMode(double start_time)
{
double ramp;
if (vclamp_flag==1)
{
double time_vclamp_on1 = floor((start_time-shift)/period)*period;
double vclamp_duration = time_vclamp_off - time_vclamp_on;
if (((start_time-shift) >= time_vclamp_on1+time_vclamp_on) &&
((start_time-shift) < time_vclamp_on1+time_vclamp_on+vclamp_duration))
{
ramp = (((start_time-shift)-time_vclamp_on1-time_vclamp_on)/2.0)
*(vclamp_set-vclamp_hold) + vclamp_hold;
if (vclamp_hold<=vclamp_set)
V = std::min(vclamp_set,ramp); // depol. steps
else
V = std::max(vclamp_set,ramp); // hyperpol. steps
}
else if ((start_time-shift)<(time_vclamp_on1+time_vclamp_on))
{
V = vclamp_hold;
}
else
{
ramp = vclamp_set +((time_vclamp_on1+time_vclamp_on
+ vclamp_duration-(start_time-shift))/2.0)*(vclamp_set-vclamp_hold);
if (vclamp_hold<=vclamp_set)
V = std::max(vclamp_hold,ramp); // depol. step
else
V = std::min(vclamp_hold,ramp); // hyper. step
}
}
}
/* algebraic membrane potential method */
void Experiment::CalcMembranePotential()
{
if (membranepot_flag==1) {
double F_JSR, F_i, Ca_all;
double Na_all, K_all, extra, Co;
double a1;
//Take out the Ca++ buffering in Subspace
/*a1=CMDNSStot/(CaSS+KmCMDN);
//there is no EGTA and we need to add CMDNSStot to parameter file
double F_SS;
F_SS=1.0+a1;*/
a1=CSQNtot/(CaJSR+KmCSQN);
F_JSR=1.0+a1;
a1=CMDNtot/(Cai+KmCMDN);
F_i=1.0+a1;
//global
//Take out the Ca buffereing in subspace
/*Ca_all=Vmyo*(Cai*F_i+LTRPNCa+HTRPNCa)+VNSR*CaNSR+VSS*(F_SS*CaSS)+VJSR*F_JSR*CaJSR
+Vmito*Cam;*/
Ca_all=Vmyo*(Cai*F_i+LTRPNCa+HTRPNCa)+VNSR*CaNSR+VSS*CaSS+VJSR*F_JSR*CaJSR+Vmito*Cam;
//there is no EGTA
//note that LTRPNCa is in (mM), unlike the canine version LTRPNCa is %
Na_all=Vmyo*Nai;
K_all=Vmyo*Ki;
extra=(extra_charge_myo+extra_q_myo)*Vmyo+(extra_charge_NSR+extra_q_NSR)*VNSR
+(extra_charge_JSR+extra_q_JSR)*VJSR
+(extra_charge_SS+extra_q_SS)*VSS
+(extra_charge_MITO+extra_q_MITO)*Vmito;
Co=Vtotal*(2*Cao+Ko+Nao); //volume scaled to match the volume of a cell
/*
cout << "Nai " << Nai << endl;
cout << "Ki " << Ki << endl;
cout << "Na_all " << Na_all << endl;
cout << "K_all " << K_all << endl;
cout << "Ca_all " << Ca_all << endl;
cout << "Co " << Co << endl;
cout << "extra " << extra << endl;
*/
V=(Faraday*1000)/(Acap*C_m)*(Na_all+K_all+2*Ca_all-Co+extra);
y0[0]=V;
// cout << "calcMembrane: " << V << endl;
}
}
void Experiment::AlgDiff(double states[])
{
update(states);
double a1=Acap*C_m/(Vmyo*Faraday*1000);
if (vclamp_flag==1){
states[index_V]=vclamp_hold;
}
/*
cout << "stop" << endl;
//cin >> temp;
//CalcMembranePotential();
cout << states[index_V]<< endl;
cout << V << endl;
cout << a1 << endl;
cout << Vtotal << endl;
*/
extra_charge_myo=(states[index_V]-V)*a1*Vmyo/Vtotal;
extra_charge_SS+=extra_charge_myo;
extra_charge_NSR+=extra_charge_myo;
extra_charge_JSR+=extra_charge_myo;
extra_charge_MITO+=extra_charge_myo;
CalcMembranePotential(); //not sure
cout<<"Difference in concentrations to alg formulation: "<< extra_charge_myo<< " mM" <<endl;
cout<<"Extra charge used in cytosol: "<<extra_charge_myo<< " (mM)" << endl;
cout<<"Extra charge used in SS: "<< extra_charge_SS<< " (mM)" << endl;
cout<<"Extra charge used in NSR: "<< extra_charge_NSR<< " (mM)" << endl;
cout<<"Extra charge used in JSR: "<< extra_charge_JSR<< " (mM)" << endl;
cout<<"Extra charge used in MITO: " << extra_charge_MITO<< " (mM) "<< endl;
}
//******************************************************* ReversalPot
// Calculates reversal potentials
void Experiment::getReversalPotentials()
{
double a1=Ko+0.01833*Nao;
double a2=Ki+0.01833*Nai;
E_Na=RT_over_F*log(Nao/Nai);
E_K=RT_over_F*log(Ko/Ki);
E_Ks=RT_over_F*log(a1/a2); //IKs is dependent on both Na and K
if (Cai<1.0e-10)
Cai=1.0E-10;
E_Ca=0.5*RT_over_F*log(Cao/Cai);
}
//**************************************************************** INa
//getFNa will get the time rate of change of mNa, hNa, and jNa
void Experiment::getFNa()
{
/**paper
double a1=0.32*(V+47.13);
double MAlpha=a1/(1.0-exp(-0.1*(V+47.13)));
**/
/**fortran**/
double a1=0.32*(V+47.13);
double MAlpha;
if (V==-47.13)
MAlpha=3.2;
else
MAlpha = a1/(1.0-exp(-0.1*(V+47.13)));
// if statements taken out in calculation of MAlpha and dmNa
//
double MBeta = 0.08*exp(-V/11.0);
/**fortran**/
if (1.0/(MBeta+MAlpha)<0.03)
mNa=MAlpha/(MBeta+MAlpha);
//
dmNa = MAlpha*(1.0-mNa)-MBeta*mNa;
double HAlpha, HBeta, JAlpha, JBeta;
if (V<-40)
{
HAlpha = 0.135*exp((80.0+V)/-6.8);
HBeta = 3.56*exp(0.079*V)+310000.0*exp(0.35*V);
a1 = -127140.0*exp(0.2444*V);
double a2 = 3.474E-5*exp(-0.04391*V);
double a3 = 1.0+exp(0.311*(V+79.23));
JAlpha = (a1-a2)*(V+37.78)/a3;
a2 = 1.0+exp(-0.1378*(V+40.14));
JBeta = 0.1212*exp(-0.01052*V)/a2;
}
else
{
HAlpha = 0.0;
HBeta = 1.0/(0.13*(1+exp((V+10.66)/-11.1)));
JAlpha = 0.0;
a1 = 1.0+exp(-0.1*(V+32.0));
JBeta = 0.3*exp(-2.535E-7*V)/a1;
}
dhNa = HAlpha*(1.0-hNa)-HBeta*hNa;
djNa = JAlpha*(1.0-jNa)-JBeta*jNa;
}
//getINa gets the current value of INa
void Experiment::getINa()
{
INa=G_Na*(pow(mNa,3.0)*hNa*jNa*(V-E_Na));
}
//***************************************************** Concentration
//Intercellular Concentration calculations
//These methods get the time rate of change for Nai, Ki, and Cai
void Experiment::getFNai()
{
double a1=Acap/(Vmyo*Faraday*1000.0);
/*before Mitochondria mod
dNai=-( INa+INab+3.0*(INaCa+INaK)+InsNa )*a1;
*/
/**after Mitochondria mod**/
dNai=-(INa+INab+3.0*(INaCa+INaK)+InsNa)*a1-VnaCa*0.615;
}
void Experiment::getFKi()
{
double a1=Acap/(Vmyo*Faraday*1000.0);
dKi = 0;
// dKi=-(IKs+IK1+IKp+ICaK-2.0*INaK+InsK+Istim)*a1;
}
void Experiment::getFCai()
{
double a1=Acap/(2.0*Vmyo*Faraday*1000.0);
/** Commented out by JT, replace with Faraday*1000
double a1=Acap/(2*Vmyo*Faraday);
**/
double a3=ICab-2.0*INaCa+IpCa;
// scaling factor for volume correction
/*before Mitochondria mod
dCai=beta_i*(Jxfer-Jup-Jtrpn-a3*.5*a1);
*/
/**after Mitochondria mod**/
dCai=beta_i*(Jxfer-Jup-Jtrpn-a3*.5*a1+(-Vuni+VnaCa)*0.615);
}