Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAGEL interface for XMS and MS-CASPT2 in FSSH and LZSH #153

Merged
merged 36 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d6c03ee
BAGEL interface for XMS and MS-CASPT2 in FSSH and LZSH
Dec 1, 2022
93b5301
Quick fix: r.bagel-lz and r.bagel-sh
JanosJiri Dec 1, 2022
f514ed6
Improvements of BAGEL interface progress
JanosJiri Dec 5, 2022
cc4fe19
Improvements of the BAGEL interfaces
JanosJiri Jan 24, 2023
5595767
Final adjustments of the interface after review of DH
JanosJiri Mar 2, 2023
5d179a7
BAGEL interface for ground-state MD
JanosJiri Aug 15, 2023
928e32b
Refactor BAGEL-SH/ interface
Nov 29, 2023
140e11e
Remove Hamilton-specific config, set shift=0.25
danielhollas Nov 30, 2023
728c218
Fix CASSCF
danielhollas Nov 30, 2023
0eb7c26
Add missing function+minor fixes
Dec 1, 2023
527106c
WIP: Address review
danielhollas Dec 2, 2023
9560ce4
Converge CASSCF up to thresh=6.e-6
danielhollas Dec 3, 2023
0a1927d
Use local vars in BASH functions
Dec 5, 2023
6a2f6a9
Run through shellcheck
danielhollas Dec 5, 2023
6d792f6
Add more printing to numdiff.py
danielhollas Dec 5, 2023
653851e
Merge branch 'master' into bagel
danielhollas Dec 5, 2023
0d221e8
Fix regex in fprettify
danielhollas Dec 5, 2023
1a37237
Fix Intel OneAPI test?
danielhollas Dec 5, 2023
6397200
WIP: Final refactor
danielhollas Dec 6, 2023
cc7fe73
Fixes
Dec 8, 2023
4a505ad
Do not specified nstate twice, more fixes
Dec 11, 2023
7a103f2
Shellcheck
danielhollas Dec 11, 2023
096b580
Pass parameters to generate_hf_orbitals
danielhollas Dec 11, 2023
c252e16
Small fix in MOLPRO-SH, caught by shellcheck
danielhollas Dec 11, 2023
53505f2
Pass basis/df_basis explicitly
danielhollas Dec 11, 2023
a14053c
Fix print_cas
danielhollas Dec 11, 2023
187f948
Ignore shellcheck source error for SetEnvironment.sh
danielhollas Dec 11, 2023
b7653a6
Pass more arguments to print_cas
danielhollas Dec 11, 2023
f3e3249
Pass all parameters explicitly to BASH functions
danielhollas Dec 11, 2023
8cd689b
Stupid json, not liking dangling commas
danielhollas Dec 11, 2023
99a22ef
One more fix
danielhollas Dec 11, 2023
b187f88
Concatenate error outputs
Dec 11, 2023
276e56d
Cleanup BAGEL and BAGEL-LZ
danielhollas Dec 12, 2023
bc648bc
Fix ground state interface
Dec 12, 2023
680685d
Fix LZ interface
Dec 12, 2023
50dc81c
Merge branch 'master' into bagel
danielhollas Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP: Final refactor
  • Loading branch information
danielhollas committed Dec 6, 2023
commit 6397200bbc5c6fc7280ce26ae68ee5cd750c0d78
3 changes: 3 additions & 0 deletions dev_scripts/install_mpich.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ cd $MPICH_DIR/$MPICH_VERSION/src && tar -xzf ../pkg/${TAR_FILE} && cd mpich-${MP
# Use the two rows below for a debug build/
# export CFLAGS='-g -O0'
# --disable-fast --enable-g-option=all \
# These are needed for newer compilers
# export FFLAGS=-fallow-argument-mismatch
# export FCFLAGS=-fallow-argument-mismatch
./configure FC=gfortran CC=gcc \
--enable-fortran=all \
--with-pm=hydra --with-device=ch3:nemesis \
Expand Down
217 changes: 217 additions & 0 deletions interfaces/BAGEL-SH/bagel_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#!/bin/bash
# Common functions for BAGEL BASH interface
# NOTE: This file is meant to be sourced, not executed!

function exec_bagel {
local input_file=$1
local output_file=$2
local timestep=$3
# BAGEL does not print its input so we will prepend it.
cp "$input_file" "$output_file"
$BAGELEXE "$input_file" >> "$output_file" 2>&1
local exitcode=$?

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

function bagel_error {
local errmsg="$1"
local bagel_output="$2"
local copy="$3"
if [[ -f "$bagel_output" && -n "$copy" ]]; then
cp "$bagel_output" "$copy"
fi
>&2 echo -e "${errmsg}"
>&2 echo "Inspect file $(dirname "$0")/$copy"
exit 2
}

function file_exists {
if [[ ! -f $1 ]];then
>&2 echo "File \"$1\" does not exist!"
exit 2
fi
}

### FUNCTIONS FOR GENERATING BAGEL INPUT

# This function must be called first to start the bagel input
function specify_molecule {
# XYZ coordinates provided by ABIN in geom.dat
local xyz=$1
# Name of the bagel input file we are creating
local inp=$2
local natom
natom=$(wc -l < "$xyz")

# NOTE: $basis and $df_basis MUST be defined at the top of the file!
cat > "$inp" << EOF
{"bagel": [
{
"title": "molecule",
"basis": "$basis",
"df_basis": "$df_basis",
"angstrom": "true",
"geometry": [
EOF

# Geometry specification in json format
awk -v natom="$natom" '{printf "\t{\"atom\": \"%s\", \"xyz\": [ %.10f, %.10f, %.10f ]}", $1, $2, $3, $4} NR != natom {print ","}END{print ""}' "$xyz" >> "$inp"
echo -e " ]\n }," >> "$inp"
}

function generate_hf_orbitals {
local inp=$1
cat >> "$inp" << EOF
{
"title" : "$hf_variant",
"charge" : $charge,
"nspin" : $nspin
},
EOF
}

function load_orbitals {
local orbfile=$1
local inp=$2
cat >> "$inp" << EOF
{
"title": "load_ref",
"file": "$orbfile",
"continue_geom": false
},
EOF
}

function save_orbitals {
local orbfile=$1
local inp=$2
cat >> "$inp" << EOF
{
"title": "save_ref",
"file": "$orbfile"
},
EOF
}

function print_molden {
local moldenfile=$1
local inp=$2
cat >> "$inp" << EOF
{
"title": "print",
"file": "$moldenfile",
"orbitals": true
}
EOF
}

# Specify for which state we calculate the forces
# (for now we only support one target state)
# This needs to be called before the CAS section.
function print_forces {
local target_state=$1
local inp=$2
cat >> "$inp" << EOF
{
"title" : "forces",
"export" : true,
"grads" : [
{
"title": "force",
"target": $target_state
}
],
EOF
}

function print_cas {
local method=$1
local inp=$2
# 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
}

function print_casscf {
local method=$1
local thresh=$2
local maxiter=$3
local inp=$4
cat >> "$inp" << EOF
"method": [{
"title": "$method",
"nspin": $nspin,
"charge": $charge,
"maxiter": $maxiter,
"thresh": $thresh,
"nact": $nact,
"nclosed": $nclosed,
"nstate": $nstates,
EOF
# NOTE: We have to leave this JSON section unclosed since we might need to append CASPT2 input
}

function print_caspt2 {
# XMS-CASPT2 - "true" or "false"
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": {
"method": "caspt2",
"xms": "$xms",
"shift": $shift,
"shift_imag": true,
"maxiter": $maxiter,
"thresh": $thresh
}
EOF
}

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

# Execute BAGEL
exec_bagel "$input" "$output" "$timestep"
local returncode=$?
if grep -q "$casscf_error" "$output"; then
error=true
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,
# further increase would lead to inaccurate results.
if awk "BEGIN{exit !($thresh_CASSCF >= $max_thresh_CASSCF)}" ;then
return 1
fi
# Retry again 5*thresh
thresh_CASSCF=$(awk "BEGIN{print $thresh_CASSCF*5}")
maxiter_CASSCF=600
>&2 echo "Trying again with thresh=$thresh_CASSCF and maxiter=$maxiter_CASSCF"
converge_casscf "$input" "$output"
returncode=$?
fi
return $returncode
}
57 changes: 5 additions & 52 deletions interfaces/BAGEL-SH/r.bagel-sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# You should't need to modify this file.
# Specification of CAS wavefunction is in file "bagel.inp"

set -u

cd "$(dirname "$0")" || exit 1
source bagel.inp
source bagel_common.sh

timestep=$1
ibead=$2
Expand All @@ -30,8 +30,6 @@ file_exists "$geom"

rm -f "../engrad.dat.$ibead" ../nacm.dat ENERGY.out FORCE_*

### GENERATE BAGEL INPUT
#
# Determine for which state we need to calculate forces
for ((ist=0; ist<nstate; ist++))
do
Expand All @@ -45,6 +43,7 @@ do
fi
done

### GENERATE BAGEL INPUT
function generate_input {
local input=$1
local method=$2
Expand All @@ -60,71 +59,26 @@ function generate_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

print_cas "$method" "$input"
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

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

# Execute BAGEL
exec_bagel "$input" "$output" "$timestep"
local returncode=$?
if grep -q "$casscf_error" "$output"; then
error=true
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,
# further increase would lead to inaccurate results.
if awk "BEGIN{exit !($thresh_CASSCF >= $max_thresh_CASSCF)}" ;then
return 1
fi
# Retry again 5*thresh
thresh_CASSCF=$(awk "BEGIN{print $thresh_CASSCF*5}")
maxiter_CASSCF=600
>&2 echo "Trying again with thresh=$thresh_CASSCF and maxiter=$maxiter_CASSCF"
converge_casscf "$input" "$output"
returncode=$?
fi
return $returncode
}

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

### CHECK CHONVERGENCES + RESTART BAGEL JOB with convergence threshold and increased itterations ###

smith_error='EXCEPTION RAISED: SMITH convergence not reached'
if grep -q "$smith_error" "$input.out"; then
error=true
Expand All @@ -150,7 +104,6 @@ if [[ $returncode -ne 0 ]];then
fi

# NOW IT'S TIME TO COLLECT ALL THE DATA FOR ABIN

# Extract energy.
file_exists "ENERGY.out"
cat ENERGY.out > "../engrad.dat.$ibead"
Expand Down
11 changes: 9 additions & 2 deletions interfaces/BAGEL-SH/r.bagel-sh.nacm
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ set -u

cd "$(dirname "$0")" || exit 1

### READ INPUT PARAMETERS ###
source bagel.inp
timestep=$1
ibead=$2
input=input$ibead.nacm
Expand All @@ -26,6 +24,12 @@ thresh="1.0e-8"
ORBITAL_FILE=orbitals

file_exists "$geom"
file_exists bagel.inp
file_exists bagel_common.sh

### READ INPUT PARAMETERS ###
source bagel.inp
source bagel_common.sh

rm -f ../nacm.dat NACME_*.out

Expand Down Expand Up @@ -62,6 +66,9 @@ done
sed -i '$ s/,$//' "$input.json"
echo " ]," >> "$input.json"

# Need to pass different thresholds
#print_cas "$method" "$input"

# CAS section
if [[ $method = "xms_caspt2" ]]; then

Expand Down
2 changes: 1 addition & 1 deletion interfaces/BAGEL/bagel.inp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
basis=cc-pvdz
df_basis=cc-pvdz-jkfit
nstates=1
istate=0 # state to be calculated, 0=ground state
target_state=0 # state to be calculated, 0=ground state
nact=8
nclosed=14
maxiter_CASSCF=250 # maximum number of CASSCF iterations
Expand Down
Loading