# -*-shell-script-*-
# Silicon nanowire energy (TCL)

#*******************************************
# Definition of procedures
#*******************************************
proc initmd {  } { MD++ {
#setnolog
setoverwrite
dirname = runs/si-nw-eng-111
} }
#end of proc initmd

proc create_nw_110 { r } { MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
latticesize  = [ 0  0  1 15 
                 1 -1  0 10 
                 1  1  0 4 ] # D = 5nm L = 3.0nm
makecrystal }
MD++ input  = \[ 3 2  0 0  $r  0  \]  
MD++ makecylinder
MD++ writeall = 1 zipfiles = 1 finalcnfile = "si-nw-$r.cn" writecn 
MD++ incnfile = "si-nw-$r.cn" readcn
} 
#end of proc initmd_110

proc create_nw_111 { r } { MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
latticesize  = [ 1 -1  2 10
                 1 -1  0 10 
                 1  1  1 4 ] # D = 5nm L = 3.7nm
makecrystal }
MD++ input  = \[ 3 2  0 0  $r  0  \]  
MD++ makecylinder
MD++ writeall = 1 zipfiles = 1 finalcnfile = "si-nw-$r.cn" writecn 
MD++ incnfile = "si-nw-$r.cn" readcn
} 
#end of proc initmd_110

#------------------------------------------------------------
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 readmeam { } { MD++ {
#Read in MEAM potential (MD++ format)
potfile = "/home/caiwei/Codes/MD++/potentials/MEAMDATA/meamdata.Siz" readMEAM
#xzbl = -10 xzbl0 = -9 #turn off correction
} }

#-------------------------------------------------------------
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 = [ 1 1 1
                                    1 1 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 = 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  = [ 1 1 1 
               1 1 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"

set radius             16
set anneal_temperature 600
set anneal_steps       2000

initmd
readmeam-lammps
#openwindow  #(cannot be used with for loop)

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

if { 0 } { 
for { set radius 16 } { $radius <= 25 } { incr radius 1 } {
 #create_nw_110 $radius 
 create_nw_111 $radius
} 
exitmd } 

for { set radius 16 } { $radius <= 25 } { incr radius 1 } {
 MD++ incnfile = "si-nw-$radius.cn" readcn  refreshnnlist eval
 MD++ NNM = 120
 relax_freebox
 MD++ finalcnfile = "si-nw-$radius-relaxed.cn" writecn
 set E0 [MD++_Get EPOT]
 set NP [MD++_Get NP ]
 setup_md
 MD++ T_OBJ = $anneal_temperature initvelocity ensemble_type = "NPT" integrator_type = "Gear6" 
 MD++ totalsteps = $anneal_steps run
 MD++ finalcnfile = "si-nw-$radius-equil.cn" writecn
 relax_freebox
 MD++ finalcnfile = "si-nw-$radius-relaxed-2.cn" writecn
 set E1 [MD++_Get EPOT]
 set e0 [expr $E0/$NP]
 set e1 [expr $E1/$NP]
 puts $fileID "$radius $E0 $E1 $NP $e0 $e1"
}
close $fileID

exitmd

