/
cabcode.cpp
2275 lines (2109 loc) · 62 KB
/
cabcode.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>
/* /local/src/master/nrn/src/nrnoc/cabcode.cpp,v 1.37 1999/07/08 14:24:59 hines Exp */
#define HOC_L_LIST 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "section.h"
#include "nrn_ansi.h"
#include "nrniv_mf.h"
#include "membfunc.h"
#include "parse.hpp"
#include "hocparse.h"
#include "membdef.h"
extern int hoc_execerror_messages;
#define symlist hoc_symlist
int tree_changed = 1; /* installing section, changeing nseg
and connecting sections set this flag.
The flag is set to 0 when the topology
is set up */
int diam_changed = 1; /* changing diameter, length set this flag
The flag is set to 0 when node.a and node.b
is set up */
extern int nrn_shape_changed_;
char* (*nrnpy_pysec_name_p_)(Section*);
Object* (*nrnpy_pysec_cell_p_)(Section*);
int (*nrnpy_pysec_cell_equals_p_)(Section*, Object*);
/* switching from Ra being a global variable to it being a section variable
opens up the possibility of a great deal of confusion and inadvertant wrong
results. To help avoid this we print a warning message whenever the value
in one section is set but no others. But only the first time through treeset.
*/
#if RA_WARNING
int nrn_ra_set = 0;
#endif
#define NSECSTACK 200
static Section* secstack[NSECSTACK + 1];
static int isecstack = 0; /* stack index */
/* don't do section stack auto correction
in the interval between push_section() ... pop_section in hoc (also for
point process get_loc. These should be deprecated in favor of doing
everything with SectionRef
*/
static int skip_secstack_check = 0;
static int range_vec_indx(Symbol* s);
int nrn_isecstack(void) {
return isecstack;
}
void nrn_secstack(int i) {
if (skip_secstack_check) {
return;
}
#if 1
if (isecstack > i) {
Printf("The sectionstack index should be %d but it is %d\n", i, isecstack);
hoc_warning(
"prior to version 5.3 the section stack would not have been properly popped\n\
and the currently accessed section would have been ",
secname(secstack[isecstack]));
}
#endif
while (isecstack > i) {
nrn_popsec();
}
}
void nrn_initcode(void) {
while (isecstack > 0) {
nrn_popsec();
}
isecstack = 0;
section_object_seen = 0;
state_discon_allowed_ = 1;
skip_secstack_check = 0;
}
void oc_save_cabcode(int* a1, int* a2) {
*a1 = isecstack;
*a2 = section_object_seen;
}
void oc_restore_cabcode(int* a1, int* a2) {
while (isecstack > *a1) {
nrn_popsec();
}
isecstack = *a1;
section_object_seen = *a2;
}
void nrn_pushsec(Section* sec) {
isecstack++;
if (isecstack >= NSECSTACK) {
int i = NSECSTACK;
hoc_warning("section stack overflow", (char*) 0);
while (--i >= 0) {
fprintf(stderr, "%*s%s\n", i, "", secname(secstack[i]));
--i;
}
hoc_execerror("section stack overflow", (char*) 0);
}
secstack[isecstack] = sec;
if (sec) {
#if 0
if (sec->prop && sec->prop->dparam[0].sym) {
printf("pushsec %s\n", sec->prop->dparam[0].sym->name);
}else{
printf("pushsec unnamed or deleted section\n");
}
#endif
++sec->refcount;
}
}
void nrn_popsec(void) {
if (isecstack > 0) {
Section* sec = secstack[isecstack--];
if (!sec) {
return;
}
section_unref(sec);
}
}
void sec_access_pop(void) {
nrn_popsec();
}
#if 0
static void free_point_process(void) {
free_all_point();
free_clamp();
free_stim();
free_syn();
}
#endif
#if 0
void clear_sectionlist(void) /* merely change all SECTION to UNDEF */
{
printf("clear_sectionlist not fixed yet, doing nothing\n");
return;
Symbol *s;
free_point_process();
if (symlist) for (s=symlist->first; s; s = s->next) {
if (s->type == SECTION) {
s->type = UNDEF;
no longer done this way see OPARINFO
if (s->arayinfo) {
free((char *)s->arayinfo);
s->arayinfo = (Arrayinfo *)0;
}
}
nrn_initcode();
secstack[0] = (Section *)0;
}
if (section) {
sec_free(section, nsection);
section = (Section *)0;
nsection = 0;
}
}
#endif
void add_section(void) /* symbol at pc+1, number of indices at pc+2 */
{
Symbol* sym;
int nsub, size;
Item** pitm;
sym = (pc++)->sym;
/*printf("declaring %s as section\n", sym->name);*/
if (sym->type == SECTION) {
int total, i;
total = hoc_total_array(sym);
for (i = 0; i < total; ++i) {
sec_free(*(OPSECITM(sym) + i));
}
free((char*) OPSECITM(sym));
hoc_freearay(sym);
} else {
assert(sym->type == UNDEF);
if (hoc_objectdata != hoc_top_level_data && hoc_thisobject) {
hoc_execerr_ext(
"First time declaration of Section %s in %s "
"must happen at command level (not in method)",
sym->name,
hoc_object_name(hoc_thisobject));
}
sym->type = SECTION;
hoc_install_object_data_index(sym);
}
nsub = (pc++)->i;
if (nsub) {
size = hoc_arayinfo_install(sym, nsub);
} else {
size = 1;
}
hoc_objectdata[sym->u.oboff].psecitm = pitm = (Item**) emalloc(size * sizeof(Item*));
if (hoc_objectdata == hoc_top_level_data) {
new_sections((Object*) 0, sym, pitm, size);
} else {
new_sections(hoc_thisobject, sym, pitm, size);
}
#if 0
printf("%s", s->name);
for (i=0; i<ndim; i++) {printf("[%d]",s->arayinfo->sub[i]);}
printf(" is a section name\n");
#endif
}
Object* nrn_sec2cell(Section* sec) {
if (sec->prop) {
if (auto* o = sec->prop->dparam[6].get<Object*>(); o) {
return o;
} else if (nrnpy_pysec_cell_p_) {
Object* o = (*nrnpy_pysec_cell_p_)(sec);
if (o) {
--o->refcount;
}
return o;
}
}
return nullptr;
}
int nrn_sec2cell_equals(Section* sec, Object* obj) {
if (sec && sec->prop) {
if (auto o = sec->prop->dparam[6].get<Object*>(); o) {
return o == obj;
} else if (nrnpy_pysec_cell_equals_p_) {
return (*nrnpy_pysec_cell_equals_p_)(sec, obj);
}
}
return 0;
}
static Section* new_section(Object* ob, Symbol* sym, int i) {
Section* sec;
Prop* prop;
static Symbol* nseg;
double d;
if (!nseg) {
nseg = hoc_lookup("nseg");
}
sec = sec_alloc();
section_ref(sec);
prop = prop_alloc(&(sec->prop), CABLESECTION, (Node*) 0);
prop->dparam[0] = {neuron::container::do_not_search, sym};
prop->dparam[5] = i;
prop->dparam[6] = {neuron::container::do_not_search, ob};
#if USE_PYTHON
prop->dparam[PROP_PY_INDEX] = nullptr;
#endif
nrn_pushsec(sec);
d = (double) DEF_nseg;
cable_prop_assign(nseg, &d, 0);
tree_changed = 1;
/*printf("new_section %s\n", secname(sec));*/
return sec;
}
void new_sections(Object* ob, Symbol* sym, Item** pitm, int size) {
int i;
for (i = 0; i < size; ++i) {
Section* sec = new_section(ob, sym, i);
if (ob) {
if (ob->secelm_) {
pitm[i] = insertsec(ob->secelm_->next, sec);
} else {
pitm[i] = lappendsec(section_list, sec);
}
ob->secelm_ = pitm[i];
} else {
pitm[i] = lappendsec(section_list, sec);
}
sec->prop->dparam[8] = {neuron::container::do_not_search, pitm[i]};
}
}
#if USE_PYTHON
struct NPySecObj;
Section* nrnpy_newsection(NPySecObj* v) {
Item* itm;
Section* sec;
sec = new_section((Object*) 0, (Symbol*) 0, 0);
#if USE_PYTHON
sec->prop->dparam[PROP_PY_INDEX] = static_cast<void*>(v);
#endif
itm = lappendsec(section_list, sec);
sec->prop->dparam[8] = itm;
return sec;
}
#endif
void delete_section(void) {
Section* sec;
Item** pitm;
if (ifarg(1)) {
hoc_execerror(
"delete_section takes no positional arguments and deletes the HOC currently accessed "
"section. If using Python, did you mean a named arg of the form, sec=section?",
NULL);
}
sec = chk_access();
if (!sec->prop) { /* already deleted */
hoc_retpushx(0.0);
return;
}
#if USE_PYTHON
if (sec->prop->dparam[PROP_PY_INDEX].get<void*>()) { /* Python Section */
/* the Python Section will be a zombie section with a pointer
to an invalid Section*.
*/
sec->prop->dparam[PROP_PY_INDEX] = nullptr;
section_ref(sec);
sec_free(sec->prop->dparam[8].get<hoc_Item*>());
hoc_retpushx(0.0);
return;
}
#endif
auto* sym = sec->prop->dparam[0].get<Symbol*>();
if (!sym) {
hoc_execerror("Cannot delete an unnamed hoc section", (char*) 0);
}
auto* ob = sec->prop->dparam[6].get<Object*>();
auto i = sec->prop->dparam[5].get<int>();
if (ob) {
pitm = ob->u.dataspace[sym->u.oboff].psecitm + i;
} else {
pitm = hoc_top_level_data[sym->u.oboff].psecitm + i;
}
sec_free(*pitm);
*pitm = 0;
hoc_retpushx(1.);
}
/*
At this point, all the sections are cables and each section has the following
properties (except for section 0). Only one property with 9 Datums
section[i].prop->dparam[0].sym pointer to section symbol
[1].val position (0--1) of connection to parent
[2].val length of section in microns
[3].val first node at position 0 or 1
[4].val rall branch
[5].i aray index
[6].obj pointer to the object pointer
[7].val Ra
[8].itm hoc_Item* with Section* as element
[9]._pvoid NrnThread*
[PROP_PY_INDEX].pvoid pointer to the Python Section object
The first element allows us to find the symbol when we know the section number.
If an array section the index can be computed with
i - (section[i].sym)->u.u_auto
*/
double section_length(Section* sec) {
if (sec->recalc_area_ && sec->npt3d) {
sec->prop->dparam[2] = sec->pt3d[sec->npt3d - 1].arc;
}
double x = sec->prop->dparam[2].get<double>();
if (x <= 1e-9) {
x = 1e-9;
sec->prop->dparam[2] = x;
}
return x;
}
int arc0at0(Section* sec) {
return (sec->prop->dparam[3].get<double>() ? 0 : 1);
}
double nrn_ra(Section* sec) {
return sec->prop->dparam[7].get<double>();
}
void cab_alloc(Prop* p) {
Datum* pd;
#if USE_PYTHON
#define CAB_SIZE 11
#else
#define CAB_SIZE 10
#endif
pd = nrn_prop_datum_alloc(CABLESECTION, CAB_SIZE, p);
pd[1] = 0.0;
pd[2] = DEF_L;
pd[3] = 0.0;
pd[4] = DEF_rallbranch;
pd[7] = DEF_Ra;
p->dparam = pd;
p->dparam_size = CAB_SIZE; /* this one is special since it refers to dparam */
}
void morph_alloc(Prop* p) {
assert(p->param_size() == 1);
p->param(0) = DEF_diam; /* microns */
diam_changed = 1;
}
double nrn_diameter(Node* nd) {
Prop* p = nrn_mechanism(MORPHOLOGY, nd);
return p->param(0);
}
void nrn_chk_section(Symbol* s) {
if (s->type != SECTION) {
execerror("Not a SECTION name:", s->name);
}
}
Section* chk_access() {
Section* sec = secstack[isecstack];
if (!sec || !sec->prop) {
/* use any existing section as a default section */
hoc_Item* qsec;
// ForAllSections(lsec)
ITERATE(qsec, section_list) {
Section* lsec = hocSEC(qsec);
if (lsec->prop) {
sec = lsec;
++sec->refcount;
secstack[isecstack] = sec;
/*printf("automatic default section %s\n", secname(sec));*/
break;
}
}
}
if (!sec) {
execerror("Section access unspecified", (char*) 0);
}
if (sec->prop) {
return sec;
} else {
execerror("Accessing a deleted section", (char*) 0);
}
return NULL; /* never reached */
}
Section* nrn_noerr_access(void) /* return 0 if no accessed section */
{
Section* sec = secstack[isecstack];
if (!sec || !sec->prop) {
/* use any existing section as a default section */
hoc_Item* qsec;
// ForAllSections(lsec)
ITERATE(qsec, section_list) {
Section* lsec = hocSEC(qsec);
if (lsec->prop) {
sec = lsec;
++sec->refcount;
secstack[isecstack] = sec;
/*printf("automatic default section %s\n", secname(sec));*/
break;
}
}
}
if (!sec) {
return (Section*) 0;
}
if (sec->prop) {
return sec;
} else {
return (Section*) 0;
}
}
/*sibling and child pointers do not ref sections to avoid mutual references */
/* the sibling list is ordered according to increasing distance from parent */
void nrn_remove_sibling_list(Section* sec) {
Section* s;
if (sec->parentsec) {
if (sec->parentsec->child == sec) {
sec->parentsec->child = sec->sibling;
return;
}
for (s = sec->parentsec->child; s; s = s->sibling) {
if (s->sibling == sec) {
s->sibling = sec->sibling;
return;
}
}
}
}
static double ncp_abs(Section* sec) {
double x = nrn_connection_position(sec);
if (sec->parentsec) {
if (!arc0at0(sec->parentsec)) {
return 1. - x;
}
}
return x;
}
void nrn_add_sibling_list(Section* sec) {
Section* s;
double x;
if (sec->parentsec) {
x = ncp_abs(sec);
s = sec->parentsec->child;
if (!s || x <= ncp_abs(s)) {
sec->sibling = s;
sec->parentsec->child = sec;
return;
}
for (; s->sibling; s = s->sibling) {
if (x <= ncp_abs(s->sibling)) {
sec->sibling = s->sibling;
s->sibling = sec;
return;
}
}
s->sibling = sec;
sec->sibling = 0;
}
}
static void reverse_sibling_list(Section* sec) {
int scnt;
Section* ch;
Section** pch;
for (scnt = 0, ch = sec->child; ch; ++scnt, ch = ch->sibling) {
hoc_pushobj((Object**) ch);
}
pch = &sec->child;
while (scnt--) {
ch = (Section*) hoc_objpop();
*pch = ch;
pch = &ch->sibling;
ch->parentnode = 0;
}
*pch = 0;
}
void disconnect(void) {
if (ifarg(1)) {
hoc_execerror(
"disconnect takes no positional arguments and disconnects the HOC currently accessed "
"section. If using Python, did you mean a named arg of the form, sec=section? Or you "
"can use section.disconnect().",
NULL);
}
nrn_disconnect(chk_access());
hoc_retpushx(0.);
}
static void reverse_nodes(Section* sec) {
int i, j;
Node* nd;
/* printf("reverse %d nodes for %s\n", sec->nnode-1, secname(sec));*/
for (i = 0, j = sec->nnode - 2; i < j; ++i, --j) {
nd = sec->pnode[i];
sec->pnode[i] = sec->pnode[j];
sec->pnode[i]->sec_node_index_ = i;
sec->pnode[j] = nd;
nd->sec_node_index_ = j;
}
}
void nrn_disconnect(Section* sec) {
Section* ch;
Section* oldpsec = sec->parentsec;
Node* oldpnode = sec->parentnode;
if (!oldpsec) {
return;
}
nrn_remove_sibling_list(sec);
sec->parentsec = (Section*) 0;
sec->parentnode = (Node*) 0;
nrn_parent_info(sec);
nrn_relocate_old_points(sec, oldpnode, sec, sec->parentnode);
for (ch = sec->child; ch; ch = ch->sibling)
if (nrn_at_beginning(ch)) {
ch->parentnode = sec->parentnode;
nrn_relocate_old_points(ch, oldpnode, ch, ch->parentnode);
}
section_unref(oldpsec);
tree_changed = 1;
neuron::model().node_data().mark_as_unsorted();
}
static void connectsec_impl(Section* parent, Section* sec) {
Section* ch;
Section* oldpsec = sec->parentsec;
Node* oldpnode = sec->parentnode;
double d1, d2;
Datum* pd;
d2 = xpop();
d1 = xpop();
if (d1 != 0. && d1 != 1.) {
hoc_execerror(secname(sec), " must connect at position 0 or 1");
}
if (d2 < 0. || d2 > 1.) {
hoc_execerror(secname(sec), " must connect from 0<=x<=1 of parent");
}
if (sec->parentsec) {
fprintf(stderr, "Notice: %s(%g)", secname(sec), nrn_section_orientation(sec));
fprintf(stderr,
" had previously been connected to parent %s(%g)\n",
secname(sec->parentsec),
nrn_connection_position(sec));
nrn_remove_sibling_list(sec);
}
if (nrn_section_orientation(sec) != d1) {
reverse_sibling_list(sec);
reverse_nodes(sec);
}
pd = sec->prop->dparam;
pd[1] = d2;
pd[3] = d1;
section_ref(parent);
sec->parentsec = parent;
nrn_add_sibling_list(sec);
sec->parentnode = (Node*) 0;
nrn_parent_info(sec);
nrn_relocate_old_points(sec, oldpnode, sec, sec->parentnode);
for (ch = sec->child; ch; ch = ch->sibling)
if (nrn_at_beginning(ch)) {
ch->parentnode = sec->parentnode;
nrn_relocate_old_points(ch, oldpnode, ch, ch->parentnode);
}
if (oldpsec) {
section_unref(oldpsec);
} else if (oldpnode) {
delete oldpnode;
}
tree_changed = 1;
diam_changed = 1;
}
void simpleconnectsection(void) /* 2 expr on stack and two sections on section stack */
/* for a prettier syntax: connect sec1(x), sec2(x) */
{
Section *parent, *child;
parent = nrn_sec_pop();
child = nrn_sec_pop();
connectsec_impl(parent, child);
}
void connectsection(void) /* 2 expr on stack and section symbol on section stack */
{
Section *parent, *child;
child = nrn_sec_pop();
parent = chk_access();
connectsec_impl(parent, child);
}
static Section* Sec_access(void) /* section symbol at pc */
{
Objectdata* odsav;
Object* obsav = 0;
Symlist* slsav;
Item* itm;
Symbol* s = (pc++)->sym;
if (!s) {
return chk_access();
}
if (s->cpublic == 2) {
s = s->u.sym;
odsav = hoc_objectdata_save();
obsav = hoc_thisobject;
slsav = hoc_symlist;
hoc_objectdata = hoc_top_level_data;
hoc_thisobject = 0;
hoc_symlist = hoc_top_level_symlist;
}
nrn_chk_section(s);
itm = OPSECITM(s)[range_vec_indx(s)];
if (obsav) {
hoc_objectdata = hoc_objectdata_restore(odsav);
hoc_thisobject = obsav;
hoc_symlist = slsav;
}
if (!itm) {
hoc_execerror(s->name, ": section was deleted");
}
return itm->element.sec;
#if 0
printf("accessing %s with index %d\n", s->name, access_index);
#endif
}
void sec_access(void) { /* access section */
Section** psec;
Section* sec = chk_access();
++sec->refcount;
nrn_popsec();
psec = secstack + isecstack;
if (*psec) {
section_unref(*psec);
}
*psec = sec;
}
void sec_access_object(void) { /* access section */
Section** psec;
Section* sec;
if (!section_object_seen) {
hoc_execerror("Access: Not a section", (char*) 0);
}
sec = chk_access();
++sec->refcount;
nrn_popsec();
psec = secstack + isecstack;
if (*psec) {
section_unref(*psec);
}
*psec = sec;
section_object_seen = 0;
}
void sec_access_push(void) {
nrn_pushsec(Sec_access());
}
Section* nrn_sec_pop(void) {
Section* sec = chk_access();
nrn_popsec();
return sec;
}
void hoc_sec_internal_push(void) {
Section* sec = (Section*) ((pc++)->ptr);
nrn_pushsec(sec);
}
void* hoc_sec_internal_name2ptr(const char* s, int eflag) {
/*
syntax is __nrnsec_0xff... and need to verify that ff... is a pointer
to a Section. To avoid invalid memory read errors, we changed
the section allocation/free in solve.cpp to use a SectionPool which
allows checking to see if a void* is a possible Section* from
the pool.
*/
int n;
Section* sec;
void* vp = NULL;
int err = 0;
n = strlen(s);
if (n < 12 || strncmp(s, "__nrnsec_0x", 11) != 0) {
err = 1;
} else {
if (sscanf(s + 9, "%p", &vp) != 1) {
err = 1;
}
}
if (err) {
if (eflag) {
hoc_execerror("Invalid internal section name:", s);
} else {
hoc_warning("Invalid internal section name:", s);
}
return NULL;
}
sec = (Section*) vp;
if (nrn_is_valid_section_ptr(vp) == 0 || !sec->prop || !sec->prop->dparam ||
!sec->prop->dparam[8].get<hoc_Item*>() ||
sec->prop->dparam[8].get<hoc_Item*>()->itemtype != SECTION) {
if (eflag) {
hoc_execerror("Section associated with internal name does not exist:", s);
} else {
hoc_warning("Section associated with internal name does not exist:", s);
}
vp = NULL;
}
return vp;
}
void* hoc_pysec_name2ptr(const char* s, int eflag) {
/*
syntax is _pysec.<name> where <name> is the name of a python
nrn.Section from (*nrnpy_pysec_name_p_)(sec)
*/
Section* sec = nrnpy_pysecname2sec(s);
return (void*) sec;
}
/* in an object syntax a section may either be last or next to last
in either case it is pushed when it is seen in hoc_object_component
and section_object_seen is set.
If it was last then this is called with it set and we do nothing.
ie the section is on the stack till the end of the next statement.
If it was second to last then it hoc_object_component will set it
back to 0 and pop the section. Therefore we need to push a section
below to keep the stack ok when it is popped at the end of the next
statement.
*/
void ob_sec_access_push(Item* qsec) {
if (!qsec) {
hoc_execerror("section in the object was deleted", (char*) 0);
}
nrn_pushsec(qsec->element.sec);
}
void ob_sec_access(void) {
if (!section_object_seen) {
hoc_nopop();
nrn_pushsec(secstack[isecstack]);
}
section_object_seen = 0;
}
/* For now there is always one more node than segments and the last
node has no properties. This fact is spread everywhere in which
nnode is dealt with */
void mech_access(void) { /* section symbol at pc */
Section* sec = chk_access();
Symbol* s = (pc++)->sym;
mech_insert1(sec, s->subtype);
}
void mech_insert1(Section* sec, int type) {
int n, i;
Node *nd, **pnd;
Prop* m;
/* make sure that all nodes but last of the current section have this
membrane type */
/* subsequent range variables refer to this mechanism */
/* For now, we assume:
1) Parameters for different mechanisms do not have to
have distinct names. Thus access mech marks those
symbols which are allowed.
Another access section gets back to the geometry properties.
2) Membrane is installed in entire section. (except last node)
*/
/* Is the property already allocated*/
n = sec->nnode - 1;
pnd = sec->pnode;
nd = pnd[0];
m = nrn_mechanism(type, nd);
if (!m) { /* all nodes get the property */
for (i = n - 1; i >= 0; i--) {
IGNORE(prop_alloc(&(pnd[i]->prop), type, pnd[i]));
}
#if EXTRACELLULAR
if (type == EXTRACELL) {
prop_alloc(&(pnd[n]->prop), type, pnd[n]); /*even last node*/
/* if rootnode owned by section, it gets property as well*/
if (!sec->parentsec) {
nd = sec->parentnode;
if (nd) {
prop_alloc(&nd->prop, type, nd);
}
}
extcell_2d_alloc(sec);
diam_changed = 1;
}
#endif
}
}
void mech_uninsert(void) {
Section* sec = chk_access();
Symbol* s = (pc++)->sym;
mech_uninsert1(sec, s);
}
void mech_uninsert1(Section* sec, Symbol* s) {
/* remove the mechanism from the currently accessed section */
int n, i;
Prop *m, *mnext;
int type = s->subtype;
if (type == MORPHOLOGY
#if EXTRACELLULAR
|| type == EXTRACELL
#endif
) {
hoc_warning("Can't uninsert mechanism", s->name);
return;
}
if (nrn_is_ion(type)) {
hoc_warning("Not allowed to uninsert ions at this time", s->name);
return;
}
n = sec->nnode;
for (i = 0; i < n; ++i) {
mnext = sec->pnode[i]->prop;
if (mnext && mnext->_type == type) {
sec->pnode[i]->prop = mnext->next;
single_prop_free(mnext);
continue;
}
for (m = mnext; m; m = mnext) {
mnext = m->next;
if (mnext && mnext->_type == type) {
m->next = mnext->next;
single_prop_free(mnext);
break;
}
}
}
}
void nrn_rangeconst(Section* sec, Symbol* s, neuron::container::data_handle<double> pd, int op) {
short n, i;
Node* nd;
int indx;
neuron::container::data_handle<double> dpr{};
double d = *pd;
n = sec->nnode - 1;
if (s->u.rng.type == VINDEX) {
nd = node_ptr(sec, 0., (double*) 0);
if (op) {
*pd = hoc_opasgn(op, NODEV(nd), d);
}
nd->set_v(*pd);
nd = node_ptr(sec, 1., (double*) 0);
if (op) {
*pd = hoc_opasgn(op, NODEV(nd), d);
}
nd->set_v(*pd);
for (i = 0; i < n; i++) {
if (op) {
*pd = hoc_opasgn(op, NODEV(sec->pnode[i]), d);
}
sec->pnode[i]->set_v(*pd);
}
} else {
if (s->u.rng.type == IMEMFAST) {
hoc_execerror("i_membrane_ cannot be assigned a value", 0);
}
indx = range_vec_indx(s);
if (s->u.rng.type == MORPHOLOGY) {
if (!can_change_morph(sec)) {
return;
}
diam_changed = 1;
if (sec->recalc_area_ && op != 0) {
nrn_area_ri(sec);
}
}
for (i = 0; i < n; i++) {
dpr = dprop(s, indx, sec, i);
if (op) {
*pd = hoc_opasgn(op, *dpr, d);
}
*dpr = *pd;
}
if (s->u.rng.type == MORPHOLOGY) {
sec->recalc_area_ = 1;
nrn_diam_change(sec);
}
#if EXTRACELLULAR
if (s->u.rng.type == EXTRACELL) {
if (s->u.rng.index == 0) {
diam_changed = 1;
}
dpr = neuron::container::data_handle<double>{
nrn_vext_pd(s, indx, node_ptr(sec, 0., nullptr))};
if (dpr) {
if (op) {
*dpr = hoc_opasgn(op, *dpr, d);
} else {
*dpr = d;
}
}
dpr = neuron::container::data_handle<double>{
nrn_vext_pd(s, indx, node_ptr(sec, 1., nullptr))};
if (dpr) {
if (op) {
*dpr = hoc_opasgn(op, *dpr, d);
} else {
*dpr = d;
}
}
}
#endif
}
}
// rangevariable symbol at pc, value on stack
void range_const() {
Symbol* s = (hoc_pc++)->sym;
int const op{(hoc_pc++)->i};
double d{hoc_xpop()};
auto* const sec = nrn_sec_pop();
nrn_rangeconst(sec,
s,
neuron::container::data_handle<double>{neuron::container::do_not_search, &d},
op);
hoc_pushx(d);
}