Adds new pattern, curve and reporting functions to API

This commit is contained in:
Lew Rossman
2019-02-20 12:43:32 -05:00
parent aef6e29bd6
commit fcbbbf9b2e
13 changed files with 562 additions and 8 deletions

View File

@@ -864,6 +864,19 @@ int DLLEXPORT EN_report(EN_Project p)
return errcode;
}
int DLLEXPORT EN_copyreport(EN_Project p, char *filename)
/*----------------------------------------------------------------
** Input: filename = name of file to receive copy of report
** Output: none
** Returns: error code
** Purpose: copies the contents of a project's report file to
** another file
**----------------------------------------------------------------
*/
{
return copyreport(p, filename);
}
int DLLEXPORT EN_clearreport(EN_Project p)
/*----------------------------------------------------------------
** Input: none
@@ -873,7 +886,6 @@ int DLLEXPORT EN_clearreport(EN_Project p)
**----------------------------------------------------------------
*/
{
if (!p->Openflag) return 102;
return clearreport(p);
}
@@ -3870,6 +3882,53 @@ int DLLEXPORT EN_addpattern(EN_Project p, char *id)
return 0;
}
int DLLEXPORT EN_deletepattern(EN_Project p, int index)
/*----------------------------------------------------------------
** Input: index = index of the pattern to delete
** Output: none
** Returns: error code
** Purpose: deletes a time pattern from a project
**----------------------------------------------------------------
*/
{
int i;
Network *net = &p->network;
Parser *parser = &p->parser;
Hydraul *hyd = &p->hydraul;
// Can't delete a pattern while a solver is active
if (!p->Openflag)return 102;
if (p->hydraul.OpenHflag || p->quality.OpenQflag) return 262;
// Check that pattern exists
if (index < 1 || index > p->network.Npats) return 205;
// Adjust references by other objects to patterns
adjustpatterns(net, index);
// Modify default demand pattern
if (hyd->DefPat == index)
{
hyd->DefPat = 0;
strcpy(parser->DefPatID, "");
}
else if (hyd->DefPat > index) hyd->DefPat--;
// Modify global energy price pattern
if (hyd->Epat == index) hyd->Epat = 0;
else if (hyd->Epat > index) hyd->Epat--;
// Free the pattern's factor array
FREE(net->Pattern[index].F);
// Shift the entries in the network's Pattern array
for (i = index; i < net->Npats; i++) net->Pattern[i] = net->Pattern[i+1];
net->Npats--;
parser->MaxPats--;
return 0;
}
int DLLEXPORT EN_getpatternindex(EN_Project p, char *id, int *index)
/*----------------------------------------------------------------
** Input: id = time pattern name
@@ -3911,6 +3970,28 @@ int DLLEXPORT EN_getpatternid(EN_Project p, int index, char *id)
return 0;
}
int DLLEXPORT EN_setpatternid(EN_Project p, int index, char *id)
/*----------------------------------------------------------------
** Input: index = time pattern index
** id = time pattern ID name
** Returns: error code
** Purpose: changes the ID name of a time pattern
**----------------------------------------------------------------
*/
{
int i;
if (!p->Openflag) return 102;
if (index < 1 || index > p->network.Npats) return 205;
if (strlen(id) > MAXID) return 250;
for (i = 1; i <= p->network.Npats; i++)
{
if (i != index && strcmp(id, p->network.Pattern[i].ID) == 0) return 215;
}
strcpy(p->network.Pattern[index].ID, id);
return 0;
}
int DLLEXPORT EN_getpatternlen(EN_Project p, int index, int *len)
/*----------------------------------------------------------------
** Input: index = time pattern index
@@ -4083,6 +4164,41 @@ int DLLEXPORT EN_addcurve(EN_Project p, char *id)
return 0;
}
int DLLEXPORT EN_deletecurve(EN_Project p, int index)
/*----------------------------------------------------------------
** Input: index = index of the curve to delete
** Output: none
** Returns: error code
** Purpose: deletes a data curve from a project
**----------------------------------------------------------------
*/
{
int i;
Network *net = &p->network;
Parser *parser = &p->parser;
// Can't delete a curve while a solver is active
if (!p->Openflag)return 102;
if (p->hydraul.OpenHflag || p->quality.OpenQflag) return 262;
// Check that curve exists
if (index < 1 || index > p->network.Ncurves) return 205;
// Adjust references by other objects to curves
adjustcurves(net, index);
// Free the curve's data arrays
FREE(net->Curve[index].X);
FREE(net->Curve[index].Y);
// Shift the entries in the network's Curve array
for (i = index; i < net->Ncurves; i++) net->Curve[i] = net->Curve[i + 1];
net->Ncurves--;
parser->MaxCurves--;
return 0;
}
int DLLEXPORT EN_getcurveindex(EN_Project p, char *id, int *index)
/*----------------------------------------------------------------
** Input: id = data curve name
@@ -4124,6 +4240,28 @@ int DLLEXPORT EN_getcurveid(EN_Project p, int index, char *id)
return 0;
}
int DLLEXPORT EN_setcurveid(EN_Project p, int index, char *id)
/*----------------------------------------------------------------
** Input: index = data curve index
** id = data curve ID name
** Returns: error code
** Purpose: changes the ID name of a data curve
**----------------------------------------------------------------
*/
{
int i;
if (!p->Openflag) return 102;
if (index < 1 || index > p->network.Ncurves) return 205;
if (strlen(id) > MAXID) return 250;
for (i = 1; i <= p->network.Ncurves; i++)
{
if (i != index && strcmp(id, p->network.Curve[i].ID) == 0) return 215;
}
strcpy(p->network.Curve[index].ID, id);
return 0;
}
int DLLEXPORT EN_getcurvelen(EN_Project p, int index, int *len)
/*----------------------------------------------------------------
** Input: index = data curve index

View File

@@ -189,6 +189,11 @@ int DLLEXPORT ENwriteline(char *line) { return EN_writeline(_defaultProject, lin
int DLLEXPORT ENreport() { return EN_report(_defaultProject); }
int DLLEXPORT ENcopyreport(char *filename)
{
return EN_copyreport(_defaultProject, filename);
}
int DLLEXPORT ENclearreport() { return EN_clearreport(_defaultProject); }
int DLLEXPORT ENresetreport() { return EN_resetreport(_defaultProject); }
@@ -515,6 +520,11 @@ int DLLEXPORT ENaddpattern(char *id)
return EN_addpattern(_defaultProject, id);
}
int DLLEXPORT ENdeletepattern(int index)
{
return EN_deletepattern(_defaultProject, index);
}
int DLLEXPORT ENgetpatternindex(char *id, int *index)
{
return EN_getpatternindex(_defaultProject, id, index);
@@ -525,6 +535,11 @@ int DLLEXPORT ENgetpatternid(int index, char *id)
return EN_getpatternid(_defaultProject, index, id);
}
int DLLEXPORT ENsetpatternid(int index, char *id)
{
return EN_setpatternid(_defaultProject, index, id);
}
int DLLEXPORT ENgetpatternlen(int index, int *len)
{
return EN_getpatternlen(_defaultProject, index, len);
@@ -577,6 +592,11 @@ int DLLEXPORT ENaddcurve(char *id)
return EN_addcurve(_defaultProject, id);
}
int DLLEXPORT ENdeletecurve(int index)
{
return EN_deletecurve(_defaultProject, index);
}
int DLLEXPORT ENgetcurveindex(char *id, int *index)
{
return EN_getcurveindex(_defaultProject, id, index);
@@ -587,6 +607,11 @@ int DLLEXPORT ENgetcurveid(int index, char *id)
return EN_getcurveid(_defaultProject, index, id);
}
int DLLEXPORT ENsetcurveid(int index, char *id)
{
return EN_setcurveid(_defaultProject, index, id);
}
int DLLEXPORT ENgetcurvelen(int index, int *len)
{
return EN_getcurvelen(_defaultProject, index, len);

View File

@@ -35,6 +35,8 @@ int findlink(Network *, char *);
int findtank(Network *, int);
int findvalve(Network *, int);
int findpump(Network *, int);
void adjustpatterns(Network *, int);
void adjustcurves(Network *, int);
char *getTmpName(char *);
int strcomp(const char *, const char *);
@@ -109,6 +111,7 @@ int checkrules(Project *, long);
// ------- REPORT.C -----------------
int clearreport(Project *);
int copyreport(Project *, char *);
int writereport(Project *);
void writelogo(Project *);
void writesummary(Project *);

View File

@@ -219,6 +219,19 @@ void initpointers(Project *pr)
**----------------------------------------------------------------
*/
{
Network* nw = &pr->network;
nw->Nnodes = 0;
nw->Ntanks = 0;
nw->Njuncs = 0;
nw->Nlinks = 0;
nw->Npipes = 0;
nw->Npumps = 0;
nw->Nvalves = 0;
nw->Ncontrols = 0;
nw->Nrules = 0;
nw->Npats = 0;
nw->Ncurves = 0;
pr->hydraul.NodeDemand = NULL;
pr->hydraul.NodeHead = NULL;
pr->hydraul.LinkFlow = NULL;
@@ -755,6 +768,102 @@ int findvalve(Network *network, int index)
return NOTFOUND;
}
void adjustpattern(int *pat, int index)
/*----------------------------------------------------------------
** Local function that modifies a reference to a deleted time pattern
**----------------------------------------------------------------
*/
{
if (*pat == index) *pat = 0;
else if (*pat > index) (*pat)--;
}
void adjustpatterns(Network *network, int index)
/*----------------------------------------------------------------
** Input: index = index of time pattern being deleted
** Output: none
** Purpose: modifies references made to a deleted time pattern
**----------------------------------------------------------------
*/
{
int j;
Pdemand demand;
Psource source;
// Adjust patterns used by junctions
for (j = 1; j <= network->Njuncs; j++)
{
// Adjust demand patterns
for (demand = network->Node[j].D; demand != NULL; demand = demand->next)
{
adjustpattern(&demand->Pat, index);
}
// Adjust WQ source patterns
source = network->Node[j].S;
if (source) adjustpattern(&source->Pat, index);
}
// Adjust patterns used by reservoir tanks
for (j = 1; j <= network->Ntanks; j++)
{
adjustpattern(&network->Tank[j].Pat, index);
}
// Adjust patterns used by pumps
for (j = 1; j <= network->Npumps; j++)
{
adjustpattern(&network->Pump[j].Upat, index);
adjustpattern(&network->Pump[j].Epat, index);
}
}
void adjustcurve(int *curve, int index)
/*----------------------------------------------------------------
** Local function that modifies a reference to a deleted data curve
**----------------------------------------------------------------
*/
{
if (*curve == index) *curve = 0;
else if (*curve > index) (*curve)--;
}
void adjustcurves(Network *network, int index)
/*----------------------------------------------------------------
** Input: index = index of data curve being deleted
** Output: none
** Purpose: modifies references made to a deleted data curve
**----------------------------------------------------------------
*/
{
int j, k, setting;
// Adjust tank volume curves
for (j = 1; j <= network->Ntanks; j++)
{
adjustcurve(&network->Tank[j].Vcurve, index);
}
// Adjust pump curves
for (j = 1; j <= network->Npumps; j++)
{
adjustcurve(&network->Pump[j].Hcurve, index);
adjustcurve(&network->Pump[j].Ecurve, index);
}
// Adjust GPV curves
for (j = 1; j <= network->Nvalves; j++)
{
k = network->Valve[j].Link;
if (network->Link[k].Type == GPV)
{
setting = INT(network->Link[k].Kc);
adjustcurve(&setting, index);
network->Link[k].Kc = setting;
}
}
}
char *getTmpName(char *fname)
//----------------------------------------------------------------
// Input: fname = file name string

View File

@@ -71,6 +71,51 @@ int clearreport(Project *pr)
return 0;
}
int copyreport(Project* pr, char *filename)
/*
**------------------------------------------------------
** Input: filename = name of file to copy to
** Output: returns error code
** Purpose: copies contents of a project's report file
**------------------------------------------------------
*/
{
FILE *tfile;
int c;
Report *rpt = &pr->report;
// Check that project's report file exists
if (rpt->RptFile == NULL) return 0;
// Open the new destination file
tfile = fopen(filename, "w");
if (tfile == NULL) return 303;
// Re-open project's report file in read mode
fclose(rpt->RptFile);
rpt->RptFile = fopen(rpt->Rpt1Fname, "r");
// Copy contents of project's report file
if (rpt->RptFile)
{
c = fgetc(rpt->RptFile);
while (c != EOF)
{
fputc(c, tfile);
c = fgetc(rpt->RptFile);
}
fclose(rpt->RptFile);
}
// Close destination file
fclose(tfile);
// Re-open project's report file in append mode
rpt->RptFile = fopen(rpt->Rpt1Fname, "a");
if (rpt->RptFile == NULL) return 303;
return 0;
}
int writereport(Project *pr)
/*
**------------------------------------------------------