Skip to content

Add set_branch_orientations utility function#156

Merged
Thijss merged 31 commits intomainfrom
fix-branch-orientation
Mar 3, 2026
Merged

Add set_branch_orientations utility function#156
Thijss merged 31 commits intomainfrom
fix-branch-orientation

Conversation

@Thijss
Copy link
Copy Markdown
Member

@Thijss Thijss commented Feb 14, 2026

Adds two new Grid functions:

    def get_reversed_branches(self) -> BranchArray:
        """Get the branch array of branches that are oriented towards the source(s).

        Orientation is determined by the distance of the branch's from_node and to_node to the source node.
        The node that is closer to the source is considered the "from_node".
        Note that this might not reflect the actual power flow direction in the grid.

        - If a source is connected to another source. The orientation will be set from the perspective of
          the source with the lowest node id.
        - Parallel edges (multiple edges between the same two nodes) and cycles are supported.
        - Open branches will be corrected so that the from_status is 1 and the to_status is 0.

        Returns:
            BranchArray: All branches that are oriented towards the source(s)
        """
        return get_reversed_branches(self)

    def set_branch_orientations(self) -> BranchArray:
        """Set branch orientations in the grid so that all branches are oriented away from the sources.
        See also get_reversed_branches() for how the branches to reverse are determined.

        Returns:
            BranchArray: All branches that were reversed.
        """
        return set_branch_orientations(self)

Todo

  • On second thought, I think we can allow fixing branches on cycles after all.

Piggyback

  • Add BaseGraph.has_parallel_edges()

Performance

I did some testing on our production data

93790 branches to consider
fix_branch_orientations took 8.61 seconds
1226 branches are reversed

Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Comment thread src/power_grid_model_ds/utils.py Outdated
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
…tests

Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
… source connection issues

Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
@Thijss Thijss marked this pull request as ready for review February 14, 2026 20:29
@Thijss Thijss changed the title Add fix_branch_orientation utility function Add fix_branch_orientations utility function Feb 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new utility function fix_branch_orientations() to automatically orient all branches in a grid away from source nodes. The function ensures that in a radial network topology, all branches flow outward from the sources, which is important for power flow calculations and network analysis.

Changes:

  • Added fix_branch_orientations() utility function that reorients branches to flow away from sources
  • Added has_parallel_edges() method to the graph model interface to detect parallel edges
  • Added comprehensive test coverage for both new features

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/power_grid_model_ds/utils.py New file containing the fix_branch_orientations() function and helper functions for branch orientation fixing
src/power_grid_model_ds/_core/model/graphs/models/base.py Added abstract method has_parallel_edges() to the base graph model interface
src/power_grid_model_ds/_core/model/graphs/models/rustworkx.py Implemented has_parallel_edges() by delegating to rustworkx's built-in method
tests/unit/test_utils.py New test file with comprehensive tests for fix_branch_orientations() covering basic functionality, dry-run mode, cycle detection, connected sources, and parallel edges
tests/unit/model/graphs/test_graph_model.py Added tests for has_parallel_edges() method, including both same-direction and reversed parallel edges

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/power_grid_model_ds/utils.py Outdated
Comment thread src/power_grid_model_ds/utils.py Outdated
Comment thread src/power_grid_model_ds/utils.py Outdated
Thijss and others added 5 commits February 14, 2026 21:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Copy link
Copy Markdown
Member

@jaapschoutenalliander jaapschoutenalliander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some input on the setup but overall it looks good and I think it is a useful util function

Comment thread src/power_grid_model_ds/utils.py Outdated
Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Copy link
Copy Markdown
Member

@vincentkoppen vincentkoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
Some remarks, but nice functionality to add :)

Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
Comment thread src/power_grid_model_ds/_core/utils/grid.py Outdated
@Thijss Thijss changed the title Add fix_branch_orientations utility function Add set_branch_orientations utility function Feb 27, 2026
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Comment thread src/power_grid_model_ds/_core/model/grids/_reverse.py Outdated
Comment thread src/power_grid_model_ds/_core/model/grids/_reverse.py
Comment thread src/power_grid_model_ds/_core/model/grids/_reverse.py Outdated
Comment thread src/power_grid_model_ds/_core/model/grids/_reverse.py Outdated
Comment thread src/power_grid_model_ds/_core/model/grids/base.py Outdated
Comment thread tests/unit/model/grids/test_reverse.py
Comment thread tests/unit/model/grids/test_reverse.py Outdated
Comment thread tests/unit/model/grids/test_reverse.py Outdated
Comment thread tests/unit/model/grids/test_reverse.py Outdated
Comment thread tests/unit/model/grids/test_reverse.py Outdated
Thijss and others added 4 commits March 2, 2026 12:52
Co-authored-by: Vincent Koppen <53343926+vincentkoppen@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 3, 2026

@Thijss Thijss merged commit c6cca75 into main Mar 3, 2026
15 checks passed
@Thijss Thijss deleted the fix-branch-orientation branch March 3, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants