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

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

SRC = sigma_main.f90 inread.f90 input.f90 input_outer.f90 genwf_disk.f90 \
      genwf_mpi.f90 mtxel.f90 mtxel_ch.f90 mtxel_sxch.f90 mtxel_vxc.f90 wpeff.f90 \
      write_result.f90 write_result_hp.f90 write_result_dyn.f90 \
      write_result_dyn_hp.f90 epscopy.f90 \
      shiftenergy.f90 shiftenergy_dyn.f90 
OBJ = $(SRC:.f90=.o)

# object files from $(COMMON) directory
COMOBJ = misc.o subgrp.o bessel.o gmap.o spinor_symmetries.o check_inversion.o sort.o \
         check_screening.o checkgriduniformity.o fftw.o fullbz.o \
         fft_parallel.o vcoul_generator.o minibzaverage.o fixwings.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 write_program_header.o svninfo.o random.o input_utils.o \
         wfn_rho_vxc_io.o irrbz.o blas.o splines.o eqpcor.o scissors.o \
         epsread_hdf5.o io_utils.o read_rho_vxc.o inread_common.o
COMMONOBJS = $(addprefix $(COMMON)/,$(COMOBJ)) $(GLOBALOBJS)

TOOLOBJEXT = offdiag.o sig2wan.o


TOOLOBJ = $(TOOLOBJEXT) $(TOOLOBJINT)

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

default: sigma

all: sigma tools

#tools: offdiag$(FLAVOR).x offdiag_QSGW$(FLAVOR).x sig2wan.x
tools: $(TOOLOBJINT:.o=$(FLAVOR).x) offdiag$(FLAVOR).x sig2wan.x

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

offdiag$(FLAVOR).x: offdiag.o $(GLOBALOBJS) $(COMMON)/lapack.o
	$(LINK) $(FOPTS) -o $@ $^ $(LAPACKLIB)
	$(INSTALL_CMD)


# sig2wan does not use libraries, but misc uses BLAS so we need to link it
sig2wan.x: sig2wan.o $(GLOBALOBJS) $(COMMON)/misc.o $(COMMON)/blas.o
	$(LINK) $(FOPTS) -o $@ $^ $(LAPACKLIB)
	$(INSTALL_CMD)

# dependencies

$(OBJ) $(TOOLOBJ) : $(GLOBALMODS)
offdiag.o : $(COMMON)/lapack_m.mod

# ifort issued a warning when compiling Sigma/sigma_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
sigma_main.o: sigma_main.f90 $(GLOBALMODS) $(COMMON)/sort_m.mod $(COMMON)/misc_m.mod $(COMMON)/fullbz_m.mod \
              $(COMMON)/irrbz_m.mod $(COMMON)/fftw_m.mod $(COMMON)/vcoul_generator_m.mod mtxel_sxch_m.mod \
              $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/gmap_m.mod $(COMMON)/checkbz_m.mod $(COMMON)/check_screening_m.mod \
              $(COMMON)/input_utils_m.mod $(COMMON)/fixwings_m.mod $(COMMON)/checkgriduniformity_m.mod \
	      $(COMMON)/read_rho_vxc_m.mod $(COMMON)/io_utils_m.mod write_result_m.mod write_result_hp_m.mod \
	      write_result_dyn_m.mod write_result_dyn_hp_m.mod epscopy_m.mod
	$(f90_CPP)
	$(F90_CMD_NOOPT)
	$(RM_P_F_CMD)

input.o genwf_mpi.o genwf_disk.o : $(COMMON)/sort_m.mod
genwf_mpi.o genwf_disk.o : $(COMMON)/gmap_m.mod $(COMMON)/misc_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/spinor_symmetries_m.mod
input.o mtxel_vxc.o mtxel.o mtxel_ch.o : $(COMMON)/fftw_m.mod $(COMMON)/misc_m.mod
input.o input_outer.o sig2wan.o : $(COMMON)/misc_m.mod
input.o input_outer.o : $(COMMON)/wfn_rho_vxc_io_m.mod $(COMMON)/input_utils_m.mod $(COMMON)/eqpcor_m.mod $(COMMON)/io_utils_m.mod
input.o : $(COMMON)/createpools_m.mod $(COMMON)/fullbz_m.mod $(COMMON)/checkbz_m.mod $(COMMON)/read_rho_vxc_m.mod
mtxel_sxch.o mtxel_sxch_m.mod : $(COMMON)/misc_m.mod $(COMMON)/global_m.mod
inread.o input.o input_outer.o : $(COMMON)/scissors_m.mod
inread.o : $(COMMON)/inread_common_m.mod
write_result.o write_result_m.mod : $(COMMON)/global_m.mod
write_result_hp.o write_result_hp_m.mod : $(COMMON)/global_m.mod
write_result_dyn.o write_result_dyn_m.mod : $(COMMON)/global_m.mod
write_result_dyn_hp.o write_result_dyn_hp_m.mod : $(COMMON)/global_m.mod
wpeff.o : $(COMMON)/misc_m.mod
sigma_main.o : $(COMMON)/epsread_hdf5_m.mod
epscopy.o epscopy_m.mod : $(COMMON)/misc_m.mod $(COMMON)/io_utils_m.mod $(COMMON)/epsread_hdf5_m.mod
