NEURON
hocevent.cpp
Go to the documentation of this file.
1 #include <objcmd.h>
2 #include <pool.h>
3 #include <netcon.h>
4 #include <nrnoc2iv.h>
5 #include <mymath.h>
6 
7 extern int cvode_active_;
8 
9 declarePool(HocEventPool, HocEvent)
10  implementPool(HocEventPool, HocEvent) HocEventPool* HocEvent::hepool_;
11 
13  stmt_ = nil;
14  ppobj_ = nil;
15  reinit_ = 0;
16 }
17 
19  if (stmt_) {
20  delete stmt_;
21  }
22 }
23 
24 void HocEvent::pr(const char* s, double tt, NetCvode* ns) {
25  Printf("%s HocEvent %s %.15g\n", s, stmt_ ? stmt_->name() : "", tt);
26 }
27 
28 HocEvent* HocEvent::alloc(const char* stmt, Object* ppobj, int reinit, Object* pyact) {
29  if (!hepool_) {
30  nrn_hoc_lock();
31  if (!hepool_) {
32  hepool_ = new HocEventPool(100, 1);
33  }
35  }
36  HocEvent* he = hepool_->alloc();
37  he->stmt_ = nil;
38  he->ppobj_ = ppobj;
39  he->reinit_ = reinit;
40  if (pyact) {
41  he->stmt_ = new HocCommand(pyact);
42  } else if (stmt) {
43  he->stmt_ = new HocCommand(stmt);
44  }
45  return he;
46 }
47 
49  if (stmt_) {
50  delete stmt_;
51  stmt_ = nil;
52  }
53  hepool_->hpfree(this);
54 }
55 
57  if (stmt_) {
58  delete stmt_;
59  stmt_ = nil;
60  }
61 }
62 
63 void HocEvent::deliver(double tt, NetCvode* nc, NrnThread* nt) {
64  extern double t;
65  if (!ppobj_) {
66  nc->allthread_handle(tt, this, nt);
67  return;
68  }
69  if (stmt_) {
70  if (nrn_nthread > 1 || nc->is_local()) {
71  if (!ppobj_) {
73  "multiple threads and/or local variable time step method require an "
74  "appropriate POINT_PROCESS arg to CVode.event to safely execute:",
75  stmt_->name());
76  }
77  Cvode* cv = (Cvode*) ob2pntproc(ppobj_)->nvi_;
78  if (cv && cvode_active_) {
79  nc->local_retreat(tt, cv);
80  if (reinit_) {
81  cv->set_init_flag();
82  }
83  nt->_t = cv->t_;
84  }
85  nrn_hoc_lock();
86  t = tt;
87  } else if (cvode_active_ && reinit_) {
88  nc->retreat(tt, nc->gcv_);
89  assert(MyMath::eq(tt, nc->gcv_->t_, NetCvode::eps(tt)));
90  assert(tt == nt->_t);
91  nc->gcv_->set_init_flag();
92  t = tt;
93  } else {
94  t = nt_t = tt;
95  }
96  stmt_->execute(false);
97  if (nrn_nthread > 1 || nc->is_local()) {
99  }
100  }
101  hefree();
102 }
103 
105  if (stmt_) {
106  stmt_->execute(false);
107  } else {
108  tstopset;
109  }
110  hefree();
111 }
112 
113 void HocEvent::pgvts_deliver(double tt, NetCvode* nc) {
114  deliver(tt, nc, nrn_threads);
115 }
116 
118  if (hepool_) {
119  hepool_->free_all();
120  }
121 }
122 
124  // pr("HocEvent::savestate_save", 0, net_cvode_instance);
125  HocEvent* he = new HocEvent();
126  if (stmt_) {
127  if (stmt_->pyobject()) {
128  he->stmt_ = new HocCommand(stmt_->pyobject());
129  } else {
130  he->stmt_ = new HocCommand(stmt_->name(), stmt_->object());
131  }
132  he->reinit_ = reinit_;
133  he->ppobj_ = ppobj_;
134  }
135  return he;
136 }
137 
138 void HocEvent::savestate_restore(double tt, NetCvode* nc) {
139  // pr("HocEvent::savestate_restore", tt, nc);
140  HocEvent* he = alloc(nil, nil, 0);
141  NrnThread* nt = nrn_threads;
142  if (stmt_) {
143  if (stmt_->pyobject()) {
144  he->stmt_ = new HocCommand(stmt_->pyobject());
145  } else {
146  he->stmt_ = new HocCommand(stmt_->name(), stmt_->object());
147  }
148  he->reinit_ = reinit_;
149  he->ppobj_ = ppobj_;
150  if (ppobj_) {
151  nt = (NrnThread*) ob2pntproc(ppobj_)->_vnt;
152  }
153  }
154  nc->event(tt, he, nt);
155 }
156 
158  HocEvent* he = new HocEvent();
159  int have_stmt, have_obj, index;
160  char stmt[256], objname[100], buf[200];
161  Object* obj = nil;
162  // nrn_assert(fscanf(f, "%d %d\n", &have_stmt, &have_obj) == 2);
163  nrn_assert(fgets(buf, 200, f));
164  nrn_assert(sscanf(buf, "%d %d\n", &have_stmt, &have_obj) == 2);
165  if (have_stmt) {
166  nrn_assert(fgets(stmt, 256, f));
167  stmt[strlen(stmt) - 1] = '\0';
168  if (have_obj) {
169  // nrn_assert(fscanf(f, "%s %d\n", objname, &index) == 1);
170  nrn_assert(fgets(buf, 200, f));
171  nrn_assert(sscanf(buf, "%s %d\n", objname, &index) == 1);
172  obj = hoc_name2obj(objname, index);
173  }
174  he->stmt_ = new HocCommand(stmt, obj);
175  }
176  return he;
177 }
178 
180  fprintf(f, "%d\n", HocEventType);
181  fprintf(f, "%d %d\n", stmt_ ? 1 : 0, (stmt_ && stmt_->object()) ? 1 : 0);
182  if (stmt_) {
183  fprintf(f, "%s\n", stmt_->name());
184  if (stmt_->object()) {
185  fprintf(f, "%s %d\n", stmt_->object()->ctemplate->sym->name, stmt_->object()->index);
186  }
187  }
188 }
#define nil
Definition: enter-scope.h:36
short index
Definition: cabvars.h:10
Definition: cvodeobj.h:76
double t_
Definition: cvodeobj.h:105
void set_init_flag()
Definition: cvodeobj.cpp:803
Object * object()
Definition: objcmd.h:30
Object * pyobject()
Definition: objcmd.h:33
int execute(bool notify=true)
Definition: objcmd.cpp:102
const char * name()
Definition: objcmd.cpp:81
static DiscreteEvent * savestate_read(FILE *)
Definition: hocevent.cpp:157
HocCommand * stmt_
Definition: netcon.h:381
virtual void savestate_restore(double deliverytime, NetCvode *)
Definition: hocevent.cpp:138
static void reclaim()
Definition: hocevent.cpp:117
virtual void allthread_handle()
Definition: hocevent.cpp:104
static HocEvent * alloc(const char *stmt, Object *, int, Object *pyact=nil)
Definition: hocevent.cpp:28
virtual ~HocEvent()
Definition: hocevent.cpp:18
virtual void deliver(double, NetCvode *, NrnThread *)
Definition: hocevent.cpp:63
void clear()
Definition: hocevent.cpp:56
int reinit_
Definition: netcon.h:383
HocCommand * stmt()
Definition: netcon.h:365
virtual void savestate_write(FILE *)
Definition: hocevent.cpp:179
HocEvent()
Definition: hocevent.cpp:12
virtual void pr(const char *, double t, NetCvode *)
Definition: hocevent.cpp:24
virtual DiscreteEvent * savestate_save()
Definition: hocevent.cpp:123
Object * ppobj_
Definition: netcon.h:382
static HocEventPool * hepool_
Definition: netcon.h:385
void hefree()
Definition: hocevent.cpp:48
virtual void pgvts_deliver(double t, NetCvode *)
Definition: hocevent.cpp:113
static bool eq(double x, double y, double e)
Definition: mymath.h:66
static double eps(double x)
Definition: netcvode.h:139
Cvode * gcv_
Definition: netcvode.h:246
void local_retreat(double, Cvode *)
Definition: netcvode.cpp:3585
bool is_local()
Definition: netcvode.cpp:1306
void retreat(double, Cvode *)
Definition: netcvode.cpp:3612
void allthread_handle(double, HocEvent *, NrnThread *)
Definition: netcvode.cpp:2791
TQItem * event(double tdeliver, DiscreteEvent *, NrnThread *)
Definition: netcvode.cpp:2712
Point_process * ob2pntproc(Object *)
Definition: hocmech.cpp:88
double t
Definition: cvodeobj.cpp:59
#define nt_t
Definition: cvodeobj.cpp:61
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:754
char buf[512]
Definition: init.cpp:13
Object * hoc_name2obj(const char *name, int index)
Definition: hoc_oop.cpp:922
void nrn_hoc_unlock()
Definition: multicore.cpp:1127
void nrn_hoc_lock()
Definition: multicore.cpp:1119
#define assert(ex)
Definition: hocassrt.h:32
declarePool(HocEventPool, HocEvent) implementPool(HocEventPool
int cvode_active_
Definition: fadvance.cpp:163
#define Printf
Definition: model.h:237
int nrn_nthread
Definition: multicore.cpp:46
NrnThread * nrn_threads
Definition: multicore.cpp:47
#define fprintf
Definition: mwprefix.h:30
#define HocEventType
Definition: netcon.h:44
#define nrn_assert(ex)
Definition: nrnassrt.h:53
#define tstopset
Definition: section.h:326
Represent main neuron object computed by single thread.
Definition: multicore.h:58
double _t
Definition: multicore.h:59
Definition: hocdec.h:227
int index
Definition: hocdec.h:229
void * nvi_
Definition: section.h:269
void * _vnt
Definition: section.h:270
implementPool(TQItemPool, TQItem) SelfQueue
Definition: tqueue.cpp:117