NEURON
global_vars.cpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================
7 */
8 
9 #include <cstdio>
10 #include <cstring>
11 #include <map>
12 #include <string>
13 #include <algorithm>
14 
16 #include "coreneuron/nrnconf.h"
21 
22 void* (*nrn2core_get_global_dbl_item_)(void*, const char*& name, int& size, double*& val);
23 int (*nrn2core_get_global_int_item_)(const char* name);
24 
25 namespace coreneuron {
26 using PSD = std::pair<std::size_t, double*>;
27 using N2V = std::map<std::string, PSD>;
28 
29 static N2V* n2v;
30 
32  if (!n2v) {
33  n2v = new N2V();
34  }
35  for (size_t i = 0; ds[i].name; ++i) {
36  (*n2v)[ds[i].name] = PSD(0, ds[i].pdoub);
37  }
38  for (size_t i = 0; dv[i].name; ++i) {
39  (*n2v)[dv[i].name] = PSD(dv[i].index1, ds[i].pdoub);
40  }
41 }
42 
43 void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_value) {
44  if (!n2v) {
45  n2v = new N2V();
46  }
47  (*n2v)["celsius"] = PSD(0, &celsius);
48  (*n2v)["dt"] = PSD(0, &dt);
49  (*n2v)["t"] = PSD(0, &t);
50  (*n2v)["PI"] = PSD(0, &pi);
51 
52  if (corenrn_embedded) { // CoreNEURON embedded, get info direct from NEURON
53 
54  const char* name;
55  int size;
56  double* val = nullptr;
57  void* p = nullptr;
58  while (1) {
59  p = (*nrn2core_get_global_dbl_item_)(p, name, size, val);
60  // If the last item in the NEURON symbol table is a USERDOUBLE
61  // then p is NULL but val is not NULL and following fragment
62  // will be processed before exit from loop.
63  if (val) {
64  N2V::iterator it = n2v->find(name);
65  if (it != n2v->end()) {
66  if (size == 0) {
67  nrn_assert(it->second.first == 0);
68  *(it->second.second) = val[0];
69  } else {
70  nrn_assert(it->second.first == (size_t) size);
71  double* pval = it->second.second;
72  for (int i = 0; i < size; ++i) {
73  pval[i] = val[i];
74  }
75  }
76  }
77  delete[] val;
78  val = nullptr;
79  }
80  if (!p) {
81  break;
82  }
83  }
84  secondorder = (*nrn2core_get_global_int_item_)("secondorder");
85  nrnran123_set_globalindex((*nrn2core_get_global_int_item_)("Random123_global_index"));
86 
87  } else { // get the info from the globals.dat file
88  std::string fname = std::string(path) + std::string("/globals.dat");
89  FILE* f = fopen(fname.c_str(), "r");
90  if (!f) {
91  printf("ignore: could not open %s\n", fname.c_str());
92  delete n2v;
93  n2v = nullptr;
94  return;
95  }
96 
97  char line[256];
98 
99  nrn_assert(fscanf(f, "%s\n", line) == 1);
101 
102  for (;;) {
103  char name[256];
104  double val;
105  int n;
106  nrn_assert(fgets(line, 256, f) != nullptr);
107  N2V::iterator it;
108  if (sscanf(line, "%s %lf", name, &val) == 2) {
109  if (strcmp(name, "0") == 0) {
110  break;
111  }
112  it = n2v->find(name);
113  if (it != n2v->end()) {
114  nrn_assert(it->second.first == 0);
115  *(it->second.second) = val;
116  }
117  } else if (sscanf(line, "%[^[][%d]\n", name, &n) == 2) {
118  if (strcmp(name, "0") == 0) {
119  break;
120  }
121  it = n2v->find(name);
122  if (it != n2v->end()) {
123  nrn_assert(it->second.first == (size_t) n);
124  double* pval = it->second.second;
125  for (int i = 0; i < n; ++i) {
126  nrn_assert(fgets(line, 256, f) != nullptr);
127  nrn_assert(sscanf(line, "%lf\n", &val) == 1);
128  pval[i] = val;
129  }
130  }
131  } else {
132  nrn_assert(0);
133  }
134  }
135 
136  while (fgets(line, 256, f)) {
137  char name[256];
138  int n;
139  if (sscanf(line, "%s %d", name, &n) == 2) {
140  if (strcmp(name, "secondorder") == 0) {
141  secondorder = n;
142  } else if (strcmp(name, "Random123_globalindex") == 0) {
143  nrnran123_set_globalindex((uint32_t) n);
144  }
145  }
146  }
147 
148  fclose(f);
149 
150  // overwrite global.dat config if seed is specified on Command line
151  if (cli_global_seed) {
152  nrnran123_set_globalindex((uint32_t) cli_global_seed_value);
153  }
154  }
155 
156 #if CORENRN_DEBUG
157  for (const auto& item: *n2v) {
158  printf("%s %ld %p\n", item.first.c_str(), item.second.first, item.second.second);
159  }
160 #endif
161 
162  delete n2v;
163  n2v = nullptr;
164 }
165 
166 } // namespace coreneuron
#define pval
Definition: md1redef.h:40
#define i
Definition: md1redef.h:19
int(* nrn2core_get_global_int_item_)(const char *name)
Definition: global_vars.cpp:23
double * pdoub
Definition: hocmodl.h:24
int index1
Definition: hocmodl.h:15
printf
Definition: extdef.h:5
const char * name
Definition: init.cpp:16
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
void nrnran123_set_globalindex(uint32_t gix)
Definition: nrnran123.cpp:117
void set_globals(const char *path, bool cli_global_seed, int cli_global_seed_value)
Definition: global_vars.cpp:43
void check_bbcore_write_version(const char *)
Definition: nrnoc_aux.cpp:128
double celsius
void hoc_register_var(DoubScal *ds, DoubVec *dv, VoidFunc *)
Definition: global_vars.cpp:31
std::pair< std::size_t, double * > PSD
Definition: global_vars.cpp:26
std::map< std::string, PSD > N2V
Definition: global_vars.cpp:27
static N2V * n2v
Definition: global_vars.cpp:29
bool corenrn_embedded
--> Coreneuron
Definition: nrn_setup.cpp:47
#define nrn_assert(x)
assert()-like macro, independent of NDEBUG status
Definition: nrn_assert.h:33
int const size_t const size_t n
Definition: nrngsl.h:10
size_t p
const char * name
Definition: membfunc.hpp:176
const char * name
Definition: membfunc.hpp:180