#!/bin/bash
set -e

# Runs the whole tutorial: displacement patterns, LAMMPS forces, harmonic and
# cubic force constants, phonon dispersion, and thermal conductivity.
# The paths below can be overridden with environment variables, e.g.
#   LAMMPS=/path/to/lmp ALAMODE_ROOT=/path/to/alamode bash run_all.sh
# python3 must provide NumPy and PyYAML.

# Binaries
LAMMPS=${LAMMPS:-lmp}
ALAMODE_ROOT=${ALAMODE_ROOT:-${HOME}/src/alamode}
ALM=${ALM:-${ALAMODE_ROOT}/_build/alm/alm}
ANPHON=${ANPHON:-${ALAMODE_ROOT}/_build/anphon/anphon}

# Generate displacement patterns

cat << EOF > si_alm0.in
&general
  PREFIX = si222
  MODE = suggest
  KD = Si
/

&interaction
  NORDER = 2  # 1: harmonic, 2: cubic, ..
/

&cell
  20.406 # factor in Bohr unit
  1.0 0.0 0.0 # a1
  0.0 1.0 0.0 # a2
  0.0 0.0 1.0 # a3
/

&cutoff 
  Si-Si 7.3 7.3
/


&position
  1 0.0000000000000000 0.0000000000000000 0.0000000000000000   
  1 0.0000000000000000 0.0000000000000000 0.5000000000000000
  1 0.0000000000000000 0.2500000000000000 0.2500000000000000
  1 0.0000000000000000 0.2500000000000000 0.7500000000000000
  1 0.0000000000000000 0.5000000000000000 0.0000000000000000
  1 0.0000000000000000 0.5000000000000000 0.5000000000000000
  1 0.0000000000000000 0.7500000000000000 0.2500000000000000
  1 0.0000000000000000 0.7500000000000000 0.7500000000000000
  1 0.1250000000000000 0.1250000000000000 0.1250000000000000
  1 0.1250000000000000 0.1250000000000000 0.6250000000000000
  1 0.1250000000000000 0.3750000000000000 0.3750000000000000
  1 0.1250000000000000 0.3750000000000000 0.8750000000000000
  1 0.1250000000000000 0.6250000000000000 0.1250000000000000
  1 0.1250000000000000 0.6250000000000000 0.6250000000000000
  1 0.1250000000000000 0.8750000000000000 0.3750000000000000
  1 0.1250000000000000 0.8750000000000000 0.8750000000000000
  1 0.2500000000000000 0.0000000000000000 0.2500000000000000
  1 0.2500000000000000 0.0000000000000000 0.7500000000000000
  1 0.2500000000000000 0.2500000000000000 0.0000000000000000
  1 0.2500000000000000 0.2500000000000000 0.5000000000000000
  1 0.2500000000000000 0.5000000000000000 0.2500000000000000
  1 0.2500000000000000 0.5000000000000000 0.7500000000000000
  1 0.2500000000000000 0.7500000000000000 0.0000000000000000
  1 0.2500000000000000 0.7500000000000000 0.5000000000000000
  1 0.3750000000000000 0.1250000000000000 0.3750000000000000
  1 0.3750000000000000 0.1250000000000000 0.8750000000000000
  1 0.3750000000000000 0.3750000000000000 0.1250000000000000
  1 0.3750000000000000 0.3750000000000000 0.6250000000000000
  1 0.3750000000000000 0.6250000000000000 0.3750000000000000
  1 0.3750000000000000 0.6250000000000000 0.8750000000000000
  1 0.3750000000000000 0.8750000000000000 0.1250000000000000
  1 0.3750000000000000 0.8750000000000000 0.6250000000000000
  1 0.5000000000000000 0.0000000000000000 0.0000000000000000
  1 0.5000000000000000 0.0000000000000000 0.5000000000000000
  1 0.5000000000000000 0.2500000000000000 0.2500000000000000
  1 0.5000000000000000 0.2500000000000000 0.7500000000000000
  1 0.5000000000000000 0.5000000000000000 0.0000000000000000
  1 0.5000000000000000 0.5000000000000000 0.5000000000000000
  1 0.5000000000000000 0.7500000000000000 0.2500000000000000
  1 0.5000000000000000 0.7500000000000000 0.7500000000000000
  1 0.6250000000000000 0.1250000000000000 0.1250000000000000
  1 0.6250000000000000 0.1250000000000000 0.6250000000000000
  1 0.6250000000000000 0.3750000000000000 0.3750000000000000
  1 0.6250000000000000 0.3750000000000000 0.8750000000000000
  1 0.6250000000000000 0.6250000000000000 0.1250000000000000
  1 0.6250000000000000 0.6250000000000000 0.6250000000000000
  1 0.6250000000000000 0.8750000000000000 0.3750000000000000
  1 0.6250000000000000 0.8750000000000000 0.8750000000000000
  1 0.7500000000000000 0.0000000000000000 0.2500000000000000
  1 0.7500000000000000 0.0000000000000000 0.7500000000000000
  1 0.7500000000000000 0.2500000000000000 0.0000000000000000
  1 0.7500000000000000 0.2500000000000000 0.5000000000000000
  1 0.7500000000000000 0.5000000000000000 0.2500000000000000
  1 0.7500000000000000 0.5000000000000000 0.7500000000000000
  1 0.7500000000000000 0.7500000000000000 0.0000000000000000
  1 0.7500000000000000 0.7500000000000000 0.5000000000000000
  1 0.8750000000000000 0.1250000000000000 0.3750000000000000
  1 0.8750000000000000 0.1250000000000000 0.8750000000000000
  1 0.8750000000000000 0.3750000000000000 0.1250000000000000
  1 0.8750000000000000 0.3750000000000000 0.6250000000000000
  1 0.8750000000000000 0.6250000000000000 0.3750000000000000
  1 0.8750000000000000 0.6250000000000000 0.8750000000000000
  1 0.8750000000000000 0.8750000000000000 0.1250000000000000
  1 0.8750000000000000 0.8750000000000000 0.6250000000000000
/

EOF

"${ALM}" si_alm0.in > alm.log


# Generate structure files of LAMMPS
mkdir -p displace; cd displace/

python3 "${ALAMODE_ROOT}/tools/displace.py" --LAMMPS ../Si222.lammps --prefix harm --mag 0.01 -pf ../si222.pattern_HARMONIC >> run.log
python3 "${ALAMODE_ROOT}/tools/displace.py" --LAMMPS ../Si222.lammps --prefix cubic --mag 0.04 -pf ../si222.pattern_ANHARM3 >> run.log

cp ../Si.sw .
cp ../in.sw .

# Run LAMMPS
for ((i=1; i<=1; i++))
do
   cp harm${i}.lammps tmp.lammps
   "$LAMMPS" < in.sw >> run.log
   mv XFSET XFSET.harm${i}
done

for ((i=1; i<=20; i++))
do
   suffix=`echo ${i} | awk '{printf("%02d", $1)}'`
   cp cubic${suffix}.lammps tmp.lammps
   "$LAMMPS" < in.sw >> run.log
   mv XFSET XFSET.cubic${suffix}
done

# Collect data
python3 "${ALAMODE_ROOT}/tools/extract.py" --LAMMPS ../Si222.lammps XFSET.harm* > DFSET_harmonic
python3 "${ALAMODE_ROOT}/tools/extract.py" --LAMMPS ../Si222.lammps XFSET.cubic* > DFSET_cubic

cd ../

# Extract harmonic force constants
cat << EOF > si_alm1.in
&general
  PREFIX = si222_harm
  MODE = optimize
  KD = Si
/

&optimize
 DFSET = displace/DFSET_harmonic
/

&interaction
  NORDER = 1  # 1: harmonic, 2: cubic, ..
/

&cell
  20.406 # factor in Bohr unit
  1.0 0.0 0.0 # a1
  0.0 1.0 0.0 # a2
  0.0 0.0 1.0 # a3
/

&cutoff 
  Si-Si 7.3 7.3
/


&position
  1 0.0000000000000000 0.0000000000000000 0.0000000000000000   
  1 0.0000000000000000 0.0000000000000000 0.5000000000000000
  1 0.0000000000000000 0.2500000000000000 0.2500000000000000
  1 0.0000000000000000 0.2500000000000000 0.7500000000000000
  1 0.0000000000000000 0.5000000000000000 0.0000000000000000
  1 0.0000000000000000 0.5000000000000000 0.5000000000000000
  1 0.0000000000000000 0.7500000000000000 0.2500000000000000
  1 0.0000000000000000 0.7500000000000000 0.7500000000000000
  1 0.1250000000000000 0.1250000000000000 0.1250000000000000
  1 0.1250000000000000 0.1250000000000000 0.6250000000000000
  1 0.1250000000000000 0.3750000000000000 0.3750000000000000
  1 0.1250000000000000 0.3750000000000000 0.8750000000000000
  1 0.1250000000000000 0.6250000000000000 0.1250000000000000
  1 0.1250000000000000 0.6250000000000000 0.6250000000000000
  1 0.1250000000000000 0.8750000000000000 0.3750000000000000
  1 0.1250000000000000 0.8750000000000000 0.8750000000000000
  1 0.2500000000000000 0.0000000000000000 0.2500000000000000
  1 0.2500000000000000 0.0000000000000000 0.7500000000000000
  1 0.2500000000000000 0.2500000000000000 0.0000000000000000
  1 0.2500000000000000 0.2500000000000000 0.5000000000000000
  1 0.2500000000000000 0.5000000000000000 0.2500000000000000
  1 0.2500000000000000 0.5000000000000000 0.7500000000000000
  1 0.2500000000000000 0.7500000000000000 0.0000000000000000
  1 0.2500000000000000 0.7500000000000000 0.5000000000000000
  1 0.3750000000000000 0.1250000000000000 0.3750000000000000
  1 0.3750000000000000 0.1250000000000000 0.8750000000000000
  1 0.3750000000000000 0.3750000000000000 0.1250000000000000
  1 0.3750000000000000 0.3750000000000000 0.6250000000000000
  1 0.3750000000000000 0.6250000000000000 0.3750000000000000
  1 0.3750000000000000 0.6250000000000000 0.8750000000000000
  1 0.3750000000000000 0.8750000000000000 0.1250000000000000
  1 0.3750000000000000 0.8750000000000000 0.6250000000000000
  1 0.5000000000000000 0.0000000000000000 0.0000000000000000
  1 0.5000000000000000 0.0000000000000000 0.5000000000000000
  1 0.5000000000000000 0.2500000000000000 0.2500000000000000
  1 0.5000000000000000 0.2500000000000000 0.7500000000000000
  1 0.5000000000000000 0.5000000000000000 0.0000000000000000
  1 0.5000000000000000 0.5000000000000000 0.5000000000000000
  1 0.5000000000000000 0.7500000000000000 0.2500000000000000
  1 0.5000000000000000 0.7500000000000000 0.7500000000000000
  1 0.6250000000000000 0.1250000000000000 0.1250000000000000
  1 0.6250000000000000 0.1250000000000000 0.6250000000000000
  1 0.6250000000000000 0.3750000000000000 0.3750000000000000
  1 0.6250000000000000 0.3750000000000000 0.8750000000000000
  1 0.6250000000000000 0.6250000000000000 0.1250000000000000
  1 0.6250000000000000 0.6250000000000000 0.6250000000000000
  1 0.6250000000000000 0.8750000000000000 0.3750000000000000
  1 0.6250000000000000 0.8750000000000000 0.8750000000000000
  1 0.7500000000000000 0.0000000000000000 0.2500000000000000
  1 0.7500000000000000 0.0000000000000000 0.7500000000000000
  1 0.7500000000000000 0.2500000000000000 0.0000000000000000
  1 0.7500000000000000 0.2500000000000000 0.5000000000000000
  1 0.7500000000000000 0.5000000000000000 0.2500000000000000
  1 0.7500000000000000 0.5000000000000000 0.7500000000000000
  1 0.7500000000000000 0.7500000000000000 0.0000000000000000
  1 0.7500000000000000 0.7500000000000000 0.5000000000000000
  1 0.8750000000000000 0.1250000000000000 0.3750000000000000
  1 0.8750000000000000 0.1250000000000000 0.8750000000000000
  1 0.8750000000000000 0.3750000000000000 0.1250000000000000
  1 0.8750000000000000 0.3750000000000000 0.6250000000000000
  1 0.8750000000000000 0.6250000000000000 0.3750000000000000
  1 0.8750000000000000 0.6250000000000000 0.8750000000000000
  1 0.8750000000000000 0.8750000000000000 0.1250000000000000
  1 0.8750000000000000 0.8750000000000000 0.6250000000000000
/

EOF
"${ALM}" si_alm1.in >> alm.log

# Extract cubic force constants
cat << EOF > si_alm2.in
&general
  PREFIX = si222_cubic
  MODE = optimize
  KD = Si
/

&optimize
 DFSET = displace/DFSET_cubic
 FC2FIX = si222_harm.h5
/

&interaction
  NORDER = 2  # 1: harmonic, 2: cubic, ..
/

&cell
  20.406 # factor in Bohr unit
  1.0 0.0 0.0 # a1
  0.0 1.0 0.0 # a2
  0.0 0.0 1.0 # a3
/

&cutoff 
  Si-Si 7.3 7.3
/


&position
  1 0.0000000000000000 0.0000000000000000 0.0000000000000000   
  1 0.0000000000000000 0.0000000000000000 0.5000000000000000
  1 0.0000000000000000 0.2500000000000000 0.2500000000000000
  1 0.0000000000000000 0.2500000000000000 0.7500000000000000
  1 0.0000000000000000 0.5000000000000000 0.0000000000000000
  1 0.0000000000000000 0.5000000000000000 0.5000000000000000
  1 0.0000000000000000 0.7500000000000000 0.2500000000000000
  1 0.0000000000000000 0.7500000000000000 0.7500000000000000
  1 0.1250000000000000 0.1250000000000000 0.1250000000000000
  1 0.1250000000000000 0.1250000000000000 0.6250000000000000
  1 0.1250000000000000 0.3750000000000000 0.3750000000000000
  1 0.1250000000000000 0.3750000000000000 0.8750000000000000
  1 0.1250000000000000 0.6250000000000000 0.1250000000000000
  1 0.1250000000000000 0.6250000000000000 0.6250000000000000
  1 0.1250000000000000 0.8750000000000000 0.3750000000000000
  1 0.1250000000000000 0.8750000000000000 0.8750000000000000
  1 0.2500000000000000 0.0000000000000000 0.2500000000000000
  1 0.2500000000000000 0.0000000000000000 0.7500000000000000
  1 0.2500000000000000 0.2500000000000000 0.0000000000000000
  1 0.2500000000000000 0.2500000000000000 0.5000000000000000
  1 0.2500000000000000 0.5000000000000000 0.2500000000000000
  1 0.2500000000000000 0.5000000000000000 0.7500000000000000
  1 0.2500000000000000 0.7500000000000000 0.0000000000000000
  1 0.2500000000000000 0.7500000000000000 0.5000000000000000
  1 0.3750000000000000 0.1250000000000000 0.3750000000000000
  1 0.3750000000000000 0.1250000000000000 0.8750000000000000
  1 0.3750000000000000 0.3750000000000000 0.1250000000000000
  1 0.3750000000000000 0.3750000000000000 0.6250000000000000
  1 0.3750000000000000 0.6250000000000000 0.3750000000000000
  1 0.3750000000000000 0.6250000000000000 0.8750000000000000
  1 0.3750000000000000 0.8750000000000000 0.1250000000000000
  1 0.3750000000000000 0.8750000000000000 0.6250000000000000
  1 0.5000000000000000 0.0000000000000000 0.0000000000000000
  1 0.5000000000000000 0.0000000000000000 0.5000000000000000
  1 0.5000000000000000 0.2500000000000000 0.2500000000000000
  1 0.5000000000000000 0.2500000000000000 0.7500000000000000
  1 0.5000000000000000 0.5000000000000000 0.0000000000000000
  1 0.5000000000000000 0.5000000000000000 0.5000000000000000
  1 0.5000000000000000 0.7500000000000000 0.2500000000000000
  1 0.5000000000000000 0.7500000000000000 0.7500000000000000
  1 0.6250000000000000 0.1250000000000000 0.1250000000000000
  1 0.6250000000000000 0.1250000000000000 0.6250000000000000
  1 0.6250000000000000 0.3750000000000000 0.3750000000000000
  1 0.6250000000000000 0.3750000000000000 0.8750000000000000
  1 0.6250000000000000 0.6250000000000000 0.1250000000000000
  1 0.6250000000000000 0.6250000000000000 0.6250000000000000
  1 0.6250000000000000 0.8750000000000000 0.3750000000000000
  1 0.6250000000000000 0.8750000000000000 0.8750000000000000
  1 0.7500000000000000 0.0000000000000000 0.2500000000000000
  1 0.7500000000000000 0.0000000000000000 0.7500000000000000
  1 0.7500000000000000 0.2500000000000000 0.0000000000000000
  1 0.7500000000000000 0.2500000000000000 0.5000000000000000
  1 0.7500000000000000 0.5000000000000000 0.2500000000000000
  1 0.7500000000000000 0.5000000000000000 0.7500000000000000
  1 0.7500000000000000 0.7500000000000000 0.0000000000000000
  1 0.7500000000000000 0.7500000000000000 0.5000000000000000
  1 0.8750000000000000 0.1250000000000000 0.3750000000000000
  1 0.8750000000000000 0.1250000000000000 0.8750000000000000
  1 0.8750000000000000 0.3750000000000000 0.1250000000000000
  1 0.8750000000000000 0.3750000000000000 0.6250000000000000
  1 0.8750000000000000 0.6250000000000000 0.3750000000000000
  1 0.8750000000000000 0.6250000000000000 0.8750000000000000
  1 0.8750000000000000 0.8750000000000000 0.1250000000000000
  1 0.8750000000000000 0.8750000000000000 0.6250000000000000
/

EOF
"${ALM}" si_alm2.in >> alm.log

# Phonon dispersion
cat << EOF > phband.in
&general
  PREFIX = si222
  MODE = phonons
  FCSFILE = si222_harm.h5
/

&cell
  10.203
  0.0 0.5 0.5
  0.5 0.0 0.5
  0.5 0.5 0.0
/

&kpoint
  1  # KPMODE = 1: line mode
  G 0.0 0.0 0.0 X 0.5 0.5 0.0 51
  X 0.5 0.5 1.0 G 0.0 0.0 0.0 51
  G 0.0 0.0 0.0 L 0.5 0.5 0.5 51
/

EOF

"${ANPHON}" phband.in > phband.log

# Thermal conductivity
cat << EOF > RTA.in
&general
  PREFIX = si222_10
  MODE = kappa
  FCSFILE = si222_cubic.h5
/

&cell
  10.203
  0.0 0.5 0.5
  0.5 0.0 0.5
  0.5 0.5 0.0
/

&kpoint
  2
  10 10 10
/

EOF

"${ANPHON}" RTA.in > RTA.log
