Files
EPANET/src/project.c
T
Sam Hatchett 4d8d82ddc2 v2.2
* removing reference to strncpy

* Fixing memory problems with test_toolkit

Fixes memory leaks and some minor refactoring.

* Update test_toolkit.hpp

removing crtdbg.h from header

* Update CMakeLists.txt

Restoring test_net_builder to test_toolkit.exe

* Cleaning up include statements adding crtdbg.h

* Fixing index error in test

* Add more analysis options to the API (issue #425)

* Fixed epanet2_enums.h

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

* Update input2.c

* Bug fix for 2Comp and LIFO tank mixing models (issue #448)

* Triggering build to update benchmarks

* Added new reg tests

Updating reference build id

* Initial commit list

generic linked list

* Update test_list.cpp

Tests are passing

* Update list.h

Adding documentation

* Fix typo

* Fixing bug in head_list

* Fixing indentation

* Fixed memory leak

Fixed memory leak in test_head_list

* Clean up and inline comments

* Updating file headers

* Update list.c

Updating in line comments.

* Update test_list.cpp

* Fixing indent

Spaces not tabs

* Update list.c

Fixing indent

* Update test_list.cpp

Updating file header to reflect proper attribution

* Expanding test

Added test where data is a struct

* Fixing indent

* Work in progress

* Reorganized to contain list abstraction

* Update list.c

* Refactoring head_list and tail_list

Simplifying head and tail list. Adding delete_node() to list API.

* Update test_list.cpp

* Update test_list.cpp

Fixing bug on gcc

* Fixing bug

* Fixing bug on gcc

* Update CMakeLists.txt

Adding test_list to ctest

* Fixes memory leak in EN_addnode() (#455)


* Fixing memory leak in EN_addnode()

* Separating test_net_builder from test_toolkit

Making test_net_builder a standalone test

* Removing BOOST_TEST_MAIN

* Work in progress

* Updating unit tests

* Fixing compilation bug on gcc

* Work in progress

compiles with warnings, definitely not working

* Update demand.h

* Work in progress

Implementing demand_list

* Work in progress

Creating function for validateing element ID strings

* Work in progress

Refactoring cstr_copy and adding test

* Update cstr_helper.c

fixing indentation

* Update cstr_helper.c

Fixing indentation

* Update test_cstrhelper.cpp

Fixed mem leak

* Adding element id validity checks

* Adding element id validity check

Adding checks for element set id functions

* Fixing build warnings on gcc

* Update errror code from 250 to 252

* Work in progress

Implementing generic demand pattern lists. Compiles but does not run.

* Update demand.c

Work in progress

* Return object index from EN_addnode and EN_addlink (issue #432)

Adds an output argument to EN_addnode and EN_addlink that returns the index of the newly added object.
Also refactors the validity check on object ID names.

* Fixed compilation errors

* Update test_node.cpp

* Create test_demand_data.cpp

* test demand data passing

* Work in progress

Fixing problems when demand lists are null

* Passing open and close test

* get/set demand name are passing

* Updated criteria for valid object ID name

* Work in progress

* Work in progress

Working on demand lists

* Work in progress

Fixing memory leaks
Unit tests passing

* Cleaning up build on gcc

* Cleaning up gcc build

* Fixing bug

* Working on gcc bug

Tests are passing on Appveyor

* Update inpfile.c

Trying to isolate bug

* GCC Bug

* Refactored xstrcpy function

* Update inpfile.c

Testing linux build

* Update epanet.c

Trying to isolate bug

* updating get demand name and write demands

Everything passing locally

* Update test_project.cpp

Isolating bug on gcc

* Isolating bug

Not writing demand section of input file should eliminate it

* Update demand.c

Fixing bug in get_category_name when category_name is NULL

* Restoring write_demands section in saveinpfile

* Update test_demand_data.cpp

Adding index to addnode calls. Fixing indent

* Update demand.c

* Reverted handling of default pattern

When creating demands, no pattern is marked with a zero. Then when data is adjusted it gets updated to default.

* Update epanet.c

Updating EN_getnodevalue() and EN_setnodevalue() to process the primary demand located at the head of the demand list

* Update demand.c

* Work in progress

code cleanup, addressed issue raised in review, and implemented EN_adddemand()

* Adding key and search to list

* Adding remove node method to generic list

* Adding remove demand method to toolkit

* Fix bug and test remove demand

* Fix problems with setting tank parameters (issue #464 )

* Fixed NULL pointer error, if no label is provided after the rule keyword.

* Create Makefile2.bat

Co-Authored-By: Demetrios G. Eliades <eldemet@users.noreply.github.com>
Co-Authored-By: Elad Salomons <selad@optiwater.com>

* Create LICENSE

* Fixed NULL pointer error, if no label is provided after the rule keyword.
Add NULL guard in freerules function. Use strncat and strncpy to ensure
the buffer lengths are adhered to.

* For "conditional" do delete a node connected to a link

For "conditional" deletion the node is deleted only if all of its links have been explicitly deleted beforehand #473

Co-Authored-By: Lew Rossman <lrossman@outlook.com>

* Create CODE_OF_CONDUCT.md

* Refactors the API's demand editing functions

* Update test_demand.cpp

* Update CODE_OF_CONDUCT.md

* Update rules.c

Fix broken win build script

* Updates to doc files

* Documentation edits

* Update Makefile.bat

Updates on the Microsoft SDK 7.1 compilation script to generate runepanet.exe and to use the \include\epanet2.def

* Update Makefile2.bat

Modified epanet2.exe to runepanet.exe, for consistency.

* Delete epanet2.def

Deleted the redundant `epanet2.def` file in the WindSDK folder

* Minor format change to status report

* Removing status reports from CI testing

* rm WinSDK folder and update Makefiles

Co-Authored-By: Demetrios G. Eliades <eldemet@users.noreply.github.com>

* Restored CI testing of status reports

* Removes _DEBUG directives from all source files

This commit removes the #ifdef _DEBUG statements at the top of all source code files per issue #482. It also updates the doc files to stress that the speedup observed for hydraulic analysis with the MMD node re-ordering method only applies to single period runs.

* Fix refactor of types.h

* updates authors

* updates AUTHORS and generator script

* Update run\CMakeLists.txt

* add help file win_build.md

Co-Authored-By: Elad Salomons <selad@optiwater.com>

* move win_build.md to root dir and renaiming to BUILDING.md

* Move BuildAndTest.md to the tools directory

* Update BUILDING.md

* Update BUILDING.md

* Update BUILDING.md

* Fixes problem with findpattern() function (issue #498)

* Change default properties for new pipe created with EN_addlink (issue #500)

* Numerous updates to project documentation

* Adds tank overflow feature

* Updating docs for tank overflow feature

* Updating VB include files

* Update input3.c

* Identifies overflowing tank in Status Report

* Update Makefile.bat

* Update Makefile2.bat

#508

* rethinking the python wrapper (#511)

* renames certain function parameter declarations and removes double pointer call from the deleteproject function

* deprecates conditonal compilation, removes python-specific headers and function renaming

* fixes tests and docs

* fixes test

* PDA fixes

* Minor update to force new CI test

* Another minor change to force another CI test

* Fixes Overflow and PDA tests not being run

* Fix EN_getElseaction and EN_setelseaction

Co-Authored-By: Andreas Ashikkis <andreasashikkis@users.noreply.github.com>

* Add -MT switch for CMake Windows build

* Updates to the docs

* Update BUILDING.md

* Build script updates

* Fixes EN_setlinkvalue bug

* fix in EN_deleteLink

when pipes are deleted via deletelink it also deletes comment of last link

Co-Authored-By: Pavlos Pavlou <pavlou.v.pavlos@ucy.ac.cy>

* rm set to null in functions EN_deletenode, EN_deletelink

* trial actions config

* Update ccpp.yml

* welcome to the Actions beta

* fixes mkstemp file handle-leaking behavior (#529)

* reverts posix include (#533)

... because it is not needed

* Fixes bugs in pump and demand head loss gradients

* Removed dependence on unistd.h in project.c

Travis CI failed because system could not find unistd.h.

* getTmpName() and xstrcpy() made safer

* Fixed use of strncpy in xstrcpy()

* Refactor of hydcoeffs.c

Simplifies and unifies how limit on head gradient at low flow is handled.

* Update ReleaseNotes2_2.md

* Return error if node/link name is too long (#535)

* co-authored with @ehsan-shafiee

* removes errant slashes

* Throws correct error for ID name too long

* Revert "Throws correct error for ID name too long"

This reverts commit 57b4873f5882cb9fd983f7e1e5a703b9e442cd74.

* fixes #534 by bubbling error codes up from add node/link internal functions

* fixes tests on Mac at least

* fixes improper success code

* Error 252 (not 250) returned for ID name too long.

From errors.dat: DAT(252,"invalid ID name")

* Fixes problems with EN_addnode() (#543)

See issue #542 . Also modifies unit test test_node to check that fixup works.

* Adds EN_getresultindex function to the API

See issue #546 . Also fixes a small bug in project.c.

* Adds link vertex get/set functions to the API

* Fixes to EN_addlink and EN_deletelink

* Updates the docs

* 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 ).

* Bug fix for EN_getrule

Fixes possible seg fault condition in EN_getrule. Also defines EN_MISSING as an API constant since it can be assigned internally to several variables that are retrievable by the API.

* Updating the docs

* Adds error check to EN_setheadcurveindex

See issue #556 .

* Update epanet2.pas

* Incorrect characterd

There was a character ’ instead of ' which created an error when compiling LaTeX.

* fixes a crashing issue in freedata (#559)

The freedata function used cached values for sizes of certain arrays found in the parser struct. However, now that the network is mutable, those values can become invalid. Relying instead on the actual array lengths prevents freeing unallocated memory, or ignoring cleanup on newly created elements.

* Bug fix for valvecheck function

See issue #561

* Restored prior update to project.c that got overwritten

* Fixed editing errors made to project.c

* PDF Guide

PDF users' guide for EPANET, and some minor corrections to readme.md to fix some formatting issues.

* HTML Users Guide

* Fixes a "copy over" bug in input3.c

The copying of one input line token over another was causing a compilation error under Clang. With v2.2 this copying is no longer needed so the line of code in question was simply deleted.

This commit also deletes the HTML and Latex output generated by running Doxygen that got added from the previous update to dev since they don't really belong in a source code repo.

* Correction made to doc files

The output-format.dox file was deprecated and not included in the doxyfile so it was deleted. The description of the format of of the Energy Usage section of the binary output in toolkit-files.dox was corrected.

* Update ReleaseNotes2_2.md

I added the v2.2 contributing authors to the notes. I checked PR's from 2017 and beyond and these were the only names I could find. Please append any one I might have missed.

* Fixes problem with re-opening const. HP pumps

See latest comments in issue #528. Also, the setlinkflow() function was deleted as it was never called anywhere.

* Update README.md (#539)

* Update README.md

* Update README.md

Some section titles were re-named to conform to GitHub guidelines and the OWA info was moved to a CREDITS section.

* Update README.md

Added link to the Community Forum page.

* Replaced OWA copyright with "(see AUTHORS)".

* Update AUTHORS

Copied format used by the OWA-SWMM project.

* Update README.md

The Disclaimer section was edited to reflect that there actually is a "collaborative" connection between USEPA and OWA.

* updates CI badges

* cleanup of readme links and unused files

* possessive vs contraction

* adding contributor to notes
2019-12-10 10:19:36 -05:00

1360 lines
41 KiB
C

/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: project.c
Description: project data management routines
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 11/15/2019
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//*** For the Windows SDK _tempnam function ***//
#ifdef _WIN32
#include <windows.h>
#endif
#include "types.h"
#include "funcs.h"
int openfiles(Project *pr, const char *f1, const char *f2, const char *f3)
/*----------------------------------------------------------------
** Input: f1 = pointer to name of input file
** f2 = pointer to name of report file
** f3 = pointer to name of binary output file
** Output: none
** Returns: error code
** Purpose: opens input & report files
**----------------------------------------------------------------
*/
{
// Initialize file pointers to NULL
pr->parser.InFile = NULL;
pr->report.RptFile = NULL;
pr->outfile.OutFile = NULL;
pr->outfile.HydFile = NULL;
pr->outfile.TmpOutFile = NULL;
// Save file names
strncpy(pr->parser.InpFname, f1, MAXFNAME);
strncpy(pr->report.Rpt1Fname, f2, MAXFNAME);
strncpy(pr->outfile.OutFname, f3, MAXFNAME);
if (strlen(f3) > 0) pr->outfile.Outflag = SAVE;
else
{
pr->outfile.Outflag = SCRATCH;
strcpy(pr->outfile.OutFname, pr->TmpOutFname);
}
// Check that file names are not identical
if (strlen(f1) > 0 && (strcomp(f1, f2) || strcomp(f1, f3))) return 301;
if (strlen(f3) > 0 && strcomp(f2, f3)) return 301;
// Attempt to open input and report files
if (strlen(f1) > 0)
{
if ((pr->parser.InFile = fopen(f1, "rt")) == NULL) return 302;
}
if (strlen(f2) == 0) pr->report.RptFile = stdout;
else
{
pr->report.RptFile = fopen(f2, "wt");
if (pr->report.RptFile == NULL) return 303;
}
writelogo(pr);
return 0;
}
int openhydfile(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Returns: error code
** Purpose: opens file that saves hydraulics solution
**----------------------------------------------------------------
*/
{
const int Nnodes = pr->network.Nnodes;
const int Ntanks = pr->network.Ntanks;
const int Nlinks = pr->network.Nlinks;
const int Nvalves = pr->network.Nvalves;
const int Npumps = pr->network.Npumps;
INT4 nsize[6]; // Temporary array
INT4 magic;
INT4 version;
int errcode = 0;
// If HydFile currently open, then close it if its not a scratch file
if (pr->outfile.HydFile != NULL)
{
if (pr->outfile.Hydflag == SCRATCH) return 0;
fclose(pr->outfile.HydFile);
pr->outfile.HydFile = NULL;
}
// Use Hydflag to determine the type of hydraulics file to use.
// Write error message if the file cannot be opened.
pr->outfile.HydFile = NULL;
switch (pr->outfile.Hydflag)
{
case SCRATCH:
strcpy(pr->outfile.HydFname, pr->TmpHydFname);
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "w+b");
break;
case SAVE:
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "w+b");
break;
case USE:
pr->outfile.HydFile = fopen(pr->outfile.HydFname, "rb");
break;
}
if (pr->outfile.HydFile == NULL) return 305;
// If a previous hydraulics solution is not being used, then
// save the current network size parameters to the file.
if (pr->outfile.Hydflag != USE)
{
magic = MAGICNUMBER;
version = ENGINE_VERSION;
nsize[0] = Nnodes;
nsize[1] = Nlinks;
nsize[2] = Ntanks;
nsize[3] = Npumps;
nsize[4] = Nvalves;
nsize[5] = (int)pr->times.Dur;
fwrite(&magic, sizeof(INT4), 1, pr->outfile.HydFile);
fwrite(&version, sizeof(INT4), 1, pr->outfile.HydFile);
fwrite(nsize, sizeof(INT4), 6, pr->outfile.HydFile);
}
// If a previous hydraulics solution is being used, then
// make sure its network size parameters match those of
// the current network
if (pr->outfile.Hydflag == USE)
{
fread(&magic, sizeof(INT4), 1, pr->outfile.HydFile);
if (magic != MAGICNUMBER) return 306;
fread(&version, sizeof(INT4), 1, pr->outfile.HydFile);
if (version != ENGINE_VERSION) return 306;
if (fread(nsize, sizeof(INT4), 6, pr->outfile.HydFile) < 6) return 306;
if (nsize[0] != Nnodes || nsize[1] != Nlinks || nsize[2] != Ntanks ||
nsize[3] != Npumps || nsize[4] != Nvalves ||
nsize[5] != pr->times.Dur
) return 306;
pr->outfile.SaveHflag = TRUE;
}
// Save current position in hydraulics file
// where storage of hydraulic results begins
pr->outfile.HydOffset = ftell(pr->outfile.HydFile);
return errcode;
}
int openoutfile(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Returns: error code
** Purpose: opens binary output file.
**----------------------------------------------------------------
*/
{
int errcode = 0;
// Close output file if already opened
closeoutfile(pr);
// Try to open binary output file
pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b");
if (pr->outfile.OutFile == NULL) return 304;
// Save basic network data & energy usage results
ERRCODE(savenetdata(pr));
pr->outfile.OutOffset1 = ftell(pr->outfile.OutFile);
ERRCODE(saveenergy(pr));
pr->outfile.OutOffset2 = ftell(pr->outfile.OutFile);
// Open temporary file if computing time series statistic
if (!errcode)
{
if (pr->report.Tstatflag != SERIES)
{
pr->outfile.TmpOutFile = fopen(pr->TmpStatFname, "w+b");
if (pr->outfile.TmpOutFile == NULL) errcode = 304;
}
else pr->outfile.TmpOutFile = pr->outfile.OutFile;
}
return errcode;
}
void closeoutfile(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Purpose: closes binary output file.
**----------------------------------------------------------------
*/
{
if (pr->outfile.TmpOutFile != pr->outfile.OutFile)
{
if (pr->outfile.TmpOutFile != NULL)
{
fclose(pr->outfile.TmpOutFile);
pr->outfile.TmpOutFile = NULL;
}
}
if (pr->outfile.OutFile != NULL)
{
if (pr->outfile.OutFile == pr->outfile.TmpOutFile)
{
pr->outfile.TmpOutFile = NULL;
}
fclose(pr->outfile.OutFile);
pr->outfile.OutFile = NULL;
}
}
void initpointers(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Purpose: initializes data array pointers to NULL
**----------------------------------------------------------------
*/
{
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;
pr->hydraul.LinkStatus = NULL;
pr->hydraul.LinkSetting = NULL;
pr->hydraul.OldStatus = NULL;
pr->hydraul.P = NULL;
pr->hydraul.Y = NULL;
pr->hydraul.Xflow = NULL;
pr->quality.NodeQual = NULL;
pr->quality.PipeRateCoeff = NULL;
pr->network.Node = NULL;
pr->network.Link = NULL;
pr->network.Tank = NULL;
pr->network.Pump = NULL;
pr->network.Valve = NULL;
pr->network.Pattern = NULL;
pr->network.Curve = NULL;
pr->network.Control = NULL;
pr->network.Adjlist = NULL;
pr->network.NodeHashTable = NULL;
pr->network.LinkHashTable = NULL;
pr->hydraul.smatrix.Aii = NULL;
pr->hydraul.smatrix.Aij = NULL;
pr->hydraul.smatrix.F = NULL;
pr->hydraul.smatrix.Order = NULL;
pr->hydraul.smatrix.Row = NULL;
pr->hydraul.smatrix.Ndx = NULL;
pr->hydraul.smatrix.XLNZ = NULL;
pr->hydraul.smatrix.NZSUB = NULL;
pr->hydraul.smatrix.LNZ = NULL;
initrules(pr);
}
int allocdata(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Returns: error code
** Purpose: allocates memory for network data structures
**----------------------------------------------------------------
*/
{
int n;
int errcode = 0;
// Allocate node & link ID hash tables
pr->network.NodeHashTable = hashtable_create();
pr->network.LinkHashTable = hashtable_create();
ERRCODE(MEMCHECK(pr->network.NodeHashTable));
ERRCODE(MEMCHECK(pr->network.LinkHashTable));
// Allocate memory for network nodes
//*************************************************************
// NOTE: Because network components of a given type are indexed
// starting from 1, their arrays must be sized 1
// element larger than the number of components.
//*************************************************************
if (!errcode)
{
n = pr->parser.MaxNodes + 1;
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));
ERRCODE(MEMCHECK(pr->network.Node));
ERRCODE(MEMCHECK(pr->hydraul.NodeDemand));
ERRCODE(MEMCHECK(pr->hydraul.NodeHead));
ERRCODE(MEMCHECK(pr->quality.NodeQual));
}
// Allocate memory for network links
if (!errcode)
{
n = pr->parser.MaxLinks + 1;
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));
ERRCODE(MEMCHECK(pr->hydraul.LinkFlow));
ERRCODE(MEMCHECK(pr->hydraul.LinkSetting));
ERRCODE(MEMCHECK(pr->hydraul.LinkStatus));
}
// 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 =
(Stank *)calloc(pr->parser.MaxTanks + 1, sizeof(Stank));
pr->network.Pump =
(Spump *)calloc(pr->parser.MaxPumps + 1, sizeof(Spump));
pr->network.Valve =
(Svalve *)calloc(pr->parser.MaxValves + 1, sizeof(Svalve));
pr->network.Control =
(Scontrol *)calloc(pr->parser.MaxControls + 1, sizeof(Scontrol));
ERRCODE(MEMCHECK(pr->network.Tank));
ERRCODE(MEMCHECK(pr->network.Pump));
ERRCODE(MEMCHECK(pr->network.Valve));
ERRCODE(MEMCHECK(pr->network.Control));
}
// Initialize pointers used in nodes and links
if (!errcode)
{
for (n = 0; n <= pr->parser.MaxNodes; n++)
{
pr->network.Node[n].D = NULL; // node demand
pr->network.Node[n].S = NULL; // node source
pr->network.Node[n].Comment = NULL;
}
for (n = 0; n <= pr->parser.MaxLinks; n++)
{
pr->network.Link[n].Vertices = NULL;
pr->network.Link[n].Comment = NULL;
}
}
// Allocate memory for rule base (see RULES.C)
if (!errcode) errcode = allocrules(pr);
return errcode;
}
void freedata(Project *pr)
/*----------------------------------------------------------------
** Input: none
** Output: none
** Purpose: frees memory allocated for network data structures.
**----------------------------------------------------------------
*/
{
int j;
// Free memory for computed results
free(pr->hydraul.NodeDemand);
free(pr->hydraul.NodeHead);
free(pr->hydraul.LinkFlow);
free(pr->hydraul.LinkSetting);
free(pr->hydraul.LinkStatus);
free(pr->quality.NodeQual);
// Free memory used for nodal adjacency lists
freeadjlists(&pr->network);
// Free memory for node data
if (pr->network.Node != NULL)
{
for (j = 1; j <= pr->network.Nnodes; j++)
{
// Free memory used for demands and WQ source data
freedemands(&(pr->network.Node[j]));
free(pr->network.Node[j].S);
free(pr->network.Node[j].Comment);
}
free(pr->network.Node);
}
// Free memory for link data
if (pr->network.Link != NULL)
{
for (j = 1; j <= pr->network.Nlinks; j++)
{
freelinkvertices(&pr->network.Link[j]);
free(pr->network.Link[j].Comment);
}
}
free(pr->network.Link);
// Free memory for other network objects
free(pr->network.Tank);
free(pr->network.Pump);
free(pr->network.Valve);
free(pr->network.Control);
// Free memory for time patterns
if (pr->network.Pattern != NULL)
{
for (j = 0; j <= pr->network.Npats; j++)
{
free(pr->network.Pattern[j].F);
free(pr->network.Pattern[j].Comment);
}
free(pr->network.Pattern);
}
// Free memory for curves
if (pr->network.Curve != NULL)
{
// There is no Curve[0]
for (j = 1; j <= pr->network.Ncurves; j++)
{
free(pr->network.Curve[j].X);
free(pr->network.Curve[j].Y);
free(pr->network.Curve[j].Comment);
}
free(pr->network.Curve);
}
// Free memory for rule base (see RULES.C)
freerules(pr);
// Free hash table memory
if (pr->network.NodeHashTable != NULL)
{
hashtable_free(pr->network.NodeHashTable);
}
if (pr->network.LinkHashTable != NULL)
{
hashtable_free(pr->network.LinkHashTable);
}
}
Pdemand finddemand(Pdemand d, int index)
/*----------------------------------------------------------------
** Input: d = pointer to start of a list of demands
** index = the position of the demand to retrieve
** Output: none
** Returns: the demand at the requested position
** Purpose: finds the demand at a given position in a demand list
**----------------------------------------------------------------
*/
{
int n = 1;
if (index <= 0)return NULL;
while (d)
{
if (n == index) break;
n++;
d = d->next;
}
return d;
}
int adddemand(Snode *node, double dbase, int dpat, char *dname)
/*----------------------------------------------------------------
** Input: node = a network junction node
** dbase = base demand value
** dpat = demand pattern index
** dname = name of demand category
** Output: returns TRUE if successful, FALSE if not
** Purpose: adds a new demand category to a node.
**----------------------------------------------------------------
*/
{
Pdemand demand, lastdemand;
// Create a new demand struct
demand = (struct Sdemand *)malloc(sizeof(struct Sdemand));
if (demand == NULL) return FALSE;
// Assign it the designated properties
demand->Base = dbase;
demand->Pat = dpat;
demand->Name = NULL;
if (dname && strlen(dname) > 0) xstrcpy(&demand->Name, dname, MAXID);
demand->next = NULL;
// If node has no demands make this its first demand category
if (node->D == NULL) node->D = demand;
// Otherwise append this demand to the end of the node's demands list
else
{
lastdemand = node->D;
while (lastdemand->next) lastdemand = lastdemand->next;
lastdemand->next = demand;
}
return TRUE;
}
void freedemands(Snode *node)
/*----------------------------------------------------------------
** Input: node = a network junction node
** Output: node
** Purpose: frees the memory used for a node's list of demands.
**----------------------------------------------------------------
*/
{
Pdemand nextdemand;
Pdemand demand = node->D;
while (demand != NULL)
{
nextdemand = demand->next;
free(demand->Name);
free(demand);
demand = nextdemand;
}
node->D = NULL;
}
int addlinkvertex(Slink *link, double x, double y)
/*----------------------------------------------------------------
** Input: link = pointer to a network link
** x = x-coordinate of a new vertex
** y = y-coordiante of a new vertex
** Returns: an error code
** Purpose: adds to a link's collection of vertex points.
**----------------------------------------------------------------
*/
{
static int CHUNKSIZE = 5;
int n;
Pvertices vertices;
if (link->Vertices == NULL)
{
vertices = (struct Svertices *) malloc(sizeof(struct Svertices));
if (vertices == NULL) return 101;
vertices->Npts = 0;
vertices->Capacity = CHUNKSIZE;
vertices->X = (double *) calloc(vertices->Capacity, sizeof(double));
vertices->Y = (double *) calloc(vertices->Capacity, sizeof(double));
link->Vertices = vertices;
}
vertices = link->Vertices;
if (vertices->Npts >= vertices->Capacity)
{
vertices->Capacity += CHUNKSIZE;
vertices->X = realloc(vertices->X, vertices->Capacity * sizeof(double));
vertices->Y = realloc(vertices->Y, vertices->Capacity * sizeof(double));
}
if (vertices->X == NULL || vertices->Y == NULL) return 101;
n = vertices->Npts;
vertices->X[n] = x;
vertices->Y[n] = y;
vertices->Npts++;
return 0;
}
void freelinkvertices(Slink *link)
/*----------------------------------------------------------------
** Input: vertices = list of link vertex points
** Output: none
** Purpose: frees the memory used for a link's list of vertices.
**----------------------------------------------------------------
*/
{
if (link->Vertices)
{
free(link->Vertices->X);
free(link->Vertices->Y);
free(link->Vertices);
link->Vertices = NULL;
}
}
int buildadjlists(Network *net)
/*
**--------------------------------------------------------------
** Input: none
** Output: returns error code
** Purpose: builds linked list of links adjacent to each node
**--------------------------------------------------------------
*/
{
int i, j, k;
int errcode = 0;
Padjlist alink;
// Create an array of adjacency lists
freeadjlists(net);
net->Adjlist = (Padjlist *)calloc(net->Nnodes + 1, sizeof(Padjlist));
if (net->Adjlist == NULL) return 101;
for (i = 0; i <= net->Nnodes; i++) net->Adjlist[i] = NULL;
// For each link, update adjacency lists of its end nodes
for (k = 1; k <= net->Nlinks; k++)
{
i = net->Link[k].N1;
j = net->Link[k].N2;
// Include link in start node i's list
alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
if (alink == NULL)
{
errcode = 101;
break;
}
alink->node = j;
alink->link = k;
alink->next = net->Adjlist[i];
net->Adjlist[i] = alink;
// Include link in end node j's list
alink = (struct Sadjlist *) malloc(sizeof(struct Sadjlist));
if (alink == NULL)
{
errcode = 101;
break;
}
alink->node = i;
alink->link = k;
alink->next = net->Adjlist[j];
net->Adjlist[j] = alink;
}
if (errcode) freeadjlists(net);
return errcode;
}
void freeadjlists(Network *net)
/*
**--------------------------------------------------------------
** Input: none
** Output: none
** Purpose: frees memory used for nodal adjacency lists
**--------------------------------------------------------------
*/
{
int i;
Padjlist alink;
if (net->Adjlist == NULL) return;
for (i = 0; i <= net->Nnodes; i++)
{
for (alink = net->Adjlist[i]; alink != NULL; alink = net->Adjlist[i])
{
net->Adjlist[i] = alink->next;
free(alink);
}
}
FREE(net->Adjlist);
}
int incontrols(Project *pr, int objType, int index)
/*----------------------------------------------------------------
** Input: objType = type of object (either NODE or LINK)
** index = the object's index
** Output: none
** Returns: 1 if any controls contain the object; 0 if not
** Purpose: determines if any simple or rule-based controls
** contain a particular node or link.
**----------------------------------------------------------------
*/
{
Network *net = &pr->network;
int i, ruleObject;
Spremise *premise;
Saction *action;
// Check simple controls
for (i = 1; i <= net->Ncontrols; i++)
{
if (objType == NODE && net->Control[i].Node == index) return 1;
if (objType == LINK && net->Control[i].Link == index) return 1;
}
// Check rule-based controls
for (i = 1; i <= net->Nrules; i++)
{
// Convert objType to a rule object type
if (objType == NODE) ruleObject = 6;
else ruleObject = 7;
// Check rule's premises
premise = net->Rule[i].Premises;
while (premise != NULL)
{
if (ruleObject == premise->object && premise->index == index) return 1;
premise = premise->next;
}
// Rule actions only need to be checked for link objects
if (objType == LINK)
{
// Check rule's THEN actions
action = net->Rule[i].ThenActions;
while (action != NULL)
{
if (action->link == index) return 1;
action = action->next;
}
// Check rule's ELSE actions
action = net->Rule[i].ElseActions;
while (action != NULL)
{
if (action->link == index) return 1;
action = action->next;
}
}
}
return 0;
}
int valvecheck(Project *pr, int index, int type, int j1, int j2)
/*
**--------------------------------------------------------------
** Input: index = link index
** type = valve type
** j1 = index of upstream node
** j2 = index of downstream node
** Output: returns an error code
** Purpose: checks for illegal connections between valves
**--------------------------------------------------------------
*/
{
Network *net = &pr->network;
int k, vj1, vj2;
LinkType vtype;
Slink *link;
Svalve *valve;
if (type == PRV || type == PSV || type == FCV)
{
// Can't be connected to a fixed grade node
if (j1 > net->Njuncs || j2 > net->Njuncs) return 219;
// Examine each existing valve
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;
vtype = link->Type;
// Cannot have two PRVs sharing downstream nodes or in series
if (vtype == PRV && type == PRV)
{
if (vj2 == j2 || vj2 == j1 || vj1 == j2) return 220;
}
// Cannot have two PSVs sharing upstream nodes or in series
if (vtype == PSV && type == PSV)
{
if (vj1 == j1 || vj1 == j2 || vj2 == j1) return 220;
}
// Cannot have PSV connected to downstream node of PRV
if (vtype == PSV && type == PRV && vj1 == j2) return 220;
if (vtype == PRV && type == PSV && vj2 == j1) return 220;
// Cannot have PSV connected to downstream node of FCV
// nor have PRV connected to upstream node of FCV
if (vtype == FCV && type == PSV && vj2 == j1) return 220;
if (vtype == FCV && type == PRV && vj1 == j2) return 220;
if (vtype == PSV && type == FCV && vj1 == j2) return 220;
if (vtype == PRV && type == FCV && vj2 == j1) return 220;
}
}
return 0;
}
int findnode(Network *network, char *id)
/*----------------------------------------------------------------
** Input: id = node ID
** Output: none
** Returns: index of node with given ID, or 0 if ID not found
** Purpose: uses hash table to find index of node with given ID
**----------------------------------------------------------------
*/
{
return (hashtable_find(network->NodeHashTable, id));
}
int findlink(Network *network, char *id)
/*----------------------------------------------------------------
** Input: id = link ID
** Output: none
** Returns: index of link with given ID, or 0 if ID not found
** Purpose: uses hash table to find index of link with given ID
**----------------------------------------------------------------
*/
{
return (hashtable_find(network->LinkHashTable, id));
}
int findtank(Network *network, int index)
/*----------------------------------------------------------------
** Input: index = node index
** Output: none
** Returns: index of tank with given node id, or NOTFOUND if tank not found
** Purpose: for use in the deletenode function
**----------------------------------------------------------------
*/
{
int i;
for (i = 1; i <= network->Ntanks; i++)
{
if (network->Tank[i].Node == index) return i;
}
return NOTFOUND;
}
int findpump(Network *network, int index)
/*----------------------------------------------------------------
** Input: index = link ID
** Output: none
** Returns: index of pump with given link id, or NOTFOUND if pump not found
** Purpose: for use in the deletelink function
**----------------------------------------------------------------
*/
{
int i;
for (i = 1; i <= network->Npumps; i++)
{
if (network->Pump[i].Link == index) return i;
}
return NOTFOUND;
}
int findvalve(Network *network, int index)
/*----------------------------------------------------------------
** Input: index = link ID
** Output: none
** Returns: index of valve with given link id, or NOTFOUND if valve not found
** Purpose: for use in the deletelink function
**----------------------------------------------------------------
*/
{
int i;
for (i = 1; i <= network->Nvalves; i++)
{
if (network->Valve[i].Link == index) return i;
}
return NOTFOUND;
}
int findpattern(Network *network, char *id)
/*----------------------------------------------------------------
** Input: id = time pattern ID
** Output: none
** Returns: time pattern index, or -1 if pattern not found
** Purpose: finds index of time pattern given its ID
**----------------------------------------------------------------
*/
{
int i;
// Don't forget to include the "dummy" pattern 0 in the search
for (i = 0; i <= network->Npats; i++)
{
if (strcmp(id, network->Pattern[i].ID) == 0) return i;
}
return -1;
}
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
**----------------------------------------------------------------
*/
{
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->Nnodes; 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;
}
}
}
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
** 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
// index = index of the specified object
// comment = the object's comment string
// Output: error code
// Purpose: gets the comment string assigned to an object.
//----------------------------------------------------------------
{
char *currentcomment;
// Get pointer to specified object's comment
switch (object)
{
case NODE:
if (index < 1 || index > network->Nnodes) return 251;
currentcomment = network->Node[index].Comment;
break;
case LINK:
if (index < 1 || index > network->Nlinks) return 251;
currentcomment = network->Link[index].Comment;
break;
case TIMEPAT:
if (index < 1 || index > network->Npats) return 251;
currentcomment = network->Pattern[index].Comment;
break;
case CURVE:
if (index < 1 || index > network->Ncurves) return 251;
currentcomment = network->Curve[index].Comment;
break;
default:
strcpy(comment, "");
return 251;
}
// Copy the object's comment to the returned string
if (currentcomment) strcpy(comment, currentcomment);
else comment[0] = '\0';
return 0;
}
int setcomment(Network *network, int object, int index, const char *newcomment)
//----------------------------------------------------------------
// Input: object = a type of network object
// index = index of the specified object
// newcomment = new comment string
// Output: error code
// Purpose: sets the comment string of an object.
//----------------------------------------------------------------
{
char *comment;
switch (object)
{
case NODE:
if (index < 1 || index > network->Nnodes) return 251;
comment = network->Node[index].Comment;
network->Node[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
return 0;
case LINK:
if (index < 1 || index > network->Nlinks) return 251;
comment = network->Link[index].Comment;
network->Link[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
return 0;
case TIMEPAT:
if (index < 1 || index > network->Npats) return 251;
comment = network->Pattern[index].Comment;
network->Pattern[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
return 0;
case CURVE:
if (index < 1 || index > network->Ncurves) return 251;
comment = network->Curve[index].Comment;
network->Curve[index].Comment = xstrcpy(&comment, newcomment, MAXMSG);
return 0;
default: return 251;
}
}
int namevalid(const char *name)
//----------------------------------------------------------------
// Input: name = name used to ID an object
// Output: returns TRUE if name is valid, FALSE if not
// Purpose: checks that an object's ID name is valid.
//----------------------------------------------------------------
{
size_t n = strlen(name);
if (n < 1 || n > MAXID || strpbrk(name, " ;") || name[0] == '"') return FALSE;
return TRUE;
}
void getTmpName(char *fname)
//----------------------------------------------------------------
// Input: fname = file name string
// Output: an unused file name
// Purpose: creates a temporary file name with an "en" prefix
// or a blank name if an error occurs.
//----------------------------------------------------------------
{
#ifdef _WIN32
char* name = NULL;
// --- use Windows _tempnam function to get a pointer to an
// unused file name that begins with "en"
strcpy(fname, "");
name = _tempnam(NULL, "en");
if (name)
{
// --- copy the file name to fname
if (strlen(name) < MAXFNAME) strncpy(fname, name, MAXFNAME);
// --- free the pointer returned by _tempnam
free(name);
}
// --- for non-Windows systems:
#else
// --- use system function mkstemp() to create a temporary file name
/*
int f = -1;
strcpy(fname, "enXXXXXX");
f = mkstemp(fname);
close(f);
remove(fname);
*/
strcpy(fname, "enXXXXXX");
FILE *f = fdopen(mkstemp(fname), "r");
if (f == NULL) strcpy(fname, "");
else fclose(f);
remove(fname);
#endif
}
char *xstrcpy(char **s1, const char *s2, const size_t n)
//----------------------------------------------------------------
// Input: s1 = destination string
// s2 = source string
// n = maximum size of strings
// Output: none
// Purpose: like strcpy except for dynamic strings.
// Note: The calling program is responsible for ensuring that
// s1 points to a valid memory location or is NULL. E.g.,
// the following code will likely cause a segment fault:
// char *s;
// s = xstrcpy(s, "Some text");
// while this would work correctly:
// char *s = NULL;
// s = xstrcpy(s, "Some text");
//----------------------------------------------------------------
{
size_t n1 = 0, n2 = 0;
// Find size of source string
if (s2) n2 = strlen(s2);
if (n2 > n) n2 = n;
// Source string is empty -- free destination string
if (n2 == 0)
{
free(*s1);
*s1 = NULL;
return NULL;
}
// See if size of destination string needs to grow
if (*s1) n1 = strlen(*s1);
if (n2 > n1) *s1 = realloc(*s1, (n2 + 1) * sizeof(char));
// Copy the source string into the destination string
strncpy(*s1, s2, n2+1);
return *s1;
}
int strcomp(const char *s1, const char *s2)
/*---------------------------------------------------------------
** Input: s1 = character string
** s2 = character string
** Output: none
** Returns: 1 if s1 is same as s2, 0 otherwise
** Purpose: case insensitive comparison of strings s1 & s2
**---------------------------------------------------------------
*/
{
int i;
for (i = 0; UCHAR(s1[i]) == UCHAR(s2[i]); i++)
{
if (!s1[i + 1] && !s2[i + 1]) return 1;
}
return 0;
}
double interp(int n, double x[], double y[], double xx)
/*----------------------------------------------------------------
** Input: n = number of data pairs defining a curve
** x = x-data values of curve
** y = y-data values of curve
** xx = specified x-value
** Output: none
** Returns: y-value on curve at x = xx
** Purpose: uses linear interpolation to find y-value on a
** data curve corresponding to specified x-value.
** NOTE: does not extrapolate beyond endpoints of curve.
**----------------------------------------------------------------
*/
{
int k, m;
double dx, dy;
m = n - 1; // Highest data index
if (xx <= x[0]) return (y[0]); // xx off low end of curve
for (k = 1; k <= m; k++) // Bracket xx on curve
{
if (x[k] >= xx) // Interp. over interval
{
dx = x[k] - x[k - 1];
dy = y[k] - y[k - 1];
if (ABS(dx) < TINY) return (y[k]);
else return (y[k] - (x[k] - xx) * dy / dx);
}
}
return (y[m]); // xx off high end of curve
}
char *geterrmsg(int errcode, char *msg)
/*----------------------------------------------------------------
** Input: errcode = error code
** Output: none
** Returns: pointer to string with error message
** Purpose: retrieves text of error message
**----------------------------------------------------------------
*/
{
switch (errcode)
{
//#define DAT(code,string) case code: sprintf(msg, "%s", string); break;
#define DAT(code,string) case code: strcpy(msg, string); break;
#include "errors.dat"
#undef DAT
default:
strcpy(msg, "");
}
return (msg);
}
void errmsg(Project *pr, int errcode)
/*----------------------------------------------------------------
** Input: errcode = error code
** Output: none
** Purpose: writes error message to report file
**----------------------------------------------------------------
*/
{
char errmsg[MAXMSG + 1] = "";
if (errcode == 309) /* Report file write error - */
{ /* Do not write msg to file. */
}
else if (pr->report.RptFile != NULL && pr->report.Messageflag && errcode > 100)
{
sprintf(pr->Msg, "Error %d: %s", errcode, geterrmsg(errcode, errmsg));
writeline(pr, pr->Msg);
}
}
void writewin(void(*vp)(char *), char *s)
/*----------------------------------------------------------------
** Input: text string
** Output: none
** Purpose: passes character string to viewprog() in
** application which calls the EPANET DLL
**----------------------------------------------------------------
*/
{
char progmsg[MAXMSG + 1];
if (vp != NULL)
{
strncpy(progmsg, s, MAXMSG);
vp(progmsg);
}
}