# -*-shell-script-*-
# MD code of LJ liquid with dimer molecule
#*******************************************
# Definition of procedures
#*******************************************
proc initmd { } { MD++ {
setnolog
setoverwrite
dirname = runs/ljdimer

#--------------------------------------------
# Create Perfect Lattice Configuration
crystalstructure = simple-cubic
latticeconst = 4.0371  #(1/0.6)^(1/3)*sigma, sigma=3.405, density = 0.6/sigma^3
latticesize  = [ 1 0 0  4
                 0 1 0  4
                 0 0 1  4 ]
makecrystal
srand48bytime randomposition
writeall = 1 zipfiles = 1 finalcnfile = rand.cn writecn

#set atoms 0 and 1 as dimer atoms (species = 1)
input = [ 2  0 1 ] fixatoms_by_ID input = 1 setfixedatomsspecies freeallatoms
SR(0) = [ -0.2 0 0  0.2 0 0 ]
writeall = 1 zipfiles = 1 finalcnfile = rand-dimer.cn writecn
atommass = [ 28.0855  28.0855 ]
} }
#end of proc initmd_prepare_liquid


#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = [0.40 0.67] bondradius = 0.3 bondlength = 0 #no bond
atomcolor0 = orange atomcolor1 = blue bondcolor = red backgroundcolor = gray70 fixatomcolor = yellow
plotfreq = 1000  rotateangles = [ 0 0 0 1.25 ]
plot_map_pbc = 1
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 = [ 0 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 = [ 0 1 1
                                    1 0 1
                                    1 1 0 ]
relax
} }
#end of proc relax_freebox

#--------------------------------------------
proc setup_md { } { MD++ {     
equilsteps = 0  timestep = 0.01 # (ps)
atommass = [ 28.0855  28.0855 ] # (g/mol)
DOUBLE_T = 0
saveprop = 1 savepropfreq = 10000 openpropfile #run
savecn = 0 savecnfreq = 10000 openintercnfile
plotfreq = 10000
vt2 = 2e28  #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 R_DIMER" 
} }
#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
#*******************************************
# status 0:prepare liquid, 1: prepare liquid-solid interface, 2,3: measure Tm
# read in status from command line argument
if { $argc == 0 } {
 set status 0
} elseif { $argc > 0 } {
 set status [lindex $argv 0]
}
puts "status = $status"

#energy barrier of dimer is H = 6 epsilon
# kB = 8.617e-5
# if T = 40 K, H = 6 epsilon, H/kBT = 18
# if T = 100 K, H = 6 epsilon, H/kBT = 7
set EPSILON 0.010324
set SIGMA 3.405


if { $status == 0 } {
  #creates random structure
  initmd  
  MD++ initLJ
  #openwindow
  relax_fixbox
  puts "r_dimer = [expr [MD++_Get R_DIMER]/$SIGMA] (sigma)"

  setup_md
  MD++ T_OBJ = 90 initvelocity
  MD++ ensemble_type = "NVE" integrator_type = "VVerlet"

  MD++ totalsteps = 1000000 run
  #MD++ totalsteps = 10000000 run
  MD++ finalcnfile = "ljdimer-equil.cn" writecn
  
  MD++ sleep
  exitmd

} elseif { $status == 1 } {
  # free energy calculation      
  initmd  
  MD++ initLJ
  MD++ NNM = 100
  MD++ incnfile = "ljdimer-equil.cn" readcn setconfig1
  #openwindow
  setup_md
  MD++ srand48bytime  plot_map_pbc = 1
  MD++ T_OBJ = 90 ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP R_DIMER F_DIMER F_DIMER_INTERNAL F_DIMER_EXTERNAL dEdlambda Wtot Wavg" }
  MD++ Ecoh = 0  switchfreq = 10 ecspring = 0 
  MD++ savecn = 0 saveprop = 1 savepropfreq = 10000 printfreq = 5000

  set datafile "dW.dat"
  set fileID [open $datafile w ]
  
  for {set x 0} {$x<=20} {incr x 1} {
      
      set y [format %04d $x]
      MD++ readcn initvelocity totalsteps = 100000 saveprop = 0 
      MD++ lambda0 = 3.5 lambda1 = 3.5 switchfunc = 1 refpotential = 10 runMDSWITCH
      
      MD++ lambda0 = 3.5 lambda1 = 6.0 switchfunc = 1 refpotential = 10
      MD++ totalsteps = 10000000 saveprop = 1 runMDSWITCH 

      puts $fileID "$x [MD++_Get Wavg ]"
      flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 2 } {
  # free energy calculation      
  initmd  
  MD++ initLJ
  MD++ NNM = 100
  MD++ incnfile = "ljdimer-equil.cn" readcn setconfig1
  #openwindow
  setup_md
  MD++ srand48bytime  plot_map_pbc = 1
  MD++ T_OBJ = 90 ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP R_DIMER F_DIMER F_DIMER_INTERNAL F_DIMER_EXTERNAL dEdlambda Wtot Wavg" }
  MD++ Ecoh = 0  switchfreq = 10 ecspring = 0 
  MD++ savecn = 0 saveprop = 1 savepropfreq = 10000 printfreq = 5000

  set datafile "dW.dat"
  set fileID [open $datafile w ]
  
  for {set x 0} {$x<=20} {incr x 1} {
      
      set y [format %04d $x]
      MD++ readcn initvelocity totalsteps = 100000 saveprop = 0 
      MD++ lambda0 = 6.0 lambda1 = 6.0 switchfunc = 1 refpotential = 10 runMDSWITCH
      
      MD++ lambda0 = 6.0 lambda1 = 3.5 switchfunc = 1 refpotential = 10
      MD++ totalsteps = 10000000 saveprop = 1 runMDSWITCH 

      puts $fileID "$x [MD++_Get Wavg ]"
      flush $fileID

     MD++ finalcnfile = f$y.cn writecn
 }

 close $fileID
 exitmd

} elseif { $status == 3 } {
  # free energy calculation      
  initmd  
  MD++ initLJ
  MD++ NNM = 100
  MD++ incnfile = "ljdimer-equil.cn" readcn setconfig1
  #openwindow
  setup_md
  MD++ plot_map_pbc = 1
  MD++ T_OBJ = 90 ensemble_type = "NVE" integrator_type = "VVerlet" implementation_type = 0
  MD++ {output_fmt = "curstep EPOT KATOM Tinst HELMP" }
  MD++ savecn = 0 saveprop = 1 savepropfreq = 100 printfreq = 5000

  MD++ totalsteps = 1000 plotfreq = 10 run
  MD++ sleep
  exitmd

} else {

 puts "unknown status = $status"
 exitmd 

} 
#-------------------------------------------
