# -*-shell-script-*-
# MD code of lipid molecules by ljbond model

source "scripts/Examples/Tcl/startup.tcl"

#*******************************************
# Definition of procedures
#*******************************************
proc initmd { n } { 
MD++ setnolog
MD++ setoverwrite
MD++ dirname = runs/lipid-$n
}

proc initialize_potential { } { MD++ {
#--------------------------------------------
C12_00 = 4  C6_00 = 4
C12_01 = 8  C6_01 = 8
C12_02 = 2  C6_02 = 2
C12_11 = 4  C6_11 = 4
C12_12 = 4  C6_12 = 4
C12_22 = 4  C6_22 = 4

BOND_R0 = 3   # Angstrom
BOND_K  = 1 # eV/A^2

Rcut = 5   # Angstrom
initLJ
NNM = 200
atommass = [ 34 100 120 ] # mass in g/mol
} }

proc initialize_config { } { MD++ {
#--------------------------------------------
#Create a lipid structure
# solvent (water)       species = 0
# tail    (hydrophobic) species = 1
# head    (hydrophilic) species = 2
#
input = [3000  # number of solvent particles 
          100  # number of lipid   molecules
           17  # number of monomers per molecule (chain)
            8  # id of head (hydrophilic) monomer in chain (starting from 0)
            3  # bond length (in Angstrom)
     40 40 50  # Lx, Ly, Lz (simulation cell size in Angstrom)
        ] 
makelipids finalcnfile = lipid.cn writeall = 1 writecn
} }
#end of proc initmd_prepare_liquid


#-------------------------------------------------------------
proc openwindow { } { MD++ {
# Plot Configuration
#
atomradius = [0.40 0.50 0.60] bondradius = 0.3 bondlength = 0 #no bond
atomcolor0 = gray90 atomcolor1 = cyan atomcolor2 = red
bondcolor = red backgroundcolor = gray70 fixatomcolor = yellow
plotfreq = 1000  rotateangles = [ 0 0 0 1.5 ]
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.001 # (ps)
atommass = [ 28.0855  28.0855 ] # (g/mol)
DOUBLE_T = 0
saveprop = 1 savepropfreq = 100 openpropfile #run
savecn = 1 savecnfreq = 10000 openintercnfile
plotfreq = 100
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 TSTRESS_xx TSTRESS_yy TSTRESS_zz H_11 H_22 H_33" 
} }
#end of proc setup_md


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

if { $argc <= 1 } {
 set n 0
} elseif { $argc > 1 } {
 set n [lindex $argv 1]
}
puts "n = $n"


if { $status == 0 } {
  #creates random structure
  initmd $n
  
  initialize_potential

  MD++ srand48bytime
  initialize_config
  #MD++ incnfile = "lipid.cn" readcn
  #openwindow

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

  MD++ sleep
  exitmd

} elseif { $status == 1 } {
  #creates random structure
  initmd $n
  initialize_potential

  MD++ srand48bytime
  #initialize_config
  MD++ incnfile = "lipid-relaxed.cn" readcn

  #openwindow
  relax_fixbox

  setup_md
  MD++ T_OBJ = 10 initvelocity
  #MD++ ensemble_type = "NVT" integrator_type = "VVerlet" implementation_type = 1
  MD++ ensemble_type = "NPT" integrator_type = "Gear6"

  MD++ totalsteps = 10000000 run
  MD++ finalcnfile = "lipid-equil.cn" writecn
  
  MD++ sleep
  exitmd

} else {

 puts "unknown status = $status"
 exitmd 

} 
