############################################################################
# Below run commands 

# Generate q-points
$QHA_DIR/bin/tetra.x 

cp matdyn.init matdyn.init.tmp
cat  kpts_out >> matdyn.init.tmp
echo  EOF >>matdyn.init.tmp

mv  matdyn.init.tmp matdyn.in

echo ' Recalculating omega(q) from C(R)'
$BIN_DIR/matdyn.x < matdyn.in > matdyn.out

nmodes=`head -1 frequency | cut -c 13-16 `
nkpt=`head -1 frequency | cut -c 23-26 `
natoms=`echo "scale=0; $nmodes/3" | bc -l`
#
cat >phdos1.in <<EOF
$nkpt  $nmodes
$atoms
EOF

# Calculate partial phonon DOS
$QHA_DIR/bin/Partial_phonon_DOS.x < phdos1.in 

#rm -f phdos1.in

cat >phdos.in <<EOF
$delta_e
$atoms
EOF

# Calculate total  phonon DOS and atom projected phonon DOS
$QHA_DIR/bin/phonon_dos.x <frequency 

#rm -f phdos.in

# Remove NaN from  all phonon DOS files

cat >name <<EOF
PHDOS.out
EOF

cp PHDOS.out PHDOS.out.copy

$QHA_DIR/bin/Ghost_DOS.x <name  >out 

mv out PHDOS.out
#rm -f name

# Atomic related properties

cat >atom_info <<EOF
$natoms
$atoms
$mass
EOF

for Atom in $atoms

do 

cat > atom_name <<EOF
$Atom
EOF


$QHA_DIR/bin/atom_info.x < atom_info > atom_mass

cat >name <<EOF
projected_DOS.$Atom
EOF

$QHA_DIR/bin/Ghost_DOS.x <name  >out

mv out projected_DOS.$Atom

cp projected_DOS.$Atom projected.DOS

echo "# $Sysname $Atom  $SysInfo" >>Thermodynamics.$Atom

$QHA_DIR/bin/Atom_projected_properties.x >>Thermodynamics.$Atom

# 
# Mean Square Displacement calculations for each atoms 

cat name Temperature atom_mass > displacement.in

$QHA_DIR/bin/Mean_square_displacement.x < displacement.in

mv Displacements Displacements.$Atom

done

# Debye Temperature calculations

$QHA_DIR/bin/Debye.x >> Theta_D

#rm -f T_Debey.in

# Finally, thermodynamic properties
#
# Parameters required for QHA calculations
# Total Phonon DOS file
# output file for C_V, S, Internal energy
#
cat >fqha.in <<EOF
PHDOS.out
$Sysname.QHA.out
EOF

cat Temperature >> fqha.in

$QHA_DIR/bin/F_QHA.x <fqha.in 

#rm -f fqha.in

echo 'Phonon DOS and Quasiharmonic calculations have finished.' 
echo 'Now you can analyse these data using Gnuplot or xmgrace'
echo 'Enjoy!'


