# -*-shell-script-*-
# compute chemical potential of liquid/solid Silicon (TCL)
# run by sworig
# 1. determine equilibrium volume of solid at T0 (1600K)
# 2. compute Hessian at the thermal strain of T0
#    compute free energy (free_eng_ha.m)
# 3. adiabatic switching from real crystal to harmonic crystal at T0
# 3.1 reverse switching of 3.
# 4. switching real crystal along temperature axis
# 4.1 reverse switching of 4.
# 5. determine equilibrium volume of liquid at T1 (1800K)
# 6. switch liquid Si to Gaussian potential at T1
# 6.1 reverse switching of 6.
# 7. switch Gaussian potential to ideal gas limit
# 7.1 reverse switching of 7.
# 8. switch real liquid along temperature axis
# 8.1 reverse switching of 8. 
#------------------------------------------------------------

source "scripts/Examples/Tcl/startup.tcl"

#*******************************************
# Definition of procedures
#*******************************************
proc initmd_prepare_crystal { n } { 
#MD++ setnolog
MD++ setoverwrite
MD++ dirname = "runs/si-free-eng-$n"
MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
latticesize  = [ 1 0 0  4 
                 0 1 0  4
                 0 0 1  4 ]
makecrystal
} }
#end of proc initmd_prepare_crystal

proc initmd_prepare_liquid { n } { 
#init_prepare_crystal $n
#MD++ setnolog
MD++ setoverwrite
MD++ dirname = "runs/si-free-eng-$n"
MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
latticesize  = [ 1 0 0  4 
                 0 1 0  4
                 0 0 1  4 ]
makecrystal
srand48bytime randomposition
} }
#end of proc initmd_prepare_liquid

proc initmd_switch { n } {
#MD++ setnolog
MD++ setoverwrite
MD++ dirname = "runs/si-free-eng-$n"
} 
#end of proc initmd_switch

#------------------------------------------------------------
proc readmeam-lammps { } { MD++ {
#Read in MEAM potential (Baskes format)
meamfile = "/home/caiwei/Codes/lammps-15Dec06/examples/meam/meamf"
nspecies = 1  element0 = "Siz" rcut = 6.0  readMEAM
NNM = 300
} }

#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = 0.67 bondradius = 0.3 bondlength = 2 #2.8285 #for Si
atomcolor = orange bondcolor = red backgroundcolor = gray70 fixatomcolor = yellow
plotfreq = 100  rotateangles = [ 0 0 0 1.25 ]
openwin  alloccolors rotate saverot eval plot
} }
#end of proc openwindow

#--------------------------------------------
proc relax_freebox { } { MD++ {
# Conjugate-Gradient relaxation
conj_ftol = 2e-6 conj_itmax = 1000 conj_fevalmax = 10000
conj_fixbox = 0  conj_fixboxvec = [ 0 1 1
                                    1 0 1
                                    1 1 0 ]
relax
} }
#end of proc relax_freebox
#--------------------------------------------
proc relax_fixbox { } { MD++ {
# Conjugate-Gradient relaxation
conj_ftol = 2e-6 conj_itmax = 1000 conj_fevalmax = 10000
conj_fixbox = 1  conj_fixboxvec = [ 0 1 1
                                    1 0 1
                                    1 1 0 ]
relax
} }
#end of proc relax_freebox

#--------------------------------------------
proc setup_md { } { MD++ {     
equilsteps = 0  timestep = 0.0001 # (ps)
atommass = 28.0855 # (g/mol)
DOUBLE_T = 0
saveprop = 1 savepropfreq = 100 openpropfile #run
savecn = 1 savecnfreq = 10000 openintercnfile
plotfreq = 100
vt2 = 2e28  #1e28 2e28 5e28
wallmass = 2e3     # atommass * NP = 14380
boxdamp = 1e-3     # optimal damping for 216 atoms and wallmass 1e-3
saveH # Use current H as reference (H0), needed for specifying stress
fixboxvec  = [ 0 1 1 
               1 0 1
               1 1 0 ]
stress = [ 0 0 0
           0 0 0
           0 0 0 ]
output_fmt = "curstep EPOT KATOM Tinst HELMP TSTRESS_xx TSTRESS_yy TSTRESS_zz H_11 H_22 H_33" 
} }
#end of proc setup_md


# end of diagnosis procs

    



#*******************************************
# Main program starts here
#*******************************************
# status 0:prepare liquid by MD (NPT) simulation
#        1:MC equilibration
#        2:compute free energy by MC switching simulation
#
# read in status from command line argument
if { $argc == 0 } {
 set status 0
} elseif { $argc > 0 } {
 set status [lindex $argv 0]
}
puts "status = $status"

if { $argc <= 1 } {
 set n 0
} elseif { $argc > 1 } {
 set n [lindex $argv 1]
}
puts "n = $n"

set n0 0

if { $status == 0 } {
 # equilibrate perfect crystal
 initmd_prepare_crystal $n

 relax_freebox
 MD++ eval

 set NP [MD++_Get NP]
 set EPOT [MD++_Get EPOT]
 set Ecoh [expr $EPOT/$NP]

 set Nx [MD++_Get latticesize(3)]
 set Lx [MD++_Get H(0)]
 set a  [expr $Lx/$Nx]
 
 puts "Ecoh = $Ecoh (eV)"
 puts "a    = $a (A)"
 
 exitmd

} elseif { $status == 1 } {
 # equilibrate perfect crystal
 initmd_prepare_crystal $n

 #set T 251.43
 set T 1600

 MD++ plot_map_pbc = 1
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 #MD++ randseed = 47919 srand48
 MD++ srand48bytime

 setup_md
 MD++ NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]   T_OBJ = $T
 MD++ ensemble_type = "NPTC" integrator_type = "Gear6" NHChainLen = 4 initvelocity
 
 MD++ run finalcnfile = "crystal-md-T$T.cn" writeall = 1 writecn
 # thermal expansion coefficient is 4.1e-6 K^-1
 
 exitmd

} elseif { $status == 2 } {
 # compute Hessian matrix at expanded volume of crystal

    initmd_prepare_crystal $n
    set T 1600

    # thermal strain
    set eT 0.005177
    
    puts "T = $T  eT = [format %10.5e $eT]"

    #straining 
    MD++ input = \[ 1 1 $eT \] shiftbox
    MD++ input = \[ 2 2 $eT \] shiftbox
    MD++ input = \[ 3 3 $eT \] shiftbox
        
    MD++ timestep = 1e-5 input = 0 calHessian
    exec mv hessian.out hessian.T$T.out

    exitmd

} elseif { $status == 3 } {
 # SWITCH from real crystal to Harmonic potential
 initmd_prepare_crystal $n

    set T 1600

    # thermal strain
    set eT 0.005177
    
    puts "T = $T  eT = [format %10.5e $eT]"

    #straining 
    MD++ input = \[ 1 1 $eT \] shiftbox
    MD++ input = \[ 2 2 $eT \] shiftbox
    MD++ input = \[ 3 3 $eT \] shiftbox

    MD++ finalcnfile = "crystal-perf-T$T.cn" writecn setconfig1

    MD++ incnfile = "../si-free-eng-orig-2-${T}K/hessian.T$T.out" readHessian

 # equilibrate crystal at NVT-chain ensemble
    setup_md
    MD++ srand48bytime    NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]    totalsteps = 200000
    MD++ ensemble_type = "NVTC" integrator_type = "Gear6" NHChainLen = 4 
    MD++ T_OBJ = $T  initvelocity  saveprop = 0 savecn = 0
    MD++ run finalcnfile = "crystal-md-T$T-NVT.cn" writeall = 1 writecn
    MD++        incnfile = "crystal-md-T$T-NVT.cn" 

 # prepare for switching simulation
    MD++ Ecoh = -4.335  switchfreq = 10 savecn = 0
    MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 100000 saveprop = 0

     # equilibration run
     MD++ initvelocity run
     
     # switching run
     MD++ lambda0 = 0 lambda1 = 1 refpotential = 1 
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writeall = 1  writecn
 }

 close $fileID
 exitmd

} elseif { $status == 3.1 } {
 # SWITCH from Harmonic potential to real crystal
 initmd_prepare_crystal $n

    set T 1600

    # thermal strain
    set eT 0.005177
    
    puts "T = $T  eT = [format %10.5e $eT]"

    #straining 
    MD++ input = \[ 1 1 $eT \] shiftbox
    MD++ input = \[ 2 2 $eT \] shiftbox
    MD++ input = \[ 3 3 $eT \] shiftbox

    MD++ finalcnfile = "crystal-perf-T$T.cn" writecn setconfig1

    MD++ incnfile = "../si-free-eng-orig-2-${T}K/hessian.T$T.out" readHessian

 # equilibrate harmonic crystal at NVT-chain ensemble
    setup_md
    MD++ srand48bytime    NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]    totalsteps = 200000
    MD++ ensemble_type = "NVTC" integrator_type = "Gear6" NHChainLen = 4 
    MD++ T_OBJ = $T  initvelocity  saveprop = 0 savecn = 0
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ lambda0 = 1 lambda1 = 1 refpotential = 1    
    MD++ runMDSWITCH finalcnfile = "crystal-ha-md-T$T.cn" writeall = 1 writecn
    MD++                incnfile = "crystal-ha-md-T$T.cn" 

 # prepare for switching simulation
    MD++ Ecoh = -4.335  switchfreq = 10 savecn = 0
    MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 100000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 1    

     # equilibration run
     MD++ initvelocity runMDSWITCH
     
     # switching run
     MD++ lambda0 = 1 lambda1 = 0 refpotential = 1 
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writeall = 1  writecn
 }

 close $fileID
 exitmd

} elseif { $status == 4 } {
 # SWITCH crystal along temperature axis (1600->2000 K) (refpotential = 4)
 initmd_switch $n
 
 set T 1600
 set factor 0.80

 MD++ incnfile = "../si-free-eng-orig-3-${T}K/crystal-md-T$T-NVT.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }
 MD++ NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]   T_OBJ = $T
 MD++ ensemble_type = "NPTC" integrator_type = "Gear6" NHChainLen = 4
 
 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]

     # equilibration run
     MD++ readcn initvelocity totalsteps = 100000 saveprop = 0 run

     # switching run
     MD++ lambda0 = 1 lambda1 = $factor switchfunc = 1 refpotential = 4
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn

 }

 close $fileID
 exitmd

} elseif { $status == 4.1 } {
 #SWITCH crystal along temperature axis (2000->1600 K) (refpotential = 4)
 initmd_switch $n
 
 set T 1600
 set factor 0.80
 set T1 [expr $T/$factor]
 
 MD++ incnfile = "../si-free-eng-orig-3-${T}K/crystal-md-T$T-NVT.cn" readcn setconfig1
 #openwindow

 # equilibrate crystal at temperature T1
    setup_md
    MD++ srand48bytime    NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]    totalsteps = 200000
    MD++ ensemble_type = "NPTC" integrator_type = "Gear6" NHChainLen = 4 
    MD++ T_OBJ = $T  initvelocity  saveprop = 1 openpropfile savecn = 0
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ lambda0 = $factor lambda1 = $factor refpotential = 4 switchfunc = 1
    MD++ runMDSWITCH finalcnfile = "crystal-md-T${T1}.cn" writeall = 1 writecn
    MD++                incnfile = "crystal-md-T${T1}.cn" 

 # prepare for switching simulation    
    MD++ Ecoh = -4.335  switchfreq = 10 savecn = 0
    MD++ saveprop = 1 savepropfreq = 100 printfreq = 5000
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }
    
 
 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]

     # equilibration run
     MD++ readcn totalsteps = 100000 saveprop = 0
     MD++ lambda0 = $factor lambda1 = $factor refpotential = 4 switchfunc = 1
     MD++ initvelocity runMDSWITCH

     # switching run
     MD++ totalsteps = 1000000 saveprop = 1
     MD++ lambda0 = $factor lambda1 = 1 refpotential = 4 switchfunc = 1 
     MD++ runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID
     
     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 5 } {  
 # equilibrate liquid
 initmd_prepare_liquid $n

 #set T 2000
 set T 1800

 #set eT -0.018128
 set eT -0.016717
 #set eT -0.018967
 
 MD++ input = \[ 1 1 $eT \] shiftbox 
 MD++ input = \[ 2 2 $eT \] shiftbox
 MD++ input = \[ 3 3 $eT \] shiftbox

 MD++ plot_map_pbc = 1
 relax_fixbox
 MD++ finalcnfile = "rand-relaxed.cn" writecn
 setup_md
 MD++ randseed = 38164 srand48

 MD++ T_OBJ = $T initvelocity ensemble_type = "NPT" integrator_type = "Gear6" 
 MD++ totalsteps = 1000000 run
 MD++ finalcnfile = "liquid-md-T$T.cn" writecn

# MD++ T_OBJ = $T initvelocity ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
# MD++ totalsteps = 1000000 run
# MD++ finalcnfile = "liquid-md-T$T-NVT.cn" writecn
 exitmd

} elseif { $status == 6 } {
 #Switch from SW liquid to Gauss fluid (refpotential = 5)
 initmd_prepare_liquid $n

    set T 1800

    # thermal strain
    set eT -0.018967
    
    puts "T = $T  eT = [format %10.5e $eT]"

    #straining 
    MD++ input = \[ 1 1 $eT \] shiftbox
    MD++ input = \[ 2 2 $eT \] shiftbox
    MD++ input = \[ 3 3 $eT \] shiftbox

 # equilibrate liquid at NVT ensemble
    setup_md
    MD++ srand48bytime    NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \]    totalsteps = 200000
    MD++ ensemble_type = "NVT" integrator_type = "VVerlet" NHChainLen = 4 
    MD++ T_OBJ = $T  initvelocity  saveprop = 0 savecn = 0
    MD++ run finalcnfile = "liquid-md-T$T-NVT.cn" writeall = 1 writecn setconfig1
    MD++        incnfile = "liquid-md-T$T-NVT.cn" 
    
 # prepare for switching simulation
    MD++ Ecoh = -4.335  switchfreq = 10 savecn = 0
    MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
    MD++ Gauss_rc = 3.771   Gauss_sigma = 0.7   Gauss_epsilon = 50

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 200000 saveprop = 0

     # equilibration run
     MD++ initvelocity run

     # switching run
     MD++ lambda0 = 0 lambda1 = 1 refpotential = 5
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 6.1 } {
 #Switch from SW liquid to Gauss fluid (refpotential = 5)
 initmd_prepare_liquid $n

   set T 1800

    # thermal strain
    set eT -0.018967
    
    puts "T = $T  eT = [format %10.5e $eT]"

    #straining 
    MD++ input = \[ 1 1 $eT \] shiftbox
    MD++ input = \[ 2 2 $eT \] shiftbox
    MD++ input = \[ 3 3 $eT \] shiftbox

 # equilibrate Gauss liquid at NVT ensemble
    setup_md
    MD++ srand48bytime     totalsteps = 200000
    MD++ ensemble_type = "NVT" integrator_type = "VVerlet"
    MD++ T_OBJ = $T  initvelocity  saveprop = 0 savecn = 0 setconfig1
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0 savecn = 0
    MD++ lambda0 = 1 lambda1 = 1 refpotential = 5
    MD++ Gauss_rc = 3.771   Gauss_sigma = 0.7   Gauss_epsilon = 50
    MD++ runMDSWITCH finalcnfile = "liquid-ga-md-T$T-NVT.cn" writeall = 1 writecn 
    MD++                incnfile = "liquid-ga-md-T$T-NVT.cn" 
    
 # prepare for switching simulation
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg TSTRESS_xx TSTRESS_yy TSTRESS_zz" }

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 100000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 5

     # equilibration run
     MD++ initvelocity runMDSWITCH
     
     # switching run
     MD++ lambda0 = 1 lambda1 = 0 refpotential = 5
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 7} {
 # switch Gauss potential to ideal gas (refpotential = 6)
 initmd_switch $n

 set T 1800
 set factor 0.1

 # read equilibrated Gauss fluid
 MD++ incnfile = "../si-free-eng-orig-6.1-${T}K/liquid-ga-md-T$T-NVT.cn" readcn setconfig1

 # prepare for switching simulation
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = $T ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ Gauss_rc = 3.771   Gauss_sigma = 0.7   Gauss_epsilon = 50
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg TSTRESS_xx TSTRESS_yy TSTRESS_zz" }

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 100000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 5 switchfunc = 1

     # equilibration run
     MD++ initvelocity runMDSWITCH

     # switching run
     MD++ lambda0 = 1 lambda1 = $factor switchfunc = 1 refpotential = 6
     MD++ totalsteps = 500000 saveprop = 1 runMDSWITCH
     set W1 [MD++_Get Wavg]

     MD++ lambda0 = $factor lambda1 = [expr pow($factor,2)]
     MD++ runMDSWITCH
     set W2 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,2)] lambda1 = [expr pow($factor,3)]
     MD++ runMDSWITCH
     set W3 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,3)] lambda1 = [expr pow($factor,4)]
     MD++ runMDSWITCH
     set W4 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,4)] lambda1 = [expr pow($factor,5)]
     MD++ runMDSWITCH
     set W5 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,5)] lambda1 = [expr pow($factor,6)]
     MD++ runMDSWITCH
     set W6 [MD++_Get Wavg]

     puts $fileID "$x $W1 $W2 $W3 $W4 $W5 $W6"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 7.1} {
 # switch ideal gas to Gauss fluid (refpotential = 6)
 initmd_switch $n

 set T 1800
 set factor 0.1
 set T1 [expr $T/pow($factor,6)]
 
 # read equilibrated Gauss fluid
 MD++ incnfile = "../si-free-eng-orig-6.1-${T}K/liquid-ga-md-T$T-NVT.cn" readcn setconfig1

 # equilibrate fluid at temperature T1
    setup_md
    MD++ srand48bytime       totalsteps = 200000
    MD++ ensemble_type = "NVT" integrator_type = "VVerlet"    
    MD++ T_OBJ = $T  initvelocity  saveprop = 1 openpropfile savecn = 0
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ lambda0 = [expr pow($factor,6)] lambda1 = [expr pow($factor,6)]
    MD++ refpotential = 6 switchfunc = 1
    MD++ Gauss_rc = 3.771   Gauss_sigma = 0.7   Gauss_epsilon = 50
    MD++ runMDSWITCH finalcnfile = "liquid-ga-md-T${T1}.cn" writeall = 1 writecn
    MD++                incnfile = "liquid-ga-md-T${T1}.cn" 
 
 # prepare for switching simulation
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ savecn = 0 saveprop = 1 savepropfreq = 100 printfreq = 5000
    MD++ refpotential = 6 switchfunc = 1
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg TSTRESS_xx TSTRESS_yy TSTRESS_zz" }

    
 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]

     # equilibration run
     MD++ readcn totalsteps = 100000 saveprop = 0
     MD++ lambda0 = [expr pow($factor,6)] lambda1 = [expr pow($factor,6)]
     MD++ initvelocity runMDSWITCH

     # switching run
     MD++ totalsteps = 500000 saveprop = 1
     
     MD++ lambda0 = [expr pow($factor,6)] lambda1 = [expr pow($factor,5)]
     MD++ runMDSWITCH
     set W6 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,5)] lambda1 = [expr pow($factor,4)]
     MD++ runMDSWITCH
     set W5 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,4)] lambda1 = [expr pow($factor,3)]
     MD++ runMDSWITCH
     set W4 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,3)] lambda1 = [expr pow($factor,2)]
     MD++ runMDSWITCH
     set W3 [MD++_Get Wavg]

     MD++ lambda0 = [expr pow($factor,2)] lambda1 = [expr pow($factor,1)]
     MD++ runMDSWITCH
     set W2 [MD++_Get Wavg]

     MD++ lambda0 = $factor lambda1 = 1
     MD++ runMDSWITCH
     set W1 [MD++_Get Wavg]

     puts $fileID "$x $W6 $W5 $W4 $W3 $W2 $W1"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 8 } {
 #Switch liquid along temperature axis (1800->1500 K)
 initmd_switch $n

 set T 1800
 set factor 1.2

 MD++ incnfile = "../si-free-eng-orig-6-${T}K/liquid-md-T$T-NVT.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0

 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_1 H_22 H_33" }
 MD++ T_OBJ = $T ensemble_type = "NPT" integrator_type = "Gear6"
 
 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 100000 saveprop = 0
     
     # equilibration run
     MD++ initvelocity run

     # switching run
     MD++ lambda0 = 1 lambda1 = $factor refpotential = 4 switchfunc = 1 
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 8.1 } {
 #Switch liquid along temperature axis (1800->1500 K)
 initmd_switch $n

 set T 1800
 set factor 1.2
 set T1 [expr $T/$factor]
 
 MD++ incnfile = "../si-free-eng-orig-6-${T}K/liquid-md-T$T-NVT.cn" readcn setconfig1
 #openwindow

 # equilibrate crystal at temperature T1
    setup_md
    MD++ srand48bytime    plot_map_pbc = 1    totalsteps = 200000
    MD++ ensemble_type = "NPT" integrator_type = "Gear6"
    MD++ T_OBJ = $T  initvelocity  saveprop = 1 openpropfile savecn = 0
    MD++ Ecoh = -4.335  switchfreq = 10 ecspring = 0
    MD++ lambda0 = $factor lambda1 = $factor refpotential = 4 switchfunc = 1
    MD++ runMDSWITCH finalcnfile = "crystal-md-T${T1}.cn" writeall = 1 writecn
    MD++                incnfile = "crystal-md-T${T1}.cn" 

 # prepare for switching simulation    
    MD++ Ecoh = -4.335  switchfreq = 10 savecn = 0
    MD++ saveprop = 1 savepropfreq = 100 printfreq = 5000
    MD++ refpotential = 4 switchfunc = 1 
    MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }
    

 set datafile "dW.dat"
 set fileID [open $datafile w ]

 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]
     MD++ readcn totalsteps = 200000 saveprop = 0

     # equilibration run
     MD++ lambda0 = $factor lambda1 = $factor
     MD++ initvelocity runMDSWITCH

     # switching run
     MD++ lambda0 = $factor lambda1 = 1
     MD++ totalsteps = 1000000 saveprop = 1 runMDSWITCH

     puts $fileID "$x [MD++_Get Wavg ]"
     flush $fileID
     
     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} else {

 puts "unknown status = $status"
 exitmd 

} 
#-------------------------------------------
