#!/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:30:00
#SBATCH --exclusive
#SBATCH --output=sp_int2lm.o%j
#SBATCH --error=sp_int2lm.o%j
#SBATCH --account=bb0884

# 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

#### !!!! Adopt this directory !!!!
SPDIR=/pf/b/b324083/cclm-sp_2.1

cd ${SPDIR}/step_by_step/gcm_to_cclm/log_int2lm

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

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

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

cat > INPUT <<**
 &CONTRL
  ydate_ini='1979010100', 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 = 512, je_in_tot = 256, ke_in_tot = 60,
  startlat_in_tot = -89.46282, startlon_in_tot = -180.,
  endlat_in_tot = 89.46282, endlon_in_tot = 179.2969,
  pollat_in = 90.0, pollon_in = 180.0,
  lushift_in=.FALSE.,.FALSE., lvshift_in=.FALSE.,.FALSE.,
  ke_soil_in=3,   
  czml_soil_in=0.015, 0.1, 0.405, 1.205,
  east_add_in=1,
 /END
 &LMGRID
  ivctype = 2,
  irefatm = 2,
  ielm_tot=290, jelm_tot=190, kelm_tot=40,
  pollat = 60.0, pollon = -95.0, 
  dlon=0.11, dlat=0.11, 
  startlat_tot  = -7.89, startlon_tot  = -11.89,   
  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=300, je_ext=200,
  ylmext_lfn='domain2016032117044.nc',
  ylmext_cat='/work/bb0884/chi/input/ext',
  yinext_lfn='caf1979010100.nc',
  yinext_cat='/work/bb0884/chi/input/year1979',
  yin_cat='/work/bb0884/chi/input/year1979',
  ylm_cat='/work/bb0884/chi/output/int2lm_output/1979',
  nprocess_ini = 131, nprocess_bd = 132, 
 /END
 &PRICTR
  igp_tot = 36,
  jgp_tot = 30,
  lchkin=.TRUE., lchkout=.TRUE.,
 /END
**


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

# 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
echo ----- INT2CLM finished


