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

source "scripts/Examples/Tcl/startup.tcl"
#*******************************************
# Definition of procedures
#*******************************************
proc initmd {  } { MD++ {
#setnolog
setoverwrite
dirname = runs/si-nw-torsion
writeall = 1
} }
#end of proc initmd

proc create_nw_110 { r } { MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
latticesize  = [ 1  1  -2 12   
                 1  1   1 12
                 1 -1   0 18 ]
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 12
                 1 -1  0 18
                 1  1  1 12 ]
makecrystal }
MD++ input  = \[ 1 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 = [ 90 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 = 2000
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 = 2000
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 
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_zz P_com(0) P_com(1) P_com(2) F_com(0) F_com(1) F_com(2) Ptheta_com Torque" 
ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
} }
#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]
}
puts "status = $status"

set radius             16

if { $status == 0 } {
  initmd
  #readmeam
  readmeam-lammps

  MD++ allocmultiple = 2
  create_nw_110 $radius
  #MD++ input = \[ 3 3 0.1 \] shiftbox
  MD++ eval
  #create_nw_111 $radius

  #openwindow 

  set dz 0.1
  MD++ torsionsim = 1 torquespec = \[ 0.1 $dz 0 \] maketorquehandle
  MD++ finalcnfile = "handle-dz${dz}.cn" writeall = 1 writecn eval 
  #MD++ addtorque
  MD++ eval plot

  setup_md
  
  MD++ ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ T_OBJ = 1000 totalsteps = 10000 initvelocity run
  MD++ finalcnfile = "handle-equil.cn" writeall = 1 writecn

  relax_fixbox
  MD++ finalcnfile = "handle-relaxed.cn" writeall = 1 writecn
  #MD++ sleep
  exitmd 

} elseif { $status == 1 } {

  initmd
  #readmeam  
  readmeam-lammps

  set dz 0.1
  #set NP0 4776
  set NP0 5580
  
  #MD++ incnfile = "handle-dz${dz}.cn" readcn 
  MD++ incnfile = "handle-dz${dz}-relaxed.cn" readcn 
  MD++ NP0 = $NP0 NIMAGES = [expr [MD++_Get NP] - $NP0]

  #openwindow 
 
  MD++ torsionsim = 1 torquespec = \[ 0.02 $dz 0 \]

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

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

     if { $x > 0 } { MD++ addtorque }

     MD++ eval plot 
     relax_fixbox

     set Theta  [format %.4f [MD++_Get torquespec(2)]]
     set Epot   [MD++_Get EPOT]
     set Torque [MD++_Get Torque]

     puts $fileID "$Theta $Epot $Torque"
     flush $fileID

     MD++ finalcnfile = Torque$x.cn writecn
  }

  close $fileID
  exitmd 
    
} elseif { $status == 2 } {

  initmd
  readmeam-lammps

  #MD++ incnfile = "si-nw-16.cn" readcn
  #MD++ incnfile = "handle-relaxed.cn" readcn 
  MD++ incnfile = "torque-relaxed.cn" readcn

  set dz 0.1
  set NP0 5580

  MD++ torsionsim = 1 torquespec = \[ 0.1 $dz 0.1 \] 
  MD++ NP0 = $NP0 NIMAGES = [expr [MD++_Get NP] - $NP0]

  #openwindow 

  setup_md
  
  MD++ ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ T_OBJ = 300 totalsteps = 100000 initvelocity run
  MD++ sleep
  exitmd

} elseif { $status == 3 } {
  # verify Virial expression
  initmd
  readmeam-lammps
  #readmeam

  set dz 0.1
  set NP0 5580

  MD++ NNM = 100
  set datafile "Ediff.dat"
  set fileID [open $datafile w ]
#  MD++ sw3b_multiply = 1
#  MD++ sw2b_multiply = 1
  
#  MD++ incnfile = "torque-relaxed.cn" readcn
#  MD++ incnfile = "handle-relaxed.cn" readcn
#  MD++ torsionsim = 1 torquespec = \[ 0 $dz 0 \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] eval
#  MD++ input = \[ 0.1 \] addtorque

   MD++ incnfile = "Torque10.cn" readcn
   MD++ freeallatoms
#   openwindow
#   MD++ sleep
#  MD++ input = \[ 1 -10 10 -10 10 -0.4 0.4 \] fixatoms_by_position reversefixedatoms removefixedatoms
#  set NP0 [MD++_Get NP]

#  MD++ atomeyepath = "/home/caiwei/Codes/MD++/Tools/AtomEye" atomeyeexe = "A.i686" atomeye

#  MD++ torsionsim = 1 torquespec = \[ 0.02 $dz 0.0 \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] addtorque

  MD++ torsionsim = 1 torquespec = \[ 0 $dz 0.2 \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0]
  MD++ eval
      set Theta  [format %.4f [MD++_Get torquespec(2)]]
      set E0   [MD++_Get EPOT]
      set T0 [MD++_Get Torque]
  puts $fileID "$Theta $E0 $T0"  

  set Lz [expr 5.431*18*sqrt(2)*1.1]
  set delta 1e-3
  set m2delta [expr 2*$delta]
  
  MD++ torsionsim = 1 torquespec = \[ $delta \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] addtorque eval
      set Theta  [format %.4f [MD++_Get torquespec(2)]]
      set E1     [MD++_Get EPOT]
      set T1     [MD++_Get Torque]
  puts $fileID "$Theta $E1 $T1"
      
  MD++ incnfile = "Torque10.cn" readcn
#  MD++ torsionsim = 1 torquespec = \[ 0.02 $dz 0.0 \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] addtorque

#  MD++ input = \[ 1 -10 10 -10 10 -0.4 0.4 \] fixatoms_by_position reversefixedatoms removefixedatoms
#  MD++ incnfile = "handle-relaxed.cn" readcn
#  MD++ torsionsim = 1 torquespec = \[ 0 $dz 0 \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] eval
#  MD++ input = \[ 0.1 \] addtorque
  
  MD++ torsionsim = 1 torquespec = \[ -$delta $dz 0.2 \]
  MD++ torsionsim = 1 torquespec = \[ -$delta \] NP0 = $NP0 NIMAGES = [expr [MD++_Get NP]-$NP0] addtorque eval
      
      set Theta  [format %.4f [MD++_Get torquespec(2)]]
      set E2     [MD++_Get EPOT]
      set T2     [MD++_Get Torque]
  puts $fileID "$Theta $E2 $T2"

  set T0 [expr $T0/$Lz]
  set T1 [expr $T1/$Lz]
  set T2 [expr $T2/$Lz]
  set Tnum [expr ($E1-$E2)/2/$delta]
  puts $fileID "T0=$T0  Tnum=$Tnum DT=[expr $T0-$Tnum]"
  
  close $fileID
#  MD++ sleep
  exitmd
        
} elseif { $status == 4 } {
  # verify Virial stress expression
  initmd
  readmeam-lammps
  #readmeam

  MD++ NNM = 200
  set datafile "Ediff.dat"
  set fileID [open $datafile w ]
#  MD++ sw3b_multiply = 1
#  MD++ sw2b_multiply = 1
  
  create_nw_110 $radius 
  MD++ input = \[ 3 3 0.1 \] shiftbox
  #MD++ incnfile = "torque-relaxed.cn" readcn
  #MD++ incnfile = "Torque5.cn" readcn

#  set dz 0.1
#  set NP0 5580
#  MD++ incnfile = "handle-dz${dz}.cn" readcn 
#
#  MD++ torsionsim = 1 torquespec = \[ 0.1 $dz 0.0 \] 
#  MD++ NP0 = $NP0 NIMAGES = [expr [MD++_Get NP] - $NP0]

#  MD++ input = \[ 3 3 0.1 \] shiftbox
  
  MD++ eval
      set E0   [MD++_Get EPOT]
      set S0   [MD++_Get VIRIAL(8)]
  puts $fileID "$E0 $S0"  

  set delta 1e-5
  
  MD++ input = \[ 3 3 $delta \] shiftbox
  MD++ eval
      set E1   [MD++_Get EPOT]
      set S1   [MD++_Get VIRIAL(8)]
  puts $fileID "$E1 $S1"  

  create_nw_110 $radius
  MD++ input = \[ 3 3 0.1 \] shiftbox
  #MD++ incnfile = "torque-relaxed.cn" readcn
  #MD++ incnfile = "Torque5.cn" readcn
#  MD++ incnfile = "handle-dz${dz}.cn" readcn 
#  
#  MD++ NP0 = $NP0 NIMAGES = [expr [MD++_Get NP] - $NP0]
  
  MD++ input = \[ 3 3 -$delta \] shiftbox
  MD++ eval
      set E2   [MD++_Get EPOT]
      set S2   [MD++_Get VIRIAL(8)]
  puts $fileID "$E2 $S2"  

  set Lx [MD++_Get H(0)]
  set Ly [MD++_Get H(4)]
  set Lz [MD++_Get H(8)]
  set Vol [expr $Lx*$Ly*$Lz]
  puts "Lx=$Lx Ly=$Ly Lz=$Lz Vol=$Vol"

  set S0 [expr -$S0]
  set S1 [expr -$S1]
  set S2 [expr -$S2]

  set Snum [expr ($E1-$E2)/2/$delta]
  puts $fileID "S0=$S0  Snum=$Snum DS=[expr $S0-$Snum]"
  
  close $fileID
#  MD++ sleep
  exitmd
        
} else {
 
  puts "unknown status = $status"
  exitmd
}

