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

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

# object files from $(COMMON) directory
COMOBJ = bessel.o misc.o gmap.o spinor_symmetries.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 \
         check_screening.o checkgriduniformity.o checkbz.o eqpcor.o \
         write_program_header.o svninfo.o createpools.o mtxel_optical.o input_utils.o \
         wfn_rho_vxc_io.o splines.o eqpcor.o sort.o random.o blas.o scissors.o cells.o tile.o \
         io_utils.o epsread_hdf5.o kernel_io.o read_rho_vxc.o
COMMONOBJS = $(addprefix $(COMMON)/,$(COMOBJ)) $(GLOBALOBJS) $(TILE_LIBS)

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

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

##
## Common Absorption files
##
SRC_ABS = absp0.f90 distrib.f90 epsdiag.f90 $(COMMON)/genwf.f90 genwf_co.f90 \
	  input.f90 input_co.f90 input_q.f90 inread.f90 intpts.f90 interp.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 \
          bse_init.f90
OBJ_ABS = $(SRC_ABS:.f90=.o)

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

TOOLOBJ = bseascbin.o bsebinasc.o summarize_eigenvectors.o

# put truly external libs here, i.e. not part of our buildsystem at all
LIBS = $(SCALAPACKLIB) $(FFTWLIB) $(LAPACKLIB) $(PERFORMANCE) $(HDF5LIB)
ifeq ($(findstring -DUSEVORO,$(MATHFLAG)),-DUSEVORO)
  LIBS += $(CC_LINK_LIBS)
endif

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_ABS) $(COMMONOBJS) $(COMMON)/scalapack.o $(COMMON)/lapack.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)


bseascbin$(FLAVOR).x: bseascbin.o $(GLOBALOBJS) bse_convert.o $(COMMON)/sort.o $(COMMON)/check_inversion.o $(COMMON)/wfn_rho_vxc_io.o $(COMMON)/kernel_io.o
	$(LINK) $(FOPTS) -o $@ $^
	$(INSTALL_CMD)

bsebinasc$(FLAVOR).x: bsebinasc.o $(GLOBALOBJS) bse_convert.o $(COMMON)/sort.o $(COMMON)/check_inversion.o $(COMMON)/wfn_rho_vxc_io.o $(COMMON)/kernel_io.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 $(COMMON)/check_screening_m.mod $(COMMON)/io_utils_m.mod mtxel_kernel_m.mod $(COMMON)/fullbz_m.mod bsewrite_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_m.mod : mtxel_kernel.f90 $(COMMON)/fftw_m.mod $(COMMON)/misc_m.mod $(COMMON)/gmap_m.mod w_sum_m.mod g_sum_m.mod gx_sum_m.mod 

$(OBJ_K) $(OBJ_ABS) $(OBJ_INT) $(TOOLOBJ): $(GLOBALMODS)
bsewrite.o bsewrite_m.mod : $(COMMON)/global_m.mod $(COMMON)/io_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod  $(COMMON)/kernel_io_m.mod
interp.o interp_m.mod : $(COMMON)/global_m.mod $(COMMON)/lapack_m.mod $(COMMON)/sort_m.mod $(COMMON)/tile_m.mod
intpts.o intpts_m.mod : $(COMMON)/global_m.mod $(COMMON)/lapack_m.mod $(COMMON)/sort_m.mod
mtxel_t.o mtxel_t_m.mod : $(COMMON)/global_m.mod
haydock.o mtxel_jdos.o : $(COMMON)/random_m.mod
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 $(COMMON)/misc_m.mod $(COMMON)/checkgriduniformity_m.mod \
  $(COMMON)/cells_m.mod interp_m.mod $(COMMON)/io_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/kernel_io_m.mod epsdiag_m.mod
diagonalize.o distrib.o : $(COMMON)/scalapack_m.mod
distrib.o distrib_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 epscopy.o : $(COMMON)/misc_m.mod
distrib_kernel.o : $(COMMON)/createpools_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 $(COMMON)/read_rho_vxc_m.mod
input_co.o input.o input_q.o: $(COMMON)/io_utils_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 $(COMMON)/scissors_m.mod
haydock.o diag.o : $(COMMON)/genwf_m.mod intkernel_m.mod $(COMMON)/misc_m.mod $(COMMON)/mtxel_optical_m.mod
genwf_kernel.o : $(COMMON)/blas_m.mod $(COMMON)/gmap_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/spinor_symmetries_m.mod
epscopy.o input_kernel.o input_q.o input_co.o input_kernel.o input_kernel_q.o input_co_q.o : $(COMMON)/fullbz_m.mod $(COMMON)/checkbz_m.mod
input.o : $(COMMON)/fullbz_m.mod intpts_m.mod $(COMMON)/checkbz_m.mod
epsdiag.o epsdiag_m.mod  : $(COMMON)/gmap_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/epsread_hdf5_m.mod $(COMMON)/fullbz_m.mod \
	$(COMMON)/checkbz_m.mod $(COMMON)/misc_m.mod $(COMMON)/sort_m.mod
genwf_co.o : $(COMMON)/gmap_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/spinor_symmetries_m.mod
bseascbin.o bsebinasc.o : bse_convert_m.mod
bse_convert.o bse_convert_m.mod : $(GLOBALMODS) $(COMMON)/kernel_io_m.mod
inread.o inread_m.mod : $(GLOBALMODS) $(COMMON)/scissors_m.mod
absorption.o inteqp.o : inread_m.mod $(GLOBALMODS)
intwfn.o intwfn_m.mod : $(COMMON)/genwf_m.mod intpts_m.mod interp_m.mod mtxel_t_m.mod interp_m.mod $(GLOBALMODS) $(COMMON)/fullbz_m.mod $(COMMON)/io_utils_m.mod $(COMMON)/misc_m.mod
inteqp.o haydock.o diag.o : intwfn_m.mod $(COMMON)/fullbz_m.mod
absp0.o iterate.o : $(COMMON)/misc_m.mod
g_sum.o g_sum_m.mod : $(COMMON)/blas_m.mod
gx_sum.o gx_sum_m.mod : $(COMMON)/blas_m.mod
w_sum.o w_sum_m.mod : $(GLOBALMODS)
bse_init.o bse_init_m.mod: $(GLOBALMODS) $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/fullbz_m.mod intpts_m.mod
absorption.o inteqp.o : bse_init_m.mod
epscopy.o : $(COMMON)/epsread_hdf5_m.mod

