Fixes problems with EN_addnode() (#543)
See issue #542 . Also modifies unit test test_node to check that fixup works.
This commit is contained in:
committed by
Sam Hatchett
parent
5f7fd55a36
commit
344700a136
18
src/epanet.c
18
src/epanet.c
@@ -1735,6 +1735,9 @@ int DLLEXPORT EN_addnode(EN_Project p, char *id, int nodeType, int *index)
|
|||||||
// Check if a node with same id already exists
|
// Check if a node with same id already exists
|
||||||
if (EN_getnodeindex(p, id, &i) == 0) return 215;
|
if (EN_getnodeindex(p, id, &i) == 0) return 215;
|
||||||
|
|
||||||
|
// Check for valid node type
|
||||||
|
if (nodeType < EN_JUNCTION || nodeType > EN_TANK) return 251;
|
||||||
|
|
||||||
// Grow node-related arrays to accomodate the new node
|
// Grow node-related arrays to accomodate the new node
|
||||||
size = (net->Nnodes + 2) * sizeof(Snode);
|
size = (net->Nnodes + 2) * sizeof(Snode);
|
||||||
net->Node = (Snode *)realloc(net->Node, size);
|
net->Node = (Snode *)realloc(net->Node, size);
|
||||||
@@ -1746,18 +1749,20 @@ int DLLEXPORT EN_addnode(EN_Project p, char *id, int nodeType, int *index)
|
|||||||
// Actions taken when a new Junction is added
|
// Actions taken when a new Junction is added
|
||||||
if (nodeType == EN_JUNCTION)
|
if (nodeType == EN_JUNCTION)
|
||||||
{
|
{
|
||||||
|
// shift indices of non-Junction nodes at end of Node array
|
||||||
|
for (i = net->Nnodes; i > net->Njuncs; i--)
|
||||||
|
{
|
||||||
|
hashtable_update(net->NodeHashTable, net->Node[i].ID, i + 1);
|
||||||
|
net->Node[i + 1] = net->Node[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// set index of new Junction node
|
||||||
net->Njuncs++;
|
net->Njuncs++;
|
||||||
nIdx = net->Njuncs;
|
nIdx = net->Njuncs;
|
||||||
node = &net->Node[nIdx];
|
node = &net->Node[nIdx];
|
||||||
node->D = NULL;
|
node->D = NULL;
|
||||||
adddemand(node, 0.0, 0, NULL);
|
adddemand(node, 0.0, 0, NULL);
|
||||||
|
|
||||||
// shift rest of Node array
|
|
||||||
for (i = net->Nnodes; i >= net->Njuncs; i--)
|
|
||||||
{
|
|
||||||
hashtable_update(net->NodeHashTable, net->Node[i].ID, i + 1);
|
|
||||||
net->Node[i + 1] = net->Node[i];
|
|
||||||
}
|
|
||||||
// shift indices of Tank array
|
// shift indices of Tank array
|
||||||
for (i = 1; i <= net->Ntanks; i++)
|
for (i = 1; i <= net->Ntanks; i++)
|
||||||
{
|
{
|
||||||
@@ -1816,6 +1821,7 @@ int DLLEXPORT EN_addnode(EN_Project p, char *id, int nodeType, int *index)
|
|||||||
strncpy(node->ID, id, MAXID);
|
strncpy(node->ID, id, MAXID);
|
||||||
|
|
||||||
// set default values for new node
|
// set default values for new node
|
||||||
|
node->Type = nodeType;
|
||||||
node->El = 0;
|
node->El = 0;
|
||||||
node->S = NULL;
|
node->S = NULL;
|
||||||
node->C0 = 0;
|
node->C0 = 0;
|
||||||
|
|||||||
@@ -25,17 +25,21 @@ BOOST_FIXTURE_TEST_CASE(test_adddelete_node, FixtureInitClose)
|
|||||||
|
|
||||||
error = EN_addnode(ph, (char *)"N2", EN_JUNCTION, &index);
|
error = EN_addnode(ph, (char *)"N2", EN_JUNCTION, &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
error = EN_addnode(ph, (char *)"N4", EN_TANK, &index);
|
||||||
|
BOOST_REQUIRE(error == 0);
|
||||||
error = EN_addnode(ph, (char *)"N3", EN_RESERVOIR, &index);
|
error = EN_addnode(ph, (char *)"N3", EN_RESERVOIR, &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
error = EN_addnode(ph, (char *)"N1", EN_JUNCTION, &index);
|
||||||
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
|
error = EN_getnodeindex(ph, (char *)"N1", &index);
|
||||||
|
BOOST_REQUIRE(error == 0);
|
||||||
|
BOOST_REQUIRE(index == 2);
|
||||||
error = EN_getnodeindex(ph, (char *)"N2", &index);
|
error = EN_getnodeindex(ph, (char *)"N2", &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
error = EN_deletenode(ph, index, EN_UNCONDITIONAL);
|
error = EN_deletenode(ph, index, EN_UNCONDITIONAL);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
error = EN_addnode(ph, (char *)"N4", EN_TANK, &index);
|
|
||||||
BOOST_REQUIRE(error == 0);
|
|
||||||
|
|
||||||
error = EN_getnodeindex(ph, (char *)"N4", &index);
|
error = EN_getnodeindex(ph, (char *)"N4", &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
error = EN_deletenode(ph, index, EN_UNCONDITIONAL);
|
error = EN_deletenode(ph, index, EN_UNCONDITIONAL);
|
||||||
|
|||||||
Reference in New Issue
Block a user