#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 = SAPRC99_Precision.f90  \
	 SAPRC99_Parameters.f90     \
	 SAPRC99_Global.f90  

GENOBJ = SAPRC99_Precision.o    \
	 SAPRC99_Parameters.o       \
	 SAPRC99_Global.o     

FUNSRC = SAPRC99_Function.f90 
FUNOBJ = SAPRC99_Function.o 

JACSRC = SAPRC99_JacobianSP.f90  SAPRC99_Jacobian.f90
JACOBJ = SAPRC99_JacobianSP.o    SAPRC99_Jacobian.o

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

STMSRC = SAPRC99_StoichiomSP.f90 SAPRC99_Stoichiom.f90 
STMOBJ = SAPRC99_StoichiomSP.o   SAPRC99_Stoichiom.o

UTLSRC = SAPRC99_Rates.f90 SAPRC99_Util.f90 SAPRC99_Monitor.f90
UTLOBJ = SAPRC99_Rates.o   SAPRC99_Util.o   SAPRC99_Monitor.o

LASRC  = SAPRC99_LinearAlgebra.f90 
LAOBJ  = SAPRC99_LinearAlgebra.o   

STOCHSRC = SAPRC99_Stochastic.f90 
STOCHOBJ = SAPRC99_Stochastic.o 

MAINSRC = SAPRC99_Main.f90   SAPRC99_Initialize.f90   SAPRC99_Integrator.f90 SAPRC99_Model.f90
MAINOBJ = SAPRC99_Main.o     SAPRC99_Initialize.o     SAPRC99_Integrator.o   SAPRC99_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 SAPRC99.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 SAPRC99_stochastic.exe

mex:    $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O SAPRC99_mex_Fun.f90     $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O SAPRC99_mex_Jac_SP.f90  $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O SAPRC99_mex_Hessian.f90 $(ALLOBJ)

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

distclean:
	rm -f SAPRC99*.o SAPRC99*.mod \
	SAPRC99*.dat SAPRC99.exe SAPRC99.map \
	SAPRC99*.f90 SAPRC99_*.mexglx

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

SAPRC99_Parameters.o: SAPRC99_Parameters.f90 \
	            SAPRC99_Precision.o
	$(FC) $(FOPT) -c $<

SAPRC99_Monitor.o: SAPRC99_Monitor.f90 \
	             SAPRC99_Precision.o
	$(FC) $(FOPT) -c $<

SAPRC99_Global.o: SAPRC99_Global.f90 \
	            SAPRC99_Parameters.o SAPRC99_Precision.o
	$(FC) $(FOPT) -c $<

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

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

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

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

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

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

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

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

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

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

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

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

SAPRC99_Main.o: SAPRC99_Main.f90  $(ALLOBJ) SAPRC99_Initialize.o SAPRC99_Model.o SAPRC99_Integrator.o
	$(FC) $(FOPT) -c $<

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

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