-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathg16.nbo6prop.sh
executable file
·527 lines (459 loc) · 19.1 KB
/
g16.nbo6prop.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/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/>.
#
###
#
# The help lines are distributed throughout the script and grepped for
#
#hlp This script reads a Gaussian input file,
#hlp extracts (or replaces) the route section,
#hlp removes and 'opt' keyword, uses 'geom(check)' and 'guess(read) to
#hlp set up and write a new input file for a single point calculation.
#hlp
#hlp This script may generally be useful to base additional calculations,
#hlp like NMR tensors, or energy calculations using a different level of theory,
#hlp on already converged calculations.
#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 [options] [--] <INPUT_FILE>
#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
# Ensure that in/outputfile variables are empty
unset inputfile
unset outputfile
# 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
#
process_inputfile ()
{
local testfile="$1"
debug "Processing Input: $testfile"
read_g16_input_file "$testfile"
if [[ -z "$route_section" ]] ; then
warning "It appears that '$testfile' does not contain a valid (or recognised) route section."
warning "Make sure this template file contains '#/#P/#N/#T' followed by a space."
[[ -z $overwrite_route_section ]] && return 1
else
debug "Route (unmodified): $route_section"
fi
local modified_route="$route_section"
local -a additional_keywords
extract_jobname_inoutnames "$testfile"
if [[ -n $overwrite_route_section ]] ; then
message "Discarding read route section and using specified one instead."
modified_route="$overwrite_route_section"
fi
# The new input should be a single point calculation, therefore remove opt
while ! modified_route=$(remove_opt_keyword "$modified_route") ; do : ; done
# To avoid compound jobs, the freq keyword is also removed
while ! modified_route=$(remove_freq_keyword "$modified_route") ; do : ; done
# The new input should be a single point calculation, therefore remove irc
while ! modified_route=$(remove_irc_keyword "$modified_route") ; do : ; done
# The guess/geom keyword will be added, it will clash if already present
while ! modified_route=$(remove_guess_keyword "$modified_route") ; do : ; done
# The calculation should not be repeated, only properties should be collected
additional_keywords+=("guess(read,only)")
message "Added '${additional_keywords[-1]}' to the route section."
if check_allcheck_option "$modified_route" ; then
debug "Keyword 'AllCheck' detected in input stream."
else
while ! modified_route=$(remove_geom_keyword "$modified_route") ; do : ; done
additional_keywords+=("geom(check)")
message "Added '${additional_keywords[-1]}' to the route section."
fi
# Population analysis will be carried out for nbo6 (that's the purpose of this script)
while ! modified_route=$(remove_pop_keyword "$modified_route") ; do : ; done
additional_keywords+=( "pop=nbo6read" )
# Writing additional output must be disabled for this type of run
while ! modified_route=$(remove_output_keyword "$modified_route") ; do : ; done
if modified_route=$(remove_gen_keyword "$modified_route") ; then
debug "No gen keyword present."
else
warning "Additional basis set specifications have not been read,"
warning "but will be retrieved from the checkpointfile."
while ! modified_route=$(remove_gen_keyword "$modified_route") ; do : ; done
additional_keywords+=('ChkBasis')
message "Added '${additional_keywords[-1]}' to the route section."
if check_denfit_keyword "$modified_route" ; then
warning "Please check density fitting settings are compatible with 'ChkBasis'."
else
debug "No 'DenFit' present."
fi
fi
# Add the custom route options
if (( ${#use_custom_route_keywords[@]} == 0 )) ; then
debug "No custom route keywords specified."
else
additional_keywords+=("${use_custom_route_keywords[@]}")
debug "Added the following custom keywords to route section:"
debug "$(fold -w80 -s <<< "${use_custom_route_keywords[*]}")"
fi
debug "Added the following keywords to route section:"
debug "$(fold -w80 -s <<< "${additional_keywords[*]}")"
# add the custom keywords
modified_route="$modified_route ${additional_keywords[*]}"
local verified_checkpoint
if [[ -z $checkpoint ]] ; then
checkpoint="${jobname}.chk"
# Check if the guessed checkpointfile exists
# (We'll trust the user if it was specified in the input file,
# after all the calculation might not be completed yet.)
if verified_checkpoint=$(test_file_location "$checkpoint") ; then
debug "verified_checkpoint=$verified_checkpoint"
fatal "Cannot find '$verified_checkpoint'."
else
old_checkpoint="$checkpoint"
fi
else
old_checkpoint="$checkpoint"
fi
# Since this is a property run, there is no need to store the checkpoint file
g16_checkpoint_save="false"
# Throw away the body of the input file
unset inputfile_body
# declare a variable to hold the suffix
local jobbasename use_file_suffix
# The following only ensures, that if it is based on a freq run,
# the suffix is removed, because the new job will have no frequencies
jobbasename="${jobname%.freq*}"
# Assign new checkpoint/inputfile
use_file_suffix="nbo6"
jobname="${jobbasename}.$use_file_suffix"
[[ -z $inputfile_new ]] && inputfile_new="${jobname}.$g16_input_suffix"
checkpoint="${inputfile_new%.*}.chk"
backup_if_exists "$inputfile_new"
route_section="$modified_route"
# Add the tail section with the nbo keywords
if (( ${#use_custom_tail[*]} > 0 )) ; then
warning "Found specified keywords for the tail, will skip automatic input stack."
else
# Insert marker for NBO
#shellcheck disable=SC2016
use_custom_tail+=( '$NBO' )
# Insert archive file name
use_custom_tail+=( " archive" " file=${inputfile_new%.*}" )
# Add the custom NBO stack provided from the command line
use_custom_tail+=( "${use_custom_nbo_stack[@]}" )
# Insert marker for the end of the stack
#shellcheck disable=SC2016
use_custom_tail+=( '$END' )
fi
write_g16_input_file > "$inputfile_new"
message "Written modified inputfile '$inputfile_new'."
}
#
# Process Options
#
process_options ()
{
#hlp Options:
#hlp
local OPTIND=1
while getopts :o:r:R:t:f:m:p:d:sh options ; do
case $options in
#hlp -o <ARG> Adds <ARG> as a new line on to the NBO6 input stack.
#hlp May be specified multiple times.
#hlp The 'archive' and 'file' statements will always be added.
#hlp Note that this only adds to the actual NBO stack, if for
#hlp example a 'CHOOSE' stack should be included, the former
#hlp has to be terminated first. See the manual for more information.
#hlp
o)
use_custom_nbo_stack+=("$OPTARG")
;;
#hlp -r <ARG> Adds custom command <ARG> to the route section.
#hlp May be specified multiple times.
#hlp The stack will be collated, but no sanity check will be performed.
#hlp This may or may not have an effect on the calculation, as with the
#hlp property run not all keywords will be carried out by Gaussian.
#hlp
r)
use_custom_route_keywords+=("$OPTARG" )
;;
#hlp -R <ARG> Specify the complete route section.
#hlp If specified multiple times, only the last has an effect.
#hlp This overwrites any previously specified route section.
#hlp This can be amended with other switches, like -r.
#hlp This may or may not have an effect on the calculation, it might even
#hlp cause the program to fail. As this script is designed to perform a
#hlp property run not all keywords will be carried out by Gaussian.
#hlp
R)
overwrite_route_section="$OPTARG"
if validate_g16_route "$overwrite_route_section" ; then
debug "Route specified with -R is fine."
else
warning "Syntax error in specified route section:"
warning " $overwrite_route_section"
fatal "Emergency stop."
fi
;;
#hlp -t <ARG> Adds <ARG> to the end (tail) of the new input file.
#hlp If specified multiple times, each argument goes to a new line.
#hlp This will cause the NBO stack to be empty, i.e. -o will be ignored.
#hlp The option should be used to manually specify all NBO commands.
#hlp
t)
use_custom_tail[${#use_custom_tail[@]}]="$OPTARG"
;;
#hlp -f <ARG> Write inputfile to <ARG>.
#hlp
f)
inputfile_new="$OPTARG"
debug "Setting inputfile_new='$inputfile_new'."
;;
# Link 0 related options
#hlp -m <ARG> Define the total memory to be used in megabyte.
#hlp The total request will be larger to account for
#hlp overhead which Gaussian may need. (Default: 512)
#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. (Default: 4)
#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"
;;
#hlp -d <ARG> Define disksize via the MaxDisk keyword (MB).
#hlp This option does not set a parameter for the queueing system,
#hlp but will only modify the input file with the size specification.
#hlp
d)
validate_integer "$OPTARG" "the 'MaxDisk' keyword"
if (( OPTARG == 0 )) ; then
fatal "The keyword 'MaxDisk' must not be zero."
fi
requested_maxdisk="$OPTARG"
;;
#hlp -s Suppress logging messages of the script.
#hlp (May be specified multiple times.)
#hlp
s)
(( stay_quiet++ ))
;;
#hlp -h this help.
#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."
;;
#hlp
#hlp Note: This script will set the option to write a checkpoint file to false.
#hlp
esac
done
# Shift all variables processed to far
shift $((OPTIND-1))
if [[ -z "$1" ]] ; then
fatal "There is no inputfile specified"
fi
# If a filename is specified, it must exist, otherwise exit
# different mode let's you only use the jobname
requested_inputfile=$(is_readable_file_or_exit "$1") || exit 1
shift
debug "Specified input: $requested_inputfile"
# Issue a warning that the addidtional flag has no effect.
warn_additional_args "$@"
}
#
# 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
# Check for settings in three default locations (increasing priority):
# install path of the script, user's home directory, 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 some variables
# declare -a use_opt_opts
declare -a use_custom_route_keywords
# Evaluate Options
process_options "$@" || exit_status=$?
process_inputfile "$requested_inputfile" || exit_status=$?
#hlp $scriptname is part of $softwarename $version ($versiondate)
message "$scriptname is part of $softwarename $version ($versiondate)"
debug "$script_invocation_spell"
exit $exit_status