# -*-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_liquid { 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 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
#set n0 1

#for debugging I12potential (reference potential 2)
#MD++ refpotential = 2  RLIST = 4.2 SKIN = 0.4
#MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675 

if { $status == 0 } {  
 initmd_prepare_liquid $n
 #readmeam-lammps
 openwindow
 MD++ plot_map_pbc = 1
 relax_fixbox
 MD++ finalcnfile = "rand-relaxed.cn" writecn
 setup_md
 MD++ T_OBJ = 1600 initvelocity ensemble_type = "NPT" integrator_type = "Gear6" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst T_OBJ HELMP TSTRESS_xx TSTRESS_yy TSTRESS_zz H_11 H_22 H_33" }
 #MD++ ensemble_type = "NVT" integrator_type = "VVerlet"
 MD++ totalsteps = 100000 run
 MD++ finalcnfile = "liquid-equil.cn" writecn
 exitmd

} elseif { $status == 1 } {

 initmd_switch $n
 MD++ incnfile = "../si-free-eng-$n0/liquid-equil.cn" readcn 
 openwindow
 MD++ plot_map_pbc = 1
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 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
 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 } {

 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++ I12_rc = 3.771   I12_sigma = 3.0      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 initvelocity totalsteps = 10000 saveprop = 0 run
     MD++ lambda0 = 0 lambda1 = 1 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 == 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 } {

 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 

 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 = 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 } {
 # equilibrate SW with zero 3-body potential (unfortunately leads to void formation)    
 initmd_switch $n

 MD++ incnfile = "../si-free-eng-$n0/liquid-equil.cn" readcn 
 openwindow
 MD++ plot_map_pbc = 1

 #MD++ sw3b_multiply = 1 
 #relax_fixbox
 MD++ sw3b_multiply = 0
# relax_fixbox
# MD++ sleep quit
 
 setup_md
 MD++ T_OBJ = 1600 initvelocity ensemble_type = "NVT" integrator_type = "VVerlet" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst T_OBJ HELMP TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 MD++ totalsteps = 100000 run
 MD++ finalcnfile = "liquid-no3b-equil.cn" writecn
 exitmd

} else {
        
 puts "unknown status = $status"
 exitmd 

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