Skip to content

Commit

Permalink
fix(mfsfr2.py:find_path): wrap find_path() so that the routing dictio…
Browse files Browse the repository at this point in the history
…nary (graph) can still be copied, but won't be copied with every call of find_path() as it recursively finds the routing connections along a path in the SFR network. This was severely impacting performance for large SFR networks. (#955)
  • Loading branch information
aleaf committed Aug 11, 2020
1 parent 0b368cc commit 573da13
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions flopy/modflow/mfsfr2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3638,10 +3638,35 @@ def _parse_6bc(line, icalc, nstrm, isfropt, reachinput, per=0):
return hcond, thickm, elevupdn, width, depth, thts, thti, eps, uhc


def find_path(graph, start, end=0, path=()):
def find_path(graph, start, end=0):
"""Get a path through the routing network,
from a segment to an outlet.
Parameters
----------
graph : dict
Dictionary of seg : outseg numbers
start : int
Starting segment
end : int
Ending segment (default 0)
Returns
-------
path : list
List of segment numbers along routing path.
"""
graph = graph.copy()
path = list(path) + [start]
return _find_path(graph, start, end=end)


def _find_path(graph, start, end=0, path=None):
"""Like find_path, but doesn't copy the routing
dictionary (graph) so that the recursion works.
"""
if path is None:
path = list()
path = path + [start]
if start == end:
return path
if start not in graph:
Expand All @@ -3650,7 +3675,7 @@ def find_path(graph, start, end=0, path=()):
graph[start] = [graph[start]]
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
newpath = _find_path(graph, node, end, path)
if newpath:
return newpath
return None

0 comments on commit 573da13

Please sign in to comment.