#!/bin/bash 

#PBS -j oe 


 case=`echo $PWD | awk -F "/" ' {  split($0,pole,"/") ; print pole[NF] } ' ` 
  echo "case = $case" 

 parentCase=`echo $PWD | awk -F "/" ' {  split($0,pole,"/") ; print pole[NF-1] } ' ` 
  echo "parentCase = $parentCase" 

 
  parentDirectory=${PWD%%/${case}} 
   echo "parent directory: " 
   echo $parentDirectory 
      
    mySCRATCH=${parentDirectory} 
      
 
   cd ${mySCRATCH}/${case}  
  echo "in $PWD" 

  test -f BACKUPvol.struct && rm BACKUPvol.struct 
  test -f BACKUP.struct && rm BACKUP.struct 
  test -f A*[0-9]*\.[0-9]*\.scf && rm A*[0-9]*\.[0-9]*\.scf  
test -f A*[0-9]*\.[0-9]*B*[0-9]*\.[0-9]*\.scf && rm  A*[0-9]*\.[0-9]*B*[0-9]*\.[0-9]*\.scf 
test -f C*[0-9]*\.[0-9]*D*[0-9]*\.[0-9]*\.scf && rm  C*[0-9]*\.[0-9]*D*[0-9]*\.[0-9]*\.scf 
  

# Czech ZDE means HERE  
ZDE=$PWD 

#ls 
printf "\n" 

  

  pocetPrvku=` sed -n "4p" ${case}.struct | awk ' { print NF } ' `
  #echo "number of fields is  $pocetPrvku" 

  if test $pocetPrvku == 6 
   then 
 A=` sed -n "4p" ${case}.struct | awk ' { print $1 } ' ` 
  echo "A = $A" 
  APuvodni=$A   # puvodni means original 
  echo "APuvodni = $A" 


 
 B=` sed -n "4p" ${case}.struct | awk ' { print $2 } ' ` 
  echo "B = $B" 
  
 C=` sed -n "4p" ${case}.struct | awk ' { print $3 } ' ` 
  echo "C = $C" 

 alpha=` sed -n "4p" ${case}.struct | awk ' { print $4 } ' ` 
  echo "alpha = $alpha" 
 
 beta=` sed -n "4p" ${case}.struct | awk ' { print $5 } ' ` 
  echo "beta = $beta" 

 gamma=` sed -n "4p" ${case}.struct | awk ' { print $6 } ' ` 
  echo "gamma = $gamma" 

  else 
 A=` sed -n "4p" ${case}.struct | awk ' { print $1 } ' ` 
  echo "A = $A" 
  APuvodni=$A 
  echo "APuvodni = $A" 
 
 B=` sed -n "4p" ${case}.struct | awk ' { print $2 } ' ` 
  echo "B = $B" 
  
 C=` sed -n "4p" ${case}.struct | awk ' { print $3 } ' ` 
  echo "C = $C" 

 alpha=` sed -n "4p" ${case}.struct | awk ' { print $4 } ' ` 
  echo "alpha = $alpha" 
 
 beta=` sed -n "4p" ${case}.struct | awk ' { print substr($5,1,9) } ' ` 
  echo "beta = $beta" 

 gamma=` sed -n "4p" ${case}.struct | awk ' { print substr($5,10) } ' ` 
  echo "gamma = $gamma" 
 fi 

  sed -n -e "4p" ${case}.struct 

  volPuvodni=`echo "$A  $B  $C " | awk ' { printf "%.10f", $1*$2*$3 } ' ` 
  echo "initial volume volPuvodni is $volPuvodni" 


    cp ${case}.struct BACKUPvol.struct 
 
      

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  
 function  EQUALIZER() { 
 
     for i in A*.scf 
        do 

  if test $i != ${case}.scf 
  then 
        pomocna=${i%%\.scf} 
        krok=${pomocna##A}   # krok means step 

	 grep :LAT $i | tail -1 | awk ' { printf "%.6f \t %.6f \t %.6f \t", $5, $6, $7 } '  
	grep :ENE $i | tail -1 | awk ' { printf "%.6f \t", $9 } '   
   if test -z $1 
     then 
        printf "%.2f \n" $krok 
     else 
        printf "%s \n" $1 
     fi

   fi  
	done | sort -k4 -n 
 } 

 function  singleEQUALIZER() { 
 
     for i in ${case}.scf 
        do 
	 grep :LAT $i | tail -1 | awk ' { printf "%.6f \t %.6f \t %.6f \t", $5, $6, $7 } '  
	grep :ENE $i | tail -1 | awk ' { printf "%.6f \t", $9 } '   
   if test -z $1 
     then 
        printf "%.2f \n" $step 
     else
        printf "%.3f \n" $1
     fi 
	done  
 }
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  



 #__________________________________________________________ 
  
  function Fingac() { 
 
    parentDirectory=${mySCRATCH}  
    echo "parent directory: " 
    echo $parentDirectory 

   cd ${parentDirectory} 
   test -d ${parentDirectory}/palmex${case}A${step} && rm -rf ${parentDirectory}/palmex${case}A${step} 
   mkdir ${parentDirectory}/palmex${case}A${step} 

    cp -r $case  palmex${case}A${step}/   
    
     cd ${parentDirectory}/palmex${case}A${step}/${case}/  

   echo "in $PWD" 

 sed -n -e "4p" ${case}.struct 


  cd ${parentDirectory}/palmex${case}A${step}/${case} 
   echo "in $PWD" 



       test -f ENE && mv ENE ENE.old 
      EQUALIZER 0.00  | sort -k4 -n  > ENE 

} 
#___________________________________________________________



#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
  
  function snoopyMouse() { 
 
    parentDirectory=${mySCRATCH}  
    echo "parent directory: " 
    echo $parentDirectory 

   cd ${parentDirectory} 
   test -d ${parentDirectory}/lanza${case}A${step} && rm -rf ${parentDirectory}/lanza${case}A${step} 
   mkdir ${parentDirectory}/lanza${case}A${step} 

    cp -r $case  lanza${case}A${step}/   
    
     cd ${parentDirectory}/lanza${case}A${step}/${case}/  

   echo "in $PWD" 
     echo "erasing struct files except BACKUPvol.struct" 

      for i in *\.struct 
        do 
	 test $i == BACKUPvol.struct || rm $i 
	done 
	cp BACKUPvol.struct  ${case}.struct 
    

     cat > palmex${1}.txt <<+
1
1
${step}  
+
  x optimize < palmex${1}.txt ; rm palmex${1}.txt  
   mv  ${case}_vol_*\.struct  ${case}.struct 
      
 sed -n -e "4p" ${case}.struct 

   test -f chybovaHlaska.txt && rm chybovaHlaska.txt 
   echo 2 | x nn | grep -A 2  ERROR   > chybovaHlaska.txt 

    if  test -s chybovaHlaska.txt 
     then 
      echo "mouse: All right RMT spheres do not overlap in the step $step" 
      echo $step >> ${parentDirectory}/STOPnn${case}.txt 
     else 
     echo "mouse: All right RMT spheres do not overlap in the step $step" 
     fi 
     
     rm chybovaHlaska.txt 


  cd ${parentDirectory}/
  rm -rf ${parentDirectory}/lanza${case}A${step}



} 
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


#-----------------------------------------------------------
 function gnuplotStep() { 
     cd ${parentDirectory}/${case} 

         step=$minStep      
        EQUALIZER 0.00   

       test -f ENE && mv ENE ENE.old 

        EQUALIZER 0.00  > ENE  


 test -f ridiciProGnuplot && rm ridiciProGnuplot 

 cat > ridiciProGnuplot <<+ 
 pocetAtomu = 1 

 set terminal postscript eps enhanced 
 set output 
 
 auToAng = 0.529177249 
  

 set encoding iso 
 set pointsize 2 

hladina =  `sort -k4 -n ENE | head -1 | awk ' { printf "%.6f", -\$4 } ' ` 
  print "hladina = ",  hladina

  set xlabel "a (a.u.)"
  set ylabel "E (eV/atom)"
  set ticslevel 0.02

 set grid 

# plot "ENE" using (\$1*\$2*\$3):((\$4 + hladina)*13.6058/pocetAtomu) w p pt 7  ti "" 
 
 f(x) = a0 + a1*x + a2*x**2 + a3*x**3 #+ a4*x**4 
 
 fit f(x) "ENE" using (\$1*\$2*\$3):((\$4 + hladina)*13.6058/pocetAtomu) via a0, a1, a2, a3#, a4 
 print "a0 = ", a0 
 print "a1 = ", a1 
 print "a2 = ", a2 
 print "a3 = ", a3 

 Diskriminant = sqrt(4*a2**2 - 12*a3*a1) 
 x0 = (-2*a2 + Diskriminant)/(6*a3) 
 print  x0  
+

 test -f gnuplotVystup.txt && rm gnuplotVystup.txt 

gnuplot ridiciProGnuplot  2>  gnuplotVystup.txt 
volMin=`tail -1 gnuplotVystup.txt  | awk ' { printf "%.7f ", $1 } ' `

echo "volMin is $volMin" 

 gnuStep=` echo "$volMin  $volPuvodni"  | awk ' { printf "%.3f", 100*($1/$2) - 100 } ' ` 
  echo "gnuStep is $gnuStep" 
}  

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



#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  
  function oneStep() { 
 
    parentDirectory=${mySCRATCH}  
    echo "parent directory: " 
    echo $parentDirectory 

   cd ${parentDirectory} 
   test -d ${parentDirectory}/palmex${case}A${step} && rm -rf ${parentDirectory}/palmex${case}A${step} 
   mkdir ${parentDirectory}/palmex${case}A${step} 

    cp -r $case  palmex${case}A${step}/   
    
     cd ${parentDirectory}/palmex${case}A${step}/${case}/  

   echo "in $PWD" 

     echo "erasing struct files except BACKUPvol.struct" 

      for i in *\.struct 
        do 
	 test $i == BACKUPvol.struct || rm $i 
	done 
	cp BACKUPvol.struct  ${case}.struct 
    

     cat > palmex${1}.txt <<+
1
1
${step}  
+
  x optimize < palmex${1}.txt ; rm palmex${1}.txt  
   mv  ${case}_vol_*\.struct  ${case}.struct 
      
 sed -n -e "4p" ${case}.struct 

   test -f chybovaHlaska.txt && rm chybovaHlaska.txt 
   echo 2 | x nn | grep -A 2  ERROR   > chybovaHlaska.txt 

    if  test -s chybovaHlaska.txt 
     then 
    while test -s chybovaHlaska.txt 
       do 

     echo "RMT spheres overlap" 
  oKolikPridat=`tail -1 chybovaHlaska.txt | awk ' { print ($3 - $6) + 0.002} '`
   echo "We add  $oKolikPridat to A " 
   Anew=` echo "$Anew + $oKolikPridat" | bc -l `
   Bnew=$Anew 

   # echo "Anew = $Anew, Bnew = $Bnew, Cnew = $Cnew " 
   test -f menic.awk && rm menic.awk 
   cat > menic.awk  <<+ 
NR != 4 { print } 
NR == 4 { printf " %9.6f %9.6f %9.6f%10.6f%10.6f%10.6f\n", $Anew, $Bnew, $Cnew, $alpha, $beta, $gamma } 
+

  awk -f menic.awk ${case}.struct > ${case}.struct_new 
  rm menic.awk 

  mv ${case}.struct_new ${case}.struct 
 sed -n -e "4p" ${case}.struct 

   test -f chybovaHlaska.txt && rm chybovaHlaska.txt 
   echo 2 | x nn | grep -A 2  ERROR   > chybovaHlaska.txt 

   done  

     else 
     echo "All right RMT spheres do not overlap" 
     fi 
     
     rm chybovaHlaska.txt 

     if test -f A${step}.scf 
     then 
       echo "no computation, file A${step}.scf already exists" 
       cp  A${step}.scf ${case}.scf   
       test -f ENE && mv ENE ENE.old 
      singleEQUALIZER  > ENE 
     else   
   test -f *.broyd*  && rm *.broyd*
   export SCRATCH=$PWD 
   ( source cellShapeOrtho ) 

    cd ${parentDirectory}/palmex${case}A${step}/${case}/  
    echo "continuing script volumeOrtho in $PWD" 

       test -f ENE && mv ENE ENE.old 
      EQUALIZER | sort -k4 -n > ENE 
      fi 




} 
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



# first three steps 
     
       cd ${parentDirectory}
    test -f STOPnn${case}.txt && rm STOPnn${case}.txt 
    test -f STOP${case}.txt && rm STOP${case}.txt 

      step=0.00 
     increment=1.00 

   echo "step is $step" 
   echo "increment is $increment" 
     minusIncrement=`echo $increment | awk ' { printf "%.2f", -$1 } '` 
   echo "minusIncrement is $minusIncrement" 
 doubleIncrement=`echo $increment | awk ' { printf "%.2f", 2*$1 } '` 
   echo "doubleIncrement is $doubleIncrement" 
minusDoubleIncrement=`echo $increment | awk ' { printf "%.2f", -2*$1 } '` 
   echo "minusDoubleIncrement is $minusDoubleIncrement" 

 tripleIncrement=`echo $increment | awk ' { printf "%.2f", 3*$1 } '` 
   echo "tripleIncrement is $tripleIncrement" 
minusTripleIncrement=`echo $increment | awk ' { printf "%.2f", -3*$1 } '` 
   echo "minusTripleIncrement is $minusTripleIncrement" 

     export > /dev/null 



progression=(0.00 $increment $minusIncrement) 

   
   (step=${progression[0]}; oneStep ) & 
   (step=${progression[1]}; oneStep ) & 
 
    step=${progression[2]} ; oneStep  ; wait 
   

#------------------------------------------------------------------
function  COMPARE_CLEAN() {  

     cd ${parentDirectory}/ 

    test -f nextStep${case}.txt && rm nextStep${case}.txt 

     for step in  ${progression[*]} 
     do 
      cd ${parentDirectory}/palmex${case}A${step}/${case}/  

           test -f ENE &&  mv  ENE  ENE.old 
        if test "$step" = "0.00" 
            then 
           EQUALIZER 0.00 > ENE
      
        elif test "$step" = "$gnuStep" 
            then 
           singleEQUALIZER $gnuStep > ENE 
        
        else 
           singleEQUALIZER  > ENE 
         fi 

   sort -k4 -n ENE | head -1 | awk ' { print $5, $4 } ' >> ${parentDirectory}/nextStep${case}.txt  
     done 
   
      echo "The file  nextStep${case}.txt" 
      sort -k2 -n  ${parentDirectory}/nextStep${case}.txt 

     cd ${parentDirectory}/ 

     test -f minimum.txt && rm minimum.txt 
   sort -k2 -n nextStep${case}.txt | head -1 > minimum.txt 
   
    read minStep  minENE  nic < minimum.txt ; rm minimum.txt 
      echo "minStep is $minStep" 
      echo "minENE is $minENE" 


     test -f secondMinimum.txt && rm secondMinimum.txt 

     sort -k2 -n  nextStep${case}.txt  | sed -n -e "2p" > secondMinimum.txt 
     read secondLowestENEstep secondLowestENE  < secondMinimum.txt 
        rm secondMinimum.txt 

      echo "secondLowestENEstep is $secondLowestENEstep" 
      echo "secondLowestENE is $secondLowestENE" 

        rm -rf ${parentDirectory}/${case}  
  mv ${parentDirectory}/palmex${case}A${minStep}/${case} ${parentDirectory}/ 
 
   rmdir ${parentDirectory}/palmex${case}A${minStep} 



     cd ${parentDirectory}/ 
       while test -s ${parentDirectory}/nextStep${case}.txt 
    do 
   read    myStep  nic < ${parentDirectory}/nextStep${case}.txt 
       if test "$myStep" = "$minStep"
         then
        echo "adresar A${myStep} has been already shifted to ${case}" 

         else

    cp ${parentDirectory}/palmex${case}A${myStep}/${case}/A${myStep}.scf  ${parentDirectory}/${case}/ 
    rm -rf ${parentDirectory}/palmex${case}A${myStep} 

         fi

   sed -i -e "1d" ${parentDirectory}/nextStep${case}.txt 
    done 

       rm ${parentDirectory}/nextStep${case}.txt 

     echo "what was the last step?" 
      lastStep=` echo ${progression[*]} | awk ' { print $NF } ' `
        echo "last step is $lastStep" 
}
#-----------------------------------------------------------------

   COMPARE_CLEAN 

#####################################################
            step=$minStep 
#####################################################
        echo "step is $step" 

        cd ${parentDirectory}/ 

       case $step in 
           0.00) 
           echo "first three steps are close to minimum" 
           echo "Making additional steps for a fit" 
         echo ${progression[*]} > ${parentDirectory}/STOP${case}.txt            
halfIncrement=`echo "0.5 $increment" | awk ' { printf "%.2f", $1*$2 } '` 
   echo "halfIncrement is $halfIncrement" 

oneAndHalfIncrement=`echo "1.5 $increment" | awk ' { printf "%.2f", $1*$2 } '` 
   echo "oneAndHalfIncrement is $oneAndHalfIncrement" 


minusSafeIncrement=`echo "0.2 $minusIncrement" | awk ' { printf "%.2f", $1*$2 } '` 
   echo "first guess of minusSafeIncrement is $minusSafeIncrement" 

 for i in 0.4 0.5 0.6 
  do 
   cd ${parentDirectory} 
  test -f STOPnn${case}.txt && rm STOPnn${case}.txt 
 
snoopyStep=`echo "$i $minusIncrement" | awk ' { printf "%.2f", $1*$2 } '` 
  echo "snoopyStep is $snoopyStep" 

  (step=$snoopyStep; snoopyMouse) 
   if test -f STOPnn${case}.txt 
    then 
      echo "$step $snoopyStep already overlaps RMT"
        break
    else 
       echo "step $snoopyStep is still OK" 
     minusSafeIncrement=${snoopyStep} 
    fi 
  done 

     echo "minusSafeIncrement is $minusSafeIncrement" 
     printf "\n" 

   cd ${parentDirectory} 
  test -f STOPnn${case}.txt && rm STOPnn${case}.txt 


progression=(0.00 $oneAndHalfIncrement  $halfIncrement  $minusSafeIncrement) 

          (step=${progression[0]}; Fingac )  
          (step=${progression[1]}; oneStep ) & 
          (step=${progression[2]}; oneStep ) & 
           step=${progression[3]} ; oneStep  ; wait 

      COMPARE_CLEAN 

   gnuplotStep 
  progression=(0.00  $gnuStep) 

          (step=${progression[0]}; Fingac )  
           step=${progression[1]}; oneStep 
      COMPARE_CLEAN 

           ;;

           $increment)
           echo "energy decreases going upwards" 
           actualIncrement=$increment 
progression=(0.00 \
`echo "$step  $increment" | awk ' { printf "%.2f", $1+$2 }'` \
`echo "$step  $doubleIncrement" | awk ' { printf "%.2f", $1+$2 } '` \
`echo "$step  $tripleIncrement" | awk ' { printf "%.2f", $1+$2 } '` )
           ;;

           $minusIncrement)
           echo "energy decreases going downwards" 
           actualIncrement=$minusIncrement 
progression=(0.00 \
`echo "$step  $minusIncrement" | awk ' { printf "%.2f", $1+$2 }'` \
`echo "$step  $minusDoubleIncrement" | awk ' { printf "%.2f", $1+$2 } '` \
`echo "$step  $minusTripleIncrement" | awk ' { printf "%.2f", $1+$2 } '` )
           ;;

           *)
           echo "something unusual happened when comparing energies" 
           exit 0 
           ;; 
        esac  

    
      cd ${parentDirectory} 
       test -f STOPnn${case}.txt && rm STOPnn${case}.txt 

#nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
# main cycle 

 insurance=1   
  
until test -f ${parentDirectory}/STOP${case}.txt -o -f ${parentDirectory}/STOPnn${case}.txt 
      do  

     echo "number of main cycles: $insurance" 
     if test $insurance -gt 20
       then
      echo "too much cycles, probably looping" 
        break 
       fi 
     let insurance=insurance+1

  
   (step=${progression[0]}; snoopyMouse)  
   (step=${progression[1]}; snoopyMouse)  
   (step=${progression[2]}; snoopyMouse)  
   (step=${progression[3]}; snoopyMouse)  

      if test -f STOPnn${case}.txt 
       then
       numberOverlaps=`wc -l STOPnn${case}.txt | awk ' { print $1 } '` 
       else 
       numberOverlaps=0 
       fi 

      echo "file STOPnn${case}.txt contains $numberOverlaps lines" 


  if  test "$numberOverlaps" = "0" -o "$numberOverlaps" = "1" 
        then 
        echo "three steps" 
          (step=${progression[0]}; Fingac )  
          (step=${progression[1]}; oneStep ) & 
          (step=${progression[2]}; oneStep ) & 
           step=${progression[3]} ; oneStep  ; wait 

      elif  test "$numberOverlaps" = "2" 
        then 
        echo "two steps"
       (step=${progression[0]}; Fingac )  
       (step=${progression[1]}; oneStep ) & 
        step=${progression[2]}; oneStep  ; wait 

      elif  test "$numberOverlaps" = "3" 
        then 
       (step=${progression[0]}; Fingac )  
        step=${progression[1]}; oneStep 
 
  else  
      echo "too many  RMT overlap stops: $numberOverlaps"
      echo ${progression[*]} >> ${parentDirectory}/STOP${case}.txt 
  fi 

   COMPARE_CLEAN 


if test "$minStep" = "0.00" 
        then 
    echo ${progression[*]} >> ${parentDirectory}/STOP${case}.txt 
      gnuplotStep 
       
    progression=(0.00  $gnuStep) 
         
   (step=${progression[0]}; Fingac )  
   (step=${progression[1]}; oneStep ) 
         COMPARE_CLEAN   

elif test "$minStep" = "${progression[2]}" 
        then 
    echo ${progression[*]} >> ${parentDirectory}/STOP${case}.txt 
      gnuplotStep 
    
  progression=(0.00  \
`echo "$lastStep $actualIncrement" | awk ' { printf "%.2f", $1+$2 }'` \
   $gnuStep) 

   (step=${progression[0]}; Fingac )  
   (step=${progression[1]}; oneStep ) & 
   (step=${progression[2]}; oneStep ) ; wait  
         COMPARE_CLEAN   
else  
 
     cd ${parentDirectory} 

#####################################################
            step=$lastStep 
#####################################################
        echo "step is $step" 

      if test ` echo "$actualIncrement > 0.0" | bc -l ` = "1" 
          then           
progression=(0.00 \
`echo "$step  $increment" | awk ' { printf "%.2f", $1+$2 }'` \
`echo "$step  $doubleIncrement" | awk ' { printf "%.2f", $1+$2 } '` \
`echo "$step  $tripleIncrement" | awk ' { printf "%.2f", $1+$2 } '` )
           
      elif test ` echo "$actualIncrement < 0.0" | bc -l ` = "1" 
          then           
progression=(0.00 \
`echo "$step  $minusIncrement" | awk ' { printf "%.2f", $1+$2 }'` \
`echo "$step  $minusDoubleIncrement" | awk ' { printf "%.2f", $1+$2 } '` \
`echo "$step  $minusTripleIncrement" | awk ' { printf "%.2f", $1+$2 } '` )
  else 
     echo "something unusual happened when deciding about progression" 
      echo ${progression[*]} >> ${parentDirectory}/STOP${case}.txt 
  fi  
    
    echo "progression is ${progression[*]} " 
fi 
 
      done 
#nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
# end of main cycle          

      cd ${parentDirectory}
   test -f STOPnn${case}.txt && rm STOPnn${case}.txt 
   test -f STOP${case}.txt && rm STOP${case}.txt 
   test -f progression${case}.txt && rm progression${case}.txt 


    


   echo y | clean_lapw  1> /dev/null  2> /dev/null 

   cd ${parentDirectory}/${case}              
   echo "back in $PWD" 


       step=$minStep 
     
        EQUALIZER  
   
    test -f ENE && mv ENE ENE.old 
        EQUALIZER > ENE 
  
 echo "Making file for gnuplot  showPlot" 
  test -f showPlot && rm showPlot 
 cat > showPlot <<+

 pocetAtomu = 1 
# number of atoms 

 set terminal x11  font "rk16"  
 
 auToAng = 0.529177249 

  set title "`pwd | awk -F/ ' { print \$NF } '`"  

 set encoding iso 
 set pointsize 2 

hladina =  `sort -k4 -n ENE | head -1 | awk ' { printf "%.6f", -\$4 } ' ` 
  print "hladina = ",  hladina

  set xlabel "volume a\*b\*c (bohr^3)"
  set ylabel "E (eV/cell)"
  set ticslevel 0.02

 set grid 

 plot "ENE" using (\$1*\$2*\$3):((\$4 + hladina)*13.6058/pocetAtomu) w p pt 7  ti "" 
 
 f(x) = a0 + a1*x + a2*x**2 + a3*x**3 #+ a4*x**4 
 
 fit f(x) "ENE" using (\$1*\$2*\$3):((\$4 + hladina)*13.6058/pocetAtomu) via a0, a1, a2, a3#, a4 

 replot f(x)  
 
replot "ENEactual" using (\$1*\$2*\$3):((\$4 + hladina)*13.6058/pocetAtomu) w p pt 5  ti "" 
+

 test -f ENEactual && rm ENEactual 
( grep :LAT ${case}.scf | tail -1 | awk ' { printf "%.5f \t %.5f \t %.5f \t", $5, $6, $7 } '   
  grep :ENE ${case}.scf | tail -1 | awk ' { print $9 } ' ) > ENEactual 


 echo "optimization finished" 
 echo "You can visualize the results using "
 echo "gnuplot --persist showPlot" 

 hostname 

      
  


