diff --git a/parameter/coreneuron/neuron_variables.cpp b/parameter/coreneuron/neuron_variables.cpp new file mode 100644 index 0000000..95eb173 --- /dev/null +++ b/parameter/coreneuron/neuron_variables.cpp @@ -0,0 +1,277 @@ +/********************************************************* +Model Name : NeuronVariables +Filename : neuron_variables.mod +NMODL Version : 7.7.0 +Vectorized : true +Threadsafe : true +Created : DATE +Simulator : CoreNEURON +Backend : C++ (api-compatibility) +NMODL Compiler : VERSION +*********************************************************/ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace coreneuron { + #ifndef NRN_PRCELLSTATE + #define NRN_PRCELLSTATE 0 + #endif + + + /** channel information */ + static const char *mechanism_info[] = { + "7.7.0", + "NeuronVariables", + 0, + "range_celsius_NeuronVariables", + 0, + 0, + 0 + }; + + + /** all global variables */ + struct NeuronVariables_Store { + int reset{}; + int mech_type{}; + }; + static_assert(std::is_trivially_copy_constructible_v); + static_assert(std::is_trivially_move_constructible_v); + static_assert(std::is_trivially_copy_assignable_v); + static_assert(std::is_trivially_move_assignable_v); + static_assert(std::is_trivially_destructible_v); + NeuronVariables_Store NeuronVariables_global; + + + /** all mechanism instance variables and global variables */ + struct NeuronVariables_Instance { + double* celsius{&coreneuron::celsius}; + double* range_celsius{}; + double* v_unused{}; + double* g_unused{}; + NeuronVariables_Store* global{&NeuronVariables_global}; + }; + + + /** connect global (scalar) variables to hoc -- */ + static DoubScal hoc_scalar_double[] = { + {nullptr, nullptr} + }; + + + /** connect global (array) variables to hoc -- */ + static DoubVec hoc_vector_double[] = { + {nullptr, nullptr, 0} + }; + + + static inline int first_pointer_var_index() { + return -1; + } + + + static inline int first_random_var_index() { + return -1; + } + + + static inline int float_variables_size() { + return 3; + } + + + static inline int int_variables_size() { + return 0; + } + + + static inline int get_mech_type() { + return NeuronVariables_global.mech_type; + } + + + static inline Memb_list* get_memb_list(NrnThread* nt) { + if (!nt->_ml_list) { + return nullptr; + } + return nt->_ml_list[get_mech_type()]; + } + + + static inline void* mem_alloc(size_t num, size_t size, size_t alignment = 16) { + void* ptr; + posix_memalign(&ptr, alignment, num*size); + memset(ptr, 0, size); + return ptr; + } + + + static inline void mem_free(void* ptr) { + free(ptr); + } + + + static inline void coreneuron_abort() { + abort(); + } + + // Allocate instance structure + static void nrn_private_constructor_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + assert(!ml->instance); + assert(!ml->global_variables); + assert(ml->global_variables_size == 0); + auto* const inst = new NeuronVariables_Instance{}; + assert(inst->global == &NeuronVariables_global); + ml->instance = inst; + ml->global_variables = inst->global; + ml->global_variables_size = sizeof(NeuronVariables_Store); + } + + // Deallocate the instance structure + static void nrn_private_destructor_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + auto* const inst = static_cast(ml->instance); + assert(inst); + assert(inst->global); + assert(inst->global == &NeuronVariables_global); + assert(inst->global == ml->global_variables); + assert(ml->global_variables_size == sizeof(NeuronVariables_Store)); + delete inst; + ml->instance = nullptr; + ml->global_variables = nullptr; + ml->global_variables_size = 0; + } + + /** initialize mechanism instance variables */ + static inline void setup_instance(NrnThread* nt, Memb_list* ml) { + auto* const inst = static_cast(ml->instance); + assert(inst); + assert(inst->global); + assert(inst->global == &NeuronVariables_global); + assert(inst->global == ml->global_variables); + assert(ml->global_variables_size == sizeof(NeuronVariables_Store)); + int pnodecount = ml->_nodecount_padded; + Datum* indexes = ml->pdata; + inst->range_celsius = ml->data+0*pnodecount; + inst->v_unused = ml->data+1*pnodecount; + inst->g_unused = ml->data+2*pnodecount; + } + + + + static void nrn_alloc_NeuronVariables(double* data, Datum* indexes, int type) { + // do nothing + } + + + void nrn_constructor_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + #ifndef CORENEURON_BUILD + int nodecount = ml->nodecount; + int pnodecount = ml->_nodecount_padded; + const int* node_index = ml->nodeindices; + double* data = ml->data; + const double* voltage = nt->_actual_v; + Datum* indexes = ml->pdata; + ThreadDatum* thread = ml->_thread; + auto* const inst = static_cast(ml->instance); + + #endif + } + + + void nrn_destructor_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + #ifndef CORENEURON_BUILD + int nodecount = ml->nodecount; + int pnodecount = ml->_nodecount_padded; + const int* node_index = ml->nodeindices; + double* data = ml->data; + const double* voltage = nt->_actual_v; + Datum* indexes = ml->pdata; + ThreadDatum* thread = ml->_thread; + auto* const inst = static_cast(ml->instance); + + #endif + } + + + /** initialize channel */ + void nrn_init_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + int nodecount = ml->nodecount; + int pnodecount = ml->_nodecount_padded; + const int* node_index = ml->nodeindices; + double* data = ml->data; + const double* voltage = nt->_actual_v; + Datum* indexes = ml->pdata; + ThreadDatum* thread = ml->_thread; + + setup_instance(nt, ml); + auto* const inst = static_cast(ml->instance); + + if (_nrn_skip_initmodel == 0) { + #pragma omp simd + #pragma ivdep + for (int id = 0; id < nodecount; id++) { + int node_id = node_index[id]; + double v = voltage[node_id]; + #if NRN_PRCELLSTATE + inst->v_unused[id] = v; + #endif + } + } + } + + + /** update state */ + void nrn_state_NeuronVariables(NrnThread* nt, Memb_list* ml, int type) { + int nodecount = ml->nodecount; + int pnodecount = ml->_nodecount_padded; + const int* node_index = ml->nodeindices; + double* data = ml->data; + const double* voltage = nt->_actual_v; + Datum* indexes = ml->pdata; + ThreadDatum* thread = ml->_thread; + auto* const inst = static_cast(ml->instance); + + #pragma omp simd + #pragma ivdep + for (int id = 0; id < nodecount; id++) { + int node_id = node_index[id]; + double v = voltage[node_id]; + #if NRN_PRCELLSTATE + inst->v_unused[id] = v; + #endif + inst->range_celsius[id] = *(inst->celsius); + } + } + + + /** register channel with the simulator */ + void _neuron_variables_reg() { + + int mech_type = nrn_get_mechtype("NeuronVariables"); + NeuronVariables_global.mech_type = mech_type; + if (mech_type == -1) { + return; + } + + _nrn_layout_reg(mech_type, 0); + register_mech(mechanism_info, nrn_alloc_NeuronVariables, nullptr, nullptr, nrn_state_NeuronVariables, nrn_init_NeuronVariables, nrn_private_constructor_NeuronVariables, nrn_private_destructor_NeuronVariables, first_pointer_var_index(), 1); + + hoc_register_prop_size(mech_type, float_variables_size(), int_variables_size()); + hoc_register_var(hoc_scalar_double, hoc_vector_double, NULL); + } +} diff --git a/parameter/neuron/neuron_variables.cpp b/parameter/neuron/neuron_variables.cpp new file mode 100644 index 0000000..de17ba9 --- /dev/null +++ b/parameter/neuron/neuron_variables.cpp @@ -0,0 +1,221 @@ +/********************************************************* +Model Name : NeuronVariables +Filename : neuron_variables.mod +NMODL Version : 7.7.0 +Vectorized : true +Threadsafe : true +Created : DATE +Simulator : NEURON +Backend : C++ (api-compatibility) +NMODL Compiler : VERSION +*********************************************************/ + +#include +#include +#include + +#include "mech_api.h" +#include "neuron/cache/mechanism_range.hpp" +#include "nrniv_mf.h" +#include "section_fwd.hpp" + +/* NEURON global macro definitions */ +/* VECTORIZED */ +#define NRN_VECTORIZED 1 + +static constexpr auto number_of_datum_variables = 0; +static constexpr auto number_of_floating_point_variables = 3; + +namespace { +template +using _nrn_mechanism_std_vector = std::vector; +using _nrn_model_sorted_token = neuron::model_sorted_token; +using _nrn_mechanism_cache_range = neuron::cache::MechanismRange; +using _nrn_mechanism_cache_instance = neuron::cache::MechanismInstance; +using _nrn_non_owning_id_without_container = neuron::container::non_owning_identifier_without_container; +template +using _nrn_mechanism_field = neuron::mechanism::field; +template +void _nrn_mechanism_register_data_fields(Args&&... args) { + neuron::mechanism::register_data_fields(std::forward(args)...); +} +} // namespace + +Prop* hoc_getdata_range(int type); +extern double celsius; + + +namespace neuron { + #ifndef NRN_PRCELLSTATE + #define NRN_PRCELLSTATE 0 + #endif + + + /** channel information */ + static const char *mechanism_info[] = { + "7.7.0", + "NeuronVariables", + 0, + "range_celsius_NeuronVariables", + 0, + 0, + 0 + }; + + + /* NEURON global variables */ + static int mech_type; + static Prop* _extcall_prop; + /* _prop_id kind of shadows _extcall_prop to allow validity checking. */ + static _nrn_non_owning_id_without_container _prop_id{}; + static int hoc_nrnpointerindex = -1; + static _nrn_mechanism_std_vector _extcall_thread; + + + /** all global variables */ + struct NeuronVariables_Store { + }; + static_assert(std::is_trivially_copy_constructible_v); + static_assert(std::is_trivially_move_constructible_v); + static_assert(std::is_trivially_copy_assignable_v); + static_assert(std::is_trivially_move_assignable_v); + static_assert(std::is_trivially_destructible_v); + NeuronVariables_Store NeuronVariables_global; + + + /** all mechanism instance variables and global variables */ + struct NeuronVariables_Instance { + double* celsius{&::celsius}; + double* range_celsius{}; + double* v_unused{}; + double* g_unused{}; + NeuronVariables_Store* global{&NeuronVariables_global}; + }; + + + struct NeuronVariables_NodeData { + int const * nodeindices; + double const * node_voltages; + double * node_rhs; + int nodecount; + }; + + + static NeuronVariables_Instance make_instance_NeuronVariables(_nrn_mechanism_cache_range& _ml) { + return NeuronVariables_Instance { + &::celsius, + _ml.template fpfield_ptr<0>(), + _ml.template fpfield_ptr<1>(), + _ml.template fpfield_ptr<2>() + }; + } + + + static NeuronVariables_NodeData make_node_data_NeuronVariables(NrnThread& _nt, Memb_list& _ml_arg) { + return NeuronVariables_NodeData { + _ml_arg.nodeindices, + _nt.node_voltage_storage(), + _nt.node_rhs_storage(), + _ml_arg.nodecount + }; + } + + + static void nrn_alloc_NeuronVariables(Prop* _prop) { + Prop *prop_ion{}; + Datum *_ppvar{}; + _nrn_mechanism_cache_instance _ml_real{_prop}; + auto* const _ml = &_ml_real; + size_t const _iml{}; + assert(_nrn_mechanism_get_num_vars(_prop) == 3); + /*initialize range parameters*/ + } + + + /* Neuron setdata functions */ + extern void _nrn_setdata_reg(int, void(*)(Prop*)); + static void _setdata(Prop* _prop) { + _extcall_prop = _prop; + _prop_id = _nrn_get_prop_id(_prop); + } + static void _hoc_setdata() { + Prop *_prop = hoc_getdata_range(mech_type); + _setdata(_prop); + hoc_retpushx(1.); + } + /* Mechanism procedures and functions */ + + + /** connect global (scalar) variables to hoc -- */ + static DoubScal hoc_scalar_double[] = { + {nullptr, nullptr} + }; + + + /** connect global (array) variables to hoc -- */ + static DoubVec hoc_vector_double[] = { + {nullptr, nullptr, 0} + }; + + + /* declaration of user functions */ + + + /* connect user functions to hoc names */ + static VoidFunc hoc_intfunc[] = { + {"setdata_NeuronVariables", _hoc_setdata}, + {0, 0} + }; + static NPyDirectMechFunc npy_direct_func_proc[] = { + }; + + + void nrn_init_NeuronVariables(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, Memb_list* _ml_arg, int _type) { + _nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type}; + auto inst = make_instance_NeuronVariables(_lmr); + auto node_data = make_node_data_NeuronVariables(*_nt, *_ml_arg); + auto nodecount = _ml_arg->nodecount; + for (int id = 0; id < nodecount; id++) { + } + } + + + void nrn_state_NeuronVariables(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, Memb_list* _ml_arg, int _type) { + _nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type}; + auto inst = make_instance_NeuronVariables(_lmr); + auto node_data = make_node_data_NeuronVariables(*_nt, *_ml_arg); + auto nodecount = _ml_arg->nodecount; + for (int id = 0; id < nodecount; id++) { + inst.range_celsius[id] = *(inst.celsius); + } + } + + + /** nrn_jacob function */ + static void nrn_jacob_NeuronVariables(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, Memb_list* _ml_arg, int _type) {} + + + static void _initlists() { + } + + + /** register channel with the simulator */ + extern "C" void _neuron_variables_reg() { + _initlists(); + + + + register_mech(mechanism_info, nrn_alloc_NeuronVariables, nullptr, nrn_jacob_NeuronVariables, nrn_state_NeuronVariables, nrn_init_NeuronVariables, hoc_nrnpointerindex, 1); + + mech_type = nrn_get_mechtype(mechanism_info[1]); + _nrn_mechanism_register_data_fields(mech_type, + _nrn_mechanism_field{"range_celsius"} /* 0 */, + _nrn_mechanism_field{"v_unused"} /* 1 */, + _nrn_mechanism_field{"g_unused"} /* 2 */ + ); + + hoc_register_prop_size(mech_type, 3, 0); + hoc_register_var(hoc_scalar_double, hoc_vector_double, hoc_intfunc); + hoc_register_npy_direct(mech_type, npy_direct_func_proc); + } +}