-
Notifications
You must be signed in to change notification settings - Fork 3
/
g09.propnbo6.sh
executable file
·492 lines (439 loc) · 16.5 KB
/
g09.propnbo6.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
#!/bin/bash
#####################################################################
# #
# Warning: This repository is no longer maintained as Gaussian 09 #
# is also becoming obsolete, or deprecated. #
# Info: The sucessor repository for Gaussian 16 can be found at #
# https://github.com/polyluxus/tools-for-g16.bash #
# #
# Martin, 2019-05-01 #
# #
#####################################################################
how_called="$0 $@"
scriptname=${0##*\/} # Remove trailing path
scriptname=${scriptname%.sh} # remove scripting ending (if present)
version="0.1.9"
versiondate="2018-02-15"
# A script to take an input file and write a new inputfile to
# perform a (non-standard) nbo6 analysis.
# To Do: import (formatted) checkpoint files
#hlp This script takes a Gaussian inputfile and writes a new inputfile for a property run.
#hlp The newly created inputfile relies on a checkpointfile to read all data for the NBO6 analysis.
#
# Print logging information and warnings nicely.
# If there is an unrecoverable error: display a message and exit.
#
indent ()
{
echo -n "INFO : " "$*"
}
message ()
{
echo "INFO : " "$@"
}
warning ()
{
echo "WARNING: " "$@" >&2
return 1
}
fatal ()
{
echo "ERROR : " "$@" >&2
exit 1
}
#
# Print some helping commands
# The lines are distributed throughout the script and grepped for
#
helpme ()
{
local line
local pattern="^[[:space:]]*#hlp[[:space:]]?(.*)$"
while read -r line; do
[[ $line =~ $pattern ]] && eval "echo \"${BASH_REMATCH[1]}\""
done < <(grep "#hlp" "$0")
#echo "Version: $version ($versiondate)"
#echo "Usage: $scriptname [options] filename"
exit 0
}
getCheckpointfile ()
{
# The checkpointfile should be indicated in the original input file
# (It is a link 0 command and should therefore before the route section.)
local parseline="$1"
local pattern="^[[:space:]]*%chk=([^[:space:]]+)([[:space:]]+|$)"
if [[ $parseline =~ $pattern ]]; then
checkpointfile="${BASH_REMATCH[1]}"
else
return 1
fi
}
removeComment ()
{
local variableName="$1" variableContent="$2"
local pattern="^[[:space:]]*([^!]+)[!]*[[:space:]]*(.*)$"
if [[ $variableContent =~ $pattern ]] ; then
printf -v "$variableName" "%s" "${BASH_REMATCH[1]}"
[[ ! -z ${BASH_REMATCH[2]} ]] && message "Removed comment: ${BASH_REMATCH[2]}"
else
return 1 # Return false if blank line
fi
}
parseInputfile ()
{
# The route section contains one or more lines.
# It always starts with # folowed by a space or the various verbosity levels
# NPT (case insensitive). The route section is terminated by a blank line.
# It is immediately followed by the title section, which can also consist of
# multiple lines made up of (almost) anything. It is also terminated by a blank line.
# Following that is the charge and multiplicity.
local line appendline
# The hash marks the beginning of the route
local routeStartPattern="^[[:space:]]*#[nNpPtT]?[[:space:]]"
local storeRoute=0 storeTitle=0 storeChargeMultiplicity=0
while read -r line; do
# If we found the checkpointfile, we can skip out of the loop
if [[ -z $checkpointfile ]] ; then
getCheckpointfile "$line" && continue
fi
if (( storeRoute == 0 )) ; then
if [[ $line =~ $routeStartPattern ]] ; then
storeRoute=1
removeComment appendline "$line"
routeSection="$appendline"
continue
fi
fi
if (( storeRoute == 1 )) ; then
if [[ $line =~ ^[[:space:]]*$ ]]; then
storeTitle=1
storeRoute=2
continue
fi
removeComment appendline "$line"
[[ ! -z $appendline ]] && routeSection="$routeSection $appendline"
unset appendline
continue
fi
if (( storeTitle == 1 )) ; then
if [[ $line =~ ^[[:space:]]*$ ]]; then
storeTitle=2
storeChargeMultiplicity=1
continue
fi
removeComment appendline "$line"
[[ ! -z $appendline ]] && titleSection="$titleSection $appendline"
unset appendline
continue
fi
if (( storeChargeMultiplicity == 1 )) ; then
removeComment appendline "$line"
pattern="^[[:space:]]*([0-9]+)[[:space:]]+([0-9]+)[[:space:]]*$"
[[ $appendline =~ $pattern ]]
molCharge="${BASH_REMATCH[1]}"
molMultiplicity="${BASH_REMATCH[2]}"
# We now have everything we need
break
fi
done < "$1"
}
collateKeywordOptions ()
{
# The function takes an inputstring and removes any unnecessary spaces
# needed for collateKeywords
local inputstring="$1"
# The collated section will be saved to
local keepstring transformstring
# Any combination of spaces, equals signs, and opening parentheses
# can and need to be removed
local removeFront="[[:space:]]*[=]?[[:space:]]*[\(]?"
# Any trailing closing parentheses and spaces need to be cut
local removeEnd="[\)]?[[:space:]]*"
[[ $inputstring =~ $removeFront([^\)]+)$removeEnd ]] && inputstring="${BASH_REMATCH[1]}"
# Spaces, tabs, or commas can be used in any combination
# to separate items within the options.
# Does massacre IOPs.
local pattern="[^[:space:],]+([[:space:]]*=[[:space:]]*[^[:space:],]+)?([[:space:],]+|$)"
while [[ $inputstring =~ $pattern ]] ; do
transformstring="${BASH_REMATCH[0]}"
inputstring="${inputstring//${BASH_REMATCH[0]}/}"
# remove stuff
transformstring="${transformstring// /}"
transformstring="${transformstring//,/}"
if [[ -z $keepstring ]] ; then
keepstring="$transformstring"
else
keepstring="$keepstring,$transformstring"
fi
done
echo "$keepstring"
}
collateKeywords ()
{
# This function removes spaces which have been entered in the original input
# so that the folding (to 80 characters) doesn't break a keyword.
local inputstring="$1"
# The collated section will be saved to
local keepstring
# If we encounter a long keyword stack, we need to set a different returncode
local returncode=0
# extract the hashtag of the route section
local routeStartPattern="^[[:space:]]*(#[nNpPtT]?)[[:space:]]"
if [[ $inputstring =~ $routeStartPattern ]] ; then
keepstring="${BASH_REMATCH[1]}"
inputstring="${inputstring//${BASH_REMATCH[0]}/}"
fi
# The following formats for the input of keywords are given in the manual:
# keyword = option
# keyword(option)
# keyword=(option1, option2, …)
# keyword(option1, option2, …)
# Spaces can be added or left out, I could also confirm that the following will work, too:
# keyword (option[1, option2, …])
# keyword = (option[1, option2, …])
# Spaces, tabs, commas, or forward slashes can be used in any combination
# to separate items within a line.
# Multiple spaces are treated as a single delimiter.
# see http://gaussian.com/input/?tabid=1
# The ouptput of this function should only use the keywords without any options, or
# the following format: keyword=(option1,option2,…) [no spaces]
# Exeptions to the above: temperature=500 and pressure=2,
# where the equals is the only accepted form.
# This is probably because they can also be options to 'freq'.
local keywordPattern="[^[:space:],/\(=]+"
local optionPatternEquals="[[:space:]]*=[[:space:]]*[^[:space:],/\(\)]+"
local optionPatternParens="[[:space:]]*[=]?[[:space:]]*\([^\)]+\)"
local keywordOptions="$optionPatternEquals|$optionPatternParens"
local keywordTerminate="[[:space:],/]+|$"
local testPattern="($keywordPattern)($keywordOptions)?($keywordTerminate)"
local keepKeyword keepOptions
local numericalPattern="[[:digit:]]+\.?[[:digit:]]*"
while [[ $inputstring =~ $testPattern ]] ; do
# Unify input pattern and remove unnecessary spaces
# Remove found portion from inputstring:
inputstring="${inputstring//${BASH_REMATCH[0]}/}"
# Keep keword, options, and how it was terminated
keepKeyword="${BASH_REMATCH[1]}"
keepOptions="${BASH_REMATCH[2]}"
keepTerminate="${BASH_REMATCH[3]}"
# Remove spaces from IOPs (only evil people use them there)
if [[ $keepKeyword =~ ^[Ii][Oo][Pp]$ ]] ; then
keepKeyword="$keepKeyword$keepOptions"
keepKeyword="${keepKeyword// /}"
unset keepOptions # unset to not run into next 'if'
fi
if [[ ! -z $keepOptions ]] ; then
# remove spaces, equals, parens from front and end
# substitute option separating spaces with commas
keepOptions=$(collateKeywordOptions "$keepOptions")
# Check for the exceptions to the desired format
if [[ $keepKeyword =~ ^[Tt][Ee][Mm][Pp].*$ ]] ; then
if [[ ! $keepOptions =~ ^$numericalPattern$ ]] ; then
warning "Unrecognised format for temperature: $keepOptions."
returncode=1
fi
keepKeyword="$keepKeyword=$keepOptions"
elif [[ $keepKeyword =~ ^[Pp][Rr][Ee].*$ ]] ; then
if [[ ! $keepOptions =~ ^$numericalPattern$ ]] ; then
warning "Unrecognised format for temperature: $keepOptions."
returncode=1
fi
keepKeyword="$keepKeyword=$keepOptions"
else
keepKeyword="$keepKeyword($keepOptions)"
fi
fi
if [[ $keepTerminate =~ / ]] ; then
keepKeyword="$keepKeyword/"
fi
if (( ${#keepKeyword} > 80 )) ; then
returncode=1
warning "Found extremely long keyword, heck input before running the calculation."
fi
if [[ $keepstring =~ /$ ]] ; then
keepstring="$keepstring$keepKeyword"
elif [[ -z $keepstring ]] ; then
keepstring="$keepKeyword"
else
keepstring="$keepstring $keepKeyword"
fi
done
echo "$keepstring"
return $returncode
}
removeAnyKeyword ()
{
# Takes in a string (the route section) and
local testLine="$1"
# removes the pattern (keyword) if present and
local testPattern="$2"
# stores the result to the new route section.
# Since spaces have been removed form within the keywords previously with collateKeywords,
# and inter-keyword delimiters are set to spaces only also,
# it is safe to use that as a criterion to remove unnecessary keywords.
# The test pattern is extended to catch the whole keyword including options.
local extendedPattern="($testPattern[^[:space:]]*)([[:space:]]+|$)"
if [[ $testLine =~ $extendedPattern ]] ; then
#echo "-->|${BASH_REMATCH[0]}|<--" #(Debug Pattern:)
local foundPattern=${BASH_REMATCH[1]}
message "Removed keyword '$foundPattern'."
newRouteSection="${testLine/$foundPattern/}"
return 1
fi
}
removeOptKeyword ()
{
# Assigns the opt keyword to the pattern
local testRouteSection="$1"
local pattern
pattern="[Oo][Pp][Tt]"
removeAnyKeyword "$testRouteSection" "$pattern" || return 1
}
removeFreqKeyword ()
{
# Assign the freq keyword to the pattern
local testRouteSection="$1"
local pattern
pattern="[Ff][Rr][Ee][Qq]"
removeAnyKeyword "$testRouteSection" "$pattern" || return 1
}
removeGuessKeyword ()
{
# Assigns the guess heyword to the pattern
local testRouteSection="$1"
local pattern
pattern="[Gg][Uu][Ee][Ss][Ss]"
removeAnyKeyword "$testRouteSection" "$pattern" || return 1
}
removeGeomKeyword ()
{
# Assigns the geom keyword to the pattern
local testRouteSection="$1"
local pattern
pattern="[Gg][Ee][Oo][Mm]"
removeAnyKeyword "$testRouteSection" "$pattern" || return 1
}
removePopKeyword ()
{
local testRouteSection="$1"
local pattern
pattern="[Pp][Oo][Pp]"
removeAnyKeyword "$testRouteSection" "$pattern" || return 1
}
removeOutputKeyword ()
{
local testRouteSection="$1"
local pattern
local functionExitStatus=0
pattern="[Oo][Uu][Tt][Pp][Uu][Tt]"
removeAnyKeyword "$testRouteSection" "$pattern" || functionExitStatus=1
if (( functionExitStatus != 0 )) ; then
warning "Presence opt the 'OUTPUT' keyword might indicate that the calculation is not suited for a property run."
fi
return $functionExitStatus
}
addRunKeywords ()
{
local newKeywords
newKeywords="geom=check guess(read,only) pop=nbo6read $customRouteInput"
newKeywords=$(collateKeywords "$newKeywords")
echo "$newKeywords"
}
createNewInputFileData ()
{
parseInputfile "$1"
# If there were any long keywords, then return value is not 0.
# A warning must be issued
newRouteSection=$(collateKeywords "$routeSection")
while ! removeOptKeyword "$newRouteSection" ; do : ; done
while ! removeFreqKeyword "$newRouteSection" ; do : ; done
while ! removeGuessKeyword "$newRouteSection" ; do : ; done
while ! removeGeomKeyword "$newRouteSection" ; do : ; done
while ! removePopKeyword "$newRouteSection" ; do : ; done
while ! removeOutputKeyword "$newRouteSection" ; do : ; done
newRouteSection="$newRouteSection $(addRunKeywords)"
# If the checkpoint file was not specified in the input file, guess it
if [[ -z $checkpointfile ]] ; then
checkpointfile="${1%.*}.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.)
[[ ! -e $checkpointfile ]] && fatal "Cannot find '$checkpointfile'."
fi
nbo6basefilename="${checkpointfile%.chk}.nbo6"
# Check if nbo archive file already exists
[[ -e $nbo6basefilename.47 ]] && fatal "File '$nbo6basefilename.47' already exists. Rename or delete it."
tempCheckpointfile="$nbo6basefilename.chk"
[[ -e $tempCheckpointfile ]] && fatal "File '$tempCheckpointfile' already exists. Rename or delete it."
}
# Print the input file in a more readable form
printNewInputFile ()
{
echo "%oldchk=$checkpointfile"
echo "%chk=$tempCheckpointfile"
echo "%NoSave"
fold -w80 -c -s <<< "$newRouteSection"
echo ""
fold -w80 -c -s <<< "$titleSection"
echo ""
echo "$molCharge $molMultiplicity"
echo ""
echo "\$NBO"
echo " archive file=$nbo6basefilename"
[[ ! -z $customNBO6Input ]] && echo " $customNBO6Input"
echo "\$END"
echo ""
if [[ ! -z $customTail ]] ; then
echo "$customTail"
echo ""
fi
echo "! Input file created with: "
echo "! $how_called"
}
#
# Main
#
(( $# == 0 )) && helpme
# Evaluate options
#
#hlp Usage: $scriptname [options] filename
#hlp
#hlp Options:
while getopts :n:r:t:hu options ; do
case $options in
#hlp -n <ARG> Adds custom command <ARG> to the nbo6 input stack.
#hlp May be specified multiple times.
#hlp
n) customNBO6Input="$customNBO6Input $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
r) customRouteInput="$customRouteInput $OPTARG" ;;
#hlp -t <ARG> Adds <ARG> to the end (tail) of the new input file.
#hlp If specified multiple times, only the last one has an effect.
#hlp
t) customTail="$OPTARG" ;;
#hlp -h Prints this short help message
#hlp
h) helpme ;;
u) helpme ;;
\?) warning "Invalid option: -$OPTARG." ;;
:) fatal "Option -$OPTARG requires an argument." ;;
esac
done
shift $((OPTIND-1))
[[ -z $1 ]] && fatal "No filename specified."
inputFilename="$1"
[[ ! -e "$inputFilename" ]] && fatal "Cannot access '$inputFilename'."
[[ ! -r "$inputFilename" ]] && fatal "Cannot access '$inputFilename'."
shift
(( $# > 0 )) && warning "Additional input will be ignored."
outputFilename="${inputFilename%.*}.nbo6.com"
[[ -e "$outputFilename" ]] && fatal "File '$outputFilename' exists. Rename or delete it."
createNewInputFileData "$inputFilename"
printNewInputFile > "$outputFilename"
message "Modified '$inputFilename'."
message "New input is called '$outputFilename'."
#hlp (Martin; $version; $versiondate.)
message "$scriptname is part of tools-for-g09.bash $version ($versiondate)"