source: BOL/LMDZ_Setup/setup.sh @ 5859

Last change on this file since 5859 was 5858, checked in by idelkadi, 4 weeks ago

The "veget" key reset to CMIP6 by default following the previous modification

File size: 35.2 KB
Line 
1#!/bin/bash
2# This script sets up and launches a (series of) simulation(s).
3# RECOMMENDATION: use main.sh to drive it (do not run it directly)
4
5set -eu
6
7#---------------------------------------------------------------------------
8function define_expert_options() { # Expert-level choices
9#---------------------------------------------------------------------------
10
11  # optim: either "" or "-debug" to compile in debug mode
12  #       (slower but better diagnosis of segfaults)
13  optim=""
14
15  # "n" or "y". If testmode="y", then simulations run for a single day
16  # per period.
17  # NOTE: you must set mthend accordingly !
18  testmode="n"
19
20  # instll : either "y" or "n"
21  # default : y if n LMDZ will not be installed and compiled
22  install="y"
23
24  # Radiative code: "oldrad" / "rrtm" / "ecrad"
25  rad="rrtm"
26
27  #   !!! STRONG recommendation : experiments with DIFFERENT Orchidee or
28  #   aerosol options should be performed in DIFFERENT LMDZ_Setup folders
29  #   !!! (especially as they may need different initial files)
30
31  # AEROSOLS : "n" (=no)/"clim" (=average 1995-2014) / "spla" (interactive)
32  # (WARNING : if you first run the scripts with aerosols=n, then you want
33  # to change to =clim, you must remove the INIT and LIMIT folders and
34  # rerun main.sh with init=1 in order to create aerosol files
35
36  aerosols="clim"
37
38  # OZONE :
39  # AI et ZE oct 2025
40  #         0 => analytic formula fonction of altitude and latitude
41  #         1 => ozonread a single ozone climatology that will be used day and night
42  ozone=0
43
44  # SURFACE/VEGETATION SCHEME
45  # - "none" : bucket scheme
46  #   CMIP6 : orchidee CMIP6 version
47  #   7983/7994 : orchidee svn release 7983 (orch2.2) and 7994 (last tested)
48  #   If you need other orch versions, and also require XIOS, you'll need
49  #                    to create the appropriate files in DEF/XMLfilesOR...
50  veget="CMIP6"
51
52  # New snow scheme INLANDSIS! "y" / "n"
53  # Activates INLANDSIS compilation
54  #  to be done : treatment of specific restart and def file
55  inlandsis="n"
56
57  # netcdf: 0 (use existing library) / 1 (recompile netcdf, slow)
58  netcdf=0
59
60  # --->>> ALSO PAY ATTENTION TO OUTPUT files, frequency, level ------------
61  #   With IOIPSL : Choose your config.def among versions available in DEF,
62  #     copy it as config.def (the copy is done automatically for "iso"),
63  #     edit it and set up output files, frequencies and levels.
64  #     NB : For aerosols=spla, output level minimum 4 is required to outpu
65  #     the specific variables.
66  #     For aerosols=n, aerosol flags will automatically be set to "n".
67  #   With XIOS : adjust DEF/XMLfiles*/file*xml
68
69  save_pub=0
70
71}
72
73
74#---------------------------------------------------------------------------
75function enable_platform() {  # Sed platform-specific headers
76#---------------------------------------------------------------------------
77  local file="$1"
78  local platform
79
80  case ${hostname:0:5} in
81    jean-) platform="JZ";;
82    spiri) platform="SP";;
83    adast) platform="ADS";;
84    *) echo "Warning: $hostname is not a known job platform (ignore if running locally)"; return 0;;
85  esac
86
87  sed -i'' -e "s/^#@$platform//" "$file"
88}
89
90#---------------------------------------------------------------------------
91function load_install_lib() { #Fetch and source install_lmdz.sh to get myget
92#---------------------------------------------------------------------------
93  if [[ ! -f "install_lmdz.sh" ]]; then
94    wget --no-cache "http://svn.lmd.jussieu.fr/LMDZ/BOL/script_install/install_lmdz.sh"
95  fi
96  # shellcheck disable=SC1090
97  source <(sed 's/function \(.*\) {/function installlmdz_\1 {/g' install_lmdz.sh)  # source with a namespace for functions
98  function myget { installlmdz_myget "$@"; }
99}
100
101#---------------------------------------------------------------------------
102function set_default_params() { # Default value of script parameters
103#---------------------------------------------------------------------------
104
105  SIM=$(basename "$local_d")CTL  # name
106  phylmd="lmd" #option -p $phylmd for makelmdz
107
108  cosp="n"  # COSP
109  xios="n"  #XIOS
110
111  # Nudging :
112  ok_guide="n"
113  # With nudging, use real calendar (climato=0) and monthly integrations
114  climato=1
115  freq="mo"  # frequence mensuelle mo ou annuelle yr
116
117  # NB : the run stops in the BEGINNING of mthend (test "next=stopsim")
118  mthini=200001
119  mthend=200501
120  resol="144x142x79"
121
122  version="20230412.trunk"
123  svn=""
124
125  init=1
126
127  LIMIT="LIMIT"
128
129}
130
131#---------------------------------------------------------------------------
132function read_cmdline_args() {
133#---------------------------------------------------------------------------
134  while (($# > 0)); do
135    case $1 in
136      "-h") cat <<........fin
137       setup.sh can be launched/driven by main.sh
138             setup.sh [-v version] [-r svn_release] [-init INIT] [-d 96x95x79] [-f mo] [-nudging]
139             -v "version" like 20150828.trunk; see https://lmdz.lmd.jussieu.fr/Distrib/LISMOI.trunk (default <$version>)
140             -r "svn_release"        either the svn release number or "last" (default <$svn>)
141             -d IMxJMxLM             to run in resolution IM x JM x LM (default <$resol>)
142             -install y/n            pour installer et compiler le modele
143             -f mo/yr                pour tourner en mensuel ou annuel (default <$freq>)
144             -rad RAD                Radiation
145             -netcdf  0/1/DIR        Netcdf installation
146             -aerosols n/clim/spla   Aerosols : none / climatological / interactive with SPLA
147             -ozone 0/1              Ozone : not read / read an ozone climatology
148             -veget CMIP6            CMIP6 version of rchidee
149                    7983             orch2.2
150                    7994             trunk
151             "INIT"                  1: creates INIT and LIMIT
152                                     0: reads from INIT and LIMIT
153                                     SIMU: reads from preexisting simulation SIMU and LIMIT (default <$init>)
154             -nudging                to run with nudging. Nudging files must be created independently
155             -p                      the physics to use (default <$phylmd>)
156             -save_pub               To files downlaoded with wget (default <$SIM>)
157             -name                   install folder name (default <$SIM>)
158             Other options available (see "options" section in the script)
159........fin
160        exit;;
161      "-v") version="$2"; shift 2;;
162      "-r") svn=$2; shift 2;;
163      "-d") resol=$2; shift 2;;
164      "-f") freq=$2; shift 2;;
165      "-p") phylmd=$2; shift 2;;
166      "-name") SIM=$2; shift 2;;
167      "-save_pub") save_pub=$2 ; shift 2 ;;
168      "-install") install=$2; shift 2;;
169      "-rad") rad=$2; shift 2;;
170      "-netcdf") netcdf=$2; shift 2;;
171      "-aerosols") aerosols=$2; shift 2;;
172      "-veget") veget=$2; shift 2;;
173      "-cosp") cosp=y; shift;;
174      "-xios") xios=y; shift;;
175      "-init") init=$2; shift 2;;
176      "-nudging") ok_guide=y; shift;;
177      "-climato") climato=$2; shift 2;;
178      "-ozone") ozone=$2; shift 2;;
179      "-mthini") mthini=$2; shift 2;;
180      "-mthend") mthend=$2; shift 2;;
181      *) echo "unexpected $1"; $0 -h; exit
182    esac
183  done
184
185  # Initialisation
186  if [[ $init = 1 || $init = 0 ]]; then
187    INIT="INIT"
188  else
189    INIT=$init
190  fi
191
192  case $rad in
193    oldrad) iflag_rrtm=0; NSW=2;;
194    rrtm)   iflag_rrtm=1; NSW=6;;
195    ecrad)  iflag_rrtm=2; NSW=6;;
196  esac
197
198  im=$(echo "$resol" | cut -dx -f1)
199  jm=$(echo "$resol" | cut -dx -f2)
200  lm=$(echo "$resol" | cut -dx -f3)
201
202  case $save_pub in
203     0) save_pub_opt=0 ;;
204     1) save_pub_opt="-save_pub" ;;
205     *) save_pub_opt="-save_pub -save_pub_dir $save_pub"
206          mkdir -p $save_pub
207          if [ $? != 0 ] ; then echo Cannot create directory $save_pub
208                           exit 1 ; fi
209  esac
210
211  yearini=$(echo "$mthini" | cut -c-4)
212  if [[ $freq = yr ]]; then stopsim=$(echo "$mthend" | cut -c-4); else stopsim=$mthend; fi
213
214  if [[ -d $SIM ]]; then
215     echo "La simulation $SIM existe deja. Il est conseillé d'arrêter et de vérifier."
216     echo "Si vous êtes sûr de vous, vous pouvez la prolonger. Voulez vous la prolonger ? (y/n)"
217     read -r ans
218     if [[ $ans != y ]]; then exit 1; fi
219  fi
220
221  ######################################################################
222  # Choix du nombre de processeurs
223  # NOTES :
224  # omp=8 by default (for Jean-Zay must be a divisor of 40 procs/node)
225  # omp=1 : required for SPLA (omp parallelism not ready)
226  # omp=2 for veget=CMIP6+XIOS beacause of a bug in ORCHIDEE/src_xml/xios_orchidee.f90
227  ######################################################################
228  (( mpi = jm / 2 ))
229  omp=8
230  if [[ $aerosols = "spla" ]]; then omp=1; fi
231  if [[ $veget = "CMIP6" && $xios = "y" ]]; then omp=2; fi
232  if [[ $mpi -gt $NB_MPI_MAX ]]; then mpi=$NB_MPI_MAX; fi
233  if [[ $omp -gt $NB_OMP_MAX ]]; then omp=$NB_OMP_MAX; fi
234
235  # Compute how many mpi per node (required e.g. for Adastra)
236  mpi_per_node=0
237  if [[ $NB_CORE_PER_NODE_MAX -gt 0 ]]; then
238    local N_omp_mt=1
239    if [[ $omp -gt 1 ]]; then (( N_omp_mt = omp / N_HYPERTHREADING )); fi  # take into account hyperthreading
240    (( mpi_per_node = NB_CORE_PER_NODE_MAX / N_omp_mt ))
241    if [[ mpi_per_node -gt mpi ]]; then mpi_per_node=$mpi; fi
242  fi
243
244  echo "Total MPI=$mpi (PER NODE=$mpi_per_node), OMP=$omp"
245}
246
247#---------------------------------------------------------------------------
248function ensure_correct_option_combinations() {
249  # AVOID COMBINATIONS OF OPTIONS THAT DON'T WORK in user choices
250#---------------------------------------------------------------------------
251  if [[ $ok_guide = y && $climato = 1 ]]; then
252     echo "STOP: Running nudged simulations with climatological SSTs is not planned. Change <climato> to <0> or modify the setup (experts)"; exit 1
253  fi
254
255  if [[ $climato = 0 && $freq = "yr" ]]; then
256     echo "STOP: Running simulations with interannual SSTs is possible only month by month and a true calendar."
257     echo "Change <climato> to <1> or <freq> to <mo> or modify setup.sh (experts)"; exit 1
258  fi
259
260
261  # (Temporary) Constraints for aerosols=spla :
262  # --> resolution 128x88x79 and rad=rrtm
263  if [[ $aerosols = "spla" && ( ${im}x${jm} != "128x88" || $freq != mo ) ]]; then
264    echo 'STOP: For now, <aerosols=spla> requires <resol=128x88x79>,'
265    echo 'and uses the zoomed grid from gcm.def_zNAfrica_BiJe,'
266    echo 'for which forcing & initial files are available'
267    echo "Right now resol=<$resol>"
268    echo 'Should be run in monthly mode'
269    exit 1
270  fi
271
272  if [[ $rad = "ecrad" && $phylmd != "lmd" ]]; then
273    echo "<rad=ecrad> is only supported for <phy=phylmd> here"  # (Amaury) I added this check because we fetch ecrad data from libf/phylmd/ecrad/data only.
274  fi
275}
276
277#---------------------------------------------------------------------------
278function install_model() {
279#---------------------------------------------------------------------------
280  mkdir -p "$LMDZD"
281
282  local ins_xios ins_cosp ins_aero ins_inlandsis
283  if [[ $xios = "y" ]]; then ins_xios="-xios"; else ins_xios=""; fi
284  if [[ $cosp = "y" ]]; then ins_cosp="-cosp v1"; else ins_cosp=""; fi
285  if [[ $aerosols = "spla" ]]; then ins_aero="-spla"; else ins_aero=""; fi
286  if [[ $inlandsis = "y" ]]; then ins_inlandsis="-inlandsis"; else ins_inlandsis=""; fi
287  if [[ -n $svn ]]; then svnopt="-r $svn"; else svnopt=""; fi
288
289  if [[ $PARALLEL == 0 ]] ; then parallel= ; parsuf=seq ; else parallel="-parallel mpi_omp" ; parsuf= ; fi
290  version_name=LMDZ$(echo "$version" | sed -e 's/-v//g' -e 's/-unstable//' -e 's/-r/r/' -e 's/ //g')
291  LMDZname="${version_name}${svn}OR$veget${ins_xios}${parsuf}$( echo $optim | sed -e 's/[\ -]//g' )"
292  MODEL="$LMDZD/$LMDZname/modipsl/modeles/LMDZ"
293
294  if [[ -d $MODEL ]]; then echo "Found existing install at MODEL=$MODEL"; fi
295  if [[ $install = "y" ]]; then
296     echo "Installing model"
297     cd "$LMDZD"
298     cp "$local_d/install_lmdz.sh" .
299     chmod +x install_lmdz.sh
300     local make_j=8
301     # We launch using $MPICMD, except if it's using mpirun (no srun equivalent for bash script) => if supported, the compilation runs in a cluster job
302     jobcmd="\"$RUNBASHCMD $make_j\""
303     if [[ ${hostname:0:5} = "jean-" ]]; then jobcmd="\"$RUNBASHCMD $make_j --partition=compil\""; fi  # On JeanZay: compile on the <compil> partition
304     if [[ $(echo "$RUNBASHCMD" | cut -c -4) = "bash" ]]; then
305        jobcmd="bash"
306     fi
307     echo "./install_lmdz.sh -noclean $optim -v $version $svnopt -d $resol -rad $rad -bench compile_only $parallel $ins_cosp $ins_xios $ins_aero $ins_inlandsis -name $LMDZname -veget $veget -netcdf $netcdf -arch $ARCH -make_j $make_j -jobcmd $jobcmd" $save_pub_opt >> install_lmdz_options.$$.sh
308     chmod +x install_lmdz_options.$$.sh
309     echo "Running install_lmdz_options.$$.sh"
310     set -o pipefail
311       ./install_lmdz_options.$$.sh
312       #gcm=$MODEL/$(./install_lmdz_options.$$.sh | tee /dev/tty | tail -n 1 | sed -n "s:.* executable is \(.*\.e\).*:\1:p")
313       gcm=$MODEL/bin/gcm.e
314     set +o pipefail
315     mv install_lmdz.sh install_lmdz.$$.sh
316  else 
317    echo "install=n no need to install and recompile the model (already done)"
318    gcm=$MODEL/bin/gcm.e
319  fi 
320  cd "$local_d"
321}
322
323#---------------------------------------------------------------------------
324function setup_def() {  # modify various .def in ./DEF (+ xios xml )
325#---------------------------------------------------------------------------
326  cd "$local_d"
327
328  # Utilisation des .def_iso pour LMDZ-ISOtopes
329  if [[ $phylmd = "lmdiso" ]]; then
330    for file_iso in $(ls DEF | grep _iso); do
331      cp DEF/"$file_iso" DEF/"${file_iso%%_iso}"
332    done
333  fi
334
335  ######################################################################
336  # Choix de la grille verticale
337  ######################################################################
338  if [ ! -f "DEF/DYN/L$lm.def" ]; then
339    echo "STOP: Résolution verticale non prévue - créer un fichier DEF/DYN/L$lm.def"; exit 1
340  else
341    sed -i'' -e "s/INCLUDEDEF=L.*.def/INCLUDEDEF=L$lm.def/" DEF/run.def
342    cp DEF/DYN/L$lm.def DEF/
343  fi
344
345  ######################################################################
346  # Changements dans les fichiers DEF/*def
347  # (ils vont se repercuter dans les repertoires de simulation $local_d/$SIM et de run $SIMRUNDIR)
348  ######################################################################
349
350  # Choix du fichier tracer.def coherent avec l'option "aerosols"
351  #  NOTE : Le nouveau tracer.def_nospla par defaut n'inclut pas Rn-Pb;
352  #  si on les veut, il faut utiliser ci-dessous; a la place, tracer_RN_PB.def
353  #---------------------------------------------------------------------
354  # NB : Les traceurs absents de start* files sont initialises=0 dans le code
355  if [[ $aerosols = "spla" ]]; then
356    # nouveau tracer.def (remplace "traceur.def")
357    cp DEF/CONFIG/traceur.def_spla DEF/traceur.def
358    \rm -f DEF/tracer.def
359  elif [[ $phylmd = "lmdiso" ]]; then
360    # déjà copié si 'y'
361    cp DEF/CONFIG/tracer.def_nospla DEF/tracer.def
362  fi
363
364  # TEMPORAIREMENT pour spla on force l'utilisation de gcm.def_zNAfrica_BiJe (avec resolution 128x88x79)
365  #----------------------------------------------------------------------
366  if [[ $aerosols = "spla" ]]; then
367      cp DEF/DYN/gcm.def_zNAfrica_BiJe DEF/gcm.def; fi
368
369  # Inscription du choix ok_guide dans DEF/guide.def
370  #---------------------------------------------------------------------
371  sed -i'' -e 's/ok_guide=.*.$/ok_guide='$ok_guide'/' DEF/guide.def
372
373  # Inscription du type de calendrier (qui est fonction de $climato) dans DEF/run.def
374  #---------------------------------------------------------------------
375  # NB Contrairement a ce qui est ecrit dans les fichiers run.def standard,
376  # dans ce tutorial le choix earth_365d n'est pas disponible, et earth_366d s'appelle gregorian
377  if [[ $climato = 0 ]]; then calend="gregorian"; else calend="earth_360d"; fi
378  sed -i'' -e 's/calend=.*.$/calend='$calend'/' DEF/run.def
379
380  # Changements dans config.def (pre-choisi, et regle pour output si utilisation avec IOIPSL)
381  #   cf options veget, aerosols, cosp, xios
382  #---------------------------------------------------------------------
383  if [[ $veget = "none" ]]; then  VEGET="n"; else VEGET="y"; fi
384  sed -i'' -e 's/VEGET=.*.$/VEGET='$VEGET'/' DEF/config.def
385
386  ok_cdnc=y ; ok_ade=y ; ok_aie=y ; ok_alw=y
387  case $aerosols in
388    clim) flag_aerosol=6 ;;
389    spla) flag_aerosol=1 ;;
390    n)    flag_aerosol=0 ; ok_cdnc=n ; ok_ade=n ; ok_aie=n ; ok_alw=n ;;
391    *)    echo Option aerosols=$aerosols not available ; exit 1
392  esac
393  sed -i'' -e 's/^ok_cdnc=.*.$/ok_cdnc='$ok_cdnc'/' \
394           -e 's/^ok_ade=.*.$/ok_ade='$ok_ade'/' \
395           -e 's/^ok_aie=.*.$/ok_aie='$ok_aie'/' \
396           -e 's/^ok_alw=.*.$/ok_alw='$ok_alw'/' \
397           -e 's/^flag_aerosol=.*.$/flag_aerosol='$flag_aerosol'/' DEF/config.def
398  pwd
399
400  # OZONE : modifier la cles logique pour ozone
401  if [[ $ozone -eq 1 ]]; then
402     sed -i'' -e 's/read_climoz=.*.$/read_climoz='$ozone'/' DEF/config.def
403  fi
404
405  # COSP : ok_cosp desactive COSP si on a compile avec; il ne l'active pas si on a compile sans
406  sed -i'' -e 's/ok_cosp.*.$/ok_cosp='$cosp'/' DEF/config.def
407  if [[ $cosp = "y" ]]; then \cp -f "$MODEL"/DefLists/cosp*.txt "$local_d"/DEF/; fi
408
409  # Sorties LMDZ en fonction de l'option "xios"
410  sed -i'' -e 's/ok_all_xml=.*.$/ok_all_xml='$xios'/' DEF/config.def
411
412  # Ajuster physiq.def en fonction de radiative code (default: values for rad=rrtm)
413  #   Pour isotopes=y , mettre iflag_ice_thermo=0 au lieu de 1
414  #---------------------------------------------------------------------
415  sed -i'' -e "s/iflag_rrtm=.*.$/iflag_rrtm=$iflag_rrtm/" -e "s/NSW=.*.$/NSW=$NSW/" DEF/physiq.def
416  sed -i"" -e "s:directory_name.*$:directory_name=\"$MODEL/libf/phylmd/ecrad/data\",:" DEF/namelist_ecrad
417
418  if [[ $phylmd = "lmdiso" ]]; then iflag_ice_thermo=0; else iflag_ice_thermo=1; fi
419  sed -i -e 's/iflag_ice_thermo=.*.$/iflag_ice_thermo='$iflag_ice_thermo'/' DEF/physiq.def
420
421  # Choix de orchidee.def en fonction de orchidee_rev; modification pour xios
422  #  NOTE separate orchidee_pft.def file for ORCHIDEE trunk post-CMIP6
423  #---------------------------------------------------------------------
424  orchidee_def=orchidee.def_6.1
425  orchidee_pft_def=""
426  if [[ $veget = "7983" ]]; then
427    orchidee_def=orchidee.def_6.2work
428  elif [[ $veget = "8758" ]]; then
429    orchidee_def=orchidee.def_6.4work
430    orchidee_pft_def=orchidee_pft.def_6.4work
431    if ! grep "INCLUDEDEF=orchidee_pft.def" DEF/run.def; then
432      sed -i'' -e 's/INCLUDEDEF=orchidee.def/INCLUDEDEF=orchidee.def\nINCLUDEDEF=orchidee_pft.def/' DEF/run.def; fi
433  fi
434  cp -f DEF/CONFIG/$orchidee_def DEF/orchidee.def
435  if [[ $orchidee_pft_def != "" ]]; then cp -f DEF/CONFIG/$orchidee_pft_def DEF/orchidee_pft.def; fi
436
437  # Only for veget=CMIP6 it is still possible to use IOIPSL; newer versions of orchidee.def have XIOS_ORCHIDEE_OK = y
438  sed -i'' -e 's/XIOS_ORCHIDEE_OK =.*.$/XIOS_ORCHIDEE_OK = '$xios'/' DEF/orchidee.def
439
440  ######################################################################
441  # Si on tourne avec XIOS, mise a jour des fichiers context et field* dans XMLfilesLMDZ
442  # (ils vont se repercuter dans les repertoires de simulation $local_d/$SIM et de run $SIMRUNDIR)
443  ######################################################################
444  if [[ $xios = y ]]; then
445    cp -f "$MODEL"/DefLists/context_lmdz.xml "$local_d"/DEF/XMLfilesLMDZ/.
446    cp -f "$MODEL"/DefLists/field_def_lmdz.xml "$local_d"/DEF/XMLfilesLMDZ/.
447    if [[ $cosp = y ]]; then cp -f "$MODEL"/DefLists/field_def_cosp1.xml "$local_d"/DEF/XMLfilesLMDZ/.; fi
448  fi
449}
450
451#---------------------------------------------------------------------------
452function setup_ce0l() { # Verification de l'existance de l'état initial, compilation eventuelle pour sa creation
453#---------------------------------------------------------------------------
454  if [[ ! -d $INIT ]]; then
455    if [[ $init = 0 ]]; then
456      echo "STOP: Récuperer les repertoires $INIT ou lancer avec option -init"; exit 1
457    else
458      # Compile ce0l
459      cd "$MODEL"
460      sed -e 's/gcm/ce0l/g' compile.sh > compile_ce0l.sh; chmod +x compile_ce0l.sh
461      echo "Compiling ce0l"
462      if ! ./compile_ce0l.sh &> ce0l.log; then echo "STOP: ce0l compilation failed, see $MODEL/ce0l.log"; exit 1; fi
463      echo "Compiled ce0l"
464      ce0l=${gcm/gcm/ce0l}
465
466      cd "$local_d"
467    fi
468  elif [[ $init = 1 ]]; then
469    echo "STOP: Vous essayez d initialiser le modele mais $INIT existe deja"; exit 1
470  fi
471}
472
473#---------------------------------------------------------------------------
474function setup_simu() {
475#---------------------------------------------------------------------------
476  #SIMRUNTOPDIR="$SIMRUNBASEDIR/$(basename "$local_d")"
477  SIMRUNTOPDIR="$SIMRUNBASEDIR"
478  SIMRUNDIR=$SIMRUNTOPDIR
479  mkdir -p "$SIMRUNDIR"
480  cd "$SIMRUNDIR"
481  echo "La simulation s'exécutera sur $SIMRUNDIR"
482
483  #####################################################################
484  # Creation du repertoire $SIM s'il n'existe pas deja
485  #####################################################################
486  if [[ ! -d $local_d/$SIM ]]; then
487    mkdir "$local_d/$SIM"
488    cd "$local_d"
489
490    # Edit reb.sh
491    cp reb.sh "$local_d/$SIM/reb.sh"; chmod +x "$local_d/$SIM/reb.sh"
492    sed -i'' -e "s:^rebuild=.*.$:rebuild=$LMDZD/$LMDZname/modipsl/bin/rebuild:" "$local_d/$SIM/reb.sh"
493    enable_platform "$local_d/$SIM/reb.sh"
494
495    # Copy .def
496    cp lmdz_env.sh "$local_d/$SIM/"
497    mkdir "$local_d/$SIM/DEF"; cp DEF/*def DEF/namelis* "$local_d/$SIM/DEF/"
498    #Pour XIOS, respectivement COSP, copier aussi les fichiers *xml / *txt
499    if [[ $cosp = "y" ]]; then cp DEF/cosp*txt "$local_d/$SIM/DEF/"; fi
500    if [[ $xios = "y" ]]; then
501       cp DEF/XMLfilesLMDZ/*xml "$local_d/$SIM/DEF/"
502       if [[ $veget != 'none' ]]; then cp DEF/XMLfilesOR$veget/*xml "$local_d/$SIM/DEF/"; fi
503    fi
504    chmod u+w "$local_d/$SIM"/DEF/*
505
506    # Gestion du calendrier
507    #######################
508    cd "$SIM"
509    sed -i'' -e "s/anneeref=.*.$/anneeref=$yearini/" DEF/run.def
510    if [[ $freq = "yr" ]]; then date=$yearini; else date=$mthini; fi
511    echo "$date a faire" >| etat
512
513    # Recuperation des fichiers : executable initiaux et forcages
514    #############################################################
515    echo "date: $date"
516    for f in start startphy; do
517      inf=../$INIT/$f.$date.nc
518      if [[ -f $inf || $init = 1 ]]; then ln -s "$inf" ./; else echo "STOP: $inf missing"; exit ; fi;
519    done
520    for f in sechiba stomate; do
521      if [[ -f ../$INIT/start_$f.$date.nc ]]; then ln -sf "../$INIT/start_$f.$date.nc" .; fi
522    done
523    cp "$gcm" gcm.e
524  fi
525  cd "$local_d"/..
526
527  #####################################################################
528  echo "Modification du script de lancement"
529  #####################################################################
530  local cput
531  if [[ $freq = "yr" ]]; then cput="04:00:00"; else cput="01:00:00"; fi
532  local isotopes="n"
533  if [[ $phylmd = "lmdiso" ]]; then isotopes="y"; fi
534  sed -e "s/NOM_SIMU/$SIM/" \
535  -e "s/time=.*.$/time=$cput/" \
536  -e "s/ntasks=.*.$/ntasks=$mpi/" \
537  -e "s/ntasks-per-node=.*.$/ntasks-per-node=$mpi_per_node/" \
538  -e "s/cpus-per-task=.*.$/cpus-per-task=$omp/" \
539  -e "s/nthreads=.*./nthreads=$omp/" \
540  -e "s/MAINDIR=.*.$/MAINDIR=$(basename "$local_d")/" \
541  -e "s:STORED=.*.*:STORED=$(dirname "$local_d"):" \
542  -e "s:SCRATCHD=.*.*:SCRATCHD=$SIMRUNBASEDIR:" \
543  -e "s/stopsim=.*.$/stopsim=$stopsim/" \
544  -e "s/^veget=.*.$/veget=$veget/" \
545  -e "s/^aerosols=.*.$/aerosols=$aerosols/" \
546  -e "s/^isotopes=.*.$/isotopes=$isotopes/" \
547  -e "s/^climato=.*.$/climato=$climato/" \
548  -e "s/^ozone=.*.$/ozone=$ozone/" \
549  -e "s/^ok_guide=.*.$/ok_guide=$ok_guide/" \
550  "$local_d/script_SIMU" >| "$SIMRUNDIR/tmp_$SIM"
551
552  enable_platform "$SIMRUNDIR/tmp_$SIM"
553
554  if [[ $testmode = "y" ]]; then
555    sed -i'' -e "s/time=.*.$/time=00:10:00/" -e "s/#nday=1/nday=1/" -e "s/#@TESTQ//" "$SIMRUNTOPDIR/tmp_$SIM"
556  fi
557}
558
559#----------------------------------------------------------------------
560function list_yrmth() {
561#----------------------------------------------------------------------
562    local liste=""
563    local mthini=$1
564    local mthend=$2
565    local mo_=$mthini
566    while [[ $mo_ != $mthend ]] ; do
567        liste="$liste $mo_"
568        yr__=$( echo $mo_ | cut -c 1-4 )
569        mo__=$( echo $mo_ | cut -c 5-6 )
570        echo YM $yr__ $mo__
571        if [ $(( $mo__ +1 )) == 12 ] ; then
572           yr__=$(( $yr__ + 1 )) ; mo_=$yr__$mo__
573        else
574           mo_=$(( $mo_ + 1 ))
575        fi
576    done
577    echo $liste
578   
579}
580
581#----------------------------------------------------------------------
582increment_month() {
583#----------------------------------------------------------------------
584  local year=$( echo $1 | cut -c 1-4 )
585  local month=$(( $1 - ${year}00 ))
586  if [ $(( $month - 12 )) -eq 0 ]; then
587    year=$((year + 1))
588    month=1
589  else
590    month=$((month + 1))
591  fi
592  if [[ $month -le 9 ]] ; then month=0$month ; fi
593  echo $year$month
594}
595
596#----------------------------------------------------------------------
597function fetch_simu_init_files() {
598#----------------------------------------------------------------------
599  #####################################################################
600  echo "Recuperation eventuelle de certains fichiers sur http:lmdz.lmd.jussieu.fr/pub"
601  #####################################################################
602
603  #  ORCHIDEE input files
604  get_input_files wget_pub Orchidee
605
606  # Files for aerosols and chemistry
607  if [[ $aerosols = "clim" ]]; then
608       get_input_files wget_pub AerChem ; fi
609
610  # Files for SPLA
611  if [[ $aerosols = "spla" ]]; then
612       get_input_files wget_pub SPLA_WA/emissions
613       mo_=$mthini
614       while [[ $mo_ != $mthend ]] ; do
615       # Code horrible duplique a cause de 08 (FH)
616           local yr__=$( echo $mo_ | cut -c 1-4 )
617           local mo__=$(( $mo_ - ${yr__}00 ))
618           if [[ $mo__ -le 9 ]] ; then mo__=0$mo__ ; fi
619           for var in u10m v10m u v ; do
620               echo wget_pub 3DInputData/SPLA_WA/ERA5/$yr__/$mo__ $var.nc
621               wget_pub 3DInputData/SPLA_WA/ERA5/$yr__/$mo__ $var.nc
622           done
623           mo_=$( increment_month $mo_ )
624       done
625       cd $local_d ; ln -sf $LMDZ_INIT/3DInputData/SPLA_WA/ERA5 GUIDE
626  fi
627}
628
629#----------------------------------------------------------------------
630function run_sim_or_init() {
631#----------------------------------------------------------------------
632  cd "$local_d"
633
634  if [[ $init = 1 ]]; then
635    #####################################################################
636    echo "Creation de l etat initial"
637    #####################################################################
638
639    # Creation du repertoire INIT et mise en place de liens logiques vers les starts
640    # en anticipation de leur création :
641    mkdir "$local_d/$INIT"; cd "$local_d/$INIT"
642    for an in $mthini $yearini; do for f in start startphy; do ln -s "$f.nc" "$f.$an.nc"; done; done
643
644    # Creation du repertoire INIT temporaire et rapatriement des fichiers necessaires
645    if [[ -d $SIMRUNDIR/$INIT ]]; then mv "$SIMRUNDIR/$INIT" "$SIMRUNDIR/$INIT$$"; fi
646    mkdir "$SIMRUNDIR/$INIT"; cp -r "$local_d/DEF" "$SIMRUNDIR/$INIT/"
647    cd "$SIMRUNDIR/$INIT"; cp DEF/*.def .; cp "$local_d/lmdz_env.sh" .
648    if [[ $xios = "y" ]]; then
649      cp DEF/XMLfilesLMDZ/*xml .
650      if [[ $veget != 'none' ]]; then cp DEF/XMLfilesOR$veget/*xml .; fi
651    fi
652    sed -e "s/anneeref=.*.$/anneeref=$yearini/" DEF/run.def >| run.def
653
654    #-------------------------------------------------------------------
655    # Fichiers Limit
656    #-------------------------------------------------------------------
657    local yrs suf
658    if [[ $climato = 0 ]]; then
659      # calend est choisi plus haut dans "Changements dans les fichiers DEF/*def" et ecrit dans $MAINDIR/DEF/run.def
660      yrini=$(echo "$mthini" | cut -c-4)
661      yrend=$(echo "$mthend" | cut -c-4)
662      yrs=""; yr=$yrini
663      while [[ $yr -le $yrend ]]; do yrs="$yrs $yr"; (( yr = yr + 1 )); done
664      suf="360x180_"
665    else
666      yrs=2000
667      suf="1x1_clim"
668    fi
669
670    liste_get="Albedo.nc Relief.nc Rugos.nc landiceref.nc"
671    for yr in $yrs; do
672       if [[ $climato = 0 ]]; then sufyr=$suf$yr; else sufyr=$suf; fi
673       liste_get="$liste_get amipbc_sic_$sufyr.nc amipbc_sst_$sufyr.nc"
674    done
675    echo LISTEGET "$liste_get"
676    for file in $liste_get; do
677       wget_pub 3DInputData/Limit $file
678       ln_from_pub  3DInputData/Limit $file
679    done
680
681    #  Ozone
682    if [[ $ozone -eq 1 ]]; then
683       if [[ $climato -eq 1 ]]; then
684        echo LISTEGET tro3_1979_2008_clim.nc
685           wget_pub 3DInputData/Ozone/original tro3_1979_2008_clim.nc
686           ln_from_pub  3DInputData/Ozone/original tro3_1979_2008_clim.nc
687       elif [[ $climato -eq 0 ]]; then
688           yr=$yrini
689           #while [[ $yr -le $yrend ]]; do
690           echo LISTEGET tro3_${yr}.nc
691           wget_pub 3DInputData/Ozone/original tro3_$(( $yr - 1 )).nc
692           ln_from_pub 3DInputData/Ozone/original tro3_$(( $yr - 1 )).nc
693           wget_pub 3DInputData/Ozone/original tro3_${yr}.nc
694           ln_from_pub 3DInputData/Ozone/original tro3_$yr.nc
695           wget_pub 3DInputData/Ozone/original tro3_$(( $yr + 1 )).nc
696           ln_from_pub 3DInputData/Ozone/original tro3_$(( $yr + 1 )).nc
697           #done
698       fi
699    fi
700    #-------------------------------------------------------------------
701    # Initial state ECDYN
702    #-------------------------------------------------------------------
703    wget_pub 3DInputData/Init ECDYN.nc
704    ln_from_pub 3DInputData/Init ECDYN.nc
705    ln -sf ECDYN.nc ECPHY.nc
706
707    # Creation du script d'initialisation
708  if [ "$MPICMD" = "" ] ; then mpicmd='' ; else mpicmd='OMP_NUM_THREADS=1 '"$MPICMD"' 1' ; fi
709    cat << ...eod >| tmp
710#!/bin/bash
711#@JZ#JeanZay
712#@JZ#SBATCH --job-name=Init         # nom du job
713#@JZ#SBATCH --ntasks=1              # Nombre de processus MPI
714#@JZ#SBATCH --cpus-per-task=1      # nombre de threads OpenMP
715#@JZ# /!\ Attention, la ligne suivante est trompeuse mais dans le vocabulaire
716#@JZ# de Slurm "multithread" fait bien référence à l'hyperthreading.
717#@JZ#SBATCH --hint=nomultithread    # 1 thread par coeur physique (pas d'hyperthreading)
718#@JZ#SBATCH --time=01:00:00         # Temps d'exécution maximum demandé (HH:MM:SS)
719#@JZ#SBATCH --output=Init%j.out     # Nom du fichier de sortie
720#@JZ#SBATCH --error=Init%j.out      # Nom du fichier d'erreur (ici commun avec la sortie)
721#@JZ# To submit to dev queue; "time" (above) must be max 2h
722#@JZ#TESTQ#SBATCH --qos=qos_cpu-dev
723#@SP#Spirit
724#@SP#SBATCH --job-name=Init
725#@SP#SBATCH --ntasks=1
726#@SP#SBATCH --cpus-per-task=1
727#@SP#SBATCH --hint=nomultithread
728#@SP#SBATCH --time=01:00:00
729#@SP#SBATCH --output=Init%j.out
730#@SP#SBATCH --error=Init%j.out
731#@ADS#Adastra
732#@ADS#SBATCH --job-name=Init
733#@ADS#SBATCH --ntasks=1
734#@ADS#SBATCH --cpus-per-task=1
735#@ADS#SBATCH --nodes=1
736#@ADS#SBATCH --hint=nomultithread
737#@ADS#SBATCH --time=00:10:00
738#@ADS#SBATCH --output=Init%j.out
739#@ADS#SBATCH --error=Init%j.out
740
741set -eu
742
743# ANCIEN MULTI STEP  case \${LOADL_STEP_NAME} in
744
745# ANCIEN MULTI STEP   init )
746
747if [ ! -f lmdz_env.sh ]; then echo "manque fichier lmdz_env.sh"; ls; exit 1; fi
748. lmdz_env.sh
749ulimit -s unlimited
750cd $SIMRUNDIR/$INIT
751echo "Executable : $ce0l"
752for yr in $yrs; do
753  # sst and sic
754  if [ $climato = 0 ]; then sufyr=$suf\$yr; else sufyr=$suf; fi
755  ln -sf amipbc_sic_\$sufyr.nc amipbc_sic_1x1.nc
756  ln -sf amipbc_sst_\$sufyr.nc amipbc_sst_1x1.nc
757  # ozone
758  if [[ $ozone -eq 1 ]]; then
759     if [[ $climato -eq 1 ]]; then
760        ln -s tro3_1979_2008_clim.nc climoz.nc
761     else
762        ln -s tro3_${yr}.nc  climoz.nc
763        ln -s tro3_$(( $yr - 1 )).nc climoz_m.nc
764        ln -s tro3_$(( $yr + 1 )).nc climoz_p.nc
765     fi
766   fi
767  # modif annee dans run.def
768  sed -e 's/anneeref=.*.$/anneeref='\$yr'/' DEF/run.def >| run.def
769  echo Starting initialisation
770  # Runing ce0l.e
771  $mpicmd $ce0l  # ce0l requires MPI=OMP=1
772  # Renomer limit et/ou climoz_LMDZ.nc si climato=0
773  if [ $climato = 0 ]; then mv limit.nc limit.\$yr.nc; fi
774  if [[ $ozone -eq 1 ]]; then
775     if [ $climato = 0 ]; then mv climoz_LMDZ.nc climoz_LMDZ_\$yr.nc; fi
776  fi
777done
778# ANCIEN MULTI STEP   ;;
779
780# ANCIEN MULTI STEP   interp )
781if [[ $aerosols = clim ]]; then
782  cp $local_d/interp_aerosols.sh .; chmod +x interp_aerosols.sh
783  # Les aerosols de l'annee 2000 ont été remplacés par "9999" qui pointe vers un fichier moyen sur 1995-2014
784  #for year in 2000 1850; do  ./interp_aerosols.sh \$year; done
785  #mv aerosols.2000.nc aerosols.clim.nc; mv aerosols.1850.nc aerosols.nat.nc
786  for year in 9999 1850; do ./interp_aerosols.sh \$year; done
787  mv aerosols.9999.nc aerosols.clim.nc; mv aerosols.1850.nc aerosols.nat.nc
788fi
789
790for f in sta* gri*nc; do cp \$f $local_d/$INIT/\$f; done
791if [[ $climato = 1 ]]; then cp limit.nc $local_d/$INIT/limit.nc; fi
792if [[ $climato = 1 ]]; then
793   if [[ $ozone -eq 1 ]]; then cp climoz_LMDZ.nc $local_d/$INIT/climoz_LMDZ_1979_2008_clim.nc; fi
794fi
795mkdir -p $local_d/$LIMIT
796for f in limit*.nc ; do cp \$f $local_d/$LIMIT/\$f; done
797# ozone
798if [[ $ozone -eq 1 ]]; then
799   if [[ $climato -eq 1 ]]; then cp climoz_LMDZ.nc $local_d/$LIMIT/climoz_LMDZ_1979_2008_clim.nc; fi
800   if [[ $climato -eq 0 ]]; then cp climoz_LMDZ_${yr}.nc $local_d/$LIMIT/.; fi
801fi
802if [ $aerosols = clim ]; then  for f in aerosols[.0-9]*nc; do cp \$f $local_d/$LIMIT/\$f; done; fi
803#
804cd $SIMRUNDIR
805...eod
806    if [[ $ok_guide != "y" ]]; then # Running first simulation automatically except for nudging
807      cat << ...eod >> tmp
808      echo "Submitting job tmp_$SIM"
809      #echo "\$SUBMITCMD tmp_$SIM"
810      #\$SUBMITCMD tmp_$SIM
811      echo submitcmd tmp_$SIM
812      submitcmd tmp_$SIM
813...eod
814    fi
815    cat << ...eod >> tmp
816  # ANCIEN MULTI STEP   esac
817...eod
818    enable_platform tmp
819    echo "#################################################################"
820    #echo "Submitting initialisation job <$SUBMITCMD tmp> from $(pwd)"
821    echo "Submitting initialisation job <submitcmd tmp> from $(pwd)"
822    chmod +x tmp
823    #$SUBMITCMD tmp
824    submitcmd tmp
825    echo "#################################################################"
826
827  else #case [ $init != 1 ]
828     cd "$SIMRUNDIR"
829     echo "################################################################"
830     echo "Submitting job tmp_$SIM"
831     #echo "$SUBMITCMD tmp_$SIM"
832     #$SUBMITCMD "tmp_$SIM"
833     echo "submitcmd tmp_$SIM"
834     submitcmd "tmp_$SIM"
835     echo '################################################################'
836  fi
837}
838
839#---------------------------------------------------------------------------
840function message_post_submit() {
841#---------------------------------------------------------------------------
842  if [[ $ok_guide = "y" && $init = 1 ]]; then
843    cd "$local_d"
844    enable_platform era2gcm_tuto.sh
845    echo "Once initialisation is finished, you have to create nudging files"
846    echo "Edit era2gcm_tuto.sh and set the desired parameters in section <User choices>"
847    echo "Make sure you have acces to the chosen ERA files, and the required modules are loaded, then run : ./era2gcm_tuto.sh"
848    if [[ $aerosols = "spla" ]]; then
849      echo "Your aerosol choice is <spla>, so you need ERA 10m-winds interpolated on LMDZ grid. Use script era2gcm_uv10m.sh"
850    fi
851  else
852    echo "Si tout se passe bien, vous avez initialisé et lancé automatiquement la simulation."
853    echo "Le job qui a été lancé se trouve sur $SIMRUNTOPDIR/tmp_$SIM"
854  fi
855}
856
857#---------------------------------------------------------------------------
858function setup_and_load_lmdz_env() {
859#---------------------------------------------------------------------------
860  if [[ ! -f .lmdz_setup_root_dir ]]; then echo "STOP: setup.sh is not located in the root dir ??!!"; exit 1; fi
861  # sed root_dir in lmdz_env.sh
862  sed -i'' "s<root_dir=.*<root_dir=$local_d<" lmdz_env.sh
863
864  # Set up the appropriate environment
865  source lmdz_env.sh
866}
867
868local_d=$(pwd)
869
870setup_and_load_lmdz_env
871load_install_lib
872define_expert_options
873set_default_params
874read_cmdline_args "$@"
875ensure_correct_option_combinations
876install_model
877setup_def
878setup_ce0l
879setup_simu
880fetch_simu_init_files
881run_sim_or_init
882message_post_submit
Note: See TracBrowser for help on using the repository browser.