# -*-shell-script-*-
# melting temperature of Silicon (TCL)
#------------------------------------------------------------

#*******************************************
# Definition of procedures
#*******************************************
proc initmd { } { MD++ {
#setnolog
setoverwrite
dirname = runs/si-liquid
#dirname = runs/si-liquid-solid
#------------------------------------------------------------
# Read in MEAM potential (not needed if run sw_gpp)
#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
#NIC = 200 NNM = 200
#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = diamond-cubic
latticeconst = 5.431 #(A) for Si
#latticesize  = [ 6 1 0  1
#                -1 6 0  3
#                 0 0 1  6 ]
latticesize  = [ 6 1 0  1
                -1 6 0  1
                 0 0 1  6 ]
#makecrystal
#input = [ 1 -10 10  0.25   10 -10 10 ] fixatoms_by_position removefixedatoms
#input = [ 1 -10 10  -10 -0.45 -10 10 ] fixatoms_by_position

#incnfile = liquid-equil.cn readcn input = [0 0 0] pbcshiftatom finalcnfile = liquid-equil-pbc.cn writecn
#makecn incnfile = liquid-equil-pbc.cn finalcnfile = liquid-solid.cn input = [2 0] splicecn
#input = [ 1 -10 10 -10 -0.45 -10 10 ] fixatoms_by_position
#input = [ 2 2 0.5] redefinepbc finalcnfile = liquid-solid.cn writecn

incnfile = liquid-solid-equil.cn readcn
} }
#end of proc initmd

#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = 0.67 bondradius = 0.3 bondlength = 2 #2.8285 #for Si
atomcolor = orange bondcolor = red backgroundcolor = gray70 fixatomcolor = yellow
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 = 2e-6 conj_itmax = 1000 conj_fevalmax = 10000
conj_fixbox = 0  conj_fixboxvec = [ 1 1 1
                                    1 0 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 = 10000
conj_fixbox = 1  conj_fixboxvec = [ 1 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++ randomposition

#openwindow

#relax_freebox
#relax_fixbox
#MD++ finalcnfile = "relaxed.cn" writecn
#MD++ quit

#MD++ sleep

#MD++ { #for liquid-equil
#conj_fixbox = 0  conj_fixboxvec = [ 1 1 1
#                                    1 0 1
#                                    1 1 1 ]
#}

MD++ { #for liquid-solid-equil
conj_fixbox = 0  conj_fixboxvec = [ 0 1 1
                                    1 1 1
                                    1 1 0 ]
}

MD++  {

saveH # Use current H as reference (H0)

atommass = 28.0855 # (g/mol)
wallmass = 2e5     # atommass * NP 
boxdamp = 1e-3     # optimal damping (??)

srand48bytime  DOUBLE_T = 0  timestep = 0.001 # (ps)

equilsteps = 0  
integrator_type = 0 #0: Gear6, 1: Velocity Verlet
saveprop = 1 savepropfreq = 100 openpropfile
savecn = 1 savecnfreq = 10000 openintercnfile writeall = 1 
plotfreq = 100

}



# initial temperature
set  T0 1680

#MD++ T_OBJ = $T0 initvelocity

# equilibration
#MD++ totalsteps = 50000  usenosehoover = 1  vt2 = 2e28  run
#MD++ finalcnfile = liquid-solid-equil.cn writeall = 1 writecn
#
#MD++ quit

# turn off thermostat
MD++ { totalsteps = 50000  usenosehoover = 0  conj_fixbox = 0
                 conj_fixboxvec = [ 0 1 1
                                    1 1 1
                                    1 1 0 ]
}

set datafile "Tx.dat"
set fileID [open $datafile w ]

for {set x 1} {$x<=100} {incr x 1} {

    MD++ input = 1.001 scaleVel run

    puts $fileID "$x [MD++_Get Tinst ] [MD++_Get KATOM ] [MD++_Get EPOT ]"

    MD++ finalcnfile = n$x.cn writecn
}

close $fileID









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