-
Notifications
You must be signed in to change notification settings - Fork 0
/
synapse_degradation_ionDynamics.lua
494 lines (397 loc) · 16.3 KB
/
synapse_degradation_ionDynamics.lua
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
--------------------------------------------------------------------------------
-- This script is intended to examine the effect of synapse loss on single --
-- cell electrical and calcium signals. --
-- It solves the cable equation on a single cell including Hodgkin-Huxley- --
-- type K+ and Na+ channels, leakage fluxes and a Na+/K+ pump. --
-- Calcium dynamics are unbuffered and involve transport through the plasma --
-- membrane through VDCCs, PMCA and NCX pumps as well as a leakage. --
-- Activation is realized through alpha-type synapses that are randomly --
-- distributed across the dendrites of the cell. A percentage of these --
-- synapses can be removed prior to the simulation. --
-- --
-- Authors: Markus Breit, Martin Stepniewski --
-- Date: 2015-09-10 --
--------------------------------------------------------------------------------
ug_load_script("ug_util.lua")
ug_load_script("util/load_balancing_util.lua")
-- init UG
InitUG(3, AlgebraType("CPU", 1))
AssertPluginsLoaded({"cable_neuron"})
--------------------------------------------------------------------------------
-- Settings
--------------------------------------------------------------------------------
cell = util.GetParam("-cellName", "12-L3pyr")
if not cell == "12-L3pyr" or not cell == "31o_pyr" then
exit("Cell not specified correctly. Type '12-L3pyr' or '31o_pyr'.")
end
if cell == "12-L3pyr" then
gridName = "grids/13-L3pyr-77.CNG.ugx"
gridSyn = "grids/13-L3pyr-77.CNG_syn.ugx"
gridDeg = "grids/13-L3pyr-77.CNG_syn_deg.ugx"
distro = {0.0, 0.0, 0.5, 0.5}
neededSubsets = {"soma", "axon", "dendrite", "apical_dendrite"}
dendSubsets = "dendrite, apical_dendrite"
else
gridName = "grids/31o_pyramidal19aFI.CNG.ugx"
gridSyn = "grids/31o_pyramidal19aFI.CNG_syn.ugx"
gridDeg = "grids/31o_pyramidal19aFI.CNG_syn_deg.ugx"
distro = {0.0, 1.0, 0.0}
neededSubsets = {"soma", "dendrite", "axon"}
dendSubsets = "dendrite"
end
-- parameters steering simulation
numRefs = util.GetParamNumber("-numRefs", 0)
dt = util.GetParamNumber("-dt", 1e-5) -- in s
endTime = util.GetParamNumber("-endTime", 1.0) -- in s
nSteps = util.GetParamNumber("-nSteps", endTime/dt)
pstep = util.GetParamNumber("-pstep", dt, "plotting interval")
-- synapse activity parameters
avg_start = util.GetParamNumber("-avgStart", 0.01)
avg_dur = util.GetParamNumber("-avgDur", 2.4e-3)
dev_start = util.GetParamNumber("-devStart", 0.003)
dev_dur = util.GetParamNumber("-devDur", 0.0)
num_synapses = util.GetParamNumber("-nSyn", 140)
-- specify "-verbose" to output linear solver convergence
verbose = util.HasParamOption("-verbose")
-- vtk output?
generateVTKoutput = util.HasParamOption("-vtk")
-- file handling
outPath = util.GetParam("-outName", "solution")
outPath = outPath.."/"
--------------------------------------------------------------------------------
-- Synapse distributions via plugin by Lukas Reinhardt
--------------------------------------------------------------------------------
---[[
synDistr = SynapseDistributor(gridName)
synDistr:clear() -- clear any synapses from grid
synDistr:place_synapses(distro, num_synapses, "AlphaPostSynapse")
export_succes = synDistr:export_grid(gridSyn)
print("SynapseDistributor grid export successful: " .. tostring(export_succes))
--]]
gridName = gridSyn
--------------------------------------------------------------------------------
-- Synapse degeneration
--------------------------------------------------------------------------------
---[[
deg_factor = util.GetParamNumber("-degFac", 0.0)
deg_factor = deg_factor --+ 0.5/num_synapses -- rounding instead of floor-ing
synDistr = SynapseDistributor(gridName)
--synDistr:print_status()
if cell == "12-L3pyr" then
synDistr:degenerate_uniform(deg_factor, 2) -- first factor means: newNumber = (1-factor)*oldNumber
synDistr:degenerate_uniform(deg_factor, 3) -- second param is the subset index
else
synDistr:degenerate_uniform(deg_factor, 1)
end
synDistr:print_status()
synDistr:export_grid(gridDeg)
gridName = gridDeg
--]]
--------------------------
-- biological settings --
--------------------------
-- settings are according to T. Branco
-- membrane conductances (in units of S/m^2)
g_k_ax = 400.0 -- axon
g_k_so = 200.0 -- soma
g_k_de = 30 -- dendrite
g_na_ax = 3.0e4
g_na_so = 1.5e3
g_na_de = 40.0
g_l_ax = 200.0
g_l_so = 1.0
g_l_de = 1.0
-- specific capacitance (in units of F/m^2)
spec_cap = 1.0e-2
-- resistivity (in units of Ohm m)
spec_res = 1.5
-- reversal potentials (in units of V)
e_k = -0.09
e_na = 0.06
e_ca = 0.1377547409
-- equilibrium concentrations (in units of mM)
-- these concentrations will yield Nernst potentials as given above
k_out = 4.8261178697
na_out = 113.2925416647
ca_out = 1.5
k_in = 140.0
na_in = 12.0
ca_in = 5e-5
-- equilibrium potential (in units of V)
v_eq = -0.065
-- diffusion coefficients (in units of m^2/s)
diff_k = 1.0e-9
diff_na = 1.0e-9
diff_ca = 2.2e-10
-- temperature in units of deg Celsius
temp = 37.0
--------------------------------------------------------------------------------
-- Create, Load, Refine Domain
--------------------------------------------------------------------------------
dom = util.CreateDomain(gridName, numRefs, neededSubsets)
-- check domain is acyclic
isAcyclic = is_acyclic(dom)
if not isAcyclic then
print("Domain is not acyclic!")
exit()
end
--------------------------------------------------------------------------------
-- create Approximation Space
--------------------------------------------------------------------------------
--print("Create ApproximationSpace needs to be somewhere else")
approxSpace = ApproximationSpace(dom)
approxSpace:add_fct("v", "Lagrange", 1)
approxSpace:add_fct("k", "Lagrange", 1)
approxSpace:add_fct("na", "Lagrange", 1)
approxSpace:add_fct("ca", "Lagrange", 1)
approxSpace:init_levels()
approxSpace:init_surfaces()
approxSpace:init_top_surface()
approxSpace:print_layout_statistic()
approxSpace:print_statistic()
OrderCuthillMcKee(approxSpace, true)
-- cable equation
CE = CableEquation("soma, axon, " .. dendSubsets, true)
CE:set_spec_cap(spec_cap)
CE:set_spec_res(spec_res)
CE:set_rev_pot_k(e_k)
CE:set_rev_pot_na(e_na)
CE:set_rev_pot_ca(e_ca)
CE:set_k_out(k_out)
CE:set_na_out(na_out)
CE:set_ca_out(ca_out)
CE:set_diff_coeffs({diff_k, diff_na, diff_ca})
CE:set_temperature_celsius(temp)
-- Hodgkin and Huxley channels
HH = ChannelHHNernst("v", "axon, soma, " .. dendSubsets)
HH:set_conductances(g_k_ax, g_na_ax, "axon")
HH:set_conductances(g_k_so, g_na_so, "soma")
HH:set_conductances(g_k_de, g_na_de, dendSubsets)
CE:add(HH)
-- leakage
tmp_fct = math.pow(2.3,(temp-23.0)/10.0)
leak = ChannelLeak("v", "axon, soma, " .. dendSubsets)
leak:set_cond(g_l_ax*tmp_fct, "axon")
leak:set_rev_pot(-0.06614845186, "axon") -- -0.066210342630746467, "axon")
leak:set_cond(g_l_so*tmp_fct, "soma")
leak:set_rev_pot(-0.03065400447, "soma") -- -0.022074360525636, "soma")
leak:set_cond(g_l_de*tmp_fct, dendSubsets)
leak:set_rev_pot(-0.0578036208, dendSubsets) -- -0.056314322586687, dendSubsets)
CE:add(leak)
-- Calcium dynamics
vdcc = VDCC_BG_cable("ca", "soma, " .. dendSubsets)
ncx = NCX_cable("ca", "soma, " .. dendSubsets)
pmca = PMCA_cable("ca", "soma, " .. dendSubsets)
caLeak = IonLeakage("ca", "soma, " .. dendSubsets)
leakCaConst = -3.4836065573770491e-9 + -- single pump PMCA flux density (mol/s/m^2)
-1.0135135135135137e-9 + -- single pump NCX flux (mol/s/m^2)
3.3017662162505882e-11
caLeak:set_perm(leakCaConst, ca_in, ca_out, v_eq, 2)
CE:add(ncx)
CE:add(pmca)
CE:add(vdcc)
CE:add(caLeak)
-- Na-K pump -- balances K+ efflux from HH (on soma and dendrites)
-- and Na+ influx from HH (on axons)
tmp = 1.0 / (1.0 + 5.74/na_in * (1.0 + k_in/1.37))
tmp = tmp*tmp*tmp
nak_ax = Na_K_Pump("", "axon")
nak_ax:set_max_flux(1.0/tmp*1.102782816e-05) -- HH (mol/s/m^2)
nak_so = Na_K_Pump("", "soma")
nak_so:set_max_flux(1.5/tmp*1.69383842e-06) -- HH (mol/s/m^2)
nak_de = Na_K_Pump("", dendSubsets)
nak_de:set_max_flux(1.5/tmp*2.54075763e-07) -- HH (mol/s/m^2)
CE:add(nak_ax)
CE:add(nak_so)
CE:add(nak_de)
-- ion leakage -- balances Na+ influx from HH and efflux from Na+/K+ pumps (on soma and dendrites)
-- and K+ efflux from HH and influx from Na+/K+ pumps (on axons)
kLeak_ax = IonLeakage("k", "axon")
leakKConst_ax = -3.387676841e-06 + -- HH (mol/s/m^2)
7.35189e-06 -- Na/K (mol/s/m^2)
kLeak_ax:set_perm(leakKConst_ax, k_in, k_out, v_eq, 1)
naLeak_so = IonLeakage("na", "soma")
leakNaConst_so = 5.51393481e-07 + -- HH (mol/s/m^2)
-2.54076e-06 -- Na/K (mol/s/m^2)
naLeak_so:set_perm(leakNaConst_so, na_in, na_out, v_eq, 1)
naLeak_de = IonLeakage("na", dendSubsets)
leakNaConst_de = 1.470384e-08 + -- HH (mol/s/m^2)
-3.81114e-07 -- Na/K (mol/s/m^2)
naLeak_de:set_perm(leakNaConst_de, na_in, na_out, v_eq, 1)
CE:add(kLeak_ax)
CE:add(naLeak_so)
CE:add(naLeak_de)
-- synapses
syn_handler = SynapseHandler()
syn_handler:set_ce_object(CE)
syn_handler:set_activation_timing_alpha(
avg_start, -- average onset of synaptical activity in [s]
avg_dur/6.0, -- average tau of activity function in [s]
dev_start, -- deviation of onset in [s]
dev_dur/6.0, -- deviation of tau in [s]
1.2e-9) -- peak conductivity in [S]
CE:set_synapse_handler(syn_handler)
--[[
-- electrode stimulation
-- 5nA seem to enervate the pyramidal cell with uniform diameters of 1um
-- (coords for 13-L3pyr-77.CNG.ugx, current given in C/ms)
CE:set_influx(5e-9, 6.54e-05, 2.665e-05, 3.985e-05, 0.0, 0.04) -- near soma
CE:set_influx(5e-9, 3.955e-06, 1.095e-06, -3.365e-06, 0.001, 0.0025) -- 1st edge soma to dend
CE:set_influx(0.3e-9, 3.955e-06, 1.095e-06, -3.365e-06, 0.0, 0.03) -- 1st 1st edge soma to dend
CE:set_influx(0.095e-9, 0.0, 0.0, 0.0, 0.1, 0.1) -- soma center vertex
CE:set_influx(0.2e-9, 0.0, 0.0, 0.0, 0.005, 0.0005) -- soma center vertex
CE:set_influx(10.0e-9, 0.000139, 0.00020809, -2.037e-05, 0.005, 0.005) -- distal apical dendrite vertex v1
CE:set_influx(10.0e-9, -3.96e-06, 0.0002173, -5.431e-05, 0.005, 0.005) -- distal apical dendrite vertex v2
--]]
-- create domain discretization
domainDisc = DomainDiscretization(approxSpace)
domainDisc:add(CE)
assTuner = domainDisc:ass_tuner()
-- create time discretization
timeDisc = ThetaTimeStep(domainDisc)
timeDisc:set_theta(1.0)
-- create operator from discretization
linOp = AssembledLinearOperator(timeDisc)
------------------
-- solver setup --
------------------
-- debug writer
dbgWriter = GridFunctionDebugWriter(approxSpace)
dbgWriter:set_vtk_output(true)
-- linear solver --
linConvCheck = CompositeConvCheck(approxSpace, 20, 2e-26, 1e-08)
linConvCheck:set_component_check("v", 1e-21, 1e-12)
linConvCheck:set_verbose(verbose)
ilu = ILU()
cgSolver = CG()
cgSolver:set_preconditioner(ilu)
cgSolver:set_convergence_check(linConvCheck)
--cgSolver:set_debug(dbgWriter)
----------------------
-- time stepping --
----------------------
time = 0.0
-- init solution
u = GridFunction(approxSpace)
b = GridFunction(approxSpace)
u:set(0.0)
Interpolate(v_eq, u, "v")
Interpolate(k_in, u, "k")
Interpolate(na_in, u, "na")
Interpolate(ca_in, u, "ca")
-- file i/o setup for sample calcium concentration measurement
measFileVm = outPath.."measVm.txt"
measFileCa = outPath.."measCa.txt"
if ProcRank() == 0 then
measOutVm = assert(io.open(measFileVm, "a"))
measOutCa = assert(io.open(measFileCa, "a"))
end
-- write start solution
if generateVTKoutput then
out = VTKOutput()
out:print(outPath.."vtk/solution", u, 0, time)
end
-- store grid function in vector of old solutions
uOld = u:clone()
solTimeSeries = SolutionTimeSeries()
solTimeSeries:push(uOld, time)
curr_dt = dt
dtred = 2
lv = 0
maxLv = 10
cb_counter = {}
cb_counter[lv] = 0
while endTime-time > 0.001*curr_dt do
-- setup time Disc for old solutions and timestep
timeDisc:prepare_step(solTimeSeries, curr_dt)
-- reduce time step if cfl < curr_dt
-- (this needs to be done AFTER prepare_step as channels are updated there)
dtChanged = false
cfl = CE:estimate_cfl_cond(solTimeSeries:latest())
print("estimated CFL condition: dt < " .. cfl)
while (curr_dt > cfl) do
curr_dt = curr_dt/dtred
if lv+1 > maxLv then
print("Time step too small.")
exit()
end
lv = lv + 1
cb_counter[lv] = 0
print("estimated CFL condition: dt < " .. cfl .. " - reducing time step to " .. curr_dt)
dtChanged = true
end
-- increase time step if cfl > curr_dt / dtred (and if time is aligned with new bigger step size)
while curr_dt*dtred < cfl and lv > 0 and cb_counter[lv] % (dtred) == 0 do
curr_dt = curr_dt*dtred
lv = lv - 1
cb_counter[lv] = cb_counter[lv] + cb_counter[lv+1]/dtred
cb_counter[lv+1] = 0
print ("estimated CFL condition: dt < " .. cfl .. " - increasing time step to " .. curr_dt)
dtChanged = true
end
print("++++++ POINT IN TIME " .. math.floor((time+curr_dt)/curr_dt+0.5)*curr_dt .. " BEGIN ++++++")
-- prepare again with new time step size
if dtChanged == true then
timeDisc:prepare_step(solTimeSeries, curr_dt)
end
-- assemble linear problem
matrixIsConst = time ~= 0.0 and dtChanged == false
assTuner:set_matrix_is_const(matrixIsConst)
AssembleLinearOperatorRhsAndSolution(linOp, u, b)
-- synchronize (for profiling)
PclDebugBarrierAll()
-- apply linear solver
ilu:set_disable_preprocessing(matrixIsConst)
if ApplyLinearSolver(linOp, u, b, cgSolver) == false then
print("Could not apply linear solver.")
exit()
end
-- log time and vm in Soma
if ProcRank() == 0 then
if cell == "12-L3pyr" then
vm_soma = EvaluateAtClosestVertex(MakeVec(0.0, 0.0, 0.0), u, "v", "soma", dom:subset_handler())
vm_axon = EvaluateAtClosestVertex(MakeVec(-3.828e-05, -0.00013166, -2.34e-05), u, "v", "axon", dom:subset_handler())
vm_dend = EvaluateAtClosestVertex(MakeVec(8.304e-05, -1.982e-05, -8.4e-06), u, "v", "dendrite", dom:subset_handler())
vm_aDend = EvaluateAtClosestVertex(MakeVec(-3.84e-06, 0.00018561, -3.947e-05), u, "v", "apical_dendrite", dom:subset_handler())
measOutVm:write(time, "\t", vm_soma, "\t", vm_axon, "\t", vm_dend, "\t", vm_aDend, "\n")
ca_soma = EvaluateAtClosestVertex(MakeVec(0.0, 0.0, 0.0), u, "ca", "soma", dom:subset_handler())
ca_axon = EvaluateAtClosestVertex(MakeVec(-3.828e-05, -0.00013166, -2.34e-05), u, "ca", "axon", dom:subset_handler())
ca_dend = EvaluateAtClosestVertex(MakeVec(8.304e-05, -1.982e-05, -8.4e-06), u, "ca", "dendrite", dom:subset_handler())
ca_aDend = EvaluateAtClosestVertex(MakeVec(-3.84e-06, 0.00018561, -3.947e-05), u, "ca", "apical_dendrite", dom:subset_handler())
measOutCa:write(time, "\t", ca_soma, "\t", ca_axon, "\t", ca_dend, "\t", ca_aDend, "\n")
else
vm_soma = EvaluateAtClosestVertex(MakeVec(6.9e-07, 3.74e-06, -2.86e-06), u, "v", "soma", dom:subset_handler())
vm_axon = EvaluateAtClosestVertex(MakeVec(-4.05e-06, 6.736e-05, -1.341e-05), u, "v", "axon", dom:subset_handler())
vm_dend = EvaluateAtClosestVertex(MakeVec(-4.631e-05, -0.0001252, 4.62e-06), u, "v", "dendrite", dom:subset_handler())
measOutVm:write(time, "\t", vm_soma, "\t", vm_axon, "\t", vm_dend, "\t", -65, "\n")
ca_soma = EvaluateAtClosestVertex(MakeVec(6.9e-07, 3.74e-06, -2.86e-06), u, "ca", "soma", dom:subset_handler())
ca_axon = EvaluateAtClosestVertex(MakeVec(-4.05e-06, 6.736e-05, -1.341e-05), u, "ca", "axon", dom:subset_handler())
ca_dend = EvaluateAtClosestVertex(MakeVec(-4.631e-05, -0.0001252, 4.62e-06), u, "ca", "dendrite", dom:subset_handler())
measOutCa:write(time, "\t", ca_soma, "\t", ca_axon, "\t", ca_dend, "\t", -65, "\n")
end
end
-- update to new time
time = solTimeSeries:time(0) + curr_dt
-- vtk output
if generateVTKoutput then
if math.abs(time/pstep - math.floor(time/pstep+0.5)) < 1e-5 then
out:print(outPath.."vtk/solution", u, math.floor(time/pstep+0.5), time)
end
end
-- updte time series (reuse memory)
oldestSol = solTimeSeries:oldest()
VecScaleAssign(oldestSol, 1.0, u)
solTimeSeries:push_discard_oldest(oldestSol, time)
-- increment check-back counter
cb_counter[lv] = cb_counter[lv] + 1
print("++++++ POINT IN TIME " .. math.floor((time)/curr_dt+0.5)*curr_dt .. " END ++++++")
end
-- end timeseries, produce gathering file
if generateVTKoutput then
out:write_time_pvd(outPath.."vtk/solution", u)
end
-- close measure file
if ProcRank() == 0 then
measOutVm:close()
measOutCa:close()
end