-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathepanet_ga_4_leak_detection.R
199 lines (150 loc) · 7.29 KB
/
epanet_ga_4_leak_detection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#...............................................................................
# 1. - Initialize Session ####
#...............................................................................
cat("\014")
rm(list=ls())
# Installs libraries
library(tidyverse)
library(dplyr)
library(zoo)
library(lubridate)
library(epanetReader)
library(epanet2toolkit)
library(ggfortify)
library(ggthemes)
library(scales)
library(purrr)
library(visNetwork)
# Initialize params
params <- list(base_network = "base_dma_01",
new_network = "base_dma_w_leaks",
functs_name = "epanet_api_functions",
inlet_valves = "PRV_",
jt_to_analyze = "^JT_0[A-K]", # RegExp
pipe_to_analyze = "PS_", # RegExp
emitter_coeff = 10000,
leak_rate = 0.01, # Percentage of the network with leaks
demad_factor = list( names =c( "wd_spring_summer",
"hw_spring_summer",
"wd_summer_break",
"hw_summer_break",
"wd_fall_winter",
"hw_fall_winter"),
factors = c( 0.92, 1.00, 1.09,
0.81, 0.66, 0.95)),
work_folders = list( dir_work = getwd(),
dir_report = file.path(getwd(),"reports"),
dir_data = file.path(getwd(),"data"),
dir_bin = file.path(getwd(),"reports"),
dir_func = file.path(getwd(),"func")))
f_names <- list( base_file_inp = file.path(params$work_folders$dir_data,
paste0(params$base_network,".inp")),
base_file_report = file.path(params$work_folders$dir_report,
paste0(params$base_network,".rpt")),
new_file_inp = file.path(params$work_folders$dir_data,
paste0(params$new_network,".inp")),
new_file_report = file.path(params$work_folders$dir_report,
paste0(params$new_network,".rpt")),
file_func = file.path(params$work_folders$dir_func,
paste0(params$functs_name,".R")))
# Load Functions Standard
source(f_names$file_func)
#...............................................................................
# 2. GENERATE THE LEAKAGE IN A NEW NETWORK ####
#...............................................................................
# Read network information from an *.inp
net_input_01 <- read.inp(f_names$base_file_inp)
#if(!file.exists(f_names$new_file_inp)){
net_input_01 <- gen_network_w_leaks(net_input_01,
params$leak_rate,
params$jt_to_analyze )
write.inp(net_input_01, f_names$new_file_inp)
# }
rm(net_input_01)
#...............................................................................
# 3. Running a Full Simulation ####
# The function ENepanet() runs a full simulation and
# writes the results to a file.
#...............................................................................
ENepanet(f_names$base_file_inp, f_names$base_file_report)
ENepanet(f_names$new_file_inp, f_names$new_file_report)
net_input_01 <- read.inp(f_names$new_file_inp)
report_base <- read.rpt(f_names$base_file_report)
report_leack <- read.rpt(f_names$new_file_report)
# New Leaks (EMITTERS)
emitters <- as.tibble(net_input_01$Emitters)
# Select nodes
nodes <- eval_nodes (report_base,
node_type = "",
id_nodes = "",
group = TRUE, standardize = TRUE) %>%
select(ID,p_median) %>%
left_join(emitters, by = "ID")
# PIPES AND FLOW
pipes <- strc_pipes(net_input_01, "", TRUE)
pipes_f_base <- eval_pipes ( report_base,
link_type ="",
id_pipes = "",
inlet_links ="",
value = "Flow",
group = TRUE,
standardize = FALSE)
pipes_f_leack <- eval_pipes ( report_leack,
link_type ="",
id_pipes = "",
inlet_links ="",
value = "Flow",
group = TRUE,
standardize = FALSE)
pipes <- full_join( pipes, pipes_f_base, by = "ID")
pipes <- full_join( pipes, pipes_f_leack, by = "ID")
rm(pipes_f_base, pipes_f_leack)
rm(report_base, report_leack, net_input_01)
# change from_node and to_node in function of the Flow direction
#.... New function ??
# ->
df <- pipes
df$from_node[pipes$f_median.y < 0 ] <- pipes$to_node [pipes$f_median.y < 0]
df$to_node [pipes$f_median.y < 0 ] <- pipes$from_node[pipes$f_median.y < 0]
df <- df %>% mutate(d_flow = abs(f_median.y - f_median.x))
maxs <- apply(df[4:6], 2, max)
mins <- apply(df[4:6], 2, min)
scaled <- as.data.frame(scale(df[4:6], center = mins, scale = maxs - mins)*100)
names(scaled) <- c("scaled_flow.x","scaled_flow.y", "scaled_d_flow")
df <- as.tibble(cbind(df[,1:5],scaled)) %>%
mutate(relative_flow_change = abs(scaled_flow.y-scaled_flow.x))
df <- left_join(df, emitters, by = c("from_node" = "ID"))
df <- left_join(df, emitters, by = c("to_node" = "ID"))
df <- as.tibble(df) %>% arrange(desc(relative_flow_change))
pipes <- as.tibble(df)
rm(scaled,df)
# suspicious nodes
l <- round(length(pipes$ID)*params$leak_rate,0)
susp_nodes <- as.tibble(c( pipes$from_node[1:l], pipes$to_node[1:l])) %>%
count(value, sort = TRUE )
susp_pipes <- pipes[1:l,] %>% select(ID, from_node, to_node)
#-------------------------------------------------------------------------------
# NETWORK VIZUALIZATION
#-------------------------------------------------------------------------------
nodes <- nodes %>%
mutate(color = ifelse(is.na(FlowCoef),"grey","red"))
# generate data for the visNetwork
nodes <- data.frame( id = nodes$ID,
value = nodes$p_median,
color = nodes$color,
shadow = TRUE)
edges <- data.frame( from = pipes$from_node,
to = pipes$to_node,
value = pipes$relative_flow_change,
arrows = "to",
shadow = TRUE)
# visNetwork
visNetwork(nodes, edges ,
main = "A really simple example",
width = "100%") %>%
visLayout(randomSeed = 12) %>%
visIgraphLayout()
#...............................................................................
# glimpse(net_report_01)
# git push origin master
#...............................................................................