#-----------------------------------------------------------------------
# Makefile for SOFT_JGR
# Fortran 90 programs only
# P. Robert, LPP, 2021
#-----------------------------------------------------------------------


# ====================
# Choice of compiler
# ====================

FC = gfortran

#Gfortran creates a file name.mod for each module name.
#This confuses the gnu make, which thinks that .mod files are Modula2 source code files.
#You must override this built-in rule by adding the following line somewhere in your Makefile.

%.o : %.mod


# ======================
# optimize or debug mode
# ======================

#MODE=O
 MODE=D

# ================================
# Choice of compilation options
# ================================


# gfortran linux and Windows/MinGW compiler

ifeq ($(FC),gfortran)
	ifeq ($(MODE),D)
	FCOMP = -c -O -g -Wall -fcheck=all -fbacktrace -ffpe-trap=zero,underflow,overflow,invalid -fbounds-check -ftrapv -finit-local-zero -Jmod
	FLINK =	   -O -g -Wall -fcheck=all -fbacktrace -ffpe-trap=zero,underflow,overflow,invalid -fbounds-check -ftrapv -finit-local-zero -Jmod
	else
	FCOMP = -c -O2 -static -Wall -Jmod	-ffpe-summary=none	-finit-local-zero
	FLINK =	   -O2 -static -Wall -Jmod	-ffpe-summary=none	-finit-local-zero
	endif
endif
#-O -Wall -fcheck=all -g -fbacktrace

# ==========================
# Directories definition
# ==========================

SRC_DIR = src/
OBJ_DIR = obj/
BIN_DIR = bin/
MOD_DIR = mod/

# =====================
# List of executables
# =====================


EXEC =	\
	$(BIN_DIR)read_curl_div.exe \
	$(BIN_DIR)curl_div_gse_to_gsm.exe \
	$(BIN_DIR)crea_grid_B.exe \
	$(BIN_DIR)plot_3D_grid.exe \
    	$(BIN_DIR)comp_field_lines_XZ.exe \
    	$(BIN_DIR)comp_field_lines_XY.exe \
    	$(BIN_DIR)plot_field_lines_XZ.exe \
    	$(BIN_DIR)plot_field_lines_XY.exe \
	$(BIN_DIR)c_curl_from_B3D_grid.exe

# ========
# make all
# ========

# Create all exe files in bin directory.
# The library read_3D_grid.o is created first because she use modules used by the most part of all programs.

all : $(OBJ_DIR)read_3D_grid.o \
      $(EXEC)
      
	@echo mv *.mod $(MOD_DIR)
	@echo	" "
	@echo "-> compilation done."
	@echo "-> Read	lines above to check success."



# ================================================== ==
# Definition of dependencies for the creation of exe
# ================================================== ==

# The objects are taken from $ (OBJ_DIR),
# The executables are put in $ (BIN_DIR)
#
# We define here the name of each target (the .exe) and the list
# of its dependencies.
# Then, we run the compiler to link all the dependencies
# (the $ ^) and build the target (the $ @ which is the .exe).

$(BIN_DIR)read_curl_div.exe        : $(OBJ_DIR)read_curl_div.o 
	$(FC) $(FLINK) $^ -o $@
    
$(BIN_DIR)curl_div_gse_to_gsm.exe  : $(OBJ_DIR)curl_div_gse_to_gsm.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)crea_grid_B.exe          : $(OBJ_DIR)crea_grid_B.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)read_Kp_data.o \
                                     $(OBJ_DIR)geopack_2008_upd_2024.o \
                                     $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)c_curl_from_B3D_grid.exe : $(OBJ_DIR)c_curl_from_B3D_grid.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)lib_tools_CLUSTER.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)plot_3D_grid.exe         : $(OBJ_DIR)plot_3D_grid.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)rogralib_V10p0.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)mag_bow.o
	$(FC) $(FLINK) $^ -o $@

$(BIN_DIR)comp_field_lines_XZ.exe  : $(OBJ_DIR)comp_field_lines_XZ.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)trace_PR.o \
				     $(OBJ_DIR)read_3D_grid.o 
	$(FC) $(FLINK) $^ -o $@				     

$(BIN_DIR)comp_field_lines_XY.exe  : $(OBJ_DIR)comp_field_lines_XY.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)trace_PR.o \
				     $(OBJ_DIR)read_3D_grid.o 
	$(FC) $(FLINK) $^ -o $@				     
				     				     
$(BIN_DIR)plot_field_lines_XZ.exe  : $(OBJ_DIR)plot_field_lines_XZ.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)rogralib_V10p0.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)trace_PR.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)mag_bow.o
	$(FC) $(FLINK) $^ -o $@	
    
$(BIN_DIR)plot_field_lines_XY.exe  : $(OBJ_DIR)plot_field_lines_XY.o \
                                     $(OBJ_DIR)rocotlib_V3p3.o \
                                     $(OBJ_DIR)rogralib_V10p0.o \
                                     $(OBJ_DIR)lib_time.o \
                                     $(OBJ_DIR)trace_PR.o \
                                     $(OBJ_DIR)read_3D_grid.o \
                                     $(OBJ_DIR)mag_bow.o
	$(FC) $(FLINK) $^ -o $@


# ==========================
# Library compilation
# ==========================
 
# Here we define the name of each target (the .o of each library)
# and its source (the .f90)
#
# Then we run the compiler to compile the source
# (which is the first $ <)
# and make the target ("@" = the .o object)
# Warning: the libraries which are not used by the .exe
# will not be compiled.

$(OBJ_DIR)read_3D_grid.o	  : $(SRC_DIR)read_3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@
      
$(OBJ_DIR)mag_bow.o	          : $(SRC_DIR)mag_bow.f90
	  $(FC) $(FCOMP) $< -o $@
      
$(OBJ_DIR)rocotlib_V3p3.o	  : $(SRC_DIR)rocotlib_V3p3.f
	  $(FC) $(FCOMP) $< -o $@
	  
$(OBJ_DIR)rogralib_V10p0.o	  : $(SRC_DIR)rogralib_V10p0.f
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)lib_time.o x   	  : $(SRC_DIR)lib_time.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)lib_tools_CLUSTER.o     : $(SRC_DIR)lib_tools_CLUSTER.f90
	  $(FC) $(FCOMP) $< -o $@  

$(OBJ_DIR)geopack_2008_upd_2024.o : $(SRC_DIR)geopack_2008_upd_2024.f
	  $(FC) $(FCOMP) $< -o $@  

$(OBJ_DIR)read_Kp_data.o	  : $(SRC_DIR)read_Kp_data.f90
	  $(FC) $(FCOMP) $< -o $@	
	  
$(OBJ_DIR)trace_PR.o		  : $(SRC_DIR)trace_PR.f90
	  $(FC) $(FCOMP) $< -o $@	

# ==========================
# Programmes compilation
# ==========================
	  
$(OBJ_DIR)read_curl_div.o	    : $(SRC_DIR)read_curl_div.f90
	  $(FC) $(FCOMP) $< -o $@

$(OBJ_DIR)curl_div_gse_to_gsm.o	    : $(SRC_DIR)curl_div_gse_to_gsm.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)crea_grid_B.o		    : $(SRC_DIR)crea_grid_B.f90
	  $(FC) $(FCOMP) $< -o $@	    

$(OBJ_DIR)c_curl_from_B3D_grid.o    : $(SRC_DIR)c_curl_from_B3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@	  

$(OBJ_DIR)plot_3D_grid.o	    : $(SRC_DIR)plot_3D_grid.f90
	  $(FC) $(FCOMP) $< -o $@	    
		  
$(OBJ_DIR)comp_field_lines_XZ.o	    : $(SRC_DIR)comp_field_lines_XZ.f90
	  $(FC) $(FCOMP) $< -o $@	
   
$(OBJ_DIR)comp_field_lines_XY.o	    : $(SRC_DIR)comp_field_lines_XY.f90
	  $(FC) $(FCOMP) $< -o $@	
		  
$(OBJ_DIR)plot_field_lines_XZ.o	    : $(SRC_DIR)plot_field_lines_XZ.f90
	  $(FC) $(FCOMP) $< -o $@	
   
$(OBJ_DIR)plot_field_lines_XY.o	    : $(SRC_DIR)plot_field_lines_XY.f90
	  $(FC) $(FCOMP) $< -o $@		    

# ==========
# make clean
# ==========

# Delete all .exe , .o and .mod
# Delete all.tmp,	*.core, *.~

clean :
	@echo "***	Removing *.exe *.o, .mod files..."

	@find ./bin -name "*.exe"	-exec rm -f {} \;
	@find ./obj -name "*.o"		-exec rm -f {} \;
	@find ./mod -name "*.mod"	-exec rm -f {} \;
	
	@rm -f *.tmp *core *~ 
	
	@echo "clean done."

#-----------------------------------------------------------------------
# End of Makefile for all SOFT_JGR
#-----------------------------------------------------------------------
