# -*-shell-script-*- (TCL script file)
# computing the equilibrium lattice constant a0, cohesive energy Ecoh
#  and elastic constants C11, C12, C44 of
# SW potential for Au
# run by swsiau_gpp

set datafile   "bulkmodulus_TCL_SW_Au.m"
set structname "SW_Au"

#*******************************************
# Definition of procedures
#*******************************************
proc initmd { } { MD++ {
setnolog
setoverwrite
dirname = runs/au-elast-const
#--------------------------------------------
# Create Perfect Lattice Configuration

# Au 
latticestructure = face-centered-cubic
latticeconst = 4.0782 #(A) for Au

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

#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = [0.67 0.80] bondradius = 0.3 bondlength = 2.8285 #for Si
atomcolor0 = orange atomcolor1 = gray50 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 input = 1 setfixedatomsspecies freeallatoms

MD++ fixallatoms  #Note: for FCC 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
#-------------------------------------------
