~jdpipe/ascend/trunk-old

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
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass book
\use_default_options false
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_math auto
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize a4paper
\use_geometry false
\use_package amsmath 2
\use_package amssymb 2
\use_package cancel 1
\use_package esint 0
\use_package mathdots 0
\use_package mathtools 1
\use_package mhchem 0
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 2
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Chapter
Writing METHODs
\begin_inset CommandInset label
LatexCommand label
name "cha:methods"

\end_inset


\end_layout

\begin_layout Standard
In this chapter we describe a methodology (pun intended) which can help
 make anyone who can solve a quadratic equation a mathematical modeling
 expert.
 This methodology helps you to avoid mistakes and to find mistakes quickly
 when you make them.
 Finding bugs weeks after creating a model is annoying, inefficient, and
 (frequently) embarrassing.
 Because METHOD code can be large, we do not include many examples here.
 
\begin_inset Note Note
status collapsed

\begin_layout Plain Layout
See ***vessel something*** for detailed examples
\end_layout

\end_inset

 One of the advantages of this methodology is that it allows almost automatic
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
methods, automatic generation
\end_layout

\end_inset

 generation of methods for a model based on the declarative structure (defined
 parts and variables) in the model, as we shall see in Section
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "sec:methods.automation"

\end_inset


\noun off
.
 Even if you skip much of this chapter, read 
\noun default
Section
\begin_inset space ~
\end_inset


\noun off

\begin_inset CommandInset ref
LatexCommand ref
reference "sec:methods.automation"

\end_inset


\end_layout

\begin_layout Standard
We divide methods into 
\family typewriter
_self
\family default
 and 
\family typewriter
_all
\family default
 categories.
 The premise of our approach to design methods is that we can write the
 
\family typewriter
_self
\family default
 methods incrementally, building on the already tested methods of previous
 MODEL parts we are reusing.
 In this way we never have to write a single huge method that directly manipulat
es the hundreds of variables that are in the model hierarchy.
 Were that all there was to it, things would actually be pretty simple.
 However, in ASCEND, one can also select to solve any part of an ASCEND
 model (in particular, any part that is an instance of a single type), and
 this capability complicates method writing - but not by much if we really
 understand the approach we advocate here.
 As an example, suppose we have a flowsheet that has a reactor followed
 by a flash unit in it.
 In ASCEND, we can select to solve the entire flowsheet, only the reactor,
 only the flash unit or even any one of the streams in it (yes, each stream
 has physical property calculations that belong to it making it interesting
 to isolate and solve).
 Should we choose to solve only the flash unit, ASCEND will isolate the
 equations defining the flash - including the equations we use when defining
 the input and output streams to it as they are a part of the flash unit.
 But the input to the flash is also the output from the reactor and is a
 part of the reactor, too.
 Each part would typically take the prime responsibility for supplying methods
 that will set fix flags, set nominal values, etc., for its variables, but
 who owns the variables they both share such as in the connecting stream?
 By 
\begin_inset Quotes eld
\end_inset

tradition
\begin_inset Quotes erd
\end_inset

 in chemical engineering flowsheet modeling, we will assert that the reactor
 has prime responsibility for its output stream.
 If we are solving the entire flowsheet, it should set the flags, etc., for
 its output stream.
 However, when we isolate the flash for solving, the flash unit must assume
 responsibility to set fix flags, nominal values, etc., for the output stream
 from the reactor as that stream is its input stream.
 The 
\family typewriter
_all
\family default
 methods allow us to handle these shared variables correctly when we isolate
 a part for solving by itself.
\begin_inset Note Note
status collapsed

\begin_layout Plain Layout
This needs more explanation: I don't find it completely clear.
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Usually discovery of the information you need to write the methods proceeds
 in the order that they appear below: 
\family typewriter
check
\family default
, 
\family typewriter
default
\family default
, 
\family typewriter
specify
\family default
, 
\family typewriter
bound
\family default
, 
\family typewriter
scale
\family default
.
 
\end_layout

\begin_layout Section
Why use standardised methods on models?
\end_layout

\begin_layout Standard
In the present chapter, we are proposing that you use a particular standardised
 set of methods on your models.
 While ASCEND doesn't force you to follow these conventions, we hope that
 you will 
\emph on
choose
\emph default
 to follow them, because:
\end_layout

\begin_layout Itemize
You models will be more portable.
\end_layout

\begin_layout Itemize
They will integrate better with the existing model library when composing
 larger models composed of smaller perhaps pre-existing models.
\end_layout

\begin_layout Itemize
Other users will be be more easily able to understand what you have built.
\end_layout

\begin_layout Itemize
The proposed structure has, in our experience, made for models that are
 easier to debug.
\end_layout

\begin_layout Standard
There will be cases where these standard methods don't suffice for your
 needs; these are just proposals for a useful starting point and shared-use
 conventions.
\end_layout

\begin_layout Standard
Note that if you do not write the standard methods, your MODEL will inherit
 the ones given in the library 
\family typewriter
basemodel.a4l
\family default
.
 The 
\family typewriter
ClearAll
\family default
 and 
\family typewriter
reset
\family default
 methods here will work for you if you follow the guidelines for the method
 
\family typewriter
specify
\family default
.
 The other methods defined in 
\family typewriter
basemodel.a4l
\family default
 
\family typewriter
(check_self
\family default
, 
\family typewriter
default_self
\family default
, 
\family typewriter
bound_self
\family default
, 
\family typewriter
scale_self
\family default
, 
\family typewriter
check_all
\family default
, 
\family typewriter
default_all
\family default
, 
\family typewriter
bound_all
\family default
, 
\family typewriter
scale_all
\family default
) all contain 
\family typewriter
STOP
\family default
 statements that will warn you that you have skipped something important,
 should you accidentally call one of these methods.
 If you create a model for someone else and they run into one of these 
\family typewriter
STOP
\family default
 errors while using your model, that error is your fault.
\end_layout

\begin_layout Section
Methods *_self
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
methods, 
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset

 VS *_all
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
methods, 
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

all
\end_layout

\end_inset


\end_layout

\begin_layout Standard
When you create a model definition, you create a container holding variables,
 equations, arrays, and other models.
 You create methods in the same definition to control the state of (the
 values stored in) all these parts.
 ASCEND lets you share objects among several models by passing objects through
 a model interface (the 
\family typewriter
MODEL
\family default
 parameter list), by creating ALIASES
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
ALIASES
\end_layout

\end_inset

 for parts within contained objects, and even by merging parts (though merging
 should be avoided for any object larger than a variable).
 
\end_layout

\begin_layout Standard
\begin_inset Marginal
status collapsed

\begin_layout Plain Layout
Too many cooks
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
cooks
\end_layout

\end_inset

 spoil the broth
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
broth
\end_layout

\end_inset

.
\end_layout

\end_inset

The problem this creates for you as a 
\family typewriter
METHOD
\family default
 writer is to decide which of the several 
\family typewriter
MODEL
\family default
s that share an object is responsible for updating that variable's default,
 bounds, and nominal values.
 You could decide that every model which shares a variable is responsible
 for these values.
 This decision will lead to many, many, many hard to understand conflicts
 as different models all try to manage the same value.
 And, the last one run will in fact have the final say.
 But it is difficult to know always which is the last one run.
 The sensible approach is to make only one model responsible for the bounding,
 scaling, and default setting of each variable: 
\emph on
the model that creates the variable in the first place
\emph default
.
 If you abide by this approach, you will keep things much simpler for yourself.
 And, fortunately, the modeling language makes it pretty clear who has created
 each and every variable - except when merging variables.
\end_layout

\begin_layout Standard
\begin_inset Marginal
status collapsed

\begin_layout Plain Layout
Use *_self methods on locally created variables and parts
\end_layout

\end_inset

Consider the following model and creating the 
\family typewriter
*_self
\family default
 methods 
\family typewriter
default_self
\family default
, 
\family typewriter
check_self
\family default
, 
\family typewriter
bound_self
\family default
, and 
\family typewriter
scale_self
\family default
 for it.
 
\end_layout

\begin_layout LyX-Code
MODEL selfish(
\end_layout

\begin_layout LyX-Code
    external_var WILL_BE solver_var;
\end_layout

\begin_layout LyX-Code
    out_thingy WILL_BE input_part;
\end_layout

\begin_layout LyX-Code
);
\end_layout

\begin_layout LyX-Code
    my_variable IS_A solver_var;
\end_layout

\begin_layout LyX-Code

\end_layout

\begin_layout LyX-Code
    peek_at_variable ALIASES out_thingy.mabob.cost;
\end_layout

\begin_layout LyX-Code
    my_thingy IS_A nother_part;
\end_layout

\begin_layout LyX-Code
    navel_gaze ALIASES my_thingy.mabob.cost;
\end_layout

\begin_layout LyX-Code
END selfish;
\end_layout

\begin_layout Standard

\end_layout

\begin_layout Standard

\family typewriter
IS_A
\family default
 statements indicate all the parts we have created in this model: namely,
 the 
\family typewriter
solver_var
\family default
 we call 
\family typewriter
my_variable
\family default
 and the 
\family typewriter
nother_part
\family default
 we call 
\family typewriter
my_thingy
\family default
.
 This model should manage the value of the only variable it creates: 
\family typewriter
my_variable
\family default
.
 The variable, 
\family typewriter
external_var
\family default
, comes in from the outside so some other model has created it and should
 manage it.
 The variables 
\family typewriter
peek_at_variable
\family default
 and 
\family typewriter
navel_gaze
\family default
 also are not created here and should not be managed in the 
\family typewriter
*_self
\family default
 methods of 
\family typewriter
selfish
\family default
.
 
\family typewriter
my_thingy.mabob.cost
\family default
 belongs to a part we created.
 We want to default, bound, or scale variables in all parts we create, also,
 so we must call 
\family typewriter
my_thingy.default_self
\family default
 whenever 
\family typewriter
default_self
\family default
 is called for this model.
 Its 
\family typewriter
*_self
\family default
 method should in turn call the 
\family typewriter
*_self
\family default
 method for 
\family typewriter
mabob
\family default
, which should set defaults, bounds and scaling for its variable, 
\family typewriter
cost
\family default
.
 Finally, 
\family typewriter
out_thingy
\family default
 is an input parameter and is not created here; we should not call 
\family typewriter
out_thingy.default_self
\family default
, therefore, as some other model will do so.
\end_layout

\begin_layout Standard
\begin_inset Marginal
status collapsed

\begin_layout Plain Layout
Use *_all methods to manage a troublesome part
\end_layout

\end_inset

As noted above, you may choose to isolate any mathematical subproblem in
 a large simulation for debugging or solving purposes.
 When you do this isolation using the Browser and Solver tools, you still
 need to call scaling, bounding, and checking methods for all parts of the
 isolated subproblem, even for those parts that come in from the outside.
 This is easily done by writing 
\family typewriter
*_all
\family default
 methods.
 In the example above, 
\family typewriter
scale_all
\family default
 will scale 
\family typewriter
external_var
\family default
 and call 
\family typewriter
out_thingy.scale_all
\family default
 because these parts are defined using 
\family typewriter
WILL_BE
\family default

\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
WILL
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

BE
\end_layout

\end_inset

 statements.
 Finally 
\family typewriter
scale_all
\family default
 will call its local 
\family typewriter
*_self
\family default
 to do all the normal scaling.
\end_layout

\begin_layout Standard
That's the big picture of 
\family typewriter
*_self
\family default
 and 
\family typewriter
*_all
\family default
 methods.
 Each kind of method (bound, scale, default, check) has its own peculiarities,
 which we cover in Section 
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:methods.selfMethods"

\end_inset


\noun off
 and 
\noun default
Section 
\noun off

\begin_inset CommandInset ref
LatexCommand ref
reference "sec:methods.allMethods"

\end_inset

, but they all follow the rules above 
\noun default
and
\noun off
 distinguish among variables and parts defined with 
\family typewriter
\noun default
WILL_BE
\family default
\noun off
 (managed in *
\family typewriter
\noun default
_all
\family default
\noun off
 only), IS_A
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
IS
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

A
\end_layout

\end_inset

 (managed in 
\family typewriter
\noun default
*_self
\family default
\noun off
 only), and 
\family typewriter
\noun default
ALIASES
\family default
\noun off
 (not our responsibility).
\end_layout

\begin_layout Section
How to write ClearAll
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
ClearAll
\end_layout

\end_inset

 and reset
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
reset
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Writing these two standard methods in your model is very simple: do nothing.
 You may wish to write alternative 
\family typewriter
reset_*
\family default
 methods as we shall discuss.
 All models inherit these methods from the definitions in 
\family typewriter
basemodel.a4l
\family default
.
 Just so you know, here is what they do.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD ClearAll
\end_layout

\begin_layout Standard
This method finds any variable that is a 
\family typewriter
solver_var
\family default
 or refinement of 
\family typewriter
solver_var
\family default
 and changes the 
\family typewriter
.fixed
\family default
 flag on that var to 
\family typewriter
FALSE
\family default
.
 This method only touches the 
\family typewriter
.fixe
\family default
d flags; i.e., it does not change the value of 
\family typewriter
.included
\family default
 flags on relations or return boolean, integer and symbol variables to a
 default value.
 
\end_layout

\begin_layout Subsection

\family typewriter
METHOD reset
\end_layout

\begin_layout Standard
This method calls 
\family typewriter
ClearAll
\family default
 to bring the model to a standard state with all variables unfixed (free),
 then it calls the user-written specify method to bring the model to a state
 where it has an equal number of variables to calculate and equations to
 solve - i.e., to being 
\begin_inset Quotes eld
\end_inset

square.
\begin_inset Quotes erd
\end_inset

 Normally you do not need to write this method: your models will inherit
 this one unless you override it (redefine it) in your model.
\end_layout

\begin_layout Standard
This standard state is not necessarily the most useful starting state for
 any particular application.
 This method merely establishes a base case the modeler finds to be a good
 starting point.
 As an example, the modeler may elect to set fix variables for the base
 case for a flash unit so it corresponds to an isothermal flash calculation.
 Chemical engineers understand this case and can make changes from it to
 set up other cases, such as an adiabatic flash, relatively easily by setting
 and resetting but one or two 
\family typewriter
.fixed
\family default
 flags.
 There is no one perfect "reset"' for all purposes.
 Other 
\family typewriter
reset_*
\family default
 methods can also be written for particular purposes, if such arise
\begin_inset Foot
status collapsed

\begin_layout Plain Layout
The name of a method, for example 
\family typewriter
reset_someOtherPurpose
\family default
 is a communication tool.
 Please use meaningful names as long as necessary to tell what the method
 does.
 Avoid cryptic abbreviations and hyper-specialized jargon known only to
 you and your three friends when you are naming methods; however, do not
 shy away from technical terms common to the engineering domain in which
 you are modeling.
\end_layout

\end_inset

.
\end_layout

\begin_layout Section
The 
\family typewriter
*_self
\family default

\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset

 methods
\begin_inset CommandInset label
LatexCommand label
name "sec:methods.selfMethods"

\end_inset


\end_layout

\begin_layout Standard
The following methods should be redefined by each reusable library 
\family typewriter
MODEL
\family default
.
 Models that do not supply proper versions of these methods are usually
 very hard to reuse.
 
\end_layout

\begin_layout Subsection

\family typewriter
METHOD check_self
\family default

\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
check
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset


\end_layout

\begin_layout Standard
One can benefit by writing this method first, though it is run last.
 Just as they taught you in elementary school, always check your work.
 Start by defining criteria for a successful solution that will not be included
 in the equations solved and compute them in this method.
 As you develop your 
\family typewriter
MODEL
\family default
, you should expect to revise the check method from time to time, if you
 are learning anything about the model.
 We frequently change our definition of success when modeling.
\end_layout

\begin_layout Standard
When a mathematical model is solved, the assumptions that went into writing
 (deriving) the equations should be checked.
 Usually there are redundant equations available (more than one way to state
 the physics or economics mathematically).
 These should be used to check the particularly tricky bits of the model.
\end_layout

\begin_layout Standard
Check that the physical or intuitive (qualitative) relationships among variables
 you expect to hold are true, especially if you have not written such relationsh
ips in terms of inequalities (
\family typewriter
x*z <= y
\family default
) in the 
\family typewriter
MODEL
\family default
 equations.
\end_layout

\begin_layout Standard
In some models, checking the variable values against absolute physical limits
 (
\family typewriter
temperature > 0{K}
\family default
 and 
\family typewriter
temperature < Tcritical
\family default
, for example) may be all that is necessary or possible.
 Do not check variable values against their 
\family typewriter
.lower_bound
\family default
 or 
\family typewriter
.upper_bound
\family default
, as ASCEND will do this for you.
\end_layout

\begin_layout Standard
If a check fails, use a 
\family typewriter
STOP
\family default

\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
STOP
\end_layout

\end_inset

 statement to notify yourself (or your end-user) that the solution may be
 bogus.
 
\family typewriter
STOP
\family default
 raises an error signal and issues an error message.
 
\family typewriter
STOP
\family default
 normally also stops further execution of the method and returns control
 to a higher level, though there are interactive tools to force method execution
 to continue.
 
\family typewriter
STOP
\family default
 does not cause ASCEND to exit though: just for the method execution to
 halt and for control to return to the user.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD default_self
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
default
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset


\end_layout

\begin_layout Standard
This method should set default values for any variables declared locally
 (
\family typewriter
IS_A
\family default
) to the model.
 If the default value declared for the atom type, of which the variable
 is an instance, is appropriate (and typically it is), then you need not
 set a specific default value here.
 This method also should run 
\family typewriter
default_self
\family default
 on all the complex parts that are declared locally (with 
\family typewriter
IS_A
\family default
) in the model.
\end_layout

\begin_layout Standard
This method should not run any methods on model parts that come via 
\family typewriter
WILL_BE
\family default
 in the definition's parameter list.
 This method also should not change the values of variables that are passed
 in through the parameter list.
 Sometimes there will be nothing for this method to do.
 Define it anyway, leaving it empty, so that any writer reusing this model
 as part of a higher level model can safely assume it is there and call
 it without having to know the details.
\end_layout

\begin_layout Standard
When a top-level simulation is built by the compiler, this method will be
 run (for the top-level model) at the end of compilation.
 If your model's 
\family typewriter
default_self
\family default
 method does not call the lower-level 
\family typewriter
default_self
\family default
 methods in your model locally-declared (
\family typewriter
IS_A
\family default
) parts, it is quite likely that your model will not solve.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD bound_self
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
bound
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Much of the art of nonlinear physical modeling is in bounding the solution.
 This method should update the bounds on locally defined (
\family typewriter
IS_A
\family default
) variables and 
\family typewriter
(IS_A)
\family default
 defined model parts.
 Updating bounds requires some care.
 For example, the bounds on fractions frequently don't need updating.
 This method should not bound variables passed into the 
\family typewriter
MODEL
\family default
 definition or parts passed into the definition.
\end_layout

\begin_layout Standard
A common formula for updating bounds is to define a region around the current
 value of the variable.
 A linear region size formula, as an example, would be:
\end_layout

\begin_layout Standard
\begin_inset Formula 
\begin{equation}
x_{bound}=x\pm\Delta\cdot x_{nominal}\label{eq:methods.bound-width}
\end{equation}

\end_inset

or, in ASCEND syntax,
\end_layout

\begin_layout LyX-Code
v.upper_bound := v + 
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
boundwidth
\end_layout

\end_inset

boundwidth * v.nominal;
\end_layout

\begin_layout LyX-Code
v.lower_bound := v - boundwidth * v.nominal;
\end_layout

\begin_layout Standard
Care must be taken that such a formula does not move the bounds (particularly
 lower bounds) out so far as to allow non-physical solutions.
 Logarithmic bounding regions are also simple to calculate.
 Here 
\family typewriter
boundwidth
\family default
 is a 
\family typewriter
bound_width
\family default
: it could be a real atom (but 
\series bold
not
\series default
 a 
\family typewriter
solver_var
\family default
) or some value you can use to determine how much "wiggle-room" you want
 to give a solver.
 
\end_layout

\begin_layout Standard
Small powers of 4 and 10 are usually good values of 
\family typewriter
boundwidth
\family default
.
 Too small a boundwidth can cut off the portion of number space where the
 solution is found.
 Too large a bound width can allow solvers to wander for great distances
 in uninteresting regions of the number space.
\end_layout

\begin_layout Standard
\begin_inset Note Note
status open

\begin_layout Plain Layout
We need an example here
\end_layout

\end_inset


\end_layout

\begin_layout Subsection

\family typewriter
METHOD scale_self
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
scale
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

self
\end_layout

\end_inset


\begin_inset CommandInset label
LatexCommand label
name "ssec:methods.scaleself"

\end_inset


\end_layout

\begin_layout Standard
Most nonlinear (and many linear) models cannot be solved without proper
 scaling of the variables.
 
\family typewriter
scale_self
\family default
 should reset the 
\family typewriter
.nominal
\family default
 value on every real variable in need of scaling.
 It should then call the 
\family typewriter
scale_self
\family default
 method on all the locally-defined (
\family typewriter
IS_A
\family default
) parts of the MODEL.
 A proper nominal is one such that you expect at the solution
\end_layout

\begin_layout Standard
\begin_inset Formula 
\begin{equation}
0\leq abs\left(\frac{x}{x_{nominal}}\right)\leq10\label{eq:methods.choiceNominal}
\end{equation}

\end_inset


\end_layout

\begin_layout Standard
As one is dividing by the nominal value for the variable to establish its
 scaled value, zero is about the worst value you could choose for a nominal
 value.
\end_layout

\begin_layout Standard
This method should not change the 
\family typewriter
.nominal
\family default
 values for models and variables that are received through the parameter
 list of the model.
\end_layout

\begin_layout Standard
Variables (like fractions), when bounded such that they cannot be too far
 away from 1.0 in magnitude, probably don't need scaling most of the time,
 so long as they they are also bounded away from 0.0.
 
\end_layout

\begin_layout Standard
Some solvers, but not all, will attempt to scale the equations and variables
 by heuristic matrix-based methods.
 This works, but inconsistently; user-defined scaling
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
scaling
\end_layout

\end_inset

 is generally superior.
 ASCEND makes scaling equations easy to do.
 You scale the variables, which can only be done well by knowing something
 about where the solution is going to be found (by being an engineer, for
 example).
 Then ASCEND can calculate an appropriate equation-scaling by efficient
 symbolic methods.
\end_layout

\begin_layout Section
The 
\family typewriter
*_all
\family default
 methods
\begin_inset CommandInset label
LatexCommand label
name "sec:methods.allMethods"

\end_inset


\end_layout

\begin_layout Subsection

\family typewriter
METHOD default_all
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
default
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

all
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Above we discussed the ability in ASCEND to isolate and solve any part of
 a model that is defined as an instance of a single type requires you initialize
 the arguments to a model that you are isolating.
 This method should run the 
\family typewriter
default_all
\family default
 method on each of the parts received through the parameter list via 
\family typewriter
WILL_BE
\family default
 statements and should give appropriate default values to any variables
 received through the parameter list.
 After these calls and settings have been done, it should then call its
 own 
\family typewriter
default_self
\family default
 to take care of all local defaults.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD check_all
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
check
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

all
\end_layout

\end_inset


\end_layout

\begin_layout Standard
When solving only a part of a simulation, it is necessary to check the models
 and variables passed into the part as well as the locally defined parts
 and variables.
 This method should call 
\family typewriter
check_all
\family default
 on the parts received as 
\family typewriter
WILL_BE
\family default
 parameters, then call 
\family typewriter
check_self
\family default
 to check the locally defined parts and equations.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD bound_all
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
bound
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

all
\end_layout

\end_inset


\end_layout

\begin_layout Standard
This method should be like 
\family typewriter
bound_self
\family default
 except that it bounds the passed in variables and calls 
\family typewriter
bound_all
\family default
 on the passed in parts.
 It should then call 
\family typewriter
bound_self
\family default
.
\end_layout

\begin_layout Subsection

\family typewriter
METHOD scale_all
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
scale
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
_
\end_layout

\end_inset

all
\end_layout

\end_inset


\end_layout

\begin_layout Standard
This method should be like 
\family typewriter
scale_self
\family default
 above except that it scales the variables received through the parameter
 list and calls 
\family typewriter
scale_all
\family default
 on the passed in parts.
 It should then call 
\family typewriter
scale_self
\family default
 to take care of the local variables and models.
\end_layout

\begin_layout Section

\family typewriter
METHOD specify
\family default

\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
specify
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The method 
\family typewriter
specify
\family default
 sets the 
\family typewriter
.fixed
\family default
 flags so an instance of the model is solvable.
 There are two issues involved.
 First, the model must be square - i.e., it must have as many free variables
 as it has eligible equations available to compute them.
 Second, it should be a setting of 
\family typewriter
.fixed
\family default
 flags that the modeler knows will solve numerically.
 This latter requirement requires one to have an intuitive feel for the
 model.
 A chemical engineer will 
\begin_inset Quotes eld
\end_inset

know
\begin_inset Quotes erd
\end_inset

 that a flash calculation, where he has fixed both the pressure and the
 vapor fraction, is a pretty robust calculation.
 It would be a good way to set fix flags in the 
\family typewriter
specify
\family default
 method.
 Getting the 
\family typewriter
.fixed
\family default
 flags set for a large complex model is one of the hardest tasks ever invented
 by mathematicians if you go about it in the wrong way.
 If you follow the prescription here, getting the right number of flags
 set is almost automatic.
 We have set written the specify methods for a complex hierarchy of models
 correctly the first time using this approach.
\end_layout

\begin_layout Standard
We shall illustrate this section by examining the set of models in 
\family typewriter
simple_fs.a4c
\family default
.
 You should find that model in the model directory for ASCEND and open it
 in your favorite text editor.
 This model is for a simple flowsheet comprising a mixer, a reactor, a flash
 unit and a simple stream splitter.
 It contains the following models: 
\family typewriter
mixture
\family default
, 
\family typewriter
molar_stream
\family default
, 
\family typewriter
mixer
\family default
, 
\family typewriter
reactor
\family default
, 
\family typewriter
flash
\family default
, 
\family typewriter
splitter
\family default
, 
\family typewriter
flowsheet
\family default
, 
\family typewriter
controller
\family default
, 
\family typewriter
test_flowsheet
\family default
 and 
\family typewriter
test_controller
\family default
.
 When compiling and solving, one typically creates an instance of 
\family typewriter
test_controller
\family default
.
 
\end_layout

\begin_layout Standard
Model mixture only defines new variables 
\family typewriter
y[components]
\family default
 and one equation that says their sum is unity.
 Model molar_stream introduces new variables 
\family typewriter
Ftot
\family default
 and 
\family typewriter
f[components]
\family default
.
 It also introduces an instance of the model mixture, which it calls 
\family typewriter
state
\family default
.
 Finally it introduces locally the equations 
\family typewriter
f_def
\family default
, one for each component.
 Models 
\family typewriter
mixer
\family default
, 
\family typewriter
reactor
\family default
, 
\family typewriter
flash
\family default
 and 
\family typewriter
splitter
\family default
 introduce their own local variables and equations.
 Each also defines it input and output streams as instances of the model
 
\family typewriter
molar_stream
\family default
.
 Model 
\family typewriter
flowsheet
\family default
 contains a 
\family typewriter
mixer
\family default
, 
\family typewriter
reactor
\family default
, 
\family typewriter
flash
\family default
 and 
\family typewriter
splitter
\family default
, etc.
\end_layout

\begin_layout Standard
Assume you have just written a set of models, such as those in 
\family typewriter
simple_fs.a4c
\family default
.
 In this approach you should start with the lowest level models - i.e., the
 ones that only introduce (using 
\family typewriter
IS_A
\family default
 statements) new variables and new parts that are instances of types in
 existing ASCEND libraries.
 The lowest level model by this definition is 
\family typewriter
mixture
\family default
.
 It only introduces new variables and one new equation.
 Once you have written and debugged a 
\family typewriter
specify
\family default
 method for 
\family typewriter
mixture
\family default
, then again look for any model that only introduces new variables and local
 equations and/or parts for which a debugged 
\family typewriter
specify
\family default
 method exists.
 The model 
\family typewriter
molar_stream
\family default
 is such a model and can be considered next.
 It introduces a part which is an instance of 
\family typewriter
mixture
\family default
 for which we already have a 
\family typewriter
specify
\family default
 method.
 Once we have a debugged 
\family typewriter
specify
\family default
 method for 
\family typewriter
molar_stream
\family default
, then we can consider any of the models 
\family typewriter
mixer
\family default
, 
\family typewriter
reactor
\family default
, 
\family typewriter
flash
\family default
 and 
\family typewriter
splitter
\family default
 - they only have parts that are instances of 
\family typewriter
molar_stream
\family default
.
 After creating and debugging their 
\family typewriter
specify
\family default
 methods, we can consider the model 
\family typewriter
flowsheet
\family default
, then 
\family typewriter
controller
\family default
, then 
\family typewriter
test_flowsheet
\family default
 and finally 
\family typewriter
test_controller
\family default
.
\end_layout

\begin_layout Standard
The safest way to set 
\family typewriter
.fixed
\family default
 flags is first to clear all the 
\family typewriter
.fixed
\family default
 flags for a model instance by running the method 
\family typewriter
ClearAll
\family default
.
 The method 
\family typewriter
specify
\family default
 does 
\series bold
not
\series default
 run 
\family typewriter
ClearAll
\family default
, but we always write our 
\family typewriter
specify
\family default
 methods assuming 
\family typewriter
ClearAll
\family default
 has just been run and thus that all 
\family typewriter
.fixed
\family default
 flags are set to false.
 The following steps will aid you to write, almost automatically, a 
\family typewriter
specify
\family default
 method that fixes the correct number of 
\family typewriter
.fixed
\family default
 flags.
\end_layout

\begin_layout Enumerate
Find all locally defined solver variables (of type 
\family typewriter
solver_var
\family default
 - e.g., 
\family typewriter
y[components]
\family default
 are of type 
\family typewriter
fraction
\family default
 which is of type 
\family typewriter
solver_var
\family default
).
 In mixture, the statement 
\begin_inset Quotes eld
\end_inset


\family typewriter
y[components] IS_A fraction;
\family default

\begin_inset Quotes erd
\end_inset

 introduces new 
\family typewriter
solver_var
\family default
s, one for each element in the set 
\family typewriter
components
\family default
.
 Let us assume there are 
\family typewriter
nc
\family default
 such elements.
\end_layout

\begin_layout Enumerate
Find locally introduced equations.
 In 
\family typewriter
mixture
\family default
, there is one such equation that says the variables 
\family typewriter
y
\family default
 add up to one.
\end_layout

\begin_layout Enumerate
Find all new parts that are instances of previously defined types.
 In 
\family typewriter
mixture
\family default
, there are no new parts.
\end_layout

\begin_layout Enumerate
You must set 
\family typewriter
.fixed
\family default
 flags locally equal in number to the number of new locally defined 
\family typewriter
solver_var
\family default
s minus the number of new locally defined equations.
 In 
\family typewriter
mixture
\family default
 you must write set one 
\family typewriter
fixed
\family default
 flag to true for all but one of the components as there are 
\family typewriter
nc
\family default
 new locally introduced variables and one new locally introduced equation.
 The 
\family typewriter
CHOICE
\family default
 function arbitrarily selects one element of the set (in set theory, you
 cannot identify a set element as being first, another as second, etc, so
 for purity's sake, we only give you the option of letting ASCEND pick one
 arbitrarily).
 Thus we set all 
\family typewriter
nc
\family default
 flags and then clear one.
\end_layout

\begin_layout Enumerate
You must run the 
\family typewriter
specify
\family default
 method for each new part.
 Here there are none.
 Running specify will guarantee each part is 
\begin_inset Quotes eld
\end_inset

square
\begin_inset Quotes erd
\end_inset

 - i.e., after being run, the part will not alter the number of degrees of
 freedom for the current model definition.
 However, the same 
\family typewriter
solver_var
\family default
 may get fixed in two or more different parts if those parts share that
 
\family typewriter
solver_var
\family default
, and you will have to discover this sharing and add special statements
 to correct this type of multiple setting of the same flag.
 This discovery will best be done by compiling an instance of the type and
 using the Find By Type tool in the Browser.
 Its default setting is to find all 
\family typewriter
solver_vars
\family default
 with 
\family typewriter
.fixed
\family default
 equal to 
\family typewriter
TRUE
\family default
, exactly what you need to aid you with this task.
 You may also wish to change in minor ways the flag setting that the parts
 do to suit the needs of the current type definition - you may wish to free
 
\family typewriter
temperature
\family default
 and fix 
\family typewriter
pressure
\family default
 for a stream, for example, when the stream is part of a higher level model.
\end_layout

\begin_layout Standard
Look now at the 
\family typewriter
molar_stream
\family default
 model.
 Running 
\family typewriter
ClearAll
\family default
 for an instance of 
\family typewriter
molar_stream
\family default
 will clear all the 
\family typewriter
.fixed
\family default
 flags for it and all its parts.
 It introduces 
\family typewriter
Ftot
\family default
 and 
\family typewriter
f[components]
\family default
 as local new solver variables.
 It also introduces one new equation for each component, one less than the
 number of new variables.
 Finally it introduces a new part called 
\family typewriter
state
\family default
.
 We have partitioned the 
\family typewriter
specify
\family default
 method into two methods here for 
\begin_inset Quotes eld
\end_inset

chemical engineering reasons,
\begin_inset Quotes erd
\end_inset

 one of which runs the other.
 Think of what the two of them accomplish as the 
\family typewriter
specify
\family default
 method we wish to create.
 First we run the 
\family typewriter
specify
\family default
 method for the new part: 
\family typewriter
state
\family default
.
 That will set the 
\family typewriter
.fixed
\family default
 flags for 
\family typewriter
nc-1
\family default
 of the variables 
\family typewriter
state.y[components]
\family default
.
 Then, as there is one more variable than equation in this model, we must
 set a net of one added 
\family typewriter
.fixed
\family default
 flag.
 We accomplish this by first clearing all the flags for the variables 
\family typewriter
state.y[components]
\family default
 -- one of which was already clear -- and then fixing all the variables
 
\family typewriter
f[components]
\family default
.
 We cleared 
\family typewriter
nc-1
\family default
 flags and set 
\family typewriter
nc
\family default
 for a net of one new flag being set.
 For our 
\family typewriter
molar_stream
\family default
 model, we would prefer that the variables 
\family typewriter
f[components]
\family default
 are the ones we fix.
\end_layout

\begin_layout Standard
Lastly, look at the 
\family typewriter
reactor
\family default
 model.
 We introduce 
\family typewriter
nc+1
\family default
 new variables: 
\family typewriter
stoich_coef[feed.components]
\family default
 and 
\family typewriter
turnover
\family default
.
 We also introduce 
\family typewriter
nc
\family default
 new equations.
 Lastly we introduce two parts 
\family typewriter
feed
\family default
 and 
\family typewriter
out
\family default
, which are 
\family typewriter
molar_stream
\family default
s.
 The 
\family typewriter
specify
\family default
 method, again a combination of 
\family typewriter
specify
\family default
 and 
\family typewriter
seqmod
\family default
, must set a net of one new 
\family typewriter
.fixed
\family default
 flag.
 The way it does it is 
\begin_inset Quotes eld
\end_inset

tricky
\begin_inset Quotes erd
\end_inset

 but not difficult to follow.
 In 
\family typewriter
seqmod
\family default
, we fix 
\family typewriter
turnover
\family default
 and all 
\family typewriter
nc
\family default
 of the variables 
\family typewriter
stoich_coef
\family default
.
 We seem to have fixed 
\family typewriter
nc
\family default
 too many.
 In 
\family typewriter
specify
\family default
, which first runs 
\family typewriter
seqmod
\family default
, we only run the 
\family typewriter
specify
\family default
 method for 
\family typewriter
feed
\family default
 and 
\series bold
not
\series default
 the 
\family typewriter
specify
\family default
 method for 
\family typewriter
out
\family default
.
 We know that not running the 
\family typewriter
specify
\family default
 method for 
\family typewriter
out
\family default
, a 
\family typewriter
molar_stream
\family default
 as we just discussed above, will leave us with 
\family typewriter
nc
\family default
 
\family typewriter
.fixed
\family default
 flags not set.
 So we deviously traded these flags for those belonging to 
\family typewriter
stoich_coef
\family default
, giving us a net of fixing one flag.
 If we had abided by all the steps above, we would have run the 
\family typewriter
specify
\family default
 method for 
\family typewriter
out
\family default
, then gone in and cleared the flags for 
\family typewriter
out.f[components]
\family default
 while setting those for 
\family typewriter
stoich_coef[components]
\family default
 in trade to get the flags we want set for this model.
 We did the equivalent with a shortcut.
\end_layout

\begin_layout Standard
If a model is parametric, the models defined by 
\family typewriter
WILL_BE
\family default
 in the parameter list should be viewed as new variables defined in the
 model.
 Remember 
\family typewriter
specify
\family default
 must fix sufficient variables to make an instance of this model square.
\end_layout

\begin_layout Standard
At each of the above steps, pay special attention to indexed variables used
 in 
\emph on
indexed
\emph default
 equations
\begin_inset Note Note
status collapsed

\begin_layout Plain Layout
need a cross reference here to the appropriate part of the manual
\end_layout

\end_inset

.
 Frequently you must fix or free 
\family typewriter
n
\family default
 or 
\family typewriter
n-1
\family default
 variables indexed over a set of size 
\family typewriter
n
\family default
, if there are 
\family typewriter
n
\family default
 matching equations.
 In general, if you think you have 
\family typewriter
specify
\family default
 correctly written, change the sizes of all the sets in your MODEL by one
 and then by two members.
 If your 
\family typewriter
specify
\family default
 method still works, you are probably using sets correctly.
 Pursuing 'symmetry', i.e.
 the identical treatment of all variables defined in a single array, usually
 helps you write 
\family typewriter
specify
\family default
 correctly.
\end_layout

\begin_layout Standard
When writing models that combine parts which do not share very well, or
 which both try to compute the same variable in different ways, it may even
 be necessary to write a 
\family typewriter
WHEN
\family default
 statement to selectively 'turn off' the conflicting equations or model
 fragments.
 An object or equation 
\family typewriter
USE
\family default
d in any 
\family typewriter
WHEN
\family default
 statement is turned off by default and becomes a part of the solved 
\family typewriter
MODEL
\family default
 only when the condition of some 
\family typewriter
CASE
\family default
 that 
\family typewriter
USE
\family default
s that object is matched.
\end_layout

\begin_layout Standard
The setting of boolean, integer, and symbol variables that are controlling
 conditions of 
\family typewriter
WHEN
\family default
 and 
\family typewriter
SWITCH
\family default
 statements should be done in the 
\family typewriter
specify
\family default
 method.
\end_layout

\begin_layout Standard
There is no 'one perfect' 
\family typewriter
specify
\family default
 method for all purposes.
 This routine should merely define a reasonably useful base configuration
 of the model.
 Other 
\family typewriter
specify_whatElseYouWant
\family default
 methods can also be written.
\end_layout

\begin_layout Section

\family typewriter
METHOD values
\begin_inset Index idx
status collapsed

\begin_layout Plain Layout
values
\end_layout

\end_inset


\end_layout

\begin_layout Standard
In a final application 
\family typewriter
MODEL
\family default
, you should record at least one set of input values (values of the fixed
 variables and guesses of key solved-for variables) that leads to a good
 solution.
 This facilitates testing of the model, and helps the next person using
 your model to be assured that it works as expected.
\end_layout

\begin_layout Section
Summary
\end_layout

\begin_layout Standard
\begin_inset Marginal
status collapsed

\begin_layout Plain Layout
adding our standard methods to a model definition
\end_layout

\end_inset

We have defined a set of standard methods for ASCEND models which we insist
 a modeler provide before we will allow a model to be placed in any of our
 model libraries.
 These are listed in Table 
\begin_inset CommandInset ref
LatexCommand ref
reference "tab:methods.stdMethodReqd"

\end_inset


\noun off
.
 As should be evident from above, not all models must have associated methods;
 our first vessel model did not.
 It is simply our policy that models in our libraries must have these methods
 to promote model reuse and to serve as examples of best practices in mathematic
al modeling.
\end_layout

\begin_layout Standard
\begin_inset Float table
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Caption Standard

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "tab:methods.stdMethodReqd"

\end_inset

Standard methods required for types in our ASCEND model library 
\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Tabular
<lyxtabular version="3" rows="8" columns="2">
<features rotate="0" islongtable="true" longtabularalignment="center">
<column alignment="center" valignment="top">
<column alignment="center" valignment="top" width="4in">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
method
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
description
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
default_self
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method called automatically when any simulation is compiled to provide
 default values and adjust bounds for any locally created variables which
 may have unsuitable defaultsin their ATOM definitions.
 Usually the variables selected are those for which the model becomes ill-behave
d if given poor initial guesses or bounds (e.g., zero).
 This method should include statements to run the default_self method for
 each of its locally created (IS_A'd) parts.
 This method should be written first.
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
ClearAll
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method to set all the fixed flags for variables in the type to FALSE.
 This puts these flags into a known standard state -- i.e., all are FALSE.
 All models inherit this method from the base model and the need to rewrite
 it is very, very rare.
 
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
specify
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method which assumes all the 
\family typewriter
fixed
\family default
 flags are currently 
\family typewriter
FALSE
\family default
 and which then sets a suitable set of fixed flags to 
\family typewriter
TRUE
\family default
 to make an instance of this type of model well-posed.
 A well-posed model is one that is square (
\begin_inset Formula $n$
\end_inset

 equations in 
\begin_inset Formula $n$
\end_inset

 unknowns) and solvable.
 
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
reset
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method which first runs the 
\family typewriter
ClearAll
\family default
 method and then the 
\family typewriter
specify
\family default
 method.
 We include this method because it is very convenient.
 We only have to run one method to make any simulation well-posed, no matter
 how its fixed flags are currently set.
 All models inherit this method from the base model, as with 
\family typewriter
ClearAll
\family default
.
 
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
values
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method to establish typical values for the variables we have fixed in
 an application or test model.
 We may also supply values for some of the variables we will be computing
 to aid in solving a model instance of this type.
 These values are ones that we have tested for simulation of this type and
 found good.
 
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
bound_self
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method to update the 
\family typewriter
.upper_bound
\family default
 and 
\family typewriter
.lower_bound
\family default
 value for each of the variables.
 ASCEND solvers use these bound values to help solve the model equations.
 This method should bound locally created variables and then call 
\family typewriter
bound_self
\family default
 for every locally created (
\family typewriter
IS_A
\family default
'd) part.
 
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\family typewriter
scale_self
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
a method to update the 
\family typewriter
.nominal
\family default
 value for each of the variables.
 ASCEND solvers will use these nominal values to rescale the variable to
 have a value of about one in magnitude to help solve the model equations.
 This method should re-scale locally created variables and then call 
\family typewriter
scale_self
\family default
 for every locally created (
\family typewriter
IS_A
\family default
'd) part.
 
\end_layout

\end_inset
</cell>
</row>
</lyxtabular>

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Section
Method writing automation 
\begin_inset CommandInset label
LatexCommand label
name "sec:methods.automation"

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Marginal
status collapsed

\begin_layout Plain Layout
Hit the button Library/Edit/Suggest methods and tweak the results
\end_layout

\end_inset

ASCEND will help you write the standard methods.
 Writing most of the standard methods can be nearly automated once the declarati
ve portion of the model definition is written.
 Usually, however, some minor tweaking of the automatically generated code
 is needed.
 In the Library window, the Edit menu has a "Suggest methods" button.
 Select a model you have written and read into the library, then hit this
 button.
 
\end_layout

\begin_layout Standard
In the Display window will appear a good starting point for the standard
 methods that you have not yet defined.
 This starting point follows the guidelines in this chapter.
 It saves you a lot of typing but it is a starting point only.
 Select and copy the text into the model you are editing, then tailor it
 to your needs and finish the missing bits.
 The comments in the generated code can be deleted before or after you copy
 the text to your model file.
\end_layout

\begin_layout Standard
If you have suggestions for general improvements to the generated method
 code, please mail them to us and include a sample of what the generated
 code ought to look like before the user performs any hand-editing.
 We aim to create easily understood and easily fixed method suggestions,
 not perfect suggestions, because procedural code style tastes vary so widely.
\end_layout

\begin_layout Standard
\begin_inset Note Note
status open

\begin_layout Plain Layout
Closing the chapter you find below the suggested methods ASCEND generates
 for the ***vessel*** model.
\end_layout

\end_inset


\end_layout

\end_body
\end_document