# INSTRUCTIONS TO RUN CpHMD SIMULATION
#
# Although this file is written as a shell script, you should execute each
# command individually on the terminal and check the output at each step,
# reading the corresponding comments and trying to understand what you are
# doing.


# IMPORTANT: Since this implementation of CpHMD consists of a cycle of
# stop-and-go runs of different programs (GROMACS, MEAD and PETIT), GROMACS
# checkpoint (.cpt) files become largely useless. Instead, in order to
# avoid loosing data and to keep things manageable in case of crashed runs,
# it is convenient to run CpHMD simulations as a sequence of short blocks,
# as was typically done before .cpt files were introduced. This file has
# instructions to run 5 blocks of 1 ns each, giving particular attention to
# the input and output of the first block. Note that, although a block
# corresponds here to 1 ns, longer times can be used by changing the input
# parameters in the .mdp and .pHmdp files (see section 2 below); e.g., a
# simulation amounting to 100 ns may consist of 100 blocks of 1 ns, or 10
# blocks of 10 ns, etc.


############################################################################
# 1. Define some parameters

CpHDIR=/data/simulation/programs/CpHMD/ST-CpHMD-v4.1_GMX4.07


############################################################################
# 2. Get necessary files

# Copy files for PB/MC:
cp ../initial/lyso.{ogm,mgm,sites} .

# Copy file with molecular definitions for program fixbox:
cp ../files/lyso.mdf .
# (See fixbox documentation and examples)

# Copy template .mdp file and adapt it to your needs (no changes needed in
# this case):
cp $CpHDIR/templates/MyProtein.mdp lyso.mdp

# Copy topology and final .gro from initiation:
cp ../initial/lysoX-r.top .
cp ../initial/i200.gro .


############################################################################
# 3. Create the initial .pHmdp file to run the first CpHMD block

# Copy template .pHmdp file:
cp $CpHDIR/templates/MyProtein_001.pHmdp lyso_001.pHmdp

# Check $CpHDIR/manual.pdf to understand the meaning of the parameters
# to be given in the .pHmdp file. Then edit lyso_001.pHmdp and change
# the required parameters, in order to make a run for 1 ns using 4
# CPUs, at a pH of 7, temperature of 300.0 K and ionic strength of 0.1
# M, starting from the final topology lysoX-r.top and the relaxed
# structure i200.gro. In particular, make sure that you understand the
# relation between the parameters RelaxSteps, EffectiveSteps,
# InitCycle and EndCycle in lyso_001.pHmdp, as well as their relation
# with the parameter nsteps in lyso.mdp. You can compare your final
# edited lyso_001.pHmdp file with the one provided in the directory
# ../files/.


############################################################################
# 4. Run first block of CpHMD (should take 3-4 hours)

nice -n 19 $CpHDIR/scripts/CpHMD.sh lyso_001.pHmdp &> lyso_001.err


############################################################################
# 5. Inspect the output files

# Check $CpHDIR/manual.pdf to understand what's in the output files. Some
# of those are standard GROMACS files, others are specific to CpHMD.


############################################################################
# 6. Run additional blocks of CpHMD

# In a typical situation you would like to continue running a sequence of
# CpHMD blocks until you reach the intended total simulation time (see the
# 'IMPORTANT' paragraph at the top of this file). For each new block you
# just need to change the parameters in the final section 'Block Specific
# Parameters' in the block's .pHmdp file, which can be conveniently done by
# performing a loop with automated editing. This is here illustrated by
# running additional blocks 2 to 5, amounting to a total of 5 blocks, which
# in the present case corresponds to a simulation time of 5 ns.

# You can change the maximum block index if you want:
maxblock=5

# Loop over blocks:
for (( i=2 ; i<=$maxblock ; i++ )); do

    j=$(printf "%03d\n" $i)           # formated index of current block
    p=$(printf "%03d\n" $(($i-1)))    # formated index of previous block

    # Create .pHmdp for this block:
    sed -r "s|InitCycle=[^ ]+|InitCycle=$((500*($i-1)+1))|;
            s|EndCycle=[^ ]+|EndCycle=$((500*$i))|;
            s|GROin=[^ ]+|GROin=lyso_${p}.gro|" \
	lyso_${p}.pHmdp > lyso_${j}.pHmdp

    # Run CpHMD for this block (should take 3-4 hours):
    nice -n 19 $CpHDIR/scripts/CpHMD.sh lyso_${j}.pHmdp &> lyso_${j}.err

done


############################################################################
# 7. Proceed to the analysis of the CpHMD simulation

# The production runs are over. Go to the directory analysis and follow the
# instructions in there.

cd ../analyses

