#!/bin/tcsh -f
#
# Create a test-suite for DL-FIND in ChemShell
#
# This is the master script for the creation of a validation suite
#
# Tests various combinations of optimisers, coordinate systems,
# multiple-image combinations, and Hessian evaluations. The system
# to test is ethanol in a simple force field description. ChemShell
# inputs are written. Then they are run once to define the reference 
# energy.
#
# The following files are used:
# start.chm call.chm tail.chm
#

#set verbose

set restart_full = " dump=1 maxdump=10 restart=false \\"
set restart_rest = " dump=0 restart=true \\"

#
# Loops
#
foreach optimiser ( sd cg lbfgs prfo dyn )
#foreach optimiser ( lbfgs prfo )

foreach coords ( cart cart_con mass dlc tc hdlc hdlc-tc dlc_con hdlc_con )
#foreach coords ( cart )

foreach multim ( none dimer_int dimer_noint neb_frozen neb_perp neb_free dimer_w neb_w )
#foreach multim ( none )

foreach hessian ( identity diagonal twopoint onepoint external )

    set optstring = ""
    set namestring = ""

    #
    # Optimiser
    #
    set optstring = ( "$optstring" "optimiser= $optimiser" )
    if ( $optimiser == "prfo" ) then
      set  optstring = ( "$optstring" "maxupdate=30" )
    endif
    set namestring = $optimiser
    
    #
    # Coordinates
    #
    if ( `echo $coords | grep _con` == "" ) then
      set optstring = ( "$optstring" "coordinates= $coords" )
    else
      set tmp = `echo $coords | cut -d_ -f1`
      set optstring = ( "$optstring" "coordinates= $tmp constraints= { { bond 4 5 } { angle 1 4 5 } } " )
      if ( $coords != dlc_con ) then
	set optstring = ( "$optstring" " frozen= { 8 9 } " )
      endif 
    endif

    if ( `echo $coords | grep hdlc` != "" ) then
      set optstring = ( "$optstring" "residues= { {a b} {{ 1 2 3 4 5 } { 6 7 8 9 }} }  ")
    endif
    set namestring = ${namestring}_$coords

    # 
    # Multiple-image options
    #
    if ( $multim != "none" && $optimiser == "prfo" ) continue
    if ( `echo $multim | grep dimer` != "" ) then
      # Dimer
      set optstring = ( "$optstring" "dimer=true " )
      if ( `echo $multim | grep _noint ` != "" ) then
        set optstring = ( "$optstring" "dimer=true dimer_interpolate=false coords2=c2 maxstep=0.1 tolerance=0.001 " )
      endif
    endif
    if ( `echo $multim | grep neb` != "" ) then
      # NEB
      set tmp = `echo $multim | cut -d_ -f2` # frozen perp free w
      set optstring = ( "$optstring" "nebk=0.02 nimage=6 tolerance=0.005 coords2=c2 " )
      if ( $tmp != "w" ) then
        set optstring = ( "$optstring" "neb=$tmp " )
      else
        set optstring = ( "$optstring" "neb=free " )
      endif
    endif
    if ( `echo $multim | grep _w` != "" ) then
      set optstring = ( "$optstring" "weights= { 1. 1. 1. 1. 1. 0. 0. 0. 0. } delta=0.0001 tolerance=0.005 coords2=c2 ")
    endif
    set namestring = ${namestring}_$multim

    #
    # Hessian
    #
    if ( $hessian != "external" && $optimiser != "prfo" ) continue
    set optstring = ( "$optstring" " initial_hessian= $hessian " )
    set namestring = ${namestring}_$hessian

    #
    # now compose the input file
    #
    set name = v_$namestring.chm
    cp start.chm $name

    cat call.chm  >> $name
    echo $restart_full >> $name
    echo "$optstring" >> $name

    echo "" >> $name
    echo 'set efinal_full [ get_matrix_element matrix=dl-find.energy indices= {0 0 } format= "%24.12f" ] ' >> $name

    # run another calculation to test restart
    cat call.chm  >> $name
    echo $restart_rest >> $name
    echo "$optstring" >> $name

    cat tail.chm  >> $name


    if ( 1 == 1 ) then
	# one ChemShell run to get the target energy
	echo "Running ChemShell on $name"
	chemsh $name >& ${name}_out
	
	set etarget = `grep " Energy calculation finished, energy:" ${name}_out | tail -1 | cut -d: -f2`
	sed -e"s/E_REF/$etarget/" $name > .tmp
	mv .tmp $name
    endif

end # hessian
end # multim
end # coords
end # optimiser
