#!/bin/ksh
##
##################################################
## batch settings for INT2LM run on blizzard at DKRZ
##################################################
# @ shell = /bin/ksh
##### @ class = cluster
# @ class = express
# @ job_type = parallel
# @ job_name = sp_int2lm
# @ output = sp_int2lm.o$(jobid)
# @ error = sp_int2lm.o$(jobid)
# @ node_usage= shared
# @ network.MPI = sn_all,shared,us
# @ rset = rset_mcm_affinity
# @ mcm_affinity_options = mcm_accumulate
# @ node = 1
#### @ tasks_per_node = 64 # for SMT
# @ tasks_per_node = 6
# @ resources = ConsumableMemory(750mb)
##### @ task_affinity = cpu(1) # for SMT
# @ task_affinity = core(1)
# @ wall_clock_limit = 05:15:00
# @ notification     = never
#### @ account_no = bb0721 #  project account for the COSMO/CLM trainings course 2011
# @ queue
export MEMORY_AFFINITY=MCM
export MP_PRINTENV=YES
export MP_LABELIO=YES
export MP_INFOLEVEL=0
export MP_EAGER_LIMIT=64k
export MP_BUFFER_MEM=64M,256M
export MP_USE_BULK_XFER=NO
export MP_BULK_MIN_MSG_SIZE=128k
export MP_RFIFO_SIZE=4M
export MP_SHM_ATTACH_THRESH=500000
export LAPI_DEBUG_STRIPE_SEND_FLIP=8

set -e

#### !!!! Adopt this directory !!!!
SPDIR=/home/yurdanur/cclm-sp_2.4

cd ${SPDIR}/step_by_step/gcm_to_cclm/log_int2lm
# clean the directory
rm -rf I* O* Y*

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

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

cat > INPUT <<**
&CONTRL
  ydate_ini='2007080100', hstart=0, hstop=21216, hincbound=6,
  linitial=.TRUE.,  lboundaries =.TRUE.,
  lfilter_oro=.FALSE.,
  yinput_model='CM',
  l_cressman = .FALSE., l_bicub_spl = .FALSE., lbdclim = .TRUE.,
  ltime_mean = .TRUE., luvcor = .TRUE. , lreorder = .TRUE.,
  lmulti_layer_in = .TRUE., lmulti_layer_lm = .TRUE.,
  itype_aerosol = 2,
  itype_albedo = 2,
  itype_w_so_rel = 1,
  itype_t_cl = 0, !1,
  itype_rootdp = 0, !3, 
  lprog_qi = .TRUE.
  luse_t_skin = .FALSE.,
  lforest = .TRUE.,
  lsso = .TRUE.,
  nincwait = 5, nmaxwait = 20,
  llbc_smooth = .FALSE., nlbc_smooth = 10,
  nprocx = ${NPX}, nprocy = ${NPY},
  idbg_level=2,
 /END
 &GRID_IN
  pcontrol_fi =30000.,
  ie_in_tot = 512, je_in_tot = 256, ke_in_tot = 60,
  startlat_in_tot = -89.46202, startlon_in_tot = -180.,
  endlat_in_tot = 89.46252, 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.035, 0.175, 0.640, 1.775
  east_add_in =1
  south_add_in =1
 /END
 &LMGRID
  ivctype = 2,
  irefatm = 2,
  ielm_tot= 190, jelm_tot= 90, kelm_tot=40,
  pollat =  51, pollon = -148.0, polgam = 0.0,
  dlon=0.11, dlat=0.11,
  startlat_tot  = -4.45, startlon_tot  = -8.65,
  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=200, je_ext=100,
  ylmext_lfn='tr_11.nc',
  ylmext_cat='${SPDIR}/data/',
  yinext_lfn='caf2007080100.nc',
  yinext_cat='/data/EraInterim/',
  yin_cat='/data/EraInterim/',
  ylm_cat='/data/cosmoclm/int2lm_eraint_test/',
  nprocess_ini = 131, nprocess_bd = 132,
 /END
 &PRICTR
  lchkin=.TRUE., lchkout=.TRUE.,
 /END
**


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

# you have to adapt the way how to invoke the binary
echo ----- start INT2CLM
# poe ${SPDIR}/src/int2lm/bin/int2lm.exe -procs ${NP}
mpirun -mca btl ^openib -np ${NP} ${SPDIR}/src/int2lm/bin/int2lm.exe 
#mpirun -np ${NP} ${SPDIR}/src/int2lm/bin/int2lm.exe
echo ----- INT2CLM finished
