Learn R Programming

⚠️There's a newer version (1.0.8) of this package.Take me there.

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

310

Version

1.0.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bradley Eck

Last Published

November 6th, 2023

Functions in epanet2toolkit (1.0.4)

ENgetcurvelen

Retrieves the number of points in a curve.
ENgetdemandmodel

Get type of demand model in use and its parameters
ENdeletecontrol

Deletes an existing simple control
ENgetheadcurveindex

Retrieves index of head curve used by a pump
ENgetflowunits

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

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

Retrieves a curve's type
ENgetcurvevalue

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

Retrieve the index of the end nodes of a link
ENgetdemandname

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

Deletes a data curve from a project
ENgetlinktype

Retrieve the type code for a link
ENgetpumptype

Retrieves type of head curve used by a pump
ENgetlinkvalue

Retrieve parameter value for a link
ENgetqualinfo

Get quality analysis information
ENgetpatternindex

Retrieve the index a time pattern.
ENgetnodeid

Retrieve the ID label a node.
ENgetpatternlen

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

ENgetcontrol
ENgetvertex

Get vertex coordinates
ENgetvertexcount

Get number of vertices for a link
ENgetpatternvalue

Retrieve the multiplier factor for a specific time period
ENgetpremise

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

Analysis convergence statistics.
ENgetversion

Retrieve the current version number of the EPANET Toolkit.
ENsetcoord

Set coordinates for a node
ENopenH

Open hydraulics analysis system.
ENopen

Open the EPANET Toolkit.
ENsetnodevalue

Set the parameter value for a node.
ENsetnodeid

Changes the ID name of a node
ENinit

ENinit
ENgetaveragepatternvalue

Get average of all time factors in a pattern
ENgetcount

Get number of network elements.
ENdeleterule

Deletes an existing rule-based control
ENgetcurveid

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

ENepanet
ENsetcurveid

Changes the ID name of a data curve given its index.
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.
ENsetpremise

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

Advances WQ simulation one water quality time step.
ENgetcoord

Get coordinates for a node
ENsetjuncdata

Sets properties for a junction
ENsetpremisestatus

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

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

Sets index of head curve used by a pump
ENsettankdata

Sets properties for a tank
ENgetnodeindex

Retrieve the index of a node
ENgeterror

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

Retrieve the node-type code
ENgetelseaction

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

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

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

Gets properties of THEN action in rule-based control
ENreport

Write simulation report to the report file
ENsetdemandpattern

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

Sets up for Water Quality analysis
ENsetdemandname

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

Retrieve the ID label of a link
ENsetbasedemand

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

run hydraulics engine
ENsetcontrol

Set the parameters of a simple control statement
ENsetlinktype

Change a link's type
ENgetoption

Retrieve the value of an analysis option.
ENsetqualtype

Set the type of water quality analysis called for.
ENgetbasedemand

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

Retrieve the ID label a time pattern
ENgetnumdemands

Get number of demands for a junction node
ENgetnodevalue

Retrieve node parameter value.
ENgetrule

Retrieves summary information about a rule-based control.
ENgetlinkindex

Retrieve the index of a link
ENgetqualtype

Retrieve the type of water quality analysis called for.
ENgetruleID

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

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

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

Saves temporary hydraulics file to disk
ENsetreport

Processes a reporting format command.
ENinitH

Initialize hydraulic engine
ENrunQ

Computs WQ results at current time .
ENsaveH

ENsaveH Saves hydraulic results to binary file
ENgettitle

ENgettitle
ENsetlinkvalue

Set a parameter value for a link
epanet2toolkit

epanet2toolkit
ENinitQ

Initialize water quality analysis
ENnextH

determine the next hydraulic step
ENsetrulepriority

Sets the priority of a rule-based control.
ENsetstatusreport

Sets the level of hydraulic status reporting.
ENsaveinpfile

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

Change the ID of a link
ENsetlinknodes

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

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

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

Set the value of a particular analysis option.
ENsettimeparam

Set the value of a time parameter.
ENsetpattern

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

Set a link's vertices
ENsetpatternid

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

set pattern value
ENsetelseaction

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

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

Sets flow units.
ENsolveH

ENsolveH
ENsolveQ

Solve network water quality for all time periods
ENdeletedemand

Delete a demand from a junction node
ENcloseH

close hydraulics engine
ENclose

Close down the EPANET Toolkit system.
ENaddcontrol

Add a new simple control
ENaddcurve

Adds a new data curve to a project.
ENaddrule

Adds a new rule-based control to a project
ENclearreport

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

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

Add a link to the network
ENdeletepattern

Delete a new time pattern
ENgetcurveindex

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

Delete a link from the project.
ENdeletenode

Deletes a node
ENgetdemandindex

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

Adds a new node
ENaddpattern

Add a new time pattern
ENcloseQ

Close water quality analysis and free allocated memory
ENcopyreport

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