# Run Jarzynski tests for orac 

# ----------------------------------------------------------------------
#  user serviceable
# ----------------------------------------------------------------------

# Define here the executable created with `make <TARGET>' (see $(O_SRC)/Makefile) 
O_BIN=INTEL-FFTW/orac
# parallel
O_BIN_P=INTEL-FFTW-MPI/orac

# ORAC root
O_ROOT=../..
# sources dir
O_SRC=$(O_ROOT)/src
ORAC=$(O_SRC)/$(O_BIN)
ORAC_P=$(O_SRC)/$(O_BIN_P)

# Number of processors in parallel run:
NP=8

# ----------------------------------------------------------------------

CONFIGURE = ../configure
TEMP_FILES=\
  *~ \
  works* \
  bennett fes

OUT = 1a.out 1b.out 1c.out 1d.out 2a.out 2b.out thermal_changes_GCT.out steer_path.out
OUT_P = 1Pa.out 1Pb.out 1Pc.out 1Pd.out 2Pa.out 2Pb.out 3a.out 3b.out 


# default: run all tests, gathering basic output this is very much
#  like 'make all' without lots of output files
default: $(ORAC) bennett
	./run_serial_tests.bash $(ORAC)

parallel: $(ORAC_P) fes
	./run_parallel_tests.bash $(ORAC_P) $(NP)

# view differences 
diff: diff-s diff-p

diff-s: 
	diff OUT_TEST OUT

diff-p: 
	diff OUT_PARALLEL_TEST OUT_PARALLEL

all: $(OUT) free_energy


$(ORAC) $(ORAC_P): config.H
	@ echo "-------------------------------------------------------------------------"
	@ echo "WARNING: the ORAC executable ("$@") is missing or outdated"
	@ echo "Make sure you created it in the src directory, using config.H in this dir"
	@ echo "Hints:"
	@ echo "  (a) check the name and path of ORAC or ORAC_P at the top of ./Makefile"
	@ echo "  (b) compare the copy of 'config.H' in this directory "
	@ echo "      to the one in "$(O_SRC)", and if they differ do:"
	@ echo "         cp ./config.H "$(O_SRC)
	@ echo "         cd "$(O_SRC)"; make <TARGET>"
	@ echo "-------------------------------------------------------------------------"

# will delete any target output if command fails
.DELETE_ON_ERROR:


# if config.H is missing, rebuild it - warning: not foolproof!
#config.H: 1a.in
#	$(CONFIGURE) < $< > $@

# Static Pattern Rule: only for files in $(OUT), run %.in to produce %.out
$(OUT) :  %.out : %.in $(ORAC)
	$(ORAC)  <  $<  >  $@

4a.out: 4a.in 2a.out $(ORAC)
	ls RESTART_A/ | wc -l   > works_forward
	forward_trajectories.bash  $(ORAC) >> 4a.out 
	for i in `ls FORWARD/*.wrk` ; do tail -1 $$i | awk '{print $$7}' >> works_forward ; done
4b.out: 4b.in 2b.out $(ORAC)
	ls RESTART_B/ | wc -l   > works_reverse
	reverse_trajectories.bash  $(ORAC) >> 4b.out 
	for i in `ls REVERSE/*.wrk` ; do tail -1 $$i | awk '{print $$7}' >> works_reverse ; done

bennett:
	$(MAKE) -i -C $(O_ROOT)/tools/bennett > /dev/null 2>&1 
	ln -sf $(O_ROOT)/tools/bennett/bennett .

fes:
	$(MAKE) -i -C $(O_ROOT)/tools/bennett > /dev/null 2>&1 
	ln -sf $(O_ROOT)/tools/bennett/fes .

free_energy: 4a.out 4b.out

	  $(O_ROOT)/tools/bennett/bennett

# service
clean: 
	./run_serial_tests.bash -z 
	./run_parallel_tests.bash -z

realclean: clean
	rm -rf $(TEMP_FILES) 

distclean: realclean
