refactor: rework codebase#84
Open
BenjPetr wants to merge 4 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR performs a broad refactor of the DC+ Python codebase around network-data creation and solver test infrastructure, while adding new NumPy/JAX utilities and expanding test coverage for contingency-style updates.
Changes:
- Refactors network import/preprocess plumbing to use
NetworkInformationobjects and updated Jacobian/mismatch helpers. - Adds new NumPy utilities (fixed-Jacobian iterations, quasi-Newton updates, injection-outage one-step updates, injection aggregation/helpers) plus corresponding docs/tests.
- Restructures JAX kernels to accept shared pytree “inputs” objects and updates BSDF/LODF tests accordingly.
Reviewed changes
Copilot reviewed 54 out of 55 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_helper/injection_state_helper.py | Adds helper to rebuild dynamic network state with updated injection powers for tests. |
| tests/test_helper/bsdf_helper.py | Refactors BSDF test setup to be ID-driven and supports shunt/injection reassignment. |
| tests/preprocess/test_preprocessing.py | Updates tests to new NetworkInformation/Jacobian APIs. |
| tests/preprocess/test_preprocess_jacobian_bsdf.py | Updates BSDF preprocessing test for new fields (e.g., voltage setpoints). |
| tests/numpy/test_quasi_newton.py | Adds quasi-Newton tests vs full AC and N-1 comparisons. |
| tests/numpy/test_lodf.py | Extends/reshapes LODF tests and adds one-step parity helpers. |
| tests/numpy/test_injection_outage.py | Adds batched injection-outage one-step update tests. |
| tests/numpy/test_fixed_jacobian.py | Adds fixed-Jacobian iteration tests and phase tap scenarios. |
| tests/numpy/test_bsdf.py | Updates BSDF full-rank tests and adds additional BSDF scenarios. |
| tests/jax/test_lodf_jax.py | Refactors JAX LODF tests to new pytree inputs and adds parity test vs NumPy path. |
| tests/jax/test_bsdf_jax.py | Refactors JAX BSDF test to compare JAX vs NumPy update kernels. |
| tests/interfaces/test_network_info_serialization.py | Adds serialization round-trip tests for NetworkInformation components. |
| tests/interfaces/test_jacobian_network_data.py | Updates Jacobian tests to new Jacobian/mismatch APIs. |
| tests/importing/test_powsybl_vs_pandapower_dynamic.py | Updates importer comparisons to new NetworkInformation and renamed fields. |
| tests/importing/test_powerflow_consistency.py | Refactors pandapower consistency tests to NetworkInformation. |
| tests/importing/test_native_import_consistency.py | Refactors pandapower native import test to NetworkInformation. |
| tests/importing/test_import_helpers.py | Expands import-helper tests for new schema columns and reindexing behavior. |
| tests/importing/test_compare_imports.py | Updates import comparator to new APIs and renamed dynamic fields. |
| tests/importing/powsybl/test_powsybl_import.py | Adds tap-changer import tests and updates bus reference selection behavior. |
| tests/conftest.py | Adds shared fixtures for transformer replacement and IEEE14 injection-update setup. |
| src/dc_plus/preprocess/preprocess_jacobian_bsdf.py | Extends BSDF preprocessing to carry angle-reference/setpoint fields and new Jacobian layout indices. |
| src/dc_plus/preprocess/helper_functions.py | Improves documentation of time-dimension flattening helper. |
| src/dc_plus/numpy/quasi_newton.py | Adds quasi-Newton solver with lazy inverse-Broyden corrections. |
| src/dc_plus/numpy/low_rank_helper.py | Extends low-rank helper APIs (reactive mismatch masking) and dtype handling. |
| src/dc_plus/numpy/lodf.py | Cleans up array handling in inverse update implementation. |
| src/dc_plus/numpy/injection_outage.py | Adds batched injection-outage mismatch assembly and one-step dx computation. |
| src/dc_plus/numpy/injection_helpers.py | Adds injection aggregation helper used by injection-change paths. |
| src/dc_plus/numpy/injection_changes.py | Adds with_updated_injection_powers to recompute bus injections after power changes. |
| src/dc_plus/numpy/fixed_jacobian.py | Adds fixed-Jacobian iteration runner. |
| src/dc_plus/numpy/init.py | Exposes new NumPy public helpers. |
| src/dc_plus/jax/network_state_helper.py | Adds helper to compute branch complex powers without dense Y. |
| src/dc_plus/jax/low_rank_helper.py | Refactors low-rank helper to consume pytree input structs. |
| src/dc_plus/jax/lodf_voltages.py | Refactors LODF voltage solver to accept pytree inputs and reactive-mismatch masks. |
| src/dc_plus/jax/lodf_branches.py | Refactors monitored-branch kernels to pytree inputs and shared power helper. |
| src/dc_plus/jax/bsdf.py | Refactors BSDF update kernel API to pytree inputs and adds shunt handling in the delta block. |
| src/dc_plus/interfaces/network_inputs.py | Introduces shared JAX pytree “inputs” dataclasses for topology/state/admittance/Jacobian mappings. |
| src/dc_plus/interfaces/jacobian_network_data.py | Renames/publicizes Jacobian constructors; adds Jacobian-layout aware mismatch and dx application. |
| src/dc_plus/interfaces/jacobian_interface.py | Renames bus-index fields and adds utilities for Jacobian layouts and copying with a new inverse. |
| src/dc_plus/importing/powsybl/powsybl_network_helpers.py | Changes _load_test_grid to return NetworkInformation and adds reactive-limit options. |
| src/dc_plus/importing/powsybl/powsybl_loadflow_parameter.py | Updates loadflow parameter construction to new pypowsybl enums/parameter names. |
| src/dc_plus/importing/powsybl/powsybl_import_helpers.py | Updates slack selection loadflow configuration to new pypowsybl enums/parameter names. |
| src/dc_plus/importing/pandapower/pandapower_import.py | Updates schema return types, adds angle-reference flag, and introduces voltage_setpoint field. |
| src/dc_plus/importing/import_schema.py | Extends import schemas (angle reference, voltage setpoint, tap changer/positions). |
| src/dc_plus/importing/import_helpers.py | Adds main-grid filtering/reindexing and refactors admittance computation helpers. |
| src/dc_plus/example_grids/pypowsbl/example_grids.py | Adds curated “short list” network set for test parameterization. |
| pyproject.toml | Updates dependency constraints and adds devgpu/doc adjustments. |
| docs/dc_plus/injection_outage.md | Adds documentation for the injection-outage one-step update API. |
| .vscode/settings.json | Updates VS Code pytest args to run tests in parallel (-n auto). |
Comments suppressed due to low confidence (1)
src/dc_plus/importing/import_helpers.py:333
- The return documentation for
_get_admittance_branchesis inconsistent with the actual return value: the docstring lists 5 arrays but the function returns 6 (includingy_charging_symmetric). Please update the docstring (or adjust the return) so callers have an accurate contract.
"""Get the admittance matrix of the branches.
Returns
-------
Float[np.ndarray, "n_branches, n_branches, n_branches, n_branches"]
The admittance matrix of the branches.
[branch_effective_admittance_from_to, branch_effective_admittance_from_from,
branch_effective_admittance_to_to, branch_effective_admittance_to_from, branch_effective_admittance_series]
"""
y_series, y_charging_from, y_charging_to, rho_alpha = _get_branch_admittance_terms(
r=branches["r"].to_numpy(dtype=float),
x=branches["x"].to_numpy(dtype=float),
g1=branches["g1"].to_numpy(dtype=float),
b1=branches["b1"].to_numpy(dtype=float),
g2=branches["g2"].to_numpy(dtype=float),
b2=branches["b2"].to_numpy(dtype=float),
rho=branches["rho"].to_numpy(dtype=float),
alpha=branches["alpha"].to_numpy(dtype=float),
)
# check that y_series is not 0 for any branch
if np.any(np.isclose(y_series, 0.0)):
raise ValueError("Zero impedance branch detected. Check network Data!")
y_charging_symmetric = (y_charging_from + y_charging_to) / 2
y_ff = (y_series + y_charging_from) / (rho_alpha * np.conj(rho_alpha))
y_ft = -y_series / np.conj(rho_alpha)
y_tf = -y_series / rho_alpha
y_tt = y_series + y_charging_to
return y_ff, y_ft, y_tf, y_tt, y_series, y_charging_symmetric
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+283
to
+286
| if np.any(np.isclose(r + x, 0.0, rtol=1e-8)): | ||
| y_series = np.zeros_like(r, dtype=complex) | ||
| else: | ||
| y_series = 1 / (r + 1j * x) |
Comment on lines
+19
to
+25
| def _calculate_branch_complex_power( | ||
| v_from: Complex128[jnp.ndarray, " ... n_branches"], | ||
| v_to: Complex128[jnp.ndarray, " ... n_branches"], | ||
| current_from: Complex128[jnp.ndarray, " ... n_branches"], | ||
| current_to: Complex128[jnp.ndarray, " ... n_branches"], | ||
| branch_mask: Complex128[jnp.ndarray, " n_branches"], | ||
| ) -> Tuple[ |
Comment on lines
+8
to
+13
| import py | ||
| import numpy as np | ||
| import pandas as pd | ||
| import pandas.testing as pdt | ||
| import pypowsybl | ||
| import pytest |
Comment on lines
+91
to
+93
| # simple_bsdf_test_case | ||
| # based on basic_node_breaker_network_powsybl | ||
| # simple reasignment and |
Comment on lines
+9
to
+18
| from dc_plus.preprocess.create_network_data import create_network_data_pypowsybl | ||
| import pypowsybl | ||
| from dc_plus.interfaces.network_information import ( | ||
| BusType, | ||
| DynamicNetworkInformation, | ||
| StaticNetworkInformation, | ||
| StringNetworkInformation, | ||
| TransformerTapInformation, | ||
| NetworkInformation, | ||
| ) |
|
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.




Checklist
Please check if the PR fulfills these requirements:
Does this PR already have an issue describing the problem?
Fixes #
What is the new behavior (if this is a feature change)?
Does this PR introduce a breaking change?