#
# loads the functions
# this script uses the functions: m_change m_replace getcolumn m_list
#
module load new cp2k
. /cluster/apps/courses/mmm/m_functions.bash

rm a1a2ene
cp ini0.pdb ini.pdb

delta_angle=80.
angle0=-175.

for i in `m_list 0 4`
do
   a1=`m_change $angle0 $i $delta_angle`
   for j in `m_list 0 4`
   do
     a2=`m_change $angle0 $j $delta_angle`
     echo "Computing angles" $a1 $a2
     m_replace _A1_ $a1 < inp.templ | m_replace _A2_ $a2 > inp.$i.$j
     cp2k.popt -i inp.$i.$j -o out.$i.$j
#
#  Take last configuration and set it as an input for next step
#
     tail -25 ch-pos-1.pdb > opt.$i.$j.pdb
     cp opt.$i.$j.pdb ini.pdb
#
# get the torsional angles reached at the end of optimization
#
     af1=`m_pdbtorsion 5 7 9 15 opt.$i.$j.pdb `
     af2=`m_pdbtorsion 7 9 15 17 opt.$i.$j.pdb `
     ef=`m_getcolumn 'ENERGY|' 9 < out.$i.$j`
     echo $af1 $af2 $ef >> a1a2ene
done
echo " " >> a1a2ene
done

rm *Hessian
rm ch*restart*bak*
rm ch-dump*
rm ch-for* ch-pos*pdb

mkdir Logs
mv opt*pdb inp.?.? out.?.? Logs
