# -*-shell-script-*-
# Silicon bulk, lattice constant and cohesive energy


#*******************************************
# Definition of procedures
#*******************************************
proc initmd { } { MD++ {
setnolog
setoverwrite
dirname = runs/si-meam
#
NIC = 200 NNM = 200
#--------------------------------------------
# Create Perfect Lattice Configuration
#
crystalstructure = diamond-cubic
#latticeconst = 4.50
latticeconst = 5.430949821 #(A) for Si
#latticestructure = body-centered-cubic
#latticeconst = 3.1903995085 #(A) for Si
#latticesize = [ 1 0 0  10
#                0 1 0  10
#                0 0 1  60 ]
latticesize = [ 1 0 0  2
                0 1 0  2
                0 0 1  2 ]
#makecrystal
} }

#------------------------------------------------------------
proc readmeam-baskes { } { MD++ {
#Read in MEAM potential (Baskes format)
meamfile = "/home/caiwei/Codes/MD++/Fortran/MEAM-Baskes/meamf"
ntypes = 1  ename0 = "Siz  " #leave spaces to be compatible with fortran
rcut = 6  kode0 = "library"  readMEAM
} }

#------------------------------------------------------------
proc readmeam { } { MD++ {
#Read in MEAM potential (MD++ format)
potfile = "/home/caiwei/Codes/MD++/potentials/MEAMDATA/meamdata.Siz" readMEAM
#potfile = "/home/caiwei/Codes/MD++/potentials/MEAMDATA/meamdata.Si" readMEAM
#xzbl = -10 xzbl0 = -9 #turn off correction
} }

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

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

#--------------------------------------------
proc setup_md { } { MD++ {     
T_OBJ = 300 #Kelvin
equilsteps = 0  timestep = 0.0001 # (ps)
atommass = 28.0855 # (g/mol)
atomTcpl = 200.0 boxTcpl = 20.0
DOUBLE_T = 0
randseed = 12345 srand48 #srand48bytime
initvelocity 
integrator_type = 1 #0: Gear6, 1: Velocity Verlet
saveprop = 0 savepropfreq = 1 openpropfile #run
plotfreq = 1
usenosehoover = 0  vt2 = 2e28  #1e28 2e28 5e28
} }
#end of proc setup_md

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

readmeam
#readmeam-baskes
MD++ enable_zbl_fdimer = 0  enable_square_rscrn = 0

#disable fdimer term to match LAMMPS better 
#MD++ { enable_zbl_fdimer = 0  input = [ 0 0 1.0 0.01 4.0] printpairpot quit }

#MD++ makecrystal 
#MD++ srand48bytime randomposition
#MD++ { input = [ 2 2 0.04 ] redefinepbc }
#MD++ finalcnfile = rand.cn writecn

#MD++ incnfile = rand.cn readcn
#MD++ incnfile = relaxed.cn readcn
#MD++ incnfile = ../si-meam-lammps/rand.cn readcn
#MD++ incnfile = ../si-meam-lammps/rand-relaxed.cn readcn
MD++ incnfile = ../si-meam-lammps/relaxed2.cn readcn
#MD++ incnfile = ../si-meam-lammps/cylinder.cn readcn
#MD++ incnfile = ../si-meam-lammps/cylinder-relaxed.cn readcn
#MD++ fixallatoms

#MD++ eval 
#MD++ { totalsteps = 100 multieval }
#openwindow

#relax_fixbox
#MD++ finalcnfile = rand-relaxed.cn writecn
#MD++ finalcnfile = cylinder-relaxed.cn writecn
#MD++ quit


# only works for parallel (SYS=mpicc)
set ncpu 1
set ncpu [ MD++_Get numDomains ]
set neval 1
puts "ncpu = $ncpu  neval = $neval"
MD++ output_fmt = "EPOT"

#setup_md
#if { $ncpu == 1 } {
#        MD++ totalsteps = $neval  run   quit
#} else {
#        MD++ nXdoms = 1 nYdoms = 1 nZdoms = $ncpu Partition_Domains
#        MD++ Broadcast_MEAM_Param Broadcast_Atoms
#        MD++ totalsteps = $neval run_parallel  quit_all
#}


#openwindow
#relax_fixbox
#MD++ finalcnfile = relaxed.cn writecn
#MD++ quit

if { $ncpu == 1 } {
        # single CPU run
        MD++ totalsteps = $neval multieval
        #for { set x 1 } { $x <= $neval } { incr x } {
        #    MD++ eval
        #}
} else {
        # multi CPU run
        MD++ nXdoms = 1 nYdoms = 1 nZdoms = $ncpu Partition_Domains
        MD++ Broadcast_MEAM_Param Broadcast_Atoms
        for { set x 1 } { $x <= $neval } { incr x } {
            MD++ eval_parallel
        }
}

MD++_PrintVectorArray SR "no unit" 0 10 "%10.4e"
puts "---"
MD++_PrintArray EPOT_IND "eV" 0 10 "%10.4e"
puts "---"
MD++_PrintArray rhotot "no unit" 0 10 "%10.4e"
puts "---"
MD++_PrintArray embf "no unit" 0 10 "%10.4e"
puts "---"
MD++_PrintVectorArray F  "eV/A" 0 10 "%10.4e"
#MD++_PrintVectorArray F  "eV/A" 501 511 "%10.4e"
puts "---"
MD++_PrintMatrix VIRIAL  "eV"  "%10.4e"
puts "---"
MD++_PrintVar EPOT "eV" "%21.14e"

if { $ncpu == 1 }   {
        MD++ quit
} else {
        MD++ quit_all
}        
#******************************************


#openwindow

#relax_fixbox
#MD++ finalcnfile = relaxed-a.cn writecn

#MD++ eval

#for debugging purposes
#MD++_PrintArray atpe2b "eV" 0 10 "%10.4e"
#MD++_PrintArray atpe3b "eV" 0 10 "%10.4e"
#MD++_PrintArray rhotot "arb" 0 10 "%10.4e"
#MD++_PrintArray c8a    "arb" 0 10 "%10.4e"
#MD++_PrintArray rhotot "arb" 0 10 "%10.4e"
#MD++_PrintArray EPOT_IND "eV" 0 10 "%10.4e"
#puts "---"
#MD++_PrintVectorArray SR "no unit" 0 10 "%10.4e"
#puts "---"
#MD++_PrintVectorArray F  "eV/A" 0 10 "%10.4e"
#
#MD++ sleep
#MD++ quit
