# -*-shell-script-*-
# dislocation Peierls stress in Molybdenum
# make fs build=R SYS=intel

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

#*******************************************
# Definition of procedures
#*******************************************
proc initmd { n } {
MD++ setnolog
MD++ setoverwrite
MD++ dirname = runs/mo-peierls-tilt$n
MD++ NNM = 200
}

proc readpot { } { MD++ {
#--------------------------------------------
#Read in potential file
#
potfile = ~/Codes/MD++/potentials/mo_pot readpot
} }

proc create_datebase { } {
    global supercell
    set supercell(-90)    "1 1  1 24   -1 1 0 40   -1 -1  2 3"
    set supercell(-4.54)  "7 7  6 8   -1 1 0 160  -3 -3  7 2"
    set supercell(-4)     "2 2  1 25  -1 1 0 100  -1 -1  4 3"
    set supercell(-3)     "1 1  0 24  -1 1 0 40    0  0  1 8"
    set supercell(-2)     "2 2 -1 20  -1 1 0 80    1  1  4 2"
    set supercell(-1.75)  "5 5 -3 10  -1 1 0 100   3  3 10 1"
    set supercell(-1.5)   "3 3 -2 20  -1 1 0 100   1  1  3 3"
    set supercell(-1.25)  "5 5 -4 8   -1 1 0 80    2  2  5 2"
    set supercell(-1.125) "9 9 -8 4   -1 1 0 80    4  4  9 1"
    set supercell(-1)     "1 1 -1 36  -1 1 0 80    1  1  2 4"
    set supercell(-0.75)  "5 5 -6 10  -1 1 0 100   3  3  5 2"
    set supercell(-0.2)   "9 9 -16 4  -1 1 0 100   8  8  9 1"
    set supercell(0)      "1 1 -2 16  -1 1 0 40    1  1  1 4"
    
    set supercell(4.8)    "-13 -13 -14  4  -1 1 0 120  7 7 -13 1"
    set supercell(4.7)    " -6  -6  -7  8  -1 1 0 120  7 7 -12 1"
    set supercell(4.5)    " -3  -3  -4 15  -1 1 0 125  2 2  -3 3"
    set supercell(4.3)    " -9  -9 -14  5  -1 1 0 125  7 7  -9 1"
    set supercell(4.2)    " -3  -3  -5 15  -1 1 0 125  5 5  -6 1"
    set supercell(4)      " -1  -1  -2 12  -1 1 0  40  1 1  -1 5"
    set supercell(3.5)    " -3  -3  -8  8  -1 1 0 100  4 4  -3 2"
    set supercell(3)      " -1  -1  -4 16  -1 1 0  80  2 2  -1 4"
    set supercell(2.7)    " -1  -1  -6 12  -1 1 0 100  3 3  -1 3"
    set supercell(2.5)    " -1  -1 -10  8  -1 1 0 100  5 5  -1 2"
    set supercell(2.4)    " -1  -1 -20  4  -1 1 0 100 10 10 -1 1"
    set supercell(2)      "  0   0  -2 16  -1 1 0  40  1 1   0 8"
    set supercell(1.75)   "  1   1 -20  4  -1 1 0 100 10 10  1 1"
    set supercell(1.5)    "  1   1 -10  8  -1 1 0  80  5 5   1 1"
    set supercell(1)      "  1   1  -4 16  -1 1 0  80  2 2   1 4"
    set supercell(0.2)    "  4   4  -9  5  -1 1 0  65  9 9   8 1"    
}

proc make_dislocation { tilt  } { 
    global supercell

    # create perfect crystal
    MD++ crystalstructure = body-centered-cubic
    MD++ latticeconst = 3.1472
    MD++ latticesize = \[ $supercell($tilt) \]
    # overwrite simulation cell height (Ly)        
    MD++ latticesize(7) = 40
    MD++ makecrystal #finalcnfile = perf.cn writecn #eval

    # remove top and bottom layers of atoms
    MD++ input = \[ 1 -1 1 -1 -0.37 -1 1 \] fixatoms_by_position
    MD++ input = \[ 1 -1 1  0.37  1 -1 1 \] fixatoms_by_position
    MD++ removefixedatoms

    
    # compute Burgers vector in scaled coordinate
    set c1x [expr [MD++_Get latticesize(0)] * [MD++_Get latticesize(3)]]
    set c1y [expr [MD++_Get latticesize(1)] * [MD++_Get latticesize(3)]]
    set c1z [expr [MD++_Get latticesize(2)] * [MD++_Get latticesize(3)]]

    set c3x [expr [MD++_Get latticesize(8)] * [MD++_Get latticesize(11)]]
    set c3y [expr [MD++_Get latticesize(9)] * [MD++_Get latticesize(11)]]
    set c3z [expr [MD++_Get latticesize(10)]* [MD++_Get latticesize(11)]]

    set det [expr $c1x*$c3y-$c1y*$c3x]
    if  { $det != 0 } {
            set a [expr 0.5*($c3y-$c3x) / $det ]
            set b [expr 0.5*($c1x-$c1y) / $det ]
    } else {
            set det [expr $c1z*$c3y-$c1y*$c3z ]
            set a [expr 0.5*($c3y-$c3z)/$det ] 
            set b [expr 0.5*($c1z-$c1y)/$det ]
    }

    #make sure always remove atom, not insert atom
    if {$tilt > 0} {
        set a [expr -$a]
        set b [expr -$a]
    }
    set bx [expr $a*$c1x+$b*$c3x]
    set by [expr $a*$c1y+$b*$c3y]
    set bz [expr $a*$c1z+$b*$c3z]

    puts "Burgers vector = ([format "%3.1f %3.1f %3.1f" $bx $by $bz])"

    # insert dislocation dipole
    # dislocation position
    set x0 [expr 0.11 /   [MD++_Get latticesize(3)] ]
    set y0 [expr -0.5+0.125/[MD++_Get latticesize(7)] ]
    #set x0 0.01
    #set y0 -0.4996
    set y1 [expr $y0 + 0.5]
    MD++ input = \[ 3 2 $a 0 $b  $x0 $y0 $y1 0.305 -10 10 -10 10 1 0 0 0 0 \]
    MD++ makedipole
}

#--------------------------------------------
proc relax_fixbox { } { MD++ {
# Conjugate-Gradient relaxation
conj_ftol = 1e-4 conj_itmax = 1000 conj_fevalmax = 1000
conj_fixbox = 1
relax
} }
#end of proc relax_fixbox

proc setup_window { } { MD++ {
#------------------------------------------------------------
#colors for Central symmetry view
color00 = "red" color01 = "blue" color02 = "green"
color03 = "magenta" color04 = "cyan" color05 = "purple"
color06 = "gray80" color07 = "white" color08 = "orange"
#--------------------------------------------
# Plot Configuration
#
atomradius = [1.0 0.78] bondradius = 0.3 bondlength = 0 #2.8285 #for Si
win_width=600 win_height=400
#atomradius = 0.9 bondradius = 0.3 bondlength = 0 #2.8285 #for Si
atomcolor = cyan highlightcolor = purple  bondcolor = red
fixatomcolor = yellow backgroundcolor = gray70
#atomcolor = lightgrey highlightcolor = purple  bondcolor = darkgrey
plot_color_windows = [ 5
                 -5   -4   0
                 -5.5 -5   8   #color00 = red
                 -6  -5.5  1
               -6.55 -6    2
               -6.72 -6.55 3
                 -6.5 -6   6   #color06 = gray80
                 -7.5 -6.5 4
                ]
#plot_color_axis = 2
#plot_color_windows = [ 3
#                       0.6 6   1  
#                       6  10   5
#                       20 50   8
#                       10 20   6
#                     ]
#plot_atom_info = 1 # reduced coordinates of atoms
#plot_atom_info = 2 # real coordinates of atoms
plot_atom_info = 3 # energy of atoms
#plot_highlight = [ 0 0 1 2 3 4 5 6 7 8 9 ]
plotfreq = 10
#
rotateangles = [ 0 0 0 1.2 ]
#openwin alloccolors rotate saverot plot
#plot_color_axis = 0 input = [ -8 -3 10] GnuPlotHistogram
#plot_color_axis = 2 input = [ 0.6 50 50 ] GnuPlotHistogram
} }

proc openwindow { } { 
setup_window
MD++ openwin alloccolors rotate saverot eval plot
}

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

#--------------------------------------------
proc setup_md { } { MD++ {     
equilsteps = 0  timestep = 0.001 # (ps)
atommass = 95.94 # (g/mol)
DOUBLE_T = 1
saveprop = 1 savepropfreq = 100 openpropfile #run
savecn = 1 savecnfreq = 1000 openintercnfile
savecfg = 1 savecfgfreq = 50
plotfreq = 100 printfreq = 100
vt2 = 1e28  #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:
#        1:
#        2:
#
# 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

#for debugging I12potential (reference potential 2)
#MD++ refpotential = 2  RLIST = 4.2 SKIN = 0.4
#MD++ I12_rc = 3.771   I12_sigma = 2.0951   I12_epsilon = 2.1675 

if { $status == 0 } {
  # prepare and relax dislocation structure
  initmd $n
  readpot

  create_datebase

  #screw
  #set tilt 0

  #[001]
  #set tilt -3
  
  #[11-1]
  #set tilt 4

  set tilt $n
  
  make_dislocation $tilt
  
  openwindow
  
  MD++ conj_ftol = 1e-4 conj_fixbox = 1 conj_fevalmax = 10000
  MD++ relax

  MD++ finalcnfile = "mo-tilt$tilt.cn" writecn
  MD++ eval

  MD++ sleep
  exitmd
  
} elseif { $status == 1 } {
  # visualization
  initmd $n
  readpot

  set tilt $n
  
  MD++ incnfile = "../mo-peierls-tilt$tilt/mo-tilt$tilt.cn" readcn
  openwindow

  MD++ conj_ftol = 1e-4 conj_fixbox = 1 conj_fevalmax = 1000
  MD++ relax
  
  MD++ sleep
  exitmd

} else {
        
 puts "unknown status = $status"
 exitmd 

} 

