#!/bin/bash


label='TiO2'

ndecplace=9

echo 'Thermal expansion correction to free energy TiO2, eV/[TiO2]'
echo -e  '\tRutile\t\t\t\t\t\t\t\t\t\tAnatse'
echo -en '\tTotal energy\t\t\tU_correction\t\tF_correction\t'
echo -e  '\tTotal energy\t\t\tU_correction\t\tF_correction\t'


# Set electronic energies for this structure, at exp. volume V(T=300.0):
# From DFT with pbesol.

for T in 0.0 150.0 300.0 450.0 600.0 750.0 900.0 1050.0
  do

  echo -n -e $T

for struc in rutile anatase
  do

  case $struc in
   rutile) nomol='2.0' ;;
   anatase) nomol='2.0' ;;
  esac

  file1='./'$struc'_300.0/'$label'.castep'
  file2='./'$struc'_'$T'/'$label'.castep'



##### Fixed to 300K volume:
# Read in electronic energy, at exp. volume V(T=300.0):
  set -- $( grep 'Final energy =' $file1 | tail -n 1 )
  e_v300=$( echo '( '$4' )/'$nomol | bc -l )

# Read in phonon free energy at this temperature, calculated at exp. volume V(T=300.0):
  set -- $( grep ' '$T'        ' $file1  | tail -n 1 )
  f_v300=$( echo '( '$3' )/'$nomol | bc -l )
  u_v300=$( echo '( '$2' )/'$nomol | bc -l )

# Free energy at T for volume fixed to exp. 300K volume:
  fv300=$( echo '( '$e_v300' )+( '$f_v300' )' | bc -l )
  uv300=$( echo '( '$e_v300' )+( '$u_v300' )' | bc -l )


##### Thermal expansion allowed:
# Read in electronic energy at this temperature, calculated with volume V(T):
  set -- $( grep 'Final energy =' $file2 | tail -n 1 )
  e_voft=$( echo '( '$4' )/'$nomol | bc -l )
  
# Read in phonon free energy at this temperature, calculated with volume V(T):
  set -- $( grep ' '$T'        '  $file2 | tail -n 1 ) ; Tt=$1
  f_voft=$( echo '( '$3' )/'$nomol | bc -l )
  u_voft=$( echo '( '$2' )/'$nomol | bc -l )

# Free energy at T for volume V(T):
  fvoft=$( echo  '( '$e_voft' )+( '$f_voft' )' | bc -l )
  uvoft=$( echo  '( '$e_voft' )+( '$u_voft' )' | bc -l )


# Free energy correction due to thermal expansion (zeroed at 300K volume):
  deltaf=$( echo '( '$fvoft' )-( '$fv300' )' | bc -l )
  deltau=$( echo '( '$uvoft' )-( '$uv300' )' | bc -l )

######Free energy correction due to thermal expansion (not zeroed at 300K volume):
######deltaf=$( echo '( '$fvoft' )             ' | bc -l )
######deltau=$( echo '( '$uvoft' )             ' | bc -l )


# echo -n -e '\t'$e_v300'\t'$u_v300'\t'$f_v300
  echo -n -e '\t'$e_voft'\t'$u_voft'\t'$f_voft


#deltaf=$( python -c "print '%4."$ndecplace"f' % round("$deltaf","$ndecplace")" )
#deltau=$( python -c "print '%4."$ndecplace"f' % round("$deltau","$ndecplace")" )
#echo -n -e '\t'$deltaf'\t'$deltau


done
echo
done
