Bug fix for valvecheck function

See issue #561
This commit is contained in:
Lew Rossman
2019-11-15 11:53:09 -05:00
parent 88e09ba8df
commit ce2a27f38e
4 changed files with 24 additions and 15 deletions

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 11/09/2019
Last Updated: 11/15/2019
******************************************************************************
*/
@@ -3147,7 +3147,7 @@ int DLLEXPORT EN_addlink(EN_Project p, char *id, int linkType,
// Check that valve link has legal connections
if (linkType > PUMP)
{
errcode = valvecheck(p, linkType, n1, n2);
errcode = valvecheck(p, 0, linkType, n1, n2);
if (errcode) return errcode;
}
@@ -3491,7 +3491,7 @@ int DLLEXPORT EN_setlinktype(EN_Project p, int *index, int linkType, int actionC
EN_getnodeid(p, n2, id2);
// Check for illegal valve connections
errcode = valvecheck(p, linkType, n1, n2);
errcode = valvecheck(p, i, linkType, n1, n2);
if (errcode) return errcode;
// Delete the original link (and any controls containing it)
@@ -3537,6 +3537,9 @@ int DLLEXPORT EN_setlinknodes(EN_Project p, int index, int node1, int node2)
// Cannot modify network structure while solvers are active
if (p->hydraul.OpenHflag || p->quality.OpenQflag) return 262;
// Check for valid link index
if (index <= 0 || index > net->Nlinks) return 204;
// Check that nodes exist
if (node1 < 0 || node1 > net->Nnodes) return 203;
if (node2 < 0 || node2 > net->Nnodes) return 203;
@@ -3544,11 +3547,15 @@ int DLLEXPORT EN_setlinknodes(EN_Project p, int index, int node1, int node2)
// Check that nodes are not the same
if (node1 == node2) return 222;
// Do nothing if the new nodes are the same as the old ones
if (node1 == net->Link[index].N1 && node2 == net->Link[index].N2)
return 0;
// Check for illegal valve connection
type = net->Link[index].Type;
if (type > PUMP)
{
errcode = valvecheck(p, type, node1, node2);
errcode = valvecheck(p, index, type, node1, node2);
if (errcode) return errcode;
}

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 11/02/2019
Last Updated: 11/15/2019
******************************************************************************
*/
#ifndef FUNCS_H
@@ -28,7 +28,7 @@ int buildadjlists(Network *);
void freeadjlists(Network *);
int incontrols(Project *, int, int);
int valvecheck(Project *, int, int, int);
int valvecheck(Project *, int, int, int, int);
int findnode(Network *, char *);
int findlink(Network *, char *);
int findtank(Network *, int);

View File

@@ -7,7 +7,7 @@ Description: parses network data from a line of an EPANET input file
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 10/29/2019
Last Updated: 11/15/2019
******************************************************************************
*/
@@ -540,7 +540,7 @@ int valvedata(Project *pr)
if (n >= 7 && !getfloat(parser->Tok[6], &lcoeff)) return setError(parser, 6, 202);
// Check for illegal connections
if (valvecheck(pr, type, j1, j2))
if (valvecheck(pr, net->Nlinks, type, j1, j2))
{
if (j1 > net->Njuncs) return setError(parser, 1, 219);
else if (j2 > net->Njuncs) return setError(parser, 2, 219);

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 11/02/2019
Last Updated: 11/15/2019
******************************************************************************
*/
@@ -396,7 +396,7 @@ void freedata(Project *pr)
// Free memory for node data
if (pr->network.Node != NULL)
{
for (j = 1; j <= pr->network.Nnodes; j++)
for (j = 1; j <= pr->parser.MaxNodes; j++)
{
// Free memory used for demands and WQ source data
freedemands(&(pr->network.Node[j]));
@@ -409,7 +409,7 @@ void freedata(Project *pr)
// Free memory for link data
if (pr->network.Link != NULL)
{
for (j = 1; j <= pr->network.Nlinks; j++)
for (j = 1; j <= pr->parser.MaxLinks; j++)
{
freelinkvertices(&pr->network.Link[j]);
free(pr->network.Link[j].Comment);
@@ -426,7 +426,7 @@ void freedata(Project *pr)
// Free memory for time patterns
if (pr->network.Pattern != NULL)
{
for (j = 0; j <= pr->network.Npats; j++)
for (j = 0; j <= pr->parser.MaxPats; j++)
{
free(pr->network.Pattern[j].F);
free(pr->network.Pattern[j].Comment);
@@ -438,7 +438,7 @@ void freedata(Project *pr)
if (pr->network.Curve != NULL)
{
// There is no Curve[0]
for (j = 1; j <= pr->network.Ncurves; j++)
for (j = 1; j <= pr->parser.MaxCurves; j++)
{
free(pr->network.Curve[j].X);
free(pr->network.Curve[j].Y);
@@ -734,10 +734,11 @@ int incontrols(Project *pr, int objType, int index)
return 0;
}
int valvecheck(Project *pr, int type, int j1, int j2)
int valvecheck(Project *pr, int index, int type, int j1, int j2)
/*
**--------------------------------------------------------------
** Input: type = valve type
** Input: index = link index
** type = valve type
** j1 = index of upstream node
** j2 = index of downstream node
** Output: returns an error code
@@ -761,6 +762,7 @@ int valvecheck(Project *pr, int type, int j1, int j2)
for (k = 1; k <= net->Nvalves; k++)
{
valve = &net->Valve[k];
if (valve->Link == index) continue;
link = &net->Link[valve->Link];
vj1 = link->N1;
vj2 = link->N2;