|
| 1 | +/********************************************************* |
| 2 | +Model Name : leonhard |
| 3 | +Filename : leonhard.mod |
| 4 | +NMODL Version : 7.7.0 |
| 5 | +Vectorized : true |
| 6 | +Threadsafe : true |
| 7 | +Created : DATE |
| 8 | +Simulator : CoreNEURON |
| 9 | +Backend : C++ (api-compatibility) |
| 10 | +NMODL Compiler : VERSION |
| 11 | +*********************************************************/ |
| 12 | + |
| 13 | +#include <math.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include <coreneuron/gpu/nrn_acc_manager.hpp> |
| 19 | +#include <coreneuron/mechanism/mech/mod2c_core_thread.hpp> |
| 20 | +#include <coreneuron/mechanism/register_mech.hpp> |
| 21 | +#include <coreneuron/nrnconf.h> |
| 22 | +#include <coreneuron/nrniv/nrniv_decl.h> |
| 23 | +#include <coreneuron/sim/multicore.hpp> |
| 24 | +#include <coreneuron/sim/scopmath/newton_thread.hpp> |
| 25 | +#include <coreneuron/utils/ivocvect.hpp> |
| 26 | +#include <coreneuron/utils/nrnoc_aux.hpp> |
| 27 | +#include <coreneuron/utils/randoms/nrnran123.h> |
| 28 | + |
| 29 | + |
| 30 | +namespace coreneuron { |
| 31 | + #ifndef NRN_PRCELLSTATE |
| 32 | + #define NRN_PRCELLSTATE 0 |
| 33 | + #endif |
| 34 | + |
| 35 | + |
| 36 | + /** channel information */ |
| 37 | + static const char *mechanism_info[] = { |
| 38 | + "7.7.0", |
| 39 | + "leonhard", |
| 40 | + 0, |
| 41 | + "z_leonhard[3]", |
| 42 | + 0, |
| 43 | + "x_leonhard", |
| 44 | + "s_leonhard[2]", |
| 45 | + 0, |
| 46 | + 0 |
| 47 | + }; |
| 48 | + |
| 49 | + |
| 50 | + /** all global variables */ |
| 51 | + struct leonhard_Store { |
| 52 | + double x0{}; |
| 53 | + double s0{}; |
| 54 | + int reset{}; |
| 55 | + int mech_type{}; |
| 56 | + int slist1[1]{3}; |
| 57 | + int dlist1[1]{6}; |
| 58 | + }; |
| 59 | + static_assert(std::is_trivially_copy_constructible_v<leonhard_Store>); |
| 60 | + static_assert(std::is_trivially_move_constructible_v<leonhard_Store>); |
| 61 | + static_assert(std::is_trivially_copy_assignable_v<leonhard_Store>); |
| 62 | + static_assert(std::is_trivially_move_assignable_v<leonhard_Store>); |
| 63 | + static_assert(std::is_trivially_destructible_v<leonhard_Store>); |
| 64 | + leonhard_Store leonhard_global; |
| 65 | + |
| 66 | + |
| 67 | + /** all mechanism instance variables and global variables */ |
| 68 | + struct leonhard_Instance { |
| 69 | + double* z{}; |
| 70 | + double* x{}; |
| 71 | + double* s{}; |
| 72 | + double* Dx{}; |
| 73 | + double* Ds{}; |
| 74 | + double* v_unused{}; |
| 75 | + double* g_unused{}; |
| 76 | + leonhard_Store* global{&leonhard_global}; |
| 77 | + }; |
| 78 | + |
| 79 | + |
| 80 | + /** connect global (scalar) variables to hoc -- */ |
| 81 | + static DoubScal hoc_scalar_double[] = { |
| 82 | + {nullptr, nullptr} |
| 83 | + }; |
| 84 | + |
| 85 | + |
| 86 | + /** connect global (array) variables to hoc -- */ |
| 87 | + static DoubVec hoc_vector_double[] = { |
| 88 | + {nullptr, nullptr, 0} |
| 89 | + }; |
| 90 | + |
| 91 | + |
| 92 | + static inline int first_pointer_var_index() { |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + static inline int first_random_var_index() { |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + static inline int float_variables_size() { |
| 103 | + return 7; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + static inline int int_variables_size() { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + static inline int get_mech_type() { |
| 113 | + return leonhard_global.mech_type; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + static inline Memb_list* get_memb_list(NrnThread* nt) { |
| 118 | + if (!nt->_ml_list) { |
| 119 | + return nullptr; |
| 120 | + } |
| 121 | + return nt->_ml_list[get_mech_type()]; |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + static inline void* mem_alloc(size_t num, size_t size, size_t alignment = 16) { |
| 126 | + void* ptr; |
| 127 | + posix_memalign(&ptr, alignment, num*size); |
| 128 | + memset(ptr, 0, size); |
| 129 | + return ptr; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + static inline void mem_free(void* ptr) { |
| 134 | + free(ptr); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + static inline void coreneuron_abort() { |
| 139 | + abort(); |
| 140 | + } |
| 141 | + |
| 142 | + // Allocate instance structure |
| 143 | + static void nrn_private_constructor_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 144 | + assert(!ml->instance); |
| 145 | + assert(!ml->global_variables); |
| 146 | + assert(ml->global_variables_size == 0); |
| 147 | + auto* const inst = new leonhard_Instance{}; |
| 148 | + assert(inst->global == &leonhard_global); |
| 149 | + ml->instance = inst; |
| 150 | + ml->global_variables = inst->global; |
| 151 | + ml->global_variables_size = sizeof(leonhard_Store); |
| 152 | + } |
| 153 | + |
| 154 | + // Deallocate the instance structure |
| 155 | + static void nrn_private_destructor_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 156 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 157 | + assert(inst); |
| 158 | + assert(inst->global); |
| 159 | + assert(inst->global == &leonhard_global); |
| 160 | + assert(inst->global == ml->global_variables); |
| 161 | + assert(ml->global_variables_size == sizeof(leonhard_Store)); |
| 162 | + delete inst; |
| 163 | + ml->instance = nullptr; |
| 164 | + ml->global_variables = nullptr; |
| 165 | + ml->global_variables_size = 0; |
| 166 | + } |
| 167 | + |
| 168 | + /** initialize mechanism instance variables */ |
| 169 | + static inline void setup_instance(NrnThread* nt, Memb_list* ml) { |
| 170 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 171 | + assert(inst); |
| 172 | + assert(inst->global); |
| 173 | + assert(inst->global == &leonhard_global); |
| 174 | + assert(inst->global == ml->global_variables); |
| 175 | + assert(ml->global_variables_size == sizeof(leonhard_Store)); |
| 176 | + int pnodecount = ml->_nodecount_padded; |
| 177 | + Datum* indexes = ml->pdata; |
| 178 | + inst->z = ml->data+0*pnodecount; |
| 179 | + inst->x = ml->data+3*pnodecount; |
| 180 | + inst->s = ml->data+4*pnodecount; |
| 181 | + inst->Dx = ml->data+6*pnodecount; |
| 182 | + inst->Ds = ml->data+7*pnodecount; |
| 183 | + inst->v_unused = ml->data+9*pnodecount; |
| 184 | + inst->g_unused = ml->data+10*pnodecount; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + static void nrn_alloc_leonhard(double* data, Datum* indexes, int type) { |
| 190 | + // do nothing |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + void nrn_constructor_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 195 | + #ifndef CORENEURON_BUILD |
| 196 | + int nodecount = ml->nodecount; |
| 197 | + int pnodecount = ml->_nodecount_padded; |
| 198 | + const int* node_index = ml->nodeindices; |
| 199 | + double* data = ml->data; |
| 200 | + const double* voltage = nt->_actual_v; |
| 201 | + Datum* indexes = ml->pdata; |
| 202 | + ThreadDatum* thread = ml->_thread; |
| 203 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 204 | + |
| 205 | + #endif |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + void nrn_destructor_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 210 | + #ifndef CORENEURON_BUILD |
| 211 | + int nodecount = ml->nodecount; |
| 212 | + int pnodecount = ml->_nodecount_padded; |
| 213 | + const int* node_index = ml->nodeindices; |
| 214 | + double* data = ml->data; |
| 215 | + const double* voltage = nt->_actual_v; |
| 216 | + Datum* indexes = ml->pdata; |
| 217 | + ThreadDatum* thread = ml->_thread; |
| 218 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 219 | + |
| 220 | + #endif |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + /** initialize channel */ |
| 225 | + void nrn_init_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 226 | + int nodecount = ml->nodecount; |
| 227 | + int pnodecount = ml->_nodecount_padded; |
| 228 | + const int* node_index = ml->nodeindices; |
| 229 | + double* data = ml->data; |
| 230 | + const double* voltage = nt->_actual_v; |
| 231 | + Datum* indexes = ml->pdata; |
| 232 | + ThreadDatum* thread = ml->_thread; |
| 233 | + |
| 234 | + setup_instance(nt, ml); |
| 235 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 236 | + |
| 237 | + if (_nrn_skip_initmodel == 0) { |
| 238 | + #pragma omp simd |
| 239 | + #pragma ivdep |
| 240 | + for (int id = 0; id < nodecount; id++) { |
| 241 | + int node_id = node_index[id]; |
| 242 | + double v = voltage[node_id]; |
| 243 | + #if NRN_PRCELLSTATE |
| 244 | + inst->v_unused[id] = v; |
| 245 | + #endif |
| 246 | + inst->x[id] = inst->global->x0; |
| 247 | + (inst->s+id*2)[0] = inst->global->s0; |
| 248 | + (inst->s+id*2)[1] = inst->global->s0; |
| 249 | + inst->x[id] = 42.0; |
| 250 | + (inst->s+id*2)[static_cast<int>(0)] = 0.1; |
| 251 | + (inst->s+id*2)[static_cast<int>(1)] = -1.0; |
| 252 | + (inst->z+id*3)[static_cast<int>(0)] = 0.7; |
| 253 | + (inst->z+id*3)[static_cast<int>(1)] = 0.8; |
| 254 | + (inst->z+id*3)[static_cast<int>(2)] = 0.9; |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + |
| 260 | + /** update state */ |
| 261 | + void nrn_state_leonhard(NrnThread* nt, Memb_list* ml, int type) { |
| 262 | + int nodecount = ml->nodecount; |
| 263 | + int pnodecount = ml->_nodecount_padded; |
| 264 | + const int* node_index = ml->nodeindices; |
| 265 | + double* data = ml->data; |
| 266 | + const double* voltage = nt->_actual_v; |
| 267 | + Datum* indexes = ml->pdata; |
| 268 | + ThreadDatum* thread = ml->_thread; |
| 269 | + auto* const inst = static_cast<leonhard_Instance*>(ml->instance); |
| 270 | + |
| 271 | + #pragma omp simd |
| 272 | + #pragma ivdep |
| 273 | + for (int id = 0; id < nodecount; id++) { |
| 274 | + int node_id = node_index[id]; |
| 275 | + double v = voltage[node_id]; |
| 276 | + #if NRN_PRCELLSTATE |
| 277 | + inst->v_unused[id] = v; |
| 278 | + #endif |
| 279 | + inst->x[id] = inst->x[id] + (1.0 - exp(nt->_dt * ((((inst->s+id*2)[static_cast<int>(0)] + (inst->s+id*2)[static_cast<int>(1)]) * ((inst->z+id*3)[static_cast<int>(0)] * (inst->z+id*3)[static_cast<int>(1)] * (inst->z+id*3)[static_cast<int>(2)])) * (1.0)))) * ( -(0.0) / (((((inst->s+id*2)[static_cast<int>(0)] + (inst->s+id*2)[static_cast<int>(1)])) * (((((inst->z+id*3)[static_cast<int>(0)]) * ((inst->z+id*3)[static_cast<int>(1)])) * ((inst->z+id*3)[static_cast<int>(2)])))) * (1.0)) - inst->x[id]); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + |
| 284 | + /** register channel with the simulator */ |
| 285 | + void _leonhard_reg() { |
| 286 | + |
| 287 | + int mech_type = nrn_get_mechtype("leonhard"); |
| 288 | + leonhard_global.mech_type = mech_type; |
| 289 | + if (mech_type == -1) { |
| 290 | + return; |
| 291 | + } |
| 292 | + |
| 293 | + _nrn_layout_reg(mech_type, 0); |
| 294 | + register_mech(mechanism_info, nrn_alloc_leonhard, nullptr, nullptr, nrn_state_leonhard, nrn_init_leonhard, nrn_private_constructor_leonhard, nrn_private_destructor_leonhard, first_pointer_var_index(), 1); |
| 295 | + |
| 296 | + hoc_register_prop_size(mech_type, float_variables_size(), int_variables_size()); |
| 297 | + hoc_register_var(hoc_scalar_double, hoc_vector_double, NULL); |
| 298 | + } |
| 299 | +} |
0 commit comments