#!/bin/bash
#SBATCH --job-name=vite_wb97x_d4_n12
#SBATCH --partition=small
#SBATCH --account=project_2001659
#SBATCH --nodes=1
#SBATCH --ntasks=12
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=4000
#SBATCH --time=02:00:00
#SBATCH --output=vite_wb97x_d4_n12_%j.out
#SBATCH --error=vite_wb97x_d4_n12_%j.err

###############################################################################
# Vitamin E (alpha-tocopherol) wB97X-D4/def2-TZVP single-point energy on Roihu
# Geometry: https://pubchem.ncbi.nlm.nih.gov/compound/14985
# PubChem CID 14985, 3D conformer, coordinates converted to Bohr
#
# Performance benchmark — change --ntasks and job-name for scaling series:
#   n6:  --ntasks=6  --job-name=vite_wb97x_d4_n6
#   n12: --ntasks=12 --job-name=vite_wb97x_d4_n12
#   n24: --ntasks=24 --job-name=vite_wb97x_d4_n24
#   n48: --ntasks=48 --job-name=vite_wb97x_d4_n48
#
# Uses ridft (RI-DFT) for the SCF — recommended for hybrid DFT on Roihu.
# MPI parallel via ridft_mpi.
#
# Usage: sbatch vite_wb97x_d4_roihu.sh
###############################################################################

module purge
module load turbomole/8.0

export PARA_ARCH=MPI
export PARNODES=$SLURM_NTASKS
export SLURM_CPU_BIND=none
export PATH=$TURBODIR/bin/$(sysname):$PATH

WORKDIR=$PWD/vite_wb97x_d4_n${SLURM_NTASKS}_$SLURM_JOB_ID
mkdir -p $WORKDIR
cd $WORKDIR

export TURBOTMPDIR=$(echo $PWD | cut -d'/' -f1-3)/TM_TMPDIR/$SLURM_JOB_ID
mkdir -p $TURBOTMPDIR

# Background disk monitor
(while true; do
    sleep 30
    echo "$(date +%s) $(du -s $TURBOTMPDIR 2>/dev/null | awk '{print $1/1048576}')" >> $OLDPWD/disk_usage_$SLURM_JOB_ID.log
done) &
MONITOR_PID=$!

trap "kill $MONITOR_PID 2>/dev/null; wait $MONITOR_PID 2>/dev/null; rm -rf $TURBOTMPDIR" EXIT

# Vitamin E geometry in Bohr
# Source: https://pubchem.ncbi.nlm.nih.gov/compound/14985
cat > coord << 'EOF'
$coord
      1.405767162920     -2.641270014266     -0.232814241796  o
     10.050129725171      2.697772821325     -2.624829398165  o
      1.453766203030     -4.531940865861      1.723997019400  c
     -1.298997644564     -4.851304557934      2.585901042800  c
      3.054742060572     -3.689312047544      3.996959438488  c
     -3.122772196162     -4.840533119799      0.342796294332  c
     -7.824410455798     -5.088654142102     -0.931445939781  c
     -5.856071866073     -5.223013659892      1.221140933833  c
      5.750247210711     -3.066269389102      3.209510619038  c
     -7.810615456082     -2.485934538003     -2.249151871632  c
      2.359700841965     -7.043197732111      0.583736357879  c
      5.739475772576     -1.473230380713      0.865305530180  c
     -8.348620445036     -0.269852871172     -0.463171839806  c
      3.610321501220     -1.367783670549     -0.701088341771  c
     -8.370163321306      2.309245158069     -1.791838182391  c
     -8.776076463657      4.649292849753     -0.088628148865  c
     -7.319286699046     -7.246154303287     -2.798306243919  c
     -6.851201571669      4.736031272629      2.100241463730  c
      7.908503262291     -0.111304860729      0.190106434453  c
      3.633943076078      0.133036709597     -2.874651173858  c
     -4.059698341309      4.856784763301      1.325075863206  c
      5.793521935850      1.498552708960     -3.517724927779  c
     -8.630378589936      7.031481430982     -1.734579484936  c
      7.932502782346      1.371185177328     -1.989692493397  c
     -2.152964818812      4.835430859630      3.507520407440  c
      0.652900329061      4.801415791835      2.732165834318  c
     10.234566981659     -0.210515475130      1.799397086345  c
      1.332823739760      0.290639857047     -4.513232578573  c
      5.823568579069      3.108221306049     -5.840954058164  c
      2.425274333769      4.669512917831      5.010041540976  c
      1.259880316600      7.151479031258      1.158591003611  c
     -1.523308119410     -6.572278015752      3.719358690763  h
     -1.830199619959     -3.261100138527      3.809876565616  h
      3.065891443905     -5.132495785038      5.483417901121  h
      2.221372899599     -1.974007767692      4.819935106523  h
     -2.614057960030     -6.328692335821     -0.994751760399  h
     -2.988223705773     -3.030175622720     -0.642695808723  h
     -9.697884800896     -5.392522081069     -0.094486299430  h
     -6.325290829042     -3.855418961942      2.703064054093  h
     -6.002525630189     -7.081748142279      2.133689613728  h
      6.817564449072     -4.805384216411      2.856131859170  h
      6.654859041454     -2.072651464296      4.785164148333  h
     -9.271751590467     -2.502186181505     -3.722382252344  h
     -6.021611862674     -2.170917215704     -3.244092604630  h
      2.268238104117     -8.579733933442      1.966826808935  h
      1.254400111233     -7.584604227845     -1.078088676496  h
      4.311598815590     -6.913940474491     -0.091651710447  h
     -6.888807118842     -0.235270885581      0.995507650794  h
    -10.165025065278     -0.563705262399      0.489061085850  h
     -6.604781302756      2.519571660600     -2.859911311147  h
     -9.878353632808      2.280143377845     -3.217825413388  h
    -10.674684164404      4.542334358798      0.737937998548  h
     -5.590943309872     -6.945498898500     -3.893213481714  h
     -8.878688584838     -7.382025601867     -4.153806695542  h
     -7.178691085494     -9.067094265902     -1.825853250185  h
     -7.267130261760      6.406549046552      3.259777330335  h
     -7.146565743687      3.118047881190      3.359176917335  h
     -3.610699446418      3.258832467341      0.087305340673  h
     -3.775294580025      6.575868495130      0.211460338124  h
    -10.161245613301      7.038284444541     -3.128252401528  h
     -8.829177763937      8.734880437106     -0.576744371721  h
     -6.844776503308      7.156581291427     -2.771094189683  h
     -2.513524537437      6.477035825927      4.723181135907  h
     -2.524107002973      3.160566715934      4.674237232802  h
      0.989271555032      3.126929593336      1.562425447374  h
     10.677518753387     -2.157311188586      2.344394061457  h
     11.919635645693      0.494541291217      0.829589708995  h
      9.965658973481      0.930123131589      3.504874791056  h
      0.799165120579      2.264836597337     -4.837698530816  h
     -0.337316088965     -0.598476220590     -3.681186225793  h
      1.674864143696     -0.649120877084     -6.325101856443  h
      5.612675158741      5.101126333627     -5.325058863276  h
      7.592730049596      2.861045146740     -6.889374036639  h
      4.345235938187      2.645616384040     -7.206470057526  h
      2.201152831521      6.324723911245      6.232127337804  h
      4.401549772647      4.577672234785      4.403250526037  h
      2.032967218536      2.981231719615      6.140286654758  h
      0.744929984706      8.888515159979      2.159011941976  h
      0.274766158742      7.125400812615     -0.659136424824  h
      3.285099658582      7.239729234925      0.735292382164  h
      9.648185007396      4.048737930576     -3.796837456295  h
$end
EOF

# Run define non-interactively
# wB97X-D functional with def2-TZVP basis, RI approximation
cat > define.inp << 'EOF'

vitamin E wB97X-D4/def2-TZVP
a coord
*
no
b all def2-TZVP
*
eht


y
dft
on
func wb97x
*
ri
on
m 4000
*
dsp
d4
*
*
EOF

define < define.inp > define.out 2>&1

if [ ! -f control ]; then
    echo "ERROR: define failed — control file not created"
    cat define.out
    exit 1
fi

echo "=== Setup complete ==="
grep "symmetry\|functional\|basis" control | head -5

# Run ridft (RI-DFT)
echo "=== Starting ridft ==="
TIME_START=$SECONDS
ridft > ridft.out 2>&1
TIME_END=$SECONDS

if ! grep -q "convergence criteria satisfied\|SCF converged" ridft.out; then
    echo "ERROR: SCF did not converge"
    tail -20 ridft.out
    exit 1
fi

echo "=== RESULTS ==="
echo "System:   Vitamin E (C29H50O2, 81 atoms)"
echo "Method:   wB97X-D4/def2-TZVP"
echo "Tasks:    $SLURM_NTASKS MPI"
echo "Wall:     $((TIME_END - TIME_START)) s"
grep "total energy" ridft.out | tail -1
grep "TOTAL ENERGY" ridft.out | tail -1

echo ""
echo "Memory check: sacct -j $SLURM_JOB_ID --format=JobID,MaxRSS,Elapsed,State"
