#!/bin/bash
# --------------------------------------------------------------------------- #
# matrix.go: Run matrix of regression tests on target machine.                #
#                                                                             #
# Remarks:                                                                    #
# - This version is set up for automatic w3_setenv script and for the         #
#   IFREMER HPC 'datarmor'. When using this for your own setup and            #
#   computer, please copy rather than modify.                                 #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      August 2013            #
#                                                      December 2013          #
#                                                      April 2018             #
#                                                                             #
#    Copyright 2013 National Weather Service (NWS),                           #
#       National Oceanic and Atmospheric Administration.  All rights          #
#       reserved.  WAVEWATCH III is a trademark of the NWS.                   #
#       No unauthorized use without permission.                               #
#                                                                             #
# --------------------------------------------------------------------------- #
# 0. Environment file

  source $(dirname $0)/../../model/bin/w3_setenv
  main_dir=$WWATCH3_DIR
  temp_dir=$WWATCH3_TMP
  source=$WWATCH3_SOURCE
  list=$WWATCH3_LIST

  echo "Main directory    : $main_dir"
  echo "Scratch directory : $temp_dir"
  echo "Save source codes : $source"
  echo "Save listings     : $list"


# 1. Set up
# 1.a Computer/ user dependent set up

  echo '#!/bin/sh'                                           > matrix.head
  echo ' '                                                  >> matrix.head
  echo '#PBS -q mpi_2'                                      >> matrix.head
  echo '#PBS -l mem=20G'                                    >> matrix.head
  echo '#PBS -l walltime=48:00:00'                          >> matrix.head
  echo '#PBS -N ww3_regtest'                                >> matrix.head
  echo '#PBS -j oe'                                         >> matrix.head
  echo '#PBS -o matrix.out'                                 >> matrix.head
  echo ' '                                                  >> matrix.head

  echo "  cd $(dirname $main_dir)/regtests"                 >> matrix.head
  echo ' '                                                  >> matrix.head

# Netcdf modules

  comp="mpt"
  echo '  source /etc/profile.d/modules.sh'                 >> matrix.head
  echo '  module purge'                                     >> matrix.head
  if [ $comp = "mpi" ]
  then
    echo '  module load NETCDF/4.3.3.1-impi5-intel2016'     >> matrix.head
  elif [ $comp = "mpt" ]
  then
    #echo '  module load NETCDF/4.3.3.1-mpt-intel2016'       >> matrix.head
    echo '  export PATH=/home/datawork-wave/NETCDF/MPT/bin::${PATH}' >> matrix.head
    echo '  source /usr/share/Modules/3.2.10/init/sh'       >> matrix.head 
    echo '  module purge'                                   >> matrix.head
    echo '  module load intel-comp/18'                      >> matrix.head
    echo '  module load mpt/2.15'                           >> matrix.head
    echo '  module list'                                    >> matrix.head
    echo '  export NETCDF_CONFIG=/home/datawork-wave/NETCDF/MPT/bin/nf-config'  >> matrix.head
    echo '  export WWATCH3_NETCDF=NC4'                      >> matrix.head
    echo '  export MPI_LAUNCH=mpiexec_mpt'                  >> matrix.head
    echo "  export WW3_PARCOMPN=4"                          >> matrix.head

  elif [ $comp = "gfortran" ]
  then
    echo '  module load NetCDF/4.4.1.1__gcc-6.3.0__nop'     >> matrix.head  
  fi

# PARMETIS 
  echo "  export METIS_PATH=/appli/parmetis/4.0.3-intel-cc-17.0.2.174"            >> matrix.head
  echo "  export WW3_PARCOMPN=4"                            >> matrix.head
  echo ' '

# Compiler option. Choose appropriate compiler and set cmplOption to
# y if using for the first time or using a different compiler

  cmplr=datarmor_mpt_debug
  export cmplOption='y'

  export  mpi='$MPI_LAUNCH'
  export   np='56'
  export   nr='28'
  export  nth='2'

  if [ "$cmplOption" = 'y' ]
  then
     export rtst="./bin/run_test -o both -N -f -c $cmplr -S"
  else
     export rtst="./bin/run_test -o both -N -f -S"
  fi

  export  ww3='../model'

# 1.b Flags to do course selection - - - - - - - - - - - - - - - - - - - - - -
#     Addition selection by commenting out lines as below

  export     shrd='y' # Do shared architecture tests
  export     dist='y' # Do distributed architecture (MPI) tests
  export      omp='y' # Threaded (OpenMP) tests
  export     hybd='n' # Hybrid options

  export   prop1D='y' # 1-D propagation tests (ww3_tp1.X)
  export   prop2D='y' # 2-D propagation tests (ww3_tp2.X)
  export     time='y' # time linmited growth
  export    fetch='y' # fetch linmited growth
  export   hur1mg='y' # Hurricane with one moving grid
  export    shwtr='y' # shallow water tests
  export    unstr='y' # unstructured grid tests
  export    pdlib='y' # unstructured grid tests with pdlib for domain decomposition and implicit solver 
  export    smcgr='y' # SMC/Rotated grid test
  export   mudice='y' # Mud/Ice and wave interaction tests 
  export   infgrv='y' # Second harmonic generation tests
  export     uost='y' # ww3_ts4 Unresolved Obstacles Source Term (UOST)
  export    assim='y' # Restart spectra update
  export  multi01='y' # mww3_test_01 (wetting and drying)
  export  multi02='y' # mww3_test_02 (basic two-way nesting test))
  export  multi03='y' # mww3_test_03 (three high and three low res grids).
  export  multi04='y' # mww3_test_04 (swell on sea mount and/or current)
  export  multi05='y' # mww3_test_05 (three-grid moving hurricane)
  export  multi06='y' # mww3_test_06 (curvilinear grid tests)
  export  multi07='y' # mww3_test_07 (unstructured grid tests)
  export  multi08='y' # mww3_test_08 (wind and ice tests)
# export   filter='PR3 ST2 UQ'
                      # The filter does a set of consecutinve greps on the 
                      # command lines generated by filter.base with the above
                      # selected options.

# --------------------------------------------------------------------------- #
# 2.  Execute matrix.base ...                                                 #
# --------------------------------------------------------------------------- #
             
  $main_dir/../regtests/bin/matrix.base

# --------------------------------------------------------------------------- #
# End to the matrix                                                           #
# --------------------------------------------------------------------------- #
