Extract shared VirtualFactorCore from virtual matrices#313
Merged
Conversation
avoid deep copy if the SolveCaches
VirtualPTDF, VirtualMODF, and VirtualLODF each rebuilt and stored the same DC factorization (K/BA/A/ABA) and topology, duplicated the Woodbury outer wrappers, and recomputed the same PTDF_A_diag / branch_susceptances_by_arc. Introduce a private, unexported VirtualFactorCore that owns the factorization, topology, solve scratch, solver lock, and the lazily-computed shared PTDF_A_diag / branch_susceptances_by_arc. The three virtual matrices become thin wrappers holding a `core` and forwarding to it via accessors and a `getproperty` shim, so all existing public APIs (getindex, accessors, get_post_modification_ptdf_row, ...) keep working unchanged. A single core can now be shared across wrappers so the ABA matrix is factorized once: vptdf = VirtualPTDF(Ybus(sys)) vmodf = VirtualMODF(vptdf, sys) vlodf = VirtualLODF(vptdf) - New src/virtual_factor_helpers.jl relocates _create_factorization, _solve_factorization, _extract_arc_susceptances, _extract_branch_susceptances_by_arc, _get_PTDF_A_diag so they precede the core. - New src/virtual_factor_core.jl defines the struct, accessors, lazy getters, and the Ybus constructor. - Woodbury outer wrappers collapse to one method on the core; the wrappers forward to it. - dist_slack stays a PTDF/LODF wrapper concern (unused by MODF), never on core. - Added test/test_virtual_shared_core.jl asserting one shared factorization and numerical equivalence vs independently built objects.
Contributor
Performance ResultsPrecompile Time
Execution Time
|
Confine getfield to the field-getter definitions and the getproperty hooks. Introduce get_core (and get_cache/get_dist_slack/... companions) on VirtualPTDF/VirtualMODF/VirtualLODF and read wrapper fields through these accessors everywhere else, including the shared-core tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
VirtualPTDF,VirtualMODF, andVirtualLODFeach independently built and stored the same DC factorization (K/BA/A/ABA) and topology. PTDF and MODF additionally computed post-contingency PTDF rows through the same Woodbury kernel via two byte-for-byte duplicate outer wrappers, and LODF/MODF both recomputed the identicalPTDF_A_diag(one solve per arc) andbranch_susceptances_by_arc. Needing all three meant factorizing three times and reconciling their topology/reductions.What this does
Introduces a private, unexported
VirtualFactorCorethat owns the factorization, topology, solve scratch, solver lock, and the lazily-computed sharedPTDF_A_diag/branch_susceptances_by_arc. The three virtual matrices become thin wrappers that hold acoreand forward to it via accessors and agetpropertyshim — so all existing public APIs (getindex, accessors,get_post_modification_ptdf_row, …) keep working unchanged, with no deprecations.A single core can now be shared so the ABA matrix is factorized once:
This gives PowerSimulations a single object to build once and prevents reconciliation drift between the matrices.
Changes
src/virtual_factor_core.jl: theVirtualFactorCore{Ax,L,K}struct, accessors, lazyget_PTDF_A_diag/get_branch_susceptances_by_arc, and theYbusconstructor.src/virtual_factor_helpers.jl: relocates_create_factorization,_solve_factorization,_extract_arc_susceptances,_extract_branch_susceptances_by_arc,_get_PTDF_A_diagso they precede the core in the load order.virtual_ptdf_calculations.jl,virtual_modf_calculations.jl,virtual_lodf_calculations.jl: rewritten as wrappers overcore.VirtualLODFkeeps its own arc×arc axes/lookup and gains aCtype parameter.woodbury_kernel.jl: the duplicated outer wrappers collapse to one method onVirtualFactorCore; the wrappers forward to it.VirtualMODF(vptdf, sys),VirtualLODF(vptdf),VirtualPTDF(core),VirtualMODF(core, sys).dist_slackstays a PTDF/LODF wrapper concern (unused by MODF), never on the core.Notes
DC_vPTDF_Matrixalias is preserved (the wrapper keeps{Ax,L,K}).test/test_virtual_shared_core.jlasserts a single shared factorization and numerical equivalence vs independently built objects. One white-box test that readgetfield(vmodf, :PTDF_A_diag)was updated to read off the core.https://claude.ai/code/session_0195gQBv55bfXAmbUA3wmKQL
Generated by Claude Code