# -*-shell-script-*-
# melting temperature of Silicon (TCL)
# run by meam-baskes
#------------------------------------------------------------

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

#*******************************************
# Definition of procedures
#*******************************************
proc initmd_prepare_liquid { n } { 
MD++ setnolog
MD++ setoverwrite
MD++ dirname = runs/si-au-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  1
                 0 0 1  4 ]
makecrystal 
fixallatoms input = 1 setfixedatomsspecies freeallatoms
finalcnfile = "perf-si.cn" writeall = 1 writecn

latticesize = [ 1 0 0  4
                0 1 0  3
                0 0 1  4 ]
makecrystal
finalcnfile = "perf-au.cn" writeall = 1 writecn

incnfile = "perf-si.cn" finalcnfile = "perf-si-au.cn" 
input = [ 2 0 ] splicecn writecn

srand48bytime randomposition
writeall = 1 zipfiles = 1 finalcnfile = "rand-si-au.cn" writecn 
atommass = [ 196.96655 28.0855 ] 
} }
#end of proc initmd_prepare_liquid

proc initmd_switch { n } { 
MD++ setnolog
MD++ setoverwrite
MD++ dirname = runs/si-au-free-eng-$n
MD++ atommass = \[ 196.96655 28.0855 \] 
} 

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

#--------------------------------------------
proc exitmd { } { MD++ quit }
#end of proc exitmd

#--------------------------------------------
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 = [ 196.96655 28.0855 ] # (g/mol)
DOUBLE_T = 0
saveprop = 1 savepropfreq = 100 openpropfile #run
savecn = 1 savecnfreq = 1000 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




#*******************************************
# Main program starts here
#*******************************************
# status 0:prepare liquid, 1: prepare liquid-solid interface, 2,3: measure Tm
# read in status from command line argument
if { $argc == 0 } {
 set status 0
} elseif { $argc > 0 } {
 set status [lindex $argv 0]
}
if { $argc <= 1 } {
 set n 0
} elseif { $argc > 1 } {
 set n [lindex $argv 0]
}
puts "status = $status"
puts "n = $n"

set n0 0

if { $status == 0 } {  

 initmd_prepare_liquid $n
 #readmeam-baskes
 #openwindow
 MD++ plot_map_pbc = 1
 relax_fixbox
 MD++ finalcnfile = "rand-relaxed.cn" writecn
 setup_md
 MD++ T_OBJ = 700 initvelocity ensemble_type = "NPT" integrator_type = "Gear6" 
 MD++ totalsteps = 1000000 run
 MD++ finalcnfile = "liquid-equil.cn" writecn
 exitmd

} elseif { $status == 1 } {

 initmd_switch $n
 #readmeam-baskes
 MD++ incnfile = "../si-au-free-eng-$n0/liquid-equil.cn" readcn setconfig1
 #openwindow
 setup_md
 MD++ srand48bytime  plot_map_pbc = 1
 MD++ T_OBJ = 700 ensemble_type = "NVT" integrator_type = "VVerlet" 
 MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP dEdlambda Wtot Wavg" }
 MD++ Ecoh = -4.105  switchfreq = 10 ecspring = 0 
 MD++ savecn = 0 saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ I12_rc = 3.771   I12_sigma = 1.7      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 = 100000 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

} else {

 puts "unknown status = $status"
 exitmd 

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