# -*-shell-script-*-
# Screw dislocation cross slip in Al
# run by alglue

source "scripts/Examples/Tcl/startup.tcl"
#*******************************************
# Definition of procedures
#*******************************************
proc initmd { n  } {
MD++ setnolog
MD++ setoverwrite
MD++ dirname = runs/al-cross-slip-$n
MD++ NNM = 100 # number of neighbors per atom (default: 60)
}
#end of proc initmd

proc create_dislocation {  } { MD++ {
#--------------------------------------------
# Create Perfect Lattice Configuration
latticestructure = face-centered-cubic
latticeconst = 4.032 #(A) for Al
latticesize = [  1  1 -2  15
                 1  1  1  20
                 1 -1  0  5 ]
makecn finalcnfile = perf.cn writecn #refreshnnlist eval quit

#-------------------------------------------------------------
#Remove surface atoms
input = [ 1 -10 10 -10 -0.4 -10 10 ] fixatoms_by_position
input = [ 1 -10 10  0.4  10 -10 10 ] fixatoms_by_position
removefixedatoms

#-------------------------------------------------------------
#Create Dislocation Dipole, screw [111]
input = [  3  #z, dislocation line direction
           2  #y, separation direction betwen two dislocations in the dipole
           0 0 0.1           # Burgers vector, bx,by,bz in scaled coordinate
           0 -0.501 -0.001   #(x0, y0, y1)
           0.305             # nu, Poisson's ratio
           -10 10 -10 10     # number of images
           1                 # tilt box
        ]
makedipole finalcnfile = dipole.cn writecn
} }
#end of proc create_dislocation


#-------------------------------------------------------------
proc setup_window { } { MD++ {
# Plot Configuration
atomradius = 1.0 bondradius = 0.3 bondlength = 0
atomcolor = cyan highlightcolor = purple backgroundcolor = gray
bondcolor = red fixatomcolor = yellow
color00 = "orange"  color01 = "red"    color02 = "green"
color03 = "magenta" color04 = "cyan"   color05 = "purple"
color06 = "gray80"  color07 = "white"  color08 = "blue"
#
# Use local energy to plot
#plot_color_windows = [ 2
#                       -3.32  -3.29 6 #color06 = gray80
#                       -3.29   10   8 #color08 = blue
#                      ]
#
# Use CSD to plot
plot_color_axis = 2  #2: use CSD (default 0: use local energy)
plot_color_windows = [ 1
#                       1  3  8 #color08 = blue
                        5.5 20  6 #color06 = gray80
                      ]
#
#plot_limits = [ 1 -10 10 -0.05 10 -10 10 ]
plot_atom_info = 1  plotfreq = 10 
rotateangles = [ 0 0 0 1.5 ]
#
win_width = 600 win_height = 600
} }

proc openwindow { } {
setup_window
MD++ {
openwin alloccolors rotate saverot refreshnnlist 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 = 3000
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 = 3000
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.001 # (ps)
atommass = 26.9815 # (g/mol)
DOUBLE_T = 0
saveprop = 1 savepropfreq = 100 openpropfile 
savecn = 1 savecnfreq = 1000 openintercnfile
savecfg = 1 savecfgfreq = 100
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 TSTRESS_xy TSTRESS_yz TSTRESS_xz" 
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"

if { $argc <= 1 } {
 set n 0
} elseif { $argc > 1 } {
 set n [lindex $argv 1]
}
puts "n = $n"


if { $status == 0 } {
  #prepare dislocation configuration
  initmd $n
  #readmeam-lammps

  create_dislocation
  
  openwindow 
  
  relax_fixbox
  MD++ finalcnfile = "disl-relaxed.cn" writeall = 1 writecn
  MD++ sleep
  exitmd 

} elseif { $status == 1 } {
  #Extend dislocation length
  initmd $n
  #readmeam-lammps

  MD++ incnfile = "disl-relaxed.cn" allocmultiple = 10 readcn

  MD++ input = \[ 3 10 \] extendbox
  MD++ finalcnfile = "disl-100b-relaxed.cn" writecn
  
  openwindow
  MD++ sleep
  exitmd
  
} elseif { $status == 2 } {

  initmd $n
  #readmeam-lammps

  MD++ incnfile = "../al-cross-slip-0/disl-100b-relaxed.cn" readcn

  #apply strain
#  MD++ input = \[ 1 3 0.00 \] shiftbox
#  MD++ input = \[ 1 3 0.002 \] shiftbox
#  MD++ input = \[ 1 3 0.005 \] shiftbox
  MD++ input = \[ 1 3 0.008 \] shiftbox
#  MD++ input = \[ 1 3 0.009 \] shiftbox
#  MD++ input = \[ 1 3 0.01 \] shiftbox
#  MD++ input = \[ 1 3 0.011 \] shiftbox
#  MD++ input = \[ 1 3 0.012 \] shiftbox
#  MD++ input = \[ 1 3 0.013 \] shiftbox
#  MD++ input = \[ 1 3 0.014 \] shiftbox
#  MD++ input = \[ 1 3 0.015 \] shiftbox
#  MD++ input = \[ 1 3 0.016 \] shiftbox
#  MD++ input = \[ 1 3 0.017 \] shiftbox
#  MD++ input = \[ 1 3 0.018 \] shiftbox
#  MD++ input = \[ 1 3 0.019 \] shiftbox
#  MD++ input = \[ 1 3 0.02 \] shiftbox
#  MD++ input = \[ 1 3 0.05 \] shiftbox
#  MD++ input = \[ 1 3 0.10 \] shiftbox
#  MD++ input = \[ 1 3 0.20 \] shiftbox
  
  #openwindow
  setup_window
  
  setup_md
  MD++ ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ T_OBJ = 600 totalsteps = 1000000  
  #MD++ randseed = 12345 srand48
  MD++ srand48bytime
  MD++ initvelocity writeall = 1 run
  
  exitmd
        
} elseif { $status == 3 } {

  initmd $n
  #readmeam-lammps

  MD++ incnfile = "../al-cross-slip-0/inter0001.cn" readcn

  #apply strain
  #MD++ input = \[ 1 3 0.01 \] shiftbox

  openwindow

  MD++ finalcnfile = "inter0001.cfg" writeatomeyecfg
  MD++ sleep
  
  exitmd
  
} elseif { $status == 4 } {

  initmd $n
  #readmeam-lammps

  setup_window  
  MD++ input = \[ 1 70 \] convertCNtoCFG
  exitmd
        
} elseif { $status == 5 } {

  initmd $n
  #readmeam-lammps

  #MD++ incnfile = "../al-cross-slip-s001/inter0010.cn" readcn
  MD++ incnfile = "../al-cross-slip-s001/inter0015.cn" readcn
  
  openwindow 
  MD++ conj_ftol = 2e-6 conj_fixbox = 1 conj_fevalmax = 200 relax

  #MD++ finalcnfile = "s001.inter0010.relaxed.cn" writecn
  MD++ finalcnfile = "s001.inter0015.relaxed.cn" writecn
  exitmd
        
} else {
 
  puts "unknown status = $status"
  exitmd
}

