-
Notifications
You must be signed in to change notification settings - Fork 3
/
network_visualization.R
115 lines (86 loc) · 4.02 KB
/
network_visualization.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
#...............................................................................
# 1. - Initialize Session ####
#...............................................................................
cat("\014")
rm(list=ls())
# Installs libraries
library(tidyverse)
library(epanetReader)
library(epanet2toolkit)
library(visNetwork)
library(igraph)
# 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
)
# initialize files paths and files
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(work_folders$dir_data,
paste0(params$base_network,".inp")),
base_file_report = file.path(work_folders$dir_report,
paste0(params$base_network,".rpt")),
new_file_inp = file.path(work_folders$dir_data,
paste0(params$new_network,".inp")),
new_file_report = file.path(work_folders$dir_report,
paste0(params$new_network,".rpt")),
file_func = file.path(work_folders$dir_func,
paste0(params$functs_name,".R")))
# Load Functions Standard
source(f_names$file_func)
#...............................................................................
# 2. Read network information from an *.inp
#...............................................................................
base_input <- read.inp(f_names$base_file_inp)
#...............................................................................
# 3. Running a Full Simulation ####
#...............................................................................
ENepanet(f_names$base_file_inp, f_names$base_file_report)
base_report <- read.rpt(f_names$base_file_report)
# Select nodes
nodes <- eval_nodes (base_report,
node_type = "",
id_nodes = "",
group = TRUE, standardize = TRUE) %>%
select(ID,p_median)
# Select pipes
pipes <- strc_pipes(base_input, "", TRUE)
pipes_f_base <- eval_pipes ( base_report,
value = "Flow",
group = TRUE,
standardize = FALSE)
pipes <- full_join( pipes, pipes_f_base, by = "ID")
# change from_node and to_node in function of the Flow direction
df <- pipes
df$from_node[pipes$f_median < 0 ] <- pipes$to_node[pipes$f_median < 0]
df$to_node [pipes$f_median < 0 ] <- pipes$from_node[pipes$f_median < 0]
df$f_median [pipes$f_median < 0 ] <- abs(pipes$f_median[pipes$f_median < 0])
# standardize df
l <- length(df)
maxs <- max(df$f_median)
mins <- min(df$f_median)
scaled <- scale( df$f_median,
center = mins,
scale = maxs - mins)*100
df$f_median <- scaled
# generate data for the visNetwork
nodes <- data.frame( id = nodes$ID,
value = nodes$p_median,
shadow = TRUE)
edges <- data.frame( from = df$from_node,
to = df$to_node,
value = df$f_median,
arrows = "to",
shadow = TRUE)
# visNetwork
visNetwork(nodes, edges ,
main = "A really simple example",
width = "100%") %>%
visLayout(randomSeed = 12)# to have always the same network