#! /bin/csh -f

# -------------------------------------------------------------------------
# Yellowstone build specific settings
# -------------------------------------------------------------------------

set CESM_REPO = `./xmlquery CCSM_REPOTAG -value`
if($status == 0) then
  set COMPILER            = `./xmlquery  COMPILER		   -value`
  set MPILIB              = `./xmlquery  MPILIB		   -value`
  set DEBUG               = `./xmlquery  DEBUG		   -value`
  set OS                  = `./xmlquery  OS		   -value`
  set PROFILE_PAPI_ENABLE = `./xmlquery  PROFILE_PAPI_ENABLE -value`
else
  echo $0 using settings from environment:
endif
echo "COMPILER=$COMPILER"
echo "MPILIB=$MPILIB"
echo "DEBUG=$DEBUG"
echo "OS=$OS"
echo "PROFILE_PAPI_ENABLE=$PROFILE_PAPI_ENABLE"

source /glade/apps/opt/lmod/lmod/init/csh

module purge
module load ncarenv/1.0
module load ncarbinlibs/1.0
module load perlmods
module load gmake/4.1

# load the python modules
module load python
module load all-python-libs

if ( $COMPILER == "intel") then
  module load intel/15.0.1
  module load mkl/11.1.2
# Currently trilinos is only supported with the INTEL compiler
  module load trilinos/11.10.2

# Currently netcdf-mpi is only supported for intel
  if ( $MPILIB == "mpi-serial") then
    module load netcdf/4.3.2
  else
# Currently netcdf-mpi is only supported for intel
    module load netcdf-mpi/4.3.3-rc3
    module load pnetcdf/1.6.0
  endif

# Currently esmf is only supported with the INTEL compiler
  module load esmf
  if ( $MPILIB == "mpi-serial") then
    if ( $DEBUG == "TRUE") then
      module load esmf-6.3.0rp1-defio-uni-g
    else
      module load esmf-6.3.0rp1-defio-uni-O
    endif
  else
    if ( $DEBUG == "TRUE") then
      module load esmf-6.3.0rp1-defio-mpi-g
    else
      module load esmf-6.3.0rp1-defio-mpi-O
    endif
  endif
endif
if ( $COMPILER == "pgi" ) then
  module load pgi/14.7
  module load netcdf/4.3.0
if ( $MPILIB != "mpi-serial") then
  module load pnetcdf/1.4.1
endif
endif
if ( $COMPILER == "gnu" ) then
  module load gnu/4.9.2
  module load netcdf/4.3.0
  set iotype = `./xmlquery PIO_TYPENAME `
  if( "$iotype" =~ {*pnetcdf*} ) then
       echo "Changing PIO_TYPENAME to netcdf for gnu compiler"
       ./xmlchange -noecho PIO_TYPENAME=netcdf
  endif
  #setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${NETCDF}/lib

if ( $MPILIB != "mpi-serial") then
  module load pnetcdf/1.4.1
endif

endif

module load ncarcompilers/1.0
module load cmake/2.8.10.2

# -------------------------------------------------------------------------
# Build and runtime environment variables - edit before the initial build 
# -------------------------------------------------------------------------
limit stacksize unlimited
limit datasize  unlimited

setenv OMP_STACKSIZE 256M
setenv MP_LABELIO yes

# MPI Environment
setenv MP_INFOLEVEL                   2
setenv MP_SHARED_MEMORY             yes
setenv MP_EUILIB                    us
setenv MP_MPILIB                    $MPILIB
setenv MP_STDOUTMODE                unordered
#
setenv MP_RC_USE_LMC                yes

# setting MP_EUILIBPATH is a temporary work-around for occasional mpi hangs 
# on yellowstone.  this should be removed when yellowstone is patched.
#setenv MP_EUILIBPATH                /glade/u/home/archerc/EFIX5

# Set a host file so that mpi can run on login node
if ( $HOSTNAME =~ {*login*} ) then
  echo $HOSTNAME > $CASEROOT/hostfile
  setenv MP_HOSTFILE $CASEROOT/hostfile
  setenv MP_PROCS 1
endif
if ( $DEBUG == "TRUE" ) then
  module load debug
  module load totalview
  setenv MP_EUIDEVELOP yes
  setenv MP_EAGER_LIMIT               0
else
  setenv MP_EUIDEVELOP min
endif
setenv MP_EAGER_LIMIT 0

#
#  We need to turn off system level job monitor if
#  we want to run with PAPI counters
#
if ( $PROFILE_PAPI_ENABLE == "TRUE" ) then
  setenv REQUEST_SUSPEND_HPC_STAT 1
endif
