The following topics briefly describe how to accomplish some basic tasks using the OWA-EPANET Toolkit in C/C++ code. See the Examples topic for code listings of complete applications of the Toolkit.
Before any use is made of the Toolkit, a project and its handle must be created. After all processing is completed the project should be deleted. See the code snippet below:
All of the Toolkit functions return an error/warning code. A 0 indicates that the function ran successfully. A number greater than 0 but less than 100 indicates that a warning condition was generated while a number higher than 100 indicates that the function failed.
The meaning of specific error and warning codes are listed in the Error Codes and Warning Codes sections of this guide. The Toolkit function EN_geterror can be used to obtain the text of a specific error/warning code. The following example uses a macro named ERRCODE
along with a variable named errcode
to execute Toolkit commands only if no fatal errors have already been detected:
Once a project is created there are two ways in which it can be populated with data. The first is to use the EN_open function to load an EPANET-formatted Input File that provides a description of the network to be analyzed. This function should be called immediately after a project is created. It takes as arguments the name of the input file to open and the names of a report file and a binary output file, both of which are optional. Here is a code sample showing this approach:
After an input file has been loaded in this fashion the resulting network can have objects added or deleted, and their properties set using the various Toolkit functions .
The second method for supplying network data to a project is to use the Toolkit's functions to add objects and to set their properties via code. In this case the EN_init function should be called immediately after creating a project, passing in the names of a report and binary output files (both optional) as well as the choices of flow units and head loss formulas to use. After that the various EN_add functions, such as EN_addnode, EN_addlink, EN_addpattern, EN_addcontrol, etc., can be called to add new objects to the network. Here is a partial example of constructing a network from code:
See the Network Building Example for a more complete example. The labels used to name objects cannot contain spaces, semi-colons, or double quotes nor exceed EN_MAXID characters in length. While adding objects their properties can be set as described in the next section. Attemtping to change a network's structure by adding or deleting nodes and links while the Toolkit's hydraulic or water quality solvers are open will result in an error condition.
The Toolkit contains several functions for retrieving and setting the properties of a network's objects and its analysis options. The names of retrieval functions all begin with EN_get (e.g., EN_getnodevalue, EN_getoption, etc.) while the functions used for setting parameter values begin with EN_set (e.g., EN_setnodevalue, EN_setoption, etc.).
Most of these functions use an index number to refer to a specific network component (such as a node, link, time pattern or data curve). This number is simply the position of the component in the list of all components of similar type (e.g., node 10 is the tenth node, starting from 1, in the network) and is not the same as the ID label assigned to the component. A series of functions exist to determine a component's index number given its ID label (see EN_getnodeindex, EN_getlinkindex, EN_getpatternindex, and EN_getcurveindex). Likewise, functions exist to retrieve a component's ID label given its index number (see EN_getlinkid, EN_getnodeid, EN_getpatternid, and EN_getcurveid). The EN_getcount function can be used to determine the number of different components in the network. Be aware that a component's index can change as elements are added or deleted from the network. The EN_addnode and EN_addlink functions return the index of the newly added node or link as a convenience for immediately setting their properties.
The code below is an example of using the property retrieval and setting functions. It changes all links with diameter of 10 inches to 12 inches.
There are two ways to use the Toolkit to run a hydraulic analysis:
Method 1 is useful if you only want to run a single hydraulic analysis, perhaps to provide input to a water quality analysis. With this method hydraulic results are always saved to an intermediate hydraulics file at every time step.
Method 2 must be used if you need to access results between time steps or if you wish to run many analyses efficiently. To accomplish the latter, you would make only one call to EN_openH to begin the process, then make successive calls to EN_initH - EN_runH - EN_nextH to perform each analysis, and finally call EN_closeH to close down the hydraulics system. An example of this is shown below (calls to EN_nextH are not needed because we are only making a single period analysis in this example).
As with a hydraulic analysis, there are two ways to carry out a water quality analysis:
The second option can either be carried out after a hydraulic analysis has been run or simultaneously as hydraulics are being computed. Example code for these two alternatives is shown below:
The EN_getnodevalue and EN_getlinkvalue functions can also be used to retrieve the results of hydraulic and water quality simulations. The computed parameters (and their Toolkit codes) that can be retrieved are as follows:
For Nodes: | For Links: |
---|---|
EN_DEMAND (demand) | EN_FLOW (flow rate) |
EN_DEMANDDEFICIT (demand deficit) | EN_VELOCITY (flow velocity) |
EN_HEAD (hydraulic head) | EN_HEADLOSS (head loss) |
EN_PRESSURE (pressure) | EN_STATUS (link status) |
EN_TANKLEVEL (tank water level) | EN_SETTING (pump speed or valve setting) |
EN_TANKVOLUME (tank water volume) | EN_ENERGY (pump energy usage) |
EN_QUALITY (water quality) | EN_PUMP_EFFIC (pump efficiency) |
EN_SOURCEMASS (source mass inflow) |
The following code shows how to retrieve the pressure at each node of a network after each time step of a hydraulic analysis (writetofile
is a user-defined function that will write a record to a file):
The Toolkit has some built-in capabilities to produce formatted output results saved to a file. More specialized reporting needs can always be handled by writing custom code.
The EN_setreport function is used to define the format of a report while the EN_report function actually writes the report. The latter should be called only after a hydraulic or water quality analysis has been made. An example of creating a report that lists all nodes where the pressure variation over the duration of the simulation exceeds 20 psi is shown below: