/
gaCable.c
362 lines (278 loc) · 8.09 KB
/
gaCable.c
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
#include <stdio.h>
#include <math.h> /*per sin i cos*/
#include <stdlib.h> /*pel malloc i calloc*/
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "memory.h"
// what data to save
int writeData; /* command line input */
// simulation run time
double tStop; /* command line input */
// cable
double d; /* command line input */ //10; diffusion
int synCable; /* command line input */ // 5; input position
//capacitance
double c=1.;
// leak
double glk=1;
double glkCable=.1;
double vlk=-70;
// Na channel
double gna=37;
double vna=55;
// Na activation [m]
double thetam=30;
double sm=15;
// Na inactivation [h]
double thetah=-39;
double sh=3.1;
double tauh0=1;
double tauh1=500;
double thh=57;
double sigmah=3;
// K channel
double gk=45;
double vk=-80;
// K activation
double thetan=-32.;
double sn=-8.;
double taun0=1;
double taun1=100.;
double thn=80.;
double sigman=26.;
// A channel
double ga; /* command line input */
// A activation [a]
double taua = 2;
double thetaa = -50;
double sigmaa = 20;
// A inactivation [b]
double taub=150;
double thetab=-70;
double sigmab=-6;
// kinetics rescaling
double phi=0.75;
// synapse
double vsyne = 0.;
double vsyni = -85.;
double gsyne, gsyni; /* command line input */
double rsyne, rsyni; /* command line input */
double betae = .2;
double betai = .18;
double te, ti, teNext,tiNext; /* synapse event times */
double v,m,b,n,a,se,si;
double *vCable;
double minf,hinf,ninf,binf,ainf;
double tauh,taun,taua;
double ina,ik,ilk,ia,isyne,isyni;
double alphae;
//double twopi=8*atan(1.);
double twopi=3.1415926535;
int i;
int func (double t, const double x[], double dx[], void *params);
int jac (double t, const double y[], double *dfdy, double dfdt[], void *params);
int main(int argc, char *argv[]){
double *x,*dx;
double vlast,tspike;
double si0;
double rand;
int ndim=15;
FILE *output;
double tol=1.0e-5;
double mu;
//const gsl_odeiv_step_type * T = gsl_odeiv_step_rk8pd; // Runge-Kutta Prince-Dormand 8-9.
//const gsl_odeiv_step_type * T = gsl_odeiv_step_rk2imp; // Runge-Kutta 2 implicit.
const gsl_odeiv_step_type * T = gsl_odeiv_step_rk4imp; // Runge-Kutta 4 implicit.
//const gsl_odeiv_step_type * T = gsl_odeiv_step_bsimp; // Burlirsch-Stoer implicit.. Need jacobian!
//const gsl_odeiv_step_type * T = gsl_odeiv_step_gear1; // Gear 1 implicit.
//const gsl_odeiv_step_type * T = gsl_odeiv_step_gear2; // Gear 2 implicit.
gsl_odeiv_step * s = gsl_odeiv_step_alloc (T, ndim);
gsl_odeiv_control * c = gsl_odeiv_control_y_new (tol, 0.0);
gsl_odeiv_evolve * e = gsl_odeiv_evolve_alloc (ndim);
gsl_odeiv_system sys = {func, jac, ndim, &mu};
/* Random generator stuff */
const gsl_rng_type *U;
gsl_rng *r;
gsl_rng_env_setup();
U = gsl_rng_default;
r = gsl_rng_alloc (U);
/* Parameters From Command Line */
writeData = atof(argv[1]);
tStop = atof(argv[2]);
ga = atof(argv[3]);
gsyne = atof(argv[4]);
gsyni = atof(argv[5]);
rsyne = atof(argv[6]);
rsyni = atof(argv[7]);
d = atof(argv[8]);
synCable = atoi(argv[9]);
double t, tf;
double h = 1.e-3;
int status;
char name1[100];
int number1,number2,number3;
int ncount=0;
int nite;
double sum;
if (writeData==1){ /* save all data */
sprintf(name1, "data10cpt_ga%g_ge%.1f_gi%g_re%.1f_ri%g_d%g_syn%d.txt",ga,gsyne, gsyni, rsyne,rsyni,d,synCable);}
else if (writeData==2){ /* save spikes */
sprintf(name1, "spike10cpt_ga%g_ge%.1f_gi%g_re%.1f_ri%g_d%g_syn%d.txt",ga,gsyne, gsyni, rsyne,rsyni,d,synCable);}
output=fopen(name1, "w");
/*open the data file*/
output=fopen(name1, "w");
if (output==NULL){
printf("Error in opening data file\n");
exit(1);
}
/*memory allocation*/
x = new_vector(0, ndim-1);
dx = new_vector(0, ndim-1);
vCable = new_vector(0,8);
/*Initial point*/
double v0=-70;
x[0]=v0; /* v */
x[1]=1./(1.+exp((v0-thetan)/sn)); /* n */
x[2]=1/(1+exp(-(v0-thetaa)/sigmaa)); /* a */
x[3]=1/(1+exp(-(v0-thetab)/sigmab)); /* b */
x[4]=0; /* se */
x[5]=0; /* si */
for (i=6;i<15;i++){x[i] = v0;} /* cable */
/*integrate*/
t=0.;
nite=0.;
vlast=v0;
te = 0;
ti = -999.; tiNext = 1000./rsyni; si0 = 0;
te = -999.; rand =gsl_ran_flat(r, 0,1); teNext = -1000.*log(1-rand)/(rsyne);
if (teNext<tiNext){tf=teNext;}
else {tf=tiNext;}
while (t<tStop){
while (t<tf){
status = gsl_odeiv_evolve_apply (e, c, s, &sys, &t, tf, &h, x);
if (status != GSL_SUCCESS)
break;
if (writeData==1){ /* save everything */
fprintf(output," %.5f ",t);
for (i=0;i<ndim;i++){
fprintf(output," %.5f ",x[i]);
}
fprintf(output, "\n");
}
/* spike detection */
tspike =0.;
if ((x[0]>=-10)&&(vlast<-10)){
ncount++;
tspike=t;
if (writeData==2){ /* save spikes */
fprintf(output," %.5f %.5f",tspike, si0); fprintf(output, "\n");
}
}
vlast=x[0];
nite++;
}
// generate next excitatory event
if (tf>=teNext){
te = teNext;
rand=gsl_ran_flat(r, 0,1); teNext+=-1000.*log(1-rand)/(rsyne);
// for instantaneous rise se
x[4]=1.; // ceiling at 1
si0 = x[5]; // si value at the excitatory event onset
if (writeData==2){fprintf(output," %.5f %.5f ",-tf, x[5]); fprintf(output, "\n");} /* save excitatory events */
}
// generate next inhibitory event
if (tf>=tiNext){tiNext+=1000./rsyni; x[5]=1.;} // ceiling at 1
if (teNext<tiNext){tf=teNext;}
else {tf=tiNext;}
}
if (writeData==2){fprintf(output," %.5f %.5f ",tStop , tStop); fprintf(output, "\n");}
free_vector(x,0);
free_vector(dx,0);
fclose(output);
gsl_odeiv_evolve_free (e);
gsl_odeiv_control_free (c);
gsl_odeiv_step_free (s);
}
/* Vector field we want to integrate*/
int func (double t, const double x[], double dx[], void *params)
{
double mu = *(double *)params;
double m3,n4,a3;
// general variables
v = x[0];
m = 1./(1.+exp(-(v+thetam)/sm)); // m—>minf instantaneous
n = x[1];
a = x[2] ; // 1/(1+exp(-(v-thetaa)/sigmaa)); // a->ainf instantaneous
b = x[3];
se= x[4];
si= x[5];
for (i=0; i<9; i++){vCable[i] = x[6+i];}
// currents
m3=m*m*m;
n4=n*n*n*n;
a3=a*a*a;
ina= gna*m3*(1-n)*(v-vna);
ik = gk*n4*(v-vk);
ia = ga*a3*b*(v-vk);
ilk= glk*(v-vlk);
isyni = gsyni*si*(v-vsyni);
/* update voltage at soma */
dx[0] = - (ilk + ina + ik + ia + isyni)/c;
/* update n gating */
ninf=1./(1.+exp((v-thetan)/sn));
taun=taun0+taun1/(1+exp((v+thn)/sigman));
dx[1]=phi*(ninf-n)/taun;
/* update a gating */
ainf = 1/(1+exp(-(v-thetaa)/sigmaa));
dx[2] = (ainf-a)/taua;
/* update b gating */
binf = 1./(1+exp(-(v-thetab)/sigmab));
dx[3]=(binf-b)/taub;
/* update excitation */
dx[4] = -betae*se; // instantaneous rise, exponential decay
/* update inhibition */
dx[5] = -betai*si;
/* CABLE */
for (i=0; i<9; i++){
dx[6+i] = - (glkCable*(vCable[i]-vlk) )/c;
}
dx[0] += (d)*(vCable[0]-v)/c;
dx[6] += d*(v-2*vCable[0]+vCable[1])/c;
for (i=1;i<8;i++){
dx[6+i] += (d/c)*(vCable[i-1]-2*vCable[i]+vCable[i+1]);
}
dx[14] += (d)*(vCable[7]-vCable[8]) /c ;
if (synCable==0){
dx[0] -= (gsyne*se*(v-vsyne)) / c; // soma
}
else{
dx[5+synCable] -= (gsyne*se*(vCable[synCable-1]-vsyne)) / c; // cable
}
return GSL_SUCCESS;
}
/* The jacobian
* In case we wanna use implict Burslish-Stoer methods
*/
int jac (double t, const double y[], double *dfdy, double dfdt[], void *params)
{
double mu = *(double *)params;
gsl_matrix_view dfdy_mat = gsl_matrix_view_array (dfdy, 2, 2);
gsl_matrix * m = &dfdy_mat.matrix;
// Si usamos Burlish-Stoer tenemos de dar el Jacobiano
/*gsl_matrix_set (m, 0, 0, 0.0);
gsl_matrix_set (m, 0, 1, 1.0);
gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0));*/
// Si usamos otro método podemos dejar el jacobiano
// todo a cero.
gsl_matrix_set (m, 0, 0, 0.0);
gsl_matrix_set (m, 0, 0, 0.0);
gsl_matrix_set (m, 0, 0, 0.0);
gsl_matrix_set (m, 0, 0, 0.0);
dfdt[0] = 0.0;
dfdt[1] = 0.0;
return GSL_SUCCESS;
}