# SCRIPT EXAMPLE FOR MOLECULAR DYNAMICS SIMULATION OPTIMIZATION AND AUTOMATIZATION

# Simulation folder creation and coping files into it.
mkdir simulation
cp *.gro *.top *.ndx ./simulation
cd ./simulation

# Bring Gromacs into the environment
source /usr/local/gromacs/bin/GMXRC

setenv GMX_MAXCONSTRWARN -1
# Minimization
gmx grompp -f ../MDP/minimization.mdp -o minimization.tpr -c system.gro -r system.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm minimization -v -nt 2

# NPT_1-Equilibration
gmx grompp -f ../MDP/NPT_1-Equilibration.mdp -o NPT_1-Equilibration.tpr -c minimization.gro -r system.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm NPT_1-Equilibration -v -nt 2
unsetenv GMX_MAXCONSTRWARN

# NPT_2-Equilibration
gmx grompp -f ../MDP/NPT_2-Equilibration.mdp -o NPT_2-Equilibration.tpr -c NPT_1-Equilibration.gro -r system.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm NPT_2-Equilibration -v -nt 2

# NPT_3-Equilibration
gmx grompp -f ../MDP/NPT_3-Equilibration.mdp -o NPT_3-Equilibration.tpr -c NPT_2-Equilibration.gro -r system.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm NPT_3-Equilibration -v -nt 2

# NPT_4-Equilibration
gmx grompp -f ../MDP/NPT_4-Equilibration.mdp -o NPT_4-Equilibration.tpr -c NPT_3-Equilibration.gro -r system.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm NPT_4-Equilibration -v -nt 2

# Production
gmx grompp -f ../MDP/Production.mdp -o Production.tpr -c NPT_4-Equilibration.gro -p topol.top -n index.ndx -maxwarn 3
gmx mdrun -deffnm Production -v -nt 2

# Correction of Periodic Boundary Condition 
echo 0 | gmx trjconv -f Production.gro -s Production.tpr -o run-conect.pdb -conect -pbc whole
sed -i '/ENDMDL/d'  run-conect.pdb

# Center system
printf "Protein\nSystem\n" | gmx trjconv -s Production.tpr -f Production.xtc -ur compact -center -pbc mol -o final.xtc 
cd ..
