## Makefile (D. Prendergast/J. Deslippe Jan 2008)
##

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

SRC = rqstar.f90 input.f90 input_q.f90 \
      inread.f90 genwf_eps.f90 genwf_disk.f90 genwf_mpi.f90 mtxel.f90 epsinv.f90 mtxelmultiply.f90 \
      chi_summation.f90 chi_convergence.f90 epsilon_main.f90 lin_denominator.f90 
OBJ = $(SRC:.f90=.o)

# object files from $(COMMON) directory
COMOBJ = bessel.o subgrp.o check_inversion.o svninfo.o sort.o \
         scalapack.o misc.o gmap.o spinor_symmetries.o inversion.o write_program_header.o \
         write_matrix.o read_matrix.o fftw.o fullbz.o irrbz.o \
         fft_parallel.o minibzaverage.o vcoul_generator.o \
         trunc_cell_wire.o trunc_cell_box.o trunc_cell_box_d.o \
         trunc_scell_box_d.o checkbz.o eqpcor.o createpools.o \
         random.o input_utils.o wfn_rho_vxc_io.o blas.o lapack.o \
         splines.o eqpcor.o scissors.o io_utils.o epswrite_hdf5.o \
         epsread_hdf5.o find_kpt_match.o hdf5_io.o wfn_io_hdf5.o inread_common.o hdf5_io.o
COMMONOBJS = $(addprefix $(COMMON)/,$(COMOBJ)) $(GLOBALOBJS)

TOOLOBJEXT = epsmat_merge.o epsascbin.o epsbinasc.o ploteps.o epsinvomega.o epsomega.o printchi.o eps0sym.o
ifeq ($(findstring -DHDF5,$(MATHFLAG)),-DHDF5)
  TOOLOBJEXT += epsmat_old2hdf5.o 
endif


TOOLOBJ = $(TOOLOBJEXT) $(TOOLOBJINT)

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

default: epsilon

all: epsilon tools

tools: $(TOOLOBJ:.o=$(FLAVOR).x)

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

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


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

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

eps0sym$(FLAVOR).x: $(GLOBALOBJS) $(COMMON)/write_matrix.o $(COMMON)/epsread_hdf5.o $(COMMON)/epswrite_hdf5.o \
                    $(COMMON)/misc.o $(COMMON)/scalapack.o eps0sym.o $(COMMON)/blas.o $(COMMON)/io_utils.o $(COMMON)/hdf5_io.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

ifeq ($(findstring -DHDF5,$(MATHFLAG)),-DHDF5)
epsmat_old2hdf5$(FLAVOR).x: $(GLOBALOBJS) $(COMMON)/write_matrix.o $(COMMON)/epsread_hdf5.o $(COMMON)/epswrite_hdf5.o \
                    $(COMMON)/misc.o $(COMMON)/scalapack.o epsmat_old2hdf5.o $(COMMON)/blas.o $(COMMON)/io_utils.o $(COMMON)/hdf5_io.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)
endif

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

epsinvomega$(FLAVOR).x: $(GLOBALOBJS) epsinvomega.o $(COMMON)/epsread_hdf5.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

epsomega$(FLAVOR).x: $(GLOBALOBJS) epsomega.o $(COMMON)/read_matrix.o $(COMMON)/epsread_hdf5.o $(COMMON)/scalapack.o $(COMMON)/inversion.o \
                     $(COMMON)/lapack.o $(COMMON)/input_utils.o $(COMMON)/splines.o $(COMMON)/blas.o
	$(LINK) $(FOPTS) -o $@ $^ $(LIBS)
	$(INSTALL_CMD)

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

# dependencies

$(OBJ) $(TOOLOBJ) : $(GLOBALMODS)
epsinv.o : $(COMMON)/inversion_m.mod $(COMMON)/vcoul_generator_m.mod
epsomega.o : $(COMMON)/inversion_m.mod $(COMMON)/input_utils_m.mod

# ifort issued a warning when compiling Epsilon/epsilon_main.f90 on lawrencium
# << Space exceeded in Data Dependence Test in MAIN__               >>
# << Subdivide routine into smaller ones to avoid optimization loss >>
# set FNOOPTS to -O2 in lawrencium.mk and to $(FOPTS) in other .mk files

epsilon_main.o: epsilon_main.f90 $(COMMON)/sort_m.mod $(COMMON)/blas_m.mod $(COMMON)/gmap_m.mod input_m.mod input_q_m.mod \
                $(COMMON)/scalapack_m.mod mtxelmultiply_m.mod $(COMMON)/vcoul_generator_m.mod $(COMMON)/io_utils_m.mod \
		genwf_eps_m.mod mtxel_m.mod chi_summation_m.mod chi_convergence_m.mod $(COMMON)/input_utils_m.mod lin_denominator_m.mod \
                $(COMMON)/fftw_m.mod $(COMMON)/write_matrix_m.mod $(COMMON)/read_matrix_m.mod \
                $(COMMON)/fullbz_m.mod $(COMMON)/irrbz_m.mod $(COMMON)/hdf5_io_m.mod
	$(f90_CPP)
	$(F90_CMD_NOOPT)
	$(RM_P_F_CMD)

chi_summation.o chi_summation_m.mod : $(COMMON)/scalapack_m.mod $(COMMON)/blas_m.mod mtxelmultiply_m.mod lin_denominator_m.mod
chi_convergence.o chi_convergence_m.mod: $(GLOBALMODS)
genwf_eps.o genwf_eps_m.mod : genwf_mpi_m.mod genwf_disk_m.mod $(COMMON)/sort_m.mod $(COMMON)/fftw_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/gmap_m.mod $(COMMON)/spinor_symmetries_m.mod
genwf_mpi.o genwf_mpi_m.mod : $(COMMON)/sort_m.mod $(COMMON)/gmap_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/misc_m.mod $(COMMON)/find_kpt_match_m.mod $(COMMON)/spinor_symmetries_m.mod
genwf_disk.o genwf_disk_m.mod : $(COMMON)/sort_m.mod $(COMMON)/gmap_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/misc_m.mod $(COMMON)/find_kpt_match_m.mod $(COMMON)/spinor_symmetries_m.mod
mtxelmultiply.o mtxelmultiply_m.mod : $(COMMON)/scalapack_m.mod $(COMMON)/blas_m.mod
mtxel.o mtxel_m.mod : $(COMMON)/fftw_m.mod $(COMMON)/misc_m.mod lin_denominator_m.mod
epsinv.o epsomega.o : $(COMMON)/scalapack_m.mod
mtxel.o : $(COMMON)/fftw_m.mod
mtxel.o epsinv.o rqstar.o wfnmix_spinor.o wfnmix_QSGW.o : $(COMMON)/misc_m.mod
input.o input_m.mod input_q.o input_q_m.mod : $(COMMON)/wfn_io_hdf5_m.mod
wfnmix_spinor.o : $(COMMON)/wfn_rho_vxc_io_m.mod
wfnmix_QSGW.o : $(COMMON)/wfn_rho_vxc_io_m.mod
epsmat_intp.o: intp_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/sort_m.mod $(COMMON)/fullbz_m.mod
intp_utils.o intp_utils_m.mod: $(GLOBALMODS)
epsinv.o : $(COMMON)/write_matrix_m.mod
epsomega.o : $(COMMON)/read_matrix_m.mod
inread.o : $(COMMON)/scissors_m.mod $(COMMON)/inread_common_m.mod
lin_denominator.o lin_denominator_m.mod : $(COMMON)/sort_m.mod
epsmat_old2hdf5.o input.o eps0sym.o : $(COMMON)/epswrite_hdf5_m.mod
eps0sym.o : $(COMMON)/epsread_hdf5_m.mod $(COMMON)/write_matrix_m.mod
epsmat_old2hdf5.o : $(COMMON)/write_matrix_m.mod

input.o input_m.mod  : $(COMMON)/sort_m.mod $(COMMON)/fftw_m.mod $(COMMON)/scissors_m.mod $(COMMON)/fullbz_m.mod $(COMMON)/irrbz_m.mod $(COMMON)/checkbz_m.mod $(COMMON)/createpools_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/eqpcor_m.mod $(COMMON)/misc_m.mod $(COMMON)/epswrite_hdf5_m.mod
input_q.o input_q_m.mod : $(COMMON)/io_utils_m.mod $(COMMON)/scissors_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/eqpcor_m.mod $(COMMON)/misc_m.mod
