Simulation
jaxonomy.simulation
BatchSimulationResults
dataclass
Results from :func:simulate_batch.
Attributes:
| Name | Type | Description |
|---|---|---|
time |
Any
|
Time vector of shape |
outputs |
dict[str, Any]
|
Mapping |
used_vmap |
bool
|
|
provenance |
ProvenanceManifest | None
|
Optional :class: |
Source code in jaxonomy/simulation/batch.py
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 | |
mean(signal)
Mean trajectory across the batch (axis 0).
Source code in jaxonomy/simulation/batch.py
83 84 85 | |
percentile(signal, p)
p-th percentile across the batch at each time index; p in [0, 100].
Source code in jaxonomy/simulation/batch.py
91 92 93 | |
std(signal)
Standard deviation across the batch (axis 0).
Source code in jaxonomy/simulation/batch.py
87 88 89 | |
to_simulation_results(idx)
Slice one batch index into a :class:SimulationResults (no final context).
Source code in jaxonomy/simulation/batch.py
95 96 97 98 99 100 101 102 | |
Decay
Bases: LeafSystem
xdot = -k * x; output = x.
Source code in jaxonomy/simulation/testing_systems.py
4 5 6 7 8 9 10 11 12 13 14 15 16 | |
FastRestartSimulator
Stateful single-simulation runner that reuses one JIT-compiled kernel.
The simulator is built lazily on the first :meth:run so that the
recorded_signals set passed to the constructor (which selects the
set of recorded ports baked into the kernel) is locked in before
compilation. Subsequent :meth:run calls reuse the same compiled
XLA program — only the parameter pytree changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
A :class: |
required | |
t_span
|
tuple[float, float]
|
|
required |
options
|
SimulatorOptions | None
|
:class: |
None
|
recorded_signals
|
dict[str, OutputPort] | None
|
Mapping |
None
|
The context-manager protocol is supported but not strictly required;
use with FastRestartSimulator(...) as sim: ... for symmetry with
other resource-holding APIs (the __exit__ clears the JIT cache
reference, freeing the compiled kernel).
Source code in jaxonomy/simulation/fast_restart.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | |
close()
Drop references to the compiled kernel and base context.
Subsequent :meth:run calls will rebuild and recompile. The
underlying JAX persistent cache (T-017) still holds the compiled
XLA program, so the second build remains fast.
The per-diagram-identity kernel cache used by
:meth:run_with_diagram is also cleared.
Source code in jaxonomy/simulation/fast_restart.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
reset(diagram=None)
Clear the cached compiled kernel; optionally rebind to a new diagram.
When diagram is None (default) this is equivalent to
:meth:close — the next :meth:run rebuilds the simulator and
recompiles the kernel. The JAX persistent cache typically makes
this a fast operation if the diagram structure is unchanged.
When diagram is provided, the simulator rebinds to it. This
is the "swap subsystem variant" path: a parameter sweep where the
structure (block topology, port shapes, parameter pytree
layout) varies between runs. The next :meth:run will perform
a full recompile against the new diagram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram
|
Diagram | None
|
Optional new diagram (or any
:class: |
None
|
Source code in jaxonomy/simulation/fast_restart.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
run(parameters=None, initial_state=None)
Run one simulation, optionally patching parameters / initial state first.
The first call builds the simulator and JIT-compiles the kernel. Subsequent calls reuse the same compiled program; only the parameter and initial-state values change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
dict[str, Any] | None
|
Optional dot-path mapping |
None
|
initial_state
|
Any | None
|
Optional override for the simulator's
continuous state at |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SimulationResults
|
class: |
SimulationResults
|
|
|
SimulationResults
|
|
Source code in jaxonomy/simulation/fast_restart.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 | |
run_batch(parameters_batch, initial_states_batch=None)
Run N simulations differing only by parameters, vmap'd over the cached kernel.
Counterpart to :meth:run for batched parameter sweeps. Equivalent
to :func:simulate_batch with use_vmap=True but reuses the
warm-cached kernel built by the most recent :meth:run call (or
builds it lazily on first use). Calling :meth:run first to warm
the cache and then :meth:run_batch for the sweep is the typical
UX pattern; both code paths share one JIT compile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters_batch
|
dict[str, Any]
|
|
required |
initial_states_batch
|
Any | None
|
Optional batched initial-state override.
Either a single array of shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
'BatchSimulationResults'
|
class: |
'BatchSimulationResults'
|
|
Notes
- The kernel is vmap'd over all leaves of the patched context,
not just the explicitly-batched parameter paths. Unpatched
leaves are broadcast to shape
(N, ...)so the vmap'd kernel sees a uniformly batched pytree. - The cached kernel was JIT'd against a scalar context
signature.
jax.vmaptraces against the batched signature, so the very first call to :meth:run_batchincurs one extra trace (still cheap; XLA caches the inner compiled program). Subsequent :meth:run_batchcalls with the sameNand the same parameter pytree shape reuse the vmap-cached kernel.
Source code in jaxonomy/simulation/fast_restart.py
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 | |
run_with_diagram(diagram, parameters=None, initial_state=None, recorded_signals=None)
Run one simulation against diagram, caching its kernel by identity.
Use this when you hold a pool of structurally-different diagrams (e.g. controller variants) and want to rapidly switch between them. The compiled kernel for each distinct Diagram instance is built on first use and reused thereafter — no recompile on cache hit.
Compared to :meth:reset + :meth:run (which drops and
rebuilds the kernel on every swap), :meth:run_with_diagram
keeps one compiled kernel per Diagram identity alive, so
toggling back and forth between N diagrams in a loop costs N
compiles total, not one per call.
The user's :meth:run and :meth:reset APIs are unaffected;
this is a purely-additive surface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram
|
Diagram
|
Diagram (or any
:class: |
required |
parameters
|
dict[str, Any] | None
|
Optional dot-path mapping |
None
|
initial_state
|
Any | None
|
Optional override for the simulator's
continuous state at |
None
|
recorded_signals
|
dict[str, OutputPort] | None
|
Optional |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SimulationResults
|
class: |
SimulationResults
|
|
|
SimulationResults
|
|
Source code in jaxonomy/simulation/fast_restart.py
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 | |
LazyResults
dataclass
A deferred-evaluation wrapper around a :class:SimulationResults.
Construct via :meth:SimulationResults.lazy rather than directly.
Source code in jaxonomy/simulation/lazy_results.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 | |
align_to(name)
Resample every signal to name's native cadence (defers).
Convenience wrapper over :meth:resample that targets the
per-signal time vector for name. Useful when one signal
is the natural reference clock (e.g. a 1 Hz sensor) and you
want every other recorded signal aligned to its ticks before
materialising.
The returned chain inherits the active backend (eager / polars
/ duckdb) and routes through the same resample translator
— i.e. the polars backend uses the asof-join + linear-interp
plan from T-015a-followup-resample-pushdown.
Raises:
| Type | Description |
|---|---|
KeyError
|
if |
Source code in jaxonomy/simulation/lazy_results.py
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | |
cadence_of(name)
Classify name's recording cadence.
Returns one of:
"continuous"— sampled every major step (time_for(name).shape == self._time.shapeand the value array is full-length)."periodic"— sampled on a fixed schedule (Mode A path: both per-signal times AND outputs are shorter than the global vector and have matching length)."event-driven"— Mode B value-diff dedup populated per-signal times but the output array remained at the global cadence (the recording pipeline could not pin a fixed period to the sourceOutputPort)."default"— no per-signal cadence info available; the signal shares the global :attr:_timevector.
This is a structural classification derived from the recorded-
array shapes — it does not re-invoke the static
ResultsRecorder.classify_signal_cadence (which requires
live OutputPort references that aren't carried on
:class:SimulationResults). The four buckets nevertheless
line up 1-to-1 with the four cadence kinds the recording
pipeline produces (continuous / periodic / event-driven /
default), so a downstream consumer can plan I/O without
reaching back into the simulator.
Raises:
| Type | Description |
|---|---|
KeyError
|
if |
Source code in jaxonomy/simulation/lazy_results.py
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 | |
collect()
Materialise the chain. Returns {"time": t, **signals}.
Source code in jaxonomy/simulation/lazy_results.py
756 757 758 759 760 761 762 763 764 765 | |
explain()
Render the deferred operation chain as a human-readable string.
Source code in jaxonomy/simulation/lazy_results.py
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 | |
from_parquet(path, backend='polars')
classmethod
Load a parquet file written by :meth:to_parquet.
Parameters
path
Path to a parquet file produced by :meth:to_parquet
(or any parquet file with a time column).
backend
"polars" (default; T-015a) returns a :class:LazyResults
with the polars backend pre-enabled.
"duckdb" (T-015a-followup-resample-pushdown-duckdb)
opens the file via DuckDB's read_parquet(...) against a
fresh in-memory connection — the out-of-core entry point
for SQL-style queries. In both cases vector-valued signals
stored as name__i columns are re-collapsed into (T, k)
numpy arrays for compatibility with the eager-numpy fallback
path.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
resample(t_new, *, method='linear')
Interpolate every signal onto t_new (defers).
T-108 phase 2 wires the optional method= kwarg through to
the T-106 backend (:func:jaxonomy.library.lookup_table.interp_1d),
so callers can pick the smoother interpolation rules without
leaving the lazy pipeline:
"linear"(default) — uses the existing fast paths (np.interpeager, native polars asof-join + linear-interp)."pchip"— monotone cubic Hermite; smooth gradients, no overshoot near monotonic data."akima"— Akima 1970 cubic spline; less overshoot than the natural cubic on non-monotone data."cubic"— natural cubic spline (C^2 continuous, second derivative zero at boundaries)."nearest"/"flat"— zero-gradient piecewise constant.
For any non-linear method, the polars / DuckDB lazy paths fall
back to materialising the upstream chain first and then routing
each signal through interp_1d per-channel — non-linear
interpolation is not expressible as a single polars expression.
method="linear" keeps the native-polars / native-DuckDB
pushdown so large lazy plans stay out-of-core.
Polars backend (T-015a-followup-resample-pushdown): for
method="linear" only, translated natively via two
join_asof calls (backward + forward) plus a linear-interp
expression — no Python map_batches callback. Target times
must lie within the source range; non-monotonic t_new is
supported (sorted internally, then re-permuted on output).
Source code in jaxonomy/simulation/lazy_results.py
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 | |
select(*signals)
Project to a subset of signals (defers).
Source code in jaxonomy/simulation/lazy_results.py
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 | |
signal(name)
Return (time, value) for name at its NATIVE cadence.
Eager (non-lazy) accessor: bypasses the deferred op chain and
reads directly from the underlying recorded arrays. Returns
the per-signal timestamp vector populated by T-013 /
T-013a (Mode A or Mode B) when available, else falls back
to the global :attr:_time vector — matching the semantics of
:meth:SimulationResults.time_for.
For Mode B "default"-classified signals (per-signal times are
deduplicated but outputs stays at full length), the value
array is back-projected onto the deduplicated times via
searchsorted so the returned (time, value) pair has
consistent shape — same trick used by
:meth:SimulationResults.align.
Raises:
| Type | Description |
|---|---|
KeyError
|
if |
Source code in jaxonomy/simulation/lazy_results.py
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 | |
to_hdf5(path, key='results', chunk_size=10000)
Stream-write the materialised result to an HDF5 file (T-108-followup-streaming-export).
Layout: a top-level time dataset and an outputs/ group
holding one dataset per signal (vector-valued signals are
exploded into outputs/<name>__<i> to mirror the parquet
column convention). Each dataset is created with
maxshape=(None, ...) and extended chunk-by-chunk so the
file never has to hold the full frame in memory at once.
Parameters
path
Destination .h5 file path. Overwritten if it exists.
key
Currently unused — reserved for forward compatibility with
multi-result HDF5 files; the layout described above is
relative to the file root and not under key.
chunk_size
Rows written per extend. Tune for memory / I/O trade-off;
defaults to 10 000 rows.
Notes
Optional dep: requires h5py (pip install h5py). Raises
:class:ImportError if not available.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
to_numpy()
Alias for :meth:collect.
Source code in jaxonomy/simulation/lazy_results.py
767 768 769 | |
to_pandas()
Materialise to a pandas.DataFrame (requires pandas).
Vector-valued signals are exploded into name__0, name__1 columns.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
to_parquet(path, batch_size=None)
Write the materialised result to path as Parquet.
With the polars backend (T-015a) and batch_size=None, writes
via LazyFrame.sink_parquet for true streaming output that
never materialises the whole frame in memory. With
batch_size=N, partitions the output into multiple files
path.0.parquet / path.1.parquet / ... each holding at
most N rows.
With the DuckDB backend (T-015a-followup-resample-pushdown-duckdb)
and batch_size=None, writes via DuckDB's native
COPY (sql) TO 'path' (FORMAT PARQUET) — genuinely streaming
(DuckDB never materialises the whole result in Python memory).
batch_size=N partitions on the Python side just like polars.
Without an opt-in backend, uses pandas (pyarrow) and falls
back to polars when pandas is unavailable.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
to_polars()
Materialise to a polars.DataFrame (requires polars).
Source code in jaxonomy/simulation/lazy_results.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 | |
to_zarr(path, chunk_size=10000)
Stream-write the materialised result to a zarr store (T-108-followup-streaming-export).
Layout mirrors :meth:to_hdf5: a time array at the group
root and one array per signal under outputs/ (vector-valued
signals exploded as outputs/<name>__<i>). Each array is
created with shape=(0,) and resized in place per chunk.
Parameters
path Destination directory (a zarr v3 store). Created if absent; overwritten otherwise. chunk_size Rows written per extend. Also used as the underlying zarr chunk dimension so I/O alignment matches the write cadence.
Notes
Optional dep: requires zarr (pip install zarr). Raises
:class:ImportError if not available.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
where(mask)
Boolean-mask filter on rows (defers).
mask may be:
- a boolean numpy array of length len(time);
- a callable f(t, outputs) -> bool array;
- a string expression that uses t and any signal name as
free variables (e.g. "t > 5", "x > 0 & t < 1.5").
Source code in jaxonomy/simulation/lazy_results.py
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 | |
with_duckdb_backend(connection=None)
Opt in to the DuckDB SQL execution path (T-015a-followup-...-duckdb).
Parameters
connection
An existing :class:duckdb.DuckDBPyConnection, or None
(default) to allocate a fresh in-memory connection. Pass
an explicit connection to control persistence, extension
loading, or thread count.
Returns a copy of this :class:LazyResults whose terminal
materialisers run a single SQL query against an in-memory
DuckDB table built from the recorded (time, outputs)
arrays. Vector-valued signals are exposed as name__i
columns (matching the polars backend convention).
Per-op fallback: with_signal, callable where predicates,
and resample are not generally SQL-able and emit
:class:RuntimeWarning at materialise time, falling back to
the eager-numpy path for that op (the chain re-enters DuckDB
afterwards). select and where with a string predicate
translate cleanly.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
with_polars_backend()
Opt in to the polars LazyFrame execution path (T-015a).
Returns a copy of this :class:LazyResults whose terminal
materialisers (to_polars/to_pandas/to_parquet/
to_numpy/collect) build a polars.LazyFrame plan
rather than evaluating ops eagerly on numpy arrays.
Falls back to eager-numpy on a per-op basis (with
:class:RuntimeWarning) for ops that polars cannot express
natively — currently only callable where predicates.
resample is native polars (asof-join + linear-interp
expression; T-015a-followup-resample-pushdown).
with_signal is executed via collect-and-re-lazy.
Source code in jaxonomy/simulation/lazy_results.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
with_signal(name, fn)
Derive a new signal name from existing ones (defers).
fn receives (t, outputs) and returns an array shaped
like time.
Source code in jaxonomy/simulation/lazy_results.py
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 | |
ManifestMismatch
Bases: AssertionError
Raised by :func:verify_manifest when two manifests differ.
Inherits from :class:AssertionError so it composes with
pytest and standard assertion-style verification flows
without callers needing to import the exception explicitly.
The exception instance carries a differences attribute holding
the same list[tuple[str, Any, Any]] that
:func:compare_manifests returns, so programmatic consumers can
introspect the drift instead of parsing the message.
Source code in jaxonomy/simulation/provenance.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | |
ODESolverOptions
dataclass
Options for the ODE solver.
See documentation for simulate for details on these options.
Source code in jaxonomy/backend/ode_solver.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
ProvenanceManifest
dataclass
Reproducibility snapshot for one simulate(...) call (T-110).
Phase 1 captures library versions, the resolved precision policy, a
deterministic system fingerprint, and the relevant
:class:SimulatorOptions field values. An ISO-8601 UTC timestamp
is included so the manifest is self-describing; git_head is
populated when simulate is called from inside a git checkout.
The config_hash field (T-110-followup-config-hash) is a
deterministic SHA-256 of the relevant configuration — same options
+ same system + same jaxonomy/jax versions yield the same hash
across runs and across git commits (timestamp and git HEAD are
deliberately excluded).
The dataclass is frozen so a recorded manifest can't be silently mutated downstream.
Source code in jaxonomy/simulation/provenance.py
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 | |
from_dict(data)
classmethod
Construct a :class:ProvenanceManifest from a dict produced by
:meth:to_dict (round-trip helper for serialisation tests).
Source code in jaxonomy/simulation/provenance.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | |
to_dict()
Return a JSON-friendly dict representation of the manifest.
Source code in jaxonomy/simulation/provenance.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
to_json(*, indent=None)
Serialise :meth:to_dict via json.dumps.
Source code in jaxonomy/simulation/provenance.py
681 682 683 | |
ResultsWithProvenance
dataclass
Pair a results object with its :class:ProvenanceManifest.
Attribute access is forwarded to the underlying results
instance, so wrapped.outputs[name] works exactly like
results.outputs[name]. wrapped.results and
wrapped.provenance give explicit access to either side.
The wrapper is frozen so the pairing can't be silently mutated. Construction does not copy or wrap the underlying results — the wrapper holds a reference, nothing else.
Source code in jaxonomy/simulation/provenance.py
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 | |
SimulationError
Bases: JaxonomyError
Raised when a simulator entry point fails at trace or run time.
Attributes:
| Name | Type | Description |
|---|---|---|
cause |
The original exception. Accessible as |
|
block |
Name of the block that appeared innermost in the
traceback, or |
|
port |
Name of the port if the failure was inside a port
callback (best-effort), else |
Source code in jaxonomy/simulation/errors.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
SimulationResults
Bases: NamedTuple
Data structure for the results of a simulation.
Attributes:
| Name | Type | Description |
|---|---|---|
context |
ContextBase
|
The output context of the simulation, containing final states, times, etc.
May be None if |
outputs |
dict[str, Array]
|
A dictionary of the outputs of the simulation, keyed by the name provided
to |
time |
Array
|
The time vector of the simulation. |
parameters |
dict[str, Any]
|
The parameters used in the simulation, used in ensemble simulations to identify different runs. |
Source code in jaxonomy/simulation/types.py
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 | |
align(time_vector, signals=None)
Resample recorded signals onto a common time vector (T-013).
Useful when per-signal timestamps have been captured at different native rates and a rectangular timeline is required for plotting or further processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_vector
|
1-D array of times to sample at. |
required | |
signals
|
Optional iterable of signal names to include. Defaults to all recorded signals. |
None
|
Returns:
| Type | Description |
|---|---|
|
A new :class: |
|
|
signal has been linearly interpolated onto |
|
|
|
|
|
now share the same timeline. |
Source code in jaxonomy/simulation/types.py
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 | |
lazy()
Return a :class:LazyResults wrapper for fluent / deferred queries.
See :mod:jaxonomy.simulation.lazy_results for the full API.
Source code in jaxonomy/simulation/types.py
1063 1064 1065 1066 1067 1068 1069 | |
query(t, signal=None)
Interpolate recorded signal(s) at time t (T-012, T-012a).
Default path uses a linear interpolant over the recorded time/value arrays — fast, consistent across solvers, sufficient for the common post-hoc-sampling workflow.
When the simulation was run with
SimulatorOptions(record_solver_states=True) the
solver_states field is populated and query switches to a
PCHIP cubic-Hermite interpolant built from the same recorded
samples (T-012a partial). PCHIP is shape-preserving — no
overshoot at zero-order-hold plateaus — and gives ~3 orders of
magnitude better accuracy than linear on smooth (continuous)
signals. Discrete (zero-order-hold) signals are detected by
constant-plateau runs and fall back to step interpolation
rather than smoothing through the steps.
The ODE solver's native dense interpolant (Dopri5's 5th-order polynomial, BDF's polynomial predictor) — which would give sub-ULP accuracy — remains a follow-up since it requires plumbing per-major-step solver state through the recording pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Scalar time, or 1-D array of times. |
required | |
signal
|
Optional[str]
|
Optional signal name. If provided, return only that signal's interpolated value. If None, return a dict of all recorded signals. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in jaxonomy/simulation/types.py
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 | |
time_for(signal)
Return the time vector associated with signal.
Falls back to self.time when per_signal_times is None
or does not contain signal — matching the legacy behaviour
where all recorded signals share one timeline.
Source code in jaxonomy/simulation/types.py
771 772 773 774 775 776 777 778 779 780 | |
Simulator
Class for orchestrating simulations of hybrid dynamical systems.
See the simulate function for more details.
Source code in jaxonomy/simulation/simulator.py
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 | |
__init__(system, ode_solver=None, options=None)
Initialize the simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemBase
|
The hybrid dynamical system to simulate. |
required |
ode_solver
|
ODESolverBase
|
The ODE solver to use for integrating the continuous-time component of the system. If not provided, a default solver will be used. |
None
|
options
|
SimulatorOptions
|
Options for the simulation process. See |
None
|
Source code in jaxonomy/simulation/simulator.py
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 | |
compile(tf, context)
Warm up / pre-compile the simulation advance_to method on the device.
Source code in jaxonomy/simulation/simulator.py
1718 1719 1720 1721 | |
initialize(context)
Perform initial setup for the simulation.
Source code in jaxonomy/simulation/simulator.py
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 | |
while_loop(cond_fun, body_fun, val)
Structured control flow primitive for a while loop.
Dispatches to a bounded while loop when
• enable_autodiff=True (required for reverse-mode AD), or
• the caller explicitly set max_major_steps in SimulatorOptions
(acts as a hard simulation budget, e.g. for Zeno protection).
Otherwise the standard unbounded lax.while_loop (JAX backend) or a
pure-Python loop (NumPy backend) is used.
Source code in jaxonomy/simulation/simulator.py
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 | |
SimulatorOptions
dataclass
Options for the hybrid simulator.
See documentation for simulate for details on these options.
This also contains all configuration for the ODE solver as a subset of options
so that multiple options classes don't need to be created separately.
Source code in jaxonomy/simulation/types.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
algebraic_row_mask(system)
Boolean mask: True for rows of M that are identically zero.
Returns None if the system has no mass matrix (purely ODE form).
Rows with M[i, :] == 0 correspond to algebraic constraints in the
semi-explicit form M·ẋ = f.
Source code in jaxonomy/simulation/dae_drift.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
attach_provenance_to_batch(results, system, options)
Attach a :class:ProvenanceManifest to results in place and return it.
Standalone helper for the rare case where a user has a
:class:BatchSimulationResults produced without
record_provenance=True and now wants reproducibility metadata
attached (for example, after-the-fact archival). In the normal flow,
:func:simulate_batch and :func:simulate_distributed already wire
up the manifest when options.record_provenance=True; this helper
is just the explicit, opt-in escape hatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
BatchSimulationResults
|
A :class: |
required |
system
|
Diagram | None
|
The diagram that was simulated (passed through to
:func: |
required |
options
|
SimulatorOptions | None
|
The :class: |
required |
Returns:
| Type | Description |
|---|---|
BatchSimulationResults
|
The same |
BatchSimulationResults
|
populated. |
Source code in jaxonomy/simulation/batch.py
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 | |
bundle_results(results)
Wrap results in a :class:ResultsWithProvenance if applicable.
When results.provenance is populated (a non-None manifest), the
return value is a :class:ResultsWithProvenance carrying both the
original results object and its provenance. When results has
no provenance attribute or that attribute is None, the
original results object is returned unchanged — so callers can
sprinkle bundle_results(...) in front of every simulate call
without breaking byte-equivalent default-off paths.
This helper is purely ergonomic. The legacy
results.provenance field is left in place; nothing about the
underlying results object is mutated.
Source code in jaxonomy/simulation/provenance.py
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | |
compare_manifests(actual, expected, *, ignore_fields=None)
Diff two manifests field-by-field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
ProvenanceManifest
|
the manifest produced by the run being checked. |
required |
expected
|
ProvenanceManifest
|
the reference manifest (e.g. loaded from a published
release-tag artifact via :func: |
required |
ignore_fields
|
Optional[set[str]]
|
top-level field names whose drift is acceptable.
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, Any, Any]]
|
A flat list of |
list[tuple[str, Any, Any]]
|
triples — one per differing leaf. An empty list means the two |
list[tuple[str, Any, Any]]
|
manifests agree on every compared field. |
Source code in jaxonomy/simulation/provenance.py
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 | |
compute_constraint_residual(system, context)
Return the residual of the algebraic constraints at the given context.
For a semi-explicit DAE M·ẋ = f(t, x, p), rows of M that are
zero enforce f_a(t, x, p) = 0. This function returns the
concatenated f_a vector — ideally near zero on a converged solver
step, and any growth over simulation time indicates constraint drift.
Returns None for systems without a mass matrix (no constraints to
satisfy; M is identity).
Source code in jaxonomy/simulation/dae_drift.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
compute_provenance(system, options=None, *, include_git=True, timestamp=None)
Build a :class:ProvenanceManifest for system + options.
All capture happens in plain Python — no JAX tracing — so the function is safe to call before or after a JIT'd simulation kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
Optional['SystemBase']
|
the system being simulated (may be |
required |
options
|
Optional['SimulatorOptions']
|
the active :class: |
None
|
include_git
|
bool
|
when |
True
|
timestamp
|
Optional[str]
|
optional override (ISO-8601 string). Defaults to the current UTC time. Override is useful for deterministic tests. |
None
|
Returns:
| Type | Description |
|---|---|
ProvenanceManifest
|
A populated :class: |
Source code in jaxonomy/simulation/provenance.py
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 | |
constraint_residual_norm(system, context)
Max-abs residual of the algebraic constraints, or None for pure ODE.
||f_a||_∞ is the natural comparison quantity for a drift threshold:
a single violated constraint should trigger the warning even if the
average residual is tiny.
Source code in jaxonomy/simulation/dae_drift.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
estimate_max_major_steps(system, tspan, max_major_step_length=None, safety_factor=2)
Heuristic for estimating the required number of major steps.
This is used to bound the number of iterations in the while loop in the
simulate function when automatic differentiation is enabled. The number
of major steps is determined by the smallest discrete period in the system
and the length of the simulation interval. The number of major steps is
bounded by the length of the simulation interval divided by the smallest
discrete period, with a safety factor applied. The safety factor accounts
for unscheduled major steps that may be triggered by zero-crossing events.
This function assumes static time variables, so cannot be called from within
traced (JAX-transformed) functions. This is typically the case when the
beginning or end time of the simulation is a variable that will be
differentiated. In this case estimate_max_major_steps should be called
statically ahead of time to determine a reasonable bound for max_major_steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemBase
|
The system to simulate. |
required |
tspan
|
tuple[float, float]
|
The time interval to simulate over. |
required |
max_major_step_length
|
float
|
The maximum length of a major step. If provided, this will be used to bound the number of major steps. Otherwise it will be ignored. |
None
|
safety_factor
|
int
|
The safety factor to apply to the number of major steps. Defaults to 2. |
2
|
Source code in jaxonomy/simulation/simulator.py
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 | |
event_time_gradient(guard_fn, ode_rhs_fn, t_event, state_at_event_fn, params, *, eps=1e-30)
Compute ∂t_event/∂params via the implicit-function theorem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guard_fn
|
Callable[[float, Any, Any], ndarray]
|
|
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
t_event
|
ndarray
|
Scalar time at which the guard fires. |
required |
state_at_event_fn
|
Callable[[Any], Any] | Any
|
Either
* a callable |
required |
params
|
Any
|
Parameter PyTree to differentiate with respect to. May be a scalar, ndarray, or any nested container. |
required |
eps
|
float
|
Floor used to clip the denominator
|
1e-30
|
Returns:
| Type | Description |
|---|---|
Any
|
The PyTree of |
Any
|
|
Source code in jaxonomy/simulation/event_gradient.py
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 | |
event_time_jacobian(guard_fn, ode_rhs_fn, t_event, state_at_event_fn, params, *, eps=1e-30)
Vector-valued convenience wrapper of :func:event_time_gradient.
Identical semantics, but returns a flat ndarray so that the result
composes cleanly with downstream linear-algebra (Sobol sampling,
Fisher information, etc.). params should be a 1-D array.
For a 1-D params array of length n_p, returns shape (n_p,).
Source code in jaxonomy/simulation/event_gradient.py
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 | |
event_times_gradient(results, params, guards, ode_rhs_fn, state_at_event_fn, *, event_indices=None, eps=1e-30)
Batch event-time gradients across all firings recorded by
simulate(..., options=SimulatorOptions(record_event_times=True)).
For each recorded event in results.event_times, applies the
implicit-function theorem (T-125 phase 1) to every firing instant
and returns the per-firing gradient PyTrees keyed by event index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Any
|
A :class: |
required |
params
|
Any
|
Parameter PyTree to differentiate with respect to.
Same semantics as :func: |
required |
guards
|
Any
|
Either a single guard callable
|
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
state_at_event_fn
|
Callable[[float, Any], Any]
|
|
required |
event_indices
|
Any
|
Optional iterable of event indices to compute
gradients for. When |
None
|
eps
|
float
|
Forwarded to :func: |
1e-30
|
Returns:
| Type | Description |
|---|---|
dict
|
|
dict
|
the per-firing gradients stacked along a leading axis (so a |
dict
|
gradient that is itself a PyTree leaf of shape |
dict
|
an array of shape |
dict
|
preserved by mapping the stack over leaves). Events that |
dict
|
fired zero times yield an empty leading axis. |
Source code in jaxonomy/simulation/event_gradient.py
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 | |
load_manifest(path)
Load a :class:ProvenanceManifest from a JSON file written by
:meth:ProvenanceManifest.save.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
filesystem path (str or pathlib.Path) of the saved manifest. |
required |
Returns:
| Type | Description |
|---|---|
ProvenanceManifest
|
The reconstructed :class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if |
JSONDecodeError
|
if the file is not valid JSON. |
Source code in jaxonomy/simulation/provenance.py
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | |
multi_event_time_gradient(guard_fn, ode_rhs_fn, reset_map_fn, initial_state, event_times, params, *, t0=0.0, eps=1e-30, rtol=1e-10, atol=1e-12, return_state_sensitivity=False)
Saltation gradient dt_e/dp for every firing along a hybrid
trajectory, propagating the forward sensitivity through reset maps.
Unlike :func:event_time_gradient — which needs the caller to supply a
closed-form state_at_event_fn for the trajectory sensitivity, and so
only gets the first firing right — this helper reconstructs
∂x_e/∂p itself by integrating the variational equation along each
recorded arc and applying the saltation jump at each event. It is the
correct path for multi-bounce / repeated-event problems where each
firing re-initialises the arc from the previous reset map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guard_fn
|
Callable[[float, Any, Any], ndarray] | Any
|
|
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
reset_map_fn
|
Callable[[float, Any, Any], Any] | Any
|
|
required |
initial_state
|
Callable[[Any], Any] | Any
|
either a callable |
required |
event_times
|
Any
|
ordered sequence / array of recorded firing instants
|
required |
params
|
Any
|
parameter PyTree to differentiate with respect to. |
required |
t0
|
float
|
trajectory start time (default |
0.0
|
eps
|
float
|
sign-preserving floor on the implicit-function denominator
|
1e-30
|
rtol
|
float
|
relative tolerance for the augmented (state + sensitivity) arc integration. |
1e-10
|
atol
|
float
|
absolute tolerance for the augmented arc integration. |
1e-12
|
return_state_sensitivity
|
bool
|
when |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
The per-firing |
Any
|
|
Any
|
|
Any
|
carrying a leading firing axis). If |
Any
|
|
Notes
Fully JAX-traceable (the arc integration uses
jax.experimental.ode.odeint). Default-off and purely additive:
the simulator path is untouched and callers who don't import this
helper pay zero cost.
Source code in jaxonomy/simulation/event_gradient.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
scalar_cost_simulate(system, context_fn, t_span, params, cost_fn=None, *, options=None, return_grad=False)
Reverse-mode differentiable scalar cost from a simulation (T-A1).
Resolves the most common autodiff friction in jaxonomy: you cannot
record a trajectory and reduce it to a cost under jax.grad, because
enable_autodiff=True forbids save_time_series=True (recording is
not vmap/AD-safe). The supported pattern is to accumulate the cost
inside the diagram — e.g. add an Integrator whose input is the
running cost L(t, x, u) — and read the final accumulated value off
the context at t_span[1]. This helper packages that pattern so the
canonical cost = f(params) / grad = jax.grad(f)(params) workflow
works out of the box.
It is the reverse-mode counterpart to :func:simulate_jacfwd: use this
for a scalar objective (optimisation / tuning), and simulate_jacfwd
for a Jacobian when n_params is small relative to the output size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemBase
|
the Diagram / LeafSystem to simulate. |
required |
context_fn
|
Callable[[Any], ContextBase]
|
|
required |
t_span
|
tuple[float, float]
|
|
required |
params
|
Any
|
parameter pytree — the differentiation argument. |
required |
cost_fn
|
Callable[[ContextBase], Any]
|
|
None
|
options
|
SimulatorOptions
|
|
None
|
return_grad
|
bool
|
when |
False
|
Returns:
| Type | Description |
|---|---|
|
The scalar cost, or |
Example
accis an Integrator accumulating the running cost inside the diagramdef make_ctx(theta): ... return diagram.with_parameters({"ctrl.kp": theta}).create_context() cost = lambda ctx: ctx[acc.system_id].continuous_state[0] f = lambda th: scalar_cost_simulate(diagram, make_ctx, (0., 5.), th, cost) J = jax.grad(f)(jnp.array(1.0)) # doctest: +SKIP val, grad = scalar_cost_simulate(diagram, make_ctx, (0., 5.), ... jnp.array(1.0), cost, return_grad=True)
Source code in jaxonomy/simulation/simulator.py
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 | |
simulate(system, context, t_span=None, options=None, results_options=None, recorded_signals=None, postprocess=True, flatten=False, *, tspan=None)
Simulate the hybrid dynamical system defined by system.
The parameters and initial state are defined by context. The simulation time
runs from tspan[0] to tspan[1].
The simulation is "hybrid" in the sense that it handles dynamical systems with both discrete and continuous components. The continuous components are integrated using an ODE solver, while discrete components are updated periodically as specified by the individual system components. The continuous and discrete states can also be modified by "zero-crossing" events, which trigger when scalar-valued guard functions cross zero in a specified direction.
The simulation is thus broken into "major" steps, which consist of the following, in order:
(1) Perform any periodic updates to the discrete state. (2) Check if the discrete update triggered any zero-crossing events and handle associated reset maps if necessary. (3) Advance the continuous state using an ODE solver until the next discrete update or zero-crossing, localizing the zero-crossing with a bisection search. (4) Store the results data. (5) If the ODE solver terminated due to a zero-crossing, handle the reset map.
The steps taken by the ODE solver are "minor" steps in this simulation. The
behavior of the ODE solver and the hybrid simulation in general can be controlled
by configuring SimulatorOptions. Available settings are as follows:
The return value is a SimulationResults object, which is a named tuple containing
all recorded signals as well as the final context (if options.return_context is
True). Signals can be recorded by providing a dict of (name, signal_source) pairs
Typically the signal sources will be output ports, but they can actually be any
SystemCallback object in the system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemBase
|
The hybrid dynamical system to simulate. |
required |
context
|
ContextBase
|
The initial state and parameters of the system. |
required |
tspan
|
tuple[float, float]
|
The start and end times of the simulation. |
None
|
options
|
SimulatorOptions
|
Options for the simulation process and ODE solver. |
None
|
results_options
|
ResultsOptions
|
Options related to how the outputs are stored, interpolated, and returned. |
None
|
recorded_signals
|
dict[str, OutputPort]
|
Dictionary of ports for which the time series should be recorded. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
SimulationResults |
SimulationResults
|
A named tuple containing the recorded signals and the final
context (if |
Notes
If recorded_signals is provided as a kwarg, it will override any entry in
options.recorded_signals. This will be deprecated in the future in favor of
only passing via options.
This function is meant to best handle single independent simulations. Calling this function repeatedly will always trigger a recompilation of the model when using the JAX backend. To avoid this, call advance_to directly.
Source code in jaxonomy/simulation/simulator.py
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 | |
simulate_batch(diagram, t_span, param_batches, options=None, recorded_signals=None, results_options=None, use_vmap=False, _force_loop=False, lazy=False)
Run N simulations differing only by parameters given in param_batches.
Execution paths:
-
Kernel path (default for pure-JAX diagrams): builds the simulator once, compiles a single JIT kernel, and injects each batch element's parameters directly into a context pytree (no
ParameterCachemutations). This eliminates N−1 recompilations and is substantially faster for moderate to large N. -
vmap path (opt-in,
use_vmap=True, pure-JAX only): further vectorises over the batch dimension withjax.vmapso all N simulations run as a single XLA call. Requires that all parameter values have compatible shapes and that the simulation fits in device memory N-fold.
CPU note (updated by T-019-followup, 2026-07-10). The
post-vmap finalize is now fully vectorised (batched trim +
batched binary-search linear resampling instead of a per-row
host loop), which removed the old CPU penalty: on the CPU
damped-oscillator sweep at N=1000 the vmap path improved
from ~1.28 s to ~0.41 s against ~0.33 s for the kernel path
(naive loop ~130 s, FastRestart ~0.30 s). CPU kernel-path wins
are now marginal; on GPU / TPU vmap wins decisively. The old
CPU+small-batch UserWarning was removed along with the
penalty it warned about.
- Loop path (forced when
CustomPythonBlockor FMU blocks are present, or when_force_loop=True): the safe fallback — N independent calls tosimulate+with_parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram
|
Diagram
|
Template diagram (unchanged). |
required |
t_span
|
tuple[float, float]
|
|
required |
param_batches
|
dict[str, Any]
|
Dot-path keys mapping to 1-D arrays of length |
required |
options
|
SimulatorOptions | None
|
:class: |
None
|
recorded_signals
|
dict[str, OutputPort] | None
|
Same convention as :func: |
None
|
results_options
|
ResultsOptions | None
|
Optional :class: |
None
|
use_vmap
|
bool
|
If |
False
|
_force_loop
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
BatchSimulationResults
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
Inconsistent batch sizes, missing options, or invalid backend. |
TypeError
|
|
Source code in jaxonomy/simulation/batch.py
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 | |
simulate_cloud(*args, **kwargs)
Run a batch of simulations on a remote execution backend.
Not available in this build: no cloud execution backend is bundled.
Use the local :func:jaxonomy.simulate / batch / distributed runners
instead. This entry point is reserved and will be implemented, and
documented, once the backend ships.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
always, in this build. |
Source code in jaxonomy/simulation/cloud_runner.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
simulate_jacfwd(system, context_fn, t_span, params, output_fn=None, *, options=None, record_provenance=False)
Forward-mode Jacobian of a simulation w.r.t. parameters (T-100).
Wraps jax.jacfwd over a parametrised simulation. Use this when the
parameter count is small compared to the output count
(n_params < n_outputs / 5 is a useful heuristic) — forward-mode
AD scales linearly with input dim; reverse-mode (jax.grad /
jax.jacrev) scales with output dim.
The implementation uses enable_autodiff=False to bypass the
custom-VJP simulate defines for reverse-mode (custom_vjp blocks
forward-mode trace with a clear TypeError); the underlying
simulator's natural JAX trace carries the tangent. Forward-mode
plumbing is already exercised internally by linearize, the BDF
Jacobian solve, and the Kalman/EKF blocks — this function exposes
that plumbing as a stable public surface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemBase
|
a Diagram or LeafSystem to simulate. |
required |
context_fn
|
Callable[..., ContextBase]
|
a callable |
required |
t_span
|
tuple[float, float]
|
|
required |
params
|
Any
|
parameter pytree (the differentiation argument). |
required |
output_fn
|
Callable[[Any], Any]
|
callable applied to the final |
None
|
options
|
SimulatorOptions
|
|
None
|
record_provenance
|
bool
|
when |
False
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
|
|
|
|
Example
def make_ctx(a): ... ctx = sys.create_context() ... ctx.parameters['a'] = a ... return ctx J = simulate_jacfwd(sys, make_ctx, (0., 2.), jnp.array(1.5)) J, m = simulate_jacfwd(sys, make_ctx, (0., 2.), jnp.array(1.5), ... record_provenance=True)
Source code in jaxonomy/simulation/simulator.py
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 | |
simulate_static_sweep(diagram_factory, t_span, static_param_grid, options, recorded_signals_factory, results_options=None, mode='zip')
Sweep over static parameters by rebuilding the diagram per element.
Unlike :func:simulate_batch, which patches a single diagram's context with
different dynamic parameter values, this helper accepts a factory that
produces a fresh :class:Diagram for each combination of static param
values. Each element is simulated independently in a Python loop; outputs
are stacked into a :class:BatchSimulationResults-shaped struct.
Because each diagram is fresh, port references are also per-diagram;
recorded_signals_factory is invoked with the freshly-built diagram and
must return the same kind of {name: OutputPort} dict that
:func:simulate accepts.
No vmap or shared JIT cache: static parameters change the diagram's
structure (e.g. state-space dimensions of a :class:TransferFunction) and
cannot compose with jax.vmap by definition. Each element pays a JIT
compilation cost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram_factory
|
Callable[..., Diagram]
|
Callable taking the static-param keyword arguments
specified by |
required |
t_span
|
tuple[float, float]
|
|
required |
static_param_grid
|
dict[str, Sequence[Any]]
|
Mapping parameter name -> sequence of values. Every
list must have the same length under |
required |
options
|
SimulatorOptions
|
:class: |
required |
recorded_signals_factory
|
Callable[[Diagram], dict]
|
Callable |
required |
results_options
|
ResultsOptions | None
|
Optional :class: |
None
|
mode
|
str
|
|
'zip'
|
Returns:
| Type | Description |
|---|---|
BatchSimulationResults
|
class: |
BatchSimulationResults
|
and |
BatchSimulationResults
|
and |
BatchSimulationResults
|
linearly interpolated onto this grid). The |
BatchSimulationResults
|
attached to the returned object as a list of per-element final |
BatchSimulationResults
|
contexts. |
Raises:
| Type | Description |
|---|---|
ValueError
|
empty grid, mismatched zip lengths, unknown mode, or missing required options. |
TypeError
|
|
Source code in jaxonomy/simulation/static_sweep.py
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 | |
simulate_variant_sweep(diagram, t_span, *, param_batches=None, options=None, recorded_signals=None, results_options=None, use_vmap=False)
Sweep every variant configuration of diagram; for each, optionally
sweep a parameter batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram
|
Diagram
|
A built :class: |
required |
t_span
|
tuple[float, float]
|
|
required |
param_batches
|
dict[str, Any] | None
|
Optional dot-path → |
None
|
options
|
SimulatorOptions | None
|
:class: |
None
|
recorded_signals
|
Callable[[Diagram], dict[str, OutputPort]] | dict[str, OutputPort] | None
|
Either a static |
None
|
results_options
|
ResultsOptions | None
|
Forwarded to :func: |
None
|
use_vmap
|
bool
|
Forwarded to :func: |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict[tuple[tuple[str, Any], ...], BatchSimulationResults | SimulationResults]
|
Dict keyed by the variant configuration (a sorted tuple of |
|
dict[tuple[tuple[str, Any], ...], BatchSimulationResults | SimulationResults]
|
|
|
dict[tuple[tuple[str, Any], ...], BatchSimulationResults | SimulationResults]
|
class: |
|
or |
dict[tuple[tuple[str, Any], ...], BatchSimulationResults | SimulationResults]
|
class: |
Example
.. code-block:: python
results = simulate_variant_sweep(
diagram,
t_span=(0.0, 1.0),
param_batches={"plant.gain": jnp.linspace(0.5, 2.0, 8)},
recorded_signals=lambda diag: {
"y": diag["plant"].output_ports[0],
},
options=opts,
)
for cfg, batch_results in results.items():
print(dict(cfg), batch_results.outputs["y"].shape)
Notes
Each variant configuration triggers an independent JIT compile of
the simulator. For a sweep over V variants and N parameter
batches the cost is V compiles + V * N simulations (with
the parameter axis vectorised inside each variant). Variant-axis
vmap is genuinely not possible because the pytree shape is not
stable across configurations — see the module docstring.
Source code in jaxonomy/simulation/simulate_variants.py
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 | |
simulate_with_event_time_grad(diagram, ctx, t_span, params, event_index, guard_fn, ode_rhs_fn, state_at_event_fn, options=None, *, sim_runner=None, eps=1e-30)
Differentiable wrapper around simulate for event-time gradients.
Returns the scalar firing time t_event of the FIRST recorded
firing of event_index and registers a jax.custom_vjp rule
that uses the implicit-function theorem (T-125 phase 1) for the
reverse-mode gradient. As a consequence::
jax.grad(simulate_with_event_time_grad)(diagram, ctx, t_span,
params, event_index,
guard_fn, ode_rhs_fn,
state_at_event_fn)
yields ∂t_event/∂params without the caller having to invoke
:func:event_time_gradient manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagram
|
SystemBase passed straight to :func: |
required | |
ctx
|
ContextBase passed straight to :func: |
required | |
t_span
|
|
required | |
params
|
Parameter PyTree to differentiate with respect to. Same
semantics as :func: |
required | |
event_index
|
int
|
Integer event slot whose firing time is returned. |
required |
guard_fn
|
Callable[[float, Any, Any], ndarray]
|
|
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
state_at_event_fn
|
Callable[[Any], Any] | Any
|
Either
* |
required |
options
|
Optional :class: |
None
|
|
sim_runner
|
Callable[..., float] | None
|
|
None
|
eps
|
float
|
Floor for the implicit-function denominator (forwarded to
:func: |
1e-30
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Scalar |
Notes
Composes with jax.jit and jax.vmap: the forward pass
runs as a jax.pure_callback (black-box w.r.t. JAX), and the
backward pass uses :func:event_time_gradient which is itself
JAX-traceable. Default-off byte-equivalence is preserved — the
existing :func:event_time_gradient and :func:simulate are
not touched by this wrapper.
Source code in jaxonomy/simulation/event_gradient.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 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 | |
verify_manifest(actual, expected, *, ignore_fields=None)
Assert that actual matches expected field-by-field.
Convenience wrapper around :func:compare_manifests that raises
:class:ManifestMismatch (an :class:AssertionError subclass) if
any field drifted. The exception message lists every differing
field on its own line; the .differences attribute carries the
same data structurally for programmatic introspection.
Composes naturally with pytest (ManifestMismatch is an
AssertionError, so test runners will treat it like any other
assertion failure).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
ProvenanceManifest
|
the manifest produced by the run being checked. |
required |
expected
|
ProvenanceManifest
|
the reference manifest. |
required |
ignore_fields
|
Optional[set[str]]
|
see :func: |
None
|
Raises:
| Type | Description |
|---|---|
ManifestMismatch
|
if any compared field differs. |
Source code in jaxonomy/simulation/provenance.py
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 | |
vmap_event_time_gradient(guard_fn, ode_rhs_fn, t_event_array, state_at_event_fn, params_batch, *, eps=1e-30, use_python_loop=False)
Vectorised event-time gradient over a batch of parameter samples.
For N samples, computes ∂t_event/∂params for each in turn and
stacks the results along the leading axis — the same shape contract
Monte-Carlo / Sobol workflows expect from :func:simulate_batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guard_fn
|
Callable[[float, Any, Any], ndarray]
|
|
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
t_event_array
|
ndarray
|
|
required |
state_at_event_fn
|
Callable[[Any, Any], Any]
|
|
required |
params_batch
|
Any
|
Batched parameter PyTree. All leaves must share
a leading axis of length |
required |
eps
|
float
|
Forwarded to :func: |
1e-30
|
use_python_loop
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
Per-sample gradients with the same leading axis as |
Any
|
|
Any
|
matches the single-sample :func: |
Notes
Default-off: the wrapper is purely additive and does not modify
the simulator path. Composes cleanly with jax.jit and
downstream jax.grad of a scalar cost over the batch axis.
Source code in jaxonomy/simulation/event_gradient.py
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 | |
vmap_event_times_gradient(results, params_batch, guards, ode_rhs_fn, state_at_event_fn, *, event_indices=None, eps=1e-30, use_python_loop=False)
Cross-product of multi-event + batched-parameter event-time gradient.
For each event index recorded in results.event_times, computes the
implicit-function-theorem gradient ∂t_event/∂params at every
(sample, firing) pair and returns the result keyed by event index.
Output contract::
{event_index: gradient_batch}
where gradient_batch has leading axes (N, n_firings, ...) for
array-valued params_batch leaves and is itself a PyTree mirroring
the structure of params_batch for nested batches. N is the
sample-axis length (shared across all batch leaves); n_firings is
the per-event firing count read from results.event_times[idx].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Any
|
A :class: |
required |
params_batch
|
Any
|
Batched parameter PyTree. All leaves must share a
leading axis of length |
required |
guards
|
Any
|
Either a single guard callable |
required |
ode_rhs_fn
|
Callable[[float, Any, Any], Any]
|
|
required |
state_at_event_fn
|
Callable[[float, Any], Any]
|
|
required |
event_indices
|
Any
|
Optional iterable of event indices to compute
gradients for. When |
None
|
eps
|
float
|
Forwarded to :func: |
1e-30
|
use_python_loop
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
dict
|
event index. For each entry, leaves carry leading axes |
dict
|
|
dict
|
correct |
Notes
Default-off: purely additive. Composes with jax.jit. The
firing times read from results.event_times are treated as
constants w.r.t. params_batch (the implicit-function theorem
is applied at the recorded instants — same convention as
:func:vmap_event_time_gradient and
:func:simulate_with_event_time_grad).
Source code in jaxonomy/simulation/event_gradient.py
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 | |