Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when to_gis() is called on a network with a leak #458

Merged
merged 11 commits into from
Nov 19, 2024
6 changes: 4 additions & 2 deletions wntr/network/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import abc
from wntr.utils.ordered_set import OrderedSet
from collections import OrderedDict
from .elements import Tank, Junction, Valve, Pump, Reservoir, Pipe
from .elements import Tank, Junction, Valve, Pump, Reservoir, Pipe, Link
from wntr.utils.doc_inheritor import DocInheritor
import warnings
from typing import Hashable, Dict, Any, Tuple, MutableSet, Iterable
Expand Down Expand Up @@ -1753,7 +1753,9 @@ def __repr__(self):
return '<ControlAction: {}, {}, {}>'.format(str(self._target_obj), str(self._attribute), str(self._repr_value()))

def __str__(self):
return "{} {} {} IS {}".format(self._target_obj.link_type.upper(),
target_obj_type = (self._target_obj.link_type if isinstance(self._target_obj, Link) else
self._target_obj.node_type)
return "{} {} {} IS {}".format(target_obj_type.upper(),
self._target_obj.name,
self._attribute.upper(),
self._repr_value())
Expand Down
10 changes: 9 additions & 1 deletion wntr/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def setUpClass(self):

self.wntr = wntr

inp_file = join(ex_datadir, "Net6.inp")
self.inp_file = join(ex_datadir, "Net3.inp")
self.inp_files = [join(ex_datadir, f) for f in ["Net1.inp", "Net2.inp", "Net3.inp", "Net6.inp"]]

@classmethod
Expand All @@ -979,6 +979,14 @@ def test_json_pattern_dump(self):
wn = wntr.network.WaterNetworkModel()
wn.add_pattern('pat0', [0,1,0,1,0,1,0])
self.wntr.network.write_json(wn, f'temp.json')

def test_dict_with_leak(self):
# This covers a bug where writing controls to a dictionary broke if a leak was added
wn = wntr.network.WaterNetworkModel(self.inp_file)
junction_name = wn.junction_name_list[0]
junction = wn.get_node(junction_name)
junction.add_leak(wn, area=1, start_time=0, end_time=3600)
wn.to_dict()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest adding from_dict to make sure the leak can be recreated in a water network model.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea!



@unittest.skipIf(not has_geopandas,
Expand Down
Loading