# -*-shell-script-*-
# compute chemical potential of liquid/solid Silicon (TCL)
# run by sw or meam-lammps
#------------------------------------------------------------

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
MD++ 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 HELM 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 } {  

 initmd_prepare_liquid $n
 #readmeam-lammps
 #openwindow

 #set T 1600
 #set T 1900
 set T 2000

 set eT -0.018128
 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 == 1 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-equil.cn" readcn 
 set T 1600
 #openwindow
 MD++ plot_map_pbc = 1
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ randseed = 23419 srand48
 #MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 #MD++ ensemble_type = NVT runMC 
 #MD++ finalcnfile = "liquid-mc-equil.cn" writecn

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

} elseif { $status == 2 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-mc-equil.cn" readcn setconfig1
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0 
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 mcatom = -1 timestep = 0.012
 MD++ randseed = 10129 srand48
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg"}
 MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675 

 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 lambda0 = 1 lambda1 = 0 refpotential = 2
     MD++ runMCSWITCH 

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

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

 close $fileID
 exitmd

} elseif { $status == 3 } {
 #Switch from SW liquid to I12 (or Gauss) liquid
 initmd_switch $n

 #set T 1600
 set T 2000

 #MD++ incnfile = "../si-free-eng-$n0/liquid-md-T$T.cn" readcn setconfig1
 MD++ incnfile = "../si-free-eng-$n0/liquid-md-T$T-NVT.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = $T ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
# MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 0.6   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 0.8   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.0   I12_epsilon = 2.1675
 MD++ I12_rc = 3.771   I12_sigma = 1.2   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.4   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.7   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.8   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.9   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.0   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.1   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.2   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.3   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.4   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.7   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 3.0   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 3.3   I12_epsilon = 2.1675
 MD++ Gauss_rc = 3.771   Gauss_sigma = 0.7   Gauss_epsilon = 50

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

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

     set y [format %04d $x]
     MD++ readcn initvelocity totalsteps = 200000 saveprop = 0 run
     #MD++ lambda0 = 0 lambda1 = 1 refpotential = 2
     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 == 4 } {
 #Equilibrate I12 potential
# MD++ refpotential = 2  RLIST = 4.2 SKIN = 0.4
        
 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-equil.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 1600 ensemble_type = "NVT" integrator_type = "VVerlet" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0 
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675 

 MD++ readcn initvelocity
# MD++ totalsteps = 10000 saveprop = 1 run 
 MD++ lambda0 = 1 lambda1 = 1 refpotential = 2 
 MD++ totalsteps = 10000 saveprop = 1 runMDSWITCH 

 MD++ finalcnfile = "liquid-I12-equil.cn" writecn
 exitmd

} elseif { $status == 5 } {
 #Switch from I12 liquid to SW liquid
 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-I12-equil.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 1600 ensemble_type = "NVT" integrator_type = "VVerlet" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0 
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
# MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.0   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.2   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.4   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.7   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.8   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 1.9   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.0   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.1   I12_epsilon = 2.1675
# MD++ I12_rc = 3.771   I12_sigma = 2.2   I12_epsilon = 2.1675
 MD++ I12_rc = 3.771   I12_sigma = 2.3   I12_epsilon = 2.1675

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

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

     set y [format %04d $x]
     
     MD++ readcn initvelocity totalsteps = 10000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 2 runMDSWITCH
     
     MD++ lambda0 = 1 lambda1 = 0 refpotential = 2 
     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 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-mc-equil.cn" readcn setconfig1
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0 
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 mcatom = -1 timestep = 0.012
 MD++ randseed = 10129 srand48
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg"}
 MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675 

 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 lambda0 = 1 lambda1 = 0 refpotential = 2
     MD++ runMCSWITCH 

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

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

 close $fileID
 exitmd

} elseif { $status == 7 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-I12-equil.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 1600 ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ I12_rc = 3.771   I12_sigma = 1.2   I12_epsilon = 2.1675

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

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

     set y [format %04d $x]

     MD++ readcn initvelocity totalsteps = 100000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 3 switchfunc = 1 runMDSWITCH

     MD++ lambda0 = 1 lambda1 = $factor switchfunc = 1 refpotential = 3
     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 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-I12-equil.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 1600 ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ I12_rc = 3.771   I12_sigma = 1.2   I12_epsilon = 2.1675

 set datafile "dW.dat"
 set fileID [open $datafile w ]
 set factor 129.7463378906250
 for {set x 0} {$x<=100} {incr x 1} {

     set y [format %04d $x]

     MD++ readcn initvelocity totalsteps = 100000 saveprop = 0
     MD++ lambda0 = $factor lambda1 = $factor refpotential = 3 switchfunc = 1 runMDSWITCH

     MD++ lambda0 = $factor lambda1 = 1 switchfunc = 1 refpotential = 3
     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 == 9 } {
 # equilibrate perfect crystal
 initmd_prepare_crystal $n

    #set T 300
    #set T 800
    #set T 1600
    set T 1900
    set alpha0 3.66e-6
    set alpha  3.975e-6
    set beta  -5.870e-10
    
    # thermal strain
    if { $T < 600 } { 
         set eT [ expr $alpha0*$T ]
    } else {
         set eT [ expr $alpha*$T+$beta*$T*$T ]
    }            
    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
    
 #openwindow
 MD++ plot_map_pbc = 1
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ randseed = 47919 srand48
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ TSTRESS_xx TSTRESS_yy TSTRESS_zz" }

 #equilibrate using the original potential
 #MD++ ensemble_type = NVT runMC
 #MD++ finalcnfile = "crystal-mc-T$T.cn" writecn

 setup_md
 MD++ NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \] 
 MD++ ensemble_type = "NVTC" integrator_type = "Gear6" NHChainLen = 4 initvelocity
 
 MD++ run finalcnfile = "crystal-md-T$T.cn" writeall = 1 writecn
 
 #equilibrate using the harmonic potential
 #MD++ incnfile = "../si-free-eng-$n0/hessian.T$T.out" readHessian
 #MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 #MD++ lambda0 = 1 lambda1 = 1 refpotential = 1
 #MD++ ensemble_type = "NVT" timestep = 0.012 runMCSWITCH finalcnfile = "crystal-ha-T$T.cn" writecn
 #MD++ runMDSWITCH finalcnfile = "crystal-ha-md-T$T.cn" writeall = 1 writecn


 exitmd

} elseif { $status == 10 } {
 # SWITCH to Harmonic Potential
 initmd_switch $n
 
 set T 1600
 MD++ incnfile = "../si-free-eng-$n0/crystal-perf-T$T.cn" readcn setconfig1
 MD++ incnfile = "../si-free-eng-$n0/hessian.T$T.out" readHessian
 MD++ incnfile = "../si-free-eng-$n0/crystal-md-T$T.cn" readcn
 #openwindow
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ srand48bytime
 
 #MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg"}
 setup_md
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 
 
 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++ ensemble_type = "NVT" runMC
     MD++ NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \] ensemble_type = "NVTC" integrator_type = "Gear6" NHChainLen = 4
     MD++ initvelocity run
     
     MD++ lambda0 = 0 lambda1 = 1 refpotential = 1 
     #MD++ totalsteps = 1000000 saveprop = 1 runMCSWITCH
     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 == 11 } {
 # SWITCH from Harmonic Potential
 initmd_switch $n

 set T 1600
 MD++ incnfile = "../si-free-eng-$n0/crystal-perf-T$T.cn" readcn setconfig1
 MD++ incnfile = "../si-free-eng-$n0/hessian.T$T.out" readHessian
 MD++ incnfile = "../si-free-eng-$n0/crystal-ha-md-T$T.cn" readcn
 #openwindow
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ srand48bytime

 #MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg"}
 setup_md
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 
 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
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 1
     
     #MD++ ensemble_type = "NVT" runMCSWITCH
     MD++ NHMass = \[ 1e-2 1e-2 1e-2 1e-2 \] ensemble_type = "NVTC" integrator_type = "Gear6" NHChainLen = 4
     MD++ initvelocity runMDSWITCH
     
     MD++ lambda0 = 1 lambda1 = 0 refpotential = 1 
     #MD++ totalsteps = 1000000 saveprop = 1 runMCSWITCH
     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 == 12 } {
 # SWITCH crystal along temperature axis (1600->1900 K)
 initmd_switch $n
 
 #set T 1600
 #set factor 0.84
 set T 800
 set factor 0.42

 MD++ incnfile = "../si-free-eng-$n0/crystal-mc-T$T.cn" readcn setconfig1
 #openwindow
 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ srand48bytime
 
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ 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 initvelocity totalsteps = 100000 saveprop = 0
     MD++ ensemble_type = "NPT" runMC    

     MD++ lambda0 = 1 lambda1 = $factor switchfunc = 1 refpotential = 4
     MD++ totalsteps = 1000000 saveprop = 1 runMCSWITCH

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

     MD++ finalcnfile = f$y.cn writecn

 }

 close $fileID
 exitmd

} elseif { $status == 13 } {
 #SWITCH crystal along temperature axis (1900->1600 K)
 initmd_switch $n
 set T 1600
 MD++ incnfile = "../si-free-eng-$n0/crystal-md-T1900.cn" readcn setconfig1
 #openwindow

 MD++ totalsteps = 1000000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ T_OBJ = $T mcatom = -1 timestep = 0.012
 MD++ srand48bytime
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg H_11 H_22 H_33"}

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

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

     set y [format %04d $x]

     MD++ readcn initvelocity totalsteps = 100000 saveprop = 0
     MD++ ensemble_type = "NPT" 
     MD++ lambda0 = $factor lambda1 = $factor refpotential = 4 switchfunc = 1 runMCSWITCH

     MD++ lambda0 = $factor lambda1 = 1 switchfunc = 1 refpotential = 4
     MD++ totalsteps = 1000000 saveprop = 1 runMCSWITCH

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

 close $fileID
 exitmd

} elseif { $status == 14 } {
 #Switch liquid along temperature axis (1600->1900 K) (2000->1600 K)
 initmd_switch $n

 #set T 1600
 #set factor 0.84
 set T 2000
 set factor 1.25

 #MD++ incnfile = "../si-free-eng-$n0/liquid-md-T$T.cn" readcn setconfig1
 MD++ incnfile = "../si-free-eng-$n0/liquid-md-T$T-NVT.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = $T ensemble_type = "NPT" integrator_type = "Gear6"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_1 H_22 H_33" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000

 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 initvelocity totalsteps = 100000 saveprop = 0 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 == 15 } {
 #Switch liquid along temperature axis (1900->1600 K)
 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-md-T1900.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 1600 ensemble_type = "NPT" integrator_type = "Gear6"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg H_11 H_22 H_33" }
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000

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

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

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

     MD++ ensemble_type = "NPT" 
     MD++ lambda0 = $factor lambda1 = $factor refpotential = 4 switchfunc = 1 runMDSWITCH

     MD++ lambda0 = $factor lambda1 = 1 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 == 16 } {
 #Equilibrate Gauss potential
 initmd_switch $n

 set T 2000
 
 MD++ incnfile = "../si-free-eng-$n0/liquid-md-T$T-NVT.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = $T ensemble_type = "NVT" integrator_type = "VVerlet" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  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++ readcn initvelocity
 MD++ lambda0 = 1 lambda1 = 1 refpotential = 5
 MD++ totalsteps = 100000 saveprop = 1 runMDSWITCH 

 MD++ finalcnfile = "liquid-Gauss-T$T.cn" writecn
 MD++ finalcnfile = "../si-free-eng-$n0/liquid-Gauss-T$T.cn" writecn
 
 exitmd

} elseif { $status == 17 } {
 #Switch Gauss potential along temperature axis
 initmd_switch $n

 set T 2000
 
 MD++ incnfile = "../si-free-eng-$n0/liquid-Gauss-T$T.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = $T ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.63  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

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

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

     set y [format %04d $x]

     MD++ readcn initvelocity totalsteps = 100000 saveprop = 0
     MD++ lambda0 = 1 lambda1 = 1 refpotential = 5 switchfunc = 1 runMDSWITCH

     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

} else {

 puts "unknown status = $status"
 exitmd 

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