Replicate the R code

Install the packages

Note: If you wish to replicate the R code below, then you

  1. will also need to download both files {https://www.ecoccs.com/R_Examples/Third_Creek_Water_Distribution_Project.inp and https://www.ecoccs.com/R_Examples/Third_Creek_Water_Distribution_Project.rpt} to the current working directory on your computer

&

  1. will need to copy and paste the following command in R first (to make sure you have all the packages and their dependencies) installed:
install.packages(c("install.load", "epanetReader", "pander"))
# install the packages and their dependencies



Load the packages

Note: If you wish to replicate the R code below, then you will need to copy and paste the following command in R first to make sure you have all the packages loaded:

# load the required packages
install.load::load_package("epanetReader", "pander")
# load needed packages using the load_package function from the install.load
# package (it is assumed that you have already installed these packages)


# set the pander options
panderOptions("table.continues", "")
panderOptions("table.caption.prefix", "")
panderOptions("missing", "")



March 2004 EPANET Analysis of the Third Creek Water Distribution system in Knoxville, Tennessee

The following document uses the .inp and .map file created in March 2004 for EPANET analysis of the Third Creek Water Distribution system in Knoxville, Tennessee. The analysis that follows is based on "Read .rpt file" and "Read .inp file" examples from epanetReader R package.



# inp is the .inp file name
inp <- "Third_Creek_Water_Distribution_Project.inp"


# read the network file into R
n1s <- read.inp(inp)

# Network summary
summary(n1s)
## $Title
## [1] "EPANET Third Creek Water Distribution Project - Irucka Embry (March 2004)"
## 
## $entryCounts
##             Number
## Junctions      117
## Tanks            2
## Reservoirs       1
## Pipes          162
## Pumps            3
## Coordinates    120
## Labels           3
## 
## attr(,"class")
## [1] "summary.epanet.inp"
# Names of the elements of the R object n1s
names(n1s)
##  [1] "Title"       "Junctions"   "Tanks"       "Reservoirs"  "Pipes"      
##  [6] "Pumps"       "Valves"      "Demands"     "Patterns"    "Curves"     
## [11] "Controls"    "Rules"       "Energy"      "Status"      "Emitters"   
## [16] "Quality"     "Sources"     "Reactions"   "Mixing"      "Times"      
## [21] "Report"      "Options"     "Coordinates" "Vertices"    "Labels"     
## [26] "Backdrop"    "Tags"
# Network summary of the junctions
summary(n1s$Junctions)
ID Elevation Demand Pattern
Length:117 Min. : 935 Min. : 0.00 Mode:logical
Class :character 1st Qu.:1020 1st Qu.: 8.91 NA's:117
Mode :character Median :1060 Median : 15.10 NA
NA Mean :1067 Mean : 51.74 NA
NA 3rd Qu.:1100 3rd Qu.: 29.49 NA
NA Max. :1260 Max. :3500.00 NA
# Network summary of the pipes
summary(n1s$Pipes)
ID Node1 Node2 Length Diameter Roughness MinorLoss Status
Length:162 Min. : 8.00 Min. : 8.00 Min. : 42.5 Min. : 2.000 Min. :100 Min. :0 Open:162
Class :character 1st Qu.: 41.00 1st Qu.: 35.25 1st Qu.:1075.8 1st Qu.: 6.000 1st Qu.:100 1st Qu.:0 NA
Mode :character Median : 72.50 Median : 75.50 Median :1735.2 Median : 8.000 Median :100 Median :0 NA
NA Mean : 77.89 Mean : 76.01 Mean :2067.2 Mean : 8.247 Mean :100 Mean :0 NA
NA 3rd Qu.:105.00 3rd Qu.:106.00 3rd Qu.:2823.0 3rd Qu.:12.000 3rd Qu.:100 3rd Qu.:0 NA
NA Max. :209.00 Max. :207.00 Max. :7760.0 Max. :16.000 Max. :100 Max. :0 NA
# Network plot
plot(n1s, legend.locn = "topleft", plot.labels = TRUE)

# read the network report file into R
rpt <- "Third_Creek_Water_Distribution_Project.rpt"

#
n1r <- read.rpt(rpt)

#
summary(n1r)
## Contains node results for  25 time steps 
## 
## Summary of Junction Results: 
##      Demand           Pressure         Quality 
##  Min.   :   0.00   Min.   :  4.54   Min.   :0  
##  1st Qu.:   8.91   1st Qu.: 78.06   1st Qu.:0  
##  Median :  15.10   Median : 96.08   Median :0  
##  Mean   :  51.74   Mean   : 97.48   Mean   :0  
##  3rd Qu.:  29.49   3rd Qu.:115.03   3rd Qu.:0  
##  Max.   :3500.00   Max.   :170.20   Max.   :0  
## 
## Summary of Tank Results:
##      Demand          Pressure        Quality 
##  Min.   :-484.1   Min.   :0.000   Min.   :0  
##  1st Qu.:-197.3   1st Qu.:0.000   1st Qu.:0  
##  Median :-108.6   Median :0.740   Median :0  
##  Mean   :-129.5   Mean   :1.970   Mean   :0  
##  3rd Qu.:   0.0   3rd Qu.:3.855   3rd Qu.:0  
##  Max.   :   0.0   Max.   :7.060   Max.   :0  
## 
## Contains link results for  25 time steps 
## 
## Summary of Pipe Results:
##       Flow             Velocity        Headloss    
##  Min.   :-897.100   Min.   :0.000   Min.   : 0.00  
##  1st Qu.:  -4.827   1st Qu.:0.130   1st Qu.: 0.02  
##  Median :  22.190   Median :0.390   Median : 0.14  
##  Mean   :  66.619   Mean   :0.707   Mean   : 1.35  
##  3rd Qu.: 107.060   3rd Qu.:1.110   3rd Qu.: 1.21  
##  Max.   :1869.230   Max.   :5.010   Max.   :21.96  
## 
## Energy Usage:
##   Pump usageFactor avgEfficiency kWh_per_Mgal avg_kW peak_kW dailyCost
## 1  230         100            75      1581.98 196.42  200.47         0
## 2  231         100            75      1581.98  96.19  107.74         0
## 3  232         100            75      1581.98 256.76  259.16         0
#
names(n1r)
## [1] "nodeResults" "linkResults" "energyUsage"
# Results for a chosen time period can be retrieved using the subset function
pander(head(subset(n1r$nodeResults, Timestamp == "19:00")))
  ID Demand Head Pressure Quality note Timestamp
2281 8 26.73 1313 163.9 0 19:00
2282 9 5.44 1311 152.1 0 19:00
2283 10 10.89 1310 156.1 0 19:00
2284 11 40.77 1310 155.8 0 19:00
2285 12 26.09 1299 146.7 0 19:00
2286 13 29.67 1307 89.91 0 19:00
  timeInSeconds nodeType
2281 68400 Junction
2282 68400 Junction
2283 68400 Junction
2284 68400 Junction
2285 68400 Junction
2286 68400 Junction
# time series plot for a nodal value
plot(Pressure ~ timeInSeconds, data = subset(n1r$nodeResults, ID == "22"))

# Plotting the epanet.rpt object itself gives a map
plot(n1r, n1s, Timestep = "19:00", legend1.locn = "bottomright")



Works Cited

epanetReader package



Donations accepted with Liberapay

If you would like to contribute to the continued development of Irucka Embry’s R packages and/or Irucka Embry’s R Examples, please feel free to donate via the link below:

Please feel free to review Irucka Embry (iaembry)’s profile on Liberapay.