#!/bin/bash
#
#
# derived from linux_amd64_ifort, tested on ollie.awi.de (CRAY CS400)
#
# requires these modules
# module purge (to remove you have anything that may conflict with ifort)
# module load intel.compiler
# module load intel.mpi
# module load netcdf/4.4.0_intel

# - For more speed, provided your data size doesn't exceed 2GB you can
# remove -fPIC which carries a performance penalty of 2-6%.
# - Provided that the libraries you link to are compiled with -fPIC this
# optfile should work.
# - You can replace -fPIC with -mcmodel=medium which may perform faster
# than -fPIC and still support data sizes over 2GB per process but all
# the libraries you link to must be compiled with -fPIC or -mcmodel=medium
#
# MPI : DON'T FORGET to set environment variable MPI_INC_DIR to the include
#       directory of your MPI implementation

if test "x$MPI" = xtrue ; then
  CC=mpiicc
  FC=mpiifort
  F90C=mpiifort
#  LINK="$F90C -ipo"
else
  CC=icc
  FC=ifort
  F90C=ifort
#  LINK="$F90C -ipo"
fi

DEFINES='-DWORDLENGTH=4'
CPP='cpp -traditional -P'
F90FIXEDFORMAT='-fixed -Tf'
EXTENDED_SRC_FLAG='-132'
GET_FC_VERSION="--version"
OMPFLAG='-qopenmp'

NOOPTFLAGS='-O0'
NOOPTFILES=''

FFLAGS="$FFLAGS -W0 -WB -convert big_endian -assume byterecl"
FFLAGS="$FFLAGS -fPIC"
#- might want to use '-r8' for fizhi pkg:
#FFLAGS="$FFLAGS -r8"

if test "x$IEEE" = x ; then     #- with optimisation:
#    FOPTIM='-O2 -align -xHost -ipo'
# fast does not work because there are no static netcdf libaries
#    FOPTIM='-fast'
# so we spell it out (-ipo makes linking very slow, so we will skip that, too)
#    FOPTIM='-ipo -align -O3 -no-prec-div -fp-model fast=2 -xHost'
    FOPTIM='-align -O3 -no-prec-div -fp-model fast=2 -xHost'
# reduce optimization level a little for some files
    NOOPTFLAGS='-align -O1 -no-prec-div -fp-model fast=2 -xHost'
    NOOPTFILES='obcs_init_fixed.F'
else
  if test "x$DEVEL" = x ; then  #- no optimisation + IEEE :
    # "-fp-model [keyword]" is the recommended method of controlling precision
    FOPTIM='-O0 -noalign -xHost -ip -fp-model consistent'
# alternatives, see man pages for details
#   FOPTIM='-O0 -noalign -xHost -ip -mp1'
#   FOPTIM='-O0 -noalign -xHost -ip -fp-model precise'
#   FOPTIM='-O0 -noalign -xHost -ip -mieee-fp'
  else                          #- development/check options:
#    FFLAGS="$FFLAGS -warn all -warn nounused"
    FFLAGS="$FFLAGS -warn unused"
    FOPTIM="-fpe0 -check all -ftrapuv -fp-model precise"
    FOPTIM="$FOPTIM -O0 -noalign -g -traceback"
  fi
fi

F90FLAGS=$FFLAGS
F90OPTIM=$FOPTIM
CFLAGS='-O0 -fPIC -ip'

INCLUDEDIRS=''
INCLUDES=''
LIBS=''

# the extra -Wl,-rpath,${NETCDF_LIB} makes sure that the path to the
# dynamics libraries is hardwired in the executable and does not depend
# on which module you have loaded
if [ "x$NETCDF_ROOT" != x ] ; then
    INCLUDEDIRS="${NETCDF_ROOT}/include"
    INCLUDES="-I${NETCDF_ROOT}/include"
    LIBS="-L${NETCDF_ROOT}/lib -Wl,-rpath,${NETCDF_ROOT}/lib"
elif [ "x$NETCDF_HOME" != x ]; then
    INCLUDEDIRS="${NETCDF_HOME}/include"
    INCLUDES="-I${NETCDF_HOME}/include"
    LIBS="-L${NETCDF_HOME}/lib -Wl,-rpath,${NETCDF_HOME}/lib"
elif [ "x$NETCDF_INC" != x -a "x$NETCDF_LIB" != x ]; then
    NETCDF_INC=`echo $NETCDF_INC | sed 's/-I//g'`
    NETCDF_LIB=`echo $NETCDF_LIB | sed 's/-L//g'`
    INCLUDEDIRS="${NETCDF_INC}"
    INCLUDES="-I${NETCDF_INC}"
    LIBS="-L${NETCDF_LIB} -Wl,-rpath,${NETCDF_LIB}"
fi

if [ -n "$MPI_INC_DIR" -a "x$MPI" = xtrue ] ; then
    INCLUDES="$INCLUDES -I$MPI_INC_DIR"
    INCLUDEDIRS="$INCLUDEDIRS $MPI_INC_DIR"
    #- used for parallel (MPI) DIVA
    MPIINCLUDEDIR="$MPI_INC_DIR"
   #MPI_HEADER_FILES='mpif.h mpiof.h'
fi
