Refactored validate.c

This commit is contained in:
Lew Rossman
2023-10-05 11:37:13 -04:00
parent 5a03def497
commit c0700c83ee
3 changed files with 118 additions and 128 deletions

View File

@@ -4059,7 +4059,7 @@ int DLLEXPORT EN_setlinkvalue(EN_Project p, int index, int property, double valu
pumpIndex = findpump(&p->network, index); pumpIndex = findpump(&p->network, index);
net->Pump[pumpIndex].Ptype = CONST_HP; net->Pump[pumpIndex].Ptype = CONST_HP;
net->Pump[pumpIndex].Hcurve = 0; net->Pump[pumpIndex].Hcurve = 0;
net->Link[index].Km = value / Ucf[POWER]; net->Link[index].Km = value;
} }
break; break;

View File

@@ -1,7 +1,7 @@
/* /*
****************************************************************************** ******************************************************************************
Project: OWA EPANET Project: OWA EPANET
Version: 2.2 Version: 2.3
Module: input2.c Module: input2.c
Description: reads and interprets network data from an EPANET input file Description: reads and interprets network data from an EPANET input file
Authors: see AUTHORS Authors: see AUTHORS
@@ -33,7 +33,6 @@ int getheadlossoption(Project *, char *);
static int newline(Project *, int, char *); static int newline(Project *, int, char *);
static int addpattern(Network *, char *); static int addpattern(Network *, char *);
static int addcurve(Network *, char *); static int addcurve(Network *, char *);
static int getpumpparams(Project *);
static void inperrmsg(Project *, int, int, char *); static void inperrmsg(Project *, int, int, char *);

View File

@@ -1,7 +1,7 @@
/* /*
****************************************************************************** ******************************************************************************
Project: OWA EPANET Project: OWA EPANET
Version: 2.2 Version: 2.3
Module: validate.c Module: validate.c
Description: validates project data Description: validates project data
Authors: see AUTHORS Authors: see AUTHORS
@@ -21,8 +21,8 @@
#include "text.h" #include "text.h"
// Exported functions // Exported functions
int validateproject(Project *pr); int validateproject(Project *);
void reindextanks(Project *pr); void reindextanks(Project *);
int validatetanks(Project *pr) int validatetanks(Project *pr)
/* /*
@@ -149,7 +149,7 @@ int validatecurves(Project *pr)
return result; return result;
} }
int powercurve(double h0, double h1, double h2, double q1, double q2, int powerfuncpump(double h0, double h1, double h2, double q1, double q2,
double *a, double *b, double *c) double *a, double *b, double *c)
/* /*
**--------------------------------------------------------- **---------------------------------------------------------
@@ -160,7 +160,7 @@ int powercurve(double h0, double h1, double h2, double q1, double q2,
** q2 = max. flow ** q2 = max. flow
** Output: *a, *b, *c = pump curve coeffs. (H = a-bQ^c), ** Output: *a, *b, *c = pump curve coeffs. (H = a-bQ^c),
** Returns 1 if sucessful, 0 otherwise. ** Returns 1 if sucessful, 0 otherwise.
** Purpose: computes coeffs. for pump curve ** Purpose: computes coeffs. for a power function pump curve
**---------------------------------------------------------- **----------------------------------------------------------
*/ */
{ {
@@ -179,44 +179,47 @@ int powercurve(double h0, double h1, double h2, double q1, double q2,
return 1; return 1;
} }
int findpumpparams(Project *pr, int pumpindex) int customcurvepump(Project *pr, Spump *pump, Scurve *curve)
/* /*
**------------------------------------------------------------- **-------------------------------------------------------------------
** Input: pumpindex = index of a pump ** Input: pump = a pump object
** Output: returns error code ** curve = a data curve object
** Purpose: computes & checks a pump's head curve coefficients ** Output: returns an error code
**-------------------------------------------------------------- ** Purpose: computes properties for a pump with a custom pump curve.
**-------------------------------------------------------------------
*/ */
{ {
Network *net = &pr->network; int m, npts = curve->Npts;
Spump *pump; pump->Ptype = CUSTOM;
Scurve *curve; for (m = 1; m < npts; m++)
int m;
int curveindex;
int npts = 0;
int errcode = 0;
double a, b, c, h0 = 0.0, h1 = 0.0, h2 = 0.0, q1 = 0.0, q2 = 0.0;
pump = &net->Pump[pumpindex];
if (pump->Ptype == CONST_HP) // Constant Hp pump
{ {
pump->H0 = 0.0; // Curve must have continuously decreasing head (the Y value)
pump->R = -8.814 * net->Link[pump->Link].Km; if (curve->Y[m] >= curve->Y[m - 1])
pump->N = -1.0; {
pump->Hmax = BIG; // No head limit pump->Ptype = NOCURVE;
pump->Qmax = BIG; // No flow limit return 227;
pump->Q0 = 1.0; // Init. flow = 1 cfs
return 0;
} }
}
pump->Qmax = curve->X[npts - 1] / pr->Ucf[FLOW];
pump->Q0 = (curve->X[0] + pump->Qmax) / 2.0 / pr->Ucf[FLOW];
pump->Hmax = curve->Y[0] / pr->Ucf[HEAD];
return 0;
}
int pumpcurvepump(Project *pr, Spump *pump, Scurve *curve)
/*
**-------------------------------------------------------------------
** Input: pump = a pump object
** curve = a data curve object
** Output: returns an error code
** Purpose: computes properties for a pump assigned a pump curve.
**-------------------------------------------------------------------
*/
{
double a, b, c, h0 = 0.0, h1 = 0.0, h2 = 0.0, q1 = 0.0, q2 = 0.0;
int npts = curve->Npts;
else if (pump->Ptype == NOCURVE) // Pump curve specified
{
curveindex = pump->Hcurve;
if (curveindex == 0) return 226;
curve = &net->Curve[curveindex];
curve->Type = PUMP_CURVE; curve->Type = PUMP_CURVE;
npts = curve->Npts;
// Generic power function curve // Generic power function curve
if (npts == 1) if (npts == 1)
@@ -239,54 +242,42 @@ int findpumpparams(Project *pr, int pumpindex)
q2 = curve->X[2]; q2 = curve->X[2];
h2 = curve->Y[2]; h2 = curve->Y[2];
} }
else return customcurvepump(pr, pump, curve);
// Custom pump curve
else
{
pump->Ptype = CUSTOM;
for (m = 1; m < npts; m++)
{
if (curve->Y[m] >= curve->Y[m - 1])
{
pump->Ptype = NOCURVE;
return 227;
}
}
pump->Qmax = curve->X[npts - 1];
pump->Q0 = (curve->X[0] + pump->Qmax) / 2.0;
pump->Hmax = curve->Y[0];
}
// Compute shape factors & limits of power function curves // Compute shape factors & limits of power function curves
if (pump->Ptype == POWER_FUNC) if (!powerfuncpump(h0, h1, h2, q1, q2, &a, &b, &c))
{
if (!powercurve(h0, h1, h2, q1, q2, &a, &b, &c))
{ {
pump->Ptype = NOCURVE; pump->Ptype = NOCURVE;
return 227; return 227;
} }
else else
{ {
pump->H0 = -a; pump->H0 = -a / pr->Ucf[HEAD];
pump->R = -b; pump->R = -b * (pow(pr->Ucf[FLOW], c) / pr->Ucf[HEAD]);
pump->N = c; pump->N = c;
pump->Q0 = q1; pump->Q0 = q1 / pr->Ucf[FLOW];
pump->Qmax = pow((-a / b), (1.0 / c)); pump->Qmax = pow((-a / b), (1.0 / c)) / pr->Ucf[FLOW];
pump->Hmax = h0; pump->Hmax = h0 / pr->Ucf[HEAD];
}
} }
return 0;
}
// Convert units of pump coefficients int constpowerpump(Project *pr, Spump *pump)
if (pump->Ptype == POWER_FUNC) /*
{ **-------------------------------------------------------------------
pump->H0 /= pr->Ucf[HEAD]; ** Input: pump = a pump object
pump->R *= (pow(pr->Ucf[FLOW], pump->N) / pr->Ucf[HEAD]); ** Output: returns an error code
} ** Purpose: computes properties for a constant power pump.
pump->R /= pr->Ucf[POWER]; **-------------------------------------------------------------------
pump->Q0 /= pr->Ucf[FLOW]; */
pump->Qmax /= pr->Ucf[FLOW]; {
pump->Hmax /= pr->Ucf[HEAD]; pump->Ptype = CONST_HP;
} pump->H0 = 0.0;
pump->R = -8.814 * pr->network.Link[pump->Link].Km / pr->Ucf[POWER];
pump->N = -1.0;
pump->Hmax = BIG; // No head limit
pump->Qmax = BIG; // No flow limit
pump->Q0 = 1.0; // Init. flow = 1 cfs
return 0; return 0;
} }
@@ -295,41 +286,41 @@ int validatepumps(Project *pr)
**------------------------------------------------------------------- **-------------------------------------------------------------------
** Input: none ** Input: none
** Output: returns 1 if successful, 0 if not ** Output: returns 1 if successful, 0 if not
** Purpose: checks if pumps assigned pump curves. ** Purpose: checks if pumps are assigned valid pump curve data.
**------------------------------------------------------------------- **-------------------------------------------------------------------
*/ */
{ {
Network *net = &pr->network; Network *net = &pr->network;
int i, k, errcode, result = 1; int i, errcode, result = 1;
char errmsg[MAXMSG+1] = ""; char errmsg[MAXMSG+1] = "";
Spump *pump; Spump *pump;
for (i = 1; i <= net->Npumps; i++) for (i = 1; i <= net->Npumps; i++)
{ {
// Check if pump has neither a head curve nor power setting // Pump has a designated pump curve
pump = &net->Pump[i]; pump = &net->Pump[i];
k = pump->Link; if (pump->Hcurve > 0)
if (net->Link[k].Km == 0.0 && pump->Hcurve <= 0) errcode = pumpcurvepump(pr, pump, &net->Curve[pump->Hcurve]);
{
sprintf(pr->Msg, "Error 226: %s %s",
geterrmsg(226, errmsg), net->Link[k].ID);
writeline(pr, pr->Msg);
result = 0;
}
// Compute & check pump's head curve coefficients // Pump has a constant power setting
else if (net->Link[pump->Link].Km > 0.0)
errcode = constpowerpump(pr, pump);
// Pump has no pump curve info assigned
else else
{ {
errcode = findpumpparams(pr, i); pump->Ptype = NOCURVE;
errcode = 226;
}
if (errcode) if (errcode)
{ {
sprintf(pr->Msg, "Error %d: %s %s", sprintf(pr->Msg, "Error %d: %s %s",
errcode, geterrmsg(errcode, errmsg), net->Link[k].ID); errcode, geterrmsg(errcode, errmsg), net->Link[pump->Link].ID);
writeline(pr, pr->Msg); writeline(pr, pr->Msg);
result = 0; result = 0;
} }
} }
}
return result; return result;
} }