File: spice-wrapper.cc

package info (click to toggle)
gnucap 1%3A0.36~20171003-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 9,336 kB
  • sloc: cpp: 29,323; sh: 319; makefile: 172
file content (1904 lines) | stat: -rw-r--r-- 63,057 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
/* $Id: spice-wrapper.cc 2017/06/07 $ -*- C++ -*-
 * Copyright (C) 2007 Albert Davis
 * Author: Albert Davis <aldavis@gnu.org>
 *
 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
 *
 * This file is distributed as is, completely without warranty or
 * service support. The author and its employees are not liable for
 * the condition or performance of the software.
 *
 * The author owns the copyright but shall not be liable for any
 * infringement of copyright or other proprietary rights brought by
 * third parties against the users of the software.
 *
 * The author hereby disclaims all implied warranties.
 *
 * This author grants the users the right to modify, copy, and
 * redistribute this file, for any purpose, both within the user's
 * organization and externally.
 */
//testing=script 2008.11.28
// code style comment:  Use of "reinterpret_cast" is always bad style.
/*--------------------------------------------------------------------------*/
// spice includes
extern "C" {
  #define _complex CompleX
  #define NODE NodE
  #define public PubliC
  #define bool BooL
  #define main MaiN
  #define setenv SetenV
  #include "capabil.h"
  #include "const.h"
  #include "iferrmsg.h"
  #include "devdefs.h"
  #include "ftedefs.h"
  #include "optdefs.h"
#ifdef JSPICE3
  #include "uflags.h"
  #include "inpdefs.h"
  #include "tskdefs.h"
#endif
  #undef setenv
  #undef main
  #undef bool
  #undef public
  #undef NODE
  #undef _complex

  #undef eq
  #undef OPT
  #undef LINEAR
  #undef STRING
  #undef BOOLEAN
  
  #undef VT_BOOL
  #undef VT_NUM
  #undef VT_REAL
  #undef VT_STRING
  #undef VT_LIST
}
/*--------------------------------------------------------------------------*/
// gnucap includes
#include "globals.h"
#include "u_xprobe.h"
#include "e_paramlist.h"
#include "e_storag.h"
#include "e_model.h"
/*--------------------------------------------------------------------------*/
// customization -- must be last
#include "wrapper.h"
#if !defined(UNCONNECTED_NODES)
  #define UNCONNECTED_NODES uDISALLOW
  #if (MIN_NET_NODES != MAX_NET_NODES)
    #error "What should I do with the unconnected nodes?"
  #endif
#endif
#if !defined(VALUE_NAME)
  #define VALUE_NAME "#"
#endif
#if !defined(TAIL_SIZE)
  #define TAIL_SIZE 1
#endif
#if !defined(IS_VALID)
#define IS_VALID {return MODEL_CARD::is_valid(d);}
#endif
/*--------------------------------------------------------------------------*/
extern SPICEdev info;
const int SPICE_INVALID_NODE = 0;
const int SPICE_UNCONNECTED_NODE = -1;
const int OFFSET = 1;
enum {uGROUND=1, uFLOAT=2, uDISALLOW=3};
const int MATRIX_NODES = (MAX_NET_NODES + INTERNAL_NODES);
class DEV_SPICE;
class MODEL_SPICE;
static COMMON_PARAMLIST Default_Params(CC_STATIC);
/*--------------------------------------------------------------------------*/
/* function mapping: see devdefs.h
 * DEVparam	DEV_SPICE::parse_spice
 * DEVmodParam	MODEL_SPICE::parse_params
 * DEVload	DEV_SPICE::do_tr
 * DEVsetup	MODEL_SPICE::precalc, DEV_SPICE::expand
 * DEVunsetup	not used -- spice baggage -- just zeros some nodes
 * DEVpzSetup	not used -- pole-zero
 * DEVtemperature DEV_SPICE::internal_precalc
 * DEVtrunc	DEV_SPICE::tr_review
 * DEVfindBranch not used -- current probes for current controlled source
 * DEVacLoad	DEV_SPICE::do_ac
 * DEVaccept 	not used -- sets break points //BUG// need for: isrc, ltra, tra, vsrc, cpl, txl
 * DEVdestroy	not used -- spice baggage -- deletes a list
 * DEVmodDelete	not used -- spice baggage -- delete one model
 * DEVdelete	not used -- spice baggage -- delete one instance
 * DEVsetic  	not used -- "getic" -- initial conditions //BUG// need this
 * DEVask	DEV_SPICE::print_args, DEV_SPICE::tr_probe_num
 * DEVmodAsk	MODEL_SPICE::print_params, MODEL_SPICE::print_calculated
 * DEVpzLoad  	not used -- pole zero -- should use for AC
 * DEVconvTest 	DEV_SPICE::do_tr
 * DEVsenSetup 	not used -- sensitivity
 * DEVsenLoad  	not used -- sensitivity
 * DEVsenUpdate	not used -- sensitivity
 * DEVsenAcLoad	not used -- sensitivity
 * DEVsenPrint 	not used -- sensitivity
 * DEVsenTrunc 	not used -- sensitivity
 * DEVdisto    	not used -- distortion
 * DEVnoise   	not used -- noise
*/
/*--------------------------------------------------------------------------*/
union SPICE_MODEL_DATA {
  mutable GENmodel _gen;// generic -- use this one
  MODEL _full;		// determines size
  char  _space;		// char pointer for fill_n
  
  SPICE_MODEL_DATA() {
    std::fill_n(&_space, sizeof(MODEL), '\0');
  }
  SPICE_MODEL_DATA(const SPICE_MODEL_DATA& p) 
    : _full(p._full) {
  }
};
/*--------------------------------------------------------------------------*/
class MODEL_SPICE : public MODEL_CARD{
private:
  static int _count;
  static CKTcircuit _ckt;
public:
  SPICE_MODEL_DATA _spice_model;
  std::string _key;
  std::string _level;
  PARAM_LIST  _params;
protected:
  explicit MODEL_SPICE(const MODEL_SPICE& p);	// for clone
public:
  explicit MODEL_SPICE(const DEV_SPICE* p);	// for dispatcher
  ~MODEL_SPICE();
public: // override virtual
  MODEL_CARD* clone()const {return new MODEL_SPICE(*this);}
  bool is_valid(const COMPONENT* d)const IS_VALID
  //void expand();
  void precalc_first();

public:	// type
  void set_dev_type(const std::string& nt);
  std::string dev_type()const	{ return _key;}

public: // parameters
  bool param_is_printable(int)const;
  std::string param_name(int)const;
  std::string param_name(int i, int j)const;
  std::string param_value(int)const; 
  void set_param_by_name(std::string Name, std::string Value);
  void set_param_by_index(int, std::string&, int);
  int param_count_dont_print()const {return MODEL_CARD::param_count();}
  int param_count()const { return (static_cast<int>(_params.size()) + MODEL_CARD::param_count());}

  void Set_param_by_name(std::string Name, std::string Value);

public: // not virtual
  static int count()		{untested(); return _count;}
  static CKTcircuit* ckt()	{return &_ckt;}
  static void init_ckt();
};
/*--------------------------------------------------------------------------*/
class DEV_SPICE : public STORAGE {
private:
  static int _count;
public:
private:
  union {
    mutable GENinstance _spice_instance;
    INSTANCE _inst;
    char _inst_space;
  };
  std::string _modelname;
  const MODEL_SPICE* _model;
  const SPICE_MODEL_DATA* _spice_model;
  node_t _nodes[MATRIX_NODES];
  DPAIR* _matrix[MATRIX_NODES+OFFSET];	// For tran, real is now, imag is saved.
  DPAIR _matrix_core[MATRIX_NODES+OFFSET][MATRIX_NODES+OFFSET];
public:
  double _i0[MATRIX_NODES+OFFSET];	// right side - current offsets or ac real part
  double _i1[MATRIX_NODES+OFFSET];	// right side - saved ......... or ac imag part
  double _v1[MATRIX_NODES+OFFSET];	// input voltages
  double* (_states[8]); // array of 8 pointers
  double* _states_1;
  int _num_states;
  int _maxEqNum;
private:
  explicit DEV_SPICE(const DEV_SPICE& p);
public:
  explicit DEV_SPICE();
  ~DEV_SPICE();
protected: // override virtual
  char	  id_letter()const	{untested();return SPICE_LETTER[0];}
  bool	  print_type_in_spice()const {return true;}
  std::string value_name()const {return VALUE_NAME;}
  int	  max_nodes()const	{return MAX_NET_NODES;}
  int	  min_nodes()const	{return MIN_NET_NODES;}
  int	  matrix_nodes()const	{return MATRIX_NODES;}
  int	  net_nodes()const	{return _net_nodes;}
  int	  int_nodes()const	{return INTERNAL_NODES;}
  CARD*	  clone()const		{return new DEV_SPICE(*this);}
  //void  precalc_first();	//ELEMENT
  void	  expand();
  void	  precalc_last();
  //void  map_nodes();		//ELEMENT
  void	  internal_precalc();

  void	  tr_iwant_matrix()	{tr_iwant_matrix_extended();}
  void	  tr_begin()		{STORAGE::tr_begin();   internal_precalc();}
  void	  tr_restore()		{STORAGE::tr_restore(); internal_precalc();}
  void	  dc_advance()		{STORAGE::dc_advance(); internal_precalc();}
  void	  tr_advance();
  void	  tr_regress();
  bool	  tr_needs_eval()const;
  //void  tr_queue_eval();	//ELEMENT
  bool	  do_tr();
  void	  tr_load();
  TIME_PAIR tr_review();
  void    tr_accept();
  void	  tr_unload();
  double  tr_involts()const	{unreachable();return NOT_VALID;}
  //double tr_input()const	//ELEMENT
  double  tr_involts_limited()const {unreachable();return NOT_VALID;}
  //double tr_input_limited()const //ELEMENT
  double  tr_amps()const	{itested();return NOT_VALID;}
  double  tr_probe_num(const std::string&)const;

  void	  ac_iwant_matrix()	{ac_iwant_matrix_extended();}
  void    ac_begin();
  void	  do_ac();
  void	  ac_load();
  COMPLEX ac_involts()const	{unreachable();return NOT_VALID;}
  COMPLEX ac_amps()const	{unreachable();return NOT_VALID;}
  XPROBE  ac_probe_ext(const std::string&)const {itested(); return XPROBE(NOT_VALID, mtNONE);}
  int	  tail_size()const	{return TAIL_SIZE;}
public:	// type
  void set_dev_type(const std::string& nt);
  std::string dev_type()const	{return _modelname;}
public:	// ports
  // bool port_exists(int i)const //COMPONENT
  std::string port_name(int i)const {itested();
    assert(i >= 0);
    assert(i < MAX_NET_NODES);
    return port_names[i];
  }
  // const std::string& port_value(int i)const; //COMPONENT
  //void set_port_by_name(std::string& name, std::string& value);
  //void set_port_by_index(int index, std::string& value);
private: // parameters
  //bool Param_exists(int i)const; // {return Param_name(i) != "";}
  //bool Param_is_printable(int)const;
  //std::string Param_name(int)const;
  //std::string Param_name(int i, int j)const {return STORAGE::Param_name(i, j);}
  //std::string Param_value(int)const; 
  void set_param_by_name(std::string Name, std::string Value);
  void Set_param_by_name(std::string Name, std::string Value);
  void Set_param_by_index(int, std::string&, int);
  int param_count_dont_print()const {return common()->COMMON_COMPONENT::param_count();}
private:
  CKTcircuit* ckt()const	{return MODEL_SPICE::ckt();}
  void init_ckt()		{MODEL_SPICE::init_ckt();}
  void update_ckt()const;
  void localize_ckt()const;
  int* spice_nodes()const	{return &(_spice_instance.GENnode1);}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
CKTcircuit MODEL_SPICE::_ckt;
/* 
 * used as intended, copy-out, matters: CKTnoncon, CKTtroubleElt
 *
 * used as intended, func specific: CKTmode, CKTcurrentAnalysis
 *
 * used as intended, localized, matters: CKTstates, CKTdelta, CKTdeltaOld,
 * CKTag(broken), CKTorder(broken), CKTrhs, CKTrhsOld, CKTirhs,
 * CKTtimePoints(broken,ltra?)
 *
 * used as intended, updated, matters: CKTtime, CKTtemp, CKTomega
 *
 * used as intended, constant, matters: CKTnomTemp, CKTabstol,
 * CKTreltol, CKTvoltTol, CKTgmin, CKTsrcFact(broken),
 * CKTdefaultMosL, CKTdefaultMosW, CKTdefaultMosAD, CKTdefaultMosAS,
 * 
 * used almost as intended, matters, probably ok:
 * CKTbypass: false to disable
 * CKTintegrateMethod: used only by Jspice -- set to 0 to disable
 * CKTsenInfo: used by sens, NULL to disable
 * CKTfixLimit(mos1236): 1 for Spice-2 mode
 * CKTbadMos3(mos3): false for Spice-3 mode
 *
 * misused: (not used by spice devs, use to pass gnucap stuff through)
 * CKTstat: the device (type DEV_SPICE)
 * CKTmaxEqNum: use as counter for internal nodes, pass to CKTmkVolt
 *
 * need to handle (ind): CKThead(ind,load)
 * need to handle (cpl,txl): CKTnodes(cpl,txl),
 *
 * need to handle ([iv]src):
 * CKTbreak([iv]src,accept), CKTfinalTime([iv]src,load), CKTstep([iv]src,load),
 *
 * need to handle (ltra):
 * CKTminBreak(ltra,tra,accept), CKTtrtol(ltra,trunc),
 * CKTmaxStep(ltra,temp), CKTtimeListSize(ltra,Jspice,accept),
 * CKTtimeIndex(ltra), CKTsizeIncr(ltra), CKTtryToCompact(ltra)
 *
 * used by "predictor": CKTagp, CKTpred, CKTsols
 * used by "sens": CKTirhsOld, CKTrhsOp
 * used by noise&distortion: CKTcurJob
 *
 * not used: CKTvt, CKTmaxOrder, CKTmatrix, CKTniState, CKTrhsSpare, 
 * CKTirhsSpare, CKTsenRhs, CKTseniRhs, CKTlastNode, CKTnumStates,
 * CKTdcMaxIter, CKTdcTrcvMaxIter, CKTtranMaxIter, CKTbreakSize, CKTsaveDelta,
 * CKTbreaks, CKTpivotAbsTol, CKTpivotRelTol, CKTchgtol, CKTlteReltol, CKTlteAbstol,
 * CKTdelmin, CKTinitTime, CKTdiagGmin, CKTnumSrcSteps, CKTnumGminSteps, CKThadNodeset,
 * CKTnoOpIter, CKTisSetup, CKTdeltaList, CKTkeepOpInfo, CKTtroubleNode
 */
#define assert_ckt_initialized(ckt) {				\
    assert(ckt);						\
    assert((ckt)->CKTnomTemp == OPT::tnom_c + CONSTCtoK);	\
    assert(((ckt)->CKTcurrentAnalysis == DOING_DCOP) == CKT_BASE::_sim->command_is_op());	\
    assert(((ckt)->CKTcurrentAnalysis == DOING_TRCV) == CKT_BASE::_sim->command_is_dc());	\
    assert(((ckt)->CKTcurrentAnalysis == DOING_AC  ) == CKT_BASE::_sim->analysis_is_ac());	\
    assert(((ckt)->CKTcurrentAnalysis == DOING_TRAN) == CKT_BASE::_sim->analysis_is_tran());	\
    assert((ckt)->CKTbypass  == false);				\
    assert((ckt)->CKTabstol == OPT::abstol);			\
    assert((ckt)->CKTreltol == OPT::reltol);			\
    assert((ckt)->CKTvoltTol == OPT::vntol);			\
    assert((ckt)->CKTsrcFact == 1.);				\
    assert((ckt)->CKTdefaultMosL == OPT::defl);			\
    assert((ckt)->CKTdefaultMosW == OPT::defw);			\
    assert((ckt)->CKTdefaultMosAD == OPT::defad);		\
    assert((ckt)->CKTdefaultMosAS == OPT::defas);		\
  }

void MODEL_SPICE::init_ckt()
{
  assert(ckt());
  ckt()->CKTtime = _sim->_time0;
  ckt()->CKTtemp    = _sim->_temp_c + CONSTCtoK; //manage by update
  ckt()->CKTnomTemp = OPT::tnom_c + CONSTCtoK;
  ckt()->CKTintegrateMethod = 0; // disable
  if (_sim->command_is_op()) {
    ckt()->CKTcurrentAnalysis = DOING_DCOP;
  }else if (_sim->command_is_dc()) {
    ckt()->CKTcurrentAnalysis = DOING_TRCV;
  }else if (_sim->command_is_ac()) {
    ckt()->CKTcurrentAnalysis = DOING_AC;
  }else if (_sim->analysis_is_tran()) {
    ckt()->CKTcurrentAnalysis = DOING_TRAN;
  }else{ // probably probe
    ckt()->CKTcurrentAnalysis = 0;
  }    
  ckt()->CKTmode = 0; // wrong but safe
  ckt()->CKTbypass  = false; // manage this elsewhere
  ckt()->CKTabstol = OPT::abstol;
  ckt()->CKTreltol = OPT::reltol;
  ckt()->CKTvoltTol = OPT::vntol;
  ckt()->CKTgmin = OPT::gmin;
  ckt()->CKTsrcFact = 1.; // source stepping kluge
  ckt()->CKTdefaultMosL = OPT::defl;
  ckt()->CKTdefaultMosW = OPT::defw;
  ckt()->CKTdefaultMosAD = OPT::defad;
  ckt()->CKTdefaultMosAS = OPT::defas;
  ckt()->CKTfixLimit = false; // limiting kluge 1 == spice2
#ifndef JSPICE3
  ckt()->CKTbadMos3 = false; // 1 = spice2 compat
  ckt()->CKTsenInfo = NULL; // used as flag to print sens info
#endif
#ifdef NGSPICE_17
  ckt()->CKTdefaultMosM = 1.;
  ckt()->CKTcopyNodesets = false;
#endif
  assert_ckt_initialized(ckt());
}

#define assert_ckt_up_to_date(ckt) {				\
    assert_ckt_initialized(ckt);				\
    assert((ckt)->CKTtime == CKT_BASE::_sim->_time0);		\
    assert((ckt)->CKTtemp == CKT_BASE::_sim->_temp_c + CONSTCtoK);	\
  }

void DEV_SPICE::update_ckt()const
{
  assert_ckt_initialized(ckt());
  ckt()->CKTgmin = OPT::gmin;
  ckt()->CKTstat = NULL; // mark as not localized
  ckt()->CKTtime = _sim->_time0;
  ckt()->CKTdelta = NOT_VALID; // localized
  ckt()->CKTtemp = _sim->_temp_c + CONSTCtoK;
  ckt()->CKTmode = 0;
  ckt()->CKTomega = _sim->_jomega.imag();
  assert_ckt_up_to_date(ckt());
}

#define assert_ckt_localized(ckt) {				\
    assert_ckt_up_to_date(ckt);					\
    assert((ckt)->CKTstat);					\
    DEV_SPICE* d = reinterpret_cast<DEV_SPICE*>((ckt)->CKTstat);\
    assert(d);							\
    assert(dynamic_cast<DEV_SPICE*>(d));			\
    assert((ckt)->CKTdelta == d->_dt);				\
    if (d->_dt == 0) {untested();				\
      assert((ckt)->CKTag[0] == 0);				\
      assert((ckt)->CKTorder == 1);				\
    }else if (d->_time[1] != 0 && d->_method_a == mTRAP) {	\
      assert(conchk((ckt)->CKTag[0], 2 / d->_dt));		\
      assert((ckt)->CKTorder == 2);				\
    }else{							\
      assert(conchk((ckt)->CKTag[0], 1 / d->_dt));		\
      assert((ckt)->CKTorder == 1);				\
    }								\
    assert((ckt)->CKTag[0] == (ckt)->CKTag[0]);			\
    assert((ckt)->CKTrhs == d->_i0);				\
    assert((ckt)->CKTrhsOld == d->_v1);				\
    assert((ckt)->CKTirhs == d->_i1);				\
    assert((ckt)->CKTtimePoints == d->_time);			\
  }

void DEV_SPICE::localize_ckt()const
{
  assert_ckt_up_to_date(ckt());
  ckt()->CKTstat = reinterpret_cast<STATistics*>(const_cast<DEV_SPICE*>(this));
  assert(OPT::_keep_time_steps <= 8);
  for (int ii=0; ii<8; ++ii) {
    ckt()->CKTstates[ii] = _states[ii];
  }
  //assert(ckt()->CKTtime == _time[0]); //BUG// can fail in ac
  ckt()->CKTdelta = _dt;
  for (int ii=0; ii<OPT::_keep_time_steps-1; ++ii) {
    ckt()->CKTdeltaOld[ii] = _time[ii] - _time[ii+1];
  }
  assert(_dt == NOT_VALID || conchk(ckt()->CKTdelta, ckt()->CKTdeltaOld[0]));

  //ckt()->CKTag[0] = tr_c_to_g(1, ckt()->CKTag[0]);
  // defer fixing this -- GEAR not here
  if (_dt == 0) {untested();
    ckt()->CKTag[1] = ckt()->CKTag[0] = 0;
    ckt()->CKTorder = 1;
  }else if (_time[1] != 0 && _method_a == mTRAP) {
    ckt()->CKTag[0] = 2 / _dt;
    ckt()->CKTag[1] = 1;
    ckt()->CKTorder = 2;
  }else{
    ckt()->CKTag[0] =  1 / _dt;
    ckt()->CKTag[1] = -1 / _dt;
    ckt()->CKTorder = 1;
  }
  ckt()->CKTrhs = const_cast<double*>(_i0);
  ckt()->CKTrhsOld = const_cast<double*>(_v1);
  ckt()->CKTirhs = const_cast<double*>(_i1);
  ckt()->CKTmode = 0;
  ckt()->CKTtimePoints = const_cast<double*>(_time);
  assert_ckt_localized(ckt());
}

#define assert_model_raw() {				\
    assert(_spice_model._gen.GENmodType == 0);		\
    assert(_spice_model._gen.GENnextModel == NULL);	\
    assert(_spice_model._gen.GENinstances == NULL);	\
  }
#define assert_model_unlocalized() {		\
    assert(_model->_spice_model._gen.GENinstances == NULL);\
    assert(_spice_model);			\
    assert(_spice_model->_gen.GENmodType == 0);	\
    assert(_spice_model->_gen.GENnextModel == NULL);	\
    assert(_spice_model->_gen.GENinstances == NULL);	\
    assert(_spice_model->_gen.GENmodName);		\
  }
#define assert_model_localized() {			\
    assert(_spice_model);				\
    assert(_spice_model->_gen.GENmodType == 0);		\
    assert(_spice_model->_gen.GENnextModel == NULL);	\
    assert(_spice_model->_gen.GENinstances);		\
    assert(_spice_model->_gen.GENmodName);		\
  }
#define assert_instance() {			\
    assert(_spice_instance.GENnextInstance == NULL);	\
    assert(_spice_instance.GENname == NULL);		\
  }
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
struct IFVA {
  IFvalue* _v;
  int _type;
  IFVA(IFvalue* v, int t) :_v(v), _type(t) {assert(v);}

  void operator=(const std::string& s)
  {
    CS cmd(CS::_STRING, s);
    assert(_v);
    int datatype = _type;
    if (datatype & IF_SET) {
      if (datatype & IF_VECTOR) {untested();
	incomplete();
      }else{
      }
      switch (datatype & 0xff) {
      case IF_FLAG:	_v->iValue = true;	break;
      case IF_INTEGER:	cmd >> _v->iValue;	break;
      case IF_REAL:	cmd >> _v->rValue;	break;
      case IF_COMPLEX:untested();
	//cmd >> _v->cValue;
	incomplete();
	break;
      case IF_NODE:untested();	incomplete();	break;
      case IF_STRING:
	{
	  //assert(!(_v->sValue));
	  //BUG//memory leak -- this is never deleted
	  _v->sValue = new char[s.length()+1];
	  strcpy(_v->sValue, s.c_str());
	  break;
	}
      case IF_INSTANCE: untested(); incomplete();	break;
      case IF_PARSETREE:untested(); incomplete();	break;
      default:		unreachable();			break;
      }
    }else{untested();
    }
  }
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
MODEL_SPICE::MODEL_SPICE(const DEV_SPICE* p) 
  :MODEL_CARD(p),
   _spice_model(),
   _key(),
   _level(),
   _params()
{
  assert_model_raw();
}
/*--------------------------------------------------------------------------*/
MODEL_SPICE::MODEL_SPICE(const MODEL_SPICE& p)
  :MODEL_CARD(p),
   _spice_model(p._spice_model),
   _key(p._key),
   _level(p._level),
   _params(p._params)
{
  assert_model_raw();
}
/*--------------------------------------------------------------------------*/
MODEL_SPICE::~MODEL_SPICE()
{
  --_count;
}
/*--------------------------------------------------------------------------*/
void MODEL_SPICE::Set_param_by_name(std::string Name, std::string new_value)
{
  assert_model_raw();
  assert(info.DEVpublic.numModelParms);
  assert(info.DEVpublic.modelParms);
  assert(info.DEVmodParam);

  int num_params = *(info.DEVpublic.numModelParms);
  for (int i = 0; i < num_params; ++i) {
    IFparm Parms = info.DEVpublic.modelParms[i];
    if (Name == Parms.keyword) {
      IFvalue Value;
      IFVA v(&Value, Parms.dataType);
      v = new_value;
      int ok = info.DEVmodParam(Parms.id, &Value, &_spice_model._gen);
      assert(ok == OK);
      return;
    }else{
    }
  }
  if (Name != "level") {untested();
    throw Exception_No_Match(Name);
  }else{
  }
}
/*--------------------------------------------------------------------------*/
void MODEL_SPICE::set_param_by_name(std::string Name, std::string Value)
{
  if (OPT::case_insensitive) {
    notstd::to_lower(&Name);
  }else{
  }
  _params.set(Name, Value);
  Set_param_by_name(Name, to_string(_params[Name].e_val(1,scope())));
}
/*--------------------------------------------------------------------------*/
void MODEL_SPICE::precalc_first()
{
  MODEL_CARD::precalc_first();

  Set_param_by_name(_key, "1");

  // push down parameters into raw spice data
  for (PARAM_LIST::iterator i = _params.begin(); i != _params.end(); ++i) {
    if (i->second.has_hard_value()) {
      try {
	Set_param_by_name(i->first, to_string(i->second.e_val(1,scope())));
      }catch (Exception_No_Match&) {
	error(bTRACE, long_label() + ": bad parameter: " + i->first + ", ignoring\n");
      }
    }else{
    }
  }

  init_ckt();
  if (info.DEVsetup) {
    assert_model_raw();
    int ok = info.DEVsetup(NULL, &_spice_model._gen, ckt(), NULL);
    assert(ok == OK);
  }else{untested();
  }
}
/*--------------------------------------------------------------------------*/
void MODEL_SPICE::set_dev_type(const std::string& new_type)
{
  assert_model_raw();

  //_spice_model._gen.set_mod_name(short_label());
  std::string s = short_label();
  char* p = new char[s.length()+1]; //BUG//memory leak
  s.copy(p, std::string::npos);
  p[s.length()] = '\0';
  _spice_model._gen.GENmodName = p;

  _key = new_type;
  if (OPT::case_insensitive) {
    notstd::to_lower(&_key);
  }else{
  }
}
/*--------------------------------------------------------------------------*/
bool MODEL_SPICE::param_is_printable(int i)const
{
  assert(i < MODEL_SPICE::param_count());
  if (i >= MODEL_CARD::param_count()) {
    return _params.is_printable(MODEL_SPICE::param_count() - 1 - i);
  }else{
    return MODEL_CARD::param_is_printable(i);
  }
}
/*--------------------------------------------------------------------------*/
std::string MODEL_SPICE::param_name(int i)const
{
  assert(i < MODEL_SPICE::param_count());
  if (i >= MODEL_CARD::param_count()) {
    return _params.name(MODEL_SPICE::param_count() - 1 - i);
  }else{
    return MODEL_CARD::param_name(i);
  }
}
/*--------------------------------------------------------------------------*/
std::string MODEL_SPICE::param_name(int i, int j)const
{untested();
  assert(i < MODEL_SPICE::param_count());
  if (j == 0) {untested();
    return param_name(i);
  }else if (i >= MODEL_CARD::param_count()) {untested();
    return "";
  }else{untested();
    return MODEL_CARD::param_name(i);
  }
}
/*--------------------------------------------------------------------------*/
std::string MODEL_SPICE::param_value(int i)const
{
  assert(i < MODEL_SPICE::param_count());
  if (i >= MODEL_CARD::param_count()) {
    return _params.value(MODEL_SPICE::param_count() - 1 - i);
  }else{
    return MODEL_CARD::param_value(i);
  }
}
/*--------------------------------------------------------------------------*/
void MODEL_SPICE::set_param_by_index(int, std::string&, int)
{untested();
  unreachable();
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
DEV_SPICE::DEV_SPICE()
  :STORAGE(),
   _inst(),
   _modelname(""),
   _model(NULL),
   _spice_model(NULL),
   _nodes(),
   _matrix(),
   _matrix_core(),
   _i0(),
   _i1(),
   _v1(),
   _states_1(NULL),
   _num_states(0),
   _maxEqNum(0)
{
  attach_common(&Default_Params);
  std::fill_n(&_inst_space, sizeof(INSTANCE), '\0');
  assert_instance();

  {
    int* node = spice_nodes();
    for (int ii = 0; ii < matrix_nodes(); ++ii) {
      node[ii] = SPICE_INVALID_NODE;
    }
  }
  _n = _nodes;
  for (int ii = 0; ii < matrix_nodes(); ++ii) {
    assert(!(_n[ii].n_()));
  }

  for (int ii = 0; ii < matrix_nodes()+OFFSET; ++ii) {
    _matrix[ii] = _matrix_core[ii];
    assert(_matrix[ii]);
  }

  assert(OPT::_keep_time_steps <= 8);
  for (int ii=0; ii<8; ++ii) {
    _states[ii] = NULL;
  }

  ++_count;
  assert_instance();
}
/*--------------------------------------------------------------------------*/
DEV_SPICE::DEV_SPICE(const DEV_SPICE& p)
  :STORAGE(p),
   _inst(p._inst),
   _modelname(p._modelname),
   _model(p._model),
   _spice_model(p._spice_model),
   _nodes(),
   _matrix(),
   _matrix_core(),
   _i0(),
   _i1(),
   _v1(),
   _states_1(NULL),
   _num_states(p._num_states),
   _maxEqNum(p._maxEqNum)
{
  assert_instance();

  {
    int* node = spice_nodes();
    for (int ii = 0; ii < matrix_nodes(); ++ii) {
      assert(node[ii] == SPICE_INVALID_NODE);
    }
  }
  _n = _nodes;
  for (int ii = 0; ii < matrix_nodes(); ++ii) {
    _n[ii] = p._n[ii];
  }

  for (int ii = 0; ii < matrix_nodes()+OFFSET; ++ii) {
    _matrix[ii] = _matrix_core[ii];
    assert(_matrix[ii]);
  }

  assert(OPT::_keep_time_steps <= 8);
  for (int ii=0; ii<8; ++ii) {
    _states[ii] = NULL;
  }

  ++_count;
  assert_instance();
}
/*--------------------------------------------------------------------------*/
DEV_SPICE::~DEV_SPICE()
{
  assert_instance();

  --_count;
  
  if (_states[0]) {
    // regular instances
    for (int ii=0; ii<OPT::_keep_time_steps; ++ii) {
      assert(_states[ii]);
      delete [] _states[ii];
    }
    assert(_states_1);
    delete [] _states_1;
  }else{
    // prototype
    assert(OPT::_keep_time_steps <= 8);
    for (int ii=0; ii<8; ++ii) {
      assert(!_states[ii]);
    }
    assert(!_states_1);
    assert(!_spice_model);
  }
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::set_dev_type(const std::string& new_type)
{
  _modelname = new_type;
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::Set_param_by_name(std::string Name, std::string new_value)
{
  assert_instance();
  assert(info.DEVpublic.numInstanceParms);
  assert(info.DEVpublic.instanceParms);
  assert(info.DEVparam);

  int num_params = *(info.DEVpublic.numInstanceParms);
  for (int i = 0; i < num_params; ++i) {
    IFparm Parms = info.DEVpublic.instanceParms[i];
    if (Name == Parms.keyword) {
      Set_param_by_index(i, new_value, 0);
      return;
    }else{
    }
  }
  mutable_common()->COMMON_COMPONENT::Set_param_by_name(Name, new_value);
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::set_param_by_name(std::string Name, std::string Value)
{
  if (OPT::case_insensitive) {
    notstd::to_lower(&Name);
  }else{
  }
  COMPONENT::set_param_by_name(Name, Value);
  COMMON_PARAMLIST* c = dynamic_cast<COMMON_PARAMLIST*>(mutable_common());
  assert(c);
  Set_param_by_name(Name, to_string(c->_params[Name].e_val(1,scope())));
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::Set_param_by_index(int i, std::string& new_value, int offset)
{
  assert_instance();
  assert(info.DEVpublic.numInstanceParms);
  assert(info.DEVpublic.instanceParms);
  assert(info.DEVparam);

  int num_params = *(info.DEVpublic.numInstanceParms);
  if (i < num_params) {
    IFparm Parms = info.DEVpublic.instanceParms[i];
    IFvalue Value;
    IFVA v(&Value, Parms.dataType);
    v = new_value;
#ifdef JSPICE3
    int ok = info.DEVparam(ckt(), Parms.id, &Value, &_spice_instance, NULL);
#else
    int ok = info.DEVparam(Parms.id, &Value, &_spice_instance, NULL);
#endif
    assert(ok == OK);
  }else{untested();
    STORAGE::set_param_by_index(i-num_params, new_value, offset+num_params);
  }
  assert_instance();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::expand()
{
  assert_instance();
  assert(info.DEVsetup);

  STORAGE::expand();

  init_ckt();
  
  { //-------- fix up external nodes
    int* node = spice_nodes();
    for (int ii = 0; ii < net_nodes(); ++ii) {
      node[ii] = ii+OFFSET;
    }
    if (UNCONNECTED_NODES == uGROUND) {
      for (int ii = net_nodes(); ii < max_nodes(); ++ii) {itested();
	node[ii] = ii+OFFSET;
      }
    }else if (UNCONNECTED_NODES == uFLOAT) {
      for (int ii = net_nodes(); ii < max_nodes(); ++ii) {untested();
	node[ii] = SPICE_UNCONNECTED_NODE;
      }
    }else{
      assert(UNCONNECTED_NODES == uDISALLOW);
      assert(min_nodes() == max_nodes());
      assert(net_nodes() == max_nodes());
    }
    ckt()->CKTmaxEqNum = max_nodes();

    for (int ii = max_nodes(); ii < matrix_nodes(); ++ii) {
      node[ii] = 0;
    }
  }

  { //------- attach model, set up matrix pointers
    _model = dynamic_cast<const MODEL_SPICE*>(find_model(_modelname));
    if (!_model) {
      throw Exception_Model_Type_Mismatch(long_label(), _modelname, DEVICE_TYPE);
    }else{
      SMPmatrix* matrix = reinterpret_cast<SMPmatrix*>(_matrix);
      _num_states = 0;

      _spice_instance.GENmodPtr = &(_model->_spice_model._gen);
      _spice_model = &(_model->_spice_model);
      SPICE_MODEL_DATA spice_model_copy(*_spice_model);
      spice_model_copy._gen.GENinstances = &_spice_instance;
      //-------------
      int ok = info.DEVsetup(matrix, &(spice_model_copy._gen), ckt(), &_num_states);
      // memory pointer setup, and sets _num_states
      // undesired side effects: sets values, messes up model
      //-------------
      assert(ok == OK);
      _maxEqNum = ckt()->CKTmaxEqNum;
      trace1("expand", ckt()->CKTmaxEqNum);
      assert_model_unlocalized();      
    }
  }

  //-------- allocate state vectors
  if (!_states[0]) {
    for (int ii=0; ii<OPT::_keep_time_steps; ++ii) {
      assert(!_states[ii]);
      _states[ii] = new double[_num_states];
    }
    _states_1 = new double[_num_states];
  }else{
  }
  for (int ii=0; ii<OPT::_keep_time_steps; ++ii) {
    assert(_states[ii]);
    std::fill_n(_states[ii], _num_states, 0);
  }
  assert(_states_1);
  std::fill_n(_states_1, _num_states, 0);
  
  //std::fill_n(_i1, matrix_nodes()+OFFSET, 0);
  //std::fill_n(_v1, matrix_nodes()+OFFSET, 0);
  
  //-------- fix up internal nodes
  if (_sim->is_first_expand()) {
    int start_internal = 0;
    if (UNCONNECTED_NODES == uGROUND) {
      for (int ii = net_nodes(); ii < max_nodes(); ++ii) {itested();
	_n[ii].set_to_ground(this);
      }
      start_internal = max_nodes();
    }else{
      assert(UNCONNECTED_NODES == uDISALLOW || UNCONNECTED_NODES == uFLOAT);
      start_internal = net_nodes();
    }
    assert(start_internal != 0);

    int* node = spice_nodes(); // treat as array
    char fake_name[] = "a";
    for (int ii = start_internal; ii < matrix_nodes(); ++ii) {
      if (node[ii] >= start_internal+OFFSET) {
	// real internal node
	_n[ii].new_model_node('.' + long_label() + '.' + fake_name, this);
	trace1("new int", node[ii]);
	assert(_n[ii].n_());
      }else if (node[ii] >= 0+OFFSET) {
	// collapsed to an external node
	_n[ii] = _n[node[ii]-OFFSET];
	trace1("collapse", node[ii]);
	assert(_n[ii].n_());
      }else{
	// not assigned
	trace1("not used", node[ii]);
	assert(!_n[ii].n_());
      }
      ++(*fake_name);
    }
    
    for (int ii = 0; ii < matrix_nodes(); ++ii) {
      trace2((_n[ii].n_()) ? (_n[ii].n_()->short_label().c_str()) : ("NULL"), ii, node[ii]);
    }
    
    // This could be one loop, but doing it this way gives more info.
    for (int ii = 0; ii < min_nodes(); ++ii) {
      assert(_n[ii].n_());
    }
    for (int ii = min_nodes(); ii < net_nodes(); ++ii) {
      assert(_n[ii].n_());
    }
    for (int ii = net_nodes(); ii < max_nodes(); ++ii) {itested();
      //assert(_n[ii].n_());
    }
    for (int ii = max_nodes(); ii < matrix_nodes(); ++ii) {
      assert(_n[ii].n_() || !node[ii]);
    }
  }else{untested();
  }
  assert_model_unlocalized();
  assert_instance();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::precalc_last()
{
  assert(_model);
  assert_instance();
  assert(info.DEVsetup);

  STORAGE::precalc_last();
  init_ckt();

  // push down parameters into spice data
  COMMON_PARAMLIST* c = dynamic_cast<COMMON_PARAMLIST*>(mutable_common());
  assert(c);
  for (PARAM_LIST::iterator i = c->_params.begin(); i != c->_params.end(); ++i) {
    if (i->second.has_hard_value()) {
      try {
	Set_param_by_name(i->first, to_string(i->second.e_val(1,scope())));
      }catch (Exception_No_Match&) {
	error(bTRACE, long_label() + ": bad parameter: " + i->first + ", ignoring\n");
      }
    }else{
    }
  }

  int* node = spice_nodes(); // treat as array	//
  int  node_stash[MATRIX_NODES];			//
  notstd::copy_n(node, matrix_nodes(), node_stash);	// save the real nodes
  
  { //-------- fix up external nodes, again ........
    // put the originals back, so DEVsetup can mess them up the same as last time
    int* node = spice_nodes();
    for (int ii = 0; ii < net_nodes(); ++ii) {
      node[ii] = ii+OFFSET;
    }
    if (UNCONNECTED_NODES == uGROUND) {
      for (int ii = net_nodes(); ii < max_nodes(); ++ii) {itested();
	node[ii] = ii+OFFSET;
      }
    }else if (UNCONNECTED_NODES == uFLOAT) {
      for (int ii = net_nodes(); ii < max_nodes(); ++ii) {untested();
	node[ii] = SPICE_UNCONNECTED_NODE;
      }
    }else{
      assert(UNCONNECTED_NODES == uDISALLOW);
      assert(min_nodes() == max_nodes());
      assert(net_nodes() == max_nodes());
    }
    ckt()->CKTmaxEqNum = max_nodes();

    for (int ii = max_nodes(); ii < matrix_nodes(); ++ii) {
      node[ii] = 0;
    }
  }
  
  {
    SMPmatrix* matrix = reinterpret_cast<SMPmatrix*>(_matrix);
    int num_states_garbage = 0;

    assert(_spice_model == &(_model->_spice_model));
    SPICE_MODEL_DATA spice_model_copy(*_spice_model);
    spice_model_copy._gen.GENinstances = &_spice_instance;

    int ok = info.DEVsetup(matrix, &(spice_model_copy._gen), ckt(), &num_states_garbage);

    assert(ok == OK);
    assert(num_states_garbage == _num_states);
    trace3("precalc", _maxEqNum, ckt()->CKTmaxEqNum, (_maxEqNum == ckt()->CKTmaxEqNum));
    assert(_maxEqNum == ckt()->CKTmaxEqNum);
    notstd::copy_n(node_stash, matrix_nodes(), node); // put back real nodes
    // hopefully, the matrix pointers are the same as last time!
  }
  assert(!is_constant());
  assert_model_unlocalized();
  assert_instance();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::internal_precalc()
{
  update_ckt();

  if (info.DEVtemperature) {
    assert_instance();

    assert_model_unlocalized();
    _spice_model->_gen.GENinstances = &_spice_instance;
    assert_model_localized();
    
    // ELEMENT::precalc(); .. don't call .. more analysis needed
    //-----
    int ok = info.DEVtemperature(&(_spice_model->_gen), ckt());
    assert(ok == OK);
    //-----
    set_converged();
    _spice_model->_gen.GENinstances = NULL;

    assert(!is_constant());
    assert_instance();
  }else{
  }
  assert_model_unlocalized();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::tr_advance()
{
  STORAGE::tr_advance();
  update_ckt();
  
  double* t = _states[OPT::_keep_time_steps-1];
  for (int ii = OPT::_keep_time_steps-1;  ii > 0;  --ii) {
    _states[ii] = _states[ii-1];
  }
  _states[0] = t;
  notstd::copy_n(_states[1], _num_states, _states[0]);
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::tr_regress()
{
  ELEMENT::tr_regress();
  update_ckt();
}
/*--------------------------------------------------------------------------*/
bool DEV_SPICE::tr_needs_eval()const
{
  if (is_q_for_eval()) {
    return false;
  }else if (!converged()) {
    return true;
  }else if (_sim->is_advance_iteration()) {
    return true;
  }else if (_time[1] == 0) {
    //BUG// needed for ngspice jfet, but not for spice3f5 jfet
    return true;
  }else{
    int* node = spice_nodes();
    // check the node voltages, reference to ground
    for (int ii=0; ii<matrix_nodes(); ++ii) {
      if ((node[ii] != SPICE_INVALID_NODE) 
	  && !conchk(_v1[node[ii]], _n[ii].v0(), 0, OPT::reltol*OPT::bypasstol)) {
	return true;
      }else{
      }
    }
    // check the node voltages, reference to each other
    for (int ii=0; ii<matrix_nodes(); ++ii) {
      for (int jj=0; jj<ii; ++jj) {
	if ((node[ii] != SPICE_INVALID_NODE) && (node[jj] != SPICE_INVALID_NODE) 
	    && !conchk((_v1[node[ii]] - _v1[node[jj]]),
		       (_n[ii].v0() - _n[jj].v0()),
		       0, OPT::reltol*OPT::bypasstol)) {
	  return true;
	}else{
	}
      }
    }
    return false;
  }
}
/*--------------------------------------------------------------------------*/
// MODEINITFLOAT = normal iteration
// MODEINITPRED  = 1st iter at a new time point
// MODEINITTRAN  = 1st iter at 1st time pt after initial DC
// MODEINITFIX   = like FLOAT, but honor options like "off"
// MODEINITJCT   = initial guess
// MODEINITSMSIG = like FLOAT, but setup for small signal, don't load arrays
/*--------------------------------------------------------------------------*/
bool DEV_SPICE::do_tr()
{
  assert_instance();
  assert(info.DEVload);
  assert(_num_states >= 0);
  
  localize_ckt();

  assert_model_unlocalized();
  _spice_model->_gen.GENinstances = &_spice_instance;
  assert_model_localized();

  if (_sim->analysis_is_tran_dynamic()) {
    if ((_time[1] == 0) && _sim->is_first_iteration()) {
      ckt()->CKTmode = MODETRAN | MODEINITTRAN;
    }else{
      ckt()->CKTmode = MODETRAN | MODEINITFLOAT;
    }
  }else{
    if (_sim->analysis_is_tran_static()) {
      ckt()->CKTmode = MODETRANOP;
    }else if (_sim->analysis_is_tran_restore()) {
      ckt()->CKTmode = MODETRAN;
    }else if (_sim->command_is_dc()) {
      ckt()->CKTmode = MODEDCTRANCURVE;
    }else if (_sim->command_is_op()) {
      ckt()->CKTmode = MODEDCOP;
    }else{unreachable();
      ckt()->CKTmode = 0;
    }
    if (_sim->uic_now()) {
      ckt()->CKTmode |= MODEINITFIX;
      ckt()->CKTmode |= MODEUIC;
    }else if (_sim->is_initial_step()) {
      ckt()->CKTmode |= MODEINITJCT;
    }else{
      ckt()->CKTmode |= MODEINITFLOAT;
    }
  }

  { // copy in
    int* node = spice_nodes();
    assert(ckt()->CKTrhsOld == _v1);
    std::fill_n(_v1, matrix_nodes()+OFFSET, 0);
    for (int ii = 0; ii < matrix_nodes(); ++ii) {
      if (node[ii] != SPICE_INVALID_NODE) {
	_v1[node[ii]] = _n[ii].v0();
      }else{
      }
    }
  }
  { // clear for copy out
    ckt()->CKTtroubleElt = NULL;
    ckt()->CKTnoncon = 0;
    
    assert(ckt()->CKTrhs == _i0);
    std::fill_n(_i0, matrix_nodes()+OFFSET, 0);

    for (int ii = 0; ii < matrix_nodes()+OFFSET; ++ii) {
      for (int jj = 0; jj < matrix_nodes()+OFFSET; ++jj) {
	_matrix[ii][jj].first = 0.;
      }
    }
  }

  // do the work -- it might also do convergence checking, might not
  //-----
  info.DEVload(&(_spice_model->_gen), ckt());
  //-----
  // convergence check -- gnucap method
  set_converged(ckt()->CKTnoncon == 0);
  for (int ii = 0; ii < _num_states; ++ii) {
    set_converged(converged() && conchk(_states[0][ii], _states_1[ii]));
    trace3("", ii, _states_1[ii], _states[0][ii]);
    _states_1[ii] = _states[0][ii];
  }
  for (int ii = 0; converged() && ii < matrix_nodes()+OFFSET; ++ii) {
    set_converged(conchk(_i0[ii], _i1[ii]));
  }
  for (int ii = 0; converged() && ii < matrix_nodes()+OFFSET; ++ii) {
    for (int jj = 0; converged() && jj < matrix_nodes()+OFFSET; ++jj) {
      set_converged(conchk(_matrix[ii][jj].first, _matrix[ii][jj].second));
    }
  }

  // convergence check -- Spice method 
  // not sure if it is worth the effort
  if (converged() && info.DEVconvTest) {
    ckt()->CKTnoncon = 0;
    ckt()->CKTrhs = _v1;    // Spice overlaps _i0 with _v1 as CKTrhs
    info.DEVconvTest(&(_spice_model->_gen), ckt());
    set_converged(ckt()->CKTnoncon == 0);
  }else{
    // either no separate test or already failed
  }

  bool needs_load = !converged();
  for (int ii = 0; !needs_load && ii < matrix_nodes()+OFFSET; ++ii) {
    needs_load = !conchk(_i0[ii], _i1[ii], 0, OPT::reltol*OPT::loadtol);
  }
  for (int ii = 0; !needs_load && ii < matrix_nodes()+OFFSET; ++ii) {
    for (int jj = 0; !needs_load && jj < matrix_nodes()+OFFSET; ++jj) {
      needs_load = !conchk(_matrix[ii][jj].first, _matrix[ii][jj].second,
			   0, OPT::reltol*OPT::loadtol);
    }
  }
  
  if (needs_load) {
    q_load();
  }else{
  }

  assert_model_localized();
  _spice_model->_gen.GENinstances = NULL;
  assert_model_unlocalized();
  return converged();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::tr_load()
{
#ifndef NDEBUG
  if (_loaditer == _sim->iteration_tag()) {untested();
    error(bDANGER, long_label() + " internal error: double load\n");
  }
  _loaditer = _sim->iteration_tag();
#endif

  int ihit[MATRIX_NODES+OFFSET];
  int jhit[MATRIX_NODES+OFFSET];

  std::fill_n(ihit, matrix_nodes()+OFFSET, 0);
  std::fill_n(jhit, matrix_nodes()+OFFSET, 0);

  int* node = spice_nodes();
  for (int ii = 0; ii < matrix_nodes(); ++ii) {
    int ni = node[ii];
    if (ni && !ihit[ni]) {
      ihit[ni] = 1;
      int nii = ni-OFFSET;
      trace4("", ii, ni, _i0[ni], _i1[ni]);
      tr_load_source_point(_n[ii], &(_i0[ni]), &(_i1[ni]));
      for (int jj = 0; jj < matrix_nodes(); ++jj) {
	int nj = node[jj];
	if (nj && jhit[nj] != ni) {
	  jhit[nj] = ni;
	  int njj = nj-OFFSET;
	  trace2("", jj, nj);
	  trace2("", _matrix[nii][njj].first, _matrix[nii][njj].second);
	  tr_load_point(_n[ii], _n[jj], &(_matrix[nii][njj].first), &(_matrix[nii][njj].second));
	}else{
	  trace2("skip", jj, nj);
	}
      }
    }else{
      trace2("=========skip", ii, ni);
    }
  }
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::tr_unload()
{untested();incomplete();

  for (int ii = 0; ii < matrix_nodes(); ++ii) {untested();
    for (int jj = 0; jj < matrix_nodes(); ++jj) {untested();
      _matrix[ii][jj].first = 0.;
    }
  }
  _sim->mark_inc_mode_bad();
  tr_load();
}
/*--------------------------------------------------------------------------*/
TIME_PAIR DEV_SPICE::tr_review()
{
  // not calling STORAGE::tr_review();

  if (info.DEVtrunc) {
    localize_ckt();
    assert_instance();
    //q_accept();
    
    assert_model_unlocalized();
    _spice_model->_gen.GENinstances = &_spice_instance;
    assert_model_localized();
    
    ckt()->CKTtroubleElt = NULL;
    double timestep = NEVER;
    //-----
    info.DEVtrunc(&(_spice_model->_gen), ckt(), &timestep);
    //-----
    
    _time_by._error_estimate = tr_review_check_and_convert(timestep);
    _time_by._event = NEVER;

    _spice_model->_gen.GENinstances = NULL;
    assert_model_unlocalized();
    return _time_by;
  }else{
    return TIME_PAIR(NEVER,NEVER);
  }
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::tr_accept()
{
  assert_model_unlocalized();
  _spice_model->_gen.GENinstances = &_spice_instance;
  assert_model_localized();
  
  //STORAGE::tr_accept(); // doesn't do anything

  if (_sim->analysis_is_dcop() || _sim->analysis_is_ac()) {
    localize_ckt();

    // don't copy in
    assert(ckt()->CKTrhsOld == _v1);
    // _v1 already has correct values
    // _n[ii].v0() is not correct -- may have been cleared
    
    ckt()->CKTmode = MODEINITSMSIG;
    info.DEVload(&(_spice_model->_gen), ckt());
  }else{itested();
  }
  assert_model_localized();
  _spice_model->_gen.GENinstances = NULL;
  assert_model_unlocalized();
}
/*--------------------------------------------------------------------------*/
double DEV_SPICE::tr_probe_num(const std::string& x)const
{
  localize_ckt();
  assert_ckt_up_to_date(ckt());
  assert_instance();

  // all of the "states" in state array
  int num_probe_states = std::min(_num_states, int(sizeof(state_names)/sizeof(std::string)));
  for (int ii=0; ii<num_probe_states && state_names[ii]!=""; ++ii) {
    if (Umatch(x, state_names[ii] + ' ')) {
      return _states[0][ii];
    }else{
    }
  }

  if (info.DEVask) {
    // data that Spice has, through "ask"
    assert(info.DEVpublic.numInstanceParms);
    assert(info.DEVpublic.instanceParms);

    for (int ii=0; ii<(*(info.DEVpublic.numInstanceParms)); ++ii) {
      IFparm Parms = info.DEVpublic.instanceParms[ii];
      int datatype = Parms.dataType;
      if (datatype & IF_ASK && Umatch(x, std::string(Parms.keyword) + ' ')) {
	IFvalue v;
	int ok = info.DEVask(ckt(), &_spice_instance, Parms.id, &v, NULL);
	if (ok == OK) {
	  switch (datatype & 0xff) {
	  case IF_FLAG:
	  case IF_INTEGER:
	    return v.iValue;
	  case IF_REAL:
	    return v.rValue;
	  case IF_COMPLEX:
	  case IF_STRING:
	  default:
	    // make believe it is not a match
	    incomplete();
	    break; // break switch, continue loop
	  }
	}else{untested();
	  // match, but not useful here.
	  assert(errMsg);
	  free(errMsg);
	  errMsg = NULL;
	  assert(errRtn);
	  errRtn = NULL;
	  // maybe there is more than one match, so continue loop
	}
      }else{
	// really not a match, keep looking
      }
    }
  }else{untested();
    // no DEVask ..  can't do anything.
  }
  return STORAGE::tr_probe_num(x);
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::ac_begin()
{
  STORAGE::ac_begin();
  internal_precalc();
  tr_accept();
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::do_ac()
{
  if (info.DEVacLoad || info.DEVpzLoad) {
    assert_instance();
    assert(_num_states >= 0);

    assert_model_unlocalized();
    _spice_model->_gen.GENinstances = &_spice_instance;
    assert_model_localized();

    localize_ckt();
    ckt()->CKTmode = MODEAC;
    ckt()->CKTomega = _sim->_jomega.imag();

    // clear for copy out
    ckt()->CKTtroubleElt = NULL;
    std::fill_n(_i0, matrix_nodes()+OFFSET, 0);
    std::fill_n(_i1, matrix_nodes()+OFFSET, 0);
    for (int ii = 0; ii < matrix_nodes()+OFFSET; ++ii) {
      for (int jj = 0; jj < matrix_nodes()+OFFSET; ++jj) {
	_matrix[ii][jj] = DPAIR(0.,0.);
      }
    }
    
    if (info.DEVpzLoad) {
      info.DEVpzLoad(&(_spice_model->_gen), ckt(), reinterpret_cast<SPcomplex*>(&_sim->_jomega));
    }else if (info.DEVacLoad) {
      info.DEVacLoad(&(_spice_model->_gen), ckt());
    }else{unreachable();
      // nothing
    }

    assert_model_localized();
    _spice_model->_gen.GENinstances = NULL;
    assert_model_unlocalized();
  }else{untested();
    // there is no acLoad function
  }
}
/*--------------------------------------------------------------------------*/
void DEV_SPICE::ac_load()
{
  if (info.DEVacLoad) {
    assert_ckt_up_to_date(ckt());
    
    int ihit[MATRIX_NODES+OFFSET];
    int jhit[MATRIX_NODES+OFFSET];
    
    std::fill_n(ihit, matrix_nodes()+OFFSET, 0);
    std::fill_n(jhit, matrix_nodes()+OFFSET, 0);
    
    int* node = spice_nodes();
    for (int ii = 0; ii < matrix_nodes(); ++ii) {
      int ni = node[ii];
      if (ni && !ihit[ni]) {
	ihit[ni] = 1;
	int nii = ni-OFFSET;
	trace3("", ii, ni, nii);
	ac_load_source_point(_n[ii], COMPLEX(_i0[ni], _i1[ni]));
	for (int jj = 0; jj < matrix_nodes(); ++jj) {
	  int nj = node[jj];
	  if (nj && jhit[nj] != ni) {
	    jhit[nj] = ni;
	    int njj = nj-OFFSET;
	    trace3("", jj, nj, njj);
	    trace2("", _matrix[nii][njj].first, _matrix[nii][njj].second);
	    //ac_load_point(_n[ii], _n[jj], _matrix[nii][njj]);
	    DPAIR& dp = _matrix[nii][njj];
	    ac_load_point(_n[ii], _n[jj], COMPLEX(dp.first, dp.second));
	  }else{
	    trace2("skip", jj, nj);
	  }
	}
      }else{
	trace2("=========skip", ii, ni);
      }
    }
  }else{
    // there is no acLoad function
  }
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
extern "C" {
  // needed to satisfy references.  Supposedly unreachable.  Stubs.
  char *errMsg = NULL;
  char *errRtn = NULL;
  char* tmalloc(int size) {itested(); return static_cast<char*>(calloc(size,1));}
  char* trealloc(char*, int) {untested();incomplete(); return NULL;} //DEVnoise
  void txfree(char *ptr) {
    if (ptr) {itested();
      free(ptr);
    }else{untested();
    }
  }
 
  static class FT_CURCKT : public circ {
    TSKtask junk;
  public:
    FT_CURCKT() {
      junk.jobs = NULL;
      ci_curTask = reinterpret_cast<char*>(&junk);
      //::ft_curckt = this;
    }
  } stupid_ft_circ_pointer_to_pointer_hack;

  circ *ft_curckt = &stupid_ft_circ_pointer_to_pointer_hack;

  IFuid CKTnodName(CKTcircuit*,int) {incomplete();return IFuid();} //DEVsenPrint

  double D1i2F1(double, double, double) {incomplete(); return NOT_VALID;} //DEVdisto
  double D1i3F1(double, double, double, double, double, double) {incomplete(); return NOT_VALID;}
  double D1iF12(double, double, double, double, double) {incomplete(); return NOT_VALID;}
  double D1i2F12(double, double, double, double, double, double, double, double, double,
		 double) {incomplete(); return NOT_VALID;}
  double D1n2F1(double, double, double) {incomplete(); return NOT_VALID;}
  double D1n3F1(double, double, double, double, double, double) {incomplete(); return NOT_VALID;}
  double D1nF12(double, double, double, double, double) {incomplete(); return NOT_VALID;}
  double D1n2F12(double, double, double, double, double, double, double, double, double,
		 double) {incomplete(); return NOT_VALID;}
  double DFn2F1(double, double, double, double, double, double, double, double, double,
		double, double, double) {incomplete(); return NOT_VALID;}
  double DFi2F1(double, double, double, double, double, double, double, double, double,
		double, double, double) {incomplete(); return NOT_VALID;}
  double DFi3F1(double, double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double, double,
		double) {incomplete(); return NOT_VALID;}
  double DFn3F1(double, double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double, double,
		double) {incomplete(); return NOT_VALID;}
  double DFnF12(double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double,
		double, double) {incomplete(); return NOT_VALID;}
  double DFiF12(double, double, double, double, double, double, double, double,
		double, double, double, double, double, double, double, double,
		double, double) {incomplete(); return NOT_VALID;}
  struct DpassStr; //DEVdisto
  double DFn2F12(DpassStr*) {incomplete(); return NOT_VALID;}
  double DFi2F12(DpassStr*) {incomplete(); return NOT_VALID;}
  struct Dderivs;
  void AtanDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void CosDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void CubeDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void DivDeriv(Dderivs*, Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void EqualDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void ExpDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void InvDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void MultDeriv(Dderivs*, Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void PlusDeriv(Dderivs*, Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void PowDeriv(Dderivs*, Dderivs*, double) {incomplete();} //DEVdisto
  void SqrtDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void TanDeriv(Dderivs*, Dderivs*) {incomplete();} //DEVdisto
  void TimesDeriv(Dderivs*, Dderivs*, double) {incomplete();} //DEVdisto
#ifdef JSPICE3
  double Nintegrate(double, double, double, GENERIC*) {incomplete(); return NOT_VALID;} //DEVnoise
#else
  double Nintegrate(double, double, double, Ndata*) {incomplete(); return NOT_VALID;} //DEVnoise
#endif
  void NevalSrc(double*, double*, CKTcircuit*, int, int, int, double) {incomplete();} //DEVnoise
  void NevalSrc2(double*, double*, CKTcircuit*, int, int, int, double, double) {incomplete();}
  //------------------------------------------------
  // should be constants, but spice wants them to be variables.
  double CONSTroot2(sqrt(2.));
  double CONSTvt0(P_CELSIUS0*P_K_Q);
  double CONSTKoverQ(P_K_Q);
  double CONSTe(M_E);
  //------------------------------------------------
  // ngspice baggage
  int ARCHme = 0;
  // jspice baggage
  IFsimulator *ft_sim;
  //------------------------------------------------
  //------------------------------------------------
  int IFerror(int flags, char* format, IFuid* names) /* output an error or warning message */
  {itested();
    static struct mesg {
      const char *string;
      long flag;
    } msgs[] = {
      { "Warning", ERR_WARNING } ,
      { "Fatal error", ERR_FATAL } ,
      { "Panic", ERR_PANIC } ,
      { "Note", ERR_INFO } ,
      { NULL, 0 }
    } ;

    struct mesg *m;
    char buf[10000], *s, *bptr;
    int nindex = 0;

    for (m = msgs; m->flag; m++) {
      if (flags & m->flag) {
	error(bDANGER, "%s: ", m->string);
      }else{
      }
    }

    for (s = format, bptr = buf; *s; s++) {
      if (*s == '%' && (s == format || *(s-1) != '%') && *(s+1) == 's') {
	if (names[nindex]) {
	  strcpy(bptr, reinterpret_cast<char*>(names[nindex]));
	}else{
	  strcpy(bptr, "(null)");
	}
        bptr += strlen(bptr);
        s++;
        nindex++;
      } else {
        *bptr++ = *s;
      }
    }
    *bptr = '\0';
    switch (flags) {
    case ERR_WARNING:error(bWARNING,buf); break;
    case ERR_FATAL:  error(bDANGER, buf); throw Exception("");
    case ERR_PANIC:  error(bDANGER, buf); throw Exception("");
    case ERR_INFO:   error(bTRACE,  buf); break;
    default:         error(bDANGER, buf); break;
    }
    return 0;
  }
  void internalerror(char *message)
  {untested();
    error(bDANGER, "internal error: %s\n", message);
  }
    
#ifdef NGSPICE_17
  int CKTinst2Node(void*, void*, int, CKTnode**, IFuid*)
  {untested();incomplete();
    return 10;
  }
#endif
#ifdef JSPICE3
  static IFfrontEnd fe = {
    NULL, //int ((*IFnewUid)());	/* create a new UID in the circuit */ noise, urcsetup
    NULL, //int ((*IFpauseTest)());	/* should we stop now? */ noisean.c only
    NULL, //double ((*IFseconds)());	/* what time is it? */ bjtdisto unused ifdef only (unused)
    IFerror, //int ((*IFerror)());	/* output an error or warning message */ temp, setup
    NULL,
    NULL,
    NULL,
    NULL,
    NULL 
  };
#else
  static IFfrontEnd fe = {
    NULL, //int ((*IFnewUid)());	/* create a new UID in the circuit */ noise, urcsetup
    NULL, //int ((*IFdelUid)());	/* create a new UID in the circuit */ not used
    NULL, //int ((*IFpauseTest)());	/* should we stop now? */ noisean.c only
    NULL, //double ((*IFseconds)());	/* what time is it? */ bjtdisto unused ifdef only (unused)
    IFerror, //int ((*IFerror)());	/* output an error or warning message */ temp, setup
    NULL, //int ((*OUTpBeginPlot)());	/* start pointwise output plot */ noisean.c only
    NULL, //int ((*OUTpData)());	/* data for pointwise plot */ noisean.c only
    NULL, //int ((*OUTwBeginPlot)());	/* start windowed output plot */ not used
    NULL, //int ((*OUTwReference)());	/* independent vector for windowed plot */ not used
    NULL, //int ((*OUTwData)());	/* data for windowed plot */ not used
    NULL, //int ((*OUTwEnd)());		/* signal end of windows */ not used
    NULL, //int ((*OUTendPlot)());	/* end of plot */ not used
    NULL, //int ((*OUTbeginDomain)());	/* start nested domain */ not used
    NULL, //int ((*OUTendDomain)());	/* end nested domain */ not used
    NULL  //int ((*OUTattributes)());	/* specify output attributes of node */ noisean.c only
  };
#endif
  IFfrontEnd* SPfrontEnd = &fe;
  //------------------------------------------------
  //------------------------------------------------
  int CKTsetBreak(CKTcircuit* ckt, double time)
  {untested();
    if (time < ckt->CKTminBreak) {untested();
      ckt->CKTminBreak = time;
    }else{untested();
    }
    return OK;
  }
  //------------------------------------------------
  void CKTterr(int qcap, CKTcircuit* ckt,double *time_step)
  {
    assert_ckt_localized(ckt);

    std::valarray<FPOLY1> q(OPT::_keep_time_steps);

    for (int ii = 0; ii < OPT::_keep_time_steps; ++ii) {
      assert(ckt->CKTstates[ii]);
      q[ii].x  = NOT_VALID;
      q[ii].f0 = ckt->CKTstates[ii][qcap];
      q[ii].f1 = NOT_VALID;
    }

    DEV_SPICE* d = reinterpret_cast<DEV_SPICE*>(ckt->CKTstat);
    assert(d);
    assert(dynamic_cast<DEV_SPICE*>(d));

    *time_step = std::min(d->tr_review_trunc_error(&q[0]), *time_step);
  }
  //------------------------------------------------
  int NIintegrate(CKTcircuit* ckt,double* geq,double* ceq,double cap,int qcap)
  { //-- used by DEVload (not DC)
    assert_ckt_localized(ckt);

    METHOD method;
    if (ckt->CKTorder == 1) {
      method = mEULER;
    }else{
      assert(ckt->CKTtimePoints[1] != 0.);
      assert(ckt->CKTorder == 2);
      method = mTRAP;
    }

    std::valarray<FPOLY1> q(OPT::_keep_time_steps);
    std::valarray<FPOLY1> i(OPT::_keep_time_steps);

    for (int ii = 0; ii < OPT::_keep_time_steps; ++ii) {
      assert(ckt->CKTstates[ii]);
      q[ii].x  = NOT_VALID;
      q[ii].f0 = ckt->CKTstates[ii][qcap];
      q[ii].f1 = cap;
      trace3("", ii, q[ii].f0, q[ii].f1);
      i[ii].x  = NOT_VALID;
      i[ii].f0 = ckt->CKTstates[ii][qcap+1];
      i[ii].f1 = q[ii].f1 * ckt->CKTag[0];
      trace3("", ii, i[ii].f0, i[ii].f1);
      assert(q[ii].f0 == q[ii].f0);
      assert(q[ii].f1 == q[ii].f1);
      assert(i[ii].f0 == i[ii].f0);
      assert(i[ii].f1 == i[ii].f1);
    }

    i[0] = differentiate(&q[0], &i[0], ckt->CKTtimePoints, method);
    assert(i[0].f0 == i[0].f0);
    assert(i[0].f1 == i[0].f1);
    trace2("", i[0].f0, i[0].f1);

    ckt->CKTstates[0][qcap+1] = i[0].f0;

    assert(ckt->CKTdelta != 0. || (ckt->CKTag[0] == 0. && i[0].f0 == 0.));
    *ceq = i[0].f0 - q[0].f0 * ckt->CKTag[0];
    *geq = i[0].f1;
    assert(*ceq == *ceq);
    assert(*geq == *geq);
    trace2("", *ceq, *geq);
    return OK;
  }
  //------------------------------------------------
  //------------------------------------------------
  int CKTmkVolt(CKTcircuit* ckt, CKTnode** n, IFuid, char*)
  { // get a new node number. -- used by DEVsetup
    assert_ckt_initialized(ckt);
    assert(n);
    static CKTnode n_static;	// always used only on next line
    *n = &n_static;		// so reuse static structure
    (*n)->number = ((ckt->CKTmaxEqNum)++)+OFFSET;
    trace1(__FUNCTION__, (*n)->number);
    // local number (- == internal)   only number is used
    return OK;
  }
  int CKTmkCur(CKTcircuit* ckt, CKTnode** n, IFuid i, char* c)
  {untested();
    return CKTmkVolt(ckt, n, i, c);
  }
  //------------------------------------------------  
  int CKTdltNNum(void*,int)
  {untested(); // complement to CKTmkVolt. -- used by DEVunsetup
    // deletes what was new in CKTmkVolt
    // Nothing, because of no alloc there.
    return OK;
  }
  //------------------------------------------------  
  //------------------------------------------------  
  double* SMPmakeElt(SMPmatrix* mm, int r, int c)
  { // returns a pointer m[r][c] -- used by DEVsetup
    //trace2("", r, c);
    assert(mm);
    if (r == 0 || c == 0) {
      static double trash;
      trash = 0;
      return &trash;
    }else{
      assert(r >= 0+OFFSET);
      assert(r < MATRIX_NODES+OFFSET);
      assert(c >= 0+OFFSET);
      assert(c < MATRIX_NODES+OFFSET);
      DPAIR** m = reinterpret_cast<DPAIR**>(mm);
      assert(m);
      assert(m[r-OFFSET]);
      return reinterpret_cast<double*>(&(m[r-OFFSET][c-OFFSET]));
    }
  }
  //------------------------------------------------  
#ifdef JSPICE3
  int IFnewUid(GENERIC*,IFuid*,IFuid,char*,int,GENERIC**) {incomplete(); return 0;}
  int INPpName(char*,IFvalue*,GENERIC*,int,GENERIC*) {incomplete(); return 0;}
  char *INPdevErr(char *) {incomplete(); return NULL;}
  char *INPerror(int) {incomplete(); return NULL;}
  spREAL *spGetElement(char* s, int r, int c) {return SMPmakeElt(s,r,c);}
  char *INPerrCat(char *, char *) {incomplete(); return NULL;}
  int INPgndInsert(GENERIC*,char**,INPtables*,GENERIC**) {incomplete(); return 0;}
  char * INPdevParse(char**,GENERIC*,int,GENERIC*,double*,int*,INPtables*) {incomplete(); return NULL;}
  char *INPgetMod(GENERIC*,char*,INPmodel**,INPtables*) {incomplete(); return NULL;}
  int INPgetTok(char**,char**,int) {incomplete(); return 0;}
  int INPlookMod(char*) {incomplete(); return 0;}
  int INPtermInsert(GENERIC*,char**,INPtables*,GENERIC**) {incomplete(); return 0;}
  int INPinsert(char**,INPtables*) {incomplete(); return 0;}
  char *copy(char*) {incomplete(); return NULL;}
  int NIsum(CKTcircuit*,double*,int) {incomplete(); return 0;}
  double INPevaluate(char**,int*,int) {incomplete(); return NOT_VALID;}
  IFvalue *INPgetValue(GENERIC*,char**,int,INPtables*) {incomplete(); return NULL;}
#endif
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// Verify that the layout of complex is as Spice assumes.
// This is not guaranteed by the standard, but is believed to always be true.
static struct COMPLEX_TEST {
  COMPLEX_TEST() {
    DPAIR x;
    DPAIR* px = &x;
    double* prx = &x.first;
    double* pix = &x.second;
    assert(reinterpret_cast<void*>(prx) == reinterpret_cast<void*>(px));
    assert(reinterpret_cast<void*>(pix-1) == reinterpret_cast<void*>(px));
  }
  ~COMPLEX_TEST() {
  }  
} complex_test;
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
int MODEL_SPICE::_count = -1;
int DEV_SPICE::_count = -1;

static DEV_SPICE p0;
static DISPATCHER<CARD>::INSTALL
  d0(&device_dispatcher, std::string(SPICE_LETTER) + "|" + DEVICE_TYPE, &p0);

static MODEL_SPICE p1(&p0);
static DISPATCHER<MODEL_CARD>::INSTALL
  d1(&model_dispatcher, MODEL_TYPE, &p1);
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: