Skip to content

Commit 1fb68d0

Browse files
authored
Merge pull request #47 from michaeltryby/dev
Add support for Python warnings
2 parents cc5ee57 + 3543223 commit 1fb68d0

File tree

7 files changed

+266
-13
lines changed

7 files changed

+266
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ target/
6565
.pytest_cache/
6666
epanet_python/toolkit/tests/data/test.*
6767
epanet_python/toolkit/tests/data/test_reopen.inp
68+
epanet_python/toolkit/tests/data/test_warnings.rpt
6869

6970
# Wrapped libraries
7071
*.h

before_build.bat

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
:: US EPA - ORD/NRMRL
88
::
99
:: Requires:
10+
:: git
1011
:: CMake
11-
:: Visual Studio 2015
12+
:: Visual Studio Build Tools
1213
:: SWIG
1314
::
1415
:: Note:
1516
:: This script must be located at the root of the project folder
16-
: in order to work correctly.
17+
:: in order to work correctly.
1718
::
1819

1920

epanet_python/output/setup.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# Python packages: numpy
1414
#
1515

16-
try:
17-
from setuptools import setup, Extension
18-
from setuptools.command.build_ext import build_ext
19-
except ImportError:
20-
from distutils.core import setup, Extension
21-
from distutils.command.build_ext import build_ext
16+
#try:
17+
from setuptools import setup, Extension
18+
from setuptools.command.build_ext import build_ext
19+
20+
#except ImportError:
21+
# from distutils.core import setup, Extension
22+
# from distutils.command.build_ext import build_ext
2223

2324

2425
microlib_name = 'epanet.output'

epanet_python/output/tests/test_output.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ def test_outputmetadata_handle(handle):
5151
assert temp == ref[attr]
5252

5353

54+
def test_getnetsize(handle):
55+
# node, tank, link, pump, valve
56+
ref_array = np.array([11, 2, 13, 1, 0])
57+
58+
netsize_list = oapi.getnetsize(handle)
59+
assert len(netsize_list) == 5
60+
61+
assert np.array_equal(netsize_list, ref_array)
62+
63+
5464
def test_getnodeSeries(handle):
5565

5666
ref_array = np.array(

epanet_python/toolkit/epanet/toolkit/toolkit.i

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,25 @@ and return a (possibly) different pointer */
9797
/* INSERTS CUSTOM EXCEPTION HANDLING IN WRAPPER */
9898
%exception
9999
{
100+
int err_code;
100101
char* err_msg;
101102

102103
err_clear(arg1);
103104

104105
$function
105-
if (err_check(arg1, &err_msg))
106+
107+
err_code = err_check(arg1, &err_msg);
108+
if ( err_code > 10)
106109
{
107110
PyErr_SetString(PyExc_Exception, err_msg);
108111
toolkit_free((void **)&err_msg);
109112
SWIG_fail;
110113
}
114+
else if (err_code > 0)
115+
{
116+
PyErr_WarnEx(PyExc_Warning, err_msg, 2);
117+
toolkit_free((void **)&err_msg);
118+
}
111119
}
112120

113121

@@ -116,6 +124,9 @@ and return a (possibly) different pointer */
116124
int proj_run(Handle ph, const char *input_path, const char *report_path, const char *output_path);
117125
int proj_init(Handle ph, const char *rptFile, const char *outFile, EN_FlowUnits unitsType, EN_HeadLossType headLossType);
118126
int proj_open(Handle ph, const char *inpFile, const char *rptFile, const char *binOutFile);
127+
//int proj_gettitle(Handle ph, char *line1, char *line2, char *line3);
128+
//int proj_settitle(Handle ph, const char *line1, const char *line2, const char *line3);
129+
int proj_getcount(Handle ph, EN_CountType code, int *OUTPUT);
119130
int proj_savefile(Handle ph, const char *filename);
120131
int proj_close(Handle ph);
121132

@@ -145,7 +156,6 @@ int rprt_writeresults(Handle ph);
145156
int rprt_reset(Handle ph);
146157
int rprt_set(Handle ph, char *reportCommand);
147158
int rprt_setlevel(Handle ph, EN_StatusReport code);
148-
int rprt_getcount(Handle ph, EN_CountType code, int *OUTPUT);
149159
int rprt_anlysstats(Handle ph, EN_AnalysisStatistic code, double *OUTPUT );
150160

151161

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
[TITLE]
2+
EPANET Example Network 1
3+
A simple example of modeling chlorine decay. Both bulk and
4+
wall reactions are included.
5+
6+
[JUNCTIONS]
7+
;ID Elev Demand Pattern
8+
10 710 0 ;
9+
11 710 750 ;
10+
12 700 750 ;
11+
13 695 100 ;
12+
21 700 150 ;
13+
22 695 200 ;
14+
23 690 150 ;
15+
31 700 100 ;
16+
32 710 100 ;
17+
18+
[RESERVOIRS]
19+
;ID Head Pattern
20+
9 800 ;
21+
22+
[TANKS]
23+
;ID Elevation InitLevel MinLevel MaxLevel Diameter MinVol VolCurve
24+
2 850 120 100 150 50.5 0 ;
25+
26+
[PIPES]
27+
;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
28+
10 10 11 10530 18 100 0 Open ;
29+
11 11 12 5280 14 100 0 Open ;
30+
12 12 13 5280 10 100 0 Open ;
31+
21 21 22 5280 10 100 0 Open ;
32+
22 22 23 5280 12 100 0 Open ;
33+
31 31 32 5280 6 100 0 Open ;
34+
110 2 12 200 18 100 0 Open ;
35+
111 11 21 5280 10 100 0 Open ;
36+
112 12 22 5280 12 100 0 Open ;
37+
113 13 23 5280 8 100 0 Open ;
38+
121 21 31 5280 8 100 0 Open ;
39+
122 22 32 5280 6 100 0 Open ;
40+
41+
[PUMPS]
42+
;ID Node1 Node2 Parameters
43+
9 9 10 HEAD 1 ;
44+
45+
[VALVES]
46+
;ID Node1 Node2 Diameter Type Setting MinorLoss
47+
48+
[TAGS]
49+
50+
[DEMANDS]
51+
;Junction Demand Pattern Category
52+
53+
[STATUS]
54+
;ID Status/Setting
55+
56+
[PATTERNS]
57+
;ID Multipliers
58+
;Demand Pattern
59+
1 1.0 1.2 1.4 1.6 1.4 1.2
60+
1 1.0 0.8 0.6 0.4 0.6 0.8
61+
62+
[CURVES]
63+
;ID X-Value Y-Value
64+
;PUMP: Pump Curve for Pump 9
65+
1 1500 250
66+
67+
[CONTROLS]
68+
LINK 9 OPEN IF NODE 2 BELOW 110
69+
LINK 9 CLOSED IF NODE 2 ABOVE 140
70+
71+
72+
[RULES]
73+
74+
[ENERGY]
75+
Global Efficiency 75
76+
Global Price 0.0
77+
Demand Charge 0.0
78+
79+
[EMITTERS]
80+
;Junction Coefficient
81+
82+
[QUALITY]
83+
;Node InitQual
84+
10 0.5
85+
11 0.5
86+
12 0.5
87+
13 0.5
88+
21 0.5
89+
22 0.5
90+
23 0.5
91+
31 0.5
92+
32 0.5
93+
9 1.0
94+
2 1.0
95+
96+
[SOURCES]
97+
;Node Type Quality Pattern
98+
99+
[REACTIONS]
100+
;Type Pipe/Tank Coefficient
101+
102+
103+
[REACTIONS]
104+
Order Bulk 1
105+
Order Tank 1
106+
Order Wall 1
107+
Global Bulk -.5
108+
Global Wall -1
109+
Limiting Potential 0.0
110+
Roughness Correlation 0.0
111+
112+
[MIXING]
113+
;Tank Model
114+
115+
[TIMES]
116+
Duration 24:00
117+
Hydraulic Timestep 1:00
118+
Quality Timestep 0:05
119+
Pattern Timestep 2:00
120+
Pattern Start 0:00
121+
Report Timestep 1:00
122+
Report Start 0:00
123+
Start ClockTime 12 am
124+
Statistic None
125+
126+
[REPORT]
127+
Status Yes
128+
Summary No
129+
Page 0
130+
131+
[OPTIONS]
132+
Units GPM
133+
Headloss H-W
134+
Specific Gravity 1.0
135+
Viscosity 1.0
136+
Trials 40
137+
Accuracy 0.001
138+
CHECKFREQ 2
139+
MAXCHECK 10
140+
DAMPLIMIT 0
141+
Unbalanced Continue 10
142+
Pattern 1
143+
Demand Multiplier 1.0
144+
Emitter Exponent 0.5
145+
Quality Chlorine mg/L
146+
Diffusivity 1.0
147+
Tolerance 0.01
148+
149+
[COORDINATES]
150+
;Node X-Coord Y-Coord
151+
10 20.00 70.00
152+
11 30.00 70.00
153+
12 50.00 70.00
154+
13 70.00 70.00
155+
21 30.00 40.00
156+
22 50.00 40.00
157+
23 70.00 40.00
158+
31 30.00 10.00
159+
32 50.00 10.00
160+
9 10.00 70.00
161+
2 50.00 90.00
162+
163+
[VERTICES]
164+
;Link X-Coord Y-Coord
165+
166+
[LABELS]
167+
;X-Coord Y-Coord Label & Anchor Node
168+
6.99 73.63 "Source"
169+
13.48 68.13 "Pump"
170+
43.85 91.21 "Tank"
171+
172+
[BACKDROP]
173+
DIMENSIONS 7.00 6.00 73.00 94.00
174+
UNITS None
175+
FILE
176+
OFFSET 0.00 0.00
177+
178+
[END]

epanet_python/toolkit/tests/test_toolkit.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_hyd_step(handle):
9191

9292
step = en.hydr_next(handle)
9393

94-
if time == 0.:
94+
if not step > 0.:
9595
break
9696

9797
en.hydr_close(handle)
@@ -109,15 +109,15 @@ def test_qual_step(handle):
109109

110110
step = en.qual_next(handle)
111111

112-
if time == 0.:
112+
if not step > 0.:
113113
break
114114

115115
en.qual_close(handle)
116116

117117

118118
def test_report(handle):
119119

120-
nlinks = en.rprt_getcount(handle, en.CountType.LINKS)
120+
nlinks = en.proj_getcount(handle, en.CountType.LINKS)
121121
assert nlinks == 13
122122

123123
en.hydr_solve(handle)
@@ -251,3 +251,55 @@ def test_simplecontrol(handle):
251251
value.clear()
252252
value = en.scntl_get(handle, 2)
253253
assert value == [1, 13, 0.0, 11, 140.0]
254+
255+
256+
WARNING_TEST_INP = os.path.join(DATA_PATH, 'test_warnings.inp')
257+
WARNING_TEST_RPT = os.path.join(DATA_PATH, 'test_warnings.rpt')
258+
WARNING_TEST_OUT = os.path.join(DATA_PATH, 'test_warnings.out')
259+
260+
@pytest.fixture()
261+
def handle_warn(request):
262+
_handle = en.proj_create()
263+
en.proj_open(_handle, WARNING_TEST_INP, WARNING_TEST_RPT, WARNING_TEST_OUT)
264+
265+
def close():
266+
en.proj_close(_handle)
267+
en.proj_delete(_handle)
268+
269+
request.addfinalizer(close)
270+
return _handle
271+
272+
273+
import warnings
274+
warnings.simplefilter("default")
275+
276+
def test_hyd_warning(handle_warn):
277+
with pytest.warns(Warning):
278+
en.hydr_open(handle_warn)
279+
en.hydr_init(handle_warn, en.SaveOption.NOSAVE)
280+
281+
while True:
282+
time = en.hydr_run(handle_warn)
283+
284+
step = en.hydr_next(handle_warn)
285+
286+
if not step > 0.:
287+
break
288+
289+
en.hydr_close(handle_warn)
290+
291+
292+
def test_exception(handle_warn):
293+
with pytest.raises(Exception):
294+
#en.hydr_open(handle_warn)
295+
en.hydr_init(handle_warn, en.SaveOption.NOSAVE)
296+
297+
while True:
298+
time = en.hydr_run(handle_warn)
299+
300+
step = en.hydr_next(handle_warn)
301+
302+
if not step > 0.:
303+
break
304+
305+
en.hydr_close(handle_warn)

0 commit comments

Comments
 (0)