-
Notifications
You must be signed in to change notification settings - Fork 113
/
netcvode.cpp
6795 lines (6398 loc) · 212 KB
/
netcvode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <../../nrnconf.h>
// define to 0 if do not wish use_min_delay_ to ever be 1
#define USE_MIN_DELAY 1
#include <stdlib.h>
#include <nrnmpi.h>
#include <errno.h>
#include <time.h>
#include <regex>
#include "classreg.h"
#include "nrnoc2iv.h"
#include "parse.hpp"
#include "cvodeobj.h"
#include "hoclist.h"
#include "pool.hpp"
#include "tqueue.hpp"
#include "ocobserv.h"
#include "nrnneosm.h"
#include "datapath.h"
#include "objcmd.h"
#include "shared/sundialsmath.h"
#include "kssingle.h"
#include "ocnotify.h"
#include "utils/enumerate.h"
#if HAVE_IV
#include "ivoc.h"
#include "glinerec.h"
#include "ocjump.h"
#endif
#include "vrecitem.h"
#include "oclist.h"
#define PROFILE 0
#include "htlist.h"
#include "ivocvect.h"
#include "netcon.h"
#include "netcvode.h"
#include "nrn_ansi.h"
#include "nrncore_write/utils/nrncore_utils.h"
#include "nrniv_mf.h"
#include "nrnste.h"
#include "profile.h"
#include "utils/profile/profiler_interface.h"
#include <array>
#include <unordered_set>
#include <utility>
typedef void (*ReceiveFunc)(Point_process*, double*, double);
#define lvardtloop(i, j) \
for (i = 0; i < nrn_nthread; ++i) \
for (j = 0; j < p[i].nlcv_; ++j)
#define NVI_SUCCESS 0
#define PP2NT(pp) ((NrnThread*) ((pp)->_vnt))
#define PP2t(pp) (PP2NT(pp)->_t)
#define LOCK(m) /**/
#define UNLOCK(m) /**/
// classical and when DiscreteEvent::deliver is already in the right thread
// via a future thread instance of NrnNetItem with its own tqe.
#define POINT_RECEIVE(type, tar, w, f) (*pnt_receive[type])(tar, w, f)
// when global tqe is managed by master thread and the correct thread
// needs to be fired to execute the NET_RECEIVE block.
//#define POINT_RECEIVE(type, tar, w, f) ns->point_receive(type, tar, w, f)
#include "membfunc.h"
extern void single_event_run();
extern void setup_topology(), v_setup_vectors();
extern int nrn_errno_check(int);
extern NetCvode* net_cvode_instance;
extern cTemplate** nrn_pnt_template_;
extern double t, dt;
extern void nrn_cvfun(double t, double* y, double* ydot);
extern void nrn_cleanup_presyn(PreSyn*);
#define nt_dt nrn_threads->_dt
#define nt_t nrn_threads->_t
extern void nrn_parent_info(Section*);
extern Object* nrn_sec2cell(Section*);
extern int nrn_sec2cell_equals(Section*, Object*);
extern ReceiveFunc* pnt_receive;
extern ReceiveFunc* pnt_receive_init;
extern short* nrn_is_artificial_; // should be bool but not using that type in c
extern short* nrn_artcell_qindex_;
int nrn_use_selfqueue_;
void nrn_pending_selfqueue(double tt, NrnThread*);
static void all_pending_selfqueue(double tt);
static void* pending_selfqueue(NrnThread*);
extern int hoc_araypt(Symbol*, int);
extern int hoc_stacktype();
void nrn_use_daspk(int);
extern int nrn_use_daspk_;
int linmod_extra_eqn_count();
extern int nrn_modeltype();
extern Symlist* hoc_built_in_symlist;
extern Symlist* hoc_top_level_symlist;
extern TQueue* net_cvode_instance_event_queue(NrnThread*);
extern hoc_Item* net_cvode_instance_psl();
extern std::vector<PlayRecord*>* net_cvode_instance_prl();
extern void nrn_update_ps2nt();
extern void nrn_use_busywait(int);
void* nrn_interthread_enqueue(NrnThread*);
extern void (*nrnthread_v_transfer_)(NrnThread*);
Object* (*nrnpy_seg_from_sec_x)(Section*, double);
extern "C" void nrnthread_get_trajectory_requests(int tid,
int& bsize,
int& n_pr,
void**& vpr,
int& n_trajec,
int*& types,
int*& indices,
double**& pvars,
double**& varrays);
extern "C" void nrnthread_trajectory_values(int tid, int n_pr, void** vpr, double t);
extern "C" void nrnthread_trajectory_return(int tid,
int n_pr,
int bsize,
int vecsz,
void** vpr,
double t);
bool nrn_trajectory_request_per_time_step_ = false;
#if NRN_MUSIC
#include "nrnmusicapi.h"
#endif
extern int hoc_return_type_code;
extern int nrn_fornetcon_cnt_;
extern int* nrn_fornetcon_index_;
extern int* nrn_fornetcon_type_;
// for use in mod files
double nrn_netcon_get_delay(NetCon* nc) {
return nc->delay_;
}
void nrn_netcon_set_delay(NetCon* nc, double d) {
nc->delay_ = d;
}
int nrn_netcon_weight(NetCon* nc, double** pw) {
*pw = nc->weight_;
return nc->cnt_;
}
double nrn_event_queue_stats(double* stats) {
#if COLLECT_TQueue_STATISTICS
net_cvode_instance_event_queue(nrn_threads)->spike_stat(stats);
return (stats[0] - stats[2]);
#else
return -1.;
#endif
}
double nrn_netcon_get_thresh(NetCon* nc) {
if (nc->src_) {
return nc->src_->threshold_;
} else {
return 0;
}
}
void nrn_netcon_set_thresh(NetCon* nc, double th) {
if (nc->src_) {
nc->src_->threshold_ = th;
}
}
void nrn_netcon_event(NetCon* nc, double td) {
nc->chktar();
net_cvode_instance->event(td, nc, PP2NT(nc->target_));
}
Point_process* nrn_netcon_target(NetCon* nc) {
nc->chktar();
return nc->target_;
}
int nrn_netcon_info(NetCon* nc, double** pw, Point_process** target, double** th, double** del) {
*target = (nc->target_) ? nc->target_ : (Point_process*) 0;
*th = (nc->src_) ? &(nc->src_->threshold_) : (double*) 0;
*del = &nc->delay_;
*pw = nc->weight_;
return nc->cnt_;
}
int nrn_presyn_count(PreSyn* ps) {
return ps->dil_.size();
}
void* nrn_presyn_netcon(PreSyn* ps, int i) {
return ps->dil_[i];
}
#if USENEOSIM
void neosim2nrn_advance(void*, void*, double);
void neosim2nrn_deliver(void*, void*);
void (*p_nrn2neosim_send)(void*, double);
static void* neosim_entity_;
#endif
void ncs2nrn_integrate(double tstop);
extern void (*nrn_allthread_handle)();
static void allthread_handle_callback() {
net_cvode_instance->allthread_handle();
}
#if USENCS
// As a subroutine for NCS, NEURON simulates the cells of a subnet
// (usually a single cell) and NCS manages whatever intercellular
// NetCon connections were specified by the hoc cvode method
// Cvode.ncs_netcons(List nc_inlist, List nc_outlist) . The former list
// is normally in one to one correspondence with all the synapses. These
// NetCon objects have those synapses as targets and nullptr sources.
// The latter list is normally in one to one correspondence with
// the cells of the subnet. Those NetCon objects have sources of the form
// cell.axon.v(1) and nullptr targets.
// Note that the program that creates the hoc file knows
// how to tell NCS that a particular integer corresponds to a particular
// NetCon
// NCS tells NEURON what time to integrate to. (Using any integration method)
// Probably the step size will be the minimum delay between output spike
// and its effect on any synapse.
// void ncs2nrn_integrate(double tstop);
// NCS gives synaptic events to NEURON but tdeliver must be >= t
void ncs2nrn_inputevent(int netcon_input_index, double tdeliver);
// and NEURON notifies NCS if cell ouputs a spike during integrate
extern void nrn2ncs_outputevent(int netcon_output_index, double firetime);
// netcon_input_index specifies the NetCon object
// in the following list. The hoc file sets this up via cvode.ncs_netcons
static NetConPList* ncs2nrn_input_;
// the netcon_ouput_index is specified in a NetCon field called
// nrn2ncs_output_index_
// helper functions
void nrn2ncs_netcons();
#endif
#if NRNMPI
extern void nrn2ncs_outputevent(int netcon_output_index, double firetime);
#endif
#if NRNMPI
extern void nrn_multisend_send(PreSyn*, double t);
extern bool use_multisend_;
extern void nrn_multisend_advance();
#endif
bool nrn_use_fifo_queue_;
bool nrn_use_bin_queue_;
#if NRNMPI
// for compressed info during spike exchange
extern bool nrn_use_localgid_;
extern void nrn_outputevent(unsigned char, double);
#endif
struct ForNetConsInfo {
double** argslist;
int size;
};
static unsigned long deliver_cnt_, net_event_cnt_;
unsigned long DiscreteEvent::discretevent_send_;
unsigned long DiscreteEvent::discretevent_deliver_;
unsigned long NetCon::netcon_send_active_;
unsigned long NetCon::netcon_send_inactive_;
unsigned long NetCon::netcon_deliver_;
unsigned long SelfEvent::selfevent_send_;
unsigned long SelfEvent::selfevent_move_;
unsigned long SelfEvent::selfevent_deliver_;
unsigned long WatchCondition::watch_send_;
unsigned long WatchCondition::watch_deliver_;
unsigned long ConditionEvent::init_above_;
unsigned long ConditionEvent::send_qthresh_;
unsigned long ConditionEvent::deliver_qthresh_;
unsigned long ConditionEvent::abandon_;
unsigned long ConditionEvent::eq_abandon_;
unsigned long ConditionEvent::abandon_init_above_;
unsigned long ConditionEvent::abandon_init_below_;
unsigned long ConditionEvent::abandon_above_;
unsigned long ConditionEvent::abandon_below_;
unsigned long PreSyn::presyn_send_mindelay_;
unsigned long PreSyn::presyn_send_direct_;
unsigned long PreSyn::presyn_deliver_netcon_;
unsigned long PreSyn::presyn_deliver_direct_;
unsigned long PreSyn::presyn_deliver_ncsend_;
unsigned long PlayRecordEvent::playrecord_send_;
unsigned long PlayRecordEvent::playrecord_deliver_;
unsigned long HocEvent::hocevent_send_;
unsigned long HocEvent::hocevent_deliver_;
unsigned long KSSingle::singleevent_deliver_;
unsigned long KSSingle::singleevent_move_;
TQueue* net_cvode_instance_event_queue(NrnThread* nt) {
return net_cvode_instance->event_queue(nt);
}
hoc_Item* net_cvode_instance_psl() {
return net_cvode_instance->psl_;
}
std::vector<PlayRecord*>* net_cvode_instance_prl() {
return net_cvode_instance->playrec_list();
}
void nrn_use_daspk(int b) {
if (net_cvode_instance) {
net_cvode_instance->use_daspk(b == 1);
}
}
double NetCvode::eps_;
static Node* node(Object*);
Node* node(Object* ob) {
return ob2pntproc(ob)->node;
}
PlayRecordEvent::PlayRecordEvent() {}
PlayRecordEvent::~PlayRecordEvent() {}
DiscreteEvent* PlayRecordEvent::savestate_save() {
PlayRecordEvent* pre = new PlayRecordEvent();
pre->plr_ = plr_;
return pre;
}
void PlayRecordEvent::savestate_restore(double tt, NetCvode* nc) {
nc->event(tt, plr_->event(), nrn_threads + plr_->ith_);
}
void PlayRecordEvent::savestate_write(FILE* f) {
fprintf(f, "%d\n", PlayRecordEventType);
fprintf(f, "%d %d\n", plr_->type(), net_cvode_instance->playrec_item(plr_));
}
DiscreteEvent* PlayRecordEvent::savestate_read(FILE* f) {
char buf[100];
int type, plr_index;
nrn_assert(fgets(buf, 100, f));
sscanf(buf, "%d %d\n", &type, &plr_index);
PlayRecord* plr = net_cvode_instance->playrec_item(plr_index);
assert(plr && plr->type() == type);
return plr->event()->savestate_save();
}
PlayRecordSave* PlayRecord::savestate_save() {
return new PlayRecordSave(this);
}
PlayRecordSave* PlayRecord::savestate_read(FILE* f) {
PlayRecordSave* prs = nullptr;
int type, index;
char buf[100];
nrn_assert(fgets(buf, 100, f));
nrn_assert(sscanf(buf, "%d %d\n", &type, &index) == 2);
PlayRecord* plr = net_cvode_instance->playrec_item(index);
assert(plr->type() == type);
switch (type) {
case VecRecordDiscreteType:
prs = new VecRecordDiscreteSave(plr);
break;
case VecRecordDtType:
prs = new VecRecordDtSave(plr);
break;
case VecPlayStepType:
prs = new VecPlayStepSave(plr);
break;
case VecPlayContinuousType:
prs = new VecPlayContinuousSave(plr);
break;
default:
// whenever there is no subclass specific data to save
prs = new PlayRecordSave(plr);
break;
}
prs->savestate_read(f);
return prs;
}
PlayRecordSave::PlayRecordSave(PlayRecord* plr) {
pr_ = plr;
prl_index_ = net_cvode_instance->playrec_item(pr_);
assert(prl_index_ >= 0);
}
PlayRecordSave::~PlayRecordSave() {}
void PlayRecordSave::check() {
assert(pr_ == net_cvode_instance->playrec_item(prl_index_));
}
void NetCon::chksrc() {
if (!src_) {
hoc_execerror(hoc_object_name(obj_), "source is missing");
}
}
void NetCon::chktar() {
if (!target_) {
hoc_execerror(hoc_object_name(obj_), "target is missing");
}
}
void NetCon::disconnect(Observable* o) {
Object* ob = ((ObjObservable*) o)->object();
// printf("%s disconnect from ", hoc_object_name(obj_));
if (target_->ob == ob) {
// printf("target %s\n", hoc_object_name(target_->ob));
target_ = nullptr;
active_ = 0;
}
}
#if 0 // way of printing dinfo
printf("NetCon from %s to ",
d->src_->osrc_ ? hoc_object_name(d->src_->osrc_) : secname(d->src_->ssrc_));
printf("%s ", hoc_object_name(d->target_->ob));
printf(" weight index=%d\n", l);
#endif
class MaxStateItem {
public:
Symbol* sym_;
double max_;
double amax_;
};
struct InterThreadEvent {
DiscreteEvent* de_;
double t_;
};
typedef std::vector<WatchCondition*> WatchList;
using SelfEventPool = MutexPool<SelfEvent>;
typedef std::vector<TQItem*> TQList;
// allows marshalling of all items in the event queue that need to be
// removed to avoid duplicates due to frecord_init after finitialize
static TQList* record_init_items_;
static DiscreteEvent* null_event_;
static PreSyn* unused_presyn; // holds the NetCons with no source
static double nc_preloc(void* v) { // user must pop section stack after call
NetCon* d = (NetCon*) v;
Section* s = nullptr;
if (d->src_) {
s = d->src_->ssrc_;
}
if (s) {
nrn_pushsec(s);
// This is a special handle, not just a pointer.
auto const& v = d->src_->thvar_;
nrn_parent_info(s); // make sure parentnode exists
// there is no efficient search for the location of
// an arbitrary variable. Search only for v at 0 - 1.
// Otherwise return .5 .
if (v == s->parentnode->v_handle()) {
return nrn_arc_position(s, s->parentnode);
}
for (int i = 0; i < s->nnode; ++i) {
if (v == s->pnode[i]->v_handle()) {
return nrn_arc_position(s, s->pnode[i]);
}
}
return -2.; // not voltage
} else {
return -1.;
}
}
static Object** nc_preseg(void* v) { // user must pop section stack after call
NetCon* d = (NetCon*) v;
Section* s = NULL;
Object* obj = NULL;
double x = -1.;
if (d->src_) {
s = d->src_->ssrc_;
}
if (s && nrnpy_seg_from_sec_x) {
auto const& v = d->src_->thvar_;
nrn_parent_info(s); // make sure parentnode exists
// there is no efficient search for the location of
// an arbitrary variable. Search only for v at 0 - 1.
// Otherwise return NULL.
if (v == s->parentnode->v_handle()) {
x = nrn_arc_position(s, s->parentnode);
}
for (int i = 0; i < s->nnode; ++i) {
if (v == s->pnode[i]->v_handle()) {
x = nrn_arc_position(s, s->pnode[i]);
continue;
}
}
// perhaps should search for v
if (x >= 0) {
obj = (*nrnpy_seg_from_sec_x)(s, x);
--obj->refcount;
}
}
return hoc_temp_objptr(obj);
}
static double nc_postloc(void* v) { // user must pop section stack after call
NetCon* d = (NetCon*) v;
if (d->target_ && d->target_->sec) {
nrn_pushsec(d->target_->sec);
return nrn_arc_position(d->target_->sec, d->target_->node);
} else {
return -1.;
}
}
static Object** nc_postseg(void* v) { // user must pop section stack after call
NetCon* d = (NetCon*) v;
Object* obj = NULL;
if (d->target_ && d->target_->sec && nrnpy_seg_from_sec_x) {
double x = nrn_arc_position(d->target_->sec, d->target_->node);
obj = (*nrnpy_seg_from_sec_x)(d->target_->sec, x);
--obj->refcount;
}
return hoc_temp_objptr(obj);
}
static Object** nc_syn(void* v) {
NetCon* d = (NetCon*) v;
Object* ob = nullptr;
if (d->target_) {
ob = d->target_->ob;
}
return hoc_temp_objptr(ob);
}
static Object** nc_pre(void* v) {
NetCon* d = (NetCon*) v;
Object* ob = nullptr;
if (d->src_) {
ob = d->src_->osrc_;
}
return hoc_temp_objptr(ob);
}
static Object** newoclist(int i, OcList*& o) {
Object** po;
if (ifarg(i) && hoc_is_object_arg(i)) {
po = hoc_objgetarg(i);
check_obj_type(*po, "List");
o = (OcList*) ((*po)->u.this_pointer);
} else {
o = new OcList();
o->ref();
Symbol* sl = hoc_lookup("List");
po = hoc_temp_objvar(sl, o);
}
return po;
}
static Object** nc_prelist(void* v) {
NetCon* d = (NetCon*) v;
OcList* o;
Object** po = newoclist(1, o);
if (d->src_) {
for (const auto& nc: d->src_->dil_) {
if (nc->obj_) {
o->append(nc->obj_);
}
}
}
return po;
}
static Object** nc_synlist(void* v) {
NetCon* d = (NetCon*) v;
OcList* o;
Object** po = newoclist(1, o);
hoc_Item* q;
if (net_cvode_instance->psl_)
ITERATE(q, net_cvode_instance->psl_) {
PreSyn* ps = (PreSyn*) VOIDITM(q);
for (const auto& nc: ps->dil_) {
if (nc->obj_ && nc->target_ == d->target_) {
o->append(nc->obj_);
}
}
}
return po;
}
static Object** nc_postcelllist(void* v) {
NetCon* d = (NetCon*) v;
OcList* o;
Object** po = newoclist(1, o);
hoc_Item* q;
Object* cell = nullptr;
if (d->target_ && d->target_->sec) {
cell = nrn_sec2cell(d->target_->sec);
}
if (cell && net_cvode_instance->psl_)
ITERATE(q, net_cvode_instance->psl_) {
PreSyn* ps = (PreSyn*) VOIDITM(q);
for (const auto& nc: ps->dil_) {
if (nc->obj_ && nc->target_ && nrn_sec2cell_equals(nc->target_->sec, cell)) {
o->append(nc->obj_);
}
}
}
return po;
}
static Object** nc_precelllist(void* v) {
NetCon* d = (NetCon*) v;
OcList* o;
Object** po = newoclist(1, o);
hoc_Item* q;
Object* cell = nullptr;
if (d->src_ && d->src_->ssrc_) {
cell = nrn_sec2cell(d->src_->ssrc_);
}
if (cell && net_cvode_instance->psl_)
ITERATE(q, net_cvode_instance->psl_) {
PreSyn* ps = (PreSyn*) VOIDITM(q);
for (const auto& nc: ps->dil_) {
if (nc->obj_ && nc->src_ && ps->ssrc_ && nrn_sec2cell_equals(ps->ssrc_, cell)) {
o->append(nc->obj_);
}
}
}
return po;
}
static Object** nc_precell(void* v) {
NetCon* d = (NetCon*) v;
if (d->src_ && d->src_->ssrc_) {
return hoc_temp_objptr(nrn_sec2cell(d->src_->ssrc_));
} else {
return hoc_temp_objptr(0);
}
}
static Object** nc_postcell(void* v) {
NetCon* d = (NetCon*) v;
Object* ob = nullptr;
if (d->target_ && d->target_->sec) {
ob = nrn_sec2cell(d->target_->sec);
}
return hoc_temp_objptr(ob);
}
static double nc_setpost(void* v) {
NetCon* d = (NetCon*) v;
Object* otar = nullptr;
if (ifarg(1)) {
otar = *hoc_objgetarg(1);
}
if (otar && !is_point_process(otar)) {
hoc_execerror("argument must be a point process or NULLobject", 0);
}
Point_process* tar = nullptr;
if (otar) {
tar = ob2pntproc(otar);
}
if (d->target_ && d->target_ != tar) {
#if DISCRETE_EVENT_OBSERVER
ObjObservable::Detach(d->target_->ob, d);
#endif
d->target_ = nullptr;
}
int cnt = 1;
if (tar) {
cnt = pnt_receive_size[tar->prop->_type];
d->target_ = tar;
#if DISCRETE_EVENT_OBSERVER
ObjObservable::Attach(otar, d);
#endif
} else {
d->active_ = false;
}
if (d->cnt_ != cnt) {
d->cnt_ = cnt;
delete[] std::exchange(d->weight_, new double[d->cnt_]);
}
return 0.;
}
static double nc_valid(void* v) {
NetCon* d = (NetCon*) v;
hoc_return_type_code = 2;
if (d->src_ && d->target_) {
return 1.;
}
return 0.;
}
static double nc_active(void* v) {
NetCon* d = (NetCon*) v;
bool a = d->active_;
if (d->target_ && ifarg(1)) {
d->active_ = bool(chkarg(1, 0, 1));
}
hoc_return_type_code = 2;
return double(a);
}
// for threads, revised net_send to use absolute time (in the
// mod file we add the thread time when we call it).
// And we can no longer check with respect to minimum time in chkarg
static double nc_event(void* v) {
NetCon* d = (NetCon*) v;
double td = chkarg(1, -1e20, 1e20);
if (d->active_ == 0) {
return 0.0;
}
d->chktar();
NrnThread* nt = PP2NT(d->target_);
const auto nrn_thread_not_initialized_for_nc_target = nt && nt >= nrn_threads &&
nt < (nrn_threads + nrn_nthread);
nrn_assert(nrn_thread_not_initialized_for_nc_target);
if (ifarg(2)) {
double flag = *getarg(2);
Point_process* pnt = d->target_;
int type = pnt->prop->_type;
if (!nrn_is_artificial_[type]) {
hoc_execerror("Can only send fake self-events to ARTIFICIAL_CELLs", 0);
}
auto* pq = pnt->prop->dparam + nrn_artcell_qindex_[type];
nrn_net_send(pq, d->weight_, pnt, td, flag);
} else {
net_cvode_instance->event(td, d, PP2NT(d->target_));
}
return (double) d->active_;
}
static double nc_record(void* v) {
NetCon* d = (NetCon*) v;
d->chksrc();
if (ifarg(1)) {
if (ifarg(2)) {
int recid = d->obj_->index;
if (ifarg(3)) {
recid = (int) (*getarg(3));
}
d->src_->record(vector_arg(1), vector_arg(2), recid);
} else if (hoc_is_str_arg(1)) {
d->src_->record_stmt(gargstr(1));
} else if (is_vector_arg(1)) {
d->src_->record(vector_arg(1));
} else {
d->src_->record_stmt(*hoc_objgetarg(1));
}
} else {
d->src_->record(nullptr);
}
return 0;
}
static double nc_srcgid(void* v) {
NetCon* d = (NetCon*) v;
hoc_return_type_code = 1;
if (d->src_) {
return (double) d->src_->gid_;
}
return -1.;
}
static Object** nc_get_recordvec(void* v) {
NetCon* d = (NetCon*) v;
Object* ob = nullptr;
if (d->src_ && d->src_->tvec_) {
ob = d->src_->tvec_->obj_;
}
return hoc_temp_objptr(ob);
}
static double nc_wcnt(void* v) {
NetCon* d = (NetCon*) v;
return d->cnt_;
}
static Member_func members[] = {{"active", nc_active},
{"valid", nc_valid},
{"preloc", nc_preloc},
{"postloc", nc_postloc},
{"setpost", nc_setpost},
{"event", nc_event},
{"record", nc_record},
{"srcgid", nc_srcgid},
{"wcnt", nc_wcnt},
{"delay", 0}, // these four changed below
{"weight", 0},
{"threshold", 0},
{"x", 0},
{0, 0}};
static Member_ret_obj_func omembers[] = {{"syn", nc_syn},
{"pre", nc_pre},
{"precell", nc_precell},
{"postcell", nc_postcell},
{"preseg", nc_preseg},
{"postseg", nc_postseg},
{"prelist", nc_prelist},
{"synlist", nc_synlist},
{"precelllist", nc_precelllist},
{"postcelllist", nc_postcelllist},
{"get_recordvec", nc_get_recordvec},
{0, 0}};
static void steer_val(void* v) {
NetCon* d = (NetCon*) v;
Symbol* s = hoc_spop();
if (strcmp(s->name, "delay") == 0) {
d->chksrc();
hoc_pushpx(&d->delay_);
d->src_->use_min_delay_ = 0;
} else if (strcmp(s->name, "weight") == 0) {
int index = 0;
if (hoc_stack_type_is_ndim()) {
s->arayinfo->sub[0] = d->cnt_;
index = hoc_araypt(s, SYMBOL);
}
hoc_pushpx(d->weight_ + index);
} else if (strcmp(s->name, "x") == 0) {
static double dummy = 0.;
d->chksrc();
if (d->src_->thvar_) {
hoc_push(d->src_->thvar_);
} else {
dummy = 0.;
hoc_pushpx(&dummy);
}
} else if (strcmp(s->name, "threshold") == 0) {
d->chksrc();
hoc_pushpx(&d->src_->threshold_);
}
}
static void* cons(Object* o) {
NetCon* d;
if (!net_cvode_instance) {
hoc_execerror("CVode instance must exist", 0);
}
// source, target, threshold, delay, magnitude
Object *osrc = nullptr, *otar;
Section* srcsec = nullptr;
neuron::container::data_handle<double> psrc{};
if (hoc_is_object_arg(1)) {
osrc = *hoc_objgetarg(1);
if (osrc && !is_point_process(osrc)) {
hoc_execerror("if arg 1 is an object it must be a point process or NULLObject", 0);
}
} else {
psrc = hoc_hgetarg<double>(1);
srcsec = chk_access();
}
otar = *hoc_objgetarg(2);
if (otar && !is_point_process(otar)) {
hoc_execerror("arg 2 must be a point process or NULLobject", 0);
}
double thresh = -1.e9; // sentinal value. default is 10 if new PreSyn
double delay = 1.;
double weight = 0.;
if (ifarg(3)) {
thresh = *getarg(3);
delay = chkarg(4, 0, 1e15);
weight = *getarg(5);
}
d = net_cvode_instance->install_deliver(psrc, srcsec, osrc, otar, thresh, delay, weight);
d->obj_ = o;
return (void*) d;
}
static void destruct(void* v) {
delete static_cast<NetCon*>(v);
}
void NetCon_reg() {
class2oc("NetCon", cons, destruct, members, NULL, omembers, NULL);
Symbol* nc = hoc_lookup("NetCon");
nc->u.ctemplate->steer = steer_val;
Symbol* s;
s = hoc_table_lookup("delay", nc->u.ctemplate->symtable);
s->type = VAR;
s->arayinfo = nullptr;
s = hoc_table_lookup("x", nc->u.ctemplate->symtable);
s->type = VAR;
s->arayinfo = nullptr;
s = hoc_table_lookup("threshold", nc->u.ctemplate->symtable);
s->type = VAR;
s->arayinfo = nullptr;
s = hoc_table_lookup("weight", nc->u.ctemplate->symtable);
s->type = VAR;
s->arayinfo = new Arrayinfo;
s->arayinfo->refcount = 1;
s->arayinfo->a_varn = nullptr;
s->arayinfo->nsub = 1;
s->arayinfo->sub[0] = 1;
}
static char* escape_bracket(const char* s) {
static char* b;
const char* p1;
char* p2;
if (!b) {
b = new char[256];
}
for (p1 = s, p2 = b; *p1; ++p1, ++p2) {
switch (*p1) {
case '<':
*p2 = '[';
break;
case '>':
*p2 = ']';
break;
case '[':
case ']':
*p2 = '\\';
*(++p2) = *p1;
break;
default:
*p2 = *p1;
break;
}
}
*p2 = '\0';
return b;
}
Object** NetCvode::netconlist() {
// interface to cvode.netconlist(precell, postcell, target, [list])
OcList* o;
Object** po = newoclist(4, o);
Object *opre = nullptr, *opost = nullptr, *otar = nullptr;
std::regex spre, spost, star;
if (hoc_is_object_arg(1)) {
opre = *hoc_objgetarg(1);
} else {
std::string s(gargstr(1));
if (s.empty()) {
spre = std::regex(".*");
} else {
try {
spre = std::regex(escape_bracket(s.data()));
} catch (std::regex_error&) {
hoc_execerror(gargstr(1), "not a valid regular expression");
}
}
}
if (hoc_is_object_arg(2)) {
opost = *hoc_objgetarg(2);
} else {
std::string s(gargstr(2));
if (s.empty()) {
spost = std::regex(".*");
} else {
try {
spost = std::regex(escape_bracket(s.data()));
} catch (std::regex_error&) {
hoc_execerror(gargstr(2), "not a valid regular expression");
}
}
}
if (hoc_is_object_arg(3)) {
otar = *hoc_objgetarg(3);
} else {
std::string s(gargstr(3));
if (s.empty()) {
star = std::regex(".*");
} else {
try {
star = std::regex(escape_bracket(s.data()));
} catch (std::regex_error&) {
hoc_execerror(gargstr(3), "not a valid regular expression");
}
}
}
hoc_Item* q;
if (psl_) {
ITERATE(q, psl_) {
PreSyn* ps = (PreSyn*) VOIDITM(q);
bool b = false;
if (ps->ssrc_) {
Object* precell = nrn_sec2cell(ps->ssrc_);
if (opre) {
b = precell == opre;
} else {
std::string s(hoc_object_name(precell));
b = std::regex_search(s, spre);
}
} else if (ps->osrc_) {
Object* presyn = ps->osrc_;
if (opre) {
b = presyn == opre;
} else {
std::string s(hoc_object_name(presyn));
b = std::regex_search(s, spre);
}
}
if (b) {
for (const auto& d: ps->dil_) {
Object* postcell = nullptr;