-
Notifications
You must be signed in to change notification settings - Fork 119
/
noccout.cpp
845 lines (786 loc) · 27.5 KB
/
noccout.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
#include <../../nmodlconf.h>
/* print the .c file from the lists */
#include "modl.h"
#include "parse1.hpp"
#include "symbol.h"
extern char* nmodl_version_;
#define P(arg) fputs(arg, fcout)
List *procfunc, *initfunc, *modelfunc, *termfunc, *initlist, *firstlist;
/* firstlist gets statements that must go before anything else */
List* nrnstate;
extern List *currents, *set_ion_variables(int), *get_ion_variables(int);
extern List *begin_dion_stmt(), *end_dion_stmt(const char*);
extern List* conductance_;
static void conductance_cout();
extern List* defs_list;
extern const char* saveindep;
char* modelline;
extern int brkpnt_exists;
extern int artificial_cell;
extern int net_receive_;
extern int debugging_;
extern int point_process;
extern int dtsav_for_nrn_state;
extern Symbol* cvode_nrn_cur_solve_;
extern Symbol* cvode_nrn_current_solve_;
extern List* state_discon_list_;
/* VECTORIZE has not been optional for years. We leave the define there but */
/* we no longer update the #else clauses. */
extern int vectorize;
static List* vectorize_replacements; /* pairs of item pointer, strings */
extern int electrode_current; /* 1 means we should watch out for extracellular
and handle it correctly */
#if SYSV
#define index strchr
#endif
static void initstates();
static void funcdec();
static void ext_vdef() {
if (artificial_cell) {
return;
}
if (electrode_current) {
P("#if EXTRACELLULAR\n");
P(" _nd = _ml_arg->_nodelist[_iml];\n");
P(" if (auto* const _extnode = _nrn_mechanism_access_extnode(_nd); _extnode) {\n");
P(" _v = NODEV(_nd) + _extnode->_v[0];\n");
P(" }else\n");
P("#endif\n");
P(" {\n");
P(" _v = _vec_v[_ni[_iml]];\n");
P(" }\n");
} else {
P(" _v = _vec_v[_ni[_iml]];\n");
}
}
/* when vectorize = 0 */
void c_out() {
Item* q;
Fprintf(fcout, "/* Created by Language version: %s */\n", nmodl_version_);
Fflush(fcout);
if (vectorize) {
vectorize_do_substitute();
kin_vect2(); /* heh, heh.. bet you can't guess what this is */
c_out_vectorize();
return;
}
P("/* NOT VECTORIZED */\n#define NRN_VECTORIZED 0\n");
Fflush(fcout);
/* things which must go first and most declarations */
P("#include <stdio.h>\n#include <stdlib.h>\n#include <math.h>\n#include \"mech_api.h\"\n");
P("#undef PI\n");
P("#define nil 0\n");
P("#define _pval pval\n"); // due to some old models using _pval
P("#include \"md1redef.h\"\n");
P("#include \"section_fwd.hpp\"\n");
P("#include \"nrniv_mf.h\"\n");
P("#include \"md2redef.h\"\n");
P("#include \"neuron/cache/mechanism_range.hpp\"\n");
P("#include <vector>\n");
/* avoid clashes with mech names */
P("using std::size_t;\n");
P("static auto& std_cerr_stream = std::cerr;\n");
printlist(defs_list);
printlist(firstlist);
P("static int _reset;\n");
P("static ");
if (modelline) {
Fprintf(fcout, "const char *modelname = \"%s\";\n\n", modelline);
} else {
Fprintf(fcout, "const char *modelname = \"\";\n\n");
}
Fflush(fcout); /* on certain internal errors partial output
* is helpful */
P("static int error;\n");
P("static ");
P("int _ninits = 0;\n");
P("static int _match_recurse=1;\n");
P("static void ");
P("_modl_cleanup(){ _match_recurse=1;}\n");
/*
* many machinations are required to make the infinite number of
* definitions involving _p in defs.h to be invisible to the user
*/
/*
* This one allows scop variables in functions which do not have the
* p array as an argument
*/
funcdec();
Fflush(fcout);
/*
* translations of named blocks into functions, procedures, etc. Also
* some special declarations used by some blocks
*/
printlist(procfunc);
Fflush(fcout);
/* Initialization function must always be present */
P("\nstatic void initmodel() {\n int _i; double _save;");
P("_ninits++;\n");
P(saveindep); /*see solve.c; blank if not a time dependent process*/
P("{\n");
initstates();
printlist(initfunc);
P("\n}\n}\n");
Fflush(fcout);
/* generation of initmodel interface */
P("\nstatic void nrn_init(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type){\n");
P("Node *_nd; double _v; int* _ni; int _cntml;\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto* const _vec_v = _nt->node_voltage_storage();\n");
P("_ml = &_lmr;\n"); // update global _ml
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n"); // use global _iml
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
if (debugging_ && net_receive_) {
P(" _tsav = -1e20;\n");
}
if (!artificial_cell) {
ext_vdef();
}
if (!artificial_cell) {
P(" v = _v;\n");
}
printlist(get_ion_variables(1));
P(" initmodel();\n");
printlist(set_ion_variables(2));
P("}}\n");
/* standard modl EQUATION without solve computes current */
P("\nstatic double _nrn_current(double _v){double _current=0.;v=_v;");
if (cvode_nrn_current_solve_) {
fprintf(fcout, "if (cvode_active_) { %s(); }\n", cvode_nrn_current_solve_->name);
}
P("{");
if (currents->next != currents) {
printlist(modelfunc);
}
ITERATE(q, currents) {
Sprintf(buf, " _current += %s;\n", SYM(q)->name);
P(buf);
}
P("\n} return _current;\n}\n");
/* For the classic BREAKPOINT block, the neuron current also has to compute the dcurrent/dv as
well as make sure all currents accumulated properly (currents list) */
if (brkpnt_exists) {
P("\nstatic void nrn_cur(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type){\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto const _vec_rhs = _nt->node_rhs_storage();\n");
P("auto const _vec_sav_rhs = _nt->node_sav_rhs_storage();\n");
P("auto const _vec_v = _nt->node_voltage_storage();\n");
P("Node *_nd; int* _ni; double _rhs, _v; int _cntml;\n");
P("_ml = &_lmr;\n"); // update global _ml
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n"); // global _iml
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
ext_vdef();
if (currents->next != currents) {
printlist(get_ion_variables(0));
cvode_rw_cur(buf);
P(buf);
}
if (cvode_nrn_cur_solve_) {
fprintf(fcout, "if (cvode_active_) { %s(); }\n", cvode_nrn_cur_solve_->name);
}
if (currents->next != currents) {
P(" auto const _g_local = _nrn_current(_v + .001);\n");
printlist(begin_dion_stmt());
if (state_discon_list_) {
P(" state_discon_flag_ = 1; _rhs = _nrn_current(_v); state_discon_flag_ = 0;\n");
} else {
P(" _rhs = _nrn_current(_v);\n");
}
printlist(end_dion_stmt(".001"));
P(" _g = (_g_local - _rhs)/.001;\n");
/* set the ion variable values */
printlist(set_ion_variables(0));
if (point_process) {
P(" _g *= 1.e2/(_nd_area);\n");
P(" _rhs *= 1.e2/(_nd_area);\n");
}
if (electrode_current) {
P(" _vec_rhs[_ni[_iml]] += _rhs;\n");
P(" if (_vec_sav_rhs) {\n");
P(" _vec_sav_rhs[_ni[_iml]] += _rhs;\n");
P(" }\n");
P("#if EXTRACELLULAR\n");
P(" if (auto* const _extnode = _nrn_mechanism_access_extnode(_nd); _extnode) {\n");
P(" *_extnode->_rhs[0] += _rhs;\n");
P(" }\n");
P("#endif\n");
} else {
P(" _vec_rhs[_ni[_iml]] -= _rhs;\n");
}
}
P(" \n}}\n");
/* for the classic breakpoint block, nrn_cur computed the conductance, _g,
and now the jacobian calculation merely returns that */
P("\nstatic void nrn_jacob(_nrn_model_sorted_token const& _sorted_token, NrnThread* "
"_nt, Memb_list* _ml_arg, int _type) {\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto const _vec_d = _nt->node_d_storage();\n");
P("auto const _vec_sav_d = _nt->node_sav_d_storage();\n");
P("auto* const _ml = &_lmr;\n");
P("Node *_nd; int* _ni; int _iml, _cntml;\n");
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n");
if (electrode_current) {
P(" _nd = _ml_arg->_nodelist[_iml];\n");
P(" _vec_d[_ni[_iml]] -= _g;\n");
P(" if (_vec_sav_d) {\n");
P(" _vec_sav_d[_ni[_iml]] -= _g;\n");
P(" }\n");
P("#if EXTRACELLULAR\n");
P(" if (auto* const _extnode = _nrn_mechanism_access_extnode(_nd); _extnode) {\n");
P(" *_extnode->_d[0] += _g;\n");
P(" }\n");
P("#endif\n");
} else {
P(" _vec_d[_ni[_iml]] += _g;\n");
}
P(" \n}}\n");
}
/* nrnstate list contains the EQUATION solve statement so this
advances states by dt */
P("\nstatic void nrn_state(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type){\n");
if (nrnstate || currents->next == currents) {
P("Node *_nd; double _v = 0.0; int* _ni; int _cntml;\n");
if (dtsav_for_nrn_state && nrnstate) {
P("double _dtsav = dt;\n"
"if (secondorder) { dt *= 0.5; }\n");
}
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto* const _vec_v = _nt->node_voltage_storage();\n");
P("_ml = &_lmr;\n"); // update global _ml
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n"); // use the global _iml
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
P(" _nd = _ml_arg->_nodelist[_iml];\n");
ext_vdef();
P(" v=_v;\n{\n");
printlist(get_ion_variables(1));
if (nrnstate) {
printlist(nrnstate);
}
if (currents->next == currents) {
printlist(modelfunc);
}
printlist(set_ion_variables(1));
P("}}\n");
if (dtsav_for_nrn_state && nrnstate) {
P(" dt = _dtsav;");
}
}
P("\n}\n");
P("\nstatic void terminal(){}\n");
/* initlists() is called once to setup slist and dlist pointers */
P("\nstatic void _initlists() {\n");
P(" int _i; static int _first = 1;\n");
P(" if (!_first) return;\n");
printlist(initlist);
P("_first = 0;\n}\n");
}
/*
* One of the things initmodel() must do is initialize all states to the
* value of state0. This generated code goes before any explicit initialize
* code written by the user.
*/
static void initstates() {
int i;
Item* qs;
Symbol* s;
SYMITER_STAT {
/* ioni and iono should not have initialization lines */
#define IONCONC 010000
if (s->nrntype & IONCONC) {
continue;
}
Sprintf(buf, "%s0", s->name);
if (lookup(buf)) { /* if no constant associated
* with a state such as the
* ones automattically
* generated by SENS then
* there is no initialization
* line */
if (s->subtype & ARRAY) {
Fprintf(fcout,
" for (_i=0; _i<%d; _i++) %s[_i] = %s0;\n",
s->araydim,
s->name,
s->name);
} else {
Fprintf(fcout, " %s = %s0;\n", s->name, s->name);
}
}
}
}
/*
* here is the only place as of 18-apr-89 where we don't explicitly know the
* type of a list element
*/
static int newline, indent;
void printitem(Item* q) {
if (q->itemtype == SYMBOL) {
if (SYM(q)->type == SPECIAL) {
switch (SYM(q)->subtype) {
case SEMI:
newline = 1;
break;
case BEGINBLK:
newline = 1;
indent++;
break;
case ENDBLK:
newline = 1;
indent--;
break;
}
}
Fprintf(fcout, " %s", SYM(q)->name);
} else if (q->itemtype == VERBATIM) {
verbatim_adjust(STR(q));
} else if (q->itemtype == ITEM) {
printitem(ITM(q));
} else {
Fprintf(fcout, " %s", STR(q));
}
}
void debugprintitem(Item* q) {
if (q->itemtype == SYMBOL) {
printf("SYM %s\n", SYM(q)->name);
} else if (q->itemtype == VERBATIM) {
printf("VERB %s\n", STR(q));
} else if (q->itemtype == ITEM) {
printf("ITM ");
debugprintitem(ITM(q));
} else {
printf("STR %s\n", STR(q));
}
}
/* does not include q2 */
char* items_as_string(Item* q1, Item* q2) {
Item* q;
buf[0] = '\0';
for (q = q1; q != q2; q = q->next) {
if (buf[0] != '\0') {
strcat(buf, " ");
}
if (q->itemtype == SYMBOL) {
strcat(buf, SYM(q)->name);
} else if (q->itemtype == STRING) {
strcat(buf, STR(q));
} else {
assert(0);
}
}
return strdup(buf);
}
void printlist(List* s) {
Item* q;
int i;
newline = 0, indent = 0;
/*
* most of this is merely to decide where newlines and indentation
* goes so that the .c file can be read if something goes wrong
*/
if (!s) {
return;
}
ITERATE(q, s) {
printitem(q);
if (newline) {
newline = 0;
Fprintf(fcout, "\n");
for (i = 0; i < indent; i++) {
Fprintf(fcout, " ");
}
}
}
}
static void funcdec() {
int i;
Symbol* s;
List* qs;
int j, narg, more;
SYMITER(NAME) {
more = 0;
if (s->subtype & PROCED) {
Fprintf(fcout, "static int %s(", s->name);
more = 1;
}
if (more) {
narg = s->varnum;
if (vectorize) {
if (narg) {
Fprintf(fcout, "_internalthreadargsprotocomma_ ");
} else {
Fprintf(fcout, "_internalthreadargsproto_");
}
}
/*loop over argcount and add ,double */
if (narg > 0) {
Fprintf(fcout, "double");
}
for (j = 1; j < narg; ++j) {
Fprintf(fcout, ", double");
}
Fprintf(fcout, ");\n");
}
}
}
/* when vectorize = 1 */
void c_out_vectorize() {
Item* q;
/* things which must go first and most declarations */
P("/* VECTORIZED */\n#define NRN_VECTORIZED 1\n");
P("#include <stdio.h>\n#include <stdlib.h>\n#include <math.h>\n#include \"mech_api.h\"\n");
P("#undef PI\n");
P("#define nil 0\n");
P("#define _pval pval\n"); // due to some old models using _pval
P("#include \"md1redef.h\"\n");
P("#include \"section_fwd.hpp\"\n");
P("#include \"nrniv_mf.h\"\n");
P("#include \"md2redef.h\"\n");
P("#include \"neuron/cache/mechanism_range.hpp\"\n");
printlist(defs_list);
printlist(firstlist);
P("static int _reset;\n");
if (modelline) {
Fprintf(fcout, "static const char *modelname = \"%s\";\n\n", modelline);
} else {
Fprintf(fcout, "static const char *modelname = \"\";\n\n");
}
Fflush(fcout); /* on certain internal errors partial output
* is helpful */
P("static int error;\n");
P("static int _ninits = 0;\n");
P("static int _match_recurse=1;\n");
P("static void _modl_cleanup(){ _match_recurse=1;}\n");
funcdec();
Fflush(fcout);
/*
* translations of named blocks into functions, procedures, etc. Also
* some special declarations used by some blocks
*/
printlist(procfunc);
Fflush(fcout);
/* Initialization function must always be present */
P("\nstatic void initmodel(_internalthreadargsproto_) {\n int "
"_i; double _save;");
P("{\n");
initstates();
printlist(initfunc);
P("\n}\n}\n");
Fflush(fcout);
/* generation of initmodel interface */
P("\nstatic void nrn_init(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type){\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto* const _vec_v = _nt->node_voltage_storage();\n");
P("auto* const _ml = &_lmr;\n");
P("Datum* _ppvar; Datum* _thread;\n");
P("Node *_nd; double _v; int* _ni; int _iml, _cntml;\n");
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("_thread = _ml_arg->_thread;\n");
/*check_tables();*/
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n");
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
check_tables();
if (debugging_ && net_receive_) {
P(" _tsav = -1e20;\n");
}
if (!artificial_cell) {
ext_vdef();
}
if (!artificial_cell) {
P(" v = _v;\n");
}
printlist(get_ion_variables(1));
P(" initmodel(_threadargs_);\n");
printlist(set_ion_variables(2));
P("}\n");
P("}\n");
/* standard modl EQUATION without solve computes current */
if (!conductance_) {
P("\nstatic double _nrn_current(_internalthreadargsprotocomma_ "
"double _v) {\n"
"double _current=0.; v=_v;\n");
if (cvode_nrn_current_solve_) {
fprintf(fcout,
"if (cvode_active_) { %s(_threadargs_); }\n",
cvode_nrn_current_solve_->name);
}
P("{");
if (currents->next != currents) {
printlist(modelfunc);
}
ITERATE(q, currents) {
if (SYM(q) != breakpoint_current(SYM(q))) {
diag(
"current can only be LOCAL in a BREAKPOINT if CONDUCTANCE statements are "
"used. ",
SYM(q)->name);
}
Sprintf(buf, " _current += %s;\n", SYM(q)->name);
P(buf);
}
P("\n} return _current;\n}\n");
}
/* For the classic BREAKPOINT block, the neuron current also has to compute the dcurrent/dv as
well as make sure all currents accumulated properly (currents list) */
if (brkpnt_exists) {
P("\nstatic void nrn_cur(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type) {\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto const _vec_rhs = _nt->node_rhs_storage();\n");
P("auto const _vec_sav_rhs = _nt->node_sav_rhs_storage();\n");
P("auto const _vec_v = _nt->node_voltage_storage();\n");
P("auto* const _ml = &_lmr;\n");
P("Datum* _ppvar; Datum* _thread;\n");
P("Node *_nd; int* _ni; double _rhs, _v; int _iml, _cntml;\n");
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("_thread = _ml_arg->_thread;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n");
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
ext_vdef();
if (currents->next != currents) {
printlist(get_ion_variables(0));
cvode_rw_cur(buf);
P(buf);
}
if (cvode_nrn_cur_solve_) {
fprintf(fcout,
"if (cvode_active_) { %s(_threadargs_); }\n",
cvode_nrn_cur_solve_->name);
}
if (currents->next != currents) {
if (conductance_) {
P(" {\n");
conductance_cout();
printlist(set_ion_variables(0));
P(" }\n");
} else {
P(" auto const _g_local = _nrn_current(_threadargscomma_ _v + .001);\n");
printlist(begin_dion_stmt());
if (state_discon_list_) {
P(" state_discon_flag_ = 1; _rhs = _nrn_current(_v); state_discon_flag_ = "
"0;\n");
} else {
P(" _rhs = _nrn_current(_threadargscomma_ _v);\n");
}
printlist(end_dion_stmt(".001"));
P(" _g = (_g_local - _rhs)/.001;\n");
/* set the ion variable values */
printlist(set_ion_variables(0));
} /* end of not conductance */
if (point_process) {
P(" _g *= 1.e2/(_nd_area);\n");
P(" _rhs *= 1.e2/(_nd_area);\n");
}
if (electrode_current) {
P(" _vec_rhs[_ni[_iml]] += _rhs;\n");
P(" if (_vec_sav_rhs) {\n");
P(" _vec_sav_rhs[_ni[_iml]] += _rhs;\n");
P(" }\n");
P("#if EXTRACELLULAR\n");
P(" if (auto* const _extnode = _nrn_mechanism_access_extnode(_nd); _extnode) {\n");
P(" *_extnode->_rhs[0] += _rhs;\n");
P(" }\n");
P("#endif\n");
} else {
P(" _vec_rhs[_ni[_iml]] -= _rhs;\n");
}
}
P(" \n}\n");
P(" \n}\n");
/* for the classic breakpoint block, nrn_cur computed the conductance, _g,
and now the jacobian calculation merely returns that */
P("\nstatic void nrn_jacob(_nrn_model_sorted_token const& _sorted_token, NrnThread* "
"_nt, Memb_list* _ml_arg, int _type) {\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto const _vec_d = _nt->node_d_storage();\n");
P("auto const _vec_sav_d = _nt->node_sav_d_storage();\n");
P("auto* const _ml = &_lmr;\n");
P("Datum* _ppvar; Datum* _thread;\n");
P("Node *_nd; int* _ni; int _iml, _cntml;\n");
P("_ni = _ml_arg->_nodeindices;\n");
P("_cntml = _ml_arg->_nodecount;\n");
P("_thread = _ml_arg->_thread;\n");
P("for (_iml = 0; _iml < _cntml; ++_iml) {\n");
if (electrode_current) {
P(" _nd = _ml_arg->_nodelist[_iml];\n");
P(" _vec_d[_ni[_iml]] -= _g;\n");
P(" if (_vec_sav_d) {\n");
P(" _vec_sav_d[_ni[_iml]] -= _g;\n");
P(" }\n");
P("#if EXTRACELLULAR\n");
P(" if (auto* const _extnode = _nrn_mechanism_access_extnode(_nd); _extnode) {\n");
P(" *_extnode->_d[0] += _g;\n");
P(" }\n");
P("#endif\n");
} else {
P(" _vec_d[_ni[_iml]] += _g;\n");
}
P(" \n}\n");
P(" \n}\n");
}
/* nrnstate list contains the EQUATION solve statement so this
advances states by dt */
P("\nstatic void nrn_state(_nrn_model_sorted_token const& _sorted_token, NrnThread* _nt, "
"Memb_list* _ml_arg, int _type) {\n");
P("_nrn_mechanism_cache_range _lmr{_sorted_token, *_nt, *_ml_arg, _type};\n");
P("auto* const _vec_v = _nt->node_voltage_storage();\n");
P("auto* const _ml = &_lmr;\n");
if (nrnstate || currents->next == currents) {
P("Datum* _ppvar; Datum* _thread;\n");
P("Node *_nd; double _v = 0.0; int* _ni;\n");
if (dtsav_for_nrn_state && nrnstate) {
P("double _dtsav = dt;\n"
"if (secondorder) { dt *= 0.5; }\n");
}
P("_ni = _ml_arg->_nodeindices;\n");
P("size_t _cntml = _ml_arg->_nodecount;\n");
P("_thread = _ml_arg->_thread;\n");
P("for (size_t _iml = 0; _iml < _cntml; ++_iml) {\n");
P(" _ppvar = _ml_arg->_pdata[_iml];\n");
P(" _nd = _ml_arg->_nodelist[_iml];\n");
ext_vdef();
P(" v=_v;\n{\n");
printlist(get_ion_variables(1));
if (nrnstate) {
printlist(nrnstate);
}
if (currents->next == currents) {
printlist(modelfunc);
}
printlist(set_ion_variables(1));
P("}}\n");
if (dtsav_for_nrn_state && nrnstate) {
P(" dt = _dtsav;");
}
}
P("\n}\n");
P("\nstatic void terminal(){}\n");
/* vectorized: data must have own copies of slist and dlist
for now we don't vectorize if slist or dlist exists. Eventually
must separate initialization of static things from vectorized
things.
*/
/* initlists() is called once to setup slist and dlist pointers */
P("\nstatic void _initlists(){\n");
P(" int _i; static int _first = 1;\n");
P(" if (!_first) return;\n");
printlist(initlist);
P("_first = 0;\n}\n");
}
void vectorize_substitute(Item* q, const char* str) {
if (!vectorize_replacements) {
vectorize_replacements = newlist();
}
lappenditem(vectorize_replacements, q);
lappendstr(vectorize_replacements, str);
}
Item* vectorize_replacement_item(Item* q) {
Item* q1;
if (vectorize_replacements) {
ITERATE(q1, vectorize_replacements) {
if (ITM(q1) == q) {
return q1->next;
}
}
}
return (Item*) 0;
}
void vectorize_do_substitute() {
Item *q, *q1;
if (vectorize_replacements) {
ITERATE(q, vectorize_replacements) {
q1 = ITM(q);
q = q->next;
replacstr(q1, STR(q));
}
}
}
static void conductance_cout() {
int i = 0;
Item* q;
List* m;
/* replace v with _v */
m = newlist();
ITERATE(q, modelfunc) {
if (q->itemtype == SYMBOL) {
if (strcmp(SYM(q)->name, "v") == 0) {
lappendstr(m, "_v");
} else {
lappendsym(m, SYM(q));
}
} else if (q->itemtype == STRING) {
lappendstr(m, STR(q));
} else {
diag("modelfunc contains item which is not a SYMBOL or STRING", (char*) 0);
}
}
/* eliminate first { */
ITERATE(q, m) {
if (q->itemtype == SYMBOL) {
if (strcmp(SYM(q)->name, "{") == 0) {
remove(q);
break;
}
}
}
/* eliminate last } */
for (q = m->prev; q != m; q = q->prev) {
if (q->itemtype == SYMBOL) {
if (strcmp(SYM(q)->name, "}") == 0) {
remove(q);
break;
}
}
}
printlist(m);
ITERATE(q, currents) {
if (i == 0) {
Sprintf(buf, " _rhs = %s", breakpoint_current(SYM(q))->name);
} else {
Sprintf(buf, " + %s", breakpoint_current(SYM(q))->name);
}
P(buf);
i += 1;
}
if (i > 0) {
P(";\n");
}
i = 0;
ITERATE(q, conductance_) {
if (i == 0) {
Sprintf(buf, " _g = %s", SYM(q)->name);
} else {
Sprintf(buf, " + %s", SYM(q)->name);
}
P(buf);
i += 1;
q = q->next;
}
if (i > 0) {
P(";\n");
}
ITERATE(q, conductance_) {
if (SYM(q->next)) {
Sprintf(buf, " _ion_di%sdv += %s", SYM(q->next)->name, SYM(q)->name);
P(buf);
if (point_process) {
P("* 1.e2/(_nd_area)");
}
P(";\n");
}
q = q->next;
}
}