Bug fix for EN_setcurve
Adjusts params of any pump that uses the curve whose data is modified by EN_setcurve or EN_setcurvevalue (issue #550 ).
This commit is contained in:
@@ -3,7 +3,7 @@ unit epanet2;
|
||||
{ Declarations of imported procedures from the EPANET PROGRAMMERs TOOLKIT }
|
||||
{ (EPANET2.DLL) }
|
||||
|
||||
{Last updated on 10/29/19}
|
||||
{Last updated on 11/02/19}
|
||||
|
||||
interface
|
||||
|
||||
@@ -355,7 +355,7 @@ const
|
||||
|
||||
function ENgetvertexcount(Index: Integer; var Count: Integer): Integer; stdcall; external EpanetLib;
|
||||
function ENgetvertex(Index: Integer; Vertex: Integer; var X: Double; var Y: Double): Integer; stdcall; external EpanetLib;
|
||||
function ENsetvertices(Index: Integer; X: array of Double; Y: array of Double; Count: Integer): Integer; stdcall; external EpanetLib;
|
||||
function ENsetvertices(Index: Integer; var X: Double; var Y: Double; Count: Integer): Integer; stdcall; external EpanetLib;
|
||||
|
||||
{Pump Functions}
|
||||
function ENgetpumptype(LinkIndex: Integer; var PumpType: Integer): Integer; stdcall; external EpanetLib;
|
||||
@@ -372,7 +372,7 @@ const
|
||||
function ENgetpatternvalue(Index: Integer; Period: Integer; var Value: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENsetpatternvalue(Index: Integer; Period: Integer; Value: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENgetaveragepatternvalue(Index: Integer; var Value: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENsetpattern(Index: Integer; F: array of Single; N: Integer): Integer; stdcall; external EpanetLib;
|
||||
function ENsetpattern(Index: Integer; var F: Single; N: Integer): Integer; stdcall; external EpanetLib;
|
||||
|
||||
{Curve Functions}
|
||||
function ENaddcurve(ID: PAnsiChar): Integer; stdcall; external EpanetLib;
|
||||
@@ -384,8 +384,8 @@ const
|
||||
function ENgetcurvetype(Index: Integer; var CurveType: Integer): Integer; stdcall; external EpanetLib;
|
||||
function ENgetcurvevalue(CurveIndex: Integer; PointIndex: Integer; var X: Single; var Y: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENsetcurvevalue(CurveIndex: Integer; PointIndex: Integer; X: Single; Y: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENgetcurve(Index: Integer; ID: PAnsiChar; var N: Integer; var X: array of Single; var Y: array of Single): Integer; stdcall; external EpanetLib;
|
||||
function ENsetcurve(Index: Integer; X: array of Single; Y: array of Single; N: Integer): Integer; stdcall; external EpanetLib;
|
||||
function ENgetcurve(Index: Integer; ID: PAnsiChar; var N: Integer; var X: Single; var Y: Single): Integer; stdcall; external EpanetLib;
|
||||
function ENsetcurve(Index: Integer; var X: Single; var Y: Single; N: Integer): Integer; stdcall; external EpanetLib;
|
||||
|
||||
{Control Functions}
|
||||
function ENaddcontrol(Ctype: Integer; Link: Integer; Setting: Single; Node: Integer; Level: Single; var Index: Integer): Integer; stdcall; external EpanetLib;
|
||||
|
||||
15
src/epanet.c
15
src/epanet.c
@@ -7,7 +7,7 @@
|
||||
Authors: see AUTHORS
|
||||
Copyright: see AUTHORS
|
||||
License: see LICENSE
|
||||
Last Updated: 10/29/2019
|
||||
Last Updated: 11/02/2019
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
@@ -4474,7 +4474,8 @@ int DLLEXPORT EN_setpattern(EN_Project p, int index, double *values, int len)
|
||||
// Check for valid arguments
|
||||
if (!p->Openflag) return 102;
|
||||
if (index <= 0 || index > net->Npats) return 205;
|
||||
if (len <= 0) return 251;
|
||||
if (values == NULL) return 205;
|
||||
if (len <= 0) return 202;
|
||||
|
||||
// Re-set number of time periods & reallocate memory for multipliers
|
||||
Pattern[index].Length = len;
|
||||
@@ -4741,7 +4742,9 @@ int DLLEXPORT EN_setcurvevalue(EN_Project p, int curveIndex, int pointIndex,
|
||||
// Insert new point into curve
|
||||
curve->X[n] = x;
|
||||
curve->Y[n] = y;
|
||||
return 0;
|
||||
|
||||
// Adjust parameters for pumps using curve as a head curve
|
||||
return adjustpumpparams(p, curveIndex);
|
||||
}
|
||||
|
||||
int DLLEXPORT EN_getcurve(EN_Project p, int index, char *id, int *nPoints,
|
||||
@@ -4765,6 +4768,7 @@ int DLLEXPORT EN_getcurve(EN_Project p, int index, char *id, int *nPoints,
|
||||
|
||||
if (!p->Openflag) return 102;
|
||||
if (index <= 0 || index > p->network.Ncurves) return 206;
|
||||
if (xValues == NULL || yValues == NULL) return 206;
|
||||
curve = &p->network.Curve[index];
|
||||
strncpy(id, curve->ID, MAXID);
|
||||
*nPoints = curve->Npts;
|
||||
@@ -4795,6 +4799,7 @@ int DLLEXPORT EN_setcurve(EN_Project p, int index, double *xValues,
|
||||
// Check for valid arguments
|
||||
if (!p->Openflag) return 102;
|
||||
if (index <= 0 || index > net->Ncurves) return 206;
|
||||
if (xValues == NULL || yValues == NULL) return 206;
|
||||
if (nPoints <= 0) return 202;
|
||||
|
||||
// Check that x values are increasing
|
||||
@@ -4811,7 +4816,9 @@ int DLLEXPORT EN_setcurve(EN_Project p, int index, double *xValues,
|
||||
curve->X[j] = xValues[j];
|
||||
curve->Y[j] = yValues[j];
|
||||
}
|
||||
return 0;
|
||||
|
||||
// Adjust parameters for pumps using curve as a head curve
|
||||
return adjustpumpparams(p, index);
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
Authors: see AUTHORS
|
||||
Copyright: see AUTHORS
|
||||
License: see LICENSE
|
||||
Last Updated: 10/29/2019
|
||||
Last Updated: 11/02/2019
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
@@ -609,6 +609,7 @@ int DLLEXPORT ENsetpattern(int index, EN_API_FLOAT_TYPE *values, int len)
|
||||
{
|
||||
double *v = NULL;
|
||||
int i, errcode;
|
||||
if (values == NULL) return 206;
|
||||
v = (double *)calloc(len, sizeof(double));
|
||||
if (v)
|
||||
{
|
||||
@@ -685,6 +686,7 @@ int DLLEXPORT ENgetcurve(int index, char *id, int *nPoints,
|
||||
Scurve *curve;
|
||||
|
||||
if (index <= 0 || index > net->Ncurves) return 206;
|
||||
if (xValues == NULL || yValues == NULL) return 206;
|
||||
curve = &net->Curve[index];
|
||||
strncpy(id, curve->ID, MAXID);
|
||||
*nPoints = curve->Npts;
|
||||
@@ -701,7 +703,11 @@ int DLLEXPORT ENsetcurve(int index, EN_API_FLOAT_TYPE *xValues,
|
||||
{
|
||||
double *xx = NULL;
|
||||
double *yy = NULL;
|
||||
int i, errcode;
|
||||
int i, errcode = 0;
|
||||
|
||||
if (xValues == NULL || yValues == NULL) return 206;
|
||||
if (nPoints < 1) return 202;
|
||||
|
||||
xx = (double *)calloc(nPoints, sizeof(double));
|
||||
yy = (double *)calloc(nPoints, sizeof(double));
|
||||
if (xx && yy)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
Authors: see AUTHORS
|
||||
Copyright: see AUTHORS
|
||||
License: see LICENSE
|
||||
Last Updated: 10/29/2019
|
||||
Last Updated: 11/02/2019
|
||||
******************************************************************************
|
||||
*/
|
||||
#ifndef FUNCS_H
|
||||
@@ -46,6 +46,7 @@ void freelinkvertices(Slink *);
|
||||
|
||||
void adjustpatterns(Network *, int);
|
||||
void adjustcurves(Network *, int);
|
||||
int adjustpumpparams(Project *, int);
|
||||
int resizecurve(Scurve *, int);
|
||||
|
||||
int getcomment(Network *, int, int, char *);
|
||||
|
||||
@@ -7,13 +7,14 @@
|
||||
Authors: see AUTHORS
|
||||
Copyright: see AUTHORS
|
||||
License: see LICENSE
|
||||
Last Updated: 10/29/2019
|
||||
Last Updated: 11/02/2019
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
//*** For the Windows SDK _tempnam function ***//
|
||||
#ifdef _WIN32
|
||||
@@ -998,6 +999,48 @@ void adjustcurves(Network *network, int index)
|
||||
}
|
||||
}
|
||||
|
||||
int adjustpumpparams(Project *pr, int curveIndex)
|
||||
/*----------------------------------------------------------------
|
||||
** Input: curveIndex = index of a data curve
|
||||
** Output: returns an error code
|
||||
** Purpose: updates head curve parameters for pumps using a
|
||||
** curve whose data have been modified.
|
||||
**----------------------------------------------------------------
|
||||
*/
|
||||
{
|
||||
Network *network = &pr->network;
|
||||
|
||||
double *Ucf = pr->Ucf;
|
||||
int j, err = 0;
|
||||
Spump *pump;
|
||||
|
||||
// Check each pump
|
||||
for (j = 1; j <= network->Npumps; j++)
|
||||
{
|
||||
// Pump uses curve as head curve
|
||||
pump = &network->Pump[j];
|
||||
if ( curveIndex == pump->Hcurve)
|
||||
{
|
||||
// Update its head curve parameters
|
||||
pump->Ptype = NOCURVE;
|
||||
err = updatepumpparams(pr, curveIndex);
|
||||
if (err > 0) break;
|
||||
|
||||
// Convert parameters to internal units
|
||||
if (pump->Ptype == POWER_FUNC)
|
||||
{
|
||||
pump->H0 /= Ucf[HEAD];
|
||||
pump->R *= (pow(Ucf[FLOW], pump->N) / Ucf[HEAD]);
|
||||
}
|
||||
pump->Q0 /= Ucf[FLOW];
|
||||
pump->Qmax /= Ucf[FLOW];
|
||||
pump->Hmax /= Ucf[HEAD];
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
int resizecurve(Scurve *curve, int size)
|
||||
/*----------------------------------------------------------------
|
||||
** Input: curve = a data curve object
|
||||
|
||||
Reference in New Issue
Block a user