# -*-shell-script-*- (TCL script file)
# computing the equilibrium lattice constant a0, cohesive energy Ecoh
#  and elastic constants C11, C12, C44 of
# Silicon at different phases

set datafile   "bulkmodulus_TCL_SC_Sin.m"
set structname "MEAM_SC_Sin"

#*******************************************
# Definition of procedures
#*******************************************
proc initmd { } { MD++ {
#setnolog
setoverwrite
dirname = runs/si-elast-const
#------------------------------------------------------------
# Read in MEAM potential
meamfile = "/home/caiwei/Codes/MD++/Fortran/MEAM-Baskes/meamf"

# original MEAM potential Si
#ntypes = 1  ename0 = "Siz  " #leave spaces to be compatible with fortran

# new MEAM potential Si
meafile  = "/home/caiwei/Codes/MD++/Fortran/MEAM-Baskes/meafile_Sin"
ntypes = 1  ename0 = "Sin  " #leave spaces to be compatible with fortran

# original MEAM potential Ge
#ntypes = 1  ename0 = "Ge   " #leave spaces to be compatible with fortran

# original MEAM potential SiGe
#meafile  = "/home/caiwei/Codes/MD++/Fortran/MEAM-Baskes/meafile_Si_Ge"
#ntypes = 2  ename0 = "Siz  " ename1 = "Ge   " #leave spaces to be compatible with fortran

rcut = 6  kode0 = "library"
readMEAM
NIC = 200 NNM = 200
#--------------------------------------------
# Create Perfect Lattice Configuration

# Si
#latticestructure = diamond-cubic
#latticeconst = 5.431 #(A) for Si
#latticestructure = body-centered-cubic
#latticeconst = 3.190 #(A) for Si
latticestructure = simple-cubic
latticeconst = 2.6530 #(A) for Si
#latticestructure = beta-tin
#latticeconst = [4.9995 4.9995 2.7104 ]  #(A) for Si beta-tin 
#latticestructure = bc8
#latticeconst = 6.75320938095 #(A) for Si

# Ge
#latticestructure = diamond-cubic
#latticeconst = 5.6575 #(A) for Ge
#latticestructure = body-centered-cubic
#latticeconst = 3.3078 #(A) for Ge
#latticestructure = simple-cubic
#latticeconst = 2.7495 #(A) for Ge
#latticestructure = beta-tin
#latticeconst = [5.1879 5.1879 2.8086 ]  #(A) for Ge beta-tin

# SiGe
#latticestructure = zinc-blende
#latticeconst = 5.5379 #(A) for SiGe
#latticestructure = CsCl #(B2)
#latticeconst = 3.2457 #(A) for SiGe
#latticestructure = NaCl #(B1)
#latticeconst = 5.3963 #(A) for SiGe
#latticestructure = beta-tin-2
#latticeconst = [5.0862 5.0862 2.7610]   #(A) for SiGe beta-tin 

latticesize  = [ 1 0 0  5
                 0 1 0  5
                 0 0 1  5 ]
allocmultiple = 2
} }
#end of proc initmd

#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = 0.67 bondradius = 0.3 bondlength = 2.8285 #for Si
atomcolor = orange bondcolor = red backgroundcolor = gray70
plotfreq = 10  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 = 1e-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

#--------------------------------------------
# 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
#*******************************************
initmd

MD++ makecrystal 

MD++ fixallatoms  #Note: only for SC and BCC crystals!

relax_freebox
MD++ finalcnfile = relaxed.cn writecn

set nx [MD++_Get latticesize  3]
set ny [MD++_Get latticesize  7]
set nz [MD++_Get latticesize 11]

set Lx [MD++_Get H 0]
set Ly [MD++_Get H 4]
set Lz [MD++_Get H 8]

set NP [MD++_Get NP]

set a0 [expr $Lx/$nx]

set nx0 [expr $Lx/$a0]
set ny0 [expr $Ly/$a0]
set nz0 [expr $Lz/$a0]

#puts "nx = $nx ny = $ny  nz = $nz"
#puts "Lx = $Lx Ly = $Ly  Lz = $Lz"
#puts "a0 = $a0"


#-----------------------
# Compute bulk modulus
#-----------------------
MD++ saveH conj_fixbox = 1

set fileID [open $datafile w ]

puts $fileID "%This file is generated by MD++ ($argv0)\n\n"
puts $fileID "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
puts $fileID "% $structname "
puts $fileID "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
puts $fileID "$structname = struct();"
puts $fileID "$structname.N = $NP;"
puts $fileID "$structname.a0 = $a0;"
puts $fileID "$structname.C1 = \[ $nx0 0 0 \];"
puts $fileID "$structname.C2 = \[ 0 $ny0 0 \];"
puts $fileID "$structname.C3 = \[ 0 0 $nz0 \];"

puts $fileID "$structname.data = \[ %lattice_const.(A)  Epot(eV)"

for {set x 0.997} {$x<=1.003} {set x [expr $x+0.001]} {

    MD++ restoreH input = $x scaleH relax eval

    puts $fileID "[format %.3f%3d%24.13e $x 1 [MD++_Get EPOT]]"
}

puts $fileID "];"
puts $fileID "$structname.data(:,1) = $structname.data(:,1)*$structname.a0;\n%"
puts $fileID "$structname = fit_aEB($structname);\n%"
puts $fileID "%disp(sprintf('$structname: a0 = %.6f A, Ecoh = %.6f eV, B = %.6f GPa', ..."
puts $fileID "%             $structname.a0, $structname.Ecoh, $structname.B));\n"

#-----------------------
# Compute C11 C12
#-----------------------
puts $fileID "$structname.data = \[ %lattice_const.(A)  Epot(eV)"

for {set x -0.003} {$x<=0.003} {set x [expr $x+0.001]} {

    MD++ restoreH input = \[ 1 1 $x \] shiftbox relax eval

    puts $fileID "[format %.3f%3d%24.13e [expr $x+1] 1 [MD++_Get EPOT]]"
}

puts $fileID "];"
puts $fileID "$structname = fit_C11($structname);\n%"
puts $fileID "%disp(sprintf('$structname: B = %.6f GPa, C11 = %.6f GPa, C12 = %.6f GPa', ..."
puts $fileID "%             $structname.B, $structname.C11, $structname.C12));\n"


#-----------------------
# Compute C44
#-----------------------
# rotate crystal by 45 degrees around z axis
MD++  {
    restoreH relax
    finalcnfile = perf.cn writeall = 1 writecn
    incnfile = perf.cn input = [ 1 0 ] splicecn
    input = [ 2 1 0.5 ] redefinepbc
    input = [ 1 2 -1  ] redefinepbc  reorientH 
    relax eval saveH
}

puts $fileID "$structname.N = [expr $NP*2];"
puts $fileID "$structname.C1 = \[  $nx0 -$ny0 0 \];"
puts $fileID "$structname.C2 = \[  $nx0 $ny0 0 \];"
puts $fileID "$structname.C3 = \[  0 0 $nz0 \];"

puts $fileID "$structname.data = \[ %lattice_const.(A)  Epot(eV)"

MD++ fixallatoms  #Note: only for SC and BCC crystals!

for {set x -0.003} {$x<=0.003} {set x [expr $x+0.001]} {

    MD++ restoreH input = \[ 1 1 $x \] shiftbox relax eval

    puts $fileID "[format %.3f%3d%24.13e [expr $x+1] 1 [MD++_Get EPOT]]"
}

puts $fileID "];"
puts $fileID "$structname = fit_C44($structname);\n%"
puts $fileID "disp(sprintf('$structname: a0 = %.6f A, Ecoh = %.6f eV, B = %.6f GPa', ..."
puts $fileID "             $structname.a0, $structname.Ecoh, $structname.B));"
puts $fileID "disp(sprintf('        C11 = %.6f GPa, C12 = %.6f GPa, C44 = %.6f GPa', ..."
puts $fileID "             $structname.C11, $structname.C12, $structname.C44));\n"


close $fileID









exitmd
#-------------------------------------------
