NEURON
mk_mech.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 <cstring>
10 #include <map>
11 #include <iostream>
12 #include <fstream>
13 #include <sstream>
14 
15 #include "coreneuron/nrnconf.h"
25 
26 static char banner[] = "Duke, Yale, and the BlueBrain Project -- Copyright 1984-2020";
27 
28 namespace coreneuron {
29 extern int nrn_nobanner_;
30 
31 // NB: this should go away
32 extern std::string cnrn_version();
33 std::map<std::string, int> mech2type;
34 
35 extern "C" {
36 void (*nrn2core_mkmech_info_)(std::ostream&);
37 }
38 static void mk_mech();
39 static void mk_mech(std::istream&);
40 
41 /// Read meta data about the mechanisms and allocate corresponding mechanism management data
42 /// structures
43 void mk_mech(const char* datpath) {
44  if (corenrn_embedded) {
45  // we are embedded in NEURON
46  mk_mech();
47  return;
48  }
49  {
50  std::string fname = std::string(datpath) + "/bbcore_mech.dat";
51  std::ifstream fs(fname);
52 
53  if (!fs.good()) {
54  fprintf(stderr,
55  "Error: couldn't find bbcore_mech.dat file in the dataset directory \n");
56  fprintf(stderr,
57  " Make sure to pass full directory path of dataset using -d DIR or "
58  "--datpath=DIR \n");
59  }
60 
61  nrn_assert(fs.good());
62  mk_mech(fs);
63  fs.close();
64  }
65 }
66 
67 // we are embedded in NEURON, get info as stringstream from nrnbbcore_write.cpp
68 static void mk_mech() {
69  static bool already_called = false;
70  if (already_called) {
71  return;
72  }
73  std::stringstream ss;
75  (*nrn2core_mkmech_info_)(ss);
76  mk_mech(ss);
77  already_called = true;
78 }
79 
80 static void mk_mech(std::istream& s) {
81  char version[256];
82  s >> version;
84 
85  // printf("reading %s\n", fname);
86  int n = 0;
87  nrn_assert(s >> n);
88 
89  /// Allocate space for mechanism related data structures
90  alloc_mech(n);
91 
92  /// Read all the mechanisms and their meta data
93  for (int i = 2; i < n; ++i) {
94  char mname[100];
95  int type = 0, pnttype = 0, is_art = 0, is_ion = 0, dsize = 0, pdsize = 0, vsize = 0;
96  nrn_assert(s >> mname >> type >> pnttype >> is_art >> is_ion >> dsize >> pdsize >> vsize);
97  nrn_assert(i == type);
98 
99  std::vector<int> array_dims(vsize);
100  for (size_t i = 0; i < vsize; ++i) {
101  nrn_assert(s >> array_dims[i]);
102  }
103  corenrn.get_array_dims().at(type) = array_dims;
104 
105 #ifdef DEBUG
106  printf("%s %d %d %d %d %d %d\n", mname, type, pnttype, is_art, is_ion, dsize, pdsize);
107 #endif
108  std::string str(mname);
109  corenrn.get_memb_func(type).sym = (Symbol*) strdup(mname);
110  mech2type[str] = type;
111  corenrn.get_pnt_map()[type] = (char) pnttype;
112  corenrn.get_prop_param_size()[type] = dsize;
113  corenrn.get_prop_dparam_size()[type] = pdsize;
114  corenrn.get_is_artificial()[type] = is_art;
115  if (is_ion) {
116  double charge = 0.;
117  nrn_assert(s >> charge);
118  // strip the _ion
119  char iname[100];
120  strcpy(iname, mname);
121  iname[strlen(iname) - 4] = '\0';
122  // printf("%s %s\n", mname, iname);
123  ion_reg(iname, charge);
124  }
125  // printf("%s %d %d\n", mname, nrn_get_mechtype(mname), type);
126  }
127 
128  if (nrnmpi_myid < 1 && nrn_nobanner_ == 0) {
129  fprintf(stderr, " \n");
130  fprintf(stderr, " %s\n", banner);
131  fprintf(stderr, " Version : %s\n", cnrn_version().c_str());
132  fprintf(stderr, " \n");
133  fflush(stderr);
134  }
135  /* will have to put this back if any mod file refers to diam */
136  // register_mech(morph_mech, morph_alloc, (Pfri)0, (Pfri)0, (Pfri)0, (Pfri)0, -1, 0);
137 
138  /// Calling _reg functions for the default mechanisms from the file mech/cfile/cabvars.h
139  for (int i = 0; mechanism[i]; i++) {
140  (*mechanism[i])();
141  }
142 }
143 
144 /// Get mechanism type by the mechanism name
145 int nrn_get_mechtype(const char* name) {
146  auto mapit = mech2type.find(name);
147  if (mapit == mech2type.end())
148  return -1; // Could not find the mechanism
149  return mapit->second;
150 }
151 
152 const char* nrn_get_mechname(int type) {
153  for (const auto& item: mech2type) {
154  if (type == item.second) {
155  return item.first.c_str();
156  }
157  }
158  return nullptr;
159 }
160 } // namespace coreneuron
auto & get_memb_func(size_t idx)
Definition: coreneuron.hpp:135
#define i
Definition: md1redef.h:19
static char banner[]
Definition: mk_mech.cpp:26
printf
Definition: extdef.h:5
const char * name
Definition: init.cpp:16
THIS FILE IS AUTO GENERATED DONT MODIFY IT.
int nrn_nobanner_
Declaring global corenrn_parameters object for this instance of CoreNeuron.
static const char * mechanism[]
Definition: capac.cpp:19
void(* nrn2core_mkmech_info_)(std::ostream &)
Definition: mk_mech.cpp:36
void ion_reg(const char *, double)
const char * nrn_get_mechname(int type)
Definition: mk_mech.cpp:152
void check_bbcore_write_version(const char *)
Definition: nrnoc_aux.cpp:128
CoreNeuron corenrn
Definition: multicore.cpp:53
static void mk_mech()
Definition: mk_mech.cpp:68
int nrn_get_mechtype(const char *name)
Get mechanism type by the mechanism name.
Definition: mk_mech.cpp:145
void alloc_mech(int memb_func_size_)
std::map< std::string, int > mech2type
Definition: mk_mech.cpp:33
std::string cnrn_version()
Definition: main1.cpp:382
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
s
Definition: multisend.cpp:521
short type
Definition: cabvars.h:10
#define charge
Definition: eion.cpp:384
Definition: model.h:47
Project version information.
Definition: config.h:26