Debian Codegraph @

brian2/codegen/runtime/cython_rt/templates/spatialstateupdate.pyx

raw
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,
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
13 {% extends 'common_group.pyx' %}
14
15 {% block before_code %}
16 cdef int _i
17 cdef int _counter
18 cdef int _first
19 cdef int _last
20 cdef double __invr0
21 cdef double __invrn
22
23 cdef double _Ri = {{Ri}} # Ri is a shared variable
24
25 # Inverse axial resistance
26 for _i in range(1, N):
27 {{_invr}}[_i] =1.0/(_Ri*(1/{{r_length_2}}[_i-1] + 1/{{r_length_1}}[_i]))
28 # Cut sections
29 for _i in range(_num{{_starts}}):
30 {{_invr}}[{{_starts}}[_i]] = 0
31
32 # Linear systems
33 # The particular solution
34 # a[i,j]=ab[u+i-j,j] -- u is the number of upper diagonals = 1
35 for _i in range(N):
36 {{_ab_star1}}[_i] = (-({{Cm}}[_i] / {{dt}}) - {{_invr}}[_i] / {{area}}[_i])
37 for _i in range(1, N):
38 {{_ab_star0}}[_i] = {{_invr}}[_i] / {{area}}[_i-1]
39 {{_ab_star2}}[_i-1] = {{_invr}}[_i] / {{area}}[_i]
40 {{_ab_star1}}[_i-1] -= {{_invr}}[_i] / {{area}}[_i-1]
41
42 # Set the boundary conditions
43 for _counter in range(_num{{_starts}}):
44 _first = {{_starts}}[_counter]
45 _last = {{_ends}}[_counter] -1 # the compartment indices are in the interval [starts, ends[
46 # Inverse axial resistances at the ends: r0 and rn
47 __invr0 = {{r_length_1}}[_first]/_Ri
48 __invrn = {{r_length_2}}[_last]/_Ri
49 {{_invr0}}[_counter] = __invr0
50 {{_invrn}}[_counter] = __invrn
51 # Correction for boundary conditions
52 {{_ab_star1}}[_first] -= (__invr0 / {{area}}[_first])
53 {{_ab_star1}}[_last] -= (__invrn / {{area}}[_last])
54 # RHS for homogeneous solutions
55 {{_b_plus}}[_last] = -(__invrn / {{area}}[_last])
56 {{_b_minus}}[_first] = -(__invr0 / {{area}}[_first])
57 {% endblock %}
58
59 {% block maincode %}
60 cdef int _i
61 cdef int _j
62 cdef int _k
63 cdef int _j_start
64 cdef int _j_end
65 cdef double _ai
66 cdef double _bi
67 cdef double _m
68 cdef int _i_parent
69 cdef int _i_childind
70 cdef int _first
71 cdef int _last
72 cdef int _num_children
73 cdef double _subfac
74 cdef int _children_rowlength
75
76 # MAIN CODE
77 _vectorisation_idx = 1
78 {{scalar_code|autoindent}}
79
80
81 # STEP 1: compute g_total and I_0
82 for _i in range(0, N):
83 _idx = _i
84 _vectorisation_idx = _idx
85
86 {{vector_code|autoindent}}
87 {{_gtot_all}}[_idx] = _gtot
88 {{_I0_all}}[_idx] = _I0
89 {{_v_previous}}[_idx] = {{v}}[_idx]
90
91 # STEP 2: for each section: solve three tridiagonal systems
92
93 # system 2a: solve for _v_star
94 for _i in range(0, _num{{_B}} - 1):
95 # first and last index of the i-th section
96 _j_start = {{_starts}}[_i]
97 _j_end = {{_ends}}[_i]
98
99 # upper triangularization of tridiagonal system for _v_star, _u_plus, and _u_minus
100 for _j in range(_j_start, _j_end):
101 {{_v_star}}[_j]=-({{Cm}}[_j]/{{dt}}*{{v}}[_j])-{{_I0_all}}[_j] # RHS -> _v_star (solution)
102 {{_u_plus}}[_j]={{_b_plus}}[_j] # RHS -> _u_plus (solution)
103 {{_u_minus}}[_j]={{_b_minus}}[_j] # RHS -> _u_minus (solution)
104 _bi={{_ab_star1}}[_j]-{{_gtot_all}}[_j] # main diagonal
105 if _j < N-1:
106 {{_c}}[_j]={{_ab_star0}}[_j+1] # superdiagonal
107 if _j > 0:
108 _ai={{_ab_star2}}[_j-1] # subdiagonal
109 _m=1.0/(_bi-_ai*{{_c}}[_j-1])
110 {{_c}}[_j]={{_c}}[_j]*_m
111 {{_v_star}}[_j]=({{_v_star}}[_j] - _ai*{{_v_star}}[_j-1])*_m
112 {{_u_plus}}[_j]=({{_u_plus}}[_j] - _ai*{{_u_plus}}[_j-1])*_m
113 {{_u_minus}}[_j]=({{_u_minus}}[_j] - _ai*{{_u_minus}}[_j-1])*_m
114 else:
115 {{_c}}[0]={{_c}}[0]/_bi
116 {{_v_star}}[0]={{_v_star}}[0]/_bi
117 {{_u_plus}}[0]={{_u_plus}}[0]/_bi
118 {{_u_minus}}[0]={{_u_minus}}[0]/_bi
119 # backwards substitution of the upper triangularized system for _v_star
120 for _j in range(_j_end-2, _j_start-1, -1):
121 {{_v_star}}[_j]={{_v_star}}[_j] - {{_c}}[_j]*{{_v_star}}[_j+1]
122 {{_u_plus}}[_j]={{_u_plus}}[_j] - {{_c}}[_j]*{{_u_plus}}[_j+1]
123 {{_u_minus}}[_j]={{_u_minus}}[_j] - {{_c}}[_j]*{{_u_minus}}[_j+1]
124
125 # STEP 3: solve the coupling system
126
127 # indexing for _P_children which contains the elements above the diagonal of the coupling matrix _P
128 _children_rowlength = _num{{_morph_children}}//_num{{_morph_children_num}}
129
130 # STEP 3a: construct the coupling system with matrix _P in sparse form. s.t.
131 # _P_diag contains the diagonal elements
132 # _P_children contains the super diagonal entries
133 # _P_parent contains the single sub diagonal entry for each row
134 # _B contains the right hand side
135
136 for _i in range(0, _num{{_B}} - 1):
137 _i_parent = {{_morph_parent_i}}[_i]
138 _i_childind = {{_morph_idxchild}}[_i]
139 _first = {{_starts}}[_i]
140 _last = {{_ends}}[_i] - 1 # the compartment indices are in the interval [starts, ends[
141 _this_invr0 = {{_invr0}}[_i]
142 _this_invrn = {{_invrn}}[_i]
143
144 # Towards parent
145 if _i == 0: # first section, sealed end
146 {{_P_diag}}[0] = {{_u_minus}}[_first] - 1
147 {{_P_children}}[0 + 0] = {{_u_plus}}[_first]
148
149 # RHS
150 {{_B}}[0] = -{{_v_star}}[_first]
151 else:
152 {{_P_diag}}[_i_parent] += (1 - {{_u_minus}}[_first]) * _this_invr0
153 {{_P_children}}[_i_parent * _children_rowlength + _i_childind] = -{{_u_plus}}[_first] * _this_invr0
154
155 # RHS
156 {{_B}}[_i_parent] += {{_v_star}}[_first] * _this_invr0
157
158 # Towards children
159 {{_P_diag}}[_i+1] = (1 - {{_u_plus}}[_last]) * _this_invrn
160 {{_P_parent}}[_i] = -{{_u_minus}}[_last] * _this_invrn
161
162 # RHS
163 {{_B}}[_i+1] = {{_v_star}}[_last] * _this_invrn
164
165 # STEP 3b: solve the linear system (the result will be stored in the former rhs _B in the end)
166 # use efficient O(n) solution of the sparse linear system (structure-specific Gaussian elemination)
167
168 # part 1: lower triangularization
169 for _i in range(_num{{_B}}-1, -1, -1):
170 _num_children = {{_morph_children_num}}[_i]
171
172 # for every child eliminate the corresponding matrix element of row i
173 for _k in range(0, _num_children):
174 _j = {{_morph_children}}[_i * _children_rowlength + _k] # child index
175
176 # subtracting _subfac times the j-th from the i-th row
177 _subfac = {{_P_children}}[_i * _children_rowlength + _k] / {{_P_diag}}[_j] # element i,j appears only here
178
179 # the following commented (superdiagonal) element is not used in the following anymore since
180 # it is 0 by definition of (lower) triangularization we keep it here for algorithmic clarity
181 #{{_P_children}}[_i * _children_rowlength +_k] = {{_P_children}}[_i * _children_rowlength + _k] - _subfac * {{_P_diag}}[_j] # = 0
182
183 {{_P_diag}}[_i] = {{_P_diag}}[_i] - _subfac * {{_P_parent}}[_j-1] # note: element j,i is only used here
184 {{_B}}[_i] = {{_B}}[_i] - _subfac * {{_B}}[_j]
185
186 # part 2: forwards substitution
187 {{_B}}[0] = {{_B}}[0] / {{_P_diag}}[0] # the first section does not have a parent
188 for _i in range(1, _num{{_B}}):
189 _j = {{_morph_parent_i}}[_i-1] # parent index
190 {{_B}}[_i] = {{_B}}[_i] - {{_P_parent}}[_i-1] * {{_B}}[_j]
191 {{_B}}[_i] = {{_B}}[_i] / {{_P_diag}}[_i]
192
193 # STEP 4: for each section compute the final solution by linear
194 # combination of the general solution (independent: sections & compartments)
195 for _i in range(0, _num{{_B}} - 1):
196 _i_parent = {{_morph_parent_i}}[_i]
197 _j_start = {{_starts}}[_i]
198 _j_end = {{_ends}}[_i]
199 for _j in range(_j_start, _j_end):
200 if _j < _num{{v}}: # don't go beyond the last element
201 {{v}}[_j] = ({{_v_star}}[_j] + {{_B}}[_i_parent] * {{_u_minus}}[_j]
202 + {{_B}}[_i+1] * {{_u_plus}}[_j])
203
204 for _i in range(0, N):
205 {{Ic}}[_i] = {{Cm}}[_i]*({{v}}[_i] - {{_v_previous}}[_i])/{{dt}}
206
207 {% endblock %}