Pathfinder¶
Depth-first search over the undirected bus/branch graph that
enumerates every simple path from a source bus to the active
fault bus. The resulting ordered branch lists are used by
ElectricalNetwork to inject the mutual-coupling Norton sources
with the correct sign.
Physical / modelling context¶
In a meshed grounding network the mutual coupling between the faulted phase and the grounding return appears only along the path that the phase current actually travels between source and fault. The phase conductor and its associated grounding conductor are inductively coupled segment by segment; the resulting per-segment voltage drop is modelled as a series-EMF that can be Thevenin–Norton-converted into a current source on the grounding-side admittance matrix. To assemble those Norton sources correctly the algorithm has to:
- enumerate every simple (loop-free) path from the source bus to the active fault bus over the undirected bus/branch graph,
- preserve the traversal direction along each branch — the sign of the impressed Norton current depends on it,
- record the branch chain, which the legacy phase-current mode
run_fault(..., phase_current_mode="paths")then weights with the optionalparallel_coefficienton each branch.
parallel_coefficient only applies to phase_current_mode="paths"
Since 0.6.0 the default mode is "auto", which solves a reduced
phase-conductor network from BranchType.phase_impedance_formula
instead of splitting a prescribed current over enumerated paths.
In that mode parallel_coefficient is not read at all — the split
follows from the phase-conductor impedances. Set it only when you
deliberately run the older "paths" mode.
The DFS enumeration is exhaustive, and its cost is exponential in the number of independent loops — not in the number of buses. A radial network of 500 buses has two paths and enumerates in milliseconds; a 5×5 grid of 25 buses already has 8 512 simple paths, and a 6×6 grid 1 262 816, which takes about a minute. Treat the enumeration as affordable only for radial and lightly meshed topologies.
Since the first release after 0.6.0 the default
phase_current_mode="auto" therefore does not enumerate at all. It only needs to know which sources
reach the fault, and Network.sources_reaching(fault)
answers that with a breadth-first search in O(V + E). The
enumeration runs for the legacy phase_current_mode="paths", for
the transient state-space solver, and whenever you ask for it with
gi.create_paths(network). net.paths is consequently empty
after a default run_fault.
PathFinder.find_paths refuses to enumerate more than
network.pathfinder_settings.max_paths paths (default 100_000)
and raises a ValueError that names the bus pair instead of
running for hours. Raise it for one network with
network.pathfinder_settings = gi.PathfinderSettings(max_paths=...)
if you genuinely need the full set on a meshed network.
Example¶
# fragment: assumes a network `net` with the fault "f1" and the buses named below
import groundinsight as gi
from groundinsight.pathfinder import PathFinder
# Build network and choose an active fault — see the quickstart
# for the create_* calls.
net.set_active_fault("f1")
# `create_paths` is NOT called by run_fault in the default
# phase_current_mode="auto". To enumerate the paths explicitly:
gi.create_paths(network=net)
for p in net.paths.values():
print(p.source, "->", p.fault,
"via", [b.name for b in p.segments])
# Direct use of the PathFinder helper:
pf = PathFinder(net)
all_paths = pf.find_paths(source_bus_name="bus_substation",
fault_bus_name="bus_fault")
Settings and memo per network¶
The path search keeps its settings and its memo per network (since audit pass 19). Up to then both were module globals: a limit set for one network applied to every network of the process — also to one another thread was solving at that moment — and three module-level dictionaries, shared by all networks and mutated without a lock, kept the branches of networks long gone alive.
Settings. gi.PathfinderSettings holds the two knobs:
| Field | Default | Meaning |
|---|---|---|
max_paths |
100_000 |
most simple paths one (source bus, fault bus) enumeration may produce |
cache_size |
256 |
entries per table of the network's memo (LRU) |
# fragment: assumes a network `net` with the fault "F"
import groundinsight as gi
net.pathfinder_settings = gi.PathfinderSettings(max_paths=1_000_000)
gi.run_fault(net, "F", phase_current_mode="paths") # this network only
net.pathfinder_settings = None # back to the defaults
The settings are runtime configuration: they are not saved to a
project file or a database, but copy.deepcopy, model_copy and
pickle keep them. PathFinder(network, settings=...) overrides them
for one search.
Memo. network.path_cache() returns the network's
PathfinderCache: the adjacency graph built in _build_graph, the
(source, fault) → List[Path] results of find_paths and the
reaches answers, each table an LRU of cache_size entries keyed on
the active-topology fingerprint
(id(network), network.name,
len(network.buses), len(network.branches),
frozenset(active_buses), frozenset(active_branches),
frozenset((branch, from_bus, to_bus) for every active branch))
Repeated PathFinder(network) constructions on the same topology (the
inner loop of analysis.inverse_rho_f.evaluate_max_epr_under_k, an
outage study returning to its base case) pay the work only once.
find_paths returns copies of the cached Path instances, so callers
such as define_paths can set Path.name / source / fault without
touching the memo. Every operation holds the memo's lock. The memo dies
with its network, a copy or an unpickled network starts with an empty
one, and two networks never compare unequal because of what they have
cached.
Mutating a Network in place
Flipping Bus.active / Branch.active after define_paths()
has already been called leaves the previously enumerated paths
in network.paths. run_fault notices it and rebuilds them; for
your own reads call
Network.invalidate_paths(), which also clears
the network's memo — and only that network's.
clear_pathfinder_cache(network) clears one network's memo;
clear_pathfinder_cache() without an argument clears the memo of every
network alive in the process (test fixtures, recovery paths).
Deprecated: the module-level switches¶
set_pathfinder_max_paths / get_pathfinder_max_paths and
gi.set_pathfinder_cache_size / gi.get_pathfinder_cache_size still
work and raise a DeprecationWarning. They change the process-wide
defaults — pathfinder.default_pathfinder_settings(), the settings of
every network without settings of its own — and
set_pathfinder_cache_size trims the memo of those networks at once.
They will be removed no earlier than two releases and six months after
the release that deprecated them.
| Before | Now |
|---|---|
pathfinder.set_pathfinder_max_paths(n) |
net.pathfinder_settings = gi.PathfinderSettings(max_paths=n) |
gi.set_pathfinder_cache_size(n) |
net.pathfinder_settings = gi.PathfinderSettings(cache_size=n) |
pathfinder.get_pathfinder_max_paths() |
net.pathfinder_settings.max_paths |
When outage_context exits
outage_context clears the per-network pathfinder
cache on exit as well as on entry (added in 0.5.0). The
resident footprint after a long outage sweep therefore matches the
externally-visible state of the network rather than carrying one
cache entry per visited scenario forward.
API reference¶
pathfinder ¶
PathFinder Module.
This module provides the PathFinder class, which is responsible for identifying all possible paths
between sources and faults within an electrical network. It utilizes Depth-First Search (DFS) to traverse
the network graph and determine the connectivity between different buses through branches.
The primary use cases include: - Determining all paths from a specific source to a fault point. - Analyzing the network's topology for fault impact assessment. - Facilitating impedance and grounding calculations based on identified paths.
Settings and memo belong to the network
The search keeps its limits and its memo per network, not per process. A
limit in a module global would apply to every network of the process -- also
to one that another thread is solving at that moment -- and a memo in
module-level dictionaries keyed by id(network) would be mutated from
several threads without a lock and keep the branches of networks long gone
alive until an LRU evicted them.
Both belong to the network:
- :class:
PathfinderSettings(max_paths,cache_size) is runtime configuration of one network, set withnetwork.pathfinder_settings = PathfinderSettings(...). It is not saved with the network, but copies keep it. A network without settings uses the process-wide defaults, which only the deprecated module-level switches change. - :class:
PathfinderCacheis the memo of one network -- adjacency graphs, enumerated paths and reachability answers per active topology, each table an LRU ofcache_sizeentries guarded by a lock.Network.path_cache()hands it out; it dies with its network, a copy or an unpickled network starts with an empty one, and it takes no part in comparing networks.
The deprecated switches still work, with a DeprecationWarning: they change
the process-wide defaults and therefore every network without settings of
its own. A small registry of live caches (weak references) remains so that
clear_pathfinder_cache() and the deprecated cache-size switch can reach
them; the search itself never reads it.
PathFinder ¶
Find all simple paths between sources and faults in a network.
Constructs an adjacency list representation of the network graph and
uses depth-first search (DFS) to identify all possible paths between
a given source bus and fault bus. These paths are used to inject the
mutual-coupling Norton sources with the correct sign in
:mod:groundinsight.electrical_network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The :class: |
required |
settings
|
PathfinderSettings
|
Settings for this search only. Defaults to
|
None
|
Notes
The adjacency graph, the per-(source, fault) paths and the
reachability answers are memoised in the network's own
:class:PathfinderCache (network.path_cache()), keyed on a topology
fingerprint (id(network), name, n_buses, n_branches,
frozenset(active_buses), frozenset(active_branches), connectivity).
Repeated constructions over the same logical topology -- for example the
inner loop of :func:groundinsight.analysis.evaluate_max_epr_under_k --
therefore pay the DFS cost only on the first call, and nothing is shared
with another network.
Build the adjacency-list representation of network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The Network instance containing buses and branches. |
required |
settings
|
PathfinderSettings
|
Settings for this search; defaults to the network's. |
None
|
Source code in src/groundinsight/pathfinder.py
find_paths ¶
Find all simple paths between a source bus and a fault bus.
Uses depth-first search (DFS) to enumerate every loop-free route from the source to the fault over the active sub-graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_bus_name
|
str
|
The name of the source bus. |
required |
fault_bus_name
|
str
|
The name of the fault bus. |
required |
Returns:
| Type | Description |
|---|---|
list of Path
|
All discovered paths, each as an ordered branch list. |
Source code in src/groundinsight/pathfinder.py
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 | |
reaches ¶
Report whether any route connects a source bus to a fault bus.
This is the question the stationary solver actually asks in the
default phase_current_mode="auto": a source contributes to a
fault if and only if at least one route exists. Answering it with
:meth:find_paths means enumerating all simple paths, whose count
is exponential in the number of independent loops -- 1 262 816 paths
and roughly a minute on a 6x6 grid, against milliseconds in the
solver. A breadth-first search over the same active sub-graph, with
an early exit at the target, answers it in O(V + E).
The result is exactly equivalent to len(find_paths(...)) > 0:
both run over the adjacency list built by :meth:_build_graph, so
inactive buses and branches are excluded in the same way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_bus_name
|
str
|
The name of the source bus. |
required |
fault_bus_name
|
str
|
The name of the fault bus. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/groundinsight/pathfinder.py
PathfinderCache ¶
The path-search memo of one network.
Internal API: a network hands it out through :meth:Network.path_cache;
configure the search with :class:PathfinderSettings instead; not exported
from groundinsight and free to change in any release.
Three LRU tables keyed by the active topology of the network (see
:meth:PathFinder._compute_topology_key): the adjacency graph, the
enumerated paths per (source bus, fault bus) and the reachability
answers per pair. Every operation holds the cache's lock, so concurrent
readers of one network do not corrupt it.
Obtain it with network.path_cache(), which makes sure the cache
belongs to that very network: a shallow copy (model_copy) shares the
private attributes of its original, and the owner check gives it a cache
of its own on first use. A deep copy and an unpickled network start empty.
Any two caches compare equal, so the memo never decides whether two
networks are equal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner
|
Network
|
The network the cache belongs to; held weakly. |
None
|
Source code in src/groundinsight/pathfinder.py
belongs_to ¶
Whether this cache is the memo of network itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The network asking. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/groundinsight/pathfinder.py
clear ¶
get ¶
Look key up in table and mark it as recently used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
(graph, paths, reaches)
|
The table. |
"graph"
|
key
|
hashable
|
The entry's key. |
required |
Returns:
| Type | Description |
|---|---|
object or None
|
The stored value, or |
Source code in src/groundinsight/pathfinder.py
keys ¶
The keys of table, least recently used first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
(graph, paths, reaches)
|
The table. |
"graph"
|
Returns:
| Type | Description |
|---|---|
list
|
A snapshot of the keys. |
Source code in src/groundinsight/pathfinder.py
owner ¶
The network this cache belongs to, if it is still alive.
Returns:
| Type | Description |
|---|---|
Network or None
|
The owner, or |
Source code in src/groundinsight/pathfinder.py
put ¶
Store value and drop the least recently used entries over capacity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
(graph, paths, reaches)
|
The table. |
"graph"
|
key
|
hashable
|
The entry's key. |
required |
value
|
object
|
The value; |
required |
capacity
|
int
|
Most entries the table may hold afterwards. |
required |
Source code in src/groundinsight/pathfinder.py
sizes ¶
Number of entries per table.
Returns:
| Type | Description |
|---|---|
dict of str to int
|
|
Source code in src/groundinsight/pathfinder.py
trim ¶
Drop least recently used entries until every table fits capacity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
Most entries per table. |
required |
Source code in src/groundinsight/pathfinder.py
PathfinderSettings ¶
Bases: BaseModel
Settings of the source-to-fault path search of one network.
Assign an instance to network.pathfinder_settings; the settings then
apply to every path search on that network -- :func:groundinsight.create_paths,
run_fault(..., phase_current_mode="paths"), the transient
state-space solver -- and to nothing else. They are runtime configuration:
not saved with the network, kept by copies.
Attributes:
| Name | Type | Description |
|---|---|---|
max_paths |
int
|
Most simple paths one |
cache_size |
int
|
Entries per table of the network's path-search memo (adjacency graph,
enumerated paths, reachability) before the least recently used one is
dropped. One entry per active topology the network has been searched
in -- an outage study visits one per scenario. Defaults to |
Examples:
>>> import groundinsight as gi
>>> settings = gi.PathfinderSettings(max_paths=1_000_000)
>>> settings.max_paths, settings.cache_size
(1000000, 256)
>>> network.pathfinder_settings = settings
clear_pathfinder_cache ¶
Drop the path-search memo of one network, or of every live network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The network whose memo to drop. If |
None
|
Notes
The memo is keyed by the active topology, so an active flip or a
rewiring never reads a stale entry; :meth:Network.invalidate_paths calls
the scoped form anyway, to release the memory. Other networks keep their
memo -- it is theirs.
Examples:
>>> import groundinsight as gi
>>> from groundinsight.api.examples import build_minimal
>>> net = build_minimal()
>>> gi.create_paths(net)
>>> gi.clear_pathfinder_cache(net)
>>> len(net.paths)
1
Source code in src/groundinsight/pathfinder.py
default_pathfinder_settings ¶
The settings of networks that have none of their own.
Returns:
| Type | Description |
|---|---|
PathfinderSettings
|
|
Source code in src/groundinsight/pathfinder.py
get_pathfinder_cache_size ¶
Return the process-wide default size of the path-search memo (deprecated).
.. deprecated::
Read network.pathfinder_settings.cache_size.
Returns:
| Type | Description |
|---|---|
int
|
The default of networks without settings of their own. |
Warns:
| Type | Description |
|---|---|
DeprecationWarning
|
Always. |
Examples:
The replacement reads the settings of one network:
>>> import groundinsight as gi
>>> net = gi.create_network(name="n", frequencies=[50.0])
>>> net.pathfinder_settings.cache_size
256
Source code in src/groundinsight/pathfinder.py
get_pathfinder_max_paths ¶
Return the process-wide default cap on enumerated paths (deprecated).
.. deprecated::
Read network.pathfinder_settings.max_paths.
Returns:
| Type | Description |
|---|---|
int
|
The default of networks without settings of their own. |
Warns:
| Type | Description |
|---|---|
DeprecationWarning
|
Always. |
Source code in src/groundinsight/pathfinder.py
set_pathfinder_cache_size ¶
Set the process-wide default size of the path-search memo (deprecated).
.. deprecated::
Use network.pathfinder_settings =
PathfinderSettings(cache_size=...), which applies to that network
only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int
|
Maximum number of entries per table of a network's memo. Must be a positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The previous default. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Warns:
| Type | Description |
|---|---|
DeprecationWarning
|
Always. |
Notes
Changes :func:default_pathfinder_settings and trims the memo of every
live network to its own effective size at once, so a smaller default is
respected immediately by the networks that follow it.
Examples:
The replacement gives one network settings of its own:
>>> import groundinsight as gi
>>> net = gi.create_network(name="n", frequencies=[50.0])
>>> net.pathfinder_settings = gi.PathfinderSettings(cache_size=64)
>>> net.pathfinder_settings.cache_size
64
Source code in src/groundinsight/pathfinder.py
set_pathfinder_max_paths ¶
Set the process-wide default cap on enumerated simple paths (deprecated).
.. deprecated::
Use network.pathfinder_settings =
PathfinderSettings(max_paths=...), which applies to that network
only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxcount
|
int
|
Maximum number of simple paths per |
required |
Returns:
| Type | Description |
|---|---|
int
|
The previous default. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Warns:
| Type | Description |
|---|---|
DeprecationWarning
|
Always. |
Notes
Changes :func:default_pathfinder_settings, i.e. the limit of every
network that has no :class:PathfinderSettings of its own.