Skip to content

Commit 4b8eab2

Browse files
authored
Bug fix for initial status and initial setting used by the WNTRSimulator (#506)
* added _user_status to initial_status setter * added test and documented status variables in comments * Added _setting to initial setting and added a test * documentation update
1 parent af2a8f9 commit 4b8eab2

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

wntr/network/base.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,22 +355,26 @@ def __init__(self, wn, link_name, start_node_name, end_node_name):
355355
# Set and register the ending node
356356
self._end_node = self._node_reg[end_node_name]
357357
self._node_reg.add_usage(end_node_name, (link_name, self.link_type))
358-
# Set up other metadata fields
359-
self._initial_status = LinkStatus.Opened
360-
self._initial_setting = None
361-
self._vertices = []
362-
self._tag = None
363-
# Model state variables
364-
self._user_status = LinkStatus.Opened
365-
self._internal_status = LinkStatus.Active
358+
# Set status variables
359+
self._user_status = LinkStatus.Opened # Control status, can be changed by the user
360+
self._initial_status = LinkStatus.Opened # Model initial status, can be changed by the user
361+
self._internal_status = LinkStatus.Active # Intermediate simulation status, read only
362+
self._status= None # Current simulation status, read only
363+
# Set setting variables
364+
self._initial_setting = None # Model initial setting, can be changed by the user
365+
self._prev_setting = None # Previous simulation setting, read only
366+
self._setting = None # Current simulation setting, read only
367+
# Other model state variables
366368
self._initial_quality = None
367-
self._prev_setting = None
368-
self._setting = None
369369
self._flow = None
370370
self._velocity = None
371371
self._is_isolated = False
372372
self._quality = None
373373
self._headloss = None
374+
# Other
375+
self._vertices = []
376+
self._tag = None
377+
374378

375379
def _compare(self, other):
376380
"""
@@ -423,6 +427,7 @@ def initial_status(self, status):
423427
elif isinstance(status, str): status = LinkStatus[status]
424428
else: status = LinkStatus(int(status))
425429
self._initial_status = status
430+
self._user_status = status
426431

427432
@property
428433
def initial_setting(self):
@@ -432,6 +437,7 @@ def initial_setting(self):
432437
def initial_setting(self, setting):
433438
# TODO: typechecking
434439
self._initial_setting = setting
440+
self._setting = setting
435441

436442
@property
437443
def start_node(self):

wntr/tests/test_network_controls.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import warnings
44
from os.path import abspath, dirname, join
5+
import copy
56

67
import wntr
78

@@ -11,15 +12,24 @@
1112

1213

1314
class TestValveSettingControls(unittest.TestCase):
14-
def test_status_open_when_setting_changes(self):
15+
@classmethod
16+
def setUpClass(self):
1517
wn = wntr.network.WaterNetworkModel()
1618
wn.add_reservoir("r1", base_head=10)
1719
wn.add_junction("j1", base_demand=0)
1820
wn.add_junction("j2", base_demand=0.05)
1921
wn.add_pipe("p1", "r1", "j1")
20-
wn.add_valve("v1", "j1", "j2", valve_type="PRV", initial_setting=2)
22+
wn.add_valve("v1", "j1", "j2", valve_type="PRV",
23+
initial_setting=2, initial_status='Active')
2124
wn.options.time.duration = 3600 * 5
22-
25+
self.wn = wn
26+
27+
@classmethod
28+
def tearDownClass(self):
29+
pass
30+
31+
def test_status_open_when_setting_changes(self):
32+
wn = copy.deepcopy(self.wn)
2333
action = wntr.network.ControlAction(
2434
wn.get_link("v1"), "status", wntr.network.LinkStatus.Closed
2535
)
@@ -37,8 +47,64 @@ def test_status_open_when_setting_changes(self):
3747
self.assertEqual(
3848
results.link["status"].at[7200, "v1"], wntr.network.LinkStatus.Active
3949
)
40-
41-
50+
51+
def test_initial_status(self):
52+
# Run simulations with open valve
53+
wn = copy.deepcopy(self.wn)
54+
valve_name = 'v1'
55+
valve = wn.get_link(valve_name)
56+
valve.initial_status = 'Open'
57+
58+
sim = wntr.sim.EpanetSimulator(wn)
59+
results_epanet_open = sim.run_sim()
60+
61+
sim = wntr.sim.WNTRSimulator(wn)
62+
results_wntr_open = sim.run_sim()
63+
64+
# Check that valve is open (1) and flow is not 0
65+
assert (results_epanet_open.link['status'].loc[:,'v1'] == 1).all()
66+
assert (results_wntr_open.link['status'].loc[:,'v1'] == 1).all()
67+
assert (results_epanet_open.link['flowrate'].loc[:,'v1'].abs() > 0).all()
68+
assert (results_wntr_open.link['flowrate'].loc[:,'v1'].abs() > 0).all()
69+
70+
# Run simulations with closed valve
71+
wn = copy.deepcopy(self.wn)
72+
valve_name = 'v1'
73+
valve = wn.get_link(valve_name)
74+
valve.initial_status = 'Closed'
75+
76+
sim = wntr.sim.EpanetSimulator(wn)
77+
results_epanet_closed = sim.run_sim()
78+
79+
sim = wntr.sim.WNTRSimulator(wn)
80+
results_wntr_closed = sim.run_sim()
81+
82+
# Check that valve is closed (0) and flow is 0
83+
assert (results_epanet_closed.link['status'].loc[:,'v1'] == 0).all()
84+
assert (results_wntr_closed.link['status'].loc[:,'v1'] == 0).all()
85+
assert (results_epanet_closed.link['flowrate'].loc[:,'v1'] == 0).all()
86+
assert (results_wntr_closed.link['flowrate'].loc[:,'v1'] == 0).all()
87+
88+
def test_initial_setting(self):
89+
# Run simulations with valve setting of 4
90+
wn = copy.deepcopy(self.wn)
91+
valve_name = 'v1'
92+
valve = wn.get_link(valve_name)
93+
# pressure setting on its downstream side when the upstream pressure is above the setting
94+
valve.initial_setting = 4
95+
96+
sim = wntr.sim.EpanetSimulator(wn)
97+
results_epanet_open = sim.run_sim()
98+
99+
sim = wntr.sim.WNTRSimulator(wn)
100+
results_wntr_open = sim.run_sim()
101+
102+
# Check that valve is active (2) and the downstream pressure is 4
103+
assert (results_epanet_open.link['status'].loc[:,'v1'] == 2).all()
104+
assert (results_wntr_open.link['status'].loc[:,'v1'] == 2).all()
105+
assert (results_epanet_open.node['pressure'].loc[:,'j2'] == 4).all()
106+
assert (results_wntr_open.node['pressure'].loc[:,'j2'] == 4).all()
107+
42108
class TestPumpSettingControls(unittest.TestCase):
43109
def test_status_open_when_setting_changes(self):
44110
wn = wntr.network.WaterNetworkModel()

0 commit comments

Comments
 (0)