Skip to content

Commit

Permalink
Use local vars in BASH functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hollas committed Dec 5, 2023
1 parent 9560ce4 commit 0a1927d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 81 deletions.
55 changes: 28 additions & 27 deletions interfaces/BAGEL-SH/bagel.inp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ source SetEnvironment.sh BAGEL

### Helper functions, no need to modify
function exec_bagel {
input_file=$1
output_file=$2
local input_file=$1
local output_file=$2
# BAGEL does not print its input so we will prepend it.
cp $input_file $output_file
$BAGELEXE $input_file >> $output_file 2>&1
exitcode=$?
local exitcode=$?

echo "TIMESTEP = $timestep" >> $output_file.all
date >> $output_file.all
echo "####################" >> $output_file.all
cat $output_file >> $input.out.all
cat "$output_file" >> "$output_file.all"
return $exitcode
}

function bagel_error {
errmsg="$1"
bagel_output="$2"
copy="$3"
local errmsg="$1"
local bagel_output="$2"
local copy="$3"
if [[ -f "$bagel_output" && -n "$copy" ]]; then
cp "$bagel_output" "$copy"
fi
Expand All @@ -64,10 +65,10 @@ function file_exists {
# This function must be called first to start the bagel input
function specify_molecule {
# XYZ coordinates provided by ABIN in geom.dat
xyz=$1
local xyz=$1
# Name of the bagel input file we are creating
inp=$2
natom=$(wc -l < $xyz)
local inp=$2
local natom=$(wc -l < $xyz)

# NOTE: $basis and $df_basis MUST be defined at the top of the file!
cat > $inp << EOF
Expand All @@ -86,7 +87,7 @@ EOF
}

function generate_hf_orbitals {
inp=$1
local inp=$1
cat >> $inp << EOF
{
"title" : "$hf_variant",
Expand All @@ -97,8 +98,8 @@ EOF
}

function load_orbitals {
orbfile=$1
inp=$2
local orbfile=$1
local inp=$2
cat >> $inp << EOF
{
"title": "load_ref",
Expand All @@ -109,8 +110,8 @@ EOF
}

function save_orbitals {
orbfile=$1
inp=$2
local orbfile=$1
local inp=$2
cat >> $inp << EOF
{
"title": "save_ref",
Expand All @@ -120,8 +121,8 @@ EOF
}

function print_molden {
moldenfile=$1
inp=$2
local moldenfile=$1
local inp=$2
cat >> $inp << EOF
{
"title": "print",
Expand All @@ -135,8 +136,8 @@ EOF
# (for now we only support one target state)
# This needs to be called before the CAS section.
function print_forces {
target_state=$1
inp=$2
local target_state=$1
local inp=$2
cat >> $inp << EOF
{
"title" : "forces",
Expand All @@ -151,10 +152,10 @@ EOF
}

function print_cas {
method=$1
thresh=$2
maxiter=$3
inp=$4
local method=$1
local thresh=$2
local maxiter=$3
local inp=$4
cat >> $inp << EOF
"method": [{
"title": "$method",
Expand All @@ -171,10 +172,10 @@ EOF

function print_caspt2 {
# XMS-CASPT2 - "true" or "false"
xms=$1
thresh=$2
maxiter=$3
inp=$4
local xms=$1
local thresh=$2
local maxiter=$3
local inp=$4
# TODO: Make it possible to use a real shift instead of imaginary
cat >> $inp << EOF
"smith": {
Expand Down
107 changes: 53 additions & 54 deletions interfaces/BAGEL-SH/r.bagel-sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,73 +35,71 @@ rm -f ../engrad.dat.$ibead ../nacm.dat ENERGY.out FORCE_*
# Determine for which state we need to calculate forces
for ((ist=0; ist<nstate; ist++))
do
if [[ ${tocalc[$ist]} -eq 1 ]]; then
if [[ -z ${target_state-} ]]; then
target_state=$ist
else
>&2 echo "ERROR: Invalid tocalc, cannot compute gradient for more than one state"
exit 2
if [[ ${tocalc[$ist]} -eq 1 ]]; then
if [[ -z ${target_state-} ]]; then
target_state=$ist
else
>&2 echo "ERROR: Invalid tocalc, cannot compute gradient for more than one state"
exit 2
fi
fi
fi
done

function generate_input {

input=$1

specify_molecule "$geom" "$input"

if [[ -f "$ORBITAL_FILE.archive" ]]; then
# Load orbitals if the archive file exists
load_orbitals $ORBITAL_FILE $input
else
# Calculate HF guess if the initial orbitals are not provided via the 'orbitals.archive' file
generate_hf_orbitals $input
fi

print_forces "$target_state" "$input"

# CAS section
if [[ $method == "xms_caspt2" ]]; then
print_cas "caspt2" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
print_caspt2 "true" "$thresh_CASPT2" "$maxiter_CASPT2" "$input"
elif [[ $method == "ms_caspt2" ]]; then
print_cas "caspt2" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
print_caspt2 "false" "$thresh_CASPT2" "$maxiter_CASPT2" "$input"
elif [[ $method == "sa_casscf" ]]; then
print_cas "casscf" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
# Remove extra dangling comma
sed -i '$ s/,$//' "$input"
else
>&2 echo "ERROR: Unknown method ($method). Specify one of \"xms_caspt2\", \"ms_caspt2\" or \"sa_casscf\" in bagel.inp"
exit 2
fi
echo -e " }]\n }," >> $input
save_orbitals $ORBITAL_FILE $input

if [[ ! -f "initial_orbitals.molden" ]];then
print_molden "initial_orbitals.molden" $input
else
print_molden "orbitals.molden" $input
fi

echo "]}" >> $input
local input=$1

specify_molecule "$geom" "$input"

if [[ -f "$ORBITAL_FILE.archive" ]]; then
# Load orbitals if the archive file exists
load_orbitals $ORBITAL_FILE $input
else
# Calculate HF guess if the initial orbitals are not provided via the 'orbitals.archive' file
generate_hf_orbitals $input
fi

print_forces "$target_state" "$input"

# CAS section
if [[ $method == "xms_caspt2" ]]; then
print_cas "caspt2" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
print_caspt2 "true" "$thresh_CASPT2" "$maxiter_CASPT2" "$input"
elif [[ $method == "ms_caspt2" ]]; then
print_cas "caspt2" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
print_caspt2 "false" "$thresh_CASPT2" "$maxiter_CASPT2" "$input"
elif [[ $method == "sa_casscf" ]]; then
print_cas "casscf" "$thresh_CASSCF" "$maxiter_CASSCF" "$input"
# Remove extra dangling comma
sed -i '$ s/,$//' "$input"
else
>&2 echo "ERROR: Unknown method ($method). Specify one of \"xms_caspt2\", \"ms_caspt2\" or \"sa_casscf\" in bagel.inp"
exit 2
fi
echo -e " }]\n }," >> $input
save_orbitals $ORBITAL_FILE $input

if [[ ! -f "initial_orbitals.molden" ]];then
print_molden "initial_orbitals.molden" $input
else
print_molden "orbitals.molden" $input
fi

echo "]}" >> $input
}
### END OF INPUT GENERATION

error=false
function converge_casscf {
input=$1
output=$2
casscf_error='EXCEPTION RAISED: Max iteration reached during the second-order optimization'
local input=$1
local output=$2
local casscf_error='EXCEPTION RAISED: Max iteration reached during the second-order optimization'
generate_input "$input"

# Execute BAGEL
exec_bagel $input $input
returncode=$?
exec_bagel $input $output
local returncode=$?
if grep -q "$casscf_error" $output; then
error=true
savefile=$output.casscf_error.$timestep
local savefile=$output.casscf_error.$timestep
cp $output $savefile
>&2 echo "ERROR: CASSCF did not converge, see file $savefile"
# Return if we reached the maximum threshold value,
Expand All @@ -120,6 +118,7 @@ function converge_casscf {
}

### EXECUTE BAGEL
error=false
converge_casscf $input.json $input.out
returncode=$?

Expand Down

0 comments on commit 0a1927d

Please sign in to comment.