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

#*******************************************
# Definition of procedures
#*******************************************
proc initmd_prepare_liquid { n } { 
#MD++ setnolog
MD++ setoverwrite
MD++ dirname = "runs/si-chem-pot-$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-chem-pot-$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 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 = 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


#--------------------------------------------
# for diagnosis purposes
proc printfile {fname} { puts [read [open $fname r]] }

proc index3 { ID coord } {
    set ind.x 1; set ind.y 2; set ind.z 3
    expr { $ID * 3 + [set ind.$coord] - 1 }
}

proc MD++_GetVector { name ID coord } {
    MD++_Get $name [index3 $ID $coord]
}

proc MD++_PrintVar { name {unit \?} {fmt %11.4e} } {
    puts "$name\t= [format $fmt [MD++_Get  $name]] (in $unit)"
}

proc MD++_PrintMatrix { name {unit \?} {fmt %11.4e} } {
    puts "$name\t= [format $fmt [MD++_Get $name 0]] [format $fmt [MD++_Get $name 1]] [format $fmt [MD++_Get $name 2]]  (in $unit)"
    puts      "\t  [format $fmt [MD++_Get $name 3]] [format $fmt [MD++_Get $name 4]] [format $fmt [MD++_Get $name 5]]"
    puts      "\t  [format $fmt [MD++_Get $name 6]] [format $fmt [MD++_Get $name 7]] [format $fmt [MD++_Get $name 8]]"
}

proc MD++_PrintArray { name {unit \?} {i0 0} {i1 5} {fmt %11.4e} } {
    for {set ID $i0} {$ID < $i1} {incr ID 1} {
        puts "$name\($ID\)= [format $fmt [MD++_Get  $name $ID]] [expr {$ID==$i0?"(in $unit)":" "}]"
    }
}

proc MD++_PrintVectorArray { name {unit \}?} {i0 0} {i1 0} {fmt %11.4e} } {
    for {set ID $i0} {$ID < $i1} {incr ID 1} {
        puts "$name\($ID\)= ([format $fmt [MD++_GetVector $name $ID x]]\
                           [format $fmt [MD++_GetVector $name $ID y]]\
                           [format $fmt [MD++_GetVector $name $ID z]])\
                           [expr {$ID==$i0?"(in $unit)":" "}]"
    }
}
# end of diagnosis procs

    



#*******************************************
# 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]
}
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
 relax_fixbox
 MD++ finalcnfile = "rand-relaxed.cn" writecn
 setup_md
 MD++ T_OBJ = 1600 initvelocity ensemble_type = "NPT" integrator_type = "Gear6" 
 MD++ totalsteps = 100000 run
 MD++ finalcnfile = "liquid-equil.cn" writecn
 exitmd

} elseif { $status == 1 } {

 initmd_switch $n
 MD++ incnfile = "../si-chem-pot-$n0/liquid-equil.cn" readcn 
 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 TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 MD++ runMC 
 MD++ finalcnfile = "liquid-mc-equil.cn" writecn
 exitmd

} elseif { $status == 2 } {

 initmd_switch $n
 MD++ incnfile = "../si-chem-pot-$n0/liquid-equil.cn" readcn 
 MD++ input = \[ 1 0 \] fixatoms_by_ID markremovefixedatoms
 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 TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 MD++ runMC 
 MD++ finalcnfile = "liquid-511-mc-equil.cn" writecn
 exitmd

} elseif { $status == 3 } {

 initmd_switch $n
 MD++ incnfile = "../si-chem-pot-$n0/liquid-mc-equil.cn" readcn setconfig1 setconfig2
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 0 switchoffatoms = \[ 1 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"}

 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 runMCSWITCH 

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

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

 close $fileID
 exitmd

} elseif { $status == 4 } {

 initmd_switch $n
 MD++ incnfile = "../si-chem-pot-$n0/liquid-511-mc-equil.cn" readcn setconfig1 setconfig2
 MD++ Ecoh = -4.63  switchfreq = 10 ecspring = 20 switchoffatoms = \[ 1 0 \]
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 mcatom = -1 timestep = 0.12
 MD++ randseed = 10129 srand48
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ dEdlambda Wtot Wavg"}

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

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

     set y [format %04d $x]
     MD++ readcn lambda0 = 0 lambda1 = 1 runMCSWITCH 

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

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

 close $fileID
 exitmd

} elseif { $status == 5 } {
 #test particle (insert) method (Widom)
 initmd_switch $n
 MD++ allocmultiple = 2 incnfile = "../si-chem-pot-$n0/liquid-511-mc-equil.cn" readcn
 MD++ NNM = 200 eval
 set NP [MD++_Get NP]
 puts "NP = $NP"

 MD++ eval_insertparticle
 MD++ quit
 

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

 MD++ totalsteps = 1000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 mcatom = -1 timestep = 0.012
 #MD++ randseed = 10129 srand48
 MD++ srand48bytime
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ" }

 for {set iter 0} {$iter<10000} { incr iter 1 } {

     MD++ runMC

     # write E_insert into file
     for { set x 0 } {$x<=$NP} {incr x 1} {
     MD++ eval_insertparticle
     set Etest [MD++_Get  EPOT_RMV($NP)]
     # NAN (not a number)
     if { !($Etest > 0) & !($Etest <= 0) } { set Etest 1e20 } 
         puts -nonewline $fileID [format "%20.12e" $Etest ]
     }
     puts $fileID " "

     MD++ finalcnfile = "liquid-mc-2.cn" writecn
 }

 close $fileID
 exitmd

} elseif { $status == 6 } {
 #test particle (remove) method
 initmd_switch $n
 MD++ allocmultiple = 2 incnfile = "../si-chem-pot-$n0/liquid-mc-equil.cn" readcn
 MD++ NNM = 200 eval
 set NP [MD++_Get NP]
 puts "NP = $NP"

 #MD++ input = 20 eval_removeparticle
 
 set datafile "E_remove.dat"
 set fileID [open $datafile w ]

 MD++ totalsteps = 1000 savecn = 0
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ T_OBJ = 1600 mcatom = -1 timestep = 0.012
 #MD++ randseed = 10129 srand48
 MD++ srand48bytime
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio T_OBJ" }

 for {set iter 0} {$iter<10000} { incr iter 1 } {

     MD++ runMC

     MD++ eval
     # write E_insert into file
     for { set x 0 } {$x<$NP} {incr x 1} {

         set Etest [MD++_Get  EPOT_RMV($x)]
         puts -nonewline $fileID [format "%20.12e" $Etest ]
         
     }
     puts $fileID " "

     MD++ finalcnfile = "liquid-mc-3.cn" writecn
 }

 close $fileID
 exitmd

} elseif { $status == 7 } {

 initmd_switch $n
 MD++ allocmultiple = 2
 MD++ incnfile = "../si-chem-pot-$n0/liquid-equil.cn" readcn 
 MD++ totalsteps = 1000000 savecn = 0 
 MD++ saveprop = 1 savepropfreq = 100 openpropfile printfreq = 5000
 MD++ mcatom = -1 timestep = 0.012
 MD++ ensemble_type = "uPT"
 MD++ MC_dV_freq = 10 MC_dN_freq = 10 MC_P_ext = 0

 #set T   1400
 #set T   1600
 set T   1700
 
 set mu0 -4.4453
 set gs  -2.7147e-4
 set hs  -8.9540e-8
 set mu  [expr $mu0+$gs*$T+$hs*$T*$T]
 MD++ T_OBJ = $T
 MD++ MC_mu_ext = $mu
 puts "T = $T (K)   mu = $mu (eV)"
 
 MD++ randseed = 10129 srand48
 MD++ {output_fmt = "curstep EPOT MC_accept_ratio NP H_11 H_22 H_33 T_OBJ TSTRESS_xx TSTRESS_yy TSTRESS_zz" }
 MD++ runMC 
 MD++ finalcnfile = "liquid-uPT-equil.cn" writecn
 exitmd

} else {

 puts "unknown status = $status"
 exitmd 

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