Symbolic Lowering & Calculus (mkpp.lowering)
mkpp.lowering
SparsityOptimizer
Analyzes Jacobian sparsity for fill-in prediction, reordering, and block detection.
Source code in src/mkpp/lowering.py
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 | |
analyze()
Full sparsity analysis pipeline.
Source code in src/mkpp/lowering.py
compute_rcm_ordering()
Reverse Cuthill-McKee on the symmetrized structure graph. Returns permutation vector p where new_index = p[old_index].
Source code in src/mkpp/lowering.py
detect_blocks()
Run Tarjan SCC on the directed Jacobian structure graph. Each SCC with no cross-block edges becomes an independent block.
Source code in src/mkpp/lowering.py
predict_fill_in()
Graph-reachability fill-in prediction (symbolic Gaussian elimination). For Doolittle LU, position (i,j) fills in if there exists k < min(i,j) such that both (i,k) and (k,j) are structurally non-zero (transitively).
Source code in src/mkpp/lowering.py
annotate_lu_expressions(lu_plan)
Annotate each LU expression with the set of species indices it depends on.
For each expression in lu_expressions_ordered, determines which species indices affect that entry. An expression at (row, col) directly depends on species row and col. Additionally, any W_i_j, L_i_j, or U_i_j references in the expression add species i and j to the dependency set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lu_plan
|
SymbolicLUPlan
|
The symbolic LU plan with lu_expressions_ordered populated. |
required |
Returns:
| Type | Description |
|---|---|
list[AnnotatedLUExpression]
|
List of AnnotatedLUExpression with depends_on sets populated. |
Source code in src/mkpp/lowering.py
apply_cse_to_plan(lu_plan, f_vector)
Apply sympy.cse() to all expressions in the LU plan + rate vector.
Returns:
| Name | Type | Description |
|---|---|---|
replacements |
list
|
List of (Symbol, expression) tuples for CSE temporaries |
reduced |
list
|
List of simplified expressions with CSE symbols substituted |
Source code in src/mkpp/lowering.py
build_sympy_matrices(mech)
Lowering function to compute unified Jacobian and symbolic sparse LU plan. Attaches results to mechanism metadata.
Source code in src/mkpp/lowering.py
compute_symbolic_lu_decomposition(J_matrix, species_map, permutation=None, blocks=None, is_block_diagonal=False)
Computes build-time symbolic sparse LU factorization schedule (Doolittle method) on W = inv_g_dt * I - J. Extracts flat scalar L, U matrix entry expressions and forward/backward substitution steps referencing previously computed scalar variables.
When is_block_diagonal=True and blocks is provided, compute per-block LU plans independently and combine results into a single SymbolicLUPlan with block metadata.
When permutation is provided (but not block-diagonal), apply the permutation to J before computing LU and store the permutation in the resulting plan.
Source code in src/mkpp/lowering.py
compute_transposed_lu_plan(lu_plan)
Given an existing SymbolicLUPlan, compute the transposed substitution steps.
W * x = b => L * U * x = b
- Forward sub: L * y = b (lower-triangular)
- Backward sub: U * x = y (upper-triangular)
W^T * x = b => U^T * L^T * x = b
- Forward sub with U^T: U^T * y = b (U^T is lower-triangular)
- Backward sub with L^T: L^T * x = y (L^T is upper-triangular)
The transposed steps are stored in
- lu_plan.transpose_forward_sub_steps
- lu_plan.transpose_backward_sub_steps
Requirements: 5.1, 5.2
Source code in src/mkpp/lowering.py
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 | |
partition_reactions(mech)
Partition reactions into implicit (stiff) and explicit (non-stiff) deterministic blocks using Tarjan's Strongly Connected Components (SCC) algorithm.
Source code in src/mkpp/lowering.py
prepare_adjoint_and_tlm(mech)
T015: Symbolic lowering hooks for analytical Jacobian, Adjoint, and Tangent-Linear models. For the MVP, this validates that the mechanism is differentiable.
Source code in src/mkpp/lowering.py
prepare_unified_jacobian_parallel(mech)
Same as prepare_unified_jacobian but with parallel Jacobian column computation.
Uses multiprocessing.Pool to compute each column df/dC_j independently, then assembles the full Jacobian in deterministic column order. Falls back to sequential computation if multiprocessing raises an error.
Source code in src/mkpp/lowering.py
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 | |