#---------------------------------------------------------------------#
#---------------------------------------------------------------------#
#                              Makefile                               #
#---------------------------------------------------------------------#
#                      Copyright (C) 2026 J. Lopez                    #
#---------------------------------------------------------------------#
#      Makefile uses to construct the VOFTools libraries              #
#      (libvoftools.a) and compile the test programs in C and         #
#      Fortran.                                                       #
#                                                                     #
#      Type:                                                          #
#                                                                     #
#      - 'make' to construct the VOFTools library and executables, or #
#      - 'make NS=nnn NV=nnn' to change the default array sizes       #
#        (140,200) used in the library.                               #
#                                                                     #
#      Before recompiling the VOFTools library, type:                 #
#                                                                     #
#      - 'make clean'                                                 #
#---------------------------------------------------------------------#
# This file is part of VOFTools.                                      #
#                                                                     #
# This program is free software: you can redistribute it and/or modify#
# it under the terms of the GNU General Public License as published by#
# the Free Software Foundation, either version 3 of the License, or   #
# (at your option) any later version.                                 #
#                                                                     #
# This program is distributed in the hope that it will be useful,     #
# but WITHOUT ANY WARRANTY; without even the implied warranty of      #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       #
# GNU General Public License for more details.                        #
#                                                                     #
# You should have received a copy of the GNU General Public License   #
# along with this program.  If not, see <http://www.gnu.org/licenses/>#
#---------------------------------------------------------------------# 
# Default parameters used for array allocation:
#---------------------------------------------
NS ?= 140 
NV ?= 200
#Change your compiler if necessary.
#----------------------------------
CC=gcc
F77=gfortran
LIBS=-lm -lgfortran

OPT1=-O3 -o
OPT2=-O3 -frecursive -fstack-arrays -c
OPTC=-O3 -o

all: check_stack lib fortran_3d c_3d fortran_2d c_2d fortran_fit c_fit fortran_parab c_parab

# Maximum memory estimate on the stack.
#--------------------------------------
#STACK_USAGE=$(shell echo $$(($(NS) * $(NV) * $(WP_SIZE) * 52 / 1024 / 1024 )))
check_stack:
	@STACK_USAGE=$$(($(NS) * $(NV) * 60 / 1024 / 1024)); \
	if [ $$STACK_USAGE -gt 7 ]; then \
		echo "-------------------------------------------------------";\
		echo "WARNING: High stack usage detected ($$STACK_USAGE MB).";\
		echo "Variables 'NS' ($(NS)) or 'NV' ($(NV)) might be too large.";\
		echo "Ensure 'ulimit -s' or 'OMP_STACKSIZE' is set correctly to";\
		echo "avoid Segmentation Faults.";\
		echo "-------------------------------------------------------";\
	fi

lib: ./lib/libvoftools.a uservoftools.o

dim:
	@echo "      INTEGER, PARAMETER :: NS=$(NS),NV=$(NV)" > ./src/dimpol.h

mkdirlib:
	mkdir -p ./lib

copyc:
	cp -f ./src/cvoftools.h ./examples/c/
	cp -f ./src/cuservoftools.h ./examples/c/

copyfit:
	cp -f ./data/polygons-set.txt ./

./lib/libvoftools.a: voftools.o | mkdirlib
	ar rv ./lib/libvoftools.a voftools.o

fortran_3d: ./examples/fortran/test3d.f90 uservoftools.o ./lib/libvoftools.a
	$(F77) $(OPT1) test3d_f ./examples/fortran/test3d.f90 uservoftools.o ./lib/libvoftools.a

c_3d: copyc ./examples/c/test3d.c uservoftools.o ./lib/libvoftools.a
	$(CC) $(OPTC) test3d_c ./examples/c/test3d.c uservoftools.o ./lib/libvoftools.a $(LIBS)

fortran_2d: ./examples/fortran/test2d.f90 uservoftools.o ./lib/libvoftools.a
	$(F77) $(OPT1) test2d_f ./examples/fortran/test2d.f90 uservoftools.o ./lib/libvoftools.a

c_2d: copyc ./examples/c/test2d.c uservoftools.o ./lib/libvoftools.a
	$(CC) $(OPTC) test2d_c ./examples/c/test2d.c uservoftools.o ./lib/libvoftools.a $(LIBS)

fortran_fit: copyfit ./examples/fortran/testfit.f90 ./lib/libvoftools.a
	$(F77) $(OPT1) testfit_f ./examples/fortran/testfit.f90 ./lib/libvoftools.a

c_fit: copyc copyfit ./examples/c/testfit.c ./lib/libvoftools.a
	$(CC) $(OPTC) testfit_c ./examples/c/testfit.c ./lib/libvoftools.a $(LIBS)

fortran_parab: ./examples/fortran/testparab.f90 ./lib/libvoftools.a
	$(F77) $(OPT1) testparab_f ./examples/fortran/testparab.f90 ./lib/libvoftools.a

c_parab: copyc ./examples/c/testparab.c ./lib/libvoftools.a
	$(CC) $(OPTC) testparab_c ./examples/c/testparab.c ./lib/libvoftools.a $(LIBS)

voftools.o: dim ./src/voftools.f90
	$(F77) $(OPT2) ./src/voftools.f90

uservoftools.o: ./src/uservoftools.f90
	$(F77) $(OPT2) ./src/uservoftools.f90

.PHONY: all lib dim mkdirlib copyc copyfit check_stack clean \
        fortran_3d c_3d fortran_2d c_2d \
        fortran_fit c_fit fortran_parab c_parab

clean:
	rm -f voftools.o uservoftools.o
	rm -f voftools_mod.mod uservoftools_mod.mod
	rm -f ./lib/libvoftools.a
	rm -f test2d_f test2d_c test3d_f test3d_c
	rm -f testfit_f testfit_c testparab_f testparab_c

