#====================================================================#
#pdelphi  Makefile for PARALLEL DELPHI PROGRAM
#====================================================================#

#++++++++++++++++++++++ user defined parameters +++++++++++++++++++++#
##set LL     to one of : pc    (compile on singple pc for testing)
##                       pgnu  (compile on palmetto w/ gnu compiler)
##                       pint  (compile on palmetto w/ intel compiler)
##                       zgnu  (compile on zeus     w/ gnu compiler) 
##                       zint  (compile on zeus     w/ intel compiler)
##set INPUT  to be parameter file name
##set OUTPUT to be desired ouput file name
##set CODE   to be desired executable name
##set NPROC  to be the number of processors, must >= 2
LL     = pgnu
INPUT  = param.txt
OUTPUT = out95.txt
CODE   = pdelphi95
NPROC  = 3 

#++++++++++++++++++ compiler and source code related ++++++++++++++++#
FFLAG = -fcray-pointer -ffixed-line-length-none -fdefault-real-8
DFLAG = -DLINUX -DIFC -DDP

#+++++++++++++++++++++++ set parallel compilers +++++++++++++++++++++#
ifeq ($(LL), pc) #on single PC
#----------- compile using OPENMPI -----------#
###   MPICOMP  = /usr/lib/openmpi/bin/mpif90
###   CFLAG    = -g #-Wall #compiling flag
###   MPIEXEC  = /usr/lib/openmpi/bin/mpirun
      MPI      = /usr/lib/openmpi
      MPIlnk   = -I$(MPI)/include -L$(MPI)/lib
###   MPIlnk   = -I/usr/include/openmpi-i386 -L$(MPI)/lib
      MPICOMP  = $(MPI)/bin/mpif90
      CFLAG    = -g $(MPIlnk) #-Wall #compiling flag
      MPIEXEC  = $(MPI)/bin/mpirun 
#----------- compile using MPICH2 ------------#
###      MPI      = /usr/lib/mpich2
###      MPIlnk   = -I$(MPI)/include -L$(MPI)/lib
###      MPICOMP  = $(MPI)/bin/mpif90
###      CFLAG    = -g $(MPIlnk) #-Wall #compiling flag
###      MPIEXEC  = $(MPI)/bin/mpirun 
endif

ifeq ($(LL), zgnu) #on zeus
   MPI      = /opt/mpich2/ch3_ssm-gnu43
   MPIlnk   = -I$(MPI)/include -L$(MPI)/lib
   MPICOMP  = $(MPI)/bin/mpif90
   CFLAG    = -O3 $(MPIlnk)
endif

ifeq ($(LL), pgnu) #on palmetto
#----------- compile using OPENMPI -----------#
###   MPI      = /opt/openmpi/1.4.3
###   MPIlnk   = -I$(MPI)/include -L$(MPI)/lib
###   MPICOMP  = $(MPI)/bin/mpif90
###   CFLAG    = -O3 $(MPIlnk)
#----------- compile using MPICH2 ------------#
      MPI      = /opt/mpich2/1.4
      MPIlnk   = -I$(MPI)/include -L$(MPI)/lib
      MPICOMP  = $(MPI)/bin/mpif90
      CFLAG    = -O3 $(MPIlnk) 
endif

#.....add your compiler here

#++++++++++++++++++++ set source code components ++++++++++++++++++++#
VPATH=./src
FLAGS = $(CFLAG) $(FFLAG) $(DFLAG)

COMM = qlog.f95 pointers.f95 oper_coord.f95 misc.f95 misc2.f95
MODL = crgarrmod.f95 epsmakmod.f95 qqintmod.f95  setrcmod.f95 \
       setbcmod.f95 encalcmod.f95 extrmmod.f95 rdhmod.f95 \
       setcrgmod.f95 wrtsitmod.f95
MAIN = pdelphi.f95 mainMR.f95 mainWR.f95

COMMOBJ = $(COMM:.f95=.o)
MODLOBJ = $(MODL:.f95=.o)
MAINOBJ = $(MAIN:.f95=.o)

#+++++++++++++++++++++ make: create executable ++++++++++++++++++++++#
default: pdelphi95

pdelphi95:$(COMMOBJ) $(MODLOBJ) $(MAINOBJ) $(COMM) $(MODL) $(MAIN)
	$(MPICOMP) $(FLAGS) -o $(CODE) $(MAINOBJ) $(COMMOBJ) $(MODLOBJ)
	@echo "         "
	@echo ">>> LL = $(LL): compiled on `hostname -s` with $(MPICOMP) <<<"

pbs:
	@ vi PBSscript
	qsub PBSscript 

zeus:
	@ vi PBSzeus
	qsub PBSzeus

run: 
	$(MPIEXEC) -np $(NPROC) ./$(CODE) ./$(INPUT) # > ./$(OUTPUT)
	
debug:
	cp $(CODE) ./src/.
	$(MPIEXEC) -np $(NPROC) ./src/$(CODE) $(INPUT) 

clean:
	rm -f *.o *.mod ARCDAT MR* WR* test*
	
wipe:
	rm -i $(CODE) out* time* fort* pdelphi95.o*

out:
	vim $(OUTPUT)	

%.o:%.f95
	$(MPICOMP) $(FLAGS) -c $(VPATH)/$*.f95 
	
%.o:%.F95
	$(MPICOMP) $(FLAGS) -c $(VPATH)/$*.F95 	
