#!/bin/bash
#
#################################################
# batch settings for INT2LM run on mistral at DKRZ
#################################################
#SBATCH --job-name=sp_int2lm
#SBATCH --partition=compute
#SBATCH --nodes=1
#SBATCH --ntasks=24
#SBATCH --time=00:05:00
#SBATCH --exclusive
#SBATCH --output=sp_int2lm.o%j
#SBATCH --error=sp_int2lm.o%j
#SBATCH --account=

# ulimit: the setting of the stack size instead of using "ulimit -s unlimited",
#         together with the --propagate=STACK in the "srun" command (see below),
#         speeds up the job considerably; in my test cases between 10% and 15%;
#         the value 102400 is the default maximum stack size on MISTRAL
#
ulimit -s 102400

#
export FORT_BUFFERED=true
export decfort_dump_flag=true
#
export MXM_LOG_LEVEL=ERROR

set -e

#### !!!! Adjust this directory !!!!
SPDIR=/home/dokukin/work/cosmo/cclm-sp_2.4

cd ${SPDIR}/step_by_step/gcm_to_cclm/log_int2lm

# clean the directory
rm -rf I* O* Y*

NPX=1 #4
NPY=1 #6
NPIO=0
NP1=`expr $NPX \* $NPY`
NP=`expr $NP1 + $NPIO`

#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
  ydate_ini='2000010100', hstart=0.0, hstop=744, hincbound=6,
  yinput_model='CM',   
  itype_aerosol = 2,
  itype_albedo=2,
  itype_calendar = 0,
  itype_rootdp = 4,
  itype_t_cl = 1, 
  itype_w_so_rel = 1,
  l_cressman = .FALSE., 
  lbdclim = .TRUE.,  
  lboundaries =.TRUE., 
  lfilter_oro=.FALSE.,
  lforest = .TRUE.,
  linitial=.TRUE.,
  lmulti_layer_in = .TRUE., 
  lmulti_layer_lm = .TRUE.,
  lprog_qi = .FALSE.,
  lreorder = .TRUE.,
  lsso = .TRUE.,
  ltime_mean = .TRUE., 
  luse_t_skin = .TRUE.,
  nincwait = 5, nmaxwait = 20, 
  nprocx = ${NPX}, nprocy = ${NPY},
/END
 &GRID_IN
  pcontrol_fi =30000., 
  ie_in_tot = 192, je_in_tot = 94, ke_in_tot = 28,
  startlat_in_tot = -88.542, startlon_in_tot = -180.,
  endlat_in_tot = 88.542, endlon_in_tot = 178.125,
  pollat_in = 90.0, pollon_in = 180.0,
  lushift_in=.FALSE.,.FALSE., lvshift_in=.FALSE.,.FALSE.,
  ke_soil_in=1,   
  czml_soil_in=0.05, 1.05,
 /END
 &LMGRID
  ivctype = 2,
  irefatm = 2,
  ielm_tot=101, jelm_tot=111, kelm_tot=40,
  pollat =  39.25, pollon = -162.0, 
  dlon=0.44, dlat=0.44, 
  startlat_tot  = -24.09, startlon_tot  = -25.13,
  ke_soil_lm=9,   
  czml_soil_lm = 0.005,0.025,0.07,0.16,0.34,0.7,1.42,2.86,5.74,11.5,
 /END
 &DATABASE
 /
 &DATA
  yinput_type='analysis',
  ytunit_in='d',
  ytunit_out='d',
  ylmext_form_read='ncdf',
  yinext_form_read='ncdf',
  yin_form_read='ncdf',
  ylm_form_write='ncdf',
  ie_ext=105, je_ext=115,
  ylmext_lfn='europe_0440.nc',
  ylmext_cat='${SPDIR}/data/ext/',
  yinext_lfn='caf2000010100.nc',
  yinext_cat='${SPDIR}/data/gcm/ncep/2000_01',
  yin_cat='${SPDIR}/data/gcm/ncep/2000_01',
  ylm_cat='${SPDIR}/step_by_step/gcm_to_cclm/data/int2lm_output/',
  nprocess_ini = 131, nprocess_bd = 132, 
 /END
 &PRICTR
  igp_tot = 36,
  jgp_tot = 30,
  lchkin=.TRUE., lchkout=.TRUE.,
 /END
**


#################################################
# run the program
#################################################

INSTALLDIR=/dataspace1/forallusers/dokukin/install
MPI_DIR=/usr/lib/mvapich2-intel

export CPPFLAGS="-I${INSTALLDIR}/include -I${MPI_DIR}/include"
export LDFLAGS="-L${INSTALLDIR}/lib -L${MPI_DIR}/lib"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${INSTALLDIR}/lib:${MPI_DIR}/lib

# you have to adapt the way how to invoke the binary
echo -----start_INT2CLM
#srun -l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic  ${SPDIR}/src/int2lm/bin/int2lm.exe
mpirun -np 1 ${SPDIR}/src/int2lm/bin/int2lm.exe -I${INSTALLDIR}/include -I${MPI_DIR}/include \
-L${INSTALLDIR}/lib -L${MPI_DIR}/lib #-l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic
echo -----INT2CLM_finished # -I${MPI_DIR}/include\

