#!/bin/bash
#
#################################################
# batch settings for CCLM run on Mistral at DKRZ
#################################################
#SBATCH --job-name=testrun
##SBATCH --partition=compute
#SBATCH --nodes=5
#SBATCH --ntasks-per-node=20
#SBATCH --time=05:00:00
#SBATCH --output=testurn_slurm.o%j.out
#SBATCH --error=testrun_slurm.o%j.err
#SBATCH --account=bb0961
#SBATCH --mail-user=edoardo.mazza@met.fu-berlin.de
#SBATCH --mail-type=FAIL       # Notify user by email in case of job failure
#
# 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
#
## -- Set up the environment -
source /sw/rhel6-x64/etc/profile.mistral

module load intel
module load mxm
module load fca
module load bullxmpi_mlx

export OMPI_MCA_pml=cm
export OMPI_MCA_mtl=mxm
export MCM_RDMA_PORTS=mlx5_0:1

export OMPI_MCA_coll=^ghc
#export OMPI_MCA_coll_fca_priority=95
#export OMPI_MCA_coll_fca_enable=1

# Some MISTRAL settings (after HJP):
ulimit -s 102400

export FORT_BUFFERED=TRUE
export decfort_dump_flag=TRUE
export MXM_LOG_LEVEL=ERROR

set -ex

# How many tasks spread over how many nodes                                                                                                   
NNODES=5
NTASKS=100
NPX=10
NPY=10
NPIO=0


########################################################################
#CLM_BIN='/work/bb0961/CCLM_EMazza/CCLM/cclm-sp-1.3.4/src/cclm/bin/cclm.exe'
CLM_BIN='/work/bb0961/COSMO-CLM/cclm-sp-1.3.4/src/cclm/bin/cclm.exe'    
MOD_VERS='4.8_clm19'                                    # model version
RESTART_NAME=date
###########################################################################

##########################################################################
# Input and output directories
##########################################################################

#####################
#JOB INFORMATION
#####################
SPDIR=/work/bb0961/CCLM_EMazza
WRKDIR=${PWD}/testrun_default
STATS=${WRKDIR}/stats                                       #output and error
SIM_ID=med_cycl_1996_era40

#INT2LM
#INPUT_DIR=/work/bb0961/CCLM_EMazza/med_cycl_101996_9ens_med00625_4gp/${SIM_ID}_4N
INPUT_DIR=/work/bb0961/CCLM_EMazza/testrun_int2lm
#
#SIMULATION
YDATE_START=1996100400
YDATE_STOP=1996100500

## Set up directories                                                                                                                                                                                       
if [ ! -d ${WRKDIR} ]; then
    mkdir ${WRKDIR}
  #  mkdir ${STATS}
fi

####################
# 4N
####################

if [ -d ${WRKDIR}/${SIM_ID}_4N ]
then
    echo "Run ${SIM_ID}_4N finished"
else

    JOB_ID=${SIM_ID}_4N
    START_LAT=-5.6162
    START_LON=-7.3060

    if [ ! -d  ${INPUT_DIR} ]; then
        echo "Directory  ${INPUT_DIR} not found"
        exit
    fi
fi

INIDIR=${INPUT_DIR}

OUTPUT=${WRKDIR}/${JOB_ID}/output
RESTART=${WRKDIR}/${JOB_ID}/restart

cd ${WRKDIR}
if [ -d $JOB_ID ]
then
echo "Directory exists"
echo "Program stop"
exit
fi

mkdir $JOB_ID
cd $JOB_ID
mkdir -p input
mkdir -p output
mkdir -p restart
mkdir -p stats

NOUTDIR=1
#NOUTDIRS=2

# creating NOUTDIRS
cd $WRKDIR/$JOB_ID/output
#typeset -x NOUTDIR

#while [ ${NOUTDIR} -le ${NOUTDIRS} ]
#  do
mkdir out0${NOUTDIR}
#  let "NOUTDIR = NOUTDIR + 1"
#done

cd $WRKDIR/$JOB_ID

HSTART=0
HSTOP=24
DT=60
################################################
### 2. CCLM ####################################
################################################
echo "!!! START CCLM !!!"

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

cat > INPUT_ORG << end_input_org
&LMGRID
  startlat_tot=-5.6162, startlon_tot=-7.3060,
  pollat=48,       pollon=-174,
  dlat=0.0625,         dlon=0.0625,
  ie_tot=288,         je_tot=192,        ke_tot=40,
/END

&RUNCTL
  dt=${DT},
  hstart=${HSTART},   hstop=${HSTOP},
  ydate_ini='1996100400',
  ydate_end='1996100500',
  nprocx= 10,
  nprocy= 10,
  nprocio=0,
  lphys    = .TRUE.,
  lreorder = .FALSE.,
  lreproduce = .TRUE.,
  ldiagnos=.FALSE.,
  ldfi=.FALSE.,
  ldatatypes = .FALSE.,
  ncomm_type = 3,
  ltime_barrier = .FALSE.,
  ldump_ascii=.FALSE.,
  nboundlines=3,
  hincmxt=24, 
  hincmxu=24,
  itype_calendar = 0,
  idbg_level=2000,
 /END

&TUNING
  wichfakt=0.15,
  tur_len=500.0,
!  rlam_mom=0.0,
!  rlam_heat=1.0,
!  rat_sea=20.,
!  rat_lam=1.0,
!  rat_can=1.0,
!  z0m_dia=0.2,
!  c_lnd=2.0,
!  c_sea=1.5,
!  pat_len=500.0,
!  clc_diag=0.5,
!  qi0=0.0,
!  qc0=0.0,
!  crsmin=150.,
/END
end_input_org

echo "!!! END_INPUT !!!"

cat > INPUT_IO  << end_input_io
&IOCTL
  lasync_io=.FALSE.,
  ngribout=1,
  yform_read ='ncdf',
  yform_write='ncdf',
  nhour_restart=${HSTOP},${HSTOP},${HSTOP},
  ydir_restart='$RESTART/',
  ytunit_restart='d',
  yncglob_institution="IFM FU Berlin"
  yncglob_title="Projektlaeufe"
  yncglob_source="LM 4.0 (2007)",
  yncglob_project_id="${JOB_ID}",
  yncglob_contact="edoardo.mazza@met.fu-berlin.de",
  yncglob_references="http://www.geo.fu-berlin.de/met/",
   /END
&DATABASE
/END 
&GRIBIN
  lan_t_so0=.TRUE.,
  lan_t_s=.FALSE.,
  lan_t_cl=.TRUE.,
  lan_w_cl=.TRUE.,
  lan_vio3=.TRUE.,
  lan_hmo3=.TRUE.,
  lan_plcov=.TRUE.,
  lan_lai=.TRUE.,
  lan_rootdp=.TRUE.,
  lan_t_snow=.TRUE.,
  lan_w_i=.TRUE.,
  lan_w_snow=.TRUE.,
  lan_rho_snow=.FALSE.,
  hincbound=6,
  lchkini=.TRUE.,
  lchkbd =.TRUE.,
  lbdana =.TRUE.,
  lana_rho_snow=.FALSE.,
!  lana_qi=.TRUE., llb_qi=.TRUE.,
!  lana_qr_qs=.FALSE., llb_qr_qs=.FALSE.,
  lana_qg=.FALSE., llb_qg=.FALSE.,
  ydirini='$INIDIR/laf',
  ydirbd='$INIDIR/laf',
  ytunitbd='d',
  /END

&GRIBOUT                                                                  
  ysystem='file',                                                             
  hcomb=0,24,6,                                                        
  yvarml=' ',                                                                    
  yvarpl='FI','T','U','V','W','QV','RELHUM','OMEGA','TINC_LH',                                   
  plev=100.,200.,300.,400.,500.,600.,700.,750.,800.,850.,900.,925.,950.,1000.,                                           
  yvarzl=' ',                                                                   
  luvmasspoint=.FALSE.,                                                      
  lcheck = .FALSE.,                                                            
  lwrite_const= .TRUE.,                                                          
  ydir='$OUTPUT/out01/',                                                          
  ytunit = 'd',                                                                 
/END
end_input_io

echo "!!! END_IO !!!"

cat > INPUT_DIA << end_input_dia
&DIACTL
 /END
end_input_dia

echo "!!! END_DIA !!!"

cat > INPUT_DYN << end_input_dyn
&DYNCTL
  l2tls=.TRUE.,
  ldyn_bbc=.TRUE.,
  ldiabf_lh=.TRUE.,
  lexpl_lbc=.TRUE.,
  rlwidth=85000,
  betasw=0.4, 
  alphaass=0.53,
  hd_corr_q=0,
  hd_corr_t=0,
  hd_corr_u=0.25,
  iadv_order=5,
  nrdtau=5,
 /END
end_input_dyn

echo "!!! END_DYN !!!"

cat > INPUT_PHY << end_input_phy
&PHYCTL
lgsp=.TRUE.
  itype_gscp=3,
  lprogprec=.TRUE.,
  ltrans_prec=.TRUE.,
lrad=.TRUE.,
  nradcoarse=1,
  lradf_avg=.FALSE.,
  hincrad=1,
  ico2_rad=2,
  lforest=.TRUE.,
ltur=.TRUE.,
  ninctura=1,
  lexpcor=.FALSE.,
  ltmpcor=.FALSE.,
  lprfcor=.FALSE.,
  lnonloc=.FALSE.,
  lcpfluc=.FALSE.,
  itype_turb=3,
  imode_turb=1,
  itype_tran=2,
  imode_tran=1,
  itype_wcld=2,
  icldm_rad=4,
  icldm_turb=2,
  icldm_tran=0,
  itype_synd=2,
lsoil=.TRUE.,
  itype_evsl=2,
  itype_trvg=2,
  lmelt=.TRUE.,
  lmelt_var=.TRUE.,
  lmulti_layer=.TRUE.,
  ke_soil=9,           
  czml_soil=0.005,0.025,0.07,0.16,0.34,0.7,1.42,2.86,5.74,11.5,  
  czbot_w_so=4.0
lconv=.TRUE.,
  lconf_avg=.TRUE.
  nincconv=4
  itype_conv=0,
  lsso=.FALSE.,
  llake=.FALSE.,
/END
end_input_phy

echo "!!! END_PHY !!!"

#################################################
# run the program
#################################################
# If reserving processors for GRIB I/O, then add 8 to 256
echo "----- start CCLM: $NTASKS tasks spread over $NNODES nodes -----" # EU-11: Used 256 processors
time srun -N $NNODES -n $NTASKS -l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic ${CLM_BIN}
echo "----- CCLM finished"

#cat YUSPECIF YUCHKDAT > OUTPUT

#exit 
