# Run REM tests for orac 

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

# Define here the executable created with `make <TARGET>' (see $(O_SRC)/Makefile) 
# parallel
O_BIN_P=GNU-FFTW-OMP-MPI/orac
# ORAC root
O_ROOT=../..
# sources dir
O_SRC=$(O_ROOT)/src
ORAC_P=$(O_SRC)/$(O_BIN_P)


#
# change 'MPIRUN=' line below according to your MPI package 
# (a) MPICH2, conforms to MPI-2 recommendation
# mpich2 v.1.0.7 (run 'mpdboot -f mpd.hosts' first; also set up ~/.mpd.conf)
MPIRUN=mpiexec -n
# mpich2 v.1.4.1
#MPIRUN=mpiexec -f machinefile -n
#   use -disable-hostname-propagation  if you encounter communication errors
#MPIRUN=mpiexec -f machinefile -disable-hostname-propagation -n


# (b) OpenMPI
#MPIRUN=mpirun -nd


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

# Number of processors in parallel run:
NP=4

SHELL=/bin/bash

OUT_TESTS = OUT_TEST_1 OUT_TEST_2 OUT_TEST_3 OUT_TEST_4 OUT_TEST_5 OUT_TEST_6
TMP_DIRS = PAR????
TMP_FILES = INPUT ORD-*


# define a "macro" that silently removes temp directories
# giving the rm command through MPI is sometimes required to
# prevent subsequent errors
define clean_parallel_dirs
  -$(MPIRUN) $(NP) rm -fr $(TMP_DIRS) > /dev/null 2>&1 
endef


# default: perform only missing tests
default parallel: $(OUT_TESTS)

# perform all tests 
all: clean $(OUT_TESTS)

test1: OUT_TEST_1

test2: OUT_TEST_2

test3: OUT_TEST_3

test4: OUT_TEST_4

test5: OUT_TEST_5

test6: OUT_TEST_6

OUT_TEST_1: 
	@printf "Starting test 1...\n\n"
	$(clean_parallel_dirs)
	$(MPIRUN) $(NP) $(ORAC_P) < 1.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_1; done
	@printf "\n... Test 1 completed.\n You may want to compare OUT_TEST_1 with the reference OUT_1\n"
	@printf "Please read the NOTE on tests in the README file\n\n"

OUT_TEST_2: OUT_TEST_1
	@printf "Starting test 2...\n\n"
	$(MPIRUN) $(NP) $(ORAC_P) < 2.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_2; done
	@printf "\n... Test 2 completed.\n You may want to compare OUT_TEST_2 with the reference OUT_2\n  "
	@printf "Please read the NOTE on tests in the README file\n\n"

OUT_TEST_3:
	@printf "Starting test 3...\n\n"
	$(clean_parallel_dirs)
	$(MPIRUN) $(NP) $(ORAC_P) < 3.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_3; done
	@printf "\n... Test 3 completed.\n You may want to compare OUT_TEST_3 with the reference OUT_3\n"
	@printf "Please read the NOTE on tests in the README file\n\n"

OUT_TEST_4: OUT_TEST_3
	@printf "Starting test 4...\n\n"
	$(MPIRUN) $(NP) $(ORAC_P) < 4.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_4; done
	@printf "\n... Test 4 completed.\n You may want to compare OUT_TEST_4 with the reference OUT_4\n"
	@printf "Please read the NOTE on tests in the README file\n\n"

OUT_TEST_5: 
	@printf "Starting test 5...\n\n"
	$(clean_parallel_dirs)
	$(MPIRUN) $(NP) $(ORAC_P) < 5.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_5; done
	@printf "\n... Test 5 completed.\n You may want to compare OUT_TEST_5 with the reference OUT_5\n"
	@printf "Please read the NOTE on tests in the README file\n\n"

OUT_TEST_6: 
	@printf "Starting test 6...\n\n"
	$(clean_parallel_dirs)
	$(MPIRUN) $(NP) $(ORAC_P) < 6.in
	@for i in PAR????/REM_DIAGNOSTIC ; do tail -10 $$i >> OUT_TEST_6; done
	@printf "\n... Test 6 completed.\n You may want to compare OUT_TEST_6 with the reference OUT_6\n"
	@printf "Please read the NOTE on tests in the README file\n\n"


$(OUT_TESTS): $(ORAC_P)

$(ORAC_P): config.H
	@ echo "-------------------------------------------------------------------------"
	@ echo "WARNING: the ORAC_P 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_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) < $< > $@

clean:
	-rm -fr *~ *# $(TMP_FILES) $(TMP_DIRS) $(OUT_TESTS) > /dev/null 2>&1 

realclean: clean

distclean: clean
