#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 = CB05_CSIRO_Precision.f90  \
	 CB05_CSIRO_Parameters.f90     \
	 CB05_CSIRO_Global.f90  

GENOBJ = CB05_CSIRO_Precision.o    \
	 CB05_CSIRO_Parameters.o       \
	 CB05_CSIRO_Global.o     

FUNSRC = CB05_CSIRO_Function.f90 
FUNOBJ = CB05_CSIRO_Function.o 

JACSRC = CB05_CSIRO_JacobianSP.f90  CB05_CSIRO_Jacobian.f90
JACOBJ = CB05_CSIRO_JacobianSP.o    CB05_CSIRO_Jacobian.o

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

STMSRC = CB05_CSIRO_StoichiomSP.f90 CB05_CSIRO_Stoichiom.f90 
STMOBJ = CB05_CSIRO_StoichiomSP.o   CB05_CSIRO_Stoichiom.o

UTLSRC = CB05_CSIRO_Rates.f90 CB05_CSIRO_Util.f90 CB05_CSIRO_Monitor.f90
UTLOBJ = CB05_CSIRO_Rates.o   CB05_CSIRO_Util.o   CB05_CSIRO_Monitor.o

LASRC  = CB05_CSIRO_LinearAlgebra.f90 
LAOBJ  = CB05_CSIRO_LinearAlgebra.o   

STOCHSRC = CB05_CSIRO_Stochastic.f90 
STOCHOBJ = CB05_CSIRO_Stochastic.o 

MAINSRC = CB05_CSIRO_Main.f90   CB05_CSIRO_Initialize.f90   CB05_CSIRO_Integrator.f90 CB05_CSIRO_Model.f90
MAINOBJ = CB05_CSIRO_Main.o     CB05_CSIRO_Initialize.o     CB05_CSIRO_Integrator.o   CB05_CSIRO_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 CB05_CSIRO.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 CB05_CSIRO_stochastic.exe

mex:    $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O CB05_CSIRO_mex_Fun.f90     $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O CB05_CSIRO_mex_Jac_SP.f90  $(ALLOBJ)
	$(MEX) FC#$(FC) -fortran -O CB05_CSIRO_mex_Hessian.f90 $(ALLOBJ)

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

distclean:
	rm -f CB05_CSIRO*.o CB05_CSIRO*.mod \
	CB05_CSIRO*.dat CB05_CSIRO.exe CB05_CSIRO.map \
	CB05_CSIRO*.f90 CB05_CSIRO_*.mexglx

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

CB05_CSIRO_Parameters.o: CB05_CSIRO_Parameters.f90 \
	            CB05_CSIRO_Precision.o
	$(FC) $(FOPT) -c $<

CB05_CSIRO_Monitor.o: CB05_CSIRO_Monitor.f90 \
	             CB05_CSIRO_Precision.o
	$(FC) $(FOPT) -c $<

CB05_CSIRO_Global.o: CB05_CSIRO_Global.f90 \
	            CB05_CSIRO_Parameters.o CB05_CSIRO_Precision.o
	$(FC) $(FOPT) -c $<

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

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

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

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

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

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

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

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

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

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

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

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

CB05_CSIRO_Main.o: CB05_CSIRO_Main.f90  $(ALLOBJ) CB05_CSIRO_Initialize.o CB05_CSIRO_Model.o CB05_CSIRO_Integrator.o
	$(FC) $(FOPT) -c $<

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

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