ALL: all
LOCDIR = .
DIRS = src include

include ${MICSC_DIR}/conf/micsc_common

all: info libmicsc

info:
	-@echo "=========================================="
	-@echo On `date` on `hostname`
	-@echo Machine characteristics: `uname -a`
	-@echo "-----------------------------------------"
	-@echo "Using MICSc directory: ${MICSC_DIR}"
	-@echo "Using PETSc directory: ${PETSC_DIR}"
	-@echo "Using PETSc arch: ${PETSC_ARCH}"
	-@echo "-----------------------------------------"
	-@grep "define PETSC_VERSION" ${PETSC_DIR}/include/petscversion.h | ${SED} "s/........//"
	-@echo "-----------------------------------------"
	-@echo "Using PETSc configure options " ${CONFIGURE_OPTIONS}
	-@echo "Using MICSCc configuration flags:"
	-@cat ${MICSC_INSTALL_DIR}/conf/micscvariables
	-@echo "Using PETSc configuration flags:"
	-@if [ -e ${PETSC_DIR}/${PETSC_ARCH}/include/petscconf.h ]; then \
	   grep "\#define " ${PETSC_DIR}/${PETSC_ARCH}/include/petscconf.h; \
          else \
	   grep "\#define " ${PETSC_DIR}/include/petscconf.h; \
          fi
	-@echo "-----------------------------------------"
	-@echo "Using include paths: ${MICSC_INCLUDE} ${PETSC_INCLUDE}"
	-@echo "------------------------------------------"
	-@echo "Using C/C++ compiler: ${PCC} ${PCC_FLAGS} ${COPTFLAGS} ${CFLAGS}"
	-@echo "C/C++ Compiler version: " `${CCV}`
	-@echo "-----------------------------------------"
	-@echo "Using libraries: ${MICSC_LIB}"
	-@echo "------------------------------------------"
	-@echo "Using mpirun: ${MPIRUN}"
	-@echo "=========================================="

${LIBNAME}: ${MICSC_SOURCEC} ${MICSC_INCLUDES}
	@${RM} ${LIBNAME}
	@${OMAKE} ${MICSC_OBJSC}
	@echo "====================================";\
	echo "Run the following to verify the install:";\
	echo "  make test";\
	echo "====================================";\
	
libmicsc: chkmicsc_dir ${LIBNAME}

tecplot: ${MICSC_SRC_DIR}/tecplot.c ${MICSC_SRC_DIR}/tecplot.h
	@${OMAKE} ${MICSC_SRC_DIR}/tecplot.o
	-${CLINKER} -o tecplot ${MICSC_SRC_DIR}/tecplot.o ${MICSC_LIB}
	@${RM} ${MICSC_SRC_DIR}/tecplot.o

bin2tecplot: ${MICSC_SRC_DIR}/bin2tecplot.c
	@${OMAKE} ${MICSC_SRC_DIR}/bin2tecplot.o
	-${CLINKER} -o bin2tecplot ${MICSC_SRC_DIR}/bin2tecplot.o ${MICSC_LIB}
	@${RM} ${MICSC_SRC_DIR}/bin2tecplot.o

binaryComparator: ${MICSC_SRC_DIR}/binaryComparator.c
	@${OMAKE} ${MICSC_SRC_DIR}/binaryComparator.o
	-${CLINKER} -o ${MICSC_DIR}/binaryComparator ${MICSC_SRC_DIR}/binaryComparator.o ${PETSC_LIB}
	@${RM} ${MICSC_SRC_DIR}/binaryComparator.o

main: ${LIBNAME} ${MAIN_SRC}
	@${OMAKE} ${MAIN_OBJ}
	-${CLINKER} $(CFLAGS) -o ${MAIN_EXEC} ${MAIN_OBJ} ${MICSC_LIB} 
	@${RM} ${MAIN_OBJ}

${MICSC_CASES_DIR}/%/main: ${LIBNAME} ${MICSC_CASES_DIR}/%/main.c
	@${OMAKE} MAIN_SRC=$@.c MAIN_EXEC=$@ main
	
examples: ${MICSC_CASES}

test: examples binaryComparator
	@MAIN_CAVITY="${MICSC_CASES_DIR}/cavity/main -max_outer_its 200 -inifl";\
	MAIN_CYLINDER="${MICSC_CASES_DIR}/cylinder/main -grid_file grd-fast.inp -max_outer_its 11 -deltat 1 -tinit 254 -tfinal 256 -out_idtevery 2";\
	MAIN_CHANNEL="${MICSC_CASES_DIR}/periodchannel/main -grid_file grd-fast.inp -max_outer_its 11 -k_restart none -deltat 1 -tfinal 2 -out_idtevery 2";\
	LOG=${MICSC_DIR}/log.txt;\
	/bin/echo -n > $$LOG;\
	cd ${MICSC_CASES_DIR}/cavity;\
    $(call test_execute, 1, $$MAIN_CAVITY, 3721, 1e-10, $$LOG, cavity); failed=$$err;\
   	$(call test_execute, 2, $$MAIN_CAVITY, 3721, 1e-5, $$LOG, cavity); failed=$$failed$$err;\
	cd ${MICSC_CASES_DIR}/cylinder;\
    $(call test_execute, 1, $$MAIN_CYLINDER, 1984, 1e-10, $$LOG, cylinder); failed=$$failed$$err;\
   	$(call test_execute, 2, $$MAIN_CYLINDER, 1984, 1e-5, $$LOG, cylinder); failed=$$failed$$err;\
	cd ${MICSC_CASES_DIR}/periodchannel;\
    $(call test_execute, 1, $$MAIN_CHANNEL, 700, 1e-10, $$LOG, periodic channel); failed=$$failed$$err;\
   	$(call test_execute, 2, $$MAIN_CHANNEL, 700, 1e-5, $$LOG, periodic channel); failed=$$failed$$err;\
	if [ $$failed -eq 0 ] ; then\
		echo "** All tests passed successfully.";\
		${RM} $$LOG;\
	else\
		echo Some tests failed. See $$LOG for more details;\
	fi;\
	${RM} ${MICSC_DIR}/binaryComparator

test_execute=\
		${RM} *.silo main.tmp trans*bin* *field*.bin* *.dat Log.* > /dev/null 2>&1;\
		err=0;\
		echo "***** $(6) test : $(1) MPI process *****" >> $(5);\
		/bin/echo -n "** Running $(6) example with" $(1) "MPI processes....." ;\
		if (${MPIEXEC} -n $(1) $(2) > main.tmp 2>&1) then\
			cd results; \
			for i in `ls *.bin 2> /dev/null`; do\
				${MICSC_DIR}/binaryComparator ../$$i $$i $(3) $(4) > /dev/null 2>&1 ;\
				if [ $$? -ne 0 ]; then\
					echo Result file $$i differs >> $(5);\
			    	err=1;\
				fi;\
			done;\
			cd .. ;\
		else\
			err=2;\
		fi;\
		if [ $$err -eq 1 ]; then\
			echo "Failed: Calculated results differ.";\
			cat main.tmp >> $(5);\
		elif [ $$err -eq 2 ]; then\
			echo "Failed. Execution error.";\
			cat main.tmp >> $(5);\
		else\
			echo "Successfully.";\
		fi;\
		echo "\n" >> $(5);\
		${RM} *.silo main.tmp Visc*bin* time*bin* \
		      *field*.bin* *.dat log.* Log.* > /dev/null 2>&1
		
alldoc: alldoc1 alldoc2

alldoc1: chk_loc deletemanualpages
	-${OMAKE} ACTION=manualpages_buildcite tree_basic LOC=${LOC}
	-@sed -e s%man+../%man+manualpages/% ${LOC}/docs/manualpages/manualpages.cit > ${LOC}/docs/manualpages/htmlmap
	-@cat ${PETSC_DIR}/src/docs/mpi.www.index >> ${LOC}/docs/manualpages/htmlmap
	-${OMAKE} ACTION=manualpages tree_basic LOC=${LOC}
	-${PETSC_DIR}/bin/maint/wwwindex.py ${MICSC_DIR} ${LOC}
	-${OMAKE} ACTION=manexamples tree_basic LOC=${LOC}

alldoc2: chk_loc
	-${OMAKE} ACTION=micsc_html PETSC_DIR=${PETSC_DIR} alltree LOC=${LOC}
	cp ${LOC}/docs/manual.htm ${LOC}/docs/index.html

alldocclean: deletemanualpages allcleanhtml

deletemanualpages: chk_loc
	-@if [ -d ${LOC} -a -d ${LOC}/docs/manualpages ]; then \
	  find ${LOC}/docs/manualpages -type f -name "*.html" -exec ${RM} {} \; ;\
	  ${RM} ${LOC}/docs/manualpages/manualpages.cit ;\
	  ${RM} ${LOC}/docs/manualpages/htmlmap ;\
	  ${RM} -r ${LOC}/docs/manualpages ;\
	fi;

allcleanhtml: 
	-${OMAKE} ACTION=cleanhtml PETSC_DIR=${PETSC_DIR} alltree