#!/bin/bash

rm -rf *.csv

#-----------------------------------------------------------------------------------
# Purpose: This script extracts the pure Fe lattice parameters a_1 and a_3, elastic
#          stiffness coefficients Cij, and Cij derivatives with respect to lattice
#          parameters dCij/da_k from the data file in PureFe/. It also extracts the
#	   derivatives of the a_k and Cij with respect to solute concentration from
#	   the data files in SoluteStrainMisfits/ and Cij-WithSolutes/.
#
# Author:  Michael R. Fellinger, University of Illinois at Urbana-Champaign
# email:   mfelling@illinois.edu
#-----------------------------------------------------------------------------------

#------------------------------ Pure Fe calculations -------------------------------

# --- Copy the PureFe.csv file to the current directory
cp PureFe/PureFe.csv .
# ------

#-----------------------------------------------------------------------------------


#--------------------------- Header for SoluteEffects.csv --------------------------
echo "Total derivatives of hcp Fe lattice parameters and Cij with respect to solute concentration c_s,,,,,,," >> SoluteEffects.csv
echo ",,,,,,," >> SoluteEffects.csv
echo "Quantity,Tag,Unit,,,,," >> SoluteEffects.csv
echo "da1/dc_s,da1dcs,milli-angstom/(at.%),,,,," >> SoluteEffects.csv
echo "da3/dc_s,da3dcs,milli-angstom/(at.%),,,,," >> SoluteEffects.csv
echo "dC11/dc_s,dC11dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo "dC33/dc_s,dC33dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo "dC12/dc_s,dC12dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo "dC13/dc_s,dC13dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo "dC44/dc_s,dC44dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo "dC66/dc_s,dC66dcs,GPa/(at.%),,,,," >> SoluteEffects.csv
echo ",,,,,,," >> SoluteEffects.csv
echo "Solute,da1dcs,da3dcs,dC11dcs,dC33dcs,dC12dcs,dC13dcs,dC44dcs,dC66dcs" >> SoluteEffects.csv
#-----------------------------------------------------------------------------------


#---------------------------Extract solute derivatives------------------------------

for sol in Al B Cu Mn Si C N; do

   #----- Lattice parameter derivatives from the solute strain misfit tensors ------

   a10=$(grep lattice PureFe/PureFe.csv | head -1 | cut -f4 -d, | awk '{print $1}')
   a30=$(grep lattice PureFe/PureFe.csv | tail -1 | cut -f4 -d, | awk '{print $1}')

   eps11=$(grep $sol SoluteStrainMisfits/misfit-DATA.txt | tail -1 | awk '{print $2}')
   da1dc=$(echo $a10 $eps11 | awk '{printf "%10.6f", 10.0*$1*$2}')

   eps33=$(grep $sol SoluteStrainMisfits/misfit-DATA.txt | tail -1 | awk '{print $4}')
   da3dc=$(echo $a30 $eps33 | awk '{printf "%10.6f", 10.0*$1*$2}')

   #--------------------------------------------------------------------------------


   #--------------------------- Cij total derivatives ------------------------------

   dC11dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $2/100.0}')
   dC33dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $3/100.0}')
   dC12dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $4/100.0}')
   dC13dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $5/100.0}')
   dC44dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $6/100.0}')
   dC66dc=$(grep $sol Cij-WithSolutes/CijDerivatives-total.txt | tail -1 | awk '{printf "%10.6f", $7/100.0}')

   #--------------------------------------------------------------------------------


   echo $sol,$da1dc,$da3dc,$dC11dc,$dC33dc,$dC12dc,$dC13dc,$dC44dc,$dC66dc >> SoluteEffects.csv

done

echo ",,,,,,," >> SoluteEffects.csv

#-----------------------------------------------------------------------------------
