Learn R Programming

epanet2toolkit

epanet2toolkit is an R package for simulating water networks using Epanet. The package provides functions from the Epanet programmer's toolkit as R functions so that basic or customized simulations can be carried out from R. The package uses Epanet version 2.2 from Open Water Analytics.
This version of the R package supports much of the extensive new functionality in EPANET version 2.2.

In addition to this readme page and the package manual, the paper An R package for EPANET simulations is published in Environmental Modelling & Software and is also available as a preprint. The conference paper Water demand and network modelling with R gives some more in depth examples.

Installation

The package can be installed from CRAN.

install.packages("epanet2toolkit")

Or, install the development version from github.com

devtools::install_github("bradleyjeck\epanet2toolkit")

Getting Started

After installation, the package needs to be loaded for use.

library(epanet2toolkit)

A brief introduction is available in the package help and each function has its own help page. Functions provided by the package map directly to functions in Epanet's API and integrate with the R system for handling exceptions. Thus the function ENgetnodeindex( nodeID ) provides the index corresponding to a node ID, or raises an error if such a node ID does not exist.

?epanet2toolkit
?ENgetnodeindex

Running a Full Simulation

The function ENepanet() runs a full simulation and writes the results to a file. A file of simulation results can be analyzed using the package epanetReader.

ENepanet("Net1.inp", "Net1.rpt")

Querying Network Properties

Characteristics of a network can be examined using package functions. Note that Epanet needs to be opened for use and should be closed when the analysis finishes.

ENopen("Net1.inp", "Net1.rpt")
ENgetflowunits()
ENgetqualtype()
ENgetcount("EN_NODECOUNT") 
ENgetcount("EN_LINKCOUNT")
ENgetnodeid(1)
ENgetlinkid(1)
ENclose()

Example Programs

The US EPA website for Epanet includes example programs for a hydrant rating curve and chlorine dosage analysis. An implementation of those programs using R and epanet2toolkit are included with the package as tests:

Programming Notes for Package Developers

Epanet provides a collection of functions known as the programmer's toolkit or API for building customized simulations. epanet2toolkit makes these functions callable from R.

Functions in the Epanet API return an integer error code and provide requested values by reference.

int ENgetnodeindex(char *nodeID, int *nodeindex); 

A C toolkit function such as ENgetnodeindex takes two arguments: the node ID, and a pointer to an integer variable where the requested nodeindex is stored. The function returns an integer error code. Using the C function requires allocating an integer for storing the requested node index and passing a pointer to the storage location to the function. Checking the returned error code is optional, but is good practice.

epanet2toolkit integrates Epanet into R by providing two layers of wrapping. First, the existing functions of Epanet's C API are wrapped by new C functions with return type 'void' or 'SEXP' so that they can be called from R. Second, new R functions are provided to call these new C functions. The R functions provide some argument checking and also check the error codes returned by Epanet.

Copy Link

Version

Install

install.packages('epanet2toolkit')

Monthly Downloads

414

Version

1.0.8

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bradley Eck

Last Published

December 16th, 2024

Functions in epanet2toolkit (1.0.8)

ENgetcurveindex

Retrieves the index of a curve given its ID name.
ENgetcurvelen

Retrieves the number of points in a curve.
ENgetbasedemand

Gets the base demand for one of a node's demand categories.
ENcopyreport

Copies the current contents of a project's report file to another file
ENcloseQ

Close water quality analysis and free allocated memory
ENgetdemandindex

Retrieves the index of a node's named demand category
ENgetdemandmodel

Get type of demand model in use and its parameters
ENgetaveragepatternvalue

Get average of all time factors in a pattern
ENgetflowunits

Retrieve a code number indicating the units used to express all flow rates.
ENgetheadcurveindex

Retrieves index of head curve used by a pump
ENgetcount

Get number of network elements.
ENgetcurveid

Retrieves the ID name of a curve given its index.
ENgeterror

Returns the text of an error message generated by an error code
ENgetelseaction

Gets the properties of an ELSE action in a rule-based control.
ENgetnodeid

Retrieve the ID label a node.
ENgetnodevalue

Retrieve node parameter value.
ENgetlinkvalue

Retrieve parameter value for a link
ENgetnumdemands

Get number of demands for a junction node
ENgetlinktype

Retrieve the type code for a link
ENgetlinkid

Retrieve the ID label of a link
ENgetlinknodes

Retrieve the index of the end nodes of a link
ENgetcontrol

ENgetcontrol
ENgetcoord

Get coordinates for a node
ENgetpumptype

Retrieves type of head curve used by a pump
ENgetrule

Retrieves summary information about a rule-based control.
ENgetlinkindex

Retrieve the index of a link
ENgetqualinfo

Get quality analysis information
ENgetruleID

Gets the ID name of a rule-based control given its index.
ENgetcurvevalue

Retrieves the value of a single data point for a curve.
ENgetdemandname

Retrieves the name of a node's demand category.
ENgetqualtype

Retrieve the type of water quality analysis called for.
ENgetpatternid

Retrieve the ID label a time pattern
ENgetoption

Retrieve the value of an analysis option.
ENgetdemandpattern

Gets the base demand for one of a node's demand categories.
ENgetresultindex

Retrieves the order in which a node or link appears in an output file.
ENopen

Open the EPANET Toolkit.
ENinitH

Initialize hydraulic engine
ENinit

ENinit
ENgetvertexcount

Get number of vertices for a link
ENopenH

Open hydraulics analysis system.
ENnextH

determine the next hydraulic step
ENsetdemandname

Sets the name of a node's demand category.
ENnextQ

Advances WQ simulation to start of the next hydraulic time period.
ENinitQ

Initialize water quality analysis
ENgetpremise

Gets the properties of a premise in a rule-based control.
ENsetbasedemand

Sets the base demand for one of a node's demand categories.
ENsetcontrol

Set the parameters of a simple control statement
ENgetstatistic

Analysis convergence statistics.
ENgetpatternvalue

Retrieve the multiplier factor for a specific time period
ENgetthenaction

Gets properties of THEN action in rule-based control
ENsetdemandpattern

Sets the index of a time pattern used for one of a node's demand categories.
ENrunQ

Computs WQ results at current time .
ENsaveH

ENsaveH Saves hydraulic results to binary file
ENsetdemandmodel

Sets the type of demand model to use and its parameters.
ENsetcurvevalue

Sets the value of a single data point for a curve.
ENsetnodeid

Changes the ID name of a node
ENsetnodevalue

Set the parameter value for a node.
ENsetpatternid

Change the ID name of a time pattern given its index.
ENsetheadcurveindex

Sets index of head curve used by a pump
ENsetpatternvalue

set pattern value
ENsolveH

ENsolveH
ENsolveQ

Solve network water quality for all time periods
ENsetstatusreport

Sets the level of hydraulic status reporting.
ENsetlinkid

Change the ID of a link
ENsetlinknodes

Set the indexes of a link's start- and end-nodes
ENsetrulepriority

Sets the priority of a rule-based control.
ENgetversion

Retrieve the current version number of the EPANET Toolkit.
ENgetvertex

Get vertex coordinates
ENresetreport

Resets a project's report options to their default values.
ENsetjuncdata

Sets properties for a junction
ENsetthenaction

Set properties of THEN action in a rule-based control
ENsettankdata

Sets properties for a tank
ENsetqualtype

Set the type of water quality analysis called for.
ENsetreport

Processes a reporting format command.
epanet2toolkit-package

epanet2toolkit: Call 'EPANET' Functions to Simulate Pipe Networks
ENgetnodetype

Retrieve the node-type code
ENgetnodeindex

Retrieve the index of a node
ENgetpatternindex

Retrieve the index a time pattern.
ENrunH

run hydraulics engine
ENsetcoord

Set coordinates for a node
ENsetcurveid

Changes the ID name of a data curve given its index.
ENgetpatternlen

Retrieve the number of time periods in a time pattern.
ENgettitle

ENgettitle
ENopenQ

Sets up for Water Quality analysis
ENreport

Write simulation report to the report file
ENgettimeparam

Get the value of one or more specific analysis time parameters.
ENsetlinktype

Change a link's type
ENsavehydfile

Saves temporary hydraulics file to disk
ENsetpremisevalue

Sets the value in a premise of a rule-based control
ENsaveinpfile

ENaveinpfile Saves current data to "INP" formatted text file.
ENsetpremisestatus

Sets the status being compared to in a premise of a rule-based control
ENsettimeparam

Set the value of a time parameter.
ENsetvertices

Set a link's vertices
ENsetoption

Set the value of a particular analysis option.
ENsetlinkvalue

Set a parameter value for a link
ENsetpattern

Set all of the multiplier factors for a specific time pattern.
ENsetelseaction

Set properties of an ELSE action in a rule-based control
ENsetpremise

Sets the properties of a premise in a rule-based control.
ENstepQ

Advances WQ simulation one water quality time step.
ENsetflowunits

Sets flow units.
ENsetpremiseindex

Sets the index of an object in a premise of a rule-based control
ENusehydfile

Uses previously saved binary hydraulics file to supply a project's hydraulics.
ENaddrule

Adds a new rule-based control to a project
ENaddpattern

Add a new time pattern
ENaddnode

Adds a new node
ENclose

Close down the EPANET Toolkit system.
ENaddcontrol

Add a new simple control
ENcloseH

close hydraulics engine
ENadddemand

Appends a new demand to a junction node demands list.
ENaddlink

Add a link to the network
ENaddcurve

Adds a new data curve to a project.
ENclearreport

Clears the contents of a project's report file.
ENdeletecurve

Deletes a data curve from a project
ENdeletecontrol

Deletes an existing simple control
ENepanet

ENepanet
ENdeleterule

Deletes an existing rule-based control
ENdeletedemand

Delete a demand from a junction node
ENdeletelink

Delete a link from the project.
ENdeletepattern

Delete a new time pattern
ENdeletenode

Deletes a node
ENgetcurvetype

Retrieves a curve's type