-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathg16.wrapper.sh
executable file
·362 lines (316 loc) · 11.9 KB
/
g16.wrapper.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
###
#
# tools-for-g16.bash --
# A collection of tools for the help with Gaussian 16.
# Copyright (C) 2019-2020 Martin C Schwarzer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
###
#hlp This is a wrapper script to set the Gaussian 16 environemt,
#hlp so that available utilities can be used interactively.
#hlp See http://gaussian.com/utils/ for more information.
#hlp
#hlp tools-for-g16.bash Copyright (C) 2019 Martin C Schwarzer
#hlp This program comes with ABSOLUTELY NO WARRANTY; this is free software,
#hlp and you are welcome to redistribute it under certain conditions;
#hlp please see the license file distributed alongside this repository,
#hlp which is available when you type 'g16.tools-info.sh -L',
#hlp or at <https://github.com/polyluxus/tools-for-g16.bash>.
#hlp
#hlp Usage: $scriptname [option] [--] <commands>
#hlp
#
#
# Generic functions to find the scripts
# (Copy of ./resources/locations.sh)
#
# Let's know where the script is and how it is actually called
#
get_absolute_location ()
{
# Resolves the absolute location of parameter and returns it
# Taken from https://stackoverflow.com/a/246128/3180795
local resolve_file="$1" description="$2"
local link_target directory_name filename resolve_dir_name
debug "Getting directory for '$resolve_file'."
# resolve $resolve_file until it is no longer a symlink
while [[ -h "$resolve_file" ]]; do
link_target="$(readlink "$resolve_file")"
if [[ $link_target == /* ]]; then
debug "File '$resolve_file' is an absolute symlink to '$link_target'"
resolve_file="$link_target"
else
directory_name="$(dirname "$resolve_file")"
debug "File '$resolve_file' is a relative symlink to '$link_target' (relative to '$directory_name')"
# If $resolve_file was a relative symlink, we need to resolve
#+ it relative to the path where the symlink file was located
resolve_file="$directory_name/$link_target"
fi
done
debug "File is '$resolve_file'"
filename="$(basename "$resolve_file")"
debug "File name is '$filename'"
resolve_dir_name="$(dirname "$resolve_file")"
directory_name="$(cd -P "$(dirname "$resolve_file")" && pwd)"
if [[ "$directory_name" != "$resolve_dir_name" ]]; then
debug "$description '$directory_name' resolves to '$directory_name'."
fi
debug "$description is '$directory_name'"
if [[ -z $directory_name ]] ; then
directory_name="."
fi
echo "$directory_name/$filename"
}
get_absolute_filename ()
{
# Returns only the filename
local resolve_file="$1" description="$2" return_filename
return_filename=$(get_absolute_location "$resolve_file" "$description")
return_filename=${return_filename##*/}
echo "$return_filename"
}
get_absolute_dirname ()
{
# Returns only the directory
local resolve_file="$1" description="$2" return_dirname
return_dirname=$(get_absolute_location "$resolve_file" "$description")
return_dirname=${return_dirname%/*}
echo "$return_dirname"
}
get_scriptpath_and_source_files ()
{
local error_count tmplog line
tmplog=$(mktemp tmp.XXXXXXXX)
# Who are we and where are we?
scriptname="$(get_absolute_filename "${BASH_SOURCE[0]}" "installname")"
debug "Script is called '$scriptname'"
# remove scripting ending (if present)
scriptbasename=${scriptname%.sh}
debug "Base name of the script is '$scriptbasename'"
scriptpath="$(get_absolute_dirname "${BASH_SOURCE[0]}" "installdirectory")"
debug "Script is located in '$scriptpath'"
resourcespath="$scriptpath/resources"
if [[ -d "$resourcespath" ]] ; then
debug "Found library in '$resourcespath'."
else
(( error_count++ ))
fi
# Import default variables
#shellcheck source=./resources/default_variables.sh
source "$resourcespath/default_variables.sh" &> "$tmplog" || (( error_count++ ))
# Set more default variables
exit_status=0
stay_quiet=0
# Import other functions
#shellcheck source=./resources/messaging.sh
source "$resourcespath/messaging.sh" &> "$tmplog" || (( error_count++ ))
#shellcheck source=./resources/rcfiles.sh
source "$resourcespath/rcfiles.sh" &> "$tmplog" || (( error_count++ ))
#shellcheck source=./resources/test_files.sh
source "$resourcespath/test_files.sh" &> "$tmplog" || (( error_count++ ))
#shellcheck source=./resources/process_gaussian.sh
source "$resourcespath/process_gaussian.sh" &> "$tmplog" || (( error_count++ ))
#shellcheck source=./resources/validate_numbers.sh
source "$resourcespath/validate_numbers.sh" &> "$tmplog" || (( error_count++ ))
if (( error_count > 0 )) ; then
echo "ERROR: Unable to locate library functions. Check installation." >&2
echo "ERROR: Expect functions in '$resourcespath'."
debug "Errors caused by:"
while read -r line || [[ -n "$line" ]] ; do
debug "$line"
done < "$tmplog"
debug "$(rm -v -- "$tmplog")"
exit 1
else
debug "$(rm -v -- "$tmplog")"
fi
}
#
# Specific functions for this script only
#
# Create a scratch directory for temporary files
cleanup_scratch () {
debug "What is the content currently?"
debug "$( ls -l "$g16_scratch" )"
message "Looking for files with filesize zero and delete them in '$g16_scratch'."
debug "$( find "$g16_scratch" -type f -size 0 -exec rm -v -- {} \; )"
message "Deleting scratch '$g16_scratch' if empty."
debug "$( find "$g16_scratch" -maxdepth 0 -empty -exec rmdir -v -- {} \; )"
[[ -e "$g16_scratch" ]] && warning "Scratch directory ($g16_scratch) is not empty, please check whether you need the files."
}
make_scratch ()
{
debug "Creating new scratch directory."
local tempdir_pattern='^(|[Tt][Ee]?[Mm][Pp]([Dd][Ii][Rr])?|0|[Dd][Ee][Ff][Aa]?[Uu]?[Ll]?[Tt]?)$'
debug "g16_scratch='$g16_scratch'; pattern: $tempdir_pattern"
if [[ "$g16_scratch" =~ $tempdir_pattern ]] ; then
debug "Pattern was found."
#shellcheck disable=SC2016
g16_scratch=$( mktemp --directory --tmpdir )
else
debug "Pattern was not found."
g16_scratch=$( mktemp --directory --tmpdir="$g16_scratch" g16-interactive-XXXXXX )
fi
[[ -e "$g16_scratch" ]] || return 1
trap cleanup_scratch EXIT SIGTERM
}
# Loading external NBO6 interface
load_nbo ()
{
[[ "$nbo6_interface" =~ [Aa][Cc][Tt][Ii][Vv][Ee] ]] || { debug "Manual NBO6 interface inactive." ; return 0 ; }
[[ -z "$nbo6_installpath" ]] && fatal "Tried loading NBO6 manually, but failed (installation path unset)."
[[ -e "$nbo6_installpath/bin" ]] || fatal "Failed locating NBO6 bin directory in '$nbo6_installpath'."
debug "Adding '$nbo6_installpath/bin' to PATH."
PATH="$nbo6_installpath/bin:$PATH"
export PATH
debug "PATH=$PATH"
debug "$(command -v gaunbo6)"
}
# How Gaussian is loaded
load_gaussian ()
{
if [[ "$load_modules" =~ [Tt][Rr][Uu][Ee] ]] ; then
(( ${#g16_modules[*]} == 0 )) && fatal "No modules to load."
# assume that in the interactive session everything is set alright already
module load ${g16_modules[*]}
else
[[ -z "$g16_installpath" ]] && fatal "Gaussian path is unset."
[[ -e "$g16_installpath/g16/bsd/g16.profile" ]] || fatal "Gaussian profile does not exist."
# Gaussian needs the g16root variable
g16root="$g16_installpath"
export g16root
#shellcheck disable=SC1090
. "${g16root}"/g16/bsd/g16.profile
# load NBO6 manually
load_nbo
fi
make_scratch || fatal "Setting scratch failed."
GAUSS_SCRDIR="$g16_scratch"
message "Using scratch '$g16_scratch'."
GAUSS_MEMDEF="${requested_memory}MB"
GAUSS_MDEF="${requested_memory}MB"
GAUSS_PDEF=$requested_numCPU
debug "$(declare -p g16root GAUSS_SCRDIR GAUSS_MEMDEF GAUSS_MDEF GAUSS_PDEF)"
export GAUSS_SCRDIR GAUSS_MEMDEF GAUSS_MDEF GAUSS_PDEF
}
#
# MAIN SCRIPT
#
# If this script is sourced, return before executing anything
if ( return 0 2> /dev/null ) ; then
# [How to detect if a script is being sourced](https://stackoverflow.com/a/28776166/3180795)
debug "Script is sourced. Return now."
return 0
fi
# Save how script was called
printf -v script_invocation_spell "'%s' " "${0/#$HOME/<HOME>}" "$@"
# Sent logging information to stdout
exec 3>&1
# Need to define debug function if unknown
if ! command -v debug ; then
debug () {
echo "DEBUG : " "$*" >&4
}
fi
# Secret debugging switch
if [[ "$1" == "debug" ]] ; then
exec 4>&1
stay_quiet=0
shift
else
exec 4> /dev/null
fi
get_scriptpath_and_source_files || exit 1
# Check whether we have the right numeric format (set it if not)
warn_and_set_locale
# Warn if neither options nor a command is given
(( $# == 0 )) && warning "There is nothing to do."
# Check for settings in four default locations (increasing priority):
# install path of the script, user's home directory, .config in user's home, current directory
g16_tools_rc_searchlocations=( "$scriptpath" "$HOME" "$HOME/.config" "$PWD" )
g16_tools_rc_loc="$( get_rc "${g16_tools_rc_searchlocations[@]}" )"
debug "g16_tools_rc_loc=$g16_tools_rc_loc"
# Load custom settings from the rc
if [[ -n $g16_tools_rc_loc ]] ; then
#shellcheck source=./g16.tools.rc
. "$g16_tools_rc_loc"
message "Configuration file '${g16_tools_rc_loc/*$HOME/<HOME>}' applied."
if [[ "${configured_version}" =~ ^${version%.*} ]] ; then
debug "Config: $configured_version ($configured_versiondate); Current: $version ($versiondate)."
else
warning "Configured version was ${configured_version:-unset} (${configured_versiondate:-unset}),"
warning "and probably needs an update to $version ($versiondate)."
fi
else
debug "No custom settings found."
fi
# Initialise options
debug "Initialising option index."
OPTIND="1"
while getopts :m:p:sh options ; do
#hlp Options:
#hlp
case $options in
#hlp -m <ARG> Define the total memory to be used in megabyte.
#hlp (Default: $requested_memory)
#hlp
m)
validate_integer "$OPTARG" "the memory"
if (( OPTARG == 0 )) ; then
fatal "Memory limit must not be zero."
fi
requested_memory="$OPTARG"
;;
#hlp -p <ARG> Define number of professors to be used.
#hlp (Default: $requested_numCPU)
#hlp
p)
validate_integer "$OPTARG" "the number of threads"
if (( OPTARG == 0 )) ; then
fatal "Number of threads must not be zero."
fi
requested_numCPU="$OPTARG"
;;
s)
(( stay_quiet++ ))
;;
#hlp -h Prints this help text
#hlp
h)
helpme
;;
#hlp -- Close reading options.
# This is the standard closing argument for getopts, it needs no implemenation.
\?)
fatal "Invalid option: -$OPTARG."
;;
:)
fatal "Option -$OPTARG requires an argument."
;;
esac
done
debug "Reading options completed."
shift $(( OPTIND - 1 ))
# Assume all other arguments are part of the wrapped command
g16_commandline=("$@")
debug "Processing: ${g16_commandline[*]}"
#Now load gaussian here
load_gaussian || fatal "Loading Gaussian failed."
"${g16_commandline[@]}" || exit_status=$?
#hlp $scriptname is part of $softwarename $version ($versiondate)
message "$scriptname is part of $softwarename $version ($versiondate)"
debug "$script_invocation_spell"
(( exit_status == 0 )) || fatal "There have been one or more errors."