# Copyright © 2022 - Rennes Physics Institute
#
# This file is part of MsSpec-DFM.
#
# MsSpec-DFM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# MsSpec-DFM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with MsSpec-DFM.  If not, see <http://www.gnu.org/licenses/>.
#
# Source file  : src/Makefile
# Last modified: Fri, 25 Feb 2022 17:27:18 +0100
# Committed by : Sylvain Tricot <sylvain.tricot@univ-rennes1.fr> 1645806435 +0100

#
# Type 'make help' for some help
#
###############################################################################
# OPTIONS                                                                     #
###############################################################################
DEBUG    = 0
BUILDDIR = build
SO       = eps.so
FC       = gfortran
PREFIX   = $(HOME)/.local
AUTHORS  = Didier Sébilleau <didier.sebilleau@univ-rennes1.fr>, \
		   Aditi Mandal <p2016009@pilani.bits-pilani.ac.in>, \
		   Sylvain Tricot <sylvain.tricot@univ-rennes1.fr>

################################################################################
# /!\ DO NOT EDIT BELOW THAT LINE (unlesss you know what you're doing...)      #
# CORE CONFIGURATION                                                           #
################################################################################
SHELL = bash

ifeq ($(DEBUG),0)
  FFLAGS =
else
  FFLAGS = -g -fbounds-check -fbacktrace -ffpe-trap=zero,overflow,underflow,invalid,denormal
endif

# Make sure to use binaries of the of the virtualenv
ifdef VIRTUAL_ENV
  PYTHON = $(VIRTUAL_ENV)/bin/python
  F2PY = $(VIRTUAL_ENV)/bin/f2py
  PIP = $(VIRTUAL_ENV)/bin/pip
else
  PYTHON = $(shell which python)
  F2PY = $(shell which f2py)
  PIP = $(shell which pip)
endif
F2PYFLAGS = --f77exec=$(FC) --f90exec=$(FC)


# Ensure FC exists
ifeq (,$(shell which $(FC)))
  $(error Missing $(FC) compiler)
endif

# Ensure python exists
ifeq (,$(shell which $(PYTHON)))
  $(error Missing Python executable)
endif

# Ensure pip is installed
ifeq (,$(shell which $(PIP)))
  $(error pip was not found)
endif



PYTHON_PKG_NAME = msspec_dfm
VERSION = $(shell cd python && $(PYTHON) -c 'from $(PYTHON_PKG_NAME) import __version__; print(__version__)')
SRCDIR  = fortran/DFM_library
PYPKG   = python/$(PYTHON_PKG_NAME)


# Source files
MANPAGE := man/eps.1

cmn_DEPS:=$(SRCDIR)/ACCURACY_LIBRARY/accuracy.f90 \
        $(SRCDIR)/INPUT_OUTPUT_LIBRARY/input_values.f90 \
        $(SRCDIR)/DIMENSIONS_LIBRARY/dimensions.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/simple_numbers.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/powers_of_ten.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/mathematical_constants.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/physical_constants.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/factorials.f90 \
		$(SRCDIR)/ERROR_HANDLING_LIBRARY/error_caltech.f90 \
		$(SRCDIR)/PRINT_LIBRARY/printfiles.f90 \
		$(SRCDIR)/STRUCTURE_FACTOR_LIBRARY/utic_values.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/external_damping.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/Fermi_values.f90

tool_SRCS:=$(cmn_DEPS) \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/external_functions.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/Lindhard_function.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/gamma.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/digamma.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/2F1_real.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/mod_mlf_garrappa.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/derivation.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/smoothing.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/interpolation.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/integration.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/integration4.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/transforms.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/basic_functions.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/bessel.f90

util_SRCS:=$(cmn_DEPS) \
		$(SRCDIR)/UTILITIES_LIBRARY/utilities_1.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_1.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_4.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_6.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_7.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_8.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_9.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/find_zero.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/polynomial_equations.f90

prop_SRCS:=$(cmn_DEPS) \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/calc_Fermi.f90 \
		$(SRCDIR)/PLASMON_LIBRARY/plasmon_ene.f90 \
		$(SRCDIR)/SCALE_PARAMETERS_LIBRARY/scale_parameters.f90 \
		$(SRCDIR)/ENERGIES_LIBRARY/correlation_energies.f90 \
		$(SRCDIR)/ENERGIES_LIBRARY/delta_t.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/thermodynamic_properties.f90 \
		$(SRCDIR)/SCREENING_LIBRARY/screening_vec1.f90 \
		$(SRCDIR)/CONFINEMENT_LIBRARY/confinement_ff.f90 \
		$(SRCDIR)/CONFINEMENT_LIBRARY/coulomb.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/utilities_3.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/material_properties.f90 \
		$(SRCDIR)/THERMAL_PROPERTIES_LIBRARY/thermal_properties.f90 \
		$(SRCDIR)/ENERGIES_LIBRARY/xc_energies.f90 \
		$(SRCDIR)/ENERGIES_LIBRARY/exchange_energies.f90 \
		$(SRCDIR)/ENERGIES_LIBRARY/kinetic_energies.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/thermodynamic_quantities.f90 \
		$(SRCDIR)/THERMAL_PROPERTIES_LIBRARY/chemical_potential.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/utilities_4.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/calc_energies.f90
#
#  Read input data file:
#
read_SRCS:=$(tool_SRCS) $(util_SRCS) $(prop_SRCS) \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/read_data.f90

io_SRCS:=$(SRCDIR)/INPUT_OUTPUT_LIBRARY/filenames.f90 \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/outfiles.f90 \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/change_filenames.f90 \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/store_coef.f90 \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/open_files.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_headers.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_calc_type.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_Fermi.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_plasmons.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_scale_param.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_material_lengths.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_thermal.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_thermodynamics.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_energies.f90 \
		$(SRCDIR)/INPUT_OUTPUT_LIBRARY/close_files.f90

calc_DEPS:=$(cmn_DEPS) \
		$(SRCDIR)/DIELECTRIC_FUNCTIONS_LIBRARY/dfuncl_static.f90 \
		$(SRCDIR)/ASYMPTOTIC_VALUES_LIBRARY/gr_asymptotic.f90 \
		$(SRCDIR)/ASYMPTOTIC_VALUES_LIBRARY/sq_asymptotic.f90 \
		$(SRCDIR)/CONFINEMENT_LIBRARY/confinement_wf.f90 \
		$(SRCDIR)/LOCAL_FIELD_LIBRARY/iq_functions_1.f90 \
		$(SRCDIR)/MOMENTS_LIBRARY/loss_moments.f90 \
		$(SRCDIR)/PAIR_DISTRIBUTION_FUNCTION/pair_distribution.f90 \
		$(SRCDIR)/PAIR_CORRELATION_LIBRARY/pair_correlation.f90 \
		$(SRCDIR)/STRUCTURE_FACTOR_LIBRARY/structure_factor_static.f90 \
		$(SRCDIR)/LOCAL_FIELD_LIBRARY/iq_functions_2.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_2.f90 \
		$(SRCDIR)/ASYMPTOTIC_VALUES_LIBRARY/gamma_asymptotic.f90 \
		$(SRCDIR)/ASYMPTOTIC_VALUES_LIBRARY/calc_asymptotic.f90 \
		$(SRCDIR)/LOCAL_FIELD_LIBRARY/local_field_static.f90 \
		$(SRCDIR)/LOCAL_FIELD_LIBRARY/local_field_static_2.f90 \
		$(SRCDIR)/STRUCTURE_FACTOR_LIBRARY/structure_factor_static_2.f90 \
		$(SRCDIR)/NEVANLINNA_FUNCTIONS_LIBRARY/Nevanlinna_functions.f90 \
		$(SRCDIR)/MEMORY_FUNCTIONS_LIBRARY/memory_functions.f90 \
		$(SRCDIR)/SCREENING_LIBRARY/screening_vec2.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/velocities.f90 \
		$(SRCDIR)/SCALE_PARAMETERS_LIBRARY/scale_parameters.f90 \
		$(SRCDIR)/MOMENTS_LIBRARY/moments.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_3.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_10.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/coulomb_log.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/packing_fraction.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/classical_fluid.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/diffusion_coefficient.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/electron_phonon_int.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/scattering_length.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/lifetime.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/viscosity.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/relaxation_time_static.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/utic_parameters.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/decay_rate.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/calc_damping.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/diffusion_coefficient_2.f90 \
		$(SRCDIR)/PLASMON_LIBRARY/plasmon_dispersion.f90 \
		$(SRCDIR)/PLASMON_LIBRARY/q_bounds.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/plasmon_damping.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/calc_energies.f90 \
		$(SRCDIR)/PHYSICAL_PROPERTIES_LIBRARY/grand_partition.f90 \
		$(SRCDIR)/UTILITIES_LIBRARY/utilities_2.f90 \
		$(SRCDIR)/LANDAU_PARAMETERS_LIBRARY/landau.f90 \
		$(SRCDIR)/VARIOUS_FUNCTIONS_LIBRARY/phi_function.f90 \
		$(SRCDIR)/DIELECTRIC_FUNCTIONS_LIBRARY/dfuncl_stan_dynamic.f90 \
		$(SRCDIR)/DIELECTRIC_FUNCTIONS_LIBRARY/dfuncl_magn_dynamic.f90 \
		$(SRCDIR)/DIELECTRIC_FUNCTIONS_LIBRARY/dfunct_dynamic.f90 \
		$(SRCDIR)/STRUCTURE_FACTOR_LIBRARY/structure_factor_dynamic_2.f90 \
		$(SRCDIR)/INTERACTION_POTENTIALS_LIBRARY/interaction_potentials_r.f90 \
		$(SRCDIR)/INTERACTION_POTENTIALS_LIBRARY/interaction_potentials_k.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/decay_rate.f90 \
		$(SRCDIR)/STRUCTURE_FACTOR_LIBRARY/structure_factor_dynamic.f90 \
		$(SRCDIR)/DIELECTRIC_FUNCTIONS_LIBRARY/dfuncl_stan_dynamic_2.f90 \
		$(SRCDIR)/DAMPING_LIBRARY/mean_free_path.f90 \
		$(SRCDIR)/SPECTRAL_FUNCTION_LIBRARY/spectral_function.f90 \
		$(SRCDIR)/LOCAL_FIELD_LIBRARY/local_field_dynamic.f90 \
		$(SRCDIR)/PRINT_LIBRARY/print_asymptotic.f90 \
		$(SRCDIR)/POST_PROCESSING_LIBRARY/re_eps_0_treatment.f90 \
		$(SRCDIR)/MOMENTS_LIBRARY/moments_loss.f90 \
		$(SRCDIR)/SPECIFIC_INTEGRALS_LIBRARY/specific_int_5.f90
#
#  Calculators:
#
calc_SRCS:=$(SRCDIR)/CALCULATORS_LIBRARY/calculators_1.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_2.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_3.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_5.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_7.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_8.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_9.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_1.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_2.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_3.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_5.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_7.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_8.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_9.f90
#
#  Test_integrals:
#
calc_TEST:=$(SRCDIR)/TEST_LIBRARY/test_integrals_2.f90 \
		$(SRCDIR)/TEST_LIBRARY/test_integrals_3.f90 \
		$(SRCDIR)/TEST_LIBRARY/test_integrals_8.f90 \
		$(SRCDIR)/TEST_LIBRARY/test_int_Hubbard.f90
#
#  Post-processing:
#
calc_POST:=$(SRCDIR)/PLASMON_LIBRARY/plasmon_dispersion_2.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/calculators_p.f90 \
		$(SRCDIR)/CALCULATORS_LIBRARY/call_calc_p.f90


SRCS:=$(read_SRCS) $(io_SRCS) $(calc_DEPS) $(calc_SRCS) $(calc_TEST) $(calc_POST)
OBJS:=$(addprefix $(BUILDDIR)/,$(patsubst %.f90,%.o,$(SRCS)))


.PHONY: all obj so pypkg required_python_packages install uninstall man clean help


all: required_python_packages so


obj: $(OBJS)


so: python/$(PYTHON_PKG_NAME)/$(SO)


pypkg: python/dist/$(PYTHON_PKG_NAME)-$(VERSION).tar.gz


required_python_packages:
	@$(PIP) install numpy click-man


python/VERSION:
	@cat $(VERSION) > $@


install: python/dist/$(PYTHON_PKG_NAME)-$(VERSION).tar.gz
	@echo "Installing $^"
	@$(PIP) install $^


python/dist/$(PYTHON_PKG_NAME)-$(VERSION).tar.gz: python/$(PYTHON_PKG_NAME)/$(SO)
python/dist/$(PYTHON_PKG_NAME)-$(VERSION).tar.gz: man
python/dist/$(PYTHON_PKG_NAME)-$(VERSION).tar.gz: python/MANIFEST.in python/setup.py python/pip.freeze $(shell find python/$(PYTHON_PKG_NAME) -name '*.py')
	@cd python && $(PYTHON) setup.py sdist


python/$(PYTHON_PKG_NAME)/$(SO): $(OBJS) $(SRCDIR)/../_dfm.f90
	@echo "Building $@ shared object..."
	@mkdir -p $(BUILDDIR)
	@$(F2PY) $(F2PYFLAGS) -I$(BUILDDIR) -m $(BUILDDIR).$(basename $(@F)) -c $(filter-out $(lastword $^), $^) $(SRCDIR)/../_dfm.f90 && mv $(BUILDDIR)/*.so $@


$(BUILDDIR)/%.o: %.f90
	@echo "Compiling $@..."
	@mkdir -p $(BUILDDIR)/$(dir $^)
	@$(FC) $(FFLAGS) -J $(BUILDDIR) -I $(BUILDDIR) -fPIC -o $@ -c $<


man: python/man/inputfile.spec
	@cd python && $(PYTHON) setup.py --command-packages=click_man.commands man_pages --target ./man/pages
	@cd python/man/pages && cat ../inputfile.spec >> *-generate.1
	@cd python/man/pages && echo -e ".SH AUTHORS\n.PP\n$(AUTHORS)" | tee -a *.1 > /dev/null
	@cd python/man/pages && gzip -f *.1


uninstall:
	@$(PIP) uninstall -y $(PYTHON_PKG_NAME)


clean:
	@rm -rf  $(BUILDDIR)
	@rm -f   python/$(PYTHON_PKG_NAME)/*.so
	@rm -rf  python/dist 
	@rm -rf  python/*.egg-info
	@rm -f   python/VERSION
	@rm -rf  python/man/pages


help:
	@echo "Type 'make' or 'make all' to build."
	@echo "Type 'make install' to install the code."
