NEURON
node_index.cpp
Go to the documentation of this file.
1 // NOTE: this assumes neuronapi.h is on your CPLUS_INCLUDE_PATH
2 // Exercises nrn_segment_node_index, the C-API equivalent of Python's
3 // seg.node_index() (index of (sec, x)'s node in NEURON's internal node array).
4 #include <array>
5 #include <cstring>
6 #include <iostream>
7 #include "neuronapi.h"
8 
9 using std::cerr;
10 using std::endl;
11 
12 extern "C" void modl_reg(){/* No modl_reg */};
13 
14 static bool check(bool cond, const char* msg) {
15  if (!cond) {
16  cerr << "FAIL: " << msg << endl;
17  }
18  return cond;
19 }
20 
21 int main(void) {
22  static std::array<const char*, 4> argv = {"node_index", "-nogui", "-nopython", nullptr};
23  nrn_init(3, argv.data());
24 
25  // soma <- dend (3 segments), so the dend owns three distinct interior nodes.
26  Section* soma = nrn_section_new("soma");
27  Section* dend = nrn_section_new("dend");
28  nrn_section_connect(dend, 0, soma, 1);
29  nrn_nseg_set(dend, 3);
30 
31  // node indices become canonical once the tree is set up.
32  nrn_double_push(-65);
33  nrn_function_call(nrn_symbol("finitialize"), 1);
35 
36  bool ok = true;
37 
38  // Exact node indices for this topology, matching Python's seg.node_index().
39  // soma's nodes are 0 (0-end), 1 (center), 2 (1-end, shared with dend's
40  // 0-end at the connection); dend's three interior segments are nodes 3-5.
41  ok &= check(nrn_segment_node_index(soma, 0.5) == 1, "soma(0.5) is node 1");
42  ok &= check(nrn_segment_node_index(dend, 1.0 / 6.0) == 3, "dend(1/6) is node 3");
43  ok &= check(nrn_segment_node_index(dend, 3.0 / 6.0) == 4, "dend(3/6) is node 4");
44  ok &= check(nrn_segment_node_index(dend, 5.0 / 6.0) == 5, "dend(5/6) is node 5");
45 
46  // contract: a null section yields -1 rather than crashing.
47  ok &= check(nrn_segment_node_index(nullptr, 0.5) == -1, "null section returns -1");
48 
49  return ok ? 0 : 1;
50 }
void modl_reg()
Definition: node_index.cpp:12
int main(void)
Definition: node_index.cpp:21
static bool check(bool cond, const char *msg)
Definition: node_index.cpp:14
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:246
double nrn_double_pop(void)
Definition: neuronapi.cpp:288
void nrn_nseg_set(Section *const sec, const int nseg)
Definition: neuronapi.cpp:181
Section * nrn_section_new(char const *const name)
Definition: neuronapi.cpp:76
void nrn_section_connect(Section *child_sec, double child_x, Section *parent_sec, double parent_x)
Definition: neuronapi.cpp:89
void nrn_function_call(Symbol *sym, int narg)
Definition: neuronapi.cpp:382
int nrn_segment_node_index(Section *const sec, const double x)
Definition: neuronapi.cpp:214
void nrn_double_push(double val)
Definition: neuronapi.cpp:284