ningshuxia
2025-04-25 9eb7f4af097fb41b81fbff725d930cd6ab052c97
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
/*
 ******************************************************************************
 Project:      OWA EPANET
 Version:      2.2
 Module:       test_link.cpp
 Description:  Tests EPANET toolkit api functions
 Authors:      see AUTHORS
 Copyright:    see AUTHORS
 License:      see LICENSE
 Last Updated: 03/21/2019
 ******************************************************************************
*/
 
#include <math.h>
 
#include <boost/test/unit_test.hpp>
 
#include "test_toolkit.hpp"
 
 
BOOST_AUTO_TEST_SUITE (test_link)
 
 
BOOST_FIXTURE_TEST_CASE(test_adddelete_link, FixtureInitClose)
{
    int index;
 
    // Build a network
    EN_addnode(ph, (char *)"N1", EN_JUNCTION, &index);
    EN_addnode(ph, (char *)"N2", EN_JUNCTION, &index);
    EN_addnode(ph, (char *)"N3", EN_RESERVOIR, &index);
 
    error = EN_addlink(ph, (char *)"L1", EN_PUMP, (char *)"N3", (char *)"N1", &index);
    BOOST_REQUIRE(error == 0);
 
    error = EN_addlink(ph, (char *)"L2", EN_PIPE, (char *)"N1", (char *)"N3", &index);
    BOOST_REQUIRE(error == 0);
 
    error = EN_getlinkindex(ph, (char *)"L2", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_deletelink(ph, index, EN_UNCONDITIONAL);
    BOOST_REQUIRE(error == 0);
 
    error = EN_addlink(ph, (char *)"L3", EN_PIPE, (char *)"N1", (char *)"N2", &index);
    BOOST_REQUIRE(error == 0);
 
    error = EN_getlinkindex(ph, (char *)"L1", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_deletelink(ph, index, EN_UNCONDITIONAL);
    BOOST_REQUIRE(error == 0);
    error = EN_getlinkindex(ph, (char *)"L3", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_deletelink(ph, index, EN_UNCONDITIONAL);
    BOOST_REQUIRE(error == 0);
 
}
 
BOOST_FIXTURE_TEST_CASE(test_link_id_isvalid, FixtureInitClose)
{
    int index;
 
    // Build a network
    EN_addnode(ph, (char *)"N1", EN_JUNCTION, &index);
    EN_addnode(ph, (char *)"N2", EN_JUNCTION, &index);
    EN_addnode(ph, (char *)"N3", EN_RESERVOIR, &index);
 
    error = EN_addlink(ph, (char *)"L1", EN_PUMP, (char *)"N1", (char *)"N2", &index);
    BOOST_REQUIRE(error == 0);
 
    error = EN_addlink(ph, (char *)"L 2", EN_PIPE, (char *)"N1", (char *)"N2", &index);
    BOOST_REQUIRE(error == 252);
 
    error = EN_addlink(ph, (char *)"\"L2", EN_PIPE, (char *)"N1", (char *)"N2", &index);
    BOOST_REQUIRE(error == 252);
 
    error = EN_addlink(ph, (char *)"L;2", EN_PIPE, (char *)"N1", (char *)"N2", &index);
    BOOST_REQUIRE(error == 252);
 
    EN_getlinkindex(ph, (char *)"L1", &index);
    error = EN_setlinkid(ph, index, (char *)"L;1");
    BOOST_REQUIRE(error == 252);
}
 
BOOST_AUTO_TEST_CASE(test_setlinktype)
{
    int error = 0;
    int p113, n31, p121, n113_1, n113_2;
    double q113 = 0.0, p31 = 0.0, diam;
 
    EN_Project ph = NULL;
    EN_createproject(&ph);
 
    error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, "");
    BOOST_REQUIRE(error == 0);
 
    // Change duration to 0
    error = EN_settimeparam(ph, EN_DURATION, 0);
    BOOST_REQUIRE(error == 0);
 
    // Get indexes of pipe 113 and node 31
    error = EN_getlinkindex(ph, (char *)"113", &p113);
    BOOST_REQUIRE(error == 0);
    error = EN_getnodeindex(ph, (char *)"31", &n31);
    BOOST_REQUIRE(error == 0);
 
    // Reverse pipe 113 and give it a check valve
    error = EN_getlinknodes(ph, p113, &n113_1, &n113_2);
    BOOST_REQUIRE(error == 0);
    error = EN_setlinknodes(ph, p113, n113_2, n113_1);
    BOOST_REQUIRE(error == 0);
    error = EN_setlinktype(ph, &p113, EN_CVPIPE, 0);
    BOOST_REQUIRE(error == 0);
 
    // Get index & diameter of pipe 121 connected to node 31
    error = EN_getlinkindex(ph, (char *)"121", &p121);
    BOOST_REQUIRE(error == 0);
    error = EN_getlinkvalue(ph, p121, EN_DIAMETER, &diam);
    BOOST_REQUIRE(error == 0);
 
    // Replace it with a PRV
    error = EN_setlinktype(ph, &p121, EN_PRV, 0);
    BOOST_REQUIRE(error == 0);
 
    // Set diameter & setting of new PRV
    error = EN_setlinkvalue(ph, p121, EN_INITSETTING, 100);
    BOOST_REQUIRE(error == 0);
    error = EN_setlinkvalue(ph, p121, EN_DIAMETER, diam);
    BOOST_REQUIRE(error == 0);
 
    // Solve for hydraulics
    error = EN_solveH(ph);
    BOOST_REQUIRE(error == 0);
 
    // Get flow in link 113 and pressure at node 31
    error = EN_getlinkvalue(ph, p113, EN_FLOW, &q113);
    BOOST_REQUIRE(error == 0);
    error = EN_getnodevalue(ph, n31, EN_PRESSURE, &p31);
    BOOST_REQUIRE(error == 0);
 
    // Require that link 113 flow be 0
    q113 = fabs(q113);
    BOOST_REQUIRE(q113 < 0.001);
 
    // Require that node 31 pressure be 100
    p31 = fabs(p31 - 100.0f);
    BOOST_REQUIRE(p31 < 0.001);
 
    // Close and delete project
    error = EN_close(ph);
    BOOST_REQUIRE(error == 0);
    EN_deleteproject(ph);
}
 
 
BOOST_AUTO_TEST_CASE(test_link_setid_save)
{
    int error = 0;
 
    EN_Project ph = NULL;
    EN_createproject(&ph);
 
    error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, "");
    BOOST_REQUIRE(error == 0);
 
    // Test of illegal link name change
    char newid_3[] = "Illegal; link name";
    error = EN_setlinkid(ph, 3, newid_3);
    BOOST_REQUIRE(error > 0);
 
    // Test of legal link name change
    char newid_4[] = "Link3";
    error = EN_setlinkid(ph, 3, newid_4);
    BOOST_REQUIRE(error == 0);
 
    // Save the project
    error = EN_saveinpfile(ph, "net1_setid.inp");
    BOOST_REQUIRE(error == 0);
 
    error = EN_close(ph);
    BOOST_REQUIRE(error == 0);
    EN_deleteproject(ph);
}
 
BOOST_AUTO_TEST_CASE(test_link_setid_reopen, * boost::unit_test::depends_on("test_link/test_link_setid_save"))
{
    int error = 0;
    int index;
 
    EN_Project ph = NULL;
 
    // Re-open the saved project
    EN_createproject(&ph);
    error = EN_open(ph, "net1_setid.inp", DATA_PATH_RPT, "");
    BOOST_REQUIRE(error == 0);
 
    // Check that 3rd link has its new name
    error = EN_getlinkindex(ph, (char *)"Link3", &index);
    BOOST_REQUIRE(error == 0);
    BOOST_CHECK(index == 3);
 
    error = EN_close(ph);
    BOOST_REQUIRE(error == 0);
    EN_deleteproject(ph);
}
 
BOOST_FIXTURE_TEST_CASE(test_link_comments, FixtureOpenClose)
{
    int index;
    char comment[EN_MAXMSG + 1];
 
    // Set link comments
    error = EN_getlinkindex(ph, (char *)"11", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_setcomment(ph, EN_LINK, index, (char *)"P11");
    BOOST_REQUIRE(error == 0);
 
    error = EN_getlinkindex(ph, (char *)"9", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_setcomment(ph, EN_LINK, index, (char *)"Pump9");
    BOOST_REQUIRE(error == 0);
 
    // Check link comments
    error = EN_getlinkindex(ph, (char *)"11", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_getcomment(ph, EN_LINK, index, comment);
    BOOST_REQUIRE(error == 0);
    BOOST_CHECK(check_string(comment, (char *)"P11"));
 
    error = EN_getlinkindex(ph, (char *)"9", &index);
    BOOST_REQUIRE(error == 0);
    error = EN_getcomment(ph, EN_LINK, index, comment);
    BOOST_REQUIRE(error == 0);
    BOOST_CHECK(check_string(comment, (char *)"Pump9"));
}
 
BOOST_AUTO_TEST_SUITE_END()