# Run this script by typing: source grizzly-intel-cmake
# After this script completes, type: make -j 8
# If rebuilding, type 'make clean' before running 'make -j 8'

# This cmake configuration script is set up to perform a parallel build with Trilinos

#setenv("_MODULES_FC","intel/18.0.2")
#setenv("FC","/usr/projects/hpcsoft/toss3/common/intel-clusterstudio/2018.2.046/compilers_and_libraries_2018/linux/bin/intel64/ifort")
module purge
module use /usr/projects/climate/SHARED_CLIMATE/modulefiles/all
module load intel/17.0.1
module load mkl/2018.0.2
module load netcdf/4.4.1
module load cmake/3.9.0
module load python/2.7-anaconda-4.1.1
module load netcdf/4.4.1
module load openmpi

# remove old build data:

rm ./CMakeCache.txt
rm -r ./CMakeFiles

echo
echo "Doing CMake Configuration step"

# Note: the compilation flags were taken from the defaults for a CESM build on
# yellowstone-intel (using Machines_140218). Some of these options (e.g.,
# -convert big_endian and -assume byterecl) are probably unnecessary for a
# standalone build, but I am keeping things consistent with the CESM build for
# simplicity.

# A few non-intuitive things:
#
# - CISM_FORCE_FORTRAN_LINKER: without this, cmake tries to use a C++ linker, which doesn't work
#
# - CISM_INCLUDE_IMPLICIT_LINK_LIBRARIES: if this is on (the default), some
#   libraries are included on the link line which can't be found (e.g.,
#   hdf5). This may be related to the fact that trilinos on yellowstone is old,
#   and/or the fact that cmake wants to use a C++ linker but we're telling it to
#   use a fortran linker.

cmake \
  -D CISM_BUILD_CISM_DRIVER:BOOL=ON \
  -D CISM_ENABLE_BISICLES=OFF \
  -D CISM_ENABLE_FELIX=OFF \
\
  -D CISM_USE_TRILINOS:BOOL=OFF \
  -D CISM_MPI_MODE:BOOL=ON \
  -D CISM_SERIAL_MODE:BOOL=OFF \
\
  -D CISM_USE_GPTL_INSTRUMENTATION:BOOL=OFF \
  -D CISM_COUPLED:BOOL=OFF \
  -D CISM_USE_CISM_FRONT_END:BOOL=OFF \
\
  -D CISM_NETCDF_DIR=$NETCDF \
  -D CMAKE_VERBOSE_MAKEFILE:BOOL=OFF \
  -D CISM_EXTRA_LIBS="-lblas" \
\
  -D CMAKE_CXX_COMPILER=mpiicpc \
  -D CMAKE_C_COMPILER=mpicc \
  -D CMAKE_Fortran_COMPILER=mpif90 \
\
  -D CMAKE_Fortran_FLAGS:STRING="-fp-model source -convert big_endian -assume byterecl -ftz -traceback -assume realloc_lhs -xHost -O2" \
  -D CMAKE_C_FLAGS:STRING="-O2 -fp-model precise -xHost" \
  -D CMAKE_CXX_FLAGS:STRING="-O2 -fp-model precise -xHost" \
 ../..

# Note: last argument above  "../.."  is path to top level cism directory
