Reactions¶
reactions
¶
Compile op_system reaction artifacts into a flat stochastic network.
op_system publishes one typed :class:ReactionArtifact per named
transition (CompiledRhs.reactions). :func:compile_reaction_network
expands each artifact into one channel per source cell and builds the
stoichiometry, reactant orders, and propensity callback that
:class:~op_engine.DirectSSASolver, :class:~op_engine.TauLeapingSolver,
and :class:~op_engine.AdaptiveTauLeapingSolver consume.
:func:from_compiled_rhs does the same from a compiled op_system RHS.
The module reads artifacts structurally and imports neither op_system
nor flepimop2.
CompiledReactionNetwork(stoichiometry, reactant_stoichiometry, channel_reactions, channel_names, _blocks, _reactions, _event_shapes, _params, reactants_complete=False, incomplete_reactions=(), dependency_incidence=None, propensity_orders=None)
dataclass
¶
Flat stochastic network compiled from typed reaction artifacts.
channel_reactions retains the parent reaction name for every expanded
source-cell channel. channel_names adds integer coordinates and is
intended for diagnostics only.
n_channels
property
¶
Return the number of expanded reaction channels.
n_state
property
¶
Return the number of flattened state cells.
mean_drift(time, state)
¶
Return deterministic mean drift from the compiled channels.
Returns:
| Type | Description |
|---|---|
Array
|
|
Source code in src/op_engine/reactions.py
179 180 181 182 183 184 185 186 187 188 189 190 191 | |
propensity(time, state)
¶
Evaluate every reaction and concatenate its source-cell rates.
The stochastic core stores the state as (n_state, 1). A flat
state is accepted as well so the same callback can construct hybrid
mean drift. Returned shape mirrors the input convention.
Returns:
| Type | Description |
|---|---|
Array
|
Propensities in the evolving state's array namespace. |
Source code in src/op_engine/reactions.py
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 | |
CompiledRhsLike
¶
Bases: Protocol
Structural surface of op_system.CompiledRhs read here.
ReactionArtifact
¶
Bases: Protocol
Structural surface published by op_system.CompiledReaction.
ReactionReactantArtifact
¶
Bases: Protocol
Structural molecular-reactant surface published by op_system.
compile_reaction_network(reactions, *, template_shapes, axis_sizes, params, n_state=None, reaction_names=None)
¶
Compile op_system reaction artifacts into flat channels.
Each source-cell propensity becomes one channel. Consequently, a collapsed axis is represented by distinct columns that share one destination row; summing simultaneous firings is then exactly the stoichiometric matrix multiplication performed by the core stochastic solvers. An offset axis moves each channel to its shifted coordinate; when that leaves the axis, the column only removes the donor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactions
|
Sequence[ReactionArtifact]
|
Typed reaction artifacts, such as |
required |
template_shapes
|
Mapping[str, Sequence[int]]
|
Shape of each state template, in the flat state's
order, such as |
required |
axis_sizes
|
Mapping[str, int]
|
Number of coordinates on each axis. |
required |
params
|
Mapping[str, object]
|
Parameter values passed to every propensity. |
required |
n_state
|
int | None
|
Number of cells in the flat state. Defaults to the total
that |
None
|
reaction_names
|
Sequence[str] | None
|
Optional parent reaction names to select for a hybrid
jump partition. |
None
|
Returns:
| Type | Description |
|---|---|
CompiledReactionNetwork
|
Validated flat reaction network. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an argument or artifact field has the wrong type. |
ValueError
|
If the artifacts are inconsistent with each other or with the state layout. |
Source code in src/op_engine/reactions.py
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 | |
compiled_rhs_axis_sizes(compiled)
¶
Return each axis's coordinate count from a compiled RHS's metadata.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Mapping from axis name to size, from |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an axis entry has no name. |
Source code in src/op_engine/reactions.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | |
from_compiled_rhs(compiled, params, *, reaction_names=None, allow_gaps=False)
¶
Compile the reaction network of a compiled op_system RHS.
The flat state follows compiled.template_shapes: each template's
cells in C order, templates in declaration order. That is also the
layout of compiled.eval_fn's state vector when the RHS is
vectorized, so network.mean_drift(t, y) should equal
compiled.eval_fn(t, y, **params) for every y. Check that
identity once for a new model: a mismatch means some dynamics have no
reaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiled
|
CompiledRhsLike
|
A compiled RHS, such as |
required |
params
|
Mapping[str, object]
|
Parameter values passed to every propensity. |
required |
reaction_names
|
Sequence[str] | None
|
Optional reaction names to compile, for a hybrid
jump partition. |
None
|
allow_gaps
|
bool
|
Compile even when |
False
|
Returns:
| Type | Description |
|---|---|
CompiledReactionNetwork
|
Validated flat reaction network. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the RHS has no vectorized state layout. |
ValueError
|
If the RHS has reaction gaps and neither |
Source code in src/op_engine/reactions.py
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 | |