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.

Although EPANET version 2.2 has substantial functionality beyond 2.1 this version of the R package provides the v2.1 functionality using the v2.2 code base. Only the "legacy" style functions of EPANET are supported.

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

Install the latest release 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

323

Version

0.5.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bradley Eck

Last Published

April 22nd, 2021

Functions in epanet2toolkit (0.5.1)

ENgetcoord

Get coordinates for a node
ENgetversion

Retrieve the current version number of the EPANET Toolkit.
ENgettimeparam

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

Retrieve the index of the end nodes of a link
ENgetqualinfo

Get quality analysis information
ENopen

Open the EPANET Toolkit.
ENgetlinktype

Retrieve the type code for a link
ENopenH

Open hydraulics analysis system.
ENgetqualtype

Retrieve the type of water quality analysis called for.
ENgetpatternid

Retrieve the ID label a time pattern
ENgetpatternindex

Retrieve the index a time pattern.
ENsolveH

ENsolveH
ENsettimeparam

Set the value of a time parameter.
ENgetlinkvalue

Retrieve parameter value for a link
ENsetqualtype

Set the type of water quality analysis called for.
ENsetpatternvalue

set pattern value
epanet2toolkit

epanet2toolkit
ENsetcontrol

Set the parameters of a simple control statement
ENsetcoord

Set coordinates for a node
ENgetnodetype

Retrieve the node-type code
ENgetnodeindex

Retrieve the index of a node
ENgetpatternlen

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

Retrieve the ID label a node.
ENsetlinkvalue

Set a parameter value for a link
ENinitH

Initialize hydraulic engine
ENopenQ

Sets up for Water Quality analysis
ENgetpatternvalue

Retrieve the multiplier factor for a specific time period
ENgetoption

Retrieve the value of an analysis option.
ENgetnodevalue

Retrieve node parameter value.
ENsetnodevalue

Set the parameter value for a node.
ENnextQ

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

determine the next hydraulic step
ENrunH

run hydraulics engine
ENsolveQ

Solve network water quality for all time periods
ENrunQ

Computs WQ results at current time .
ENstepQ

Advances WQ simulation one water quality time step.
ENreport

Write simulation report to the report file
ENsaveH

ENsaveH Saves hydraulic results to binary file
ENinitQ

Initialize water quality analysis
ENsaveinpfile

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

Set the value of a particular analysis option.
ENsetpattern

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

close hydraulics engine
ENgetlinkindex

Retrieve the index of a link
ENcloseQ

Close water quality analysis and free allocated memory
ENgetcontrol

ENgetcontrol
ENgetcount

Get number of network elements.
ENclose

Close down the EPANET Toolkit system.
ENgetlinkid

Retrieve the ID label of a link
ENepanet

ENepanet
ENgetflowunits

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