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 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
|
from __future__ import print_function
import os
import shutil
import datetime
import yaml
import subprocess
import copy
import warnings
import re
from IPython.display import IFrame
from libpyvinyl.BaseCalculator import BaseCalculator
from libpyvinyl.Parameters.Collections import CalculatorParameters
from mcstasscript.data.pyvinylData import pyvinylMcStasData, pyvinylMCPLData
from mcstasscript.data.MCPLDataFormat import MCPLDataFormat
from mcstasscript.helper.mcstas_objects import DeclareVariable
from mcstasscript.helper.mcstas_objects import provide_parameter
from mcstasscript.helper.mcstas_objects import write_parameter
from mcstasscript.helper.mcstas_objects import Component
from mcstasscript.helper.component_reader import ComponentReader
from mcstasscript.helper.managed_mcrun import ManagedMcrun
from mcstasscript.helper.formatting import is_legal_filename
from mcstasscript.helper.formatting import bcolors
from mcstasscript.helper.unpickler import CustomMcStasUnpickler, CustomMcXtraceUnpickler
from mcstasscript.helper.exceptions import McStasError
from mcstasscript.helper.beam_dump_database import BeamDumpDatabase
from mcstasscript.helper.check_mccode_version import check_mcstas_major_version
from mcstasscript.helper.check_mccode_version import check_mcxtrace_major_version
from mcstasscript.helper.name_inspector import find_python_variable_name
from mcstasscript.helper.search_statement import SearchStatement, SearchStatementList
from mcstasscript.instrument_diagram.make_diagram import instrument_diagram
from mcstasscript.instrument_diagnostics.intensity_diagnostics import IntensityDiagnostics
class McCode_instr(BaseCalculator):
"""
Main class for writing a McCode instrument using McStasScript
Initialization of McCode_instr sets the name of the instrument file
and its methods are used to add all aspects of the instrument file.
The class also holds methods for writing the finished instrument
file to disk and to run the simulation. This is meant as a base class
that McStas_instr and McXtrace_instr inherits from, these have to provide
some attributes. Inherits from libpyvinyl BaseCalculator in order to
harmonize input with other simulation engines.
Required attributes in superclass
---------------------------------
executable : str
Name of executable, mcrun or mxrun
particle : str
Name of probe particle, "neutron" or "x-ray"
package_name : str
Name of package, "McStas" or "McXtrace"
Attributes
----------
name : str
name of instrument file
author : str, default "Python Instrument Generator"
name of user of McStasScript, written to the file
origin : str, default "ESS DMSC"
origin of instrument file (affiliation)
input_path : str, default "."
directory in which simulation is executed, uses components found there
output_path : str
directory in which the data is written
executable_path : str
absolute path of mcrun command, or empty if it is in path
parameters : ParameterContainer
contains all input parameters to be written to file
declare_list : list of DeclareVariable instances
contains all declare parrameters to be written to file
initialize_section : str
string containing entire initialize section to be written
trace_section : str
string containing trace section (OBSOLETE)
finally_section : str
string containing entire finally section to be written
component_list : list of component instances
list of components in the instrument
component_name_list : list of strings
list of names of the components in the instrument
component_class_lib : dict
dict of custom Component classes made at run time
component_reader : ComponentReader
ComponentReader instance for reading component files
package_path : str
Path to mccode package containing component folders
run_settings : dict
Dict of options set with settings
data : list
List of McStasData objects produced by last run
Methods
-------
add_parameter(*args, **kwargs)
Adds input parameter to the define section
add_declare_var(type, name)
Add declared variable called name of given type to the declare section
append_declare(string)
Appends to the declare section
append_initialize(string)
Appends a string to the initialize section, then adds new line
append_initialize_no_new_line(string)
Appends a string to the initialize section
append_finally(string)
Appends a string to finally section, then adds new line
append_finally_no_new_line(string)
Appends a string to finally section
append_trace(string)
Obsolete method, add components instead (used in write_c_files)
append_trace_no_new_line(string)
Obsolete method, add components instead (used in write_c_files)
available_components(string)
Shows available components in given category
component_help(name)
Shows help on component of given name
add_component(instance_name, component_name, **kwargs)
Add a component to the instrument file
copy_component(new_component_name, original_component, **kwargs)
Makes a copy of original_component with new_component_name
get_component(instance_name)
Returns component instance with name instance_name
get_last_component()
Returns component instance of last component
set_component_parameter(instance_name, dict)
Adds parameters as dict to component with instance_name
set_component_AT(instance_name, AT_data, **kwargs)
Sets position of component named instance_name
set_component_ROTATED(instance_name, ROTATED_data, **kwargs)
Sets rotation of component named instance_name
set_component_RELATIVE(instane_name, string)
Sets position and rotation reference for named component
set_component_WHEN(instance_name, string)
Sets WHEN condition of named component, is logical c expression
set_component_GROUP(instance_name, string)
Sets GROUP name of component named instance_name
append_component_EXTEND(instance_name, string)
Appends a line to EXTEND section of named component
set_component_JUMP(instance_name, string)
Sets JUMP code for named component
set_component_SPLIT(instance_name, string)
Sets SPLIT value for named component
set_component_c_code_before(instance_name, string)
Sets c code before the component
set_component_c_code_after(instance_name, string)
Sets c code after the component
set_component_comment(instance_name, string)
Sets comment to be written before named component
print_component(instance_name)
Prints an overview of current state of named component
print_component_short(instance_name)
Prints short overview of current state of named component
show_components()
Prints overview of postion / rotation of all components
write_c_files()
Writes c files for %include in generated_includes folder
write_full_instrument()
Writes full instrument file to current directory
show_instrument()
Shows instrument using mcdisplay
set_parameters(dict)
Inherited from libpyvinyl BaseCalculator
settings(**kwargs)
Sets settings for performing simulation
backengine()
Performs simulation, saves in data attribute
run_full_instrument(**kwargs)
Depricated method for performing the simulation
interface()
Shows interface with jupyter notebook widgets
get_interface_data()
Returns data set from latest simulation in widget
"""
def __init__(self, name, parameters=None, author=None,
origin=None, ncount=None, mpi="not_set", seed=None,
force_compile=None, output_path=None,
increment_folder_name=None, custom_flags=None,
executable_path=None, executable=None,
suppress_output=None, gravity=None, input_path=None,
package_path=None, checks=None, NeXus=None,
save_comp_pars=None, openacc=None):
"""
Initialization of McStas Instrument
Parameters
----------
name : str
Name of project, instrument file will be name + ".instr"
keyword arguments:
parameters : ParameterContainer or CalculatorParameters
Parameters for this instrument
author : str
Name of author, written in instrument file
origin : str
Affiliation of author, written in instrument file
input_path : str
Work directory, will load components from this folder
mpi : int
Number of mpi threads to use in simulation
output_path : str
Sets data_folder_name
increment_folder_name : bool
Will update output_path if folder already exists, default True
ncount : int
Sets ncount
mpi : int
Sets thread count
force_compile : bool
If True (default) new instrument file is written, otherwise not
custom_flags : str
Sets custom_flags passed to mcrun
executable : str
Name of the executable
executable_path : str
Path to mcrun command, "" if already in path
suppress_output : bool
Set to True to surpress output
gravity : bool
If True, gravity will be simulated
save_comp_pars : bool
If True, McStas run writes all comp pars to disk
"""
super().__init__(name, input=[],
output_keys=[name + "_data"],
output_data_types=[pyvinylMcStasData],
parameters=parameters)
# Check required attributes has been set by class that inherits
if not (hasattr(self, "particle") or
hasattr(self, "package_name")):
raise AttributeError("McCode_instr is a base class, use "
+ "McStas_instr or McXtrace_instr instead.")
if not is_legal_filename(self.name + ".instr"):
raise NameError("The instrument is called: \""
+ self.name
+ "\" resulting in an instrument file named: \""
+ self.name + ".instr"
+ "\" which is not a legal filename")
if self.calculator_base_dir == "BaseCalculator":
# If the base_dir was not set, set default to depend on instrument name
self.calculator_base_dir = self.name + "_data"
if author is None:
self.author = "Python " + self.package_name
self.author += " Instrument Generator"
else:
self.author = str(author)
if origin is None:
self.origin = "ESS DMSC"
else:
self.origin = str(origin)
# Attempt to classify given parameters in McStas context
for parameter in self.parameters.parameters.values():
if isinstance(parameter.value, (float, int)):
parameter.type = "double"
elif isinstance(parameter.value, (str)):
parameter.type = "string"
else:
# They will be typed when the instrument is written
parameter.type = None
self._run_settings = {} # Settings for running simulation
# Sets max_line_length and adds paths to run_settings
self._read_calibration()
# Settings that can't be changed later
if input_path is not None:
self.input_path = str(input_path)
if not os.path.isdir(self.input_path):
raise RuntimeError("Given input_path does not point to a "
+ "folder:\"" + self.input_path + '"')
else:
self.input_path = "."
self._run_settings["run_path"] = self.input_path
if package_path is not None:
if not os.path.isdir(str(package_path)):
raise RuntimeError("The package_path provided to mccode_instr "
+ " does not point to a + directory: \""
+ str(package_path) + "\"")
self._run_settings["package_path"] = package_path
# Settings for run that can be adjusted by user
provided_run_settings = {"executable": executable,
"checks": True,
"NeXus": False}
if executable_path is not None:
provided_run_settings["executable_path"] = str(executable_path)
if force_compile is not None:
provided_run_settings["force_compile"] = force_compile
else:
provided_run_settings["force_compile"] = True
if ncount is not None:
provided_run_settings["ncount"] = ncount
if mpi != "not_set":
provided_run_settings["mpi"] = mpi
if gravity is not None:
provided_run_settings["gravity"] = gravity
if seed is not None:
provided_run_settings["seed"] = str(seed)
if custom_flags is not None:
provided_run_settings["custom_flags"] = custom_flags
if suppress_output is not None:
provided_run_settings["suppress_output"] = suppress_output
if checks is not None:
provided_run_settings["checks"] = checks
if output_path is not None:
provided_run_settings["output_path"] = output_path
if increment_folder_name is not None:
provided_run_settings["increment_folder_name"] = increment_folder_name
if NeXus is not None:
provided_run_settings["NeXus"] = NeXus
if save_comp_pars is not None:
provided_run_settings["save_comp_pars"] = save_comp_pars
else:
provided_run_settings["save_comp_pars"] = False
if openacc is not None:
provided_run_settings["openacc"] = openacc
# Set run_settings, perform input sanitation
self.settings(**provided_run_settings)
# Read info on active McStas components
package_path = self._run_settings["package_path"]
run_path = self._run_settings["run_path"]
self.component_reader = ComponentReader(package_path,
input_path=run_path)
self.component_class_lib = {}
self.widget_interface = None
# Holds major version of underlying package
self.mccode_version = None
# Avoid initializing if loading from dump
if not hasattr(self, "declare_list"):
self.declare_list = []
self.user_var_list = []
self.dependency_statement = ""
self.search_statement_list = SearchStatementList()
self.initialize_section = ("// Start of initialize for generated "
+ name + "\n")
self.trace_section = ("// Start of trace section for generated "
+ name + "\n")
self.finally_section = ("// Start of finally for generated "
+ name + "\n")
# Handle components
self.component_list = [] # List of components (have to be ordered)
# Run subset settings
self.run_from_ref = None
self.run_to_ref = None
self.run_to_comment = ""
self.run_to_name = None
self.run_from_component_parameters = None
self.run_to_component_parameters = None
# DumpDatabase
self.dump_database = BeamDumpDatabase(self.name, self.input_path)
@property
def output_path(self) -> str:
return self.base_dir
@output_path.setter
def output_path(self, value: str) -> None:
self.calculator_base_dir = value
def init_parameters(self):
"""
Create empty ParameterContainer for new instrument
"""
self.parameters = CalculatorParameters()
def _read_calibration(self):
"""
Placeholder method that should be overwritten by classes
that inherit from McCode_instr.
"""
pass
def reset_run_points(self):
"""
Reset run_from and run_to points to the full instrument
"""
self.run_from_ref = None
self.run_to_ref = None
def show_run_subset(self):
"""
Shows current subset of instrument selected with run_from and run_to methods
"""
if self.run_from_ref is None and self.run_to_ref is None:
print("No run_from or run_to point set.")
return
if self.run_from_ref is None:
print("Running from start of the instrument", end="")
else:
print(f"Running from component named '{self.run_from_ref}'", end="")
if self.run_to_ref is None:
print(" to the end of the instrument.")
else:
print(f" to component named '{self.run_to_ref}'.")
def run_to(self, component_ref, run_name="Run", comment=None, **kwargs):
"""
Set limit for instrument, only run to given component, save MCPL there
The method accepts keywords for the MCPL output component allowing to
store for example userflags or setting the filename directly.
component_ref : str / component object
Name of component where the instrument simulation should end
run_name : str
Name associated with the generated beam dump
comment : str
Comment asscoiated with the generated beam dump
"""
if isinstance(component_ref, Component):
component_ref = component_ref.name
# Check references are valid
self.subset_check(start_ref=self.run_from_ref, end_ref=component_ref)
self.run_to_ref = component_ref
self.run_to_name = run_name
if comment is not None:
self.run_to_comment = str(comment)
else:
self.run_to_comment = ""
if component_ref is not None:
mcpl_par_name = "run_to_mcpl"
if mcpl_par_name not in self.parameters.parameters:
# Need to add parameter to instrument for mcpl filename
self.add_parameter("string", mcpl_par_name)
if "filename" not in kwargs:
# Generate a reasonable filename
auto_name = '"' + self.name + "_" + component_ref + ".mcpl" + '"'
self.set_parameters({mcpl_par_name: auto_name})
else:
# Set the instrument parameter to the given filename
self.set_parameters({mcpl_par_name: kwargs["filename"]})
# Set the mcpl component parameter to the filename instrument parameter
kwargs["filename"] = mcpl_par_name
# Check the given keywords arguments are legal for the MCPL output component
dummy_MCPL = self._create_component_instance("MCPL_output", "MCPL_output")
try:
dummy_MCPL.set_parameters(kwargs)
except:
# Provide information on what stage caused the problem
print("Problems detected with input arguments for MCPL output component")
# Show the exception for the failure to set parameters on the component
dummy_MCPL.set_parameters(kwargs)
# Store parameters for the MCPL output component
self.run_to_component_parameters = kwargs
def run_from(self, component_ref, run_name=None, tag=None, **kwargs):
"""
Set limit for instrument, only run from given component, load MCPL ot start
The method accepts keywords for the MCPL input component allowing to
set for example the smear for direction / energy / position and
repeat count.
component_ref : str / component object
Name of component where the instrument simulation should start
run_name : str
Run name of dump to use as starting point of simulation
tag : integer
Tag of the desired dump (only allowed if run_name is given)
"""
if isinstance(component_ref, Component):
component_ref = component_ref.name
# Check references are valid
self.subset_check(start_ref=component_ref, end_ref=self.run_to_ref)
self.run_from_ref = component_ref
if component_ref is not None:
mcpl_par_name = "run_from_mcpl"
if mcpl_par_name not in self.parameters.parameters:
# Need to add parameter to instrument for mcpl filename
self.add_parameter("string", mcpl_par_name)
if "filename" not in kwargs:
# Find newest dump from database
newest_dump = self.dump_database.newest_at_point(component_ref, run=run_name)
auto_name = '"' + newest_dump.data["data_path"] + '"'
self.set_parameters({mcpl_par_name: auto_name})
else:
self.set_parameters({mcpl_par_name: kwargs["filename"]})
if run_name is not None and tag is not None:
dump = self.dump_database.get_dump(component_ref, run_name, tag)
dump_filename = '"' + dump.data["data_path"] + '"'
self.set_parameters({mcpl_par_name: dump_filename})
kwargs["filename"] = mcpl_par_name
# Ensure the kwargs are allowed
dummy_MCPL = self._create_component_instance("MCPL_input", "MCPL_input")
try:
dummy_MCPL.set_parameters(kwargs)
except:
print("Problems detected with input arguments for MCPL input component")
dummy_MCPL.set_parameters(kwargs)
self.run_from_component_parameters = kwargs
def show_dumps(self):
component_names = [x.name for x in self.component_list]
self.dump_database.show_in_order(component_names)
def show_dump(self, point, run_name=None, tag=None):
if isinstance(point, Component):
point = point.name
self.dump_database.get_dump(point, run_name, tag).print_all()
def subset_check(self, start_ref, end_ref):
"""
Checks that when the instrument is broken into subsets, it is still valid
start_ref : str
Name of starting component
end_ref : str
Name of end component
"""
start_i, end_i = self.get_component_subset_index_range(start_ref, end_ref)
if start_i > end_i:
raise McStasError("Running from '" + start_ref + "' to '" + end_ref
+ "' not possible as '" + end_ref + "' is before '"
+ start_ref + "' in the component sequence.")
if start_i == end_i:
raise McStasError("Running from and to the same component is not supported "
+ "here both run_from and run_to are set to "
+ "'" + start_ref + "'.")
# Check current subset of instrument is self contained
is_self_contained = True
try:
self.check_for_relative_errors(start_ref=start_ref, end_ref=end_ref)
except McStasError:
is_self_contained = False
if not is_self_contained:
print("When using a subset of the instrument, component references "
"must be only internal as these sections are split into separate "
"files. When seeing only the specified subset of the instrument, "
"this reference can not be resolved.")
self.check_for_relative_errors(start_ref=start_ref, end_ref=end_ref)
if end_ref is None:
# If there is no end_ref, there are no parts after to check
return
# Check the part after is self consistent
is_self_contained = True
try:
self.check_for_relative_errors(start_ref=end_ref, allow_absolute=False)
except McStasError:
is_self_contained = False
if not is_self_contained:
print("When using a subset of the instrument, component references "
"must be only internal as these sections are split into separate "
"files. When seeing only the specified subset of the instrument, "
"this reference can not be resolved. \n"
"In this case the remaining instrument would fail.")
self.check_for_relative_errors(start_ref=end_ref, allow_absolute=False)
def add_parameter(self, *args, **kwargs):
"""
Method for adding input parameter to instrument
Type does not need to be specified, McStas considers that a floating
point value with the type 'double'. Uses libpyvinyl Parameter object.
Examples
--------
Creates a parameter with name wavelength and associated comment
add_parameter("wavelength", comment="wavelength in [AA]")
Creates a parameter with name A3 and default value
add_parameter("A3", value=30, comment="A3 angle in [deg]")
Creates a parameter with type string and name sample_name
add_parameter("string", "sample_name")
Parameters
----------
(optional) parameter type : str
type of input parameter, double, int, string
parameter name : str
name of parameter
keyword arguments
value : float, int or str
Default value of parameter
unit : str
Unit to be displayed
comment : str
Comment displayed next to declaration of parameter
options : list or value
list or value of allowed values for this parameter
"""
names = [x.name for x in self.declare_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.user_var_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.parameters.parameters.values()]
par = provide_parameter(*args, **kwargs)
if par.name in names:
raise NameError(f"A parameter or variable with name '{par.name}'"
f" already exists!")
self.parameters.add(par)
return par
def show_parameters(self, line_length=None):
"""
Method for displaying current instrument parameters
line_limit : int
Maximum line length for terminal output
"""
if len(self.parameters.parameters) == 0:
print("No instrument parameters available")
return
if line_length is None:
line_length = self.line_limit
# Find longest fields
types = []
names = []
values = []
comments = []
for parameter in self.parameters.parameters.values():
types.append(str(parameter.type))
names.append(str(parameter.name))
values.append(str(parameter.value))
if parameter.comment is None:
comments.append("")
else:
comments.append(str(parameter.comment))
longest_type = len(max(types, key=len))
longest_name = len(max(names, key=len))
longest_value = len(max(values, key=len))
# In addition to the data 11 characters are added before the comment
comment_start_point = longest_type + longest_name + longest_value + 11
longest_comment = len(max(comments, key=len))
length_for_comment = line_length - comment_start_point
# Print to console
for parameter in self.parameters.parameters.values():
print(str(parameter.type).ljust(longest_type), end=' ')
print(str(parameter.name).ljust(longest_name), end=' ')
if parameter.value is None:
print(" ", end=' ')
print(" ".ljust(longest_value + 1), end=' ')
else:
print(" =", end=' ')
print(str(parameter.value).ljust(longest_value + 1), end=' ')
if parameter.comment is None:
c_comment = ""
else:
c_comment = "// " + str(parameter.comment)
if (length_for_comment < 5
or length_for_comment > len(c_comment)):
print(c_comment)
else:
# Split comment into several lines
comment = c_comment
words = comment.split(" ")
words_left = len(words)
last_index = 0
current_index = 0
comment = ""
iterations = 0
max_iterations = 50
while words_left > 0:
iterations += 1
if iterations > max_iterations:
# Something went long, print on one line
break
line_left = length_for_comment
while line_left > 0:
if current_index >= len(words):
current_index = len(words) + 1
break
line_left -= len(str(words[current_index])) + 1
current_index += 1
current_index -= 1
for word in words[last_index:current_index]:
comment += word + " "
words_left = len(words) - current_index
if words_left > 0:
comment += "\n" + " " * comment_start_point
last_index = current_index
if not iterations == max_iterations + 1:
print(comment)
else:
print(c_comment.ljust(longest_comment))
def show_variables(self):
"""
Shows declared variables in instrument
"""
all_variables = [x for x in self.declare_list + self.user_var_list
if isinstance(x, DeclareVariable)]
type_heading = "type"
variable_types = [x.type for x in all_variables]
variable_types.append(type_heading)
max_type_length = len(max(variable_types, key=len))
name_heading = "variable name"
variable_names = [x.name for x in all_variables]
variable_names.append(name_heading)
max_name_length = len(max(variable_names, key=len))
vector_heading = "array length"
variable_vector = [str(x.vector) for x in all_variables]
variable_vector.append(vector_heading)
max_vector_length = len(max(variable_vector, key=len))
value_heading = "value"
variable_values = [str(x.value) for x in all_variables]
variable_values.append(value_heading)
max_value_length = len(max(variable_values, key=len))
padding = 2
header = ""
header += type_heading.ljust(max_type_length + padding)
header += name_heading.ljust(max_name_length + padding)
header += vector_heading.ljust(max_vector_length + padding)
header += value_heading.ljust(max_value_length + padding)
header += "\n"
header += "-"*(max_type_length + max_name_length + max_value_length
+ max_vector_length + 3*padding)
header += "\n"
string = "DECLARE VARIABLES \n"
string += header
if len(self.user_var_list) > 0:
first_user_var = self.user_var_list[0]
else:
first_user_var = None
for variable in all_variables:
if variable is first_user_var:
string += "\n"
string += "USER VARIABLES (per neutron, only use in EXTEND)\n"
string += header
string += str(variable.type).ljust(max_type_length + padding)
string += str(variable.name).ljust(max_name_length + padding)
if variable.vector != 0:
vector_string = str(variable.vector)
else:
vector_string = ""
string += vector_string.ljust(max_vector_length + padding)
if variable.value != "":
value_string = str(variable.value)
else:
value_string = ""
string += value_string.ljust(max_value_length + padding)
string += "\n"
print(string)
def add_declare_var(self, *args, **kwargs):
"""
Method for adding declared variable to instrument
Parameters
----------
parameter type : str
type of input parameter
parameter name : str
name of parameter
keyword arguments
array : int
default 0 for scalar, if specified length of array
value : any
Initial value of parameter, can be list of length vector
comment : str
Comment displayed next to declaration of parameter
"""
# DeclareVariable class documented independently
declare_par = DeclareVariable(*args, **kwargs)
names = [x.name for x in self.declare_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.user_var_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.parameters.parameters.values()]
if declare_par.name in names:
raise NameError("Variable with name '" + declare_par.name
+ "' already present in instrument!")
self.declare_list.append(declare_par)
return declare_par
def append_declare(self, string):
"""
Method for appending code to the declare section directly
This method is not meant for declaring simple variables which
should be done using add_declare_var. This method can be used
to declare functions, structures and unions directly.
Parameters
----------
string : str
code to be added to declare section
"""
self.declare_list.append(string)
def add_user_var(self, *args, **kwargs):
"""
Method for adding user variable to instrument
Parameters
----------
parameter type : str
type of input parameter
parameter name : str
name of parameter
keyword arguments
array : int
default 0 for scalar, if specified length of array
comment : str
Comment displayed next to declaration of parameter
"""
if "value" in kwargs:
raise ValueError("Value not allowed for UserVars.")
# DeclareVariable class documented independently
user_par = DeclareVariable(*args, **kwargs)
names = [x.name for x in self.declare_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.user_var_list
if isinstance(x, DeclareVariable)]
names += [x.name for x in self.parameters.parameters.values()]
if user_par.name in names:
raise NameError("Variable with name '" + user_par.name
+ "' already present in instrument!")
self.user_var_list.append(user_par)
return user_par
def move_user_vars_to_declare(self):
"""
Moves all uservars to declare for compatibility with McStas 2.X
"""
for var in self.user_var_list:
self.declare_list.append(var)
self.user_var_list = []
def append_initialize(self, string):
"""
Method for appending code to the initialize section
The initialize section consists of c code and will be compiled,
thus any syntax errors will crash the simulation. Code is added
on a new line for each call to this method.
Parameters
----------
string : str
code to be added to initialize section
"""
self.initialize_section = self.initialize_section + string + "\n"
def append_initialize_no_new_line(self, string):
"""
Method for appending code to the initialize section, no new line
The initialize section consists of c code and will be compiled,
thus any syntax errors will crash the simulation. Code is added
to the current line.
Parameters
----------
string : str
code to be added to initialize section
"""
self.initialize_section = self.initialize_section + string
def append_finally(self, string):
"""
Method for appending code to the finally section of instrument
The finally section consists of c code and will be compiled,
thus any syntax errors will crash the simulation. Code is added
on a new line for each call to this method.
Parameters
----------
string : str
code to be added to finally section
"""
self.finally_section = self.finally_section + string + "\n"
def append_finally_no_new_line(self, string):
"""
Method for appending code to the finally section of instrument
The finally section consists of c code and will be compiled,
thus any syntax errors will crash the simulation. Code is added
to the current line.
Parameters
----------
string : str
code to be added to finally section
"""
self.finally_section = self.finally_section + string
"""
# Handle trace string differently when components also exists
# A) Could have trace string as a component attribute and set
# it before / after
# B) Could have trace string as a McStas_instr attribute and
# still attach placement to components
# C) Could have trace string as a different object and place it
# in component_list, but have a write function named as the
# component write function?
"""
def append_trace(self, string):
"""
Appends code to trace section, only used in write_c_files
The most common way to add code to the trace section is to add
components using the seperate methods for this. This method is
kept as is still used for writing to c files used in legacy
code. Each call creates a new line.
Parameters
----------
string : str
code to be added to trace
"""
self.trace_section = self.trace_section + string + "\n"
def append_trace_no_new_line(self, string):
"""
Appends code to trace section, only used in write_c_files
The most common way to add code to the trace section is to add
components using the seperate methods for this. This method is
kept as is still used for writing to c files used in legacy
code. No new line is made with this call.
Parameters
----------
string : str
code to be added to trace
"""
self.trace_section = self.trace_section + string
def available_components(self, *args):
"""
Helper method that shows available components to the user
If called without any arguments it will display the available
component categories. If a category is given as a string in
the first input, components in that category are printed.
Parameters
----------
first argument (optional): str
Category that matches one of the McStas component folders
"""
if len(args) == 0:
print("Here are the available component categories:")
self.component_reader.show_categories()
print("Call available_components(category_name) to display")
else:
category = args[0]
print("Here are all components in the "
+ category
+ " category.")
this_reader = self.component_reader
line_lim = self.line_limit
this_reader.show_components_in_category(category,
line_length=line_lim)
def component_help(self, name, **kwargs):
"""
Method for showing parameters for a component before adding it
to the instrument
keyword arguments
line_length : int
Maximum line length in output to terminal
"""
dummy_instance = self._create_component_instance("dummy", name)
dummy_instance.show_parameters(**kwargs)
def _create_component_instance(self, name, component_name, **kwargs):
"""
Dynamically creates a class for the requested component type
Created classes kept in dictionary, if the same component type
is requested again, the class in the dictionary is used. The
method returns an instance of the created class that was
initialized with the parameters passed to this function.
"""
if component_name not in self.component_class_lib:
comp_info = self.component_reader.read_name(component_name)
input_dict = {key: None for key in comp_info.parameter_names}
input_dict["parameter_names"] = comp_info.parameter_names
input_dict["parameter_defaults"] = comp_info.parameter_defaults
input_dict["parameter_types"] = comp_info.parameter_types
input_dict["parameter_units"] = comp_info.parameter_units
input_dict["parameter_comments"] = comp_info.parameter_comments
input_dict["category"] = comp_info.category
input_dict["line_limit"] = self.line_limit
dynamic_component_class = type(component_name, (Component,),
input_dict)
# add this class to globals to allow for pickling
globals()[component_name] = dynamic_component_class
self.component_class_lib[component_name] = dynamic_component_class
return self.component_class_lib[component_name](name, component_name,
**kwargs)
def add_component(self, name, component_name=None, *, before=None,
after=None, AT=None, AT_RELATIVE=None, ROTATED=None,
ROTATED_RELATIVE=None, RELATIVE=None, WHEN=None,
EXTEND=None, GROUP=None, JUMP=None, SPLIT=None,
comment=None, c_code_before=None, c_code_after=None):
"""
Method for adding a new Component instance to the instrument
Creates a new Component instance in the instrument. This
requires a unique instance name of the component to be used for
future reference and the name of the McStas component to be
used. The component is placed at the end of the instrument file
unless otherwise specified with the after and before keywords.
The Component may be initialized using other keyword arguments,
but all attributes can be set with approrpiate methods.
Parameters
----------
name : str
Unique name of component instance
component_name : str
Name of McStas component to create instance of
Keyword arguments:
after : str
Place this component after component with given name
before : str
Place this component before component with given name
AT : List of 3 floats
Sets AT_data, position relative to reference
AT_RELATIVE : str
Sets reference component for postion
ROTATED : List of 3 floats
Sets ROTATED_data, rotation relative to reference
ROTATED_RELATIVE : str
Sets reference component for rotation
RELATIVE : str
Sets reference component for both position and rotation
WHEN : str
Sets when condition which must be a logical c expression
EXTEND : str
Initialize the extend section with a line of c code
GROUP : str
Name of the group this component should belong to
JUMP : str
Set code for McStas JUMP statement
comment : str
Comment that will be displayed before the component
"""
if component_name is None:
# Try to interpret name as the name of a McStas component
# and the python variable name as the given name to this
# instance of the McStas component.
if name in self.component_reader.component_path:
# Name is an available McStas component!
component_name = name
# Find name through call
text = ("When adding a component without giving both "
"name and type it is necessary to assign the "
"component object a python variable name that "
"can be used for the McStas component.")
name = find_python_variable_name(error_text=text, n_levels=2)
else:
raise NameError("As no name is given, the input is interpreted"
" as a component name, yet no component of"
" type " + str(name) + " is found in McStas"
" installation or work directory.")
if name in [x.name for x in self.component_list]:
raise NameError(("Component name \"" + str(name)
+ "\" used twice, " + self.package_name
+ " does not allow this."
+ " Rename or remove one instance of this"
+ " name."))
# Condense keyword input relating to component to a dict
component_input = {"AT": AT, "AT_RELATIVE": AT_RELATIVE,
"ROTATED": ROTATED,
"ROTATED_RELATIVE": ROTATED_RELATIVE,
"RELATIVE": RELATIVE, "WHEN": WHEN,
"EXTEND": EXTEND, "GROUP": GROUP, "JUMP": JUMP,
"SPLIT": SPLIT, "comment": comment,
"c_code_before": c_code_before,
"c_code_after": c_code_after}
new_component = self._create_component_instance(name, component_name,
**component_input)
self._insert_component(new_component, before=before, after=after)
return new_component
def copy_component(self, name, original_component=None, *, before=None,
after=None, AT=None, AT_RELATIVE=None, ROTATED=None,
ROTATED_RELATIVE=None, RELATIVE=None, WHEN=None,
EXTEND=None, GROUP=None, JUMP=None, SPLIT=None,
comment=None, c_code_before=None, c_code_after=None):
"""
Method for adding a copy of a Component instance to the instrument
Creates a copy of Component instance in the instrument. This
requires a unique instance name of the component to be used for
future reference and the name of the McStas component to be
used. The component is placed at the end of the instrument file
unless otherwise specified with the after and before keywords.
The component may be initialized using other keyword arguments,
but all attributes can be set with approrpiate methods.
Parameters
----------
name : str
Unique name of component instance
original_component : str
Name of component instance to create copy of
Keyword arguments:
after : str
Place this component after component with given name
before : str
Place this component before component with given name
AT : List of 3 floats
Sets AT_data, position relative to reference
AT_RELATIVE : str
Sets reference component for postion
ROTATED : List of 3 floats
Sets ROTATED_data, rotation relative to reference
ROTATED_RELATIVE : str
Sets reference component for rotation
RELATIVE : str
Sets reference component for both position and rotation
WHEN : str
Sets when condition which must be a logical c expression
EXTEND : str
Initialize the extend section with a line of c code
GROUP : str
Name of the group this component should belong to
JUMP : str
Set code for McStas JUMP statement
comment : str
Comment that will be displayed before the component
"""
if original_component is None:
# Try to interpret name as the name of a McStas component
# to be copied and the python variable name as the given
# name to the new instance of the McStas component.
if isinstance(name, Component):
name = original_component.name
component_names = [x.name for x in self.component_list]
if name in component_names:
# name is an existing component name
original_component = name
# Find new name through call
text = ("When making a component copy without providing both "
"name of the new instance and name of the original "
"component it is necessary to assign the new "
"component object to a python variable name that can "
"be used for the new McStas component object.")
name = find_python_variable_name(error_text=text, n_levels=2)
else:
raise NameError("As no name is given, the input is interpreted"
" as a component name, yet no component named"
+ str(name) + " is found in the McStas "
"instrument.")
if isinstance(original_component, Component):
original_component = original_component.name
# Condense keyword input relating to component to a dict
component_input = {"AT": AT, "AT_RELATIVE": AT_RELATIVE,
"ROTATED": ROTATED,
"ROTATED_RELATIVE": ROTATED_RELATIVE,
"RELATIVE": RELATIVE, "WHEN": WHEN,
"EXTEND": EXTEND, "GROUP": GROUP, "JUMP": JUMP,
"SPLIT": SPLIT, "comment": comment,
"c_code_before": c_code_before,
"c_code_after": c_code_after}
"""
If the name starts with COPY, use unique naming as described in the
McStas manual.
"""
component_names = [x.name for x in self.component_list]
if name.startswith("COPY("):
target_name = name.split("(", 1)[1]
target_name = target_name.split(")", 1)[0]
instance_name = target_name
label = 0
instance_name = target_name + "_" + str(label)
while instance_name in component_names:
instance_name = target_name + "_" + str(label)
label += 1
if name in component_names:
raise NameError(("Component name \"" + str(name)
+ "\" used twice, " + self.package_name
+ " does not allow this."
+ " Rename or remove one instance of this"
+ " name."))
if original_component not in component_names:
raise NameError("Component name \"" + str(original_component)
+ "\" was not found in the " + self.package_name
+ " instrument. and thus can not be copied.")
else:
component_to_copy = self.get_component(original_component)
new_component = copy.deepcopy(component_to_copy)
new_component.name = name # Set new name, duplicate names not allowed
self._insert_component(new_component, before=before, after=after)
# Run set_keyword_input for keyword arguments to take effect
new_component.set_keyword_input(**component_input)
return new_component
def remove_component(self, name):
"""
Removes component with given name from instrument
"""
# Check for errors before
errors_before = self.has_errors()
if isinstance(name, Component):
name = name.name
component_names = [x.name for x in self.component_list]
index_to_remove = component_names.index(name)
self.component_list.pop(index_to_remove)
# Check for errors after removing
errors_after = self.has_errors()
if not errors_before and errors_after:
print("Removing the component '" + name + "' introduced errors in "
"the instrument, run check_for_errors() for more "
"information.")
def move_component(self, name, before=None, after=None):
"""
Moves component with given name to before or after
"""
if isinstance(name, Component):
name = name.name
if before is None and after is None:
raise RuntimeError("Must specify 'before' or 'after' when moving "
"a component.")
# Check for errors before
errors_before = self.has_errors()
if isinstance(name, Component):
name = name.name
component_names = [x.name for x in self.component_list]
index_to_remove = component_names.index(name)
moved_component = self.component_list.pop(index_to_remove)
self._insert_component(moved_component, before=before, after=after)
# Check for errors after moving
errors_after = self.has_errors()
if not errors_before and errors_after:
print("Moving the component '" + name + "' introduced errors in "
"the instrument, run check_for_errors() for more "
"information.")
def _insert_component(self, component, before=None, after=None):
"""
Inserts component into sequence of components held by instrument
Internal method to handle placement of a new component in the
list of components held by this instrument.
name : str
Instance name of component
component : Component object
Component object to be inserted
before : str or Component object
Reference to component to place this one before
after : str or Component object
Reference to coponent to place this one after
"""
if before is not None and after is not None:
raise RuntimeError("Only specify either 'before' or 'after'.")
if before is None and after is None:
# If after and before keywords absent, place component at the end
self.component_list.append(component)
return
if after is not None:
index_addition = 1
reference = after
description = "after"
if before is not None:
index_addition = 0
reference = before
description = "before"
if isinstance(reference, Component):
reference = reference.name
component_names = [x.name for x in self.component_list]
if reference not in component_names:
raise NameError("Trying to add a component " + description
+ " a component named '" + str(after)
+ "', but a component with that name was"
+ " not found.")
new_index = component_names.index(reference) + index_addition
self.component_list.insert(new_index, component)
def get_component(self, name):
"""
Get the component instance of component with specified name
This method is used to get direct access to any component
instance in the instrument. The component instance can be
manipulated in much the same way, but it is not necessary to
specify the name in each call.
Parameters
----------
name : str
Unique name of component whose instance should be returned
"""
component_names = [x.name for x in self.component_list]
if name in component_names:
index = component_names.index(name)
return self.component_list[index]
else:
raise NameError(("No component was found with name \""
+ str(name) + "\"!"))
def get_last_component(self):
"""
Get the component instance of last component in the instrument
This method is used to get direct access to any component
instance in the instrument. The component instance can be
manipulated in much the same way, but it is not necessary to
specify the name in each call.
"""
return self.component_list[-1]
def print_component(self, name):
"""
Method for printing summary of contents in named component
Parameters
----------
name : str
Unique name of component to print
"""
component = self.get_component(name)
component.print_long()
def print_component_short(self, name):
"""
Method for printing summary of contents in named component
Parameters
----------
name : str
Unique name of component to print
"""
component = self.get_component(name)
component.print_short()
def print_components(self, line_length=None):
"""
Obsolete method, use show_components instead
Method for printing overview of all components in instrument
Provides overview of component names, what McStas component is
used for each and their position and rotation in space.
keyword arguments:
line_length : int
Maximum line length in console
"""
warnings.warn("Print components is changing name to show_components for consistency.")
self.show_components(line_length)
def show_components(self, line_length=None):
"""
Method for printing overview of all components in instrument
Provides overview of component names, what McStas component is
used for each and their position and rotation in space.
keyword arguments:
line_length : int
Maximum line length in console
"""
if len(self.component_list) == 0:
print("No components added to instrument object yet.")
return
printed_components = self.make_component_subset()
if len(printed_components) == 0:
print("No components in subset.")
return
if self.run_from_ref is not None:
print("Showing subset of instrument after cut at '"
+ self.run_from_ref
+ "' component.")
if line_length is None:
line_limit = self.line_limit
else:
if not isinstance(line_length, int):
raise ValueError("Show components now shows components in"
" instrument instead of help. For help,"
" use available_components instead. \n"
"The argument for show_components is"
" line_length and has to be an integer.")
line_limit = line_length
component_names = [x.name for x in printed_components]
longest_name = len(max(component_names, key=len))
# todo Investigate how this could have been done in a better way
# Find longest field for each type of data printed
component_type_list = []
at_xyz_list = []
at_relative_list = []
rotated_xyz_list = []
rotated_relative_list = []
for component in printed_components:
component_type_list.append(component.component_name)
at_xyz_list.append(str(component.AT_data[0])
+ str(component.AT_data[1])
+ str(component.AT_data[2]))
at_relative_list.append(component.AT_relative)
rotated_xyz_list.append(str(component.ROTATED_data[0])
+ str(component.ROTATED_data[1])
+ str(component.ROTATED_data[2]))
rotated_relative_list.append(component.ROTATED_relative)
longest_component_name = len(max(component_type_list, key=len))
longest_at_xyz_name = len(max(at_xyz_list, key=len))
longest_at_relative_name = len(max(at_relative_list, key=len))
longest_rotated_xyz_name = len(max(rotated_xyz_list, key=len))
longest_rotated_relative_name = len(max(rotated_relative_list,
key=len))
# Padding settings, 0,0,6,0,6 is minimum values
name_pad = 0
comp_name_pad = 0
AT_pad = 6 # requires (, , ) in addition to data length
RELATIVE_pad = 0
ROTATED_pad = 6 # requires (, , ) in addition to data length
ROTATED_characters = 7 # ROTATED is 7 characters
AT_characters = 2 # AT is 2 characters
SPACING_between_strings = 7 # combining 8 strings, 7 spaces
# Check if longest line length exceeded
longest_line_length = (longest_name + name_pad
+ longest_component_name + comp_name_pad
+ longest_at_xyz_name + AT_pad
+ longest_at_relative_name + RELATIVE_pad
+ longest_rotated_xyz_name + ROTATED_pad
+ longest_rotated_relative_name
+ ROTATED_characters
+ AT_characters
+ SPACING_between_strings)
def coordinates_to_string(data):
return ("("
+ str(data[0]) + ", "
+ str(data[1]) + ", "
+ str(data[2]) + ")")
n_lines = 1
"""
If calculated line length is above the limit loaded from the
configuration file, attempt to split the output over an
additional line. This is hardcoded up to 3 lines.
"""
if longest_line_length > line_limit:
n_lines = 2
longest_at_xyz_name = max([longest_at_xyz_name,
longest_rotated_xyz_name])
longest_rotated_xyz_name = longest_at_xyz_name
RELATIVE_pad = 0
SPACING_between_strings = 4 # combining 5 strings, 4 spaces
longest_line_length_at = (longest_name
+ comp_name_pad
+ longest_component_name
+ comp_name_pad
+ longest_at_xyz_name
+ AT_pad
+ longest_at_relative_name
+ ROTATED_characters
+ SPACING_between_strings)
longest_line_length_rotated = (longest_name
+ comp_name_pad
+ longest_component_name
+ comp_name_pad
+ longest_rotated_xyz_name
+ ROTATED_pad
+ longest_rotated_relative_name
+ ROTATED_characters
+ SPACING_between_strings)
if (longest_line_length_at > line_limit
or longest_line_length_rotated > line_limit):
n_lines = 3
if n_lines == 1:
for component in printed_components:
p_name = str(component.name)
p_name = p_name.ljust(longest_name + name_pad)
p_comp_name = str(component.component_name)
p_comp_name = p_comp_name.ljust(longest_component_name
+ comp_name_pad)
p_AT = coordinates_to_string(component.AT_data)
p_AT = p_AT.ljust(longest_at_xyz_name + AT_pad)
p_AT_RELATIVE = str(component.AT_relative)
p_AT_RELATIVE = p_AT_RELATIVE.ljust(longest_at_relative_name
+ RELATIVE_pad)
p_ROTATED = coordinates_to_string(component.ROTATED_data)
p_ROTATED = p_ROTATED.ljust(longest_rotated_xyz_name
+ ROTATED_pad)
p_ROTATED_RELATIVE = str(component.ROTATED_relative)
if component.ROTATED_specified:
print(p_name, p_comp_name,
"AT", p_AT, p_AT_RELATIVE,
"ROTATED", p_ROTATED, p_ROTATED_RELATIVE)
else:
print(p_name, p_comp_name, "AT", p_AT, p_AT_RELATIVE)
elif n_lines == 2:
for component in printed_components:
p_name = str(component.name)
p_name = p_name.ljust(longest_name + name_pad)
p_comp_name = str(component.component_name)
p_comp_name = p_comp_name.ljust(longest_component_name
+ comp_name_pad)
p_AT = coordinates_to_string(component.AT_data)
p_AT = p_AT.ljust(longest_at_xyz_name + AT_pad)
p_AT_RELATIVE = str(component.AT_relative)
p_AT_RELATIVE = p_AT_RELATIVE.ljust(longest_at_relative_name
+ RELATIVE_pad)
p_ROTATED_align = " "*(longest_name
+ comp_name_pad
+ longest_component_name
+ comp_name_pad)
p_ROTATED = coordinates_to_string(component.ROTATED_data)
p_ROTATED = p_ROTATED.ljust(longest_rotated_xyz_name
+ ROTATED_pad)
p_ROTATED_RELATIVE = str(component.ROTATED_relative)
if component.ROTATED_specified:
print(p_name, p_comp_name,
"AT ", p_AT, p_AT_RELATIVE, "\n",
p_ROTATED_align, "ROTATED",
p_ROTATED, p_ROTATED_RELATIVE)
else:
print(p_name, p_comp_name,
"AT ", p_AT, p_AT_RELATIVE)
elif n_lines == 3:
for component in printed_components:
p_name = bcolors.BOLD + str(component.name) + bcolors.ENDC
p_comp_name = (bcolors.BOLD
+ str(component.component_name)
+ bcolors.ENDC)
p_AT = coordinates_to_string(component.AT_data)
p_AT_RELATIVE = str(component.AT_relative)
p_ROTATED = coordinates_to_string(component.ROTATED_data)
p_ROTATED_RELATIVE = str(component.ROTATED_relative)
if component.ROTATED_specified:
print(p_name + " ", p_comp_name, "\n",
" AT ", p_AT, p_AT_RELATIVE, "\n",
" ROTATED", p_ROTATED, p_ROTATED_RELATIVE)
else:
print(p_name + " ", p_comp_name, "\n",
" AT ", p_AT, p_AT_RELATIVE)
if self.run_to_ref is not None:
print("Showing subset of instrument until cut at '"
+ self.run_to_ref
+ "' component.")
def write_c_files(self):
"""
Obsolete method for writing instrument parts to c files
It is possible to use this function to write c files to a folder
called generated_includes that can then be included in the
different sections of a McStas instrument. Component objects are
NOT written to these files, but rather the contents of the
trace_section that can be set using the append_trace method.
"""
path = os.getcwd()
path = os.path.join(path, "generated_includes")
if not os.path.isdir(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
file_path = os.path.join(".", "generated_includes",
self.name + "_declare.c")
with open(file_path, "w") as fo:
fo.write("// declare section for %s \n" % self.name)
file_path = os.path.join(".", "generated_includes",
self.name + "_declare.c")
with open(file_path, "a") as fo:
for dec_line in self.declare_list:
if isinstance(dec_line, str):
# append declare section parts written here
fo.write(dec_line)
else:
dec_line.write_line(fo)
fo.write("\n")
fo.close()
file_path = os.path.join(".", "generated_includes",
self.name + "_initialize.c")
fo = open(file_path, "w")
fo.write(self.initialize_section)
fo.close()
file_path = os.path.join(".", "generated_includes",
self.name + "_trace.c")
fo = open(file_path, "w")
fo.write(self.trace_section)
fo.close()
file_path = os.path.join(".", "generated_includes",
self.name + "_component_trace.c")
fo = open(file_path, "w")
for component in self.component_list:
component.write_component(fo)
def has_errors(self):
"""
Method that returns true if errors are found in instrument
"""
has_errors = True
try:
self.check_for_errors()
has_errors = False
except:
pass
return has_errors
def check_for_errors(self):
"""
Methods that checks for common McStas errors
Currently checks for:
RELATIVE for AT and ROTATED reference a component that have not yet
been defined.
Using variables in components that have not been defined.
"""
# Check RELATIVE exists
self.check_for_relative_errors()
# Check variables used have been declared
parameters = [x.name for x in self.parameters]
variables = [x.name for x in self.declare_list
if isinstance(x, DeclareVariable)]
pars_and_vars = parameters + variables
# Check component parameters
for component in self.component_list:
component.check_parameters(pars_and_vars)
def check_for_relative_errors(self, start_ref=None, end_ref=None, allow_absolute=True):
"""
Method for checking if RELATIVE locations does not contain unknown references
Using the start_ref and end_ref keyword arguments, a subset of the
instrument can be checked for internal consistency.
"""
if start_ref is None and end_ref is None:
component_list = self.make_component_subset()
elif start_ref == self.run_from_ref and end_ref == self.run_to_ref:
component_list = self.make_component_subset()
else:
start_i, end_i = self.get_component_subset_index_range(start_ref, end_ref)
component_list = self.component_list[start_i:end_i]
seen_instrument_names = []
for component in component_list:
seen_instrument_names.append(component.name)
if component.name == start_ref:
# Avoid checking first component when start_ref != 0
continue
references = []
if component.AT_reference not in (None, "PREVIOUS"):
references.append(component.AT_reference)
if not allow_absolute:
if component.AT_relative == "ABSOLUTE":
raise McStasError("Component '" + component.name
+ "' was set relative to ABSOLUTE"
+ " which is not allowed after an"
+ " instrument split.")
if ( component.ROTATED_specified and
component.ROTATED_reference not in (None, "PREVIOUS")):
references.append(component.ROTATED_reference)
if not allow_absolute:
if component.ROTATED_relative == "ABSOLUTE" and component.ROTATED_specified:
raise McStasError("Component '" + component.name
+ "' was set relative to ABSOLUTE"
+ " which is not allowed after an"
+ " instrument split.")
for ref in references:
if ref not in seen_instrument_names:
raise McStasError("Component '" + str(component.name) +
"' referenced unknown component"
" named '" + str(ref) + "'.\n"
"This check can be skipped with"
" settings(checks=False)")
def read_instrument_file(self):
"""
Reads current instrument file if it exists, otherwise creates one first
"""
instrument_path = os.path.join(self.input_path, self.name + ".instr")
if not os.path.exists(instrument_path):
self.write_full_instrument()
if not os.path.exists(instrument_path):
raise RuntimeError("Failing to write instrument file.")
with open(instrument_path, "r") as fo:
return fo.read()
def show_instrument_file(self, line_numbers=False):
"""
Displays the current instrument file
Parameters
----------
line_numbers : bool
Select whether line numbers should be displayed
"""
instrument_code = self.read_instrument_file()
if not line_numbers:
print(instrument_code)
return
else:
lines = instrument_code.split("\n")
number_of_lines = len(lines)
line_space = len(str(number_of_lines))
for index, line in enumerate(lines):
line_number = str(index + 1).ljust(line_space) + " | "
full_line = line_number + line
print(full_line.replace("\n", ""))
def write_full_instrument(self):
"""
Method for writing full instrument file to disk
This method writes the instrument described by the instrument
objects to disk with the name specified in the initialization of
the object.
"""
# Catch common errors before writing the instrument
if self._run_settings["checks"]:
self.check_for_errors()
# Create file identifier
fo = open(os.path.join(self.input_path, self.name + ".instr"), "w")
# Write quick doc start
fo.write("/" + 80*"*" + "\n")
fo.write("* \n")
fo.write("* McStas, neutron ray-tracing package\n")
fo.write("* Copyright (C) 1997-2008, All rights reserved\n")
fo.write("* Risoe National Laboratory, Roskilde, Denmark\n")
fo.write("* Institut Laue Langevin, Grenoble, France\n")
fo.write("* \n")
fo.write("* This file was written by McStasScript, which is a \n")
fo.write("* python based McStas instrument generator written by \n")
fo.write("* Mads Bertelsen in 2019 while employed at the \n")
fo.write("* European Spallation Source Data Management and \n")
fo.write("* Software Centre\n")
fo.write("* \n")
fo.write("* Instrument %s\n" % self.name)
fo.write("* \n")
fo.write("* %Identification\n") # Could allow the user to insert this
fo.write("* Written by: %s\n" % self.author)
t_format = "%H:%M:%S on %B %d, %Y"
fo.write("* Date: %s\n" % datetime.datetime.now().strftime(t_format))
fo.write("* Origin: %s\n" % self.origin)
fo.write("* %INSTRUMENT_SITE: Generated_instruments\n")
fo.write("* \n")
fo.write("* \n")
fo.write("* %Parameters\n")
# Add description of parameters here
fo.write("* \n")
fo.write("* %End \n")
fo.write("*"*80 + "/\n")
fo.write("\n")
fo.write("DEFINE INSTRUMENT %s (" % self.name)
fo.write("\n")
# Insert parameters
parameter_list = list(self.parameters)
end_chars = [", "]*len(parameter_list)
if len(end_chars) >= 1:
end_chars[-1] = " "
for variable, end_char in zip(parameter_list, end_chars):
write_parameter(fo, variable, end_char)
fo.write(")\n")
if self.dependency_statement != "":
fo.write("DEPENDENCY " + str(self.dependency_statement) + "\n")
fo.write("\n")
# Write declare
fo.write("DECLARE \n%{\n")
for dec_line in self.declare_list:
if isinstance(dec_line, str):
# append declare section parts written here
fo.write(dec_line)
else:
dec_line.write_line(fo)
fo.write("\n")
fo.write("%}\n\n")
# Write uservars
if len(self.user_var_list) > 0:
fo.write("USERVARS \n%{\n")
for user_var in self.user_var_list:
user_var.value = "" # Ensure no value
user_var.write_line(fo)
fo.write("\n")
fo.write("%}\n\n")
# Write initialize
fo.write("INITIALIZE \n%{\n")
fo.write(self.initialize_section)
# Alternatively hide everything in include
"""
fo.write("%include "generated_includes/"
+ self.name + "_initialize.c")
"""
save_parameter_code = ""
for component in self.make_component_subset():
if component.save_parameters or self._run_settings["save_comp_pars"]:
save_parameter_code += component.make_write_string()
if save_parameter_code != "":
fo.write('MPI_MASTER(\n')
fo.write('FILE *file = fopen("component_parameters.txt", "w");\n')
fo.write('if (file) {\n')
fo.write(save_parameter_code)
fo.write('} else {\n')
fo.write(' perror("fopen");\n')
fo.write('}\n')
fo.write(')\n')
fo.write("%}\n\n")
# Write trace
fo.write("TRACE \n")
# Write all components, the first should get the instrument search list
search_object = copy.deepcopy(self.search_statement_list)
for component in self.make_component_subset():
component.write_component(fo, instrument_search=search_object)
search_object = None # Remove for remaining components
# Write finally
fo.write("FINALLY \n%{\n")
fo.write(self.finally_section)
# Alternatively hide everything in include
fo.write("%}\n")
# End instrument file
fo.write("\nEND\n")
fo.close()
def get_component_subset_index_range(self, start_ref=None, end_ref=None):
"""
Provides start and end index for components in run_from to run_to range
Optionally start_ref and end_ref can be given manually which would
overwrite the internal run_from and run_to references.
"""
if start_ref is None:
start_ref = self.run_from_ref
if end_ref is None:
end_ref = self.run_to_ref
# Starting with component named run_from_ref, ending with run_to_ref
component_names = [x.name for x in self.component_list]
start_index = 0
end_index = len(self.component_list)
if start_ref is not None:
start_index = component_names.index(start_ref)
if end_ref is not None:
end_index = component_names.index(end_ref)
return start_index, end_index
def make_component_subset(self):
"""
Uses run_from and run_to specifications to extract subset of components
Adds MCPL component at start and/or end as needed, and adjusts the
surrounding components as necessary.
"""
if self.run_from_ref is None and self.run_to_ref is None:
# Simple case, just return full component list
return self.component_list
start_index, end_index = self.get_component_subset_index_range()
# Create a copy of the used component instances
if start_index == end_index:
component_subset = [copy.deepcopy(self.component_list[start_index])]
else:
component_subset = copy.deepcopy(self.component_list[start_index:end_index])
if self.run_from_ref is not None:
# Add MCPL input component
MCPL_in = self._create_component_instance("MCPL_" + self.run_from_ref, "MCPL_input")
MCPL_in.set_comment("Automatically inserted to split instrument into parts")
if self.run_from_component_parameters is not None:
MCPL_in.set_parameters(**self.run_from_component_parameters)
# Ensure first component reset to MCPL position
first_component = component_subset[0]
# Since a copy of the component is used, we can alter some properties safely
first_component.set_AT([0, 0, 0], "ABSOLUTE")
if first_component.ROTATED_specified:
first_component.set_ROTATED([0, 0, 0], "ABSOLUTE")
component_subset = [MCPL_in] + component_subset
if self.run_to_ref is not None:
# MCPL component will replace the component after the last included
replaced_component = self.component_list[end_index]
# Add MCPL output component
MCPL_out = self._create_component_instance("MCPL_" + self.run_to_ref, "MCPL_output")
MCPL_out.set_comment("Automatically inserted to split instrument into parts")
if self.run_to_component_parameters is not None:
MCPL_out.set_parameters(**self.run_to_component_parameters)
MCPL_out.set_AT(replaced_component.AT_data, RELATIVE=replaced_component.AT_reference)
if replaced_component.ROTATED_specified:
MCPL_out.set_ROTATED(replaced_component.ROTATED_data, RELATIVE=replaced_component.ROTATED_reference)
component_subset += [MCPL_out]
return component_subset
def set_dependency(self, string):
"""
Sets the DEPENDENCY line of instruments, expanding its system search
The dependency line can be used to tell McStas to search for files in
additional locations. Double quotes are added.
Parameters
----------
string : str
The dependency string
"""
# Disable by giving an empty string
if len(string) == 0:
self.dependency_statement = ""
return
if string[0] != '"' and string[-1] != '"':
string = '"' + string + '"'
self.dependency_statement = string
def add_search(self, statement, SHELL=False, help_name=""):
"""
Adds a search statement to the instrument
The dependency line can be used to tell McStas to search for files in
additional locations. Double quotes are added.
Parameters
----------
statement : str
The search statement
SHELL : bool (default False)
if True, shell keyword is added
help_name : str
Name used in help messages regarding the component search
"""
self.search_statement_list.add_statement(SearchStatement(statement, SHELL=SHELL))
self.component_reader.load_components_from_folder(statement, name=help_name)
def clear_search(self):
"""
Clears the instrument of all search statements
"""
self.search_statement_list.clear()
# Reset component_reader
self.component_class_lib = {}
package_path = self._run_settings["package_path"]
run_path = self._run_settings["run_path"]
self.component_reader = ComponentReader(package_path,
input_path=run_path)
def show_search(self):
"""
Shows all search statements on instrument level
"""
print(self.search_statement_list)
def settings(self, ncount=None, mpi="not_set", seed=None,
force_compile=None, output_path=None,
increment_folder_name=None, custom_flags=None,
executable=None, executable_path=None,
suppress_output=None, gravity=None, checks=None,
openacc=None, NeXus=None, save_comp_pars=False):
"""
Sets settings for McStas run performed with backengine
Some options are mandatory, for example output_path, which can not
already exist, if it does data will be read from this folder. If the
mcrun command is not in the PATH of the system, the absolute path can
be given with the executable_path keyword argument. This path could
also already have been set at initialization of the instrument object.
Parameters
----------
Keyword arguments
output_path : str
Sets data_folder_name
increment_folder_name : bool
Will update output_path if folder already exists, default True
ncount : int
Sets ncount
mpi : int
Sets thread count
force_compile : bool
If True (default) new instrument file is written, otherwise not
custom_flags : str
Sets custom_flags passed to mcrun
executable : str
Name of the executable
executable_path : str
Path to mcrun command, "" if already in path
suppress_output : bool
Set to True to suppress output
gravity : bool
If True, gravity will be simulated
openacc : bool
If True, adds --openacc to mcrun call
NeXus : bool
If True, adds --format=NeXus to mcrun call
save_comp_pars : bool
If True, McStas run writes all comp pars to disk
"""
settings = {}
if executable_path is not None:
if not os.path.isdir(str(executable_path)):
raise RuntimeError("The executable_path provided in "
+ "settings does not point to a"
+ "directory: \""
+ str(executable_path) + "\"")
settings["executable_path"] = executable_path
if executable is not None:
# check provided executable can be converted to string
str(executable)
settings["executable"] = executable
if force_compile is not None:
if not isinstance(force_compile, bool):
raise TypeError("force_compile must be a bool.")
settings["force_compile"] = force_compile
if increment_folder_name is not None:
if not isinstance(increment_folder_name, bool):
raise TypeError("increment_folder_name must be a bool.")
settings["increment_folder_name"] = increment_folder_name
if ncount is not None:
if not isinstance(ncount, (float, int)):
raise TypeError("ncount must be a number.")
settings["ncount"] = ncount
if mpi != "not_set": # None is a legal value for mpi
if not isinstance(mpi, (type(None), int)):
raise TypeError("mpi must be an integer or None.")
settings["mpi"] = mpi
if gravity is not None:
settings["gravity"] = bool(gravity)
if custom_flags is not None:
str(custom_flags) # Check a string is given
settings["custom_flags"] = custom_flags
if seed is not None:
settings["seed"] = seed
if suppress_output is not None:
settings["suppress_output"] = suppress_output
if checks is not None:
settings["checks"] = checks
if output_path is not None:
self.output_path = output_path
if openacc is not None:
settings["openacc"] = bool(openacc)
if NeXus is not None:
settings["NeXus"] = bool(NeXus)
if save_comp_pars is not None:
settings["save_comp_pars"] = bool(save_comp_pars)
self._run_settings.update(settings)
def settings_string(self):
"""
Returns a string describing settings stored in this instrument object
"""
variable_space = 20
description = "Instrument settings:\n"
if "ncount" in self._run_settings:
value = self._run_settings["ncount"]
description += " ncount:".ljust(variable_space)
description += "{:.2e}".format(value) + "\n"
if "mpi" in self._run_settings:
value = self._run_settings["mpi"]
if value is not None:
description += " mpi:".ljust(variable_space)
description += str(int(value)) + "\n"
if "gravity" in self._run_settings:
value = self._run_settings["gravity"]
description += " gravity:".ljust(variable_space)
description += str(value) + "\n"
if "seed" in self._run_settings:
value = self._run_settings["seed"]
description += " seed:".ljust(variable_space)
description += str(int(value)) + "\n"
description += " output_path:".ljust(variable_space)
description += str(self.output_path) + "\n"
if "increment_folder_name" in self._run_settings:
value = self._run_settings["increment_folder_name"]
description += " increment_folder_name:".ljust(variable_space)
description += str(value) + "\n"
if "run_path" in self._run_settings:
value = self._run_settings["run_path"]
description += " run_path:".ljust(variable_space)
description += str(value) + "\n"
if "package_path" in self._run_settings:
value = self._run_settings["package_path"]
description += " package_path:".ljust(variable_space)
description += str(value) + "\n"
if "executable_path" in self._run_settings:
value = self._run_settings["executable_path"]
description += " executable_path:".ljust(variable_space)
description += str(value) + "\n"
if "executable" in self._run_settings:
value = self._run_settings["executable"]
description += " executable:".ljust(variable_space)
description += str(value) + "\n"
if "force_compile" in self._run_settings:
value = self._run_settings["force_compile"]
description += " force_compile:".ljust(variable_space)
description += str(value) + "\n"
if "NeXus" in self._run_settings:
value = self._run_settings["NeXus"]
description += " NeXus:".ljust(variable_space)
description += str(value) + "\n"
if "openacc" in self._run_settings:
value = self._run_settings["openacc"]
description += " openacc:".ljust(variable_space)
description += str(value) + "\n"
if "save_comp_pars" in self._run_settings:
value = self._run_settings["save_comp_pars"]
description += " save_comp_pars:".ljust(variable_space)
description += str(value) + "\n"
return description.strip()
def show_settings(self):
"""
Prints settings stored in this instrument object
"""
print(self.settings_string())
def backengine(self):
"""
Runs instrument with McStas / McXtrace, saves data in data attribute
This method will write the instrument to disk and then run it using
the mcrun command of the system. Settings are set using settings
method.
"""
self.__add_input_to_mcpl()
instrument_path = os.path.join(self.input_path, self.name + ".instr")
if not os.path.exists(instrument_path) or self._run_settings["force_compile"]:
self.write_full_instrument()
parameters = {}
for parameter in self.parameters:
if parameter.value is None:
raise RuntimeError("Parameter value not set for parameter: '" + parameter.name
+ "' set with set_parameters.")
parameters[parameter.name] = parameter.value
options = self._run_settings
options["parameters"] = parameters
options["output_path"] = self.output_path
# Set up the simulation
simulation = ManagedMcrun(self.name + ".instr", **options)
# Run the simulation and return data
simulation.run_simulation()
# Load data and store in __data
#data = simulation.load_results()
#self._set_data(data)
## look for MCPL_output components and the defined filenames
self.__add_mcpl_to_output(simulation)
# simulation results from .dat files loaded as dict
data = simulation.load_results()
data_dict = {"data": data}
# adding to the libpyvinyl output datacollection with key = sim_data_key
sim_data_key = self.output_keys[0]
output_data = self.output[sim_data_key]
output_data.set_dict(data_dict)
if self.run_to_ref is not None:
filename = self.parameters.parameters["run_to_mcpl"].value
# Check for mcpl files and load those to database
db = self.dump_database
out = db.load_data(expected_filename=filename,
data_folder_path=simulation.data_folder_name,
parameters=self.parameters.parameters,
dump_point=self.run_to_ref,
run_name=self.run_to_name,
comment=self.run_to_comment)
if out is None:
print("Expected MCPL file was not loaded!")
if "data" not in self.output[sim_data_key].get_data():
print("\n\nNo data returned.")
return None
else:
return self.output[sim_data_key].get_data()["data"]
def __add_input_to_mcpl(self):
try:
mcpl_file = self.input["mcpl"].filename
for comp in self.component_list:
if comp.component_name == "MCPL_input":
comp.filename = '"' + mcpl_file + '"'
break
except:
return
def __add_mcpl_to_output(self, managed_mcrun):
MCPL_extension = MCPLDataFormat.format_register()["ext"]
num_mcpl_files = 0
for comp in self.component_list[::-1]: # starting from the last one!
if comp.component_name == "MCPL_output":
num_mcpl_files = num_mcpl_files+1
absfilepath = os.path.join(managed_mcrun.data_folder_name,
comp.filename.strip('"').strip("'")
+MCPL_extension)
if os.path.exists(absfilepath+".gz"):
absfilepath+=".gz"
if os.path.exists(absfilepath) is False:
raise RuntimeError(f"MCPL file: {absfilepath} nor {absfilepath}.gz not found")
mcpl_file = pyvinylMCPLData.from_file(absfilepath)
if num_mcpl_files>1:
mcpl_file.key = mcpl_file+str(num_mcpl_files)
self.output.add_data(mcpl_file)
self.output_keys.append(mcpl_file.key)
def run_full_instrument(self, **kwargs):
"""
Runs McStas instrument described by this class, returns list of
McStasData
This method will write the instrument to disk and then run it
using the mcrun command of the system. Options are set using
keyword arguments. Some options are mandatory, for example
output_path, which can not already exist, if it does data will
be read from this folder. If the mcrun command is not in the
path of the system, the absolute path can be given with the
executable_path keyword argument. This path could also already
have been set at initialization of the instrument object.
Parameters
----------
Keyword arguments
output_path : str
Sets data_folder_name
ncount : int
Sets ncount
mpi : int
Sets thread count
parameters : dict
Sets parameters
custom_flags : str
Sets custom_flags passed to mcrun
force_compile : bool
If True (default) new instrument file is written, otherwise not
executable_path : str
Path to mcrun command, "" if already in path
"""
warnings.warn(
"run_full_instrument will be removed in future version of McStasScript. \n"
+ "Instead supply parameters with set_parameters, set settings with "
+ "settings and use backengine() to run. See examples in package. "
+ "Documentation now at https://mads-bertelsen.github.io")
if "foldername" in kwargs:
kwargs["output_path"] = kwargs["foldername"]
del kwargs["foldername"]
if "parameters" in kwargs:
self.set_parameters(kwargs["parameters"])
del kwargs["parameters"]
self.settings(**kwargs)
return self.backengine()
def show_instrument(self, format="webgl", width=800, height=450, new_tab=False):
"""
Uses mcdisplay to show the instrument in web browser
If this method is performed from a jupyter notebook and use the webgl
format the interface will be shown in the notebook using an IFrame.
Keyword arguments
-----------------
format : str
'webgl' or 'window' format for display
width : int
width of IFrame if used in notebook
height : int
height of IFrame if used in notebook
new_tab : bool
Open webgl in new browser tab
"""
parameters = {}
for parameter in self.parameters:
if parameter.value is None:
raise RuntimeError("Unspecified parameter: '" + parameter.name
+ "' set with set_parameters.")
parameters[parameter.name] = parameter.value
# add parameters to command
parameter_string = ""
for key, val in parameters.items():
parameter_string = (parameter_string + " "
+ str(key) # parameter name
+ "="
+ str(val)) # parameter value
if self.package_name == "McXtrace":
executable = "mxdisplay"
else:
executable = "mcdisplay"
if format == "webgl":
executable = executable+"-webgl"
elif format == "window":
executable = executable+"-pyqtgraph"
# Platform dependent, check both package_path and bin
executable_path = self._run_settings["executable_path"]
bin_path = os.path.join(executable_path, executable)
if not os.path.isfile(bin_path):
# Take bin in package path into account
package_path = self._run_settings["package_path"]
bin_path = os.path.join(package_path, "bin", executable)
dir_name_original = self.name + "_mcdisplay"
dir_name = dir_name_original
index = 0
while os.path.exists(os.path.join(self.input_path, dir_name)):
dir_name = dir_name_original + "_" + str(index)
index += 1
dir_control = "--dirname " + dir_name + " "
self.write_full_instrument()
instr_path = os.path.join(self.input_path, self.name + ".instr")
instr_path = os.path.abspath(instr_path)
try:
shell = get_ipython().__class__.__name__
is_notebook = shell == "ZMQInteractiveShell"
except:
is_notebook = False
options = ""
if is_notebook and executable == "mcdisplay-webgl" and not new_tab:
options += "--nobrowse "
full_command = ('"' + bin_path + '" '
+ dir_control
+ options
+ instr_path
+ " " + parameter_string)
process = subprocess.run(full_command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
cwd=self.input_path)
if not is_notebook or executable != "mcdisplay-webgl":
print(process.stderr)
print(process.stdout)
return
html_path = os.path.join(self.input_path, dir_name, "index.html")
if not os.path.exists(html_path):
print(process.stderr)
print(process.stdout)
print("")
print("mcdisplay run failed.")
return
# Create IFrame in ipython that shows instrument
return IFrame(src=html_path, width=width, height=height)
def show_diagram(self, analysis=False, variable=None, limits=None):
"""
Shows diagram of component connections in insrument
Shows a diagram with all components as text fields and arrows between
them showing the AT RELATIVE and ROTATED RELATIVE connections. Spatial
connections are shown in blue, and rotational in red. ROTATED
connections are only shown when they are specified. To see the intensity
and number of rays over the course of the instrument, use analysis=True.
parameters:
analysis : bool
If True, a plot of intensity and ncount over the instrument is included
"""
if self.has_errors() and self._run_settings["checks"]:
print("The instrument has some error, this diagram is still "
"shown as it may help find the bug.")
if variable is not None:
analysis = True
instrument_diagram(self, analysis=analysis, variable=variable, limits=limits)
if self._run_settings["checks"]:
self.check_for_errors()
def show_analysis(self, variable=None):
beam_diag = IntensityDiagnostics(self)
beam_diag.run_general(variable=variable)
beam_diag.plot()
def saveH5(self, filename: str, openpmd: bool = True):
"""
Not relevant, but required from BaseCalculator, will be removed
"""
pass
class McStas_instr(McCode_instr):
"""
Main class for writing a McStas instrument using McStasScript
Initialization of McStas_instr sets the name of the instrument file
and its methods are used to add all aspects of the instrument file.
The class also holds methods for writing the finished instrument
file to disk and to run the simulation.
Attributes
----------
name : str
name of instrument file
author : str, default "Python Instrument Generator"
name of user of McStasScript, written to the file
origin : str, default "ESS DMSC"
origin of instrument file (affiliation)
input_path : str, default "."
directory in which simulation is executed, uses components found there
output_path : str
directory in which the data is written
executable_path : str
absolute path of mcrun command, or empty if it is in path
parameters : ParameterContainer
contains all input parameters to be written to file
declare_list : list of DeclareVariable instances
contains all declare parrameters to be written to file
initialize_section : str
string containing entire initialize section to be written
trace_section : str
string containing trace section (OBSOLETE)
finally_section : str
string containing entire finally section to be written
component_list : list of component instances
list of components in the instrument
component_class_lib : dict
dict of custom Component classes made at run time
component_reader : ComponentReader
ComponentReader instance for reading component files
package_path : str
Path to mccode package containing component folders
run_settings : dict
Dict of options set with settings
data : list
List of McStasData objects produced by last run
Methods
-------
add_parameter(*args, **kwargs)
Adds input parameter to the define section
add_declare_var(type, name)
Add declared variable called name of given type to the declare section
append_declare(string)
Appends to the declare section
append_initialize(string)
Appends a string to the initialize section, then adds new line
append_initialize_no_new_line(string)
Appends a string to the initialize section
append_finally(string)
Appends a string to finally section, then adds new line
append_finally_no_new_line(string)
Appends a string to finally section
append_trace(string)
Obsolete method, add components instead (used in write_c_files)
append_trace_no_new_line(string)
Obsolete method, add components instead (used in write_c_files)
available_components(string)
Shows available components in given category
component_help(name)
Shows help on component of given name
add_component(instance_name, component_name, **kwargs)
Add a component to the instrument file
copy_component(new_component_name, original_component, **kwargs)
Makes a copy of original_component with new_component_name
get_component(instance_name)
Returns component instance with name instance_name
get_last_component()
Returns component instance of last component
print_component(instance_name)
Prints an overview of current state of named component
print_component_short(instance_name)
Prints short overview of current state of named component
show_components()
Prints overview of postion / rotation of all components
write_c_files()
Writes c files for %include in generated_includes folder
write_full_instrument()
Writes full instrument file to current directory
show_instrument()
Shows instrument using mcdisplay
set_parameters(dict)
Inherited from libpyvinyl BaseCalculator
settings(**kwargs)
Sets settings for performing simulation
backengine()
Performs simulation, saves in data attribute
run_full_instrument(**kwargs)
Deprecated method for performing the simulation
interface()
Shows interface with jupyter notebook widgets
get_interface_data()
Returns data set from latest simulation in widget
"""
def __init__(self, name, **kwargs):
"""
Initialization of McStas Instrument
Parameters
----------
name : str
Name of project, instrument file will be name + ".instr"
keyword arguments:
parameters : ParameterContainer or CalculatorParameters
Parameters for this instrument
dumpfile: str
File path to dump file to be loaded
author : str
Name of author, written in instrument file
origin : str
Affiliation of author, written in instrument file
executable_path : str
Absolute path of mcrun or empty if already in path
input_path : str
Work directory, will load components from this folder
"""
self.particle = "neutron"
self.package_name = "McStas"
executable = "mcrun"
super().__init__(name, executable=executable, **kwargs)
try:
self.mccode_version = check_mcstas_major_version(self._run_settings["executable_path"])
except:
self.mccode_version = "Unknown"
def _read_calibration(self):
this_dir = os.path.dirname(os.path.abspath(__file__))
configuration_file_name = os.path.join(this_dir, "..",
"configuration.yaml")
if not os.path.isfile(configuration_file_name):
raise NameError("Could not find configuration file!")
with open(configuration_file_name, 'r') as ymlfile:
config = yaml.safe_load(ymlfile)
if type(config) is dict:
self.line_limit = config["other"]["characters_per_line"]
if "MCSTAS" in os.environ: # We are in a McStas environment, use that
self._run_settings["executable_path"] = os.path.dirname(shutil.which("mcrun"))
self._run_settings["package_path"] = os.environ["MCSTAS"]
elif type(config) is dict:
self._run_settings["executable_path"] = config["paths"]["mcrun_path"]
self._run_settings["package_path"] = config["paths"]["mcstas_path"]
else:
# This happens in unit tests that mocks open
self._run_settings["executable_path"] = ""
self._run_settings["package_path"] = ""
self.line_limit = 180
@classmethod
def from_dump(cls, dumpfile: str):
"""Load a dill dump from a dumpfile.
Overwrites a libpyvinyl method to load McStas components
:param dumpfile: The file name of the dumpfile.
:type dumpfile: str
:return: The calculator object restored from the dumpfile.
:rtype: CalcualtorClass
"""
with open(dumpfile, "rb") as fhandle:
try:
# Loads necessary component classes from unpackers installation
tmp = CustomMcStasUnpickler(fhandle).load()
except:
raise IOError("Cannot load calculator from {}.".format(dumpfile))
if not isinstance(tmp, cls):
raise TypeError(f"The object in the file {dumpfile} is not a {cls}")
return tmp
class McXtrace_instr(McCode_instr):
"""
Main class for writing a McXtrace instrument using McStasScript
Initialization of McXtrace_instr sets the name of the instrument file
and its methods are used to add all aspects of the instrument file.
The class also holds methods for writing the finished instrument
file to disk and to run the simulation.
Attributes
----------
name : str
name of instrument file
author : str, default "Python Instrument Generator"
name of user of McStasScript, written to the file
origin : str, default "ESS DMSC"
origin of instrument file (affiliation)
input_path : str, default "."
directory in which simulation is executed, uses components found there
output_path : str
directory in which the data is written
executable_path : str
absolute path of mcrun command, or empty if it is in path
parameters : ParameterContainer
contains all input parameters to be written to file
declare_list : list of DeclareVariable instances
contains all declare parrameters to be written to file
initialize_section : str
string containing entire initialize section to be written
trace_section : str
string containing trace section (OBSOLETE)
finally_section : str
string containing entire finally section to be written
component_list : list of component instances
list of components in the instrument
component_class_lib : dict
dict of custom Component classes made at run time
component_reader : ComponentReader
ComponentReader instance for reading component files
package_path : str
Path to mccode package containing component folders
run_settings : dict
Dict of options set with settings
data : list
List of McStasData objects produced by last run
Methods
-------
add_parameter(*args, **kwargs)
Adds input parameter to the define section
add_declare_var(type, name)
Add declared variable called name of given type to the declare section
append_declare(string)
Appends to the declare section
append_initialize(string)
Appends a string to the initialize section, then adds new line
append_initialize_no_new_line(string)
Appends a string to the initialize section
append_finally(string)
Appends a string to finally section, then adds new line
append_finally_no_new_line(string)
Appends a string to finally section
append_trace(string)
Obsolete method, add components instead (used in write_c_files)
append_trace_no_new_line(string)
Obsolete method, add components instead (used in write_c_files)
available_components(string)
Shows available components in given category
component_help(name)
Shows help on component of given name
add_component(instance_name, component_name, **kwargs)
Add a component to the instrument file
copy_component(new_component_name, original_component, **kwargs)
Makes a copy of original_component with new_component_name
get_component(instance_name)
Returns component instance with name instance_name
get_last_component()
Returns component instance of last component
print_component(instance_name)
Prints an overview of current state of named component
print_component_short(instance_name)
Prints short overview of current state of named component
show_components()
Prints overview of postion / rotation of all components
write_c_files()
Writes c files for %include in generated_includes folder
write_full_instrument()
Writes full instrument file to current directory
show_instrument()
Shows instrument using mcdisplay
set_parameters(dict)
Inherited from libpyvinyl BaseCalculator
settings(**kwargs)
Sets settings for performing simulation
backengine()
Performs simulation, saves in data attribute
run_full_instrument(**kwargs)
Deprecated method for performing the simulation
interface()
Shows interface with jupyter notebook widgets
get_interface_data()
Returns data set from latest simulation in widget
"""
def __init__(self, name, **kwargs):
"""
Initialization of McXtrace Instrument
Parameters
----------
name : str
Name of project, instrument file will be name + ".instr"
keyword arguments:
parameters : ParameterContainer or CalculatorParameters
Parameters for this instrument
dumpfile: str
File path to dump file to be loaded
author : str
Name of author, written in instrument file
origin : str
Affiliation of author, written in instrument file
executable_path : str
Absolute path of mcrun or empty if already in path
input_path : str
Work directory, will load components from this folder
"""
self.particle = "x-ray"
self.package_name = "McXtrace"
executable = "mxrun"
super().__init__(name, executable=executable, **kwargs)
try:
self.mccode_version = check_mcxtrace_major_version(self._run_settings["executable_path"])
except:
self.mccode_version = "Unknown"
def _read_calibration(self):
this_dir = os.path.dirname(os.path.abspath(__file__))
configuration_file_name = os.path.join(this_dir, "..",
"configuration.yaml")
if not os.path.isfile(configuration_file_name):
raise NameError("Could not find configuration file!")
with open(configuration_file_name, 'r') as ymlfile:
config = yaml.safe_load(ymlfile)
if type(config) is dict:
self.line_limit = config["other"]["characters_per_line"]
if "MCXTRACE" in os.environ: # We are in a McXtrace environment, use that
self._run_settings["executable_path"] = os.path.dirname(shutil.which("mxrun"))
self._run_settings["package_path"] = os.environ["MCXTRACE"]
elif type(config) is dict:
self._run_settings["executable_path"] = config["paths"]["mxrun_path"]
self._run_settings["package_path"] = config["paths"]["mcxtrace_path"]
else:
# This happens in unit tests that mocks open
self._run_settings["executable_path"] = ""
self._run_settings["package_path"] = ""
self.line_limit = 180
@classmethod
def from_dump(cls, dumpfile: str):
"""Load a dill dump from a dumpfile.
Overwrites a libpyvinyl method to load McStas components
:param dumpfile: The file name of the dumpfile.
:type dumpfile: str
:return: The calculator object restored from the dumpfile.
:rtype: CalcualtorClass
"""
with open(dumpfile, "rb") as fhandle:
try:
# Loads necessary component classes from unpackers installation
tmp = CustomMcXtraceUnpickler(fhandle).load()
except:
raise IOError("Cannot load calculator from {}.".format(dumpfile))
if not isinstance(tmp, cls):
raise TypeError(f"The object in the file {dumpfile} is not a {cls}")
return tmp
|