## Makefile (D. Prendergast July 2008)
##

PREFIX=..
include $(PREFIX)/Common/common-rules.mk

# object files from $(COMMON) directory
COMOBJ = bessel.o misc.o gmap.o check_inversion.o \
         vcoul_generator.o fftw.o fft_parallel.o \
         trunc_cell_wire.o minibzaverage.o \
         trunc_cell_box.o trunc_cell_box_d.o trunc_scell_box_d.o fullbz.o \
         checkconsistency.o checkgriduniformity.o checkbz.o eqpcor.o \
         write_program_header.o svninfo.o createpools.o mtxel_m.o mtxel_v.o input_utils.o \
         wfn_rho_vxc_io.o splines.o eqpcor.o sort.o random.o blas.o
COMMONOBJS = $(addprefix $(COMMON)/,$(COMOBJ)) $(GLOBALOBJS)

##
## Kernel files
##
SRC_K	= bsewrite.f90 distrib_kernel.f90 epscopy.f90 excwf.f90 genwf_kernel.f90 \
	  input_kernel.f90 inread_kernel.f90 mtxel_kernel.f90 kernel.f90 sortbyq.f90

OBJ_K	= $(SRC_K:.f90=.o)

##
## Common Absorption files
##
SRC_HD	= absp0.f90 distrib.f90 epsdiag.f90 $(COMMON)/genwf.f90 genwf_co.f90 \
	  input.f90 input_co.f90 input_q.f90 inread.f90 intpts.f90 \
          intkernel.f90 intwfn.f90 mtxel_t.f90 mtxel_jdos.f90 input_co_q.f90 diag.f90 \
          haydock.f90 absh.f90 iterate.f90 absp.f90 diagonalize.f90 write_eigenvectors.f90 \
          dynamic_screening.f90
OBJ_HD	= $(SRC_HD:.f90=.o)

SRC_INT = distrib.f90 $(COMMON)/genwf.f90 genwf_co.f90 \
	  input.f90 input_co.f90 input_q.f90 inread.f90 intpts.f90 \
          intkernel.f90 intwfn.f90 mtxel_t.f90 input_co_q.f90
OBJ_INT	= $(SRC_INT:.f90=.o)

TOOLOBJ = bseascbin.o bsebinasc.o summarize_eigenvectors.o

LIBS = $(SCALAPACKLIB) $(FFTWLIB) $(LAPACKLIB) $(PERFORMANCE)

default: bse

all: bse tools

bse: kernel absorption
kernel: kernel$(FLAVOR).x
absorption: absorption$(FLAVOR).x
inteqp: inteqp$(FLAVOR).x

tools: bseascbin$(FLAVOR).x bsebinasc$(FLAVOR).x summarize_eigenvectors$(FLAVOR).x inteqp

kernel$(FLAVOR).x: $(OBJ_K) $(COMMONOBJS)
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

inteqp$(FLAVOR).x: inteqp.o $(OBJ_INT) $(COMMONOBJS) $(COMMON)/scalapack.o $(COMMON)/lapack.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

absorption$(FLAVOR).x: absorption.o $(OBJ_HD) $(COMMONOBJS) $(COMMON)/scalapack.o $(COMMON)/lapack.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)


bseascbin$(FLAVOR).x: bseascbin.o $(GLOBALOBJS) bse_convert.o
	$(LINK) $(FOPTS) -o $@ $^
	$(INSTALL_CMD)

bsebinasc$(FLAVOR).x: bsebinasc.o $(GLOBALOBJS) bse_convert.o
	$(LINK) $(FOPTS) -o $@ $^
	$(INSTALL_CMD)

summarize_eigenvectors$(FLAVOR).x: summarize_eigenvectors.o $(GLOBALOBJS)
	$(LINK) $(FOPTS) -o $@ $^
	$(INSTALL_CMD)

# dependencies

# ifort -O3 -warn all can crash compiling this file
kernel.o : kernel.f90 $(COMMON)/vcoul_generator_m.mod
	$(f90_CPP)
	$(F90_CMD_NOOPT)
	$(RM_P_F_CMD)

# sunf90 can freeze your computer trying to optimize this
mtxel_kernel.o : mtxel_kernel.f90
	$(f90_CPP)
	$(F90_CMD_NOOPT)
	$(RM_P_F_CMD)

$(OBJ_K) $(OBJ_H) $(OBJ_D) $(OBJ_HD) $(OBJ_IT) $(TOOLOBJ): $(GLOBALMODS)
intpts.o intpts_m.mod : $(COMMON)/global_m.mod $(COMMON)/lapack_m.mod $(COMMON)/sort_m.mod
haydock.o mtxel_jdos.o : $(COMMON)/random_m.mod
epsdiag.o input_kernel.o genwf_co.o genwf_kernel.o input.o : $(COMMON)/sort_m.mod
intkernel.o intkernel_m.mod : $(COMMON)/blas_m.mod intpts_m.mod $(COMMON)/vcoul_generator_m.mod
diagonalize.o distrib.o : $(COMMON)/scalapack_m.mod
distrib.o distrib_kernel.o mtxel_kernel.o kernel.o : $(COMMON)/fftw_m.mod
absp.o absp0.o distrib.o distrib_kernel.o input_kernel.o input_co.o input_co_q.o input_q.o input.o epsdiag.o epscopy.o mtxel_kernel.o : $(COMMON)/misc_m.mod
input_kernel.o input_co.o input_co_q.o input_q.o input.o : $(COMMON)/input_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod
input_co.o input_co_q.o input_q.o input.o : $(COMMON)/input_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/eqpcor_m.mod
haydock.o diag.o : $(COMMON)/genwf_m.mod intkernel_m.mod
genwf_kernel.o : $(COMMON)/blas_m.mod $(COMMON)/gmap_m.mod
epscopy.o input_kernel.o epsdiag.o input_q.o input_co.o input_kernel.o input_kernel_q.o input_co_q.o : $(COMMON)/fullbz_m.mod
input.o : $(COMMON)/fullbz_m.mod intpts_m.mod
epsdiag.o mtxel_kernel.o genwf_co.o : $(COMMON)/gmap_m.mod
bseascbin.o bsebinasc.o : bse_convert_m.mod
bse_convert.o bse_convert_m.mod : $(GLOBALMODS)
inread.o inread_m.mod : $(GLOBALMODS)
absorption.o inteqp.o : inread_m.mod $(GLOBALMODS)
intwfn.o intwfn_m.mod : $(COMMON)/genwf_m.mod intpts_m.mod $(GLOBALMODS)
inteqp.o haydock.o diag.o : intwfn_m.mod
