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