# Makefile for compilation of the MEDUSA JEASIM application

.KEEP_STATE:

.SUFFIXES:

.SUFFIXES: .F .o

# First define some useful things like
RM = /bin/rm -f
MV = /bin/mv -f
CP = /bin/cp
LNSYM = /bin/ln -s
MKDIR = /bin/mkdir -p

SRCDIR_MED   := ../../src-med
XMLLIST_MCG  := $(abspath list_jeasim.xml)
INCDIR_LOC   := $(abspath include)
PWDIR_LOC    := $(abspath .)


###########################################
### Guita: Debian Squeeze with gfortran ###
###########################################
FC = gfortran
FPP = cpp -E
FFLAGS = -g -O0 -fmax-errors=5
INCLFLAGS = -I./modmedusa -I.


LDFLAG_MEDUSA = -L. -lmedusa
LDFLAG_THDYCT = -L../../../libthdyct -lthdyct
LDFLAG_NETCDF = -L/usr/lib -lnetcdf -lnetcdff

LDFLAGS = $(LDFLAG_MEDUSA)  $(LDFLAG_NETCDF)  $(LDFLAG_THDYCT)


# Special options:
# -DNCFILES_SAVEALLSTEPS
# -DPROFIW_NEGLECT_REACTION
# -DSOLVSED_NO_SVC_EQUATION

OPTIONDEFS =  -DCALENDAR_365DAYS


########################################################################
# OPTIONDEFS possible definitions (non-exhaustive list)                #
########################################################################
#
#
# Enable the debugging output, according to the finer options in debug.h
#  
#
#
# Enable MPI (default: no MPI -- see mod_execontrol_medusa.F for more
# information)
#  -DALLOW_MPI
#
#
# Select the calendar (default: -DCALENDAR_365DAYS)
#  -DCALENDAR_360DAYS    1 yr = 360 d,    1 d = 24 h, 1 h = 3600 s
#  -DCALENDAR_365DAYS    1 yr = 365 d,    1 d = 24 h, 1 h = 3600 s
#  -DCALENDAR_365P25DAYS 1 yr = 365.25 d, 1 d = 24 h, 1 h = 3600 s
#
#
# Select interface extensions for host model grids organised
# along more than 1 dimension (default: no extensions)
#  -DMEDUSA_BASE2D     host model grid is mapped onto a 2D map
#                      (host model uses (:,:) arrays)
#  -DMEDUSA_BASE2DT2D  host model grid is mapped onto a 2D array
#                      of 2D tiles (host model uses (:,:,:,:) arrays)
#
#
#  -DGRID_CUSTOM          use custom grid (for full control)
#
#  -DBIODIFFUSION_CUSTOM  use custom profile, to be provided in a
#                         source file called "bdiffc_custom.F", to be
#                         copied or linked into src-med/gen/include
#
#  -DBIOIRRIGATION_CUSTOM use custom profile, to be provided in a
#                         source file called "birric_custom.F", to be
#                         copied or linked into src-med/gen/include
#
#
########################################################################


# then go ahead
SRCFILES = mod_jeasim_o2s.F store_nc_aux.F \
           mod_jeasim_medusa_setup.F mod_timecont_medusa_tsi.F \
           mod_files_medusa.F store_csv_summary.F medusa_jeasim.F

TIMESTAMP := $(shell date +%Y%m%d%H%M%S)


###############  You need not to change anything below  ###############

OBJFILES = $(SRCFILES:.F=.o)

volumeless:
	make codegen
	make medusa_jeasim_vl "OPTIONDEFS = $(OPTIONDEFS) -DSOLIDS_VOLUMELESS"


normal:
	make codegen
	make medusa_jeasim


medusa_jeasim medusa_jeasim_vl: libmedusa.a $(OBJFILES)
	$(FC) $(OPTIONDEFS) $(FFLAGS) $(INCLFLAGS) $(OBJFILES) \
	$(LDFLAGS) -o $@.$(TIMESTAMP)
	$(MV) $@.$(TIMESTAMP) ../../work/jeasim/$@.$(TIMESTAMP)


libmedusa.a:
	(cd -P $(SRCDIR_MED); \
	 make libmedusa.a "OPTIONDEFS = $(OPTIONDEFS)")
	$(MV) $(SRCDIR_MED)/libmedusa.a .
	$(MKDIR) modmedusa
	$(CP) $(SRCDIR_MED)/*.mod modmedusa


codegen: tidy
	(cd -P $(SRCDIR_MED); \
	 make codegen "XMLLIST_MCG = $(XMLLIST_MCG)" "OPTIONDEFS = $(OPTIONDEFS)"; \
	 $(CP) gen/template/solvsed_onestep_its_solut.F_template \
          gen/include/solvsed_onestep_its_solut.F; \
	 $(CP) gen/template/solvsedLossConversions.F_template \
          gen/include/solvsedLossConversions.F; \
	 $(RM) gen/mod_seafloor_wdata.F; \
	 $(LNSYM) $(PWDIR_LOC)/mod_seafloor_wdata.F gen/mod_seafloor_wdata.F)



.F.o:;
	$(FC) $(OPTIONDEFS) -c $(FFLAGS) $(INCLFLAGS) -o $@ $*.F


clean :
	$(RM) *.o *.mod


tidy : clean
	$(RM) libmedusa.a
	$(RM) -r modmedusa
	$(RM) *% *~ ~* *.log *.bak
