-
Notifications
You must be signed in to change notification settings - Fork 182
/
test_sim_performance.py
344 lines (293 loc) · 11.6 KB
/
test_sim_performance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import sys
import unittest
import pytest
from os.path import abspath, dirname, join
import threading
import time
import numpy as np
import pandas
pandas.set_option("display.max_rows", 10000)
testdir = dirname(abspath(str(__file__)))
test_datadir = join(testdir, "networks_for_testing")
ex_datadir = join(testdir, "..", "..", "examples", "networks")
def compare_results(wntr_res, epa_res, abs_threshold, rel_threshold):
abs_diff = abs(wntr_res - epa_res)
rel_diff = abs_diff / abs(epa_res)
abs_diffa = abs_diff.mean()
rel_diffa = rel_diff.mean()
abs_diffb = abs_diff.mean(axis=1)
rel_diffb = rel_diff.mean(axis=1)
diffa = (abs_diffa < abs_threshold) | (rel_diffa < rel_threshold)
diffb = (abs_diffb < abs_threshold) | (rel_diffb < rel_threshold)
return diffa.all() and diffb.all()
class TestPerformance(unittest.TestCase):
@classmethod
def setUpClass(self):
import wntr
self.wntr = wntr
@classmethod
def tearDownClass(self):
pass
def test_Anytown_multipointcurves_performance(self):
head_diff_abs_threshold = 1e-3
demand_diff_abs_threshold = 1e-5
flow_diff_abs_threshold = 1e-5
rel_threshold = 1e-3
inp_file = join(test_datadir, "Anytown_multipointcurves.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
wn2 = self.wntr.network.WaterNetworkModel(inp_file)
# Apply a curve that is very fine along H = a - b * Q ** c and
# verify the answers are nearly identical under these conditions.
A = 1.313e3
B = 8.705e-6
C = 1.796e0
new_curve_name = "perfect_curve"
cdata = [(10 * q, A - B * (10 * q) ** C) for q in range(1200)]
wn.add_curve(new_curve_name, "HEAD", cdata)
wn2.add_curve(new_curve_name, "HEAD", cdata)
for pump_name, pump in wn.pumps():
pump.pump_curve_name = new_curve_name
for pump_name, pump in wn2.pumps():
pump.pump_curve_name = new_curve_name
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn2)
results = sim.run_sim()
self.assertTrue(
compare_results(
results.node["head"],
epa_res.node["head"],
head_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.node["demand"].loc[:, wn.tank_name_list],
epa_res.node["demand"].loc[:, wn.tank_name_list],
demand_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.link["flowrate"],
epa_res.link["flowrate"],
flow_diff_abs_threshold,
rel_threshold,
)
)
def test_Net1_float64(self):
"""Only needs to test that runs successfully with float64 time options"""
inp_file = join(ex_datadir, "Net1.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
wn.options.time.duration = np.float64(wn.options.time.duration)
wn.options.time.hydraulic_timestep = np.float64(wn.options.time.hydraulic_timestep)
wn.options.time.quality_timestep = np.float64(wn.options.time.quality_timestep)
wn.options.time.rule_timestep = np.float64(wn.options.time.rule_timestep)
wn.options.time.pattern_timestep = np.float64(wn.options.time.pattern_timestep)
wn.options.time.pattern_start = np.float64(wn.options.time.pattern_start)
wn.options.time.report_timestep = np.float64(wn.options.time.report_timestep)
wn.options.time.report_start = np.float64(wn.options.time.report_start)
wn.options.time.start_clocktime = np.float64(wn.options.time.start_clocktime)
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
def test_Net1_charset(self):
"""Only needs to test that runs successfully with latin-1 character set."""
inp_file = join(test_datadir, "latin1.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
def test_Net1_performance(self):
head_diff_abs_threshold = 1e-3
demand_diff_abs_threshold = 1e-5
flow_diff_abs_threshold = 1e-5
rel_threshold = 1e-3
inp_file = join(ex_datadir, "Net1.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
self.assertTrue(
compare_results(
results.node["head"],
epa_res.node["head"],
head_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.node["demand"].loc[:, wn.tank_name_list],
epa_res.node["demand"].loc[:, wn.tank_name_list],
demand_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.link["flowrate"],
epa_res.link["flowrate"],
flow_diff_abs_threshold,
rel_threshold,
)
)
def test_Net3_performance(self):
head_diff_abs_threshold = 1e-3
demand_diff_abs_threshold = 1e-5
flow_diff_abs_threshold = 1e-5
rel_threshold = 1e-3
inp_file = join(ex_datadir, "Net3.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
self.assertTrue(
compare_results(
results.node["head"],
epa_res.node["head"],
head_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.node["demand"].loc[:, wn.tank_name_list],
epa_res.node["demand"].loc[:, wn.tank_name_list],
demand_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.link["flowrate"],
epa_res.link["flowrate"],
flow_diff_abs_threshold,
rel_threshold,
)
)
def test_Net3_performance_PDA(self):
head_diff_abs_threshold = 1e-3
demand_diff_abs_threshold = 1e-5
flow_diff_abs_threshold = 1e-5
rel_threshold = 1e-3
inp_file = join(ex_datadir, "Net3.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
wn.options.hydraulic.demand_model = "PDA"
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim(version=2.2)
wn.options.hydraulic.demand_model = "PDA"
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
self.assertTrue(
compare_results(
results.node["head"],
epa_res.node["head"],
head_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.node["demand"].loc[:, wn.tank_name_list],
epa_res.node["demand"].loc[:, wn.tank_name_list],
demand_diff_abs_threshold,
rel_threshold,
)
)
self.assertTrue(
compare_results(
results.link["flowrate"],
epa_res.link["flowrate"],
flow_diff_abs_threshold,
rel_threshold,
)
)
@pytest.mark.time_consuming
def test_Net6_thread_performance(self):
"""
Test thread-safe performance of simulators
"""
def run_epanet(wn, name):
sim = self.wntr.sim.EpanetSimulator(wn)
sim.run_sim(name, version=2.2)
def run_wntr(wn, name):
sim = self.wntr.sim.WNTRSimulator(wn)
sim.run_sim()
inp_file = join(ex_datadir, "Net6.inp")
wn1 = self.wntr.network.WaterNetworkModel(inp_file)
wn1.options.time.duration = 24 * 3600
wn1.options.time.hydraulic_timestep = 3600
wn1.options.time.report_timestep = 3600
wn1.remove_control(
"control 72"
) # this control never gets activated in epanet because it uses a threshold equal to the tank max level
wn2 = self.wntr.network.WaterNetworkModel(inp_file)
wn2.options.time.duration = 24 * 3600
wn2.options.time.hydraulic_timestep = 3600
wn2.options.time.report_timestep = 3600
wn2.remove_control(
"control 72"
) # this control never gets activated in epanet because it uses a threshold equal to the tank max level
start_time = time.time()
run_epanet(wn1, 'temp1')
run_epanet(wn2, 'temp2')
seq_time = time.time()-start_time
t1 = threading.Thread(target=run_epanet, args=(wn1, 'temp1'))
t2 = threading.Thread(target=run_epanet, args=(wn2, 'temp2'))
start_time = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
thr_time = time.time()-start_time
self.assertGreaterEqual(seq_time - thr_time, -1, 'EPANET threading took 1s longer than sequential')
start_time = time.time()
run_wntr(wn1, 'temp1')
run_wntr(wn2, 'temp2')
seq_time = time.time()-start_time
t1 = threading.Thread(target=run_wntr, args=(wn1, 'temp1'))
t2 = threading.Thread(target=run_wntr, args=(wn2, 'temp2'))
start_time = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
thr_time = time.time()-start_time
self.assertGreaterEqual(seq_time - thr_time, -1, 'WNTR threading took 1s longer than sequential')
@pytest.mark.time_consuming
def test_Net6_mod_performance(self):
head_diff_abs_threshold = 1e-3
demand_diff_abs_threshold = 1e-5
flow_diff_abs_threshold = 1e-5
rel_threshold = 1e-3
inp_file = join(ex_datadir, "Net6.inp")
wn = self.wntr.network.WaterNetworkModel(inp_file)
wn.options.time.duration = 24 * 3600
wn.options.time.hydraulic_timestep = 3600
wn.options.time.report_timestep = 3600
wn.remove_control(
"control 72"
) # this control never gets activated in epanet because it uses a threshold equal to the tank max level
epa_sim = self.wntr.sim.EpanetSimulator(wn)
epa_res = epa_sim.run_sim()
sim = self.wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()
self.assertTrue(
compare_results(
results.node["head"],
epa_res.node["head"],
head_diff_abs_threshold,
rel_threshold,
)
)
# self.assertTrue(compare_results(results.node['demand'].loc[:, wn.tank_name_list], epa_res.node['demand'].loc[:, wn.tank_name_list], demand_diff_abs_threshold, rel_threshold))
# self.assertTrue(compare_results(results.link['flowrate'], epa_res.link['flowrate'], flow_diff_abs_threshold, rel_threshold))
if __name__ == "__main__":
unittest.main()