NEURON
hh_sim.cpp
Go to the documentation of this file.
1 // NOTE: this assumes neuronapi.h is on your CPLUS_INCLUDE_PATH
2 #include "neuronapi.h"
3 #include <dlfcn.h>
4 
5 #include <array>
6 #include <cassert>
7 #include <cstdlib>
8 #include <cstring>
9 #include <iostream>
10 #include <fstream>
11 
12 #include "./test_common.h"
13 
14 using std::cout;
15 using std::endl;
16 using std::ofstream;
17 
18 constexpr std::array<double, 3> EXPECTED_V{
19  -0x1.04p+6,
20  -0x1.b254ad82e20edp+5,
21  -0x1.24a52af1ab463p+6,
22 };
23 
24 extern "C" void modl_reg(){/* No modl_reg */};
25 
26 int main(void) {
27  static std::array<const char*, 4> argv = {"hh_sim", "-nogui", "-nopython", nullptr};
28  nrn_init(3, argv.data());
29 
30  // load the stdrun library
31  char* temp_str = strdup("stdrun.hoc");
32  nrn_str_push(&temp_str);
33  nrn_function_call(nrn_symbol("load_file"), 1);
35  free(temp_str);
36 
37  // topology
38  Section* soma = nrn_section_new("soma");
39  nrn_nseg_set(soma, 3);
40 
41  // define soma morphology with two 3d points
42  nrn_section_push(soma);
43  for (double x: {0, 0, 0, 10}) {
44  nrn_double_push(x);
45  }
46  nrn_function_call(nrn_symbol("pt3dadd"), 4);
47  nrn_double_pop(); // pt3dadd returns a number
48  for (double x: {10, 0, 0, 10}) {
49  nrn_double_push(x);
50  }
51  nrn_function_call(nrn_symbol("pt3dadd"), 4);
52  nrn_double_pop(); // pt3dadd returns a number
53 
54  // ion channels
55  nrn_mechanism_insert(soma, nrn_symbol("hh"));
56 
57  // current clamp at soma(0.5)
58  nrn_double_push(0.5);
59  Object* iclamp = nrn_object_new(nrn_symbol("IClamp"), 1);
60  nrn_property_set(iclamp, "amp", 0.3);
61  nrn_property_set(iclamp, "del", 1);
62  nrn_property_set(iclamp, "dur", 0.1);
63 
64  // setup recording
65  Object* v = nrn_object_new(nrn_symbol("Vector"), 0);
66  nrn_rangevar_push(nrn_symbol("v"), soma, 0.5);
67  nrn_double_push(5.);
68  nrn_method_call(v, nrn_method_symbol(v, "record"), 2);
69  nrn_object_unref(nrn_object_pop()); // record returns the vector
70 
71  // finitialize(-65)
72  nrn_double_push(-65);
73  nrn_function_call(nrn_symbol("finitialize"), 1);
75 
76  // continuerun(10.5)
77  nrn_double_push(10.5);
78  nrn_function_call(nrn_symbol("continuerun"), 1);
80 
81  if (!approximate(EXPECTED_V, v)) {
82  return 1;
83  }
84 }
#define v
Definition: md1redef.h:11
void modl_reg()
Definition: hh_sim.cpp:24
int main(void)
Definition: hh_sim.cpp:26
constexpr std::array< double, 3 > EXPECTED_V
Definition: hh_sim.cpp:18
static char ** argv
Definition: inithoc.cpp:46
static void nrn_init(neuron::model_sorted_token const &, NrnThread *nt, Memb_list *ml, int type)
Definition: kschan.cpp:69
Symbol * nrn_symbol(char const *const name)
Definition: neuronapi.cpp:232
double nrn_double_pop(void)
Definition: neuronapi.cpp:262
Object * nrn_object_pop(void)
Definition: neuronapi.cpp:294
void nrn_nseg_set(Section *const sec, const int nseg)
Definition: neuronapi.cpp:180
void nrn_method_call(Object *obj, Symbol *method_sym, int narg)
Definition: neuronapi.cpp:352
Symbol * nrn_method_symbol(const Object *obj, char const *const name)
Definition: neuronapi.cpp:348
void nrn_section_push(Section *sec)
Definition: neuronapi.cpp:138
Section * nrn_section_new(char const *const name)
Definition: neuronapi.cpp:75
void nrn_rangevar_push(Symbol *sym, Section *sec, double x)
Definition: neuronapi.cpp:215
Object * nrn_object_new(Symbol *sym, int narg)
Definition: neuronapi.cpp:344
void nrn_function_call(Symbol *sym, int narg)
Definition: neuronapi.cpp:356
void nrn_property_set(Object *obj, const char *name, double value)
Definition: neuronapi.cpp:600
void nrn_object_unref(Object *obj)
Definition: neuronapi.cpp:418
void nrn_double_push(double val)
Definition: neuronapi.cpp:258
void nrn_mechanism_insert(Section *sec, const Symbol *mechanism)
Definition: neuronapi.cpp:146
void nrn_str_push(char **str)
Definition: neuronapi.cpp:274
Definition: hocdec.h:173