NEURON
prcellstate.cpp
Go to the documentation of this file.
1 #include "../../nrnconf.h"
2 #include "section.h"
3 #include "membfunc.h"
4 #include "nrniv_mf.h"
5 #include "netcon.h"
6 #include <map>
7 #include "OS/list.h"
8 #include "neuron.h"
9 
10 #define precision 15
11 
12 void nrn_prcellstate(int gid, const char* filesuffix);
13 
14 declarePtrList(NetConList, NetCon); // NetCons in same order as Point_process
15 implementPtrList(NetConList, NetCon); // and there may be several per pp.
16 
17 
18 static void pr_memb(int type,
19  Memb_list* ml,
20  int* cellnodes,
21  NrnThread& nt,
22  FILE* f,
23  std::map<void*, int>& pnt2index) {
24  int header_printed = 0;
25  int size = nrn_prop_param_size_[type];
26  int receives_events = pnt_receive[type] ? 1 : 0;
27  for (int i = 0; i < ml->nodecount; ++i) {
28  int inode = ml->nodeindices[i];
29  if (cellnodes[inode] >= 0) {
30  if (!header_printed) {
31  header_printed = 1;
32  fprintf(f, "type=%d %s size=%d\n", type, memb_func[type].sym->name, size);
33  }
34  if (receives_events) {
35  fprintf(f, "%d nri %lu\n", cellnodes[inode], pnt2index.size());
36  Point_process* pp = (Point_process*) ml->pdata[i][1]._pvoid;
37  pnt2index.emplace(pp, pnt2index.size());
38  }
39  for (int j = 0; j < size; ++j) {
40  fprintf(f, " %d %d %.*g\n", cellnodes[inode], j, precision, ml->data[i][j]);
41  }
42  }
43  }
44 }
45 
46 static void pr_netcon(NrnThread& nt, FILE* f, const std::map<void*, int>& pnt2index) {
47  if (pnt2index.empty()) {
48  return;
49  }
50  // pnt2index table has been filled
51 
52  // List of NetCon for each of the NET_RECEIVE point process instances
53  // ... all NetCon list in the hoc NetCon cTemplate
54  NetConList** nclist = new NetConList*[pnt2index.size()];
55  for (size_t i = 0; i < pnt2index.size(); ++i) {
56  nclist[i] = new NetConList(1);
57  }
58  int nc_cnt = 0;
59  Symbol* ncsym = hoc_lookup("NetCon");
60  hoc_Item* q;
61  ITERATE(q, ncsym->u.ctemplate->olist) {
62  Object* obj = OBJ(q);
63  NetCon* nc = (NetCon*) obj->u.this_pointer;
64  Point_process* pp = nc->target_;
65  const auto& it = pnt2index.find(pp);
66  if (it != pnt2index.end()) {
67  nclist[it->second]->append(nc);
68  ++nc_cnt;
69  }
70  }
71  fprintf(f, "netcons %d\n", nc_cnt);
72  fprintf(f, " pntindex srcgid active delay weights\n");
73  for (size_t i = 0; i < pnt2index.size(); ++i) {
74  for (int j = 0; j < nclist[i]->count(); ++j) {
75  NetCon* nc = nclist[i]->item(j);
76  int srcgid = -3;
77  srcgid = (nc->src_) ? nc->src_->gid_ : -3;
78  if (srcgid < 0 && nc->src_ && nc->src_->osrc_) {
79  const char* name = nc->src_->osrc_->ctemplate->sym->name;
80  fprintf(f, "%zd %s %d %.*g", i, name, nc->active_ ? 1 : 0, precision, nc->delay_);
81  } else if (srcgid < 0 && nc->src_ && nc->src_->ssrc_) {
82  fprintf(f, "%zd %s %d %.*g", i, "v", nc->active_ ? 1 : 0, precision, nc->delay_);
83  } else {
84  fprintf(f, "%zd %d %d %.*g", i, srcgid, nc->active_ ? 1 : 0, precision, nc->delay_);
85  }
86  int wcnt = pnt_receive_size[nc->target_->prop->type];
87  for (int k = 0; k < wcnt; ++k) {
88  fprintf(f, " %.*g", precision, nc->weight_[k]);
89  }
90  fprintf(f, "\n");
91  }
92  }
93  // cleanup
94  for (size_t i = 0; i < pnt2index.size(); ++i) {
95  delete nclist[i];
96  }
97  delete[] nclist;
98 }
99 
100 static void pr_realcell(PreSyn& ps, NrnThread& nt, FILE* f) {
101  // threshold variable is a voltage
102  printf("thvar=%p actual_v=%p end=%p\n", ps.thvar_, nt._actual_v, nt._actual_v + nt.end);
103  int inode = -1;
104  if (ps.thvar_ < nt._actual_v || ps.thvar_ >= (nt._actual_v + nt.end)) {
105  if (ps.ssrc_) { /* not cache efficient, search the nodes in this section */
106  printf("%s\n", ps.ssrc_ ? secname(ps.ssrc_) : "unknown");
107  for (int i = 0; i < ps.ssrc_->nnode; ++i) {
108  Node* nd = ps.ssrc_->pnode[i];
109  if (ps.thvar_ == nd->_v) {
110  inode = nd->v_node_index;
111  break;
112  }
113  }
114  if (inode < 0) { /* check parent node */
115  Node* nd = ps.ssrc_->parentnode;
116  if (ps.thvar_ == nd->_v) {
117  inode = nd->v_node_index;
118  }
119  }
120  }
121  if (inode < 0) {
122  hoc_execerror("gid not associated with a voltage", 0);
123  }
124  } else {
125  inode = ps.thvar_ - nt._actual_v;
126  }
127 
128  // and the root node is ...
129  int rnode = inode;
130  while (rnode >= nt.ncell) {
131  rnode = nt._v_parent_index[rnode];
132  }
133 
134  // count the number of nodes in the cell
135  // do not assume all cell nodes except the root are contiguous
136  int* cellnodes = new int[nt.end];
137  for (int i = 0; i < nt.end; ++i) {
138  cellnodes[i] = -1;
139  }
140  int cnt = 0;
141  cellnodes[rnode] = cnt++;
142  for (int i = nt.ncell; i < nt.end; ++i) {
143  if (cellnodes[nt._v_parent_index[i]] >= 0) {
144  cellnodes[i] = cnt++;
145  }
146  }
147  fprintf(f, "%d nodes %d is the threshold node\n", cnt, cellnodes[inode] - 1);
148  fprintf(f, " threshold %.*g\n", precision, ps.threshold_);
149  fprintf(f, "inode parent area a b\n");
150  for (int i = 0; i < nt.end; ++i)
151  if (cellnodes[i] >= 0) {
152  Node* nd = nt._v_node[i]; // if not cach_efficient then _actual_area=NULL
153  fprintf(f,
154  "%d %d %.*g %.*g %.*g\n",
155  cellnodes[i],
156  i < nt.ncell ? -1 : cellnodes[nt._v_parent_index[i]],
157  precision,
158  NODEAREA(nd),
159  precision,
160  nt._actual_a[i],
161  precision,
162  nt._actual_b[i]);
163  }
164  fprintf(f, "inode v\n");
165  for (int i = 0; i < nt.end; ++i)
166  if (cellnodes[i] >= 0) {
167  Node* nd = nt._v_node[i]; // if not cach_efficient then _actual_v=NULL
168  fprintf(f, "%d %.*g\n", cellnodes[i], precision, NODEV(nd));
169  }
170 
171  {
172  std::map<void*, int> pnt2index;
173  // each mechanism
174  for (NrnThreadMembList* tml = nt.tml; tml; tml = tml->next) {
175  pr_memb(tml->index, tml->ml, cellnodes, nt, f, pnt2index);
176  }
177 
178  // the NetCon info
179  pr_netcon(nt, f, pnt2index);
180  }
181  delete[] cellnodes;
182 }
183 
184 void nrn_prcellstate(int gid, const char* suffix) {
185  PreSyn* ps = nrn_gid2outputpresyn(gid);
186  if (!ps) {
187  return;
188  }
189  // found it so create a <gid>_<suffix>.nrn file
190  char buf[200];
191  sprintf(buf, "%d_%s.nrndat", gid, suffix);
192  FILE* f = fopen(buf, "w");
193  assert(f);
194  NrnThread& nt = *ps->nt_;
195  fprintf(f, "gid = %d\n", gid);
196  fprintf(f, "t = %.*g\n", precision, nt._t);
197  fprintf(f, "celsius = %.*g\n", precision, celsius);
198  if (ps->thvar_) {
199  pr_realcell(*ps, nt, f);
200  }
201  fclose(f);
202 }
const char * secname(Section *sec)
Definition: cabcode.cpp:1776
Memb_func * memb_func
Definition: init.cpp:123
short type
Definition: cabvars.h:9
Definition: netcon.h:83
double * weight_
Definition: netcon.h:112
bool active_
Definition: netcon.h:115
double delay_
Definition: netcon.h:109
Point_process * target_
Definition: netcon.h:111
PreSyn * src_
Definition: netcon.h:110
Definition: netcon.h:255
double * thvar_
Definition: netcon.h:294
Section * ssrc_
Definition: netcon.h:296
NrnThread * nt_
Definition: netcon.h:300
int gid_
Definition: netcon.h:307
Object * osrc_
Definition: netcon.h:295
double threshold_
Definition: netcon.h:292
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:754
char buf[512]
Definition: init.cpp:13
Symbol * hoc_lookup(const char *)
#define assert(ex)
Definition: hocassrt.h:32
#define OBJ(q)
Definition: hoclist.h:67
double celsius
Definition: init.cpp:99
#define i
Definition: md1redef.h:12
#define ITERATE(itm, lst)
Definition: model.h:25
char * name
Definition: init.cpp:16
#define printf
Definition: mwprefix.h:26
#define fprintf
Definition: mwprefix.h:30
PreSyn * nrn_gid2outputpresyn(int gid)
Definition: netpar.cpp:1573
short * pnt_receive_size
Definition: init.cpp:135
ReceiveFunc * pnt_receive
Definition: init.cpp:133
int * nrn_prop_param_size_
Definition: init.cpp:140
static char suffix[256]
Definition: nocpout.cpp:149
size_t q
if(status)
size_t j
static philox4x32_key_t k
Definition: nrnran123.cpp:11
declarePtrList(NetConList, NetCon)
#define precision
Definition: prcellstate.cpp:10
implementPtrList(NetConList, NetCon)
static void pr_netcon(NrnThread &nt, FILE *f, const std::map< void *, int > &pnt2index)
Definition: prcellstate.cpp:46
static void pr_realcell(PreSyn &ps, NrnThread &nt, FILE *f)
void nrn_prcellstate(int gid, const char *filesuffix)
static void pr_memb(int type, Memb_list *ml, int *cellnodes, NrnThread &nt, FILE *f, std::map< void *, int > &pnt2index)
Definition: prcellstate.cpp:18
#define NODEV(n)
Definition: section.h:115
#define NODEAREA(n)
Definition: section.h:116
FILE * fopen()
#define cnt
Definition: spt2queue.cpp:19
int nodecount
Definition: nrnoc_ml.h:18
double ** data
Definition: nrnoc_ml.h:14
Datum ** pdata
Definition: nrnoc_ml.h:15
Definition: section.h:133
double * _v
Definition: section.h:140
int v_node_index
Definition: section.h:175
Represent main neuron object computed by single thread.
Definition: multicore.h:58
int * _v_parent_index
Definition: multicore.h:76
NrnThreadMembList * tml
Definition: multicore.h:62
int ncell
Definition: multicore.h:64
int end
Definition: multicore.h:65
double * _actual_b
Definition: multicore.h:73
double * _actual_v
Definition: multicore.h:74
double * _actual_a
Definition: multicore.h:72
Node ** _v_node
Definition: multicore.h:77
double _t
Definition: multicore.h:59
struct NrnThreadMembList * next
Definition: multicore.h:34
Definition: hocdec.h:227
void * this_pointer
Definition: hocdec.h:232
union Object::@39 u
Prop * prop
Definition: section.h:265
short type
Definition: section.h:216
struct Node ** pnode
Definition: section.h:51
struct Node * parentnode
Definition: section.h:50
short nnode
Definition: section.h:41
Definition: model.h:57
union Symbol::@18 u
HocStruct cTemplate * ctemplate
Definition: hocdec.h:152
hoc_List * olist
Definition: hocdec.h:204
void * _pvoid
Definition: hocdec.h:187