API reference¶
Everything on this page is importable directly from the top-level pyradmc namespace and
is supported and versioned. Anything not documented here is an implementation detail
that may move between releases, even if it is importable.
Re-exports are lazy, so importing pyradmc costs nothing and does not pull in NumPy,
SciPy, Warp or SimpleITK. A core-only install can name WarpEngine without having warp
installed; touching it then raises an error naming the extra to install.
Engines¶
pyradmc.backends.ref.engine.ReferenceEngine
dataclass
¶
Single-threaded reference photon engine over a voxel grid.
Source code in pyradmc/backends/ref/engine.py
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 | |
run ¶
run(source: Source, n_histories: int, n_batches: int, seed: int, pcut: float = PCUT_MEV, ecut: float = ECUT_MEV, transport_electrons: bool = True, primary_kind: str = 'photon', scoring_grid: ScoringGeometry | None = None, scoring_mode: str = 'dose_to_medium', step_energy_fraction: float | None = None, deposit_resolution_cm: float | None = None, msc_model: str = 'gs', progress: ProgressCallback | None = None, concurrent_batches: int = 1) -> TransportResult
Transport n_histories primaries in n_batches equal batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Source
|
Primary source; its geometry is particle-agnostic (see |
required |
n_histories
|
int
|
Total primaries; must be divisible by |
required |
n_batches
|
int
|
Batches for the sigma estimate (AGENTS.md section 2.4). |
required |
seed
|
int
|
Global seed; history |
required |
pcut
|
float
|
Photon and electron cutoffs in MeV. Accuracy-defining (AGENTS.md section 2.8); the defaults are the project-wide values and changing one in a call is a visible, greppable decision. |
PCUT_MEV
|
ecut
|
float
|
Photon and electron cutoffs in MeV. Accuracy-defining (AGENTS.md section 2.8); the defaults are the project-wide values and changing one in a call is a visible, greppable decision. |
PCUT_MEV
|
transport_electrons
|
bool
|
False selects the KERMA approximation (charged secondaries deposit at their creation voxel) — the explicit option docs/decisions.md keeps for photon-only physics tests. |
True
|
primary_kind
|
str
|
|
'photon'
|
scoring_grid
|
ScoringGeometry | None
|
Scoring geometry to accumulate dose on (decoupled scoring). |
None
|
scoring_mode
|
str
|
|
'dose_to_medium'
|
deposit_resolution_cm
|
float | None
|
Longest piece a half-substep's continuous energy loss is filed as, in
cm. Set it when scoring below the transport voxel scale. There, the
midpoint deposit prints the voxel lattice onto the dose: substeps are
capped at voxel faces, so their midpoints pile at voxel centres and
the sub-voxel profile becomes a tent. A natural value is the finest
bin the scorer resolves — see
:attr: It moves no energy and changes no trajectory or random stream — only where a deposit is filed — so the energy books and the transported histories are identical either way. |
None
|
step_energy_fraction
|
float | None
|
Maximum fraction of CSDA range per electron substep; |
None
|
progress
|
ProgressCallback | None
|
Optional callback invoked with a :class: |
None
|
concurrent_batches
|
int
|
Scheduling hint shared with the Warp API. The single-history reference oracle is deliberately sequential, so any positive value is accepted and has no effect. |
1
|
Source code in pyradmc/backends/ref/engine.py
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 | |
run_dij ¶
run_dij(source: BeamletSource, n_histories_per_beamlet: int, n_batches: int, seed: int, pcut: float = PCUT_MEV, ecut: float = ECUT_MEV, transport_electrons: bool = True, truncation: float = DIJ_TRUNCATION_RELATIVE, correlated: bool = True, scoring_grid: ScoringGrid | None = None, scoring_mode: str = 'dose_to_medium', step_energy_fraction: float | None = None, deposit_resolution_cm: float | None = None, msc_model: str = 'gs', progress: ProgressCallback | None = None) -> DijResult
Compute the beamlet-resolved dose influence matrix over the lattice.
History-to-beamlet mapping — the project-wide convention every backend
follows: history h feeds beamlet j = h // n_histories_per_beamlet,
and within a beamlet, batch b owns the contiguous slice of
n_histories_per_beamlet / n_batches histories starting at
j * n_histories_per_beamlet + b * (that slice length). Streams are pure
functions of (seed, h), so the Dij is bit-reproducible on one target
regardless of how a backend schedules the transport, and a 1x1 lattice
reproduces the open-field :meth:run bit for bit (test-pinned).
Correlated sampling changes the stream key only: with
correlated=True, the history at within-beamlet index
rw = h - j * n_histories_per_beamlet draws the stream (seed, rw)
instead of (seed, h), so corresponding histories of every beamlet
replay the same random sequence — same within-bixel entry offset, same
interaction sequence — and only the beamlet's position differs. Beamlet
assignment, batching, scoring and the energy books are untouched, and on
a 1x1 lattice rw == h, so the open-field anchor above holds in both
modes (test-pinned).
Every deposit of a history's whole secondary family scores into its beamlet's column: the columns partition the open-field dose exactly. Column doses are per emitted history of that beamlet, MeV/g.
Parameters mirror :meth:run; the two Dij-specific ones:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_histories_per_beamlet
|
int
|
Histories per beamlet (equal by design — stratified, not sampled);
must be divisible by |
required |
truncation
|
float
|
Per-column relative truncation threshold. Accuracy-defining
(AGENTS.md 2.8): the default is :data: |
DIJ_TRUNCATION_RELATIVE
|
correlated
|
bool
|
Key streams on the within-beamlet index so columns share random
sequences (correlated sampling). This is the shipped
configuration (default True): the noise/bias study
( |
True
|
scoring_grid
|
ScoringGrid | None
|
Dose grid the Dij columns live on; semantics as in :meth: |
None
|
scoring_mode
|
str
|
Tally weighting of the columns, as in :meth: |
'dose_to_medium'
|
deposit_resolution_cm
|
float | None
|
Sub-substep deposit resolution, as in :meth: |
None
|
progress
|
ProgressCallback | None
|
Optional callback, as in :meth: |
None
|
Source code in pyradmc/backends/ref/engine.py
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 | |
pyradmc.backends.warp.engine.WarpEngine
dataclass
¶
Dual-target production engine over a voxel grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
VoxelGrid
|
As in the reference engine. |
required |
cross_sections
|
VoxelGrid
|
As in the reference engine. |
required |
device
|
str
|
Warp device string: |
'cpu'
|
chunk_size
|
int | None
|
Histories transported concurrently. Statistically and bit-wise inert
(test-pinned); it only trades memory against launch count. Default
|
None
|
queue_factor
|
int
|
Queue capacity per chunk history. Overflow raises rather than dropping secondaries. |
16
|
Source code in pyradmc/backends/warp/engine.py
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 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 | |
run ¶
run(source: Source, n_histories: int, n_batches: int, seed: int, pcut: float = PCUT_MEV, ecut: float = ECUT_MEV, transport_electrons: bool = True, primary_kind: str = 'photon', scoring_grid: ScoringGeometry | None = None, scoring_mode: str = 'dose_to_medium', step_energy_fraction: float | None = None, deposit_resolution_cm: float | None = None, msc_model: str = 'gs', progress: ProgressCallback | None = None, concurrent_batches: int = 1) -> TransportResult
Transport n_histories primaries; same contract as the reference engine.
See :meth:pyradmc.backends.ref.engine.ReferenceEngine.run for parameter
semantics — the two signatures are deliberately identical (scoring_grid
included: the scoring geometry deposits accumulate on — a rectilinear
:class:~pyradmc.scoring.grid.ScoringGrid, default the transport grid, or a
:class:~pyradmc.scoring.cylinder.CylindricalScoringGrid for a pencil-beam
kernel — with off-geometry deposits booked to energy_unscored; and scoring_mode:
dose-to-water weights each deposit in-kernel by the stopping-power ratio
from the flattened tables while the books stay physical; progress: one
tick per completed batch, the identical cadence to the reference engine —
see :mod:pyradmc.progress). A source with an in-kernel generator is
generated on-device: the built-in mono beams from analytic parameters (all
of one primary_kind) and the exact
:class:~pyradmc.geometry.source.SpectralBeamSource type from its uploaded
spectrum tables (photons; primary_kind ignored). Any other source (a
phase space, a spectral subclass, or a user
:class:~pyradmc.geometry.source.Source) is transported by host-sampling
each chunk via sample_batch and seeding the photon and electron queues
by the per-record kind; primary_kind is then ignored and
energy_emitted is booked from the sampled records.
concurrent_batches overlaps whole statistical batches on independent
CUDA streams, with private queues, RNG slots, and fixed-point dose maps.
The float64 batch-dose fold remains serialized in batch order, so changing
the lane count is bitwise inert on one device. CPU accepts the same option
but runs sequentially. Each lane requires another queue set and dose map;
memory use therefore grows approximately linearly with the lane count.
msc_model selects the multiple-scattering law exactly as in the
reference loop (see
:func:pyradmc.transport.electron.electron_steps): "gs" — the
shipped default — samples Goudsmit-Saunderson deflections from an
eagerly precomputed table grid (built host-side on the first GS run
for this (ecut, e_max), persisted to the user cache, uploaded per
run) and does not apply the Gaussian-validity angular cap; the
"gaussian" hinge survives as the paired-comparison test
instrument. step_energy_fraction=None resolves to the selected
model's validated fraction.
Source code in pyradmc/backends/warp/engine.py
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 | |
run_dij ¶
run_dij(source: BeamletSource, n_histories_per_beamlet: int, n_batches: int, seed: int, pcut: float = PCUT_MEV, ecut: float = ECUT_MEV, transport_electrons: bool = True, truncation: float = DIJ_TRUNCATION_RELATIVE, correlated: bool = True, beamlet_group_size: int | None = None, scoring_grid: ScoringGrid | None = None, scoring_mode: str = 'dose_to_medium', step_energy_fraction: float | None = None, deposit_resolution_cm: float | None = None, msc_model: str = 'gs', devices: Sequence[str] | None = None, concurrent_batches: int = 1, progress: ProgressCallback | None = None) -> DijResult
Compute the Dij over the lattice; same contract as the reference engine.
See :meth:pyradmc.backends.ref.engine.ReferenceEngine.run_dij for the
history-to-beamlet mapping and parameter semantics, correlated
(the shipped sampling configuration, default True; False is the
test instrument) included — the signatures are deliberately identical
up to the one scheduling knob:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beamlet_group_size
|
int | None
|
Beamlets scored concurrently into one dense device buffer. Purely a
memory/occupancy trade-off: streams are pure functions of
Default This is the only dial for the dense device cost, which is
|
None
|
devices
|
Sequence[str] | None
|
Devices to shard the beamlet groups over, one host thread each; default
( Scheduling is greedy from a shared ordered queue: a device pulls the
next group the moment it is free, so an idle device always receives new
work before any deeper concurrency ( Reproducibility: every column is computed wholly on one device, so
scheduling never changes what a device computes for a beamlet — only
which device computes it (test-pinned). Over identical devices the Dij
is therefore bit-identical however the pulls interleave. Over a
heterogeneous set (e.g. |
None
|
concurrent_batches
|
int
|
Batch lanes per CUDA device: up to this many batches of the current
group transport concurrently, each lane on its own stream with its own
queues, RNG slots, and quanta map. Bitwise inert (test-pinned): the
float64 fold of batch sums is serialized in batch order across lanes,
so any value reproduces the sequential Dij exactly — the knob only
trades memory for overlap, like |
1
|
scoring_grid
|
ScoringGrid | None
|
Semantics as in :meth: |
None
|
scoring_mode
|
str
|
Weights the column tallies as in :meth: |
'dose_to_medium'
|
msc_model
|
str
|
As in :meth: |
'gs'
|
progress
|
ProgressCallback | None
|
Optional callback, as in :meth: |
None
|
Source code in pyradmc/backends/warp/engine.py
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 | |
Results¶
pyradmc.backends.results.TransportResult
dataclass
¶
One engine run: batched dose estimate plus exact energy bookkeeping.
energy_emitted == energy_deposited + energy_unscored + energy_escaped holds
to accumulation precision of the producing backend — float64 exact for ref,
float32 transport arithmetic plus scoring quantization for warp — and is
asserted in the integration tier at each backend's documented tolerance.
energy_escaped is a ledger, not purely physical escape: it
also carries the net weight-energy Russian roulette removes from the transported
population (kills positive, survivor boosts negative), which is exactly what
keeps the identity above exact per run under variance reduction.
energy_unscored is deposit energy that landed inside the transport grid but
outside the scoring grid (decoupled dose grid). It is exactly zero when
the scoring grid covers the transport grid — in particular for the default
score-on-the-transport-grid configuration.
Source code in pyradmc/backends/results.py
dose
instance-attribute
¶
Per-voxel dose on the scoring grid, MeV/g per emitted history.
dose_sigma
instance-attribute
¶
Per-voxel 1-sigma standard error from batch statistics.
scoring_mode
class-attribute
instance-attribute
¶
Tally weighting the dose was produced under: "dose_to_medium"
or "dose_to_water". The energy books are physical in both modes.
provenance
class-attribute
instance-attribute
¶
How this result was produced; see :class:RunProvenance.
Optional on the dataclass so that a hand-assembled result (a test instrument, a reload from disk) need not fabricate one, but every engine run populates it — that contract is test-pinned rather than expressed in the type, because it is a property of the engines, not of the container.
pyradmc.backends.results.RunProvenance
dataclass
¶
The configuration a result was produced under, carried with the result.
A dose array outlives the process that made it: it is archived, handed to an optimizer, attached to a plan, compared against a run from six months ago. Every field here changes the numbers, and none of them is recoverable from the array afterwards — so a result that does not carry them is not reproducible, however carefully the run was scripted.
This is a record, not a control surface: constructing one does not configure
anything, and the engines fill it in from the arguments they were actually
called with (a resolved step_energy_fraction, not the None the caller
may have passed).
Source code in pyradmc/backends/results.py
version
instance-attribute
¶
pyradmc.__version__ of the engine that produced the result.
device
instance-attribute
¶
"cpu" or a CUDA device such as "cuda:0". Results are bit-reproducible
for a given seed on one device, never across devices (AGENTS.md section 2.3).
ecut_mev
instance-attribute
¶
Electron transport and production cutoff, kinetic energy in MeV.
msc_model
instance-attribute
¶
Multiple-scattering model: "gs" (shipped) or "gaussian".
step_energy_fraction
instance-attribute
¶
Resolved electron substep energy-loss fraction, never None.
cross_sections
instance-attribute
¶
Cross-section provenance, from
:attr:~pyradmc.data.interface.CrossSectionSource.provenance — the compiled
library citation for a tabulated source, the parameterization for the analytic
one. This is the field that distinguishes two otherwise identical runs.
deposit_resolution_cm
class-attribute
instance-attribute
¶
Longest piece a half-substep's continuous energy loss was filed as, in cm.
None is the single midpoint deposit — the default, and what every result
produced before this option existed used. A value moves dose within a
transport voxel (never between voxels, and never any total), so two runs that
differ only here agree on the energy books and on any dose scored at voxel
resolution, and can differ below it. That is exactly why it is recorded: it is
not recoverable from the dose array.
summary ¶
One-line human-readable digest, for logs and file headers.
Source code in pyradmc/backends/results.py
pyradmc.scoring.dij.DijResult
dataclass
¶
Sparse beamlet-resolved dose influence matrix, CSC layout by column.
Column j holds beamlet j's dose per emitted history (MeV/g) in the
voxels that survived truncation; sigma is the matching per-entry standard
error. Voxel indices are flat C-order over grid_shape.
correlated records the stream mapping the Dij was computed under
. When True, columns share random streams and are statistically
dependent: each per-entry sigma stays valid on its own, but sigmas
must never be combined across columns in quadrature — cross-column
covariance is not carried here.
Source code in pyradmc/scoring/dij.py
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 | |
provenance
class-attribute
instance-attribute
¶
How this matrix was produced; see
:class:~pyradmc.backends.results.RunProvenance. Every engine run_dij
populates it (test-pinned); optional on the dataclass so a hand-assembled or
reloaded matrix need not fabricate one.
column_dense ¶
sigma_dense ¶
dose_for_weights ¶
Dense dose grid for a fluence-weight vector: sum_j w_j * column_j.
Source code in pyradmc/scoring/dij.py
dose_csc ¶
Export the dose matrix as a scipy.sparse.csc_array, (n_voxels, n_beamlets).
unit="mev_per_g" (default) is the engines' native score, per emitted
history of each column's beamlet; unit="gy" applies the exact SI
calibration :data:pyradmc.GY_PER_MEV_PER_G for absolute dose per
history — a planning consumer scales by its own particles-per-MU on top.
Source code in pyradmc/scoring/dij.py
sigma_csc ¶
Export the per-entry sigma as a scipy.sparse.csc_array, aligned with the dose.
Valid per column (per-beamlet QA) in either mode; see :meth:variance_csc
for the cross-column caveat under correlated sampling.
Source code in pyradmc/scoring/dij.py
variance_csc ¶
Export the per-entry variance (sigma**2), aligned with the dose.
The export a planning consumer stores as its dose-influence variance
matrix (pyRadPlan's physical_dose_var). Each entry is valid on its
own, but when the Dij was computed under correlated sampling (the
shipped default) the columns are statistically dependent, so any
cross-column combination of these variances — variance @ weights, a
quadrature plan-dose sigma — is invalid (AGENTS.md section 8).
That known downstream use is why this export carries a
:func:warnings.warn result caveat when correlated is True; compute
a plan-dose sigma from batch-resolved data or an independent-columns run
instead.
Source code in pyradmc/scoring/dij.py
Geometry and scoring¶
pyradmc.geometry.grid.VoxelGrid
dataclass
¶
Axis-aligned voxel grid with per-voxel density and material.
Voxels are half-open boxes: a position on an internal boundary belongs to the voxel on the upper side, and positions on the upper outer faces are outside. This convention is test-pinned; changing it moves dose by one voxel at every boundary.
Attributes:
| Name | Type | Description |
|---|---|---|
shape |
tuple[int, int, int]
|
Number of voxels along (x, y, z). |
spacing |
tuple[float, float, float]
|
Voxel edge lengths in cm. |
origin |
tuple[float, float, float]
|
Position of the lower corner of voxel (0, 0, 0), in cm. |
density |
ndarray
|
Mass density per voxel in g/cm^3, shape |
material |
ndarray
|
Material index per voxel (see :mod: |
Source code in pyradmc/geometry/grid.py
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 | |
upper_corner
property
¶
Position of the upper outer corner, in cm (outside, half-open).
uniform_water
classmethod
¶
uniform_water(shape: tuple[int, int, int], spacing: tuple[float, float, float], origin: tuple[float, float, float] = (0.0, 0.0, 0.0)) -> VoxelGrid
Build a homogeneous unit-density water grid, the workhorse phantom.
Source code in pyradmc/geometry/grid.py
contains ¶
Whether the position lies inside the grid (upper faces excluded).
voxel_index ¶
Voxel containing the position; the caller guarantees contains.
Source code in pyradmc/geometry/grid.py
distance_to_entry ¶
Distance along (ux, uy, uz) to the grid surface; 0 inside; inf if missed.
The region outside the grid is vacuum, so a particle born outside flies this
distance for free before Woodcock tracking starts. The grid is convex: a
straight flight that leaves it never re-enters, so this is only ever needed
once per particle. Clipping delegates to :func:slab_entry_distance.
Source code in pyradmc/geometry/grid.py
max_density_by_material ¶
Collect the (material, max density) pairs, for the Woodcock majorant.
Feeding anything less than the true per-material maximum into
:meth:pyradmc.data.interface.CrossSectionSource.majorant silently biases
the transport; this method exists so callers never compute it by hand.
Source code in pyradmc/geometry/grid.py
pyradmc.scoring.grid.ScoringGrid
dataclass
¶
Axis-aligned dose-scoring grid with per-voxel mass from the transport grid.
Construct through :meth:for_grid (score on the transport grid itself — the
engines' default, byte-identical to scoring without a separate dose grid) or
:meth:rebin (arbitrary geometry, mass by exact voxel overlap). The mass map
is only meaningful for the transport grid it was built from; hand the engine
a scoring grid built from the same :class:~pyradmc.geometry.grid.VoxelGrid
it transports on.
Voxels follow the transport grid's half-open convention (lower faces inside, upper faces outside), delegated to the shared geometry primitives.
Attributes:
| Name | Type | Description |
|---|---|---|
shape |
tuple[int, int, int]
|
Number of scoring voxels along (x, y, z). |
spacing |
tuple[float, float, float]
|
Scoring voxel edge lengths in cm. |
origin |
tuple[float, float, float]
|
Position of the lower corner of scoring voxel (0, 0, 0), in cm. |
voxel_mass |
ndarray
|
Mass per scoring voxel in g, shape |
Source code in pyradmc/scoring/grid.py
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 | |
deposit_resolution_cm
property
¶
Suggested deposit_resolution_cm for an engine run on this grid: min spacing.
Asks the transport loop to file energy no coarser than this grid can
distinguish. Passing it is only worth the cost when this grid is finer than
the transport voxels; at or above voxel resolution leave the engine's
None default, which is cheaper and equivalent. See
:meth:pyradmc.backends.ref.engine.ReferenceEngine.run.
Half the smallest spacing: a uniform grid has one bin width, so any
divisor of it is commensurate, and halving puts two deposits in the
narrowest voxel. (The aliasing a non-dividing spacing causes on a graded
axis is discussed in
:func:pyradmc.scoring.cylinder.common_bin_divisor.)
upper_corner
property
¶
Position of the upper outer corner, in cm (outside, half-open).
for_grid
classmethod
¶
Score on the transport grid itself: same geometry, mass = density * volume.
This is the engines' scoring_grid=None default. The mass is the direct
per-voxel product — the identical arithmetic the scorer used before scoring
grids existed — not an overlap rebin, so the default path stays
byte-identical, not merely equal to rounding.
Source code in pyradmc/scoring/grid.py
rebin
classmethod
¶
rebin(grid: VoxelGrid, shape: tuple[int, int, int], spacing: tuple[float, float, float], origin: tuple[float, float, float]) -> ScoringGrid
Build a scoring grid of arbitrary geometry, mass by exact voxel overlap.
mass_J = sum_i rho_i * prod_axis overlap_1d over transport voxels i:
exact for non-aligned and non-integer-ratio grids, and it degrades gracefully
at the edges — a partially covered scoring voxel carries the mass of the
covered part only, which is also the only part deposits can arrive in.
Source code in pyradmc/scoring/grid.py
contains ¶
Whether the position lies inside the scoring grid (upper faces excluded).
voxel_index ¶
Scoring voxel containing the position; the caller guarantees contains.
Source code in pyradmc/scoring/grid.py
flat_index ¶
Flat voxel index (ix * ny + iy) * nz + iz, or -1 when outside.
C order, matching voxel_mass.reshape(-1) and the flat buffer the Warp
kernels score into. Reporting "outside" in band is what lets the batched
scorer route a deposit through one query regardless of which scoring
geometry it was handed (see :class:~pyradmc.scoring.cylinder.CylindricalScoringGrid,
which offers the same two methods over a different binning).
Source code in pyradmc/scoring/grid.py
pyradmc.scoring.cylinder.CylindricalScoringGrid
dataclass
¶
Depth-by-radial-shell dose bins about a beam axis parallel to z.
Construct through :meth:for_grid (bins spanning a transport phantom, density
read from it) or :meth:uniform (bins and density stated outright). Hand the
engine a scorer built from the same :class:~pyradmc.geometry.grid.VoxelGrid
it transports on; a scorer reaching outside that grid would divide real energy
by grams that are not there, which :meth:for_grid refuses up front.
Bins follow the transport grid's half-open convention on both axes (the
entrance plane and inner shell surface are inside, the exit plane and outer
surface are outside), delegated to the shared scalar primitives in
:mod:pyradmc.geometry.cylinder so host and kernel cannot disagree.
Attributes:
| Name | Type | Description |
|---|---|---|
axis |
tuple[float, float]
|
|
depth_edges |
ndarray
|
Increasing depth boundaries in cm, |
radial_edges |
ndarray
|
Increasing shell radii in cm, |
voxel_mass |
ndarray
|
Mass per bin in g, shape |
Source code in pyradmc/scoring/cylinder.py
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 | |
depth_origin
property
¶
Position of the entrance plane of the first depth bin, in cm.
depth_thickness
property
¶
Thickness of each depth bin in cm, n_depth of them.
The per-bin dz. Divide an energy-per-bin by this to compare graded bins
against each other on a per-centimetre footing.
n_voxels
property
¶
Number of bins; the length of the flat buffer a device scores into.
deposit_resolution_cm
property
¶
Suggested deposit_resolution_cm for an engine run on this binning.
A pencil kernel bins far below the transport voxel scale, which is exactly where a half-substep filed as one midpoint deposit prints the voxel lattice onto the dose profile. Passing this asks the transport loop to file energy finely enough for these bins to be meaningful.
The depth axis, not the radial one. Spreading subdivides a substep along its own direction, and for the beam this geometry describes that direction is the depth axis. The radial profile is resolved by the transverse spread of many histories, not by subdividing one step — so the innermost geometric shell, which can be micrometres wide, would demand hundreds of deposits per step and buy nothing.
Half the largest length that divides every depth bin width, not simply
the finest bin. A spacing that does not divide the bin width aliases against
it, and because both are locked to the voxel lattice the beat stands still
instead of averaging out — see :func:common_bin_divisor for the measured
sizes. Halving it puts at least two deposits in the narrowest bin while
staying commensurate with all of them.
depth_upper
property
¶
Position of the exit plane of the last depth bin, in cm (outside).
depth_centers
property
¶
Depth bin midpoints in cm — the abscissa a depth-dose curve is plotted on.
radial_centers
property
¶
Area-weighted shell radii in cm: sqrt((r_in^2 + r_out^2) / 2).
The radius that halves each shell's area, which is where a smoothly varying radial quantity averages to its shell mean — not the arithmetic midpoint, which biases outward-falling profiles inward on wide shells.
shell_volume
property
¶
Volume of each bin in cm^3, shape (n_depth, n_shells).
uniform
classmethod
¶
uniform(density: float, axis: tuple[float, float], depth_edges: ndarray, radial_edges: ndarray) -> CylindricalScoringGrid
Bins in a medium of stated uniform density (g/cm^3); mass analytic.
The direct constructor, for a phantom whose density the caller already knows.
:meth:for_grid is the same thing with the density taken from the transport
grid and the coverage checked.
Source code in pyradmc/scoring/cylinder.py
for_grid
classmethod
¶
for_grid(grid: VoxelGrid, radial_edges: ndarray, axis: tuple[float, float] | None = None, depth_edges: ndarray | None = None) -> CylindricalScoringGrid
Bins spanning a transport phantom, density read from it and checked.
Defaults place the cylinder where a pencil-beam kernel run wants it: the
axis on the phantom's lateral centre, and depth bins covering the phantom's
full z extent at its z spacing. Either may be overridden — pass
:func:graded_edges for a depth schedule that follows the build-up.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the binned region reaches outside the transport grid (its mass would
be fictitious), or if the medium it overlays is not uniform in density
and material (the analytic annulus mass would then be wrong, and no
exact separable rebin of an annulus exists — use
:class: |
Source code in pyradmc/scoring/cylinder.py
contains ¶
Whether the position lies inside the binned region (upper faces excluded).
Source code in pyradmc/scoring/cylinder.py
voxel_index ¶
(depth bin, shell) containing the position; caller guarantees contains.
Source code in pyradmc/scoring/cylinder.py
flat_index ¶
Flat bin index iz * n_shells + ir, or -1 when outside.
C order, matching voxel_mass.reshape(-1) and the flat buffer the Warp
kernels score into. Outside is reported in band so that a caller needs one
query per deposit rather than a separate containment test.
Source code in pyradmc/scoring/cylinder.py
pyradmc.scoring.cylinder.uniform_edges ¶
Equal-thickness shell edges from 0 to r_max: n_shells + 1 radii in cm.
Simple, and adequate when the quantity of interest is integral rather than the
near-axis gradient. For a pencil kernel prefer :func:geometric_edges, which
spends its bins where the dose actually varies.
Source code in pyradmc/scoring/cylinder.py
pyradmc.scoring.cylinder.geometric_edges ¶
Shell edges that resolve the core: a central disc, then equal ratios to r_max.
A pencil-beam kernel spans several decades in dose between the axis and the scatter tail, and essentially all of the structure sits in the first few millimetres. Equal-ratio shells put a constant relative resolution everywhere, which is the natural binning for a quantity that falls roughly as a power law.
Geometric spacing cannot start at zero, so the innermost bin is the full disc
[0, r_min) and the remaining n_shells - 1 bins are geometric from
r_min to r_max. Returns n_shells + 1 radii in cm.
Bin choice is deliberately the caller's: it is a readout resolution, not an accuracy-defining default (AGENTS.md 2.8), and no value of it changes transport.
Source code in pyradmc/scoring/cylinder.py
pyradmc.scoring.cylinder.graded_edges ¶
Edges from contiguous (start, stop, step) segments of differing resolution.
The depth binning a pencil-beam kernel database wants: fine through the build-up region, where the curve has all its structure, and coarse in the slowly varying tail, so that bins are spent where the gradient is rather than uniformly. For example, the classic 0-32 cm water schedule
[(0.0, 0.5, 0.010), (0.5, 2.0, 0.025), (2.0, 6.0, 0.25), (6.0, 32.0, 1.0)]
yields 152 bins where a uniform 0.010 cm grid would need 3200.
Every segment must start where the previous one stopped and span a whole number of steps. A segment that does not is refused rather than rounded: absorbing the remainder into a short last bin would misplace every edge downstream of it, and the bin a dose lands in is not a detail that should be decided silently.
Source code in pyradmc/scoring/cylinder.py
pyradmc.scoring.cylinder.common_bin_divisor ¶
Largest length that divides every width in widths, or floor if none does.
Why divisibility matters: sub-substep deposits are laid at a fixed spacing from the step start, and step starts are pinned to transport voxel faces, so the whole point set is locked to the voxel lattice. If the spacing does not divide the scoring bin width, the two beat against each other and the fixed phase turns that beat into a standing ripple rather than noise. Measured on a 6 / 15 MeV pencil beam over 0.025 cm bins: a 0.010 cm spacing (ratio 2.5) leaves 5.7 / 12.7 percent peak-to-trough, while 0.005 cm (ratio 5) leaves 1.3 / 1.7 percent.
Euclid on floats, with a relative tolerance, because bin schedules are built from
decimal step sizes and their exact float representations are not commensurate.
Genuinely incommensurable widths drive the divisor toward zero; floor bounds
that, at the cost of leaving some ripple, since arbitrarily fine spacing is
arbitrarily expensive.
Source code in pyradmc/scoring/cylinder.py
Cross-sections and materials¶
pyradmc.data.interface.CrossSectionSource ¶
Bases: ABC
Abstract source of interaction data for photons and electrons.
Implementations are constructed once, on the host, and then expose flat array
handles to the kernels. The abstract methods below are the host-side query
API used for construction, validation, and the reference backend. Kernel-side
access goes through the flattened tables that build_tables returns.
Source code in pyradmc/data/interface.py
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 | |
n_materials
property
¶
How many registry materials this source can answer for.
Tables are flattened for material indices 0 .. n_materials - 1
(:func:~pyradmc.data.tables.build_cross_section_tables sizes its rows by
this), and a query beyond it must raise rather than approximate: a source
silently answering for a material it has no data for is a silent transport
bias. The default — the full registry — is for material-independent
sources (test instruments); a calibrated source overrides it with its real
coverage (the analytic source: water only; the tabulated source: the
compiled row count).
provenance
property
¶
Human-readable citation for the data this source answers from.
Carried into every result's
:class:~pyradmc.backends.results.RunProvenance so an archived dose says
which cross-sections produced it — the single most consequential thing about
a run and the one least recoverable from the dose array. The default names
the class, which is honest but uninformative; a source built from compiled
data overrides it with that data's own provenance string.
mu_over_rho
abstractmethod
¶
Mass attenuation coefficient for one process, in cm^2/g.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy
|
float
|
Photon energy in MeV. |
required |
material
|
int
|
Material index into the material table. |
required |
process
|
int
|
One of the :class: |
required |
Source code in pyradmc/data/interface.py
mu_over_rho_total
abstractmethod
¶
Total mass attenuation coefficient, in cm^2/g.
Must equal the sum over enabled processes. This redundancy is deliberate:
it is a contract test (tests/unit/test_xs_contract.py).
Source code in pyradmc/data/interface.py
majorant
abstractmethod
¶
Woodcock majorant: the maximum macroscopic total cross-section, in 1/cm.
Taken over all materials and densities present in the geometry, at the given energy. Delta (fictitious) scattering makes up the difference. The majorant must never be exceeded by any real macroscopic cross-section in the geometry; this is a contract test, and violating it silently biases the transport.
See Woodcock et al. (1965), ANL-7050.
Source code in pyradmc/data/interface.py
sample_coherent_cos_theta ¶
Sample the coherent (Rayleigh) polar scattering cosine.
The default is the Thomson distribution (zero-momentum-transfer, flat form
factor); a source with real atomic form-factor data (the tabulated backend)
overrides this to sample the forward-peaked coherent distribution. Kept on the
source, not as a free function, because the angular shape is cross-section data
(AGENTS.md 2.6): the sampling math lives in :mod:pyradmc.physics.rayleigh, the
data that selects it lives here.
Source code in pyradmc/data/interface.py
coherent_cumulative ¶
Return the coherent form-factor cumulative A(x)=\int_0^x F^2 x' dx' on x_grid.
This is the flattened, kernel-consumable face of :meth:sample_coherent_cos_theta:
:func:~pyradmc.data.tables.build_cross_section_tables calls it per material to
fill the table the Warp kernel inverts. The default is the flat form factor,
A(x) = x^2/2, which the sampler inverts to the Thomson distribution — so an
analytic (zero-coherent) source needs no override, and a form-factor source
(tabulated) resamples its compiled cumulative onto x_grid.
Source code in pyradmc/data/interface.py
restricted_stopping_power
abstractmethod
¶
Restricted collision stopping power, in MeV cm^2/g.
Energy losses above delta_cut are excluded, being handled explicitly as
discrete Moller (or Bhabha) events in the Class II scheme.
Berger-Seltzer formulation; see ICRU Report 37 (1984).
Source code in pyradmc/data/interface.py
radiative_stopping_power
abstractmethod
¶
moller_cross_section
abstractmethod
¶
Restricted Moller cross-section per unit mass, in cm^2/g.
Total cross-section for a discrete knock-on collision transferring more than
delta_cut (MeV, kinetic) to a delta ray. Zero when energy is at or
below 2 * delta_cut: by indistinguishability the delta is the lower
energy outgoing electron, so it can carry at most half the kinetic energy.
Consistency contract (tested): the energy moment of the Moller differential
cross-section above delta_cut equals the difference between the
unrestricted and restricted collision stopping powers. Moller (1932),
doi:10.1002/andp.19324060506.
Source code in pyradmc/data/interface.py
csda_range
abstractmethod
¶
Continuous-slowing-down-approximation range, in g/cm^2.
Used for range rejection. An overestimate is safe (it rejects less); an underestimate biases the dose. Implementations must document which side they err on.
Source code in pyradmc/data/interface.py
scattering_power
abstractmethod
¶
Mass angular scattering power, in rad^2 cm^2/g.
Drives the multiple-elastic-scattering hinge deflection: T rho s is
the small-step <theta^2> handed to the sampler. Because
Goudsmit-Saunderson pins <cos theta> = exp(-s N sigma_tr) exactly,
this must be the first transport moment 2 (N_A/A) sigma_el G_1 of
the screened scattering law — a core-width fit such as Highland's is not the same
quantity and under-scatters at high energy (the analytic source's
docstring records the approximation). delta_cut partitions the
electron-electron moment: transfers below it remain condensed here,
while the moment of above-cutoff Moller events is excluded because those
deflections are transported explicitly by the Class-II loop.
Source code in pyradmc/data/interface.py
elastic_screening ¶
Moliere screening parameter of the elastic scattering law, dimensionless.
Where :meth:scattering_power fixes the strength of multiple
scattering, this fixes the shape: it is the parameter of the
screened-Rutherford single-scattering law whose Legendre moments drive
the Goudsmit-Saunderson angular distribution
(:mod:pyradmc.data.goudsmit_saunderson). The two are consistent by
construction — the elastic cross-section is back-derived from
T = 2 (N_A/M) sigma_el G_1(eta) — so the GS mean-square deflection
over a short step reproduces the Fermi-Eyges T rho s exactly. That
anchoring is a contract test
(tests/unit/test_elastic_screening.py): it is what makes GS a
refinement of the shipped hinge rather than a rescaling of it.
Concrete on the interface, computed from the material's elemental composition, so no implementation can silently omit it and the analytic and tabulated backends cannot drift apart on the angular shape. A source carrying real elastic differential data may override it.
Source code in pyradmc/data/interface.py
sample_gs_cos_theta ¶
sample_gs_cos_theta(mean_square_angle: float, energy: float, material: int, rng_state: object) -> float
Sample the Goudsmit-Saunderson multiple-scattering deflection cosine.
The exact multiple-scattering angle for the substep, in place of the
small-angle Gaussian of :func:pyradmc.physics.msc.sample_hinge_cos_theta.
Takes the same mean_square_angle = T rho s the Gaussian hinge takes
and consumes the same single uniform, so the two are drop-in
alternatives that do not shift the random stream relative to each other —
which is what lets a transport comparison isolate the angular model.
Kept on the source rather than as a free function for the reason the
coherent sampler is (see :meth:sample_coherent_cos_theta): the angular
shape is cross-section data. The sampling math lives in
:mod:pyradmc.physics.gs; the table that selects it lives here.
Tables are memoized on a log-spaced (eta, <theta^2>) grid. Binning is
safe because the table is normalized in a scaled deflection: the
rescaling by this call's exact <1 - cos theta> restores the first
moment exactly, so the grid resolution perturbs only the shape.
Source code in pyradmc/data/interface.py
restricted_range ¶
Restricted-collision range down to delta_cut, in g/cm^2.
.. math::
r(E) = \int_{\Delta}^{E} \frac{dE'}{S_{col}(E', \Delta)}
with the restricted collision stopping power of
:meth:restricted_stopping_power — radiative and discrete-Moller losses
are booked separately by the Class II scheme, so this is exactly the mass
path over which the transport loop's continuous loss takes E to the
cutoff. Together with :meth:energy_after_mass_path it defines the
exact-energy-loss substep (DPM; Sempau et al. 2000,
doi:10.1088/0031-9155/45/8/315), replacing the first-order
S(E_start) * rho * s linearization.
Concrete on the interface: implementations answer through
:meth:restricted_stopping_power, so the range can never disagree with
the stopping power it integrates (trapezoid on a dense log grid, cached
per (material, delta_cut); node count pinned by the derivative and
round-trip tests).
Source code in pyradmc/data/interface.py
energy_after_mass_path ¶
Energy after a continuous-loss mass path, r^-1(r(E) - mass_path).
Clamped to [delta_cut, energy]: a path at or beyond the remaining
range returns exactly delta_cut (the loop's range-out branch), and
interpolation wiggle can never gain energy.
Source code in pyradmc/data/interface.py
build_tables ¶
build_tables(ecut: float, pcut: float, e_max: float, n_points: int | None = None) -> CrossSectionTables
Flatten host-side data into kernel-consumable array handles.
Generic over implementations: everything flows through the abstract query methods above, so a source never flattens itself differently from how it answers the host API (that equality is what the table parity tests pin). The result is host NumPy; kernel backends upload and cast it. Its contents are frozen after construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ecut
|
float
|
Electron and photon cutoffs in MeV the tables are built at (accuracy-defining, AGENTS.md section 2.8). |
required |
pcut
|
float
|
Electron and photon cutoffs in MeV the tables are built at (accuracy-defining, AGENTS.md section 2.8). |
required |
e_max
|
float
|
Upper grid edge in MeV; must cover the highest primary energy. |
required |
n_points
|
int | None
|
Grid nodes; defaults to :data: |
None
|
Source code in pyradmc/data/interface.py
pyradmc.data.interface.PhotonProcess ¶
Enumeration of photon interaction channels.
Integer-valued rather than a enum.Enum so that the values can cross into
Warp kernels and the reference backend unchanged.
Source code in pyradmc/data/interface.py
pyradmc.data.analytic.AnalyticCrossSections ¶
Bases: CrossSectionSource
Closed-form photon cross-sections; see the module docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry_densities
|
tuple[tuple[int, float], ...]
|
The |
((WATER, 1.0),)
|
Source code in pyradmc/data/analytic.py
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 | |
n_materials
property
¶
Water only, permanently.
The photoelectric anchor, pair calibration, I-value, density effect and radiative fit are all water-specific. The registry may grow past water; this source does not.
provenance
property
¶
Name the parameterization, so an archived dose is not mistaken for data.
These are closed-form fits with stated few-percent accuracy in the soft spectrum, not measured cross-sections; a result built on them should say so in the same place a tabulated result cites its library.
mu_over_rho ¶
Mass attenuation coefficient for one channel, in cm^2/g.
Source code in pyradmc/data/analytic.py
mu_over_rho_total ¶
Total mass attenuation coefficient: the sum over enabled channels.
Source code in pyradmc/data/analytic.py
majorant ¶
Woodcock majorant over the declared geometry contents, in 1/cm.
The maximum of rho * mu/rho_total over the (material, max density)
pairs declared at construction. Woodcock et al. (1965), ANL-7050.
Source code in pyradmc/data/analytic.py
restricted_stopping_power ¶
Restricted collision stopping power, Berger-Seltzer form, in MeV cm^2/g.
Delegates to :func:pyradmc.data.berger_seltzer.restricted_collision_stopping
(governing equation, citations and stated approximations there) with the water
registry entry — whose I-value and Sternheimer coefficients are the constants
this backend evaluated inline before the multi-material work moved them.
Source code in pyradmc/data/analytic.py
radiative_stopping_power ¶
Radiative stopping power, in MeV cm^2/g: a log-quadratic ESTAR fit.
Like the pair channel, this is a calibration, not a theory: an exact
log-quadratic (:func:pyradmc.data.berger_seltzer.radiative_stopping)
through the material's transcribed NIST ESTAR anchors at 1, 10 and 20 MeV
(Berger & Seltzer's data behind ESTAR; ICRU Report 37 (1984)).
Source code in pyradmc/data/analytic.py
moller_cross_section ¶
Restricted Moller cross-section per unit mass, in cm^2/g.
Delegates to :func:pyradmc.data.berger_seltzer.restricted_moller_cross_section
(closed form and citation there). Zero at or below 2 delta_cut.
Source code in pyradmc/data/analytic.py
csda_range ¶
CSDA range in g/cm^2: the range integral of the total stopping power.
Precomputed at construction (:func:pyradmc.data.berger_seltzer.csda_range_table)
and interpolated in log energy. Errs on the under-estimating side for range
rejection (the table's grid starts above zero, truncating the sub-keV tail) by
well under 1e-4 g/cm^2 — the safe side is documented in the interface as the
over-estimate, so range rejection (when it arrives) must add its own safety
margin anyway; the truncation here is orders of magnitude below any voxel
dimension this engine will see.
Source code in pyradmc/data/analytic.py
scattering_power ¶
Mass angular scattering power, in rad^2 cm^2/g.
The Class-II first transport moment of the Moliere-screened Rutherford law,
T = 2 (N_A/A) [Z^2 sigma_tr + Z(sigma_tr - sigma_tr,M^hard)],
from the material's composition
(:func:pyradmc.data.goudsmit_saunderson.transport_moment_scattering_power).
That is the strength Goudsmit-Saunderson theory pins for the shape the GS
tables are built from; the Rossi-Greisen/Highland (14.1/pv)^2 / X_0 core
width used until 2026-09 lacked its energy-growing logarithm and
under-scattered multi-MeV electrons. Atomic-electron scattering below
delta_cut remains condensed; the hard Moller transport moment above
the cut is removed because the transport loop applies it explicitly.
Source code in pyradmc/data/analytic.py
pyradmc.data.tabulated.source.TabulatedCrossSections ¶
Bases: CrossSectionSource
Interpolating source over compiled :class:TabulatedData.
geometry_densities are the (material, maximum mass density) pairs present
in the geometry, exactly as for the analytic source; the Woodcock majorant is
taken over them.
Source code in pyradmc/data/tabulated/source.py
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 | |
n_materials
property
¶
The compiled table's row count; queries beyond it raise on lookup.
provenance
property
¶
The compiled table's own citation: libraries, stopping strategy, cuts.
sample_coherent_cos_theta ¶
Sample the coherent cosine from the compiled atomic form factor.
Falls back to the base Thomson sampler if the table carries no form-factor data
(coherent_x/coherent_cumulative unset).
Source code in pyradmc/data/tabulated/source.py
coherent_cumulative ¶
Resample the compiled coherent cumulative onto x_grid for the kernel tables.
Falls back to the base flat (Thomson) cumulative if no form-factor data. The
cumulative is a monotone function of x, so linear resampling onto a grid
within its range is faithful; the sampler only uses cumulative ratios.
Source code in pyradmc/data/tabulated/source.py
mu_over_rho ¶
Mass attenuation coefficient for one channel, in cm^2/g.
Source code in pyradmc/data/tabulated/source.py
mu_over_rho_total ¶
Total mass attenuation coefficient: the sum over compiled channels.
majorant ¶
Woodcock majorant over the declared geometry contents, in 1/cm.
restricted_stopping_power ¶
Restricted collision stopping power at the compiled cut, in MeV cm^2/g.
Source code in pyradmc/data/tabulated/source.py
radiative_stopping_power ¶
Radiative (bremsstrahlung) mass stopping power, in MeV cm^2/g.
moller_cross_section ¶
Moller cross section for delta rays above the compiled cut, in cm^2/g.
Source code in pyradmc/data/tabulated/source.py
csda_range ¶
scattering_power ¶
Class-II multiple-scattering power at the compiled cut, rad^2 cm^2/g.
Source code in pyradmc/data/tabulated/source.py
integrate_product ¶
integrate_product(quantity_a: str, quantity_b: str, material: int, e_lo: float, e_hi: float, *, n_sub: int = 64) -> float
Integrate the product of two tabulated electron quantities over energy.
Returns \int_{e_lo}^{e_hi} a(E) b(E) dE where a and b are the
log-linear interpolants of the named quantities — capturing their intra-bin
covariance, which multiplying separately averaged bin quantities discards
(AGENTS.md 2.7). The interval is sampled on a fine geometric sub-grid and
the product integrated by the trapezoidal rule; n_sub sets the resolution.
Source code in pyradmc/data/tabulated/source.py
pyradmc.data.materials.MaterialData
dataclass
¶
Host-side physical data for one material.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable identifier. |
density |
float
|
Reference mass density in g/cm^3. Voxel densities in the geometry scale macroscopic cross-sections relative to this via the mass quantities, so this value is informational for water-like transport, not accuracy-defining. (The Sternheimer coefficients below were computed at this density; evaluating them for a voxel at a different density is the standard density-scaling approximation every mass-quantity lookup in this engine already makes.) |
electrons_per_gram |
float
|
Electron density per unit mass in 1/g, i.e. |
composition |
tuple[tuple[int, float], ...]
|
Elemental mass fractions |
mean_excitation_mev |
float
|
Mean excitation energy I in MeV (ICRU-37 vintage, consistent with the Sternheimer coefficients and with NIST ESTAR). |
sternheimer |
SternheimerParameters
|
Density-effect coefficients, computed at |
radiative_anchors |
tuple[tuple[float, float], ...]
|
|
Source code in pyradmc/data/materials.py
Sources¶
pyradmc.geometry.source.Source ¶
Bases: ABC
Interface for an open-field primary source (consumed by Engine.run).
Implement :meth:emit and :attr:max_energy and the reference backend transports
it. For a device backend, the default :meth:sample_batch provides the simple
host-pre-sampling route for free; override :attr:warp_sampler with a @wp.func
for the advanced in-kernel route. See the module docstring.
Source code in pyradmc/geometry/source.py
max_energy
abstractmethod
property
¶
Highest primary energy in MeV, for cross-section table sizing.
emit
abstractmethod
¶
sample_batch ¶
Host-sample histories [history_offset, history_offset + n) into columns.
Returns the device upload columns (particle_type plus
:data:_UPLOAD_COLUMNS). The default calls :meth:emit per history; override
for a vectorized sampler.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.BeamletSource ¶
Bases: ABC
Interface for a beamlet-resolved source (consumed by Engine.run_dij).
Like :class:Source but every emission is tagged by a beamlet index: the Dij
assembles one dose column per beamlet. Implement :meth:emit, :meth:n_beamlets
and :attr:max_energy; :meth:sample_beamlet_batch is the default simple route
and :attr:warp_beamlet_sampler the advanced one. (Beamlet geometry — a
rectangle, a lattice, an arbitrary aperture — is the source's private business; the
engine only ever asks for the count and per-beamlet emissions.)
Source code in pyradmc/geometry/source.py
max_energy
abstractmethod
property
¶
Highest primary energy in MeV, for cross-section table sizing.
n_beamlets
abstractmethod
property
¶
Number of beamlets whose columns the Dij will hold.
emit
abstractmethod
¶
sample_beamlet_batch ¶
sample_beamlet_batch(seed: int, history_offset: int, n: int, beamlet: int) -> dict[str, npt.NDArray[Any]]
Host-sample n primaries of one beamlet into device upload columns.
The caller (the Dij engine) chooses seed/history_offset to realize the
correlated-sampling history mapping; this just emits that beamlet's primaries.
The default calls :meth:emit; override for a vectorized sampler.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.Primary ¶
Bases: NamedTuple
One emitted primary particle.
kind and weight are additive with defaults so the monoenergetic beam
sources — which emit unit-weight photons and construct Primary positionally
with the first seven fields — are unchanged. kind is None for those
sources, meaning "defer to the engine's primary_kind argument"; a
phase-space source sets it per record ("photon", "electron", "positron"). A
weight other than 1.0 is the statistical weight a phase-space record carries.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.PencilBeamSource
dataclass
¶
Bases: Source
Zero-width monoenergetic beam from a fixed point along a fixed direction.
The direction is normalized at construction; a non-unit direction here would silently stretch every sampled path length.
Source code in pyradmc/geometry/source.py
emit ¶
Emit the (deterministic) primary; consumes no random numbers.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.ParallelBeamSource
dataclass
¶
Bases: Source
Broad parallel beam along +z, uniform over a rectangular field at plane z.
The broad-beam geometry of the buildup test: uniform fluence over
x_range x y_range, all photons travelling in +z.
Source code in pyradmc/geometry/source.py
emit ¶
Emit one primary at a uniform position in the field; consumes two uniforms.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.BeamletGridSource
dataclass
¶
Bases: BeamletSource
Parallel beamlet lattice along +z: an n_x x n_y tiling of the field.
The Dij source. Each beamlet is one rectangle of the tiling, indexed
x-major: j = jx * n_y + jy. Which beamlet a history feeds is the caller's
decision — the engines derive it deterministically from the history index
(stratified sampling), so per-beamlet history counts are exact rather than
multinomial. emit then places the primary uniformly within that beamlet,
consuming exactly the two uniforms :class:ParallelBeamSource consumes for the
whole field; a 1x1 lattice is therefore bit-identical to the open field on a
given target (test-pinned).
Beamlets partition the primary fluence and transport is linear in the source, so scoring each history's whole family into its beamlet's column decomposes the open-field dose exactly — no crosstalk approximation. (A phase-space source would break unique beamlet ownership; that is a known limitation.)
Source code in pyradmc/geometry/source.py
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 | |
beamlet_bounds ¶
Rectangle (x_lo, x_hi, y_lo, y_hi) of one beamlet.
Edges are computed by linear interpolation between the field bounds (never by accumulating widths), so the outer edges of the lattice are exactly the field bounds and shared edges are exactly equal between neighbours.
Source code in pyradmc/geometry/source.py
emit ¶
Emit one primary uniformly within beamlet; consumes two uniforms.
The draw order (x, then y) and count match :class:ParallelBeamSource.emit
so the 1x1 lattice bit-equivalence holds.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.GaussianSpotBeamSource ¶
Bases: Source
Photons born on a plane rectangle, aimed from a 2D-Gaussian focal spot.
The simplified head-input source of the BLD workstream: emission happens on
the rectangle at the reference plane (e.g. directly upstream of the limiting
devices), each photon travelling as if it originated at the Gaussian spot —
compose with :class:~pyradmc.geometry.collimation.CollimatedSource (whose
full-line convention handles the devices downstream of this plane) or feed
the head pre-solve. sigma_u = sigma_v = 0 degenerates to
:class:SpectralBeamSource's fan lines, started on the plane. Transports on
both backends via the vectorized pre-sampling route.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.GaussianSpotBeamletSource ¶
Bases: BeamletSource
The beamlet-resolved planar Gaussian-spot source (one rectangle per bixel).
Beamlet j emits on the rectangle centred at centers[j]; the draw
stream never sees the beamlet, so correlated Dij sampling replays the same
energy, in-rectangle offset and spot point in every column. See
:class:GaussianSpotBeamSource for the geometry and conventions.
Source code in pyradmc/geometry/source.py
max_energy
property
¶
The spectrum's top bin edge, for cross-section table sizing.
emit ¶
Emit one primary for beamlet; consumes exactly six uniforms.
Source code in pyradmc/geometry/source.py
sample_beamlet_batch ¶
sample_beamlet_batch(seed: int, history_offset: int, n: int, beamlet: int) -> dict[str, npt.NDArray[Any]]
Vectorized per-beamlet batch; chunk-invariant and beamlet-blind.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.PrimaryFluenceBeamSource ¶
Bases: Source
Open field from a measured primary fluence: the Tacke et al. (2006) VSM.
Photons are born on a rectangle of a plane upstream of the beam-limiting
devices, aimed from a 2D-Gaussian focal spot, with an energy drawn from
spectrum and a statistical weight equal to the machine's measured radial
primary fluence at that point. It is
:class:GaussianSpotBeamSource plus the fluence shape, and degenerates to it
exactly for a table that is flat over the rectangle.
Compose with :class:~pyradmc.geometry.collimation.CollimatedSource to add
jaws and an MLC downstream of the plane, or feed it to the head pre-solve.
See :class:_PrimaryFluenceFan for the weighting rationale and its cost, and
:class:~pyradmc.geometry.fluence.RadialFluence for the table conventions.
Transports on both backends via the vectorized pre-sampling route.
Stated approximation: the emitted spectrum is the same at every off-axis
radius. Real flattened beams soften off axis (the filter is thicker on the
central ray), an effect the source paper models with a radius-dependent
spectrum; a caller who needs it can build a
:class:CompositeSource of annular components with different spectra.
Source code in pyradmc/geometry/source.py
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 | |
max_energy
property
¶
The spectrum's top bin edge, for cross-section table sizing.
fluence
property
¶
The measured radial primary fluence weighting each history.
weight_at ¶
Report the weight a primary born at point on the emission plane carries.
emit ¶
Emit one weighted primary on the plane; consumes exactly six uniforms.
sample_batch ¶
Vectorized simple-route batch; see :meth:_PrimaryFluenceFan.sample_weighted_batch.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.PrimaryFluenceBeamletSource ¶
Bases: BeamletSource
The beamlet-resolved primary-fluence source (one plane rectangle per bixel).
Beamlet j emits on the rectangle centred at centers[j], all of which
must lie on the one plane the fluence is defined against. The draw stream
never sees the beamlet, so correlated Dij sampling replays the same energy,
in-rectangle offset and spot point in every column; the weights do differ
between columns, which is the point — each bixel sits at its own off-axis
radius and therefore its own primary fluence. See
:class:PrimaryFluenceBeamSource for the geometry and the stated
approximation.
Source code in pyradmc/geometry/source.py
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 | |
max_energy
property
¶
The spectrum's top bin edge, for cross-section table sizing.
fluence
property
¶
The measured radial primary fluence weighting each history.
weight_at ¶
Report the weight a primary born at point on the emission plane carries.
emit ¶
Emit one weighted primary for beamlet; consumes exactly six uniforms.
Source code in pyradmc/geometry/source.py
sample_beamlet_batch ¶
sample_beamlet_batch(seed: int, history_offset: int, n: int, beamlet: int) -> dict[str, npt.NDArray[Any]]
Vectorized per-beamlet batch; the draws are beamlet-blind, the weights are not.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.SpectralBeamSource ¶
Bases: Source
Divergent polyenergetic open field: a focal spot fanning through one aperture.
The photon energy is sampled from a histogram
:class:~pyradmc.geometry.spectrum.Spectrum by CDF inversion; the geometry is a
point source at focal_point emitting toward points sampled uniformly in the
rectangular aperture at the reference plane (see :class:_DivergentFan for the
engine-frame convention and axis semantics). The Warp engine routes this exact
type to a built-in in-kernel generator (CDF inversion over the uploaded
:attr:spectrum tables — no host round trip; the host route was measured
wall-dominant on CT-grade runs); the reference backend uses :meth:emit, and a
subclass transports via the pre-sampling route, since an override may change
what a history means.
Source code in pyradmc/geometry/source.py
max_energy
property
¶
The spectrum's top bin edge, for cross-section table sizing.
focal_point
property
¶
The point source position, engine frame (cm).
center
property
¶
The aperture centre on the reference plane, engine frame (cm).
u_axis
property
¶
First aperture axis (unit vector after fan validation).
v_axis
property
¶
Second aperture axis (unit vector after fan validation).
emit ¶
Emit one primary through the aperture; consumes exactly four uniforms.
sample_batch ¶
Vectorized simple-route batch; see :meth:_DivergentFan.sample_through_batch.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.SpectralBeamletSource ¶
Bases: BeamletSource
Divergent polyenergetic beamlet fan — the pyRadPlan adapter's Dij source.
Beamlet j is the fan from focal_point through the rectangular aperture
centred at centers[j] (all engine-frame; see :class:_DivergentFan). The
beamlet order is the caller's: pyRadPlan hands the centres in its own
bixel-index order and the Dij columns come back in the same order. All bixels
share one aperture size, the width at the reference plane the centres lie on.
The Warp engine routes this exact type to a built-in in-kernel generator (the
host per-beamlet pre-sampling was measured wall-dominant on CT-grade Dij
runs); the reference backend uses :meth:emit, and a subclass transports
via the pre-sampling route, since an override may change what a history means.
Source code in pyradmc/geometry/source.py
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 | |
max_energy
property
¶
The spectrum's top bin edge, for cross-section table sizing.
focal_point
property
¶
The point source position, engine frame (cm).
centers
property
¶
The beamlet aperture centres, engine frame (cm), in the caller's order.
u_axis
property
¶
First aperture axis (unit vector after fan validation).
v_axis
property
¶
Second aperture axis (unit vector after fan validation).
emit ¶
Emit one primary for beamlet; consumes exactly four uniforms.
Source code in pyradmc/geometry/source.py
sample_beamlet_batch ¶
sample_beamlet_batch(seed: int, history_offset: int, n: int, beamlet: int) -> dict[str, npt.NDArray[Any]]
Vectorized per-beamlet batch; see :meth:_DivergentFan.sample_through_batch.
The caller keys history_offset for the correlated/independent mapping;
the draw stream never sees the beamlet, so correlated sampling replays the
same energy and in-aperture offset in every beamlet's column (test-pinned).
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.CompositeSource ¶
Bases: Source
A mixture of open-field sources — the virtual-source-model building block.
Each history is emitted by one component, chosen with probability proportional to
its weight (a single uniform draw), then that component's emit runs. So a beam
modelled as, e.g., a narrow Gaussian core plus a broad scatter tail is
CompositeSource([(core, 0.85), (tail, 0.15)]). weight is the selection
probability, which equals the fluence fraction for unit-weight components; a
component that itself carries a per-primary weight (a phase space) has that weight
multiplied on top.
Composites transport on both backends through the pre-sampling route (they carry no
warp_sampler); a mixture wanting the in-kernel route writes a single
warp_sampler that branches internally.
Source code in pyradmc/geometry/source.py
pyradmc.geometry.source.CompositeBeamletSource ¶
Bases: BeamletSource
A per-beamlet mixture of beamlet sources — a VSM for beamlet-resolved dose.
Every component describes the same beamlets (identical n_beamlets), so beamlet
j is a mixture: :meth:emit chooses a component by weight (one uniform) and
emits that component's beamlet j. Assembling the Dij then gives each beamlet's
column as the virtual-source-model dose. Like :class:CompositeSource, it runs on
both backends through the pre-sampling Dij route; weight is the selection
probability. (The Dij transports each beamlet primary as a unit-weight photon, so
components should be photon beamlet sources.)
Source code in pyradmc/geometry/source.py
pyradmc.geometry.phasespace.PhaseSpaceSource ¶
Bases: Source
A primary source that samples particles from an IAEA phase-space file.
Unlike the analytic beam sources, a phase-space file is a recorded mix of
photons, electrons and positrons at non-unit statistical weights; each emitted
:class:~pyradmc.geometry.source.Primary therefore carries its own kind and
weight. :meth:emit draws one record per history from the history's RNG
stream (uniform random sampling with replacement), so the source stays a pure
function of (seed, history) like the rest of the engine.
Streaming. The file is memory-mapped, not read into RAM: only the header
plus a one-pass scan of the per-record type byte are held eagerly, and each
:meth:emit decodes a single record on demand. A multi-gigabyte, tens-of-
millions-of-particle file (a real linac phase space) is therefore usable
without loading it — resident memory tracks the pages actually touched.
Unsupported particles. A photon MC transports only photons, electrons and
positrons; a real file may carry the odd neutron/proton (IAEA codes 4/5). With
skip_unsupported (the default) those records are excluded from the sampled
population and their count is warned — one stray particle in tens of millions
should not reject the file, and excluding a ~1e-8 fraction is negligible. With
skip_unsupported=False any unsupported record makes construction raise.
Because a phase-space particle can be any type at any position, this source
breaks the unique beamlet ownership the Dij design relies on; it plugs into
run on either backend, never run_dij. Per-history :meth:emit serves
the reference engine; :meth:sample_batch serves the warp engine, which
transports a whole chunk at once (docs/decisions.md).
Source code in pyradmc/geometry/phasespace.py
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 | |
max_energy
property
¶
Highest particle energy in the file, in MeV (for table sizing).
emit ¶
Emit one primary, sampled uniformly from the file; consumes one uniform.
Source code in pyradmc/geometry/phasespace.py
sample_batch ¶
Sample n primaries for histories [history_offset, history_offset + n).
Returns the primaries as column arrays for bulk upload to a device backend,
keyed particle_type (IAEA code 1/2/3), energy, x/y/z, ux/uy/uz
and weight, geometry as float32 to match the device queue. Sampling and
decoding are both vectorized: record indices come from a single PCG64(seed)
stream advanced to history_offset, so history h always draws the
h-th value regardless of chunking (chunk-invariant), and the records are
gathered from the mmap through :data:_record_dtype in one fancy-indexed read.
This is a different stream from the reference :meth:emit (which spawns a
per-history generator), so the two backends draw different records — both
unbiased estimators of the same dose, compared statistically, never bit-wise.
Source code in pyradmc/geometry/phasespace.py
close ¶
Release the memory map and file handle.
Source code in pyradmc/geometry/phasespace.py
pyradmc.geometry.phasespace.InMemoryPhaseSpaceSource ¶
Bases: Source
A phase space held as column arrays — the treatment-head pre-solve's output.
The same sampling contract as :class:PhaseSpaceSource (one uniform per
emit; chunk-invariant vectorized :meth:sample_batch on its own PCG64
stream; per-particle kind and weight) backed by arrays built in
memory instead of an IAEA file, so geometry/head.py can hand its scored
exit-plane particles straight to run. Like the file-backed source it
breaks unique beamlet ownership and never plugs into run_dij.
Directions must be unit vectors (validated to 1e-5 — pre-solve output is
float64, so this is a correctness check, not a tolerance); particle_type
uses the IAEA codes (photon 1, electron 2, positron 3). The finite-reuse
latent-variance caveat (:class:_LatentVarianceTripwire) applies with force
here: the population size is whatever the pre-solve was asked for, so
oversampling it is easy — size the pre-solve at or above the planned
transport histories.
Source code in pyradmc/geometry/phasespace.py
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 | |
columns ¶
Return a copy of the stored population as column arrays.
particle_type (IAEA codes) plus the float64 columns as constructed —
for introspection, diagnostics and serialization; the sampling routes
(:meth:emit, :meth:sample_batch) remain the transport-facing API.
Source code in pyradmc/geometry/phasespace.py
emit ¶
Emit one stored particle, sampled uniformly; consumes one uniform.
Source code in pyradmc/geometry/phasespace.py
sample_batch ¶
Vectorized upload columns for histories [offset, offset + n).
A pure fancy-indexed gather of the stored columns at the chunk-invariant
:func:_sample_indices; the same stream/independence caveats as the
file-backed source apply.
Source code in pyradmc/geometry/phasespace.py
Spectra¶
pyradmc.geometry.spectrum.Spectrum ¶
A histogram photon spectrum sampled by CDF inversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
Sequence[float] | NDArray[floating[Any]]
|
Bin edges in MeV, strictly increasing, first edge positive; |
required |
weights
|
Sequence[float] | NDArray[floating[Any]]
|
Per-bin content (an integral over the bin, not a density); |
required |
convention
|
str
|
|
'number'
|
Source code in pyradmc/geometry/spectrum.py
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 | |
bin_probabilities
property
¶
Normalized per-bin photon emission probabilities (read-only).
cdf
property
¶
Cumulative bin probabilities (read-only); n values ending at 1.
The inversion table a device backend uploads to sample energies in-kernel
with the same searchsorted convention as :meth:sample_energy.
mean_energy
property
¶
Photon-number-weighted mean energy, MeV, on the bin-midpoint approximation.
sample_energy ¶
Sample one photon energy by CDF inversion; consumes exactly two uniforms.
The first uniform selects the bin from the cumulative weights (the
:class:~pyradmc.geometry.source.CompositeSource selection idiom); the
second places the energy uniformly within the bin.
Source code in pyradmc/geometry/spectrum.py
sample_energies ¶
Vectorized CDF inversion from caller-supplied uniforms.
The same inversion as :meth:sample_energy — u_bin selects the bin,
u_within the position inside it — for the vectorized pre-sampling batch
of the spectral sources, which draws its uniforms from its own stream.
Source code in pyradmc/geometry/spectrum.py
pyradmc.geometry.spectrum.ali_rogers_mv ¶
Build a Spectrum from the Ali and Rogers (2012) analytic MV form.
The governing equation is in :func:_psi_continuum; per-bin photon numbers are
the sub-grid integrals of psi(E) / E (the product is integrated, not bin
means — the AGENTS.md 2.7 rule), and a c4 > 0 adds the 511 keV annihilation
line inside the common filtration envelope of function 13. Its photon content is
c4 * exp(-mu_W C1^2 - mu_Al C2^2) / 0.511 at 511 keV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beam
|
str | AliRogersMV
|
A key of :data: |
required |
e_min
|
float
|
Lower spectrum edge in MeV. Must stay at or above the 69.5 keV validity
floor of the tungsten attenuation parameterization; the default 0.15 MeV
is far above |
0.15
|
n_bins
|
int
|
Histogram resolution; the default matches the paper's 100-bin spectra. |
100
|
Source code in pyradmc/geometry/spectrum.py
Primary fluence¶
pyradmc.geometry.fluence.RadialFluence ¶
A radially symmetric primary fluence psi(r), linearly interpolated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radii
|
Sequence[float] | NDArray[floating[Any]]
|
Off-axis radii in cm, non-negative and strictly increasing; at least
two values. They are distances at :paramref: |
required |
values
|
Sequence[float] | NDArray[floating[Any]]
|
Relative fluence at each radius; one per radius, non-negative, at least one positive. Used as given (see the module docstring on normalization). |
required |
reference_distance
|
float
|
Distance from the focal spot, in cm, at which |
100.0
|
Source code in pyradmc/geometry/fluence.py
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 | |
radii
property
¶
Tabulated off-axis radii in cm at :attr:reference_distance (read-only).
reference_distance
property
¶
Distance from the focal spot, cm, at which :attr:radii are quoted.
max_radius
property
¶
Largest tabulated radius in cm; beyond it the fluence reads zero.
at_radius ¶
Interpolate psi at one radius (cm at :attr:reference_distance).
The magnitude is taken, so a signed off-axis coordinate reads correctly. See the module docstring for the two extrapolation rules.
Source code in pyradmc/geometry/fluence.py
at_radii ¶
Vectorized :meth:at_radius, for the pre-sampling batch route.
Source code in pyradmc/geometry/fluence.py
from_file
classmethod
¶
from_file(path: str | PathLike[str], radius_scale: float = 0.1, reference_distance: float = 100.0) -> RadialFluence
Read a two-column whitespace table of radius fluence (# comments).
This is the layout of the PPBKC primflu.dat commissioning file, whose
radii are in mm at isocentre; radius_scale converts them to the
engine's cm (pass 1.0 for a table already in cm).
Source code in pyradmc/geometry/fluence.py
Random numbers¶
pyradmc.rng.host.HostRNG ¶
Bases: RNG
Counter-based NumPy RNG; see the module docstring.
Source code in pyradmc/rng/host.py
init_state ¶
Create the generator for one history as a pure function of the arguments.
SeedSequence(seed, spawn_key=(history_index,)) hashes both integers into
the PCG64 state, so histories neither share nor overlap streams regardless of
how they are partitioned into batches.
Source code in pyradmc/rng/host.py
uniform ¶
Accuracy-defining constants¶
These live in one place so that a change to any of them is visible in a diff. They are not tuning knobs: changing one requires a test demonstrating the dosimetric effect.
pyradmc: fast photon Monte Carlo for beamlet-resolved treatment planning.
See AGENTS.md for the development contract.
This module is the public API: everything named in __all__ is supported and
versioned, and everything else is an implementation detail that may move between
releases. Physical constants and defaults that are accuracy-defining live here too,
so that there is exactly one place to change them and so that a change is visible in
a diff. They are not tuning knobs; see AGENTS.md section 2.8.
ECUT_MEV
module-attribute
¶
Electron transport and production cutoff, kinetic energy in MeV (DPM default).
PCUT_MEV
module-attribute
¶
Photon transport cutoff in MeV. Below this, energy is deposited locally.
DIJ_TRUNCATION_RELATIVE
module-attribute
¶
Dij column truncation, relative to that beamlet column's maximum.
This biases the low-dose tail, which is where NTCP and LET-guided objectives operate. It is tested against DVH endpoints, never against a matrix norm.
PHOTON_ROULETTE_MEV
module-attribute
¶
Photons below this energy play Russian roulette at their creation or scatter.
Chosen just below the 511 keV annihilation line so annihilation photons are exempt and positron energy accounting stays analog.
PHOTON_ROULETTE_SURVIVAL
module-attribute
¶
Survival probability per game; a survivor's weight is boosted by its inverse.
PHOTON_ROULETTE_WEIGHT_CAP
module-attribute
¶
No roulette at or above this weight (the weight-window ceiling).
Caps the boost cascade at two consecutive survivals (1 -> 2 -> 4), bounding the graininess a single high-weight deposit can leave in the low-dose tail.
PHOTON_SPLIT_N
module-attribute
¶
Compton splitting multiplicity at a primary photon's first Compton scatter.
N = 1 is splitting off — the shipped configuration. At N == 1 the
primary Comptons into a single full-weight copy, i.e. exactly analog transport.
For N > 1 the primary's Compton final state is sampled N times, each copy
(scattered photon + recoil electron) carrying weight 1 / N: N independent
samples of the dominant scatter source, exactly unbiased and energy-conserving
per realization, with cost growing about linearly in N. Only the primary
splits, so the population is bounded and the soft-photon roulette culls the
degraded copies.
This is a variance-reduction efficiency knob, not accuracy-defining: it changes
realizations and cost, never expectations. Correctness of the N > 1 path
(unbiasedness, energy books, N-fold fair copies, variance reduction) is
test-pinned with N = 2 as the instrument, so the mechanism stays validated
though it is dormant.
Why it ships off (measured). For the analytic-water
Dij, splitting does not earn its keep: the figure of merit 1/(sigma^2*time)
is < 1 on the reference CPU (variance falls to ~0.67 in the high/mid-dose
region but cost rises ~1.7x) and roughly neutral on the GPU (a warp retires
with its longest thread). Worse, it does not help the low-dose tail — the
Dij's NTCP/LET region — because that tail is fed by rare wide-angle multiple
scatters that uniform primary splitting cannot target; splitting deeper only
degrades the FOM further (measured).
Re-measured on a phase-space source, it still ships off. On now-stable-power hardware the FOM ratio split/no-split was 0.75, 0.48, 0.28 at N = 2, 4, 8 — worse, monotonically. First-Compton splitting decorrelates copies only after that scatter (variance saturates far below 1/N) while cost grows ~linearly, and emitting a phase-space primary is as cheap as an analytic beam, so the cost structure matches. The N > 1 path stays retained and N=2-pinned. See docs/decisions.md for the full record and the emission-time-splitting alternative.
ELECTRON_MASS_MEV
module-attribute
¶
Electron rest mass energy in MeV (CODATA 2022).
GY_PER_MEV_PER_G
module-attribute
¶
Absolute-dose calibration: 1 MeV/g = this many gray.
Exact by SI definition: 1 MeV = e x 1e6 J with the elementary charge fixed at
1.602176634e-19 C (SI 2019), and per gram -> per kilogram is 1e3. The engines
score dose in MeV/g per emitted history; a planning consumer multiplies by this
constant for Gy per history and applies its own particles-per-MU scaling on top
(see :meth:pyradmc.scoring.dij.DijResult.dose_csc).
RAYLEIGH_MOMENTUM_TRANSFER_PER_MEV
module-attribute
¶
Coherent-scattering momentum-transfer coefficient: the tabulated form-factor abscissa
is x [1/angstrom] = this * E[MeV] * sin(theta/2), i.e. 1/hc with
hc = 0.012_398_42 MeV*angstrom (CODATA 2022). EPDL MF=27 tabulates F against x.
Subsystems documented at module level¶
These are coherent subsystems with their own vocabulary rather than names a first script reaches for. They are supported, but imported from their modules:
| Module | What it provides |
|---|---|
pyradmc.geometry.collimation |
Jaw pairs, rounded-tip MLC, collimated and transmission-mask source wrappers |
pyradmc.geometry.head |
Treatment-head pre-solve producing an exit-plane phase space |
pyradmc.adapters.ct |
Hounsfield calibration and CT image reading |
pyradmc.data.tabulated |
Table precompiler and the EPICS build tool |
pyradmc.study |
Toy fluence optimizer and DVH endpoints, used by the noise/bias study |