-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHelloNGEN.sh
166 lines (143 loc) · 5.09 KB
/
HelloNGEN.sh
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
#!/bin/bash
# ANSI color codes
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
BLUE='\e[34m'
MAGENTA='\e[35m'
CYAN='\e[36m'
RESET='\e[0m'
# Increasing `ulimit` to Open files
ulimit -n 500000
# Loading Lmod
source /etc/profile.d/modules.sh
# Loading OpenMPI module for Parallel Run
module load mpi
workdir="${1:-/ngen}"
cd "${workdir}" || { echo -e "${RED}Failed to change directory to ${workdir}${RESET}"; exit 1; }
set -e
echo -e "${CYAN}Working directory is:${RESET}"
pwd
echo -e "\n"
# Function to automatically select file if only one is found
auto_select_file() {
local files=($1)
if [ "${#files[@]}" -eq 1 ]; then
echo "${files[0]}"
else
echo ""
fi
}
# Finding files
HYDRO_FABRIC_CATCHMENTS=$(find . -name "*.gpkg")
HYDRO_FABRIC_NEXUS=$(find . -name "*.gpkg")
NGEN_REALIZATIONS=$(find . -name "*realization*.json")
# Auto-selecting files if only one is found
selected_catchment=$(auto_select_file "$HYDRO_FABRIC_CATCHMENTS")
selected_nexus=$(auto_select_file "$HYDRO_FABRIC_NEXUS")
selected_realization=$(auto_select_file "$NGEN_REALIZATIONS")
# Displaying found files
echo -e "${BLUE}\e[4mFound these Catchment files:${RESET}" && echo "$HYDRO_FABRIC_CATCHMENTS" || echo -e "${RED}No Catchment files found.${RESET}"
echo -e "\n"
echo -e "${MAGENTA}\e[4mFound these Nexus files:${RESET}" && echo "$HYDRO_FABRIC_NEXUS" || echo -e "${RED}No Nexus files found.${RESET}"
echo -e "\n"
echo -e "${CYAN}\e[4mFound these Realization files:${RESET}" && echo "$NGEN_REALIZATIONS" || echo -e "${RED}No Realization files found.${RESET}"
echo -e "\n"
generate_partition() {
/dmod/bin/partitionGenerator "$1" "$2" "partitions_$3.json" "$3" '' ''
}
if [ "$2" == "auto" ]
then
echo "AUTO MODE ENGAGED"
echo "Running NextGen model framework in parallel mode"
if [ -z "$3" ]; then
procs=$(($(nproc) - 2))
else
procs=$3
fi
partitions=$(find . -name "*partitions_$procs.json")
if [[ -z $partitions ]]; then
echo "No partitions file found, generating..."
generate_partition "$selected_catchment" "$selected_nexus" "$procs"
else
echo "Found paritions file! "$partitions
fi
mpirun -n $procs /dmod/bin/ngen-parallel $selected_catchment all $selected_nexus all $selected_realization $(pwd)/partitions_$procs.json
echo "Run completed successfully, exiting, have a nice day!"
exit 0
else
echo "Entering Interactive Mode"
fi
echo -e "${YELLOW}Select an option (type a number): ${RESET}"
options=("Run NextGen model framework in serial mode" "Run NextGen model framework in parallel mode" "Run Bash shell" "Exit")
select option in "${options[@]}"; do
case $option in
"Run NextGen model framework in serial mode"|"Run NextGen model framework in parallel mode")
echo -e "\n"
n1=${selected_catchment:-$(read -erp "Enter the hydrofabric catchment file path: " n1; echo "$n1")}
n2=${selected_nexus:-$(read -erp "Enter the hydrofabric nexus file path: " n2; echo "$n2")}
n3=${selected_realization:-$(read -erp "Enter the Realization file path: " n3; echo "$n3")}
echo -e "${GREEN}Selected files:\nCatchment: $n1\nNexus: $n2\nRealization: $n3${RESET}\n"
if [ "$option" == "Run NextGen model framework in parallel mode" ]; then
procs=$(nproc)
generate_partition "$n1" "$n2" "$procs"
run_command="mpirun -n $procs /dmod/bin/ngen-parallel $n1 all $n2 all $n3 $(pwd)/partitions_$procs.json"
else
run_command="/dmod/bin/ngen-serial $n1 all $n2 all $n3"
fi
echo -e "${YELLOW}Your NGEN run command is $run_command${RESET}"
break
;;
"Run Bash shell")
echo -e "${CYAN}Starting a shell, simply exit to stop the process.${RESET}"
/bin/bash
;;
"Exit")
exit 0
;;
*)
echo -e "${RED}Invalid option $REPLY${RESET}"
;;
esac
done
echo -e "${GREEN}If your model didn't run, or encountered an error, try checking the Forcings paths in the Realizations file you selected.${RESET}"
# Ask user if they want to redirect output to /dev/null
echo -e "${YELLOW}Do you want to redirect command output to /dev/null? (y/N, default: n):${RESET}"
read -r redirect_choice
# Execute the command
if [[ "$redirect_choice" == [Yy]* ]]; then
echo -e "${GREEN}Redirecting output to /dev/null.${RESET}"
time $run_command > /dev/null 2>&1
else
time $run_command
fi
command_status=$?
# Set message color based on command status
if [ $command_status -eq 0 ]; then
color=$GREEN
message="Finished executing command successfully."
else
color=$RED
message="Command execution failed with exit code $command_status."
fi
echo -e "${color}${message}${RESET}"
echo -e "${YELLOW}Would you like to continue?${RESET}"
echo -e "${YELLOW}Select an option (type a number): ${RESET}"
options=("Interactive-Shell" "Exit")
select option in "${options[@]}"; do
case $option in
"Interactive-Shell")
echo -e "${CYAN}Starting a shell, simply exit to stop the process.${RESET}"
/bin/bash
break
;;
"Exit")
echo -e "${GREEN}Have a nice day.${RESET}"
break
;;
*)
echo -e "${RED}Invalid option $REPLY${RESET}"
;;
esac
done
exit