NEURON
netcon.cpp
Go to the documentation of this file.
1 // NOTE: this assumes neuronapi.h is on your CPLUS_INCLUDE_PATH
2 #include <cstdlib>
3 #include <cstring>
4 #include <iostream>
5 #include <fstream>
6 #include <dlfcn.h>
7 #include "neuronapi.h"
8 #include "./test_common.h"
9 
10 using std::cout;
11 using std::endl;
12 using std::ofstream;
13 
14 constexpr std::array<double, 6> EXPECTED_V{
15  -0x1.04p+6,
16  -0x1.085a63d029bc3p+6,
17  -0x1.112a5e95eb67cp+6,
18  -0x1.1795abaec26c1p+6,
19  -0x1.0422351f3f9dcp+6,
20  -0x1.03e5317ac368cp+6,
21 };
22 
23 extern "C" void modl_reg(){/* No modl_reg */};
24 
25 int main(void) {
26  static std::array<const char*, 4> argv = {"netcon", "-nogui", "-nopython", nullptr};
27  nrn_init(3, argv.data());
28 
29  // load the stdrun library
30  char* temp_str = strdup("stdrun.hoc");
31  nrn_str_push(&temp_str);
32  nrn_function_call(nrn_symbol("load_file"), 1);
34  free(temp_str);
35 
36  // topology
37  auto soma = nrn_section_new("soma");
38 
39  // ion channels
40  nrn_mechanism_insert(soma, nrn_symbol("hh"));
41 
42  // NetStim
43  auto ns = nrn_object_new(nrn_symbol("NetStim"), 0);
44  nrn_property_set(ns, "start", 5);
45  nrn_property_set(ns, "noise", 1);
46  nrn_property_set(ns, "interval", 5);
47  nrn_property_set(ns, "number", 10);
48 
49  nrn_double_push(1);
50  nrn_double_push(2);
51  nrn_double_push(3);
52  nrn_method_call(ns, nrn_method_symbol(ns, "noiseFromRandom123"), 3);
53 
54  // syn = h.ExpSyn(soma(0.5))
55  nrn_double_push(0.5);
56  auto syn = nrn_object_new(nrn_symbol("ExpSyn"), 1);
57  nrn_property_set(syn, "tau", 3); // 3 ms timeconstant
58  nrn_property_set(syn, "e", 0); // 0 mV reversal potential (excitatory synapse)
59 
60  // nc = h.NetCon(ns, syn)
61  nrn_object_push(ns);
62  nrn_object_push(syn);
63  auto nc = nrn_object_new(nrn_symbol("NetCon"), 2);
64  nrn_property_array_set(nc, "weight", 0, 0.5);
65  nrn_property_set(nc, "delay", 0);
66 
67  auto vec = nrn_object_new(nrn_symbol("Vector"), 0);
68 
69  // nc.record(vec)
70  nrn_object_push(vec);
71  nrn_method_call(nc, nrn_method_symbol(nc, "record"), 1);
72  // TODO: record probably put something on the stack that should be removed
73 
74  // setup recording
75  Object* v = nrn_object_new(nrn_symbol("Vector"), 0);
76  nrn_rangevar_push(nrn_symbol("v"), soma, 0.5);
77  nrn_double_push(20);
78  nrn_method_call(v, nrn_method_symbol(v, "record"), 2);
79  nrn_object_unref(nrn_object_pop()); // record returns the vector
80 
81  // finitialize(-65)
82  nrn_double_push(-65);
83  nrn_function_call(nrn_symbol("finitialize"), 1);
85 
86  // continuerun(100)
87  nrn_double_push(100.5);
88  nrn_function_call(nrn_symbol("continuerun"), 1);
90 
91  if (!approximate(EXPECTED_V, v)) {
92  return 1;
93  }
94 }
#define v
Definition: md1redef.h:11
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
void modl_reg()
Definition: netcon.cpp:23
constexpr std::array< double, 6 > EXPECTED_V
Definition: netcon.cpp:14
int main(void)
Definition: netcon.cpp:25
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_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
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
void nrn_property_array_set(Object *obj, const char *name, int i, double value)
Definition: neuronapi.cpp:613
Object * nrn_object_new(Symbol *sym, int narg)
Definition: neuronapi.cpp:344
void nrn_object_push(Object *obj)
Definition: neuronapi.cpp:290
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