#!/bin/bash

# This script extracts the data from the MD runs to form .dat files for analysis. In addition to deriving the average stress tensor.

#Number of atoms in the simulation box
Natoms=$(grep "Atoms\:" Run1/NPT.out | awk '{printf "%.12g\n", $3}')
echo "The total number of atoms is $Natoms."

#Value of time step in [fs]
delt=$(grep "Time step \[fs\]" Run1/NPT.out | awk '{printf "%.12g\n", $5}')
echo "The time step adopted is $delt [fs] "

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat
#!/bin/bash

# This script extracts the data from the MD runs to form .dat files for analysis. In addition to deriving the average stress tensor.

#Number of atoms in the simulation box
Natoms=$(grep "Atoms\:" Run1/NPT.out | awk '{printf "%.12g\n", $3}')
echo "The total number of atoms is $Natoms."

#Value of time step in [fs]
delt=$(grep "Time step \[fs\]" Run1/NPT.out | awk '{printf "%.12g\n", $5}')
echo "The time step adopted is $delt [fs] "

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat
#!/bin/bash

# This script extracts the data from the MD runs to form .dat files for analysis. In addition to deriving the average stress tensor.

#Number of atoms in the simulation box
Natoms=$(grep "Atoms\:" Run1/NPT.out | awk '{printf "%.12g\n", $3}')
echo "The total number of atoms is $Natoms."

#Value of time step in [fs]
delt=$(grep "Time step \[fs\]" Run1/NPT.out | awk '{printf "%.12g\n", $5}')
echo "The time step adopted is $delt [fs] "

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat
grep 'word' filename

Search for "word" anywhere in file

grep: globally search for a regular expression and print matching lines

grep "Atoms\:" Run1/NPT.out
#!/bin/bash

# This script extracts the data from the MD runs to form .dat files for analysis. In addition to deriving the average stress tensor.

#Number of atoms in the simulation box
Natoms=$(grep "Atoms\:" Run1/NPT.out | awk '{printf "%.12g\n", $3}')
echo "The total number of atoms is $Natoms."

#Value of time step in [fs]
delt=$(grep "Time step \[fs\]" Run1/NPT.out | awk '{printf "%.12g\n", $5}')
echo "The time step adopted is $delt [fs] "

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat
awk printf column

extract # atoms from field 3 and create a column

awk '{printf "%.12g\n", $3}')
g\n

prints argument as decimal or exponential float

#!/bin/bash

# This script extracts the data from the MD runs to form .dat files for analysis. In addition to deriving the average stress tensor.

#Number of atoms in the simulation box
Natoms=$(grep "Atoms\:" Run1/NPT.out | awk '{printf "%.12g\n", $3}')
echo "The total number of atoms is $Natoms."

#Value of time step in [fs]
delt=$(grep "Time step \[fs\]" Run1/NPT.out | awk '{printf "%.12g\n", $5}')
echo "The time step adopted is $delt [fs] "

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

extract time step data from first run of NPT.out file and then print it as another column of a table

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

start from scratch

# If exist first erase files.
rm ConservedQuantity.dat
rm Ekin.dat
rm Epot.dat
rm Pt.dat
rm Tt.dat
rm Vt.dat
rm at.dat
rm bt.dat
rm ct.dat
rm Edrift.dat
rm Etotal.dat
rm EkinCalc.dat
rm XYZTotal.xyz

NumOfDirs=`ls */NPT.out |  wc -l`

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

number of directories is number of files under */NPT.out; subtract 1 from word/character count

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

within every directory ...

search for the KE in the ith run

-V inverts match (i.e., search for anything that does not contain INI)

convert it to a float, and add it to a table in Ekinhelp.dat

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

within every directory ...

repeat for potential energy

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

within every directory ...

repeat for pressure

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

cat = "concatenate"; append the contents of the ith run of coordinates-pos.xyz to XYZTotal.xyz

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

use v flag on awk to assign to the variable delt whatever value is in the "delt" column of a file

the f flag with comma separator looks for fields separated by commas

multiply value from first field/column with the time step and multiply by conversion factor 10^(-3)

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

use v flag on awk to assign to the variable Natoms whatever value is in the "Natoms" column of a file

boltzmann's constant

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

use paste operator to horizontally merge files into a table format

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

convert to decimal and add fields 1 (KE) and 2 (PE) to get total energy and horizontally merge the Etotalhelp.dat and Etotal.dat files

for (( i = 1; i <= $NumOfDirs; i++ ))
do

        #Kinetic energy extracted is in [hartree].

        grep "Kinetic energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Ekinhelp.dat

        #Potential energy extracted is in [hartree].

        grep "Potential energy \[hartree\]" Run$i/NPT.out | grep -v "INI"  | awk '{printf "%.12g\n", $5}' >> Epothelp.dat

        #Pressure extracted is in [bar]
        grep "Pressure \[bar\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Pthelp.dat

        #Tempertaure extracred is in [K]

        grep "Temperature \[K\]" Run$i/NPT.out | grep -v "INI" | grep -v "PAR" | awk '{printf "%.12g\n", $4}' >> Tthelp.dat

        #Volume extracred is in [bohr^3] converted to [angstrom^3]

        grep "Cell volume \[bohr^3\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.1482}' >> Vthelp.dat

        #lattice parameter a extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" | awk '{printf "%.12g\n", $5*0.529177}' >> athelp.dat

        #lattice parameter b extracred is in [bohr] converted to [angstrom]
        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $6*0.529177}' >> bthelp.dat

        #lattice parameter c extracred is in [bohr] converted to [angstrom]

        grep "Cell lengths \[bohr\]" Run$i/NPT.out | grep -v "INI" |  awk '{printf "%.12g\n", $7*0.529177}' >> cthelp.dat


        #Drift in energy per atom [K].

        grep "Energy drift per atom \[K\]" Run$i/NPT.out | awk '{printf "%.12g\n", $7}' >> Edrifthelp.dat

        #Conserved quantity [hartree] .

        grep "Conserved quantity \[hartree\]" Run$i/NPT.out | awk '{printf "%.12g\n", $5}' >> ConservedQuantity.dat
 #This generates a file with the xyz coordinates of all atoms for the entire run

        cat Run$i/coordinates-pos.xyz >> XYZTotal.xyz
        
done

NUMOFLINES=$(wc -l < Tthelp.dat)  #This counts the number of data points for the total run
seq 1 $NUMOFLINES > stepnum.dat   #creates a file with a column of consecutive number of step

#We now create the time vector for the whole run [ps]
awk -v delt="$delt"  -F,  '{print $1*delt*10^(-3)}' stepnum.dat > t.dat
rm stepnum.dat
#We also wish to calculate the kinetic energy [Ha] directly from the temperature. 
#This is beacuse sometimes this is more accurate than the kinetic energy that is given in the output file.
awk -v Natoms="$Natoms"  -F, '{printf "%.12g\n", $1*1.5*Natoms*1.38064852*10^(-16)*2.2937*10^(10)}' Tthelp.dat > EkinCalHelp.dat

paste t.dat EkinCalHelp.dat > EkinCalc.dat
rm EkinCalHelp.dat

paste t.dat Ekinhelp.dat > Ekin.dat

paste t.dat Epothelp.dat > Epot.dat

paste t.dat Pthelp.dat > Pt.dat
rm Pthelp.dat

paste t.dat Tthelp.dat > Tt.dat
rm Tthelp.dat
paste t.dat Vthelp.dat > Vt.dat
rm Vthelp.dat

paste t.dat athelp.dat > at.dat
rm athelp.dat

paste t.dat bthelp.dat > bt.dat
rm bthelp.dat

paste t.dat cthelp.dat > ct.dat
rm cthelp.dat

paste t.dat Edrifthelp.dat > Edrift.dat
rm Edrifthelp.dat

paste t.dat ConservedQuantity.dat > ConservedQuantityTemporal.dat
rm ConservedQuantity.dat

### now we produce the total energy by adding the kinetic and potential terms #######
paste Ekinhelp.dat Epothelp.dat | awk '{printf "%.12g\n", $1 + $2 }' > Etotalhelp.dat
paste t.dat Etotalhelp.dat > Etotal.dat

rm Etotalhelp.dat
rm t.dat
rm Ekinhelp.dat
rm Epothelp.dat

### Now we want to extract .dat files for the thermalized data only, for our NPT run ####

# If exist first erase files.
rm Etequilibrated.dat
rm Vtequilibrated.dat
rm atequilibrated.dat
rm btequilibrated.dat
rm ctequilibrated.dat

#We create tables containing only the thermalized data. Therefore, I erase the non-thermalized part of the run. In this case we erase the first
# x [ps].

echo How many \[ps\] are used for thermalization \(e.g. 3\) ?
read -p 'Length of thermalization in [ps] is: ' x
echo "You chose thermalization of $x [ps] "

awk -v y=$x '$1 >= y'  Etotal.dat > EtTEST.dat
awk -v y=$x '$1 >= y'  Vt.dat > VtTEST.dat
awk -v y=$x '$1 >= y'  at.dat > atTEST.dat
awk -v y=$x '$1 >= y'  bt.dat > btTEST.dat
awk -v y=$x '$1 >= y'  ct.dat > ctTEST.dat

#Take only the second coloumn of: the total energy, volume, and for the three cell parameters, from the tables above:

awk '{printf "%.12g\n", $2 }' EtTEST.dat > Etequilibrated.dat
awk '{printf "%.12g\n", $2 }' VtTEST.dat > Vtequilibrated.dat
awk '{printf "%.12g\n", $2 }' atTEST.dat > atequilibrated.dat
awk '{printf "%.12g\n", $2 }' btTEST.dat > btequilibrated.dat
awk '{printf "%.12g\n", $2 }' ctTEST.dat > ctequilibrated.dat

#remove auxiliary data
rm EtTEST.dat
rm VtTEST.dat
rm atTEST.dat
rm btTEST.dat
rm ctTEST.dat

exit 1
#Now we produce the average stress tensor
grep " X " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' > StressAverage.dat

grep " Y " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

grep " Z " */NVT.out | grep -v "X               Y               Z" | grep -v "ROT" | awk '{sumxx+=$3;sumxy+=$4;sumxz+=$5}END{print sumxx/NR,sumxy/NR,sumxz/NR}' >> StressAverage.dat

ask for how long to heat up the system in picoseconds

checks if the first field is greater than the time for thermalization and 

deck

By Refath Bari

deck

  • 10