#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# User: Set here the F90 compiler and options
#       Pedefined compilers: INTEL, PGF, HPUX, LAHEY
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#COMPILER = G95
#COMPILER = LAHEY
# ----------------------------------------------------
# BOXMOX extension - Christoph Knote - 20140610       
# gfortran is standard
#COMPILER = INTEL
# ----------------------------------------------------
#COMPILER = PGF
#COMPILER = HPUX
# ----------------------------------------------------
# BOXMOX extension - Christoph Knote - 20140610       
# gfortran is standard
COMPILER = GFORTRAN
# ----------------------------------------------------

FC_G95     = g95
FOPT_G95   = -cpp -O -pg -fbounds-check -fimplicit-none  -Wall -ftrace=full

FC_LAHEY   = lf95
# More aggressive for production runs:
#FOPT_LAHEY = -Cpp --pca -O
# More checking for debugging:
FOPT_LAHEY = -Cpp --chk a,e,s,u --pca --ap -O0 -g --trap --trace --chkglobal

FC_INTEL   = ifort 
# More aggressive for production runs:
#FOPT_INTEL = -cpp -O -fp-model precise -pc80 -prec_div
# More checking for debugging:
FOPT_INTEL = -cpp -O0 -fp-model strict -implicitnone -ftrapuv \
              -debug all -check all -warn all

FC_PGF     = pgf90
# More aggressive for production runs:
FOPT_PGF   = -Mpreprocess -O -fast -pc 80 -Kieee
# More checking for debugging:
#FOPT_PGF   = -Mpreprocess -O0 -Mbounds -Mchkfpstk -Mchkptr -Mchkstk \
#             -Ktrap=fp -pc 80 -Kieee

FC_HPUX    = f90
FOPT_HPUX  = -O -u +Oall +check=on

FC_GFORTRAN     = gfortran
# ----------------------------------------------------
# BOXMOX extension - Christoph Knote - 20140610       
# rate equation code creates pretty long lines        
# optimization does not help much and breaks MCM      
# FOPT_GFORTRAN   = -cpp -O
# double precision:
#FOPT_GFORTRAN   = -cpp -g -ffree-line-length-none -freal-4-real-16 -freal-8-real-16
# using GCC 4.7 and higher:
FOPT_GFORTRAN   = -cpp -g -ffree-line-length-none -freal-4-real-8
# for older compilers
#FOPT_GFORTRAN   = -cpp -g -ffree-line-length-none -fdefault-real-8
# ----------------------------------------------------

# define FULL_ALGEBRA for non-sparse integration
FC   = $(FC_$(COMPILER))
FOPT = $(FOPT_$(COMPILER)) # -DFULL_ALGEBRA

LIBS =
#LIBS = -llapack -lblas

# Command to create Matlab mex gateway routines 
# Note: use $(FC) as the mex Fortran compiler
MEX  = mex

GENSRC = RADMKA_Precision.f90  \
	 RADMKA_Parameters.f90     \
	 RADMKA_Global.f90  

GENOBJ = RADMKA_Precision.o    \
	 RADMKA_Parameters.o       \
	 RADMKA_Global.o     

FUNSRC = RADMKA_Function.f90 
FUNOBJ = RADMKA_Function.o 

JACSRC = RADMKA_JacobianSP.f90  RADMKA_Jacobian.f90
JACOBJ = RADMKA_JacobianSP.o    RADMKA_Jacobian.o

# ----------------------------------------------------
# BOXMOX extension - Christoph Knote - 20160906       
# if HESSIAN is off, avoid compilation errors by not
# linking to the Hessian objects.
ifeq ($(wildcard RADMKA_HessianSP.f90),)
	HESSRC =
	HESOBJ =
else
	HESSRC = RADMKA_HessianSP.f90   RADMKA_Hessian.f90
	HESOBJ = RADMKA_HessianSP.o     RADMKA_Hessian.o
endif
# ----------------------------------------------------

STMSRC = RADMKA_StoichiomSP.f90 RADMKA_Stoichiom.f90 
STMOBJ = RADMKA_StoichiomSP.o   RADMKA_Stoichiom.o

UTLSRC = RADMKA_Rates.f90 RADMKA_Util.f90 RADMKA_Monitor.f90
UTLOBJ = RADMKA_Rates.o   RADMKA_Util.o   RADMKA_Monitor.o

LASRC  = RADMKA_LinearAlgebra.f90 
LAOBJ  = RADMKA_LinearAlgebra.o   

STOCHSRC = RADMKA_Stochastic.f90 
STOCHOBJ = RADMKA_Stochastic.o 

MAINSRC = RADMKA_Main.f90   RADMKA_Initialize.f90   RADMKA_Integrator.f90 RADMKA_Model.f90
MAINOBJ = RADMKA_Main.o     RADMKA_Initialize.o     RADMKA_Integrator.o   RADMKA_Model.o 

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# User: modify the line below to include only the
#       objects needed by your application
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ALLOBJ = $(GENOBJ) $(FUNOBJ) $(JACOBJ) $(HESOBJ) $(STMOBJ) \
	 $(UTLOBJ) $(LAOBJ)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# User: modify the line below to include only the
#       executables needed by your application
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
all:    exe

exe:	$(ALLOBJ) $(MAINOBJ) 
	$(FC) $(FOPT) $(ALLOBJ) $(MAINOBJ) $(LIBS) -o RADMKA.exe

pylib:  $(ALLOBJ) lib_Initialize.o lib_Integrator.o lib_Model.o
	f2py -c -m boxmox $(ALLOBJ) lib_Initialize.o lib_Integrator.o lib_Model.o lib_Main.f90

stochastic:$(ALLOBJ) $(STOCHOBJ) $(MAINOBJ)
	$(FC) $(FOPT) $(ALLOBJ) $(STOCHOBJ) $(MAINOBJ) $(LIBS) \
	-o RADMKA_stochastic.exe

mex:    $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O RADMKA_mex_Fun.f90     $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O RADMKA_mex_Jac_SP.f90  $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O RADMKA_mex_Hessian.f90 $(ALLOBJ)

clean:
	rm -f RADMKA*.o RADMKA*.mod \
	RADMKA*.dat RADMKA.exe RADMKA*.mexglx \
	RADMKA.map

distclean:
	rm -f RADMKA*.o RADMKA*.mod \
	RADMKA*.dat RADMKA.exe RADMKA.map \
	RADMKA*.f90 RADMKA_*.mexglx

RADMKA_Precision.o: RADMKA_Precision.f90 
	$(FC) $(FOPT) -c $<

RADMKA_Parameters.o: RADMKA_Parameters.f90 \
	            RADMKA_Precision.o
	$(FC) $(FOPT) -c $<

RADMKA_Monitor.o: RADMKA_Monitor.f90 \
	             RADMKA_Precision.o
	$(FC) $(FOPT) -c $<

RADMKA_Global.o: RADMKA_Global.f90 \
	            RADMKA_Parameters.o RADMKA_Precision.o
	$(FC) $(FOPT) -c $<

RADMKA_Initialize.o: RADMKA_Initialize.f90  $(GENOBJ) 
	$(FC) $(FOPT) -c $<

RADMKA_Function.o: RADMKA_Function.f90  $(GENOBJ) 
	$(FC) $(FOPT) -c $<

RADMKA_Stochastic.o: RADMKA_Stochastic.f90  $(GENOBJ) 
	$(FC) $(FOPT) -c $<

RADMKA_JacobianSP.o: RADMKA_JacobianSP.f90 $(GENOBJ)
	$(FC) $(FOPT) -c $<

RADMKA_Jacobian.o: RADMKA_Jacobian.f90  $(GENOBJ) RADMKA_JacobianSP.o
	$(FC) $(FOPT) -c $<

RADMKA_LinearAlgebra.o: RADMKA_LinearAlgebra.f90 $(GENOBJ) RADMKA_JacobianSP.o
	$(FC) $(FOPT) -c $<

# ----------------------------------------------------
# BOXMOX extension - Christoph Knote - 20140610
# need Util in Rates
#RADMKA_Rates.o: RADMKA_Rates.f90  $(GENOBJ)
RADMKA_Rates.o: RADMKA_Rates.f90  $(GENOBJ) RADMKA_Util.o
# ----------------------------------------------------
	$(FC) $(FOPT) -c $<

RADMKA_HessianSP.o: RADMKA_HessianSP.f90  $(GENOBJ)
	$(FC) $(FOPT) -c $<

RADMKA_Hessian.o:  RADMKA_Hessian.f90 $(GENOBJ) RADMKA_HessianSP.o
	$(FC) $(FOPT) -c $<

RADMKA_StoichiomSP.o: RADMKA_StoichiomSP.f90 $(GENOBJ)
	$(FC) $(FOPT) -c $<

RADMKA_Stoichiom.o: RADMKA_Stoichiom.f90  $(GENOBJ) RADMKA_StoichiomSP.o
	$(FC) $(FOPT) -c $<

RADMKA_Util.o: RADMKA_Util.f90  $(GENOBJ) RADMKA_Monitor.o
	$(FC) $(FOPT) -c $<

RADMKA_Main.o: RADMKA_Main.f90  $(ALLOBJ) RADMKA_Initialize.o RADMKA_Model.o RADMKA_Integrator.o
	$(FC) $(FOPT) -c $<

RADMKA_Model.o: RADMKA_Model.f90  $(ALLOBJ) RADMKA_Integrator.o
	$(FC) $(FOPT) -c $<

RADMKA_Integrator.o: RADMKA_Integrator.f90  $(ALLOBJ)
	$(FC) $(FOPT) -c $<
