Eliminates use of temporary linked lists to process Patterns & Curves (issue #449)

This commit is contained in:
Lew Rossman
2019-04-05 13:02:39 -04:00
parent 1632508545
commit edcd4b69c7
7 changed files with 295 additions and 449 deletions

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 03/17/2019
Last Updated: 04/03/2019
******************************************************************************
*/
@@ -274,9 +274,6 @@ void initpointers(Project *pr)
pr->network.NodeHashTable = NULL;
pr->network.LinkHashTable = NULL;
pr->parser.Patlist = NULL;
pr->parser.Curvelist = NULL;
pr->hydraul.smatrix.Aii = NULL;
pr->hydraul.smatrix.Aij = NULL;
pr->hydraul.smatrix.F = NULL;
@@ -317,10 +314,10 @@ int allocdata(Project *pr)
if (!errcode)
{
n = pr->parser.MaxNodes + 1;
pr->network.Node = (Snode *)calloc(n, sizeof(Snode));
pr->network.Node = (Snode *)calloc(n, sizeof(Snode));
pr->hydraul.NodeDemand = (double *)calloc(n, sizeof(double));
pr->hydraul.NodeHead = (double *)calloc(n, sizeof(double));
pr->quality.NodeQual = (double *)calloc(n, sizeof(double));
pr->quality.NodeQual = (double *)calloc(n, sizeof(double));
ERRCODE(MEMCHECK(pr->network.Node));
ERRCODE(MEMCHECK(pr->hydraul.NodeDemand));
ERRCODE(MEMCHECK(pr->hydraul.NodeHead));
@@ -331,8 +328,8 @@ int allocdata(Project *pr)
if (!errcode)
{
n = pr->parser.MaxLinks + 1;
pr->network.Link = (Slink *)calloc(n, sizeof(Slink));
pr->hydraul.LinkFlow = (double *)calloc(n, sizeof(double));
pr->network.Link = (Slink *)calloc(n, sizeof(Slink));
pr->hydraul.LinkFlow = (double *)calloc(n, sizeof(double));
pr->hydraul.LinkSetting = (double *)calloc(n, sizeof(double));
pr->hydraul.LinkStatus = (StatusType *)calloc(n, sizeof(StatusType));
ERRCODE(MEMCHECK(pr->network.Link));
@@ -341,8 +338,8 @@ int allocdata(Project *pr)
ERRCODE(MEMCHECK(pr->hydraul.LinkStatus));
}
// Allocate memory for tanks, sources, pumps, valves,
// controls, demands, time patterns, & operating curves
// Allocate memory for tanks, sources, pumps, valves, and controls
// (memory for Patterns and Curves arrays expanded as each is added)
if (!errcode)
{
pr->network.Tank =
@@ -353,35 +350,15 @@ int allocdata(Project *pr)
(Svalve *)calloc(pr->parser.MaxValves + 1, sizeof(Svalve));
pr->network.Control =
(Scontrol *)calloc(pr->parser.MaxControls + 1, sizeof(Scontrol));
pr->network.Pattern =
(Spattern *)calloc(pr->parser.MaxPats + 1, sizeof(Spattern));
pr->network.Curve =
(Scurve *)calloc(pr->parser.MaxCurves + 1, sizeof(Scurve));
ERRCODE(MEMCHECK(pr->network.Tank));
ERRCODE(MEMCHECK(pr->network.Pump));
ERRCODE(MEMCHECK(pr->network.Valve));
ERRCODE(MEMCHECK(pr->network.Control));
ERRCODE(MEMCHECK(pr->network.Pattern));
ERRCODE(MEMCHECK(pr->network.Curve));
}
// Initialize pointers used in patterns, curves, and demand category lists
// Initialize pointers used in nodes and links
if (!errcode)
{
for (n = 0; n <= pr->parser.MaxPats; n++)
{
pr->network.Pattern[n].Length = 0;
pr->network.Pattern[n].F = NULL;
pr->network.Pattern[n].Comment = NULL;
}
for (n = 0; n <= pr->parser.MaxCurves; n++)
{
pr->network.Curve[n].Npts = 0;
pr->network.Curve[n].Type = GENERIC_CURVE;
pr->network.Curve[n].X = NULL;
pr->network.Curve[n].Y = NULL;
pr->network.Curve[n].Comment = NULL;
}
for (n = 0; n <= pr->parser.MaxNodes; n++)
{
pr->network.Node[n].D = NULL; // node demand
@@ -399,43 +376,6 @@ int allocdata(Project *pr)
return errcode;
}
void freeTmplist(STmplist *t)
/*----------------------------------------------------------------
** Input: t = pointer to start of a temporary list
** Output: none
** Purpose: frees memory used for temporary storage
** of pattern & curve data
**----------------------------------------------------------------
*/
{
STmplist *tnext;
while (t != NULL)
{
tnext = t->next;
freeFloatlist(t->x);
freeFloatlist(t->y);
free(t);
t = tnext;
}
}
void freeFloatlist(SFloatlist *f)
/*----------------------------------------------------------------
** Input: f = pointer to start of list of floats
** Output: none
** Purpose: frees memory used for storing list of floats
**----------------------------------------------------------------
*/
{
SFloatlist *fnext;
while (f != NULL)
{
fnext = f->next;
free(f);
f = fnext;
}
}
void freedata(Project *pr)
/*----------------------------------------------------------------
** Input: none
@@ -446,7 +386,6 @@ void freedata(Project *pr)
{
int j;
Pdemand demand, nextdemand;
Psource source;
// Free memory for computed results
free(pr->hydraul.NodeDemand);
@@ -474,8 +413,7 @@ void freedata(Project *pr)
demand = nextdemand;
}
// Free memory used for WQ source data
source = pr->network.Node[j].S;
free(source);
free(pr->network.Node[j].S);
free(pr->network.Node[j].Comment);
}
free(pr->network.Node);
@@ -511,7 +449,8 @@ void freedata(Project *pr)
// Free memory for curves
if (pr->network.Curve != NULL)
{
for (j = 0; j <= pr->parser.MaxCurves; j++)
// There is no Curve[0]
for (j = 1; j <= pr->parser.MaxCurves; j++)
{
free(pr->network.Curve[j].X);
free(pr->network.Curve[j].Y);
@@ -809,6 +748,40 @@ int findvalve(Network *network, int index)
return NOTFOUND;
}
int findpattern(Network *network, char *id)
/*----------------------------------------------------------------
** Input: id = time pattern ID
** Output: none
** Returns: time pattern index, or 0 if pattern not found
** Purpose: finds index of time pattern given its ID
**----------------------------------------------------------------
*/
{
int i;
for (i = 1; i <= network->Npats; i++)
{
if (strcmp(id, network->Pattern[i].ID) == 0) return i;
}
return 0;
}
int findcurve(Network *network, char *id)
/*----------------------------------------------------------------
** Input: id = data curve ID
** Output: none
** Returns: data curve index, or 0 if curve not found
** Purpose: finds index of data curve given its ID
**----------------------------------------------------------------
*/
{
int i;
for (i = 1; i <= network->Ncurves; i++)
{
if (strcmp(id, network->Curve[i].ID) == 0) return i;
}
return 0;
}
void adjustpattern(int *pat, int index)
/*----------------------------------------------------------------
** Local function that modifies a reference to a deleted time pattern
@@ -904,6 +877,35 @@ void adjustcurves(Network *network, int index)
}
}
int resizecurve(Scurve *curve, int size)
/*----------------------------------------------------------------
** Input: curve = a data curve object
** size = desired number of curve data points
** Output: error code
** Purpose: resizes a data curve to a desired size
**----------------------------------------------------------------
*/
{
double *x;
double *y;
if (curve->Capacity < size)
{
x = (double *)realloc(curve->X, size * sizeof(double));
if (x == NULL) return 101;
y = (double *)realloc(curve->Y, size * sizeof(double));
if (y == NULL)
{
free(x);
return 101;
}
curve->X = x;
curve->Y = y;
curve->Capacity = size;
}
return 0;
}
int getcomment(Network *network, int object, int index, char *comment)
//----------------------------------------------------------------
// Input: object = a type of network object