NEURON
vclamp.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 extern "C" void modl_reg(){/* No modl_reg */};
19 
20 constexpr std::array<double, 7> EXPECTED_V{
21  -0x1.04p+6,
22  -0x1.00d7f6756215p-182,
23  0x1.3ffe5c93f70cep+3,
24  0x1.3ffe5c93f70cep+3,
25  0x1.3ffe5c93f70cep+2,
26  0x1.3ffe5c93f70cep+2,
27  0x1.3ffe5c93f70cep+2,
28 };
29 
30 int main(void) {
31  static std::array<const char*, 4> argv = {"vclamp", "-nogui", "-nopython", nullptr};
32  nrn_init(3, argv.data());
33 
34  // load the stdrun library
35  char* temp_str = strdup("stdrun.hoc");
36  nrn_str_push(&temp_str);
37  nrn_function_call(nrn_symbol("load_file"), 1);
39  free(temp_str);
40 
41  // topology
42  Section* soma = nrn_section_new("soma");
43 
44  // define soma morphology with two 3d points
45  nrn_section_push(soma);
46  assert(soma);
48  nrn_section_length_set(soma, 10);
49  nrn_segment_diam_set(soma, 0.5, 3);
50 
51  // voltage clamp at soma(0.5)
52  nrn_double_push(0.5);
53  Object* vclamp = nrn_object_new(nrn_symbol("VClamp"), 1);
54  // 0 mV for 1 ms; 10 mV for the next 2 ms; 5 mV for the next 3 ms
55  int i = 0;
56  for (auto& [amp, dur]: std::initializer_list<std::pair<int, double>>{{0, 1}, {10, 2}, {5, 3}}) {
57  nrn_property_array_set(vclamp, "amp", i, amp);
58  nrn_property_array_set(vclamp, "dur", i, dur);
59  ++i;
60  }
61  // setup recording
62  Object* v = nrn_object_new(nrn_symbol("Vector"), 0);
63  nrn_rangevar_push(nrn_symbol("v"), soma, 0.5);
64  nrn_double_push(1);
65  nrn_method_call(v, nrn_method_symbol(v, "record"), 2);
66  nrn_object_unref(nrn_object_pop()); // record returns the vector
67 
68  // finitialize(-65)
69  nrn_double_push(-65);
70  nrn_function_call(nrn_symbol("finitialize"), 1);
72 
73  // continuerun(6)
74  nrn_double_push(6);
75  nrn_function_call(nrn_symbol("continuerun"), 1);
77 
78  if (!approximate(EXPECTED_V, v)) {
79  return 1;
80  }
81 }
#define v
Definition: md1redef.h:11
#define i
Definition: md1redef.h:19
#define assert(ex)
Definition: hocassrt.h:24
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 nrn_section_length_set(Section *sec, const double length)
Definition: neuronapi.cpp:96
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
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
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_segment_diam_set(Section *const sec, const double x, const double diam)
Definition: neuronapi.cpp:184
void nrn_function_call(Symbol *sym, int narg)
Definition: neuronapi.cpp:356
void nrn_object_unref(Object *obj)
Definition: neuronapi.cpp:418
void nrn_double_push(double val)
Definition: neuronapi.cpp:258
void nrn_str_push(char **str)
Definition: neuronapi.cpp:274
Definition: hocdec.h:173
constexpr std::array< double, 7 > EXPECTED_V
Definition: vclamp.cpp:20
void modl_reg()
Definition: vclamp.cpp:18
int main(void)
Definition: vclamp.cpp:30