#!/bin/bash


# This makefile header is used by all of the mesa modules.

#################################################################




# READ THIS FIRST.

# If you are just getting started, you should use this file as is,
# except for possibly changing the compiler (step 1 below).

# You'll need either ifort (version 11.1 or later), or
# gfortran (I suggest getting the current version from
#           http://gcc.gnu.org/wiki/GFortranBinaries)
 

# If you discover that your system doesn't support isnan,
# then you'll also need to do step 2.

# The other steps are for "fine-tuning" and can be tried later.




#################################################################


# step 1) specify your compiler

# pick one of the following options:

# BTW: ifort should be your first choice,
# followd by gfortran is you don't have ifort.
# other compilers might work, but aren't supported.


# Intel
#FC = ifort
# NOTE: mesa needs version 11.1 or later of ifort.

# GNU
#FC = gfortran
# NOTE to gfortran users: make sure you have a recent version.
# I believe that gcc 4.3.0 or 4.4.0 or newer will work okay.
# The failure mode is an infinite loop during installation of mtx.
# So if your installation seems to be hung, try a new gfortran.


# if you need special flags for the compiler, define them here:
SPECIAL_FC_FLAGS = 




#################################################################


# step 2) specify whether isnan is supported or not

# pick one of the following options:

UTILS_ISNAN = utils_isnan_okay
#UTILS_ISNAN = utils_isnan_nope

# If you aren't sure, try setting UTILS_ISNAN = utils_isnan_okay
# If this works, great.  Otherwise, the compilation of the
# mesa/utils module will complain when it tries to invoke isnan.
# In case that happens, you can simply change this makefile_header
# to the UTILS_ISNAN = utils_isnan_nope setting and redo the
# ./install command for mesa.



#################################################################


# step 3) specify which BLAS and LAPACK libraries to use for mesa/mtx

# LAPACK is a set of "high-level" linear algebra routines,
# such as routines to solve the matrix equation A*x=b.
# The routines in LAPACK call the routines in BLAS.
# BLAS is the set of the "basic linear algebra routines".
# The performance of the matrix operations depends heavily
# on the quality of the BLAS implementation.
# If you are going to do a lot of matrix banging, you should
# get yourself an optimized BLAS. You can get the Goto BLAS here:
# http://www.tacc.utexas.edu/resources/software/

# there are 3 typical ways for setting up BLAS and LAPACK for mesa:
# 1) use an optimized library that has both LAPACK and BLAS.
# 2) use an optimized library for BLAS and compile LAPACK routines from sources.
# 3) compile both BLAS and LAPACK routines from sources.
# if you are using Mac OS X 10.4.8 or later, you have another option
# 4) use the Accelerate framework which has a multithreaded, tuned LAPACK/BLAS.

# users of the mesa mtx module will have $(LOAD_LAPACK) $(LOAD_BLAS)
# following -lmtx as part of the makefiles for creating their applications.
# by defining these appropriately, you can set things up as you want.


# For case 1), comment out the definitions for WHICH_LAPACK, WHICH_BLAS,
# and LOAD_BLAS.  Define LOAD_LAPACK to load your LAPACK/BLAS library.
# For example, I sometimes use Intel's Math Kernel Library for the Mac.
# To load it, I use the following lines (and comment out the definitions
# for WHICH_LAPACK, WHICH_BLAS, and LOAD_BLAS).

#MKLPATH = /Library/Frameworks/Intel_MKL.framework/Libraries/32
#MKLLIBS = -lmkl_intel -lmkl_intel_thread -lmkl_solver -lmkl_core -lguide -lpthread
#LOAD_LAPACK = -L$(MKLPATH) $(MKLLIBS)

# BTW: just a reminder regarding MKL -- if you are using dynamic lib's, then
# you need to add the path to the MKL/lib to the DYLD_LIBRARY_PATH environment variable.
# for example, my initialization script includes the following line (without the #)
# setenv DYLD_LIBRARY_PATH /Library/Frameworks/Intel_MKL.framework/Versions/Current/lib/32:$DYLD_LIBRARY_PATH


# For case 2), define WHICH_LAPACK = USE_SRCS, define LOAD_LAPACK = -lmesalapack
# and comment out WHICH_BLAS.  Define LOAD_BLAS to load your optimized BLAS library.
# For example, I have a version of the Goto BLAS that I've compiled
# for my machine.  To use it, I do the following:

#LOAD_BLAS = -L/Users/bpaxton/GotoBLAS -lgoto


# For case 3), define WHICH_LAPACK = USE_SRCS and LOAD_LAPACK = -lmesalapack.
# Similarly, define WHICH_BLAS = USE_SRCS and LOAD_BLAS = -lmesablas.
# Note that this won't compile all of either BLAS or LAPACK.  It just
# does the routines needed in mesa -- they can be found in mtx directories
# blas_src and lapack_src.


# For case 4), comment out the definitions for WHICH_LAPACK, WHICH_BLAS,
# and LOAD_BLAS.  Define LOAD_LAPACK = -framework accelerate
# for Mac
#WHICH_LAPACK = 
#LOAD_LAPACK = -framework accelerate
#WHICH_BLAS = 
#LOAD_BLAS = 


# this is the standard default
WHICH_LAPACK = USE_SRCS
LOAD_LAPACK = -lmesalapack
WHICH_BLAS = USE_SRCS
LOAD_BLAS = -lmesablas


#################################################################


# step 4) specify optional add-on sparse matrix solvers

# if you do want to use the Super_LU sparse matrix solver, 
# edit the following lines to give the info for your setup.

# if you're not going to be using Super_LU,
# just comment out the following lines.

#USE_SUPERLU = YES
#SUPERLU_DIR = /Users/bpaxton/Programming/SuperLU_3.0
#LOAD_SUPERLU = -L$(SUPERLU_DIR) -lsuperlu_3.0
#COMPILE_SUPERLU_C = gcc -O2 -DAdd_ -I$(SUPERLU_DIR)/SRC -c

# if you want to use a c compiler other than gcc,
# you'll need to edit the COMPILE_SUPERLU_C line too.


# what's this Super_LU stuff all about?  Here's the story.
# the solvers in mesa/num are given the matrix factor/solve
# routines as arguments, so you can use sparse matrix versions
# if you'd like.  Sparse solvers come in 2 flavors:
# iterative and direct.  Mesa/mtx includes an iterative
# solver (from the SPARSKIT package), and also has hooks
# for a direct solver (from the Super_LU package).

# for SPARSKIT iterative solvers, mesa/mtx includes both a set
# of routines and the wrappers, so you're all set if that's
# going to be okay for your requirements.
# http://www-users.cs.umn.edu/~saad/software/SPARSKIT/sparskit.html

# if you'd prefer to use a direct solver, mesa includes
# hooks for the Super_LU package.  you just need to install
# Super_LU on your machine; the necessary wrappers are
# included in mesa/mtx.  http://crd.lbl.gov/~xiaoye/SuperLU/

# if you'd like to try a sparse solver other than these two,
# you'll need to implement the wrapper "decsol" routines.
# Take a look at src/mod_superlu.f to see what's involved.

# BTW: the Watson Sparse Matrix Package is not thread-safe,
# (at least as of Version 8.1), so think twice before deciding
# to use it with mesa.

# Same goes for the HSL Archive code MA28 -- it is also not
# thread safe.  However, the new HSL packages are, and if you
# are in the UK, you can get them without charge.


#################################################################


# step 5) do you want to use PGPLOT for onscreen plotting with mesa/star?
# you'll need a pgplot built with the same compiler as used for mesa
# and you'll need X windows and png.
# set USE_PGSTAR = YES and set LOAD_PGPLOT to load the libraries.
# in mesa/star, set the control parameter pgplot_flag = .true.
# when you run star it will open a window using X and draw plots using PGSTAR
# the plots will be updated each time a log entry is made.

ifeq ($(FC),ifort)

USE_PGSTAR = NO
LOAD_PGPLOT = 
#USE_PGSTAR = YES
#LOAD_PGPLOT = -L/Users/bpaxton/pgplot/lib -lpgplot -L/usr/X11R6/lib -lX11 -lpng

else
ifeq ($(findstring gfortran, $(notdir $(FC))), gfortran)

USE_PGSTAR = NO
LOAD_PGPLOT = 
#USE_PGSTAR = YES
#LOAD_PGPLOT = -L/Users/bpaxton/pgplot/lib -lpgplot -L/usr/X11R6/lib -lX11 -lpng
#LOAD_PGPLOT = -L/usr/local/lib -lpgplot -L/usr/X11R6/lib -lX11 -lpng

else

USE_PGSTAR = NO
LOAD_PGPLOT = 

endif
endif


#################################################################



# programs that use the mesa/mtx lib should use $(LOAD_MATRIX)
# to get the proper libraries loaded in the right order.

# don't change this unless you are adding other sparse matrix packages.
LOAD_MATRIX = -lmtx $(LOAD_SUPERLU) $(LOAD_LAPACK) $(LOAD_BLAS)



#################################################################


# Unless you are adding a new compiler to the list of options,
# you shouldn't need to change any of the following stuff.
# It simply defines things for use by the module makefiles.


ifeq ($(findstring ifort, $(notdir $(FC))), ifort)

FCbasic = $(SPECIAL_FC_FLAGS) -vec-report0 -traceback -error-limit 6 \
   -Wl,-stack_size,0xee6b000 
# BTW: -Wl,-stack_size,0xee6b000 increases the stack size
FCimpno = -implicitnone 
FCchecks = -check uninit -check pointers -check bounds -check all
FCwarn = -warn all -warn nounused
FCwarn_unused = -warn unused
FCfixed = -fixed -132
FCfixed72 = -fixed
FCfree = -free
#FCopt = -O3
FCopt = -O2
FCdebug = -g
OPENMP_FCFLAGS ?= -openmp
FCopenmp = $(OPENMP_FCFLAGS) -threads
#FCopenmp = 
FC_fixed_preprocess = -fpp
FC_free_preprocess = -fpp
FCstatic =
FCstatic = -Bstatic -static-intel
# BTW: -Bstatic -static-intel makes it possible to use the executable stand-alone

else
ifeq ($(findstring gfortran, $(notdir $(FC))), gfortran)

FCbasic = -fno-range-check $(SPECIAL_FC_FLAGS)
FCimpno = -fimplicit-none  
FCchecks = -fbounds-check
FCwarn = -Wunused-value -Werror -W
FCfixed = $(FCFLAGS) -ffixed-form -ffixed-line-length-132
FCfixed72 = -ffixed-form
FCfree = -ffree-form
FCopt = -O2
FCdebug = -g
FCstatic =

OPENMP_FCFLAGS ?= -fopenmp
FCopenmp = $(OPENMP_FCFLAGS)
#FCopenmp = 
FC_fixed_preprocess = -x f77-cpp-input
FC_free_preprocess = -x f95-cpp-input


else

FCbasic = UNKNOWN COMPILER
FCchecks =
FCwarn = 
FCfixed =
FCfree =
FCopt = 
FCdebug = 
FCopenmp = $(OPENMP_FCFLAGS)

endif
endif

# some definitions used in the module makefiles
MODULE_DIR = ..
MOD_PUBLIC_DIR = $(MODULE_DIR)/public
MOD_PRIVATE_DIR = $(MODULE_DIR)/private
MODULE_INCLUDES = -I$(MOD_PUBLIC_DIR) -I$(MOD_PRIVATE_DIR)
OTHER_INCLUDES = -I$(MESA_DIR)/include
INCLUDES = $(MODULE_INCLUDES) $(OTHER_INCLUDES)

COMPILE_BASIC = $(MPIFC) $(FCbasic) $(FCopenmp) $(FCstatic) $(INCLUDES)

COMPILE_TO_DEPLOY = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCopt) -c
COMPILE_TO_TEST   = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCopt) $(FCdebug) -c
COMPILE_NO_OPENMP_NO_OPT = \
   $(MPIFC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_ASAP = \
   $(MPIFC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCdebug) -c -O

COMPILE_FAST = $(COMPILE_BASIC) -c
COMPILE_NO_CHECKS = $(COMPILE_BASIC) $(FCopt) -c
COMPILE_NO_OPT    = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_DEVEL     = $(COMPILE_NO_OPT)

# some definitions used in the test makefiles and client makefiles
TEST_DIR = ..
TEST_SRC_DIR = $(TEST_DIR)/src
PACKAGE_DIR = ../..
LOCAL_LIB_DIR = $(PACKAGE_DIR)/make
MESA_LIB_DIR = $(MESA_DIR)/lib
MESA_INCLUDE_DIR = $(MESA_DIR)/include
TEST_INCLUDES = -I$(LOCAL_LIB_DIR) -I$(PACKAGE_DIR)/public -I$(MESA_INCLUDE_DIR)
TEST_COMPILE = $(MPIFC) $(FCbasic) $(FCopenmp) $(TEST_INCLUDES) $(FCchecks) $(FCdebug) -c


LOAD_MESA_NUMERICS = -lnum -linterp_2d -linterp_1d -lutils -lalert -lconst $(LOAD_MATRIX)

# micro uses numerics
LOAD_MESA_MICRO = -leos -lkap -ljina -lweak -lnet -lscreen -lrates -lneu -lchem $(LOAD_MESA_NUMERICS)

# macro uses micro
LOAD_MESA_MACRO = \
   -ldiffusion -lionization -lmlt -latm -lkaro -lcolors $(LOAD_MESA_MICRO)

# star_support uses macro (for now, LOAD_MESA_STAR_SUPPORT just = LOAD_MESA_MACRO)
LOAD_MESA_STAR_SUPPORT = $(LOAD_MESA_MACRO)

# star uses star_support
LOAD_STAR_MODS = -lstar $(LOAD_MESA_STAR_SUPPORT)

# this does the -L for the mesa lib dir
LOAD_MESA_STAR = -L$(MESA_LIB_DIR) $(LOAD_STAR_MODS) $(LOAD_PGPLOT)

# LIB_TOOL is the program that creates libraries
LIB_TOOL = ar crs

# define the PGSTAR files
ifeq ($(USE_PGSTAR),YES)
PGSTAR_OBJS = pgstar_support.o pgstar_trho_profile.o \
   pgstar_conv.o pgstar_hr.o pgstar_trho.o \
   pgstar_summary.o pgstar.o
else
PGSTAR_OBJS = pgstar_stub.o
endif

# define the STAR work files
STAR_WORK_OBJS = \
   run_star_support.o $(PGSTAR_OBJS) run_star_extras.o \
   calibrate.o isochrone.o \
   create_zams.o sample_zams.o \
   run_star.o 

