| 1 |
{# USES_VARIABLES { Cm, dt, v, N, Ic, Ri, |
| 2 |
_ab_star0, _ab_star1, _ab_star2, _b_plus, _b_minus, |
| 3 |
_v_star, _u_plus, _u_minus, |
| 4 |
_v_previous, |
| 5 |
_gtot_all, _I0_all, v |
| 6 |
_c, |
| 7 |
_P_diag, _P_parent, _P_children, |
| 8 |
_B, _morph_parent_i, _starts, _ends, |
| 9 |
_morph_children, _morph_children_num, _morph_idxchild, |
| 10 |
_invr0, _invrn, _invr, |
| 11 |
r_length_1, r_length_2, area} #} |
| 12 |
{# ITERATE_ALL { _idx } #} |
| 13 |
""" |
| 14 |
Solves the cable equation (spatial diffusion of currents). |
| 15 |
This is where most time-consuming time computations are done. |
| 16 |
""" |
| 17 |
{% extends 'common_group.py_' %} |
| 18 |
|
| 19 |
{# Preparation of the data structures #} |
| 20 |
{% block before_code %} |
| 21 |
# Inverse axial resistance |
| 22 |
{{_invr}}[1:] = 1.0/({{Ri}}*(1/{{r_length_2}}[:-1] + 1/{{r_length_1}}[1:])); |
| 23 |
# Cut sections |
| 24 |
for _first in {{_starts}}: |
| 25 |
{{_invr}}[_first] = 0 |
| 26 |
|
| 27 |
# Linear systems |
| 28 |
# The particular solution |
| 29 |
"""a[i,j]=ab[u+i-j,j]""" # u is the number of upper diagonals = 1 |
| 30 |
|
| 31 |
{{_ab_star0}}[1:] = {{_invr}}[1:] / {{area}}[:-1] |
| 32 |
{{_ab_star2}}[:-1] = {{_invr}}[1:] / {{area}}[1:] |
| 33 |
{{_ab_star1}}[:] = (-({{Cm}} / {{dt}}) - {{_invr}} / {{area}}) |
| 34 |
{{_ab_star1}}[:-1] -= {{_invr}}[1:] / {{area}}[:-1] |
| 35 |
|
| 36 |
# Set the boundary conditions |
| 37 |
for _counter, (_first, _last) in enumerate(zip({{_starts}}, |
| 38 |
{{_ends}})): |
| 39 |
_last = _last -1 # the compartment indices are in the interval [starts, ends[ |
| 40 |
# Inverse axial resistances at the ends: r0 and rn |
| 41 |
{{_invr0}}[_counter] = _invr0 = {{r_length_1}}[_first]/{{Ri}} |
| 42 |
{{_invrn}}[_counter] = _invrn = {{r_length_2}}[_last]/{{Ri}} |
| 43 |
# Correction for boundary conditions |
| 44 |
{{_ab_star1}}[_first] -= (_invr0 / {{area}}[_first]) |
| 45 |
{{_ab_star1}}[_last] -= (_invrn / {{area}}[_last]) |
| 46 |
# RHS for homogeneous solutions |
| 47 |
{{_b_plus}}[_last] = -(_invrn / {{area}}[_last]) |
| 48 |
{{_b_minus}}[_first] = -(_invr0 / {{area}}[_first]) |
| 49 |
{% endblock %} |
| 50 |
|
| 51 |
{% block maincode %} |
| 52 |
from numpy import pi |
| 53 |
from numpy import zeros |
| 54 |
try: |
| 55 |
from scipy.linalg import solve_banded |
| 56 |
except ImportError: |
| 57 |
raise ImportError("Install 'scipy' to run multi-compartmental neurons with numpy") |
| 58 |
|
| 59 |
# scalar code |
| 60 |
_vectorisation_idx = 1 |
| 61 |
{{scalar_code|autoindent}} |
| 62 |
|
| 63 |
# vector code |
| 64 |
_vectorisation_idx = LazyArange(N) |
| 65 |
{{vector_code|autoindent}} |
| 66 |
|
| 67 |
{{_v_previous}}[:] = {{v}} |
| 68 |
|
| 69 |
# Particular solution |
| 70 |
_b=-({{Cm}}/{{dt}}*{{v}})-_I0 |
| 71 |
_ab = zeros((3,N)) |
| 72 |
_ab[0,:] = {{_ab_star0}} |
| 73 |
_ab[1,:] = {{_ab_star1}} - _gtot |
| 74 |
_ab[2,:] = {{_ab_star2}} |
| 75 |
{{_v_star}}[:] = solve_banded((1,1),_ab,_b,overwrite_ab=True,overwrite_b=True) |
| 76 |
# Homogeneous solutions |
| 77 |
_b[:] = {{_b_plus}} |
| 78 |
_ab[0,:] = {{_ab_star0}} |
| 79 |
_ab[1,:] = {{_ab_star1}} - _gtot |
| 80 |
_ab[2,:] = {{_ab_star2}} |
| 81 |
{{_u_plus}}[:] = solve_banded((1,1),_ab,_b,overwrite_ab=True,overwrite_b=True) |
| 82 |
_b[:] = {{_b_minus}} |
| 83 |
_ab[0,:] = {{_ab_star0}} |
| 84 |
_ab[1,:] = {{_ab_star1}} - _gtot |
| 85 |
_ab[2,:] = {{_ab_star2}} |
| 86 |
{{_u_minus}}[:] = solve_banded((1,1),_ab,_b,overwrite_ab=True,overwrite_b=True) |
| 87 |
|
| 88 |
# indexing for _P_children which contains the elements above the diagonal of the coupling matrix _P |
| 89 |
children_rowlength = len({{_morph_children}})//len({{_morph_children_num}}) |
| 90 |
|
| 91 |
# Construct the coupling system with matrix _P in sparse form. s.t. |
| 92 |
# _P_diag contains the diagonal elements |
| 93 |
# _P_children contains the super diagonal entries |
| 94 |
# _P_parent contains the single sub diagonal entry for each row |
| 95 |
# _B contains the right hand side |
| 96 |
|
| 97 |
_P_children_2d = {{_P_children}}.reshape(-1, children_rowlength) |
| 98 |
|
| 99 |
for _i, (_i_parent, _i_childind, _first, _last, _invr0, _invrn) in enumerate(zip({{_morph_parent_i}}, |
| 100 |
{{_morph_idxchild}}, |
| 101 |
{{_starts}}, |
| 102 |
{{_ends}}, |
| 103 |
{{_invr0}}, |
| 104 |
{{_invrn}})): |
| 105 |
_last = _last - 1 # the compartment indices are in the interval [starts, ends[ |
| 106 |
# Towards parent |
| 107 |
if _i == 0: # first section, sealed end |
| 108 |
{{_P_diag}}[0] = {{_u_minus}}[_first] - 1 |
| 109 |
_P_children_2d[0, 0] = {{_u_plus}}[_first] |
| 110 |
|
| 111 |
# RHS |
| 112 |
{{_B}}[0] = -{{_v_star}}[_first] |
| 113 |
else: |
| 114 |
{{_P_diag}}[_i_parent] += (1 - {{_u_minus}}[_first]) * _invr0 |
| 115 |
_P_children_2d[_i_parent, _i_childind] = -{{_u_plus}}[_first] * _invr0 |
| 116 |
|
| 117 |
# RHS |
| 118 |
{{_B}}[_i_parent] += {{_v_star}}[_first] * _invr0 |
| 119 |
|
| 120 |
# Towards children |
| 121 |
{{_P_diag}}[_i+1] = (1 - {{_u_plus}}[_last]) * _invrn |
| 122 |
{{_P_parent}}[_i] = -{{_u_minus}}[_last] * _invrn |
| 123 |
|
| 124 |
# RHS |
| 125 |
{{_B}}[_i+1] = {{_v_star}}[_last] * _invrn |
| 126 |
|
| 127 |
# Solve the linear system (the result will be stored in the former rhs _B in the end) |
| 128 |
# use efficient O(n) solution of the sparse linear system (structure-specific Gaussian elemination) |
| 129 |
_morph_children_2d = {{_morph_children}}.reshape(-1, children_rowlength) |
| 130 |
|
| 131 |
# part 1: lower triangularization |
| 132 |
|
| 133 |
for _i in range(len({{_B}})-1, -1, -1): |
| 134 |
_num_children = {{_morph_children_num}}[_i]; |
| 135 |
|
| 136 |
for _k in range(_num_children): |
| 137 |
_j = _morph_children_2d[_i, _k] # child index |
| 138 |
|
| 139 |
# subtracting _subfac times the j-th from the _i-th row |
| 140 |
_subfac = _P_children_2d[_i, _k] / {{_P_diag}}[_j] |
| 141 |
|
| 142 |
{{_P_diag}}[_i] = {{_P_diag}}[_i] - _subfac * {{_P_parent}}[_j-1] |
| 143 |
{{_B}}[_i] = {{_B}}[_i] - _subfac * {{_B}}[_j] |
| 144 |
|
| 145 |
# part 2: forwards substitution |
| 146 |
{{_B}}[0] = {{_B}}[0] / {{_P_diag}}[0] # the first section does not have a parent |
| 147 |
for _i, j in enumerate({{_morph_parent_i}}): |
| 148 |
{{_B}}[_i+1] -= {{_P_parent}}[_i] * {{_B}}[j] |
| 149 |
{{_B}}[_i+1] /= {{_P_diag}}[_i+1] |
| 150 |
|
| 151 |
# For each section compute the final solution by linear combination of the general solution |
| 152 |
for _i, (_B_parent, _j_start, _j_end) in enumerate(zip({{_B}}[{{_morph_parent_i}}], |
| 153 |
{{_starts}}, |
| 154 |
{{_ends}})): |
| 155 |
_B_current = {{_B}}[_i+1] |
| 156 |
if _j_start == _j_end: |
| 157 |
{{v}}[_j_start] = ({{_v_star}}[_j_start] + _B_parent * {{_u_minus}}[_j_start] |
| 158 |
+ _B_current * {{_u_plus}}[_j_start]) |
| 159 |
else: |
| 160 |
{{v}}[_j_start:_j_end] = ({{_v_star}}[_j_start:_j_end] + _B_parent * {{_u_minus}}[_j_start:_j_end] |
| 161 |
+ _B_current * {{_u_plus}}[_j_start:_j_end]) |
| 162 |
|
| 163 |
{{Ic}}[:] = {{Cm}}*({{v}} - {{_v_previous}})/{{dt}} |
| 164 |
{% endblock %} |