# Run Metadynamics tests for orac 

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

# Define here the executable created with `make <TARGET>' (see $(O_SRC)/Makefile) 
O_BIN=GNU/orac

# ORAC root
O_ROOT=../..
# sources dir
O_SRC=$(O_ROOT)/src
ORAC=$(O_SRC)/$(O_BIN)
CONFIGURE = ../configure
GNUPLOT = gnuplot   		# gnuplot executable

TEMP_FILES=\
*.eps \
*.mod \
*prmtpg \
*~ \
META_DATA \
buta.pdb \
metafes \
tmp.diff \
works* \

OUT = 0.out 1.out 2.out 3.out
TRAJ_FILES = \
     0.traj \
     1.traj \
     2.traj \
     3.traj 
FES_PLOTS = \
     1.plot \
     2.plot \
     3.plot \
    2r.plot \
2theta.plot 
TEST_FILES = $(TRAJ_FILES) $(FES_PLOTS)

DIFF = $(patsubst %, %.diff, $(TEST_FILES))


# default: run all tests, gathering basic output 
default: $(ORAC) $(OUT)
	@ echo "Type 'make plots' to create FES data"
	@ echo "Type 'make diff' to check differences from reference output"

$(ORAC) : 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 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 "-------------------------------------------------------------------------"

all: default plots diff

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

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


plots: $(FES_PLOTS)

$(FES_PLOTS):    metafes $(OUT)
	@echo "Metatests complete; postprocessing data... "
	./metafes 1   < 1.traj > 1.plot 
	./metafes 2   < 2.traj > 2.plot 
	./metafes 2 1 < 2.traj > 2r.plot
	./metafes 2 2 < 2.traj > 2theta.plot
	./metafes 1   < 3.traj > 3.plot 

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

diff: $(DIFF)

$(DIFF): $(OUT) $(FES_PLOTS)
	@ printf "Print diffs (if any) to %s ... \n" $@ ; 
	@ OUTFILE=$(strip $(patsubst %.diff, %, $@)); \
          ( diff $ $${OUTFILE} $${OUTFILE}.REF > $${OUTFILE}.diff  \
           || echo "     >>> differences found! try 'make diffplot'"); 


# gnuplot is required for displaying FES plots
diffplot: $(OUT) $(FES_PLOTS)
	$(GNUPLOT) $(wildcard *.gp)

# service
clean_plots: 
	-rm $(FES_PLOTS)

clean_diff:
	-rm $(DIFF)

clean: clean_plots clean_diff
	-rm $(TEMP_FILES) $(OUT) $(TRAJ_FILES) > /dev/null 2>&1 ;

realclean: clean

distclean: clean
