source: BOL/script_install/install_lmdz.sh @ 5529

Last change on this file since 5529 was 5529, checked in by fhourdin, 9 days ago

Modifications gfortran9/makelmdz

  • Property svn:executable set to *
File size: 49.1 KB
Line 
1#!/bin/bash
2set -eu  # error on command failure, and on unset variables
3export LC_ALL=# standardize awk format
4
5###########################################################################
6# Author : Laurent Fairhead et Frédéric Hourdin
7# Usage  : install_lmdz.sh -help
8#
9# bash installation script of the LMDZ model on different computer types :
10# Linux PC, "mesocentre" (IPSL-UPMC, IPSL-X), super-computer (IDRIS)
11#
12# The model is downloaded in the following directory tree
13# $MODEL/modipsl/modeles/...
14# using the "modipsl" infrastructure created by the "IPSL"
15# for coupled (atmosphere/ocean/vegetation/chemistry) climate modeling
16# activities.
17# Here we only download atmospheric (LMDZ) and vegetation (ORCHIDEE)
18# components.
19#
20# The sources of the models can be found in the "modeles" directory.
21# In the present case, LMDZ, ORCHIDEE, and IOIPSL or XIOS (handling of
22# input-outputs using the NetCDF library).
23#
24# The script downloads various source files (including a version of NetCDF)
25# and utilities, compiles the model, and runs a test simulation in a
26# minimal configuration.
27#
28# Prerequisites : gfortran, bash or ksh, wget, gunzip, tar, ...
29#
30# Modif 18/11/2011
31#    changes for option real 8.
32#      We compile with -r8 (or equivalent) and -DNC_DOUBLE for the GCM
33#      but with -r4 for netcdf. Variable real must be set to
34#      r4 or r8 at the beginning of the script below.
35#
36###########################################################################
37
38### Functions
39
40#-----------------------------------------------------------------------------
41function myget {
42#-----------------------------------------------------------------------------
43  # Get a file from LMDZ repository, make it executable if .(ba)sh
44  local url=$1
45
46  local filename
47  filename=$(basename "$url")
48
49    # Path on local computer where to look for the datafile
50    if [ -f /u/lmdz/WWW/LMDZ/pub/$1 ] ; then
51        \cp -f -p /u/lmdz/WWW/LMDZ/pub/$1 .
52    elif [ -f $save_pub_dir/$1 ] ; then
53        \cp -f -p $save_pub_dir/$1 .
54    else
55        echo wget --no-check-certificate -nv "http://lmdz.lmd.jussieu.fr/pub/$url"
56        wget --no-check-certificate -nv "http://lmdz.lmd.jussieu.fr/pub/$url"
57        if [ $save_pub = 1 ] ; then # saving wget files on $save_pub_dir
58            dir=$save_pub_dir/`dirname $1` ; mkdir -p $dir
59            cp -r `basename $1` $dir
60        fi
61    fi
62
63  if [[ $filename =~ .*\.(ba)?sh ]]; then chmod +x "$filename"; fi
64}
65
66#-----------------------------------------------------------------------------
67function do_compile_sh {
68#-----------------------------------------------------------------------------
69  local component=$1
70  local command=$2
71  local log
72
73  log="$(pwd)/$component.log"
74  printf "#!/bin/bash\n%s\n" "$command" > compile.sh
75  echo "Compiling $component using $command (log: $log) $(date)"
76    chmod +x ./compile.sh
77    if ! ./compile.sh &> "$log"; then
78        echo "STOP: $component compilation failed, exiting"; exit 1
79    fi
80    echo "Finished $component compilation $(date)"
81}
82
83#-----------------------------------------------------------------------------
84function get_svn_branch {
85#-----------------------------------------------------------------------------
86  local url=$1
87  local rev=$2
88  local res
89
90  res=$(svn log -v -q "$url" -r "$rev" -l 1 | cut -d "/" -f -4)
91  if echo "$res" | grep -q "/trunk/"; then
92    res=$(echo "$res" | grep "/trunk/" | head -1 | cut -d "/" -f 2-3)
93  elif echo "$res" | grep -q "/branches/"; then
94    res=$(echo "$res" | grep "/branches/" | head -1 | cut -d "/" -f 2-4)
95  else
96    echo "Could not determine svn branch for $url, r=$rev"
97  fi
98  echo "$res"
99}
100
101#-----------------------------------------------------------------------------
102function set_default_params {
103#-----------------------------------------------------------------------------
104    # Valeur par défaut des parametres
105    svn_lmdz=""
106    version="20241018.trunk"
107
108    netcdf=1
109    bench=1
110    SCM=0
111    veget="none"
112    grid_resolution="32x32x39"
113    benchphysiq=""
114    compphysiq="lmd"
115    is_1D="n"
116    fortran_file="gcm"
117
118    parallel="none"
119    trusting="testing"
120    MODEL=""
121
122    with_xios=0
123    opt_makelmdz_xios=""
124
125    icolmdz=0
126    dynamico_commit="11001689"
127
128    rad="rrtm"
129
130    compile_with_fcm=1
131
132    cosp="none"
133    aerosols=0
134    strataer=0
135    inlandsis=0
136
137    make_j=8
138    clean_install=1
139    local="$(pwd)"
140
141    # Check if on a Mac /!\ Probably doesn't work anymore, to fix one day...
142    if [[ $(uname) = "Darwin" ]]; then
143        export MAKE="make"
144    fi
145
146    optim_flag="-prod"
147    arch="local"
148
149    arch="local-gfortran"
150    arch_dir=""
151
152    jobcmd=""
153    verbose=0
154
155    save_pub=0
156    save_pub_dir=$HOME/LMDZ/pub
157}
158
159#-----------------------------------------------------------------------------
160function read_cmdline_args {
161#-----------------------------------------------------------------------------
162    while (($# > 0)); do
163        case $1 in
164            "-v") version=$2; shift 2 ;;
165            "-r") svn_lmdz=$2; shift 2 ;;
166            "-d") grid_resolution=$2; shift 2 ;;
167            "-unstable") trusting="unstable"; shift;;
168            "-cosp") cosp=$2
169                     case $cosp in
170                         "none"|"v1"|"v2") cosp=$2; shift 2 ;;
171                         *) echo "Only none v1 v2 for cosp option"; exit 1
172                     esac;;
173            "-nofcm") compile_with_fcm=0; echo "This option will be reactivated soon (promesse du 8dec2022)"; exit 1;  shift;;
174            "-SCM") SCM=1; shift;;
175            "-rad") rad=$2
176                    case $rad in
177                        "oldrad"|"rrtm"|"ecrad") rad=$2; shift 2 ;;
178                        *) echo "Only oldrad rrtm ecrad for rad option"; exit 1
179                    esac;;
180            "-parallel") parallel=$2
181                         case $parallel in
182                             "none"|"mpi"|"omp"|"mpi_omp") parallel=$2; shift 2 ;;
183                             *) echo "Only none mpi omp mpi_omp for the parallel option"; exit 1
184                         esac;;
185            "-bench") bench=$2; shift 2 ;;
186            "-debug") optim_flag="-debug"; shift;;
187            "-name") MODEL=$2; shift 2 ;;
188            "-netcdf") netcdf=$2; shift 2 ;;
189            "-benchphysiq") benchphysiq=$2; shift 2 ;;
190            "-compilephysiq") compphysiq=$2; shift 2 ;;
191            "-xios") with_xios=1; shift;;
192            "-arch") arch=$2; shift 2 ;;
193            "-arch_dir") arch_dir=$2; shift 2 ;;
194            "-veget") veget=$2; shift 2 ;;
195            "-spla") aerosols=1; shift ;;
196            "-strataer") strataer=1; shift ;;
197            "-inlandsis") inlandsis=1; shift ;;
198            "-make_j") make_j=$2; shift 2 ;;
199            "-jobcmd") jobcmd=$2; shift 2 ;;
200            "-noclean") clean_install=0; shift ;;
201            "-icolmdz") icolmdz=1; shift ;;
202            "-verbose") verbose=1; shift ;;
203            "-save_pub") save_pub=1 ; shift ;;
204            "-save_pub_dir") save_pub_dir=$2 ; shift 2 ;;
205            "-h" | *) cat <<........fin
206        $0 [ -v version ] [ -r svn_release ]
207               [ -parallel PARA ] [ -d GRID_RESOLUTION ] [ -bench 0/1 ]
208               [-name LOCAL_MODEL_NAME] [-rad RADIATIF]
209
210        -v       "version" like 20150828.trunk, see http://www.lmd.jussieu.fr/~lmdz/Distrib/LISMOI.trunk (default <$version>)
211
212        -r       "svn_release" : either the svn release number or "last" (default <$svn_lmdz>)
213
214        -parallel parallel support: mpi, omp, mpi_omp (mpi with openMP) or none (default: <$parallel>)
215
216        -d        "grid resolution": should be among the available benchs if -bench 1 (valid values: 48x36x19, 48x36x39) (default : <$grid_resolution>)
217
218        -bench     activating the bench or not (0/1) (default: <$bench>)
219
220        -unstable  use unstable tar instead of testing
221
222        -name      name of the folder to install to (default <$MODEL>)
223
224        -netcdf    0, 1 or PATH. 0: do not download NetCDF, look for it in standard locations (/usr);  1: download and compile NetCDF; PATH: full path to an existing installed NetCDF library (default: <$netcdf>)
225
226        -xios      use (download and compile) the XIOS library (will compile the parallel NetCDF4-HDF5 library) (requires to also have -parallel mpi_omp)
227
228        -cosp       to run with cospv1 or cospv2 [none/v1/v2] (default <$cosp>)
229
230        -rad        radiative code: oldrad, rrtm or ecrad (default <$rad>)
231
232        -nofcm      to compile without fcm
233
234        -SCM        install 1D version automatically
235
236        -debug      compile everything in debug mode
237
238        -benchphysiq   to choose which physics.def package to use in the bench (default <$benchphysiq>)
239
240        -compilephysiq   physics to compile the model with (default <$compphysiq>)
241
242        -veget      surface/vegetation scheme treatment controlled by the single variable veget which can have the following values: none: bucket scheme (default); CMIP6 | veget2.0: orchidee version used in CMIP exercise, rev 5661; veget2.2: orchidee branch 2.2, rev 8529 (bundled); number: orchidee version number  [only orch>2.0] (default $veget)
243
244        -spla       activate interactive aerosols
245
246        -inlandsis  activate new snow scheme
247
248        -arch       name of the arch to use (default <$arch>)
249
250        -arch_dir   where to find the arch files (default <$arch_dir>)
251
252        -make_j     number of processes to parallelize installations (default <$make_j>)
253
254        -jobcmd     command prepended to fcm compile jobs, e.g. "srun" (default <$jobcmd>)
255
256        -save_pub   Saves files from local mirror of http://lmdz.lmd.jussieu.fr/pub
257
258        -save_pub_dir  for saving/geting files from local mirror of http://lmdz.lmd.jussieu.fr/pub
259
260        -noclean    will only download necessary files (but no thorough check is made on the integrity of existing files), and will recompile everything (very quick if it's already been compiled before)
261
262        -icolmdz    to compile the icolmdz executable as well as the lonlat one
263
264        -verbose    to print every executed command
265
266........fin
267                  exit 0;;
268        esac
269    done
270
271    # Isotopes : Compile and run with isotopes if lmdz_phys="lmdiso" in main.sh
272    if [[ $compphysiq = "lmdiso" ]]; then isotopes=1; else isotopes=0; fi
273
274    # Option de compilation pour Cosp
275    case $cosp in
276        v1) opt_cosp="-cosp true";;
277        v2) opt_cosp="-cospv2 true";;
278        *) opt_cosp=""
279    esac
280
281    #Define veget-related suffix for gcm name
282    if [[ $veget = 'none' ]]; then
283        suff_orc=''
284    else
285        suff_orc='_orch'
286    fi
287
288
289    if [[ $parallel = "none" ]]; then
290        suff_para='_seq'
291    else
292        suff_para='_para_mem'
293    fi
294
295    if [[ $with_xios = 1 ]]; then opt_makelmdz_xios="-io xios"; fi
296
297    if [[ $aerosols = 1 ]]; then
298      opt_aer="-dust true"; suff_aer="_spla"
299    else
300      opt_aer=""; suff_aer=""
301    fi
302
303    if [[ $strataer = 1 ]]; then
304      opt_strataer="-strataer true"
305    else
306      opt_strataer=""
307    fi
308
309    if [[ $inlandsis = 1 ]]; then
310       opt_inlandsis="-inlandsis true"
311    else
312       opt_inlandsis=""
313    fi
314
315    if [[ $isotopes = 1 ]]; then
316      opt_isotopes="-isotopes true"; suff_iso="_iso"
317    else
318      opt_isotopes="" ; suff_iso=""
319    fi
320
321    # set default arch if parallel
322    if [[ $arch = "local-gfortran" && $parallel != "none" ]]; then
323      arch="local-gfortran-parallel"
324      echo "Switching default arch to $arch"
325    fi
326
327    # Name of the model's folder. The convention taken here is that models that requires different compilation sources should have a different names.
328    local xios_name=""
329    if [[ $with_xios = 1 ]]; then xios_name="XIOS"; fi
330    if [[ $MODEL = "" ]]; then MODEL="./LMDZ$version${svn_lmdz}OR$veget$xios_name"; fi
331
332    if [[ $arch_dir = "" ]]; then
333      arch_dir="$MODEL/modipsl/config/IPSLCM7/ARCH/";
334    elif ! readlink -fe "$arch_dir" >/dev/null; then
335      echo "STOP: no arch dir <$arch_dir>"; exit 1
336    fi
337
338    if ! (echo "$grid_resolution" | grep -q "x"); then
339      is_1D="y"
340      fortran_file="lmdz1d"
341    fi
342
343    if [[ $verbose = 1 ]]; then set -vx; fi
344}
345
346#-----------------------------------------------------------------------------
347function ensure_correct_option_combinations {
348#-----------------------------------------------------------------------------
349    # Check on veget version
350    if [[ $veget != 'none' && $veget != "CMIP6" && $veget != "orch2.0" && $veget != "orch2.2" ]]; then
351        re='^[0-9]+$'
352        if ! [[ $veget =~ $re ]]; then
353            echo 'Valeur de l option veget non valable'; exit 1
354        fi
355    fi
356
357    ## if compiling icolmdz, XIOS must be set
358    if [[ $icolmdz = 1 && $with_xios = 0 ]]; then
359      echo "Error, you must set -xios to compile the icolmdz executable"; exit 1
360    fi
361    ## if also compiling XIOS, parallel must be mpi_omp
362    if [[ $with_xios = 1 && $parallel != "mpi_omp" ]]; then
363        echo "Error, you must set -parallel mpi_omp if you want XIOS"; exit 1
364    fi
365
366    if [[ $cosp = "v2" && $with_xios = 0 ]]; then
367        echo "Error, Cospv2 cannot run without Xios"; exit 1
368    fi
369
370    # STOP if trying to use both ORCHIDEE and Isotopes :
371    if [[ $isotopes = 1 && $veget != "none" ]]; then
372      echo "STOP: You cannot run LMDZ with ORCHIDEE and ISOtopes at the same time"; exit 1
373    fi
374
375    # STOP if trying to use both SPLA and Isotopes :
376    if [[ $isotopes = 1 && $aerosols = 1 ]]; then
377      echo "STOP: You cannot run LMDZ with Isotopes and aerosols=spla at the same time"; exit 1
378    fi
379
380    # (Temporary) STOP if trying to use Isotopes with XIOS :
381    if [[ $isotopes = 1 && $with_xios = 1 ]]; then
382      echo "STOP: Isotopes cannont yet be run with XIOS"; exit 1
383    fi
384
385    if [[ $aerosols = 1 && $rad != "rrtm" ]]; then
386      echo "STOP: For the time being, <aerosols=spla> requires <rad=rrtm>"; exit 1
387    fi
388}
389
390#-----------------------------------------------------------------------------
391function check_available_software {
392#-----------------------------------------------------------------------------
393    local required_soft=("wget" "tar" "gzip" "make" "gcc" "cmake" "m4" "c++")
394    echo "Checking if required software is available (${required_soft[*]})"
395    for logiciel in "${required_soft[@]}"; do
396        if [[ $(which "$logiciel") = "" ]]; then
397            echo "You must first install $logiciel on your system"; exit 1
398        fi
399    done
400}
401
402#-----------------------------------------------------------------------------
403function download_modipsl_tar {
404#-----------------------------------------------------------------------------
405    if [[ $clean_install = 1 && -d $MODEL ]]; then
406      local ans
407      echo "$MODEL already exists. Do you want to erase it and proceed (Y/n) ? You can also rerun with \`-noclean\` to only recompile the necessary components."
408      read -r ans
409      ans="${ans,,}"  # to lowercase
410      if [[ $ans != "y" ]]; then exit 0; fi
411      rm -rf "$MODEL"
412    fi
413
414    mkdir -p "$MODEL"
415    MODEL=$(readlink -e -f "$MODEL"); echo "MODEL: $MODEL"  # absolute path
416    if [[ ! -d "$MODEL/modipsl" ]]; then
417        echo "Downloading a slightly modified version of modipsl+LMDZ"
418        cd "$MODEL"
419        getlog="$(pwd)/get.log"
420        echo "logfile : $getlog"
421        set +e; myget "src_archives/$trusting/modipsl.$version.tar.gz" &>> "$getlog"; set -e
422        if [[ ! -f "modipsl.$version.tar.gz" ]]; then
423          echo "STOP: failed to download modipsl. $getlog: <$(tail "$getlog")>"; exit 1
424        fi
425        echo "install_lmdz.sh wget_OK $(date)"
426
427        gunzip "modipsl.$version.tar.gz" &>> get.log
428        tar xf "modipsl.$version.tar" &>> get.log
429        rm "modipsl.$version.tar"
430    fi
431}
432
433#-----------------------------------------------------------------------------
434function init_arch {
435#-----------------------------------------------------------------------------
436    cd "$local"
437    set +e; arch_dir=$(readlink -f "$arch_dir"); set -e  # full path. readlink must be called *after* the path is created
438
439    # Check where default fcm, path, env are located - by default in $arch_path, instead in $MODEL/modipsl/modeles/LMDZ/arch/
440    local i fcm_path path_path env_path
441    for i in "path" "fcm" "env"; do
442      local varname=${i}_path
443      if [[ -f $arch_dir/arch-$arch.$i ]]; then
444          declare $varname="$arch_dir/arch-$arch.$i"
445      else
446          declare $varname="$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.$i"
447          if [[ ! -f ${!varname} ]]; then
448              echo "STOP: no ${!varname}"; exit 1
449          fi
450      fi
451    done
452
453    default_fcm_path=$fcm_path
454    default_path_path=$path_path
455    default_env_path=$env_path
456
457    # check compiler
458    compiler=$(< "$default_fcm_path" grep "%COMPILER" | sed -e "s/%COMPILER\s*//")
459
460    # ------------------------------------------------------------------------------------------
461    # Specific problem with fortran version changes
462    # Introduit le 1er fevrier 2025 par FH. A retravailler
463    # Could be generaized to download a arch tar file if the arch is not available in the current
464    # distribution of LMDZ
465    # ------------------------------------------------------------------------------------------
466    echo QUOI $compiler
467    if [ $compiler = gfortran -o $compiler = mpif90 ] ; then
468       echo QUOI on est la
469       if [ $( gfortran --version | head -1  | awk ' { print $NF } ' | cut -d. -f1 ) -le 9 ] ; then
470          echo QUOI faire qqchose
471          arch_dir=$PWD/arch
472          if [ $parallel = none ] ; then
473             arch=local-gfortran9
474          else
475             arch=local-gfortran9-parallel
476          fi
477          if [ ! -f $arch_dir/arch-local-gfortran9.fcm ] ; then
478             wget http://lmdz.lmd.jussieu.fr/pub/src_archives/misc/arch/LMDZ/arch-local-gfortran9.tar
479             tar xvf arch-local-gfortran9.tar
480          fi
481          default_fcm_path=$arch_dir/arch-${arch}.fcm
482          default_path_path=$arch_dir/arch-${arch}.path
483          default_env_path=$arch_dir/arch-${arch}.env
484       fi
485    fi
486    # ------------------------------------------------------------------------------------------
487
488    # load env
489    if [[ -f $default_env_path ]]; then source "$default_env_path"; fi
490
491    local mpi_file  # can't be done before as it depends on sourcing the env
492    if [[ $parallel != "none" ]]; then
493      if [[ $(which "mpif90") = "" ]]; then
494        echo "STOP: parallel=$parallel but <mpif90> could not be found"; exit 1
495      fi
496      mpi_file=$(readlink -e -f "$(which mpif90)")
497      path_mpi=$(dirname "$mpi_file")
498      root_mpi=$(dirname "$path_mpi")
499    fi
500}
501
502#-----------------------------------------------------------------------------
503function check_compiler {
504#-----------------------------------------------------------------------------
505  # Must be called after init_arch to know which compiler to use
506    cat <<eod > tt.f90
507print*,'coucou'
508end
509eod
510    $compiler tt.f90 || a.out
511    ./a.out >| tt
512    if [[ $(< tt sed -e 's/ //g' ) != "coucou" ]]; then
513        echo "problem installing with compiler $compiler"; exit 1
514    fi
515    rm tt a.out tt.f90
516}
517
518#-----------------------------------------------------------------------------
519function install_arch {
520#-----------------------------------------------------------------------------
521    local component=$1
522
523    # Use same .env for all components (for module compatibility)
524    cp -f "$default_env_path" "$MODEL/modipsl/modeles/$component/arch" &> /dev/null || true  # allow failure if we're copying the file to itself
525
526    # Use local .path and .fcm if available, otherwise default
527    if [[ ! -f "$MODEL/modipsl/modeles/$component/arch/arch-$arch.path" ]]; then
528        cp -f "$default_path_path" "$MODEL/modipsl/modeles/$component/arch"
529
530        if [[ $component = "ORCHIDEE" ]]; then
531            if [[ $orcbranch = "/tags/ORCHIDEE_2_0/ORCHIDEE" ]]; then  # 2.0 and before have a different fcm convention
532                sed -i'' -e "s/-I//" -e "s/-L//" "$MODEL/modipsl/modeles/ORCHIDEE/arch/arch-$arch.path"  # /!\ we only replace first occurence on purpose
533            fi
534            if [[ $veget = "CMIP6" ]]; then  # bundled version has a different fcm convention
535                sed -i'' -e "s/-I//" -e "s/-L//" "$MODEL/modipsl/modeles/ORCHIDEE/arch/arch-$arch.path"  # /!\ we only replace first occurence on purpose
536            fi
537        fi
538    fi
539    if [[ ! -f "$MODEL/modipsl/modeles/$component/arch/arch-$arch.fcm" ]]; then
540        cp -f "$default_fcm_path"  "$MODEL/modipsl/modeles/$component/arch"
541
542        if [[ $component = "XIOS" ]]; then
543            # Adapt for XIOS, which uses different naming conventions
544            sed -i'' -e "s/%COMPILER/%FCOMPILER/" -e "s/%LINK/%LINKER/" -e "s/-fdefault-real-8//" "$MODEL/modipsl/modeles/XIOS/arch/arch-$arch.fcm"
545        fi
546    fi
547}
548
549#-----------------------------------------------------------------------------
550function install_netcdf {
551#-----------------------------------------------------------------------------
552    echo "Installing Netcdf"
553    local ncdf_compiler="$compiler"
554
555    if [[ $netcdf = 0 ]]; then
556        ncdfdir=$(nf-config --prefix)
557    else
558        cd "$MODEL"
559
560        # Convert non-basic compiler
561        case $compiler in
562            mpif90) ncdf_compiler=$($compiler --version | head -n 1 | cut -d " " -f -1)
563        esac
564
565        case $ncdf_compiler in
566            gfortran | GNU) ncdf_compiler="gfortran"; opt1="-compiler gnu"; opt2="-CC gcc -FC gfortran -CXX g++";;
567            *)      echo "unexpected compiler $ncdf_compiler for netcdf"; exit 1
568        esac
569
570        case $with_xios in
571            0) script_install_netcdf="install_netcdf4_hdf5_seq.bash"
572               ncdfdir="netcdf4_hdf5_seq"
573               opt_="$opt1";;
574            1) script_install_netcdf="install_netcdf4_hdf5.bash"
575               ncdfdir="netcdf4_hdf5"
576               opt_="$opt2 -MPI $root_mpi";;
577            *) echo "with_xios=$with_xios, should be 0 or 1"; exit 1
578        esac
579        if [[ $netcdf = 1 ]]; then
580           ncdfdir="$MODEL/$ncdfdir"
581        else
582           mkdir -p "$netcdf"; ncdfdir="$netcdf/$ncdfdir"
583        fi
584
585        echo "Repertoire netcdf $ncdfdir"
586        if [[ ! -d $ncdfdir ]]; then
587            netcdflog=$(pwd)/netcdf.log
588            echo "----------------------------------------------------------"
589            echo "Compiling the Netcdf library"
590            echo "----------------------------------------------------------"
591            echo "log file : $netcdflog"
592            myget script_install/$script_install_netcdf &>> "$netcdflog"
593            chmod u=rwx $script_install_netcdf
594            # shellcheck disable=SC2086
595            ./$script_install_netcdf -prefix "$ncdfdir" $opt_ &>> "$netcdflog"
596        fi
597
598        # Add to path
599        export PATH="$ncdfdir/bin:$PATH"
600        echo "Bin PATH" $PATH
601
602        #----------------------------------------------------------------------------
603        # LF rajout d'une verrue, pour une raison non encore expliquee,
604        # la librairie est parfois rangée dans lib64 et non dans lib
605        # par certains compilateurs
606        if [[ ! -e lib && -d lib64 ]]; then ln -s lib64 lib; fi
607        #----------------------------------------------------------------------------
608
609        echo "install_lmdz.sh netcdf_OK $(date)"
610    fi
611
612    # add exported netcdf path to .env if not already the case. This ensures we always use the same netcdf library as during the first install.
613    if [[ $(nf-config --prefix) = "" || $(nc-config --prefix) = "" ]]; then
614      echo "STOP: missing nf-config or nc-config in \$PATH"; exit 1
615    fi
616    local env_msg env_msg_tail
617    env_msg_tail="# netcdf bin path auto-added by install_lmdz.sh"
618    env_msg="export PATH=\"$PATH:\$PATH\" $env_msg_tail"
619    if ! < "$default_env_path" grep -q "$env_msg_tail"; then
620      sed -i "1s@^@$env_msg\n@" "$default_env_path"
621    fi
622
623    # Test if netcdf works fine on a simple program
624    cat >test_netcdf90.f90 <<EOF
625    use netcdf
626    print *, "NetCDF library version: ", nf90_inq_libvers()
627    end
628EOF
629
630    if $ncdf_compiler -I"$ncdfdir"/include test_netcdf90.f90 -L"$ncdfdir"/lib -lnetcdff -lnetcdf -Wl,-rpath="$ncdfdir"/lib && ./a.out; then
631        rm test_netcdf90.f90 a.out
632    else
633        cat <<EOF
634Failed test program using NetCDF-Fortran. You can:
635- check that you have NetCDF-Fortran installed in your system
636- or specify an installation directory with option -netcdf of install_lmdz.sh
637- or download and compile NetCDF-Fortran with option -netcdf 1 of install_lmdz.sh
638EOF
639        exit 1
640    fi
641
642    # Compile NetCDF95
643    cd "$MODEL/modipsl/modeles/LMD"*
644    echo "Installing NetCDF95"
645    cd "$MODEL"
646    if [[ ! -d "NetCDF95-0.3" ]]; then
647        myget src_archives/netcdf/NetCDF95-0.3.tar.gz
648        tar -xf NetCDF95-0.3.tar.gz
649        rm NetCDF95-0.3.tar.gz
650        cd NetCDF95-0.3
651        mkdir -p build && cd build
652        netCDF_INCLUDE_DIR=$(nc-config --includedir) netCDF_LIBRARY=$(nc-config --libdir) cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$ncdfdir" -DCMAKE_INSTALL_PREFIX="$MODEL/modipsl"
653        make install
654    fi
655}
656
657#-----------------------------------------------------------------------------
658function install_IOIPSL {
659#-----------------------------------------------------------------------------
660    install_arch "IOIPSL"
661
662    cd "$MODEL/modipsl/modeles/IOIPSL"
663    echo "Compiling IOIPSL, the interface library with Netcdf $(date)"
664    # in case ksh is not installed on the pc
665    if [[ ! -x /bin/ksh ]]; then
666      sed -i'' 's/ksh/bash/' ins_m_prec
667      sed -i'' 's/ksh/bash/' rebuild/rebuild
668    fi
669    do_compile_sh "IOIPSL" "$jobcmd ./makeioipsl_fcm -j $make_j -arch $arch $optim_flag"
670
671    # Link to modipsl/bin
672    cp -f bin/* ../../bin
673
674    echo "IOIPSL compiled $(date)"
675}
676
677#-----------------------------------------------------------------------------
678function install_XIOS {
679#-----------------------------------------------------------------------------
680    if [[ $with_xios = 1 ]]; then
681        cd "$MODEL/modipsl/modeles"
682        xioslog="$(pwd)/XIOS/xios.log"
683        echo "##########################################################"
684        echo "Compiling XIOS (log $xioslog) $(date)"
685        echo "##########################################################"
686
687        # Download XIOS
688        local xios_http="http://forge.ipsl.fr/ioserver/svn/XIOS2/branches/xios-2.6"
689        local xios_rev="2568"
690       
691        cd "$MODEL/modipsl/modeles"
692        set +e; svn co -r $xios_rev $xios_http XIOS; set -e
693
694        cd XIOS
695
696        install_arch "XIOS"
697        do_compile_sh "XIOS" "$jobcmd ./make_xios --job $make_j --arch $arch"
698    fi
699}
700
701#-----------------------------------------------------------------------------
702function get_orchidee_version {  # Set / Check ORCHIDEE version
703#-----------------------------------------------------------------------------
704    echo "Checking Orchidee source version"
705    local fetch_rev=""
706    orcbranch=""
707    case $veget in
708        "none") fcm_veget_version="false";;
709#        "orch2.0" | "CMIP6") fcm_veget_version=orchidee2.0; fetch_rev=7906;;  # in previous tar we used 6592 but with some modifications to xios_orchidee.f90, which got integrated in 7906
710        "orch2.0" | "CMIP6") fcm_veget_version=orchidee2.0;;  # in previous tar we used 6592 but with some modifications to xios_orchidee.f90, which got integrated in 7906
711        "orch2.2") fcm_veget_version=orchidee2.1; orcbranch="/branches/ORCHIDEE_2_2/ORCHIDEE" ;; # the bundled version
712        *) fetch_rev=$veget; fcm_veget_version=orchidee2.1;;  # /!\ arbitary rev only works for orch>=2.1
713          # /!\ Note: for orch>=4, we should be using fcm_vegt_version="orchideetrunk". Below copied comment by Adriana in LMDZ_Setup docs:
714#          Avec orchidee2.1, on va compiler LMDZ avec le cle cpp ORCHIDEE_NOLIC. On ne va donc pas prendre en compte les avancements fait dans le trunk qui permet que les fractions lic soitent traité par ORCHIDEE. Ca marche très bien sans cela puisque dans tout façon c'est en cours de développement et ce n'est pas activé par default. En fait on devrait compiler le trunk avec -v orchideetrunk mais avec landice_opt>2, ce qui ne change rien par rapport à orchidee2.1
715#Si on voudrait activer landice_opt=2 , il faut compiler avec -v orchideetrunk.
716
717    esac
718
719    if [[ -n $fetch_rev ]]; then
720        echo "IF YOU INSTALL ORCHIDEE THE VERY FIRST TIME, ASK for PASSWORD at orchidee-help@listes.ipsl.fr"
721        local curr_rev
722        curr_rev=$(svn info "$MODEL/modipsl/modeles/ORCHIDEE" | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
723        orcbranch=$(svn log -v -q svn://forge.ipsl.fr/orchidee/ -r "$fetch_rev" | grep ORCHIDEE | head -1 | sed -e 's:ORCHIDEE/.*$:ORCHIDEE:' | awk '{print $2}')
724        if [[ $fetch_rev != "$curr_rev" ]]; then
725            echo "Fetching orch $fetch_rev from the repository (curr: $curr_rev)"
726            echo "branch is $orcbranch"
727            if [[ $fetch_rev -lt 4465 ]]; then echo 'ORCHIDEE version must be >=4465, exiting'; exit 1; fi
728            cd "$MODEL/modipsl/modeles"
729            rm -rf ORCHIDEE
730            svn co -r "$fetch_rev" "svn://forge.ipsl.fr/orchidee/$orcbranch"
731            cd - > /dev/null
732        fi
733    fi
734
735    # Check parallel LMDZ+ORCH
736    if [[ (! $veget = "none") && $parallel = "none" && ($used_lmdz_rev -lt 4894) ]]; then
737        echo "LMDZ revision must be >=4894 for orchidee without parallel support. Upgrade lmdz or use -parallel mpi_omp."; exit 1
738    fi
739}
740
741#-----------------------------------------------------------------------------
742function compile_orchidee {
743#-----------------------------------------------------------------------------
744    install_arch "ORCHIDEE"
745
746    if [[ $veget != "none" ]]; then
747        cd "$MODEL/modipsl/modeles/ORCHIDEE"
748
749        local xios_orchid
750        if [[ $with_xios = 1 ]]; then
751            xios_orchid="-xios";
752        else
753            xios_orchid="-noxios"
754        fi
755
756        if [[ $parallel != "none" && ! -d src_parallel ]]; then
757           echo "STOP: Orchidee version too old for parallel support"; exit 1
758        fi
759        do_compile_sh "ORCHIDEE" "$jobcmd ./makeorchidee_fcm -j $make_j $xios_orchid $optim_flag -parallel $parallel -arch $arch"
760    fi
761}
762
763#-----------------------------------------------------------------------------
764function get_lmdz_version {
765#-----------------------------------------------------------------------------
766    echo "Checking LMDZ source version"
767    LMDZPATH=$(readlink -e -f "$MODEL/modipsl/modeles/LMD"*)
768    cd "$LMDZPATH"
769
770    if [[ -n $svn_lmdz ]]; then
771        local curr_rev
772        curr_rev=$(svn info "$MODEL/modipsl/modeles/LMD"* | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
773        if [[ $svn_lmdz != "$curr_rev" ]]; then
774            local lmdzbranch
775            echo "Fetching LMDZ $svn_lmdz from the repository"
776#            lmdzbranch=$(get_svn_branch "https://svn.lmd.jussieu.fr/LMDZ" "$svn_lmdz")
777            lmdzbranch=$(get_svn_branch "http://svn.lmd.jussieu.fr/LMDZ" "$svn_lmdz")
778            echo "branch is $lmdzbranch"
779            cd "$MODEL/modipsl/modeles"
780            rm -rf LMD*
781#            svn co -r "$svn_lmdz" "https://svn.lmd.jussieu.fr/LMDZ/$lmdzbranch" LMDZ
782            svn co -r "$svn_lmdz" "http://svn.lmd.jussieu.fr/LMDZ/$lmdzbranch" LMDZ
783            cd - > /dev/null
784        fi
785        used_lmdz_rev=$svn_lmdz
786    else  # get svn from info
787        set +e; used_lmdz_rev=$(svn info | grep "Last Changed Rev" | cut -c 19-); set -e
788        if [[ -z $used_lmdz_rev ]]; then  # svn info failed
789            used_lmdz_rev=$(grep 'Revision: [0-9]' "$MODEL"/Read*.md | awk ' { print $2 } ' 2>/dev/null)
790            if [[ -z $used_lmdz_rev ]]; then echo "Could not determine lmdz version. This is likely an issue with the .tar itself, please report to LMDZ team."; exit 1; fi
791        fi
792    fi
793}
794
795#-----------------------------------------------------------------------------
796function compile_lmdz {
797#-----------------------------------------------------------------------------
798  if [[ $bench =   "tuto" ]]; then
799      echo No need to compile if installing TUTORIAL
800  else
801    install_arch "LMDZ"
802    cd "$LMDZPATH"
803
804    if [[ $used_lmdz_rev -le 4185 ]]; then
805        exe_name="bin/${fortran_file}_${grid_resolution}_phy${compphysiq}_${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
806    else
807        exe_name="bin/${fortran_file}_${grid_resolution}_phy${compphysiq}_${rad}${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
808    fi
809
810    local opt_rad
811    case $rad in
812        oldrad) iflag_rrtm=0; NSW=2; opt_rad="";;
813        rrtm)   iflag_rrtm=1; NSW=6
814            if [[ $used_lmdz_rev -le 4185 ]]; then
815                opt_rad="-rrtm true"
816            else
817                opt_rad="-rad rrtm"
818            fi;;
819        ecrad)  iflag_rrtm=2; NSW=6; opt_rad="-rad ecrad";;
820        *) echo "Only oldrad rrtm ecrad for rad option"; exit 1
821    esac
822    if [[ $used_lmdz_rev -le 4185 && $rad = "ecrad" ]]; then
823        echo "ecrad only available for LMDZ rev starting with 4186 "; exit 1
824    fi
825
826    # Compile
827    local makelmdz="makelmdz_fcm $optim_flag -arch $arch -j $make_j"
828    local para_compile_opt="-mem -parallel $parallel"; if [[ $parallel = "none" ]]; then para_compile_opt=""; fi
829    do_compile_sh "LMDZ" "$jobcmd ./$makelmdz $opt_rad $opt_cosp $opt_makelmdz_xios $opt_aer $opt_inlandsis $opt_strataer $opt_isotopes -p $compphysiq -d ${grid_resolution} -v $fcm_veget_version $para_compile_opt $fortran_file"
830
831    # Check executable
832    if [[ ! -f $exe_name ]]; then
833        echo "STOP: Compilation failed, can't find the executable"; exit 1
834    else
835        echo "Compilation successful, the executable is $exe_name $(date)"
836    fi
837  fi
838}
839
840#-----------------------------------------------------------------------------
841function get_dynamico_icosa_version {
842#-----------------------------------------------------------------------------
843  if [[ $icolmdz = 1 ]]; then
844    echo "Checking DYNAMICO source version"
845    cd "$MODEL/modipsl/modeles"
846    if [[ ! -d DYNAMICO ]]; then
847      git clone https://gitlab.in2p3.fr/ipsl/projets/dynamico/dynamico.git DYNAMICO
848    fi
849    cd DYNAMICO
850    git checkout master && git checkout $dynamico_commit
851    cd - > /dev/null
852
853    echo "Checking ICOSA_LMDZ source version"
854    if [[ ! -d ICOSA_LMDZ ]]; then
855      svn checkout http://svn.lmd.jussieu.fr/LMDZ/ICOSA_LMDZ
856    fi
857    cd ICOSA_LMDZ
858    svn up -r 5320
859  fi
860}
861
862#-----------------------------------------------------------------------------
863function compile_icolmdzor {
864#-----------------------------------------------------------------------------
865    if [[ $icolmdz = 1 ]]; then
866      install_arch "ICOSA_LMDZ"
867      local para_compile_opt="-parallel $parallel"; if [[ $parallel = "none" ]]; then para_compile_opt=""; fi
868
869      # LMDZ physics package library already available in LMDZ/config/lib directory
870
871      # Compile DYNAMICO
872      cd "$MODEL/modipsl/modeles/DYNAMICO"
873
874      # Need to get rather than install the archs as DYNAMICO uses FCMv2 that does not understand the makefile syntax ($shell)
875      cd arch
876      wget "http://lmdz.lmd.jussieu.fr/pub/src_archives/misc/arch/DYNAMICO/arch-$arch.env"
877      wget "http://lmdz.lmd.jussieu.fr/pub/src_archives/misc/arch/DYNAMICO/arch-$arch.fcm"
878      wget "http://lmdz.lmd.jussieu.fr/pub/src_archives/misc/arch/DYNAMICO/arch-$arch.path"
879      cd ..
880
881      do_compile_sh "DYNAMICO" "$jobcmd ./make_icosa $optim_flag -arch $arch -job $make_j $para_compile_opt -external_ioipsl -with_xios"
882
883      # Compile icosa_lmdz
884      echo "Bin PATH before icosalmdz" "$PATH"
885      cd "$MODEL/modipsl/modeles/ICOSA_LMDZ"
886      do_compile_sh "ICOSA_LMDZ" "$jobcmd ./make_icosa_lmdz -arch $arch -j $make_j -nodeps -p lmd $optim_flag $para_compile_opt -with_orchidee"
887      echo "# Running environment for icosa_lmdz" > icosalmdz.env
888      echo "# "                                   >> icosalmdz.env
889      if [[ -v LD_LIBRARY_PATH ]]; then 
890        echo "export LD_LIBRARY_PATH=$ncdfdir/lib:$LD_LIBRARY_PATH" >> icosalmdz.env
891      else 
892        echo "export LD_LIBRARY_PATH=$ncdfdir/lib" >> icosalmdz.env
893      fi
894    fi
895}
896
897#-----------------------------------------------------------------------------
898function run_bench {
899#-----------------------------------------------------------------------------
900    local bench_cmd="./bench.sh"
901
902    cd "$MODEL/modipsl/modeles/LMDZ"*
903
904    if [[ $bench = "tuto" ]]; then
905        myget "Training/tutorial.tar"; tar xf tutorial.tar
906        echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
907        echo A directory $PWD/TUTORIAL
908        echo has been downloaded in which you can compile, intitialize
909        echo and then run a simulation with a zoomed grid by running ./init.sh
910        echo after modifying some input parameters in .def files in DEF/
911        echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
912    elif [[ $bench = 1 ]]; then
913      rm -rf "BENCH${grid_resolution}"
914      local bench=bench_lmdz_${grid_resolution}
915      if [[ $compphysiq = "lmdiso" ]]; then bench=bench_lmdz_iso_${grid_resolution}; fi
916
917      if [[ $is_1D = "y" ]] ; then  # 1D
918        myget "1D/1D.tar.gz"
919        mkdir -p "BENCH${grid_resolution}"
920        tar xf "1D.tar.gz" -C "BENCH${grid_resolution}" --strip-components=1
921        cd "BENCH${grid_resolution}"
922        # Below: ugly, but until we rewrite the 1D case...
923        sed -i'' -e "s:^listecas=.*$:listecas=ARMCU/REF:" -e "s:cd \$local/bin ; ./compile -L \$L:#cd \$local/bin ; ./compile -L \$L:" \
924            -e "s:./compile -L \$llm:#./compile -L \$llm:" -e "s:ln -sf \$bin/\\\\\${main}\${suffixe}.e .:ln -sf ../../../../../$exe_name \\\\\${main}\${suffixe}.e:" -e "s:gzip listing:cp listing restartphy.nc ../../../../; exit 0:" \
925            run.sh
926        cp "../$exe_name" bin/
927        bench_cmd="./run.sh -rad $rad"  # suppress ferret commands that launch after bench is "over"
928      else
929        myget "3DBenchs/$bench.tar.gz"
930        mkdir "BENCH${grid_resolution}"
931        tar xf "$bench.tar.gz" -C "BENCH${grid_resolution}" --strip-components=1
932
933        if [[ $cosp = "v1" || $cosp = "v2" ]]; then
934            cd "BENCH${grid_resolution}"
935            # copier les fichiers namelist input et output our COSP
936            cp ../DefLists/cosp*_input_nl.txt .
937            cp ../DefLists/cosp*_output_nl.txt .
938            # Activer la cles ok_cosp pour tourner avec COSP
939            sed -e 's@ok_cosp=n@ok_cosp=y@' config.def > tmp
940            \mv -f tmp config.def
941            cd ..
942        fi
943
944        if [[ -n "$benchphysiq" ]]; then
945            cd "BENCH${grid_resolution}"
946            if [[ -f "physiq.def_${benchphysiq}" ]]; then
947                cp "physiq.def_${benchphysiq}" benchphysiq.def
948                echo "using physiq.def_${benchphysiq}"
949            else
950                echo "using standard physiq.def"
951            fi
952            cd ..
953        else
954            echo "using standard physiq.def"
955        fi
956
957        if [[ $with_xios = 1 ]]; then
958            cd "BENCH${grid_resolution}"
959            cp ../DefLists/iodef.xml .
960            cp ../DefLists/context_lmdz.xml .
961            cp ../DefLists/field_def_lmdz.xml .
962            # A raffiner par la suite
963            echo "A FAIRE : Copier les *xml en fonction de l option cosp"
964            cp ../DefLists/field_def_cosp*.xml .
965            cp ../DefLists/file_def_hist*xml .
966            # adapt iodef.xml to use attached mode
967            sed -e 's@"using_server" type="bool">true@"using_server" type="bool">false@' \
968                iodef.xml > tmp
969            \mv -f tmp iodef.xml
970
971            # and convert all the enabled="_AUTO_" (for libIGCM) to enabled=.FALSE.
972            # except for histday
973            for histfile in file_def_hist*xml; do
974                if [[ "$histfile" = "file_def_histday_lmdz.xml" ]]; then
975                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".TRUE."@' \
976                        "$histfile" > tmp
977                    \mv -f tmp "$histfile"
978                    sed -e 's@output_level="_AUTO_"@output_level="5"@' "$histfile" \
979                        > tmp
980                    \mv -f tmp "$histfile"
981                    sed -e 's@compression_level="2"@compression_level="0"@' \
982                        "$histfile" > tmp
983                    \mv -f tmp "$histfile"
984                else
985                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
986                        "$histfile" > tmp
987                    \mv -f tmp "$histfile"
988                fi
989            done
990            # and add option "ok_all_xml=y" in config.def
991            echo "### XIOS outputs" >> config.def
992            echo 'ok_all_xml=.true.' >> config.def
993
994            #activer les sorties pour Cosp
995            if [[ "$cosp" = "v1" ]]; then
996                sed -i'' -e 's@enabled=".FALSE."@enabled=".TRUE."@' \
997                         -e 's@output_level="_AUTO_"@output_level="5"@' \
998                         -e 's@compression_level="2"@compression_level="0"@' \
999                         file_def_histdayCOSP_lmdz.xml
1000            fi
1001            if [[ "$cosp" = "v2" ]]; then
1002                sed -i'' -e 's@compression_level="2"@compression_level="0"@' file_def_histdayCOSPv2_lmdz.xml
1003                for type_ in hf day mth; do
1004                    file=file_def_hist${type_}COSP
1005                    sed -i'' -e 's@src="./'${file}'_lmdz.xml"@src="./'${file}'v2_lmdz.xml"@' context_lmdz.xml
1006                done
1007                sed -i'' -e 's@field_def_cosp1.xml@field_def_cospv2.xml@' field_def_lmdz.xml
1008            fi
1009
1010            cd ..
1011        fi
1012
1013        # Cas Bench avec ecrad
1014        if [[ $rad = "ecrad" ]]; then
1015            cd "BENCH${grid_resolution}"
1016            cp  ../DefLists/namelist_ecrad .
1017            cp -r ../libf/phylmd/ecrad/data .
1018            cd ..
1019        fi
1020
1021        # Adjusting bench physiq.def to radiative code chosen
1022        cd "BENCH${grid_resolution}"
1023        sed -e 's/iflag_rrtm=.*.$/iflag_rrtm='$iflag_rrtm'/' \
1024            -e 's/NSW=.*.$/NSW='$NSW'/' physiq.def > tmpdef
1025        \mv tmpdef physiq.def
1026        cd ..
1027
1028        cp "$exe_name" "BENCH${grid_resolution}/gcm.e"
1029        cd "BENCH${grid_resolution}"
1030
1031        if [[ ${parallel:0:3} = "mpi" ]]; then
1032            # Lancement avec deux procs mpi et 2 openMP
1033            echo "export OMP_STACKSIZE=800M" > bench.sh
1034            if [[ "${parallel:4:3}" = "omp" ]]; then
1035                echo "export OMP_NUM_THREADS=2" >> bench.sh
1036            fi
1037            if [[ $cosp = "v1" || $cosp = "v2" ]]; then
1038                echo "ulimit -s 200000" >> bench.sh
1039            else
1040                echo "ulimit -s unlimited" >> bench.sh
1041            fi
1042            if [[ $(which "mpirun") != "" ]]; then
1043                echo "mpirun -np 2 gcm.e &> listing" >> bench.sh
1044            elif grep -q "Adastra" /etc/motd; then
1045                local account
1046                account=$(/usr/sbin/my_project.py -l 2>&1 | head -1 | cut -d " " -f 3- | cut -c 5-)
1047                bench_cmd="srun --nodes=1 --ntasks=1 --cpus-per-task=2 --threads-per-core=2 --time=0:10:00 --constraint=GENOA --account=$account bash bench.sh"
1048                echo "./gcm.e &> listing" >> bench.sh
1049            else
1050                echo "Error: No command found to run parallel bench"; exit 1
1051            fi
1052            if [[ $(hostname | cut -c -6) = "spirit" ]]; then  # ulimit unlimited segfaults on Spirit
1053                sed -i'' "s/ulimit -s unlimited/ulimit -Ss 8000/" bench.sh
1054            fi
1055            # Add rebuild, using reb.sh if it is there
1056            cat <<EOF >> bench.sh
1057if [[ -f reb.sh ]]; then
1058  ./reb.sh histday; ./reb.sh histmth; ./reb.sh histhf;
1059  ./reb.sh histins; ./reb.sh stomate_history;
1060  ./reb.sh sechiba_history; ./reb.sh sechiba_out_2
1061fi
1062EOF
1063        else
1064            echo "./gcm.e &> listing" > bench.sh
1065        fi
1066        chmod +x bench.sh
1067        # Getting orchidee stuff
1068        if [[ $veget = 'CMIP6' || $veget = "orch2.0" ]]; then  # TODO once we have a 2.2 bench, add it here (or in planned separate bench script)
1069            echo 'myget 3DBenchs/BENCHCMIP6.tar.gz'
1070            myget 3DBenchs/BENCHCMIP6.tar.gz
1071            tar xvzf BENCHCMIP6.tar.gz
1072            sed -e "s:VEGET=n:VEGET=y:" config.def > tmp
1073            mv -f tmp config.def
1074            if [[ $with_xios = 1 ]]; then
1075                cp ../../ORCHIDEE/src_xml/context_orchidee.xml .
1076                echo '<context id="orchidee" src="./context_orchidee.xml"/>' > add.tmp
1077                cp ../../ORCHIDEE/src_xml/field_def_orchidee.xml .
1078                cp ../../ORCHIDEE/src_xml/file_def_orchidee.xml .
1079                cp ../../ORCHIDEE/src_xml/file_def_input_orchidee.xml .
1080                if [[ -f ../../ORCHIDEE/src_xml/context_input_orchidee.xml ]]; then
1081                       cp ../../ORCHIDEE/src_xml/context_input_orchidee.xml .
1082                       echo '<context id="orchidee" src="./context_input_orchidee.xml"/>' >> add.tmp
1083                fi
1084                sed -e '/id="LMDZ"/r add.tmp' iodef.xml > tmp
1085                mv tmp iodef.xml
1086                sed -e'{/sechiba1/ s/enabled="_AUTO_"/type="one_file" enabled=".TRUE."/}' \
1087                    file_def_orchidee.xml > tmp
1088                \mv -f tmp file_def_orchidee.xml
1089                sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
1090                    file_def_orchidee.xml > tmp
1091                \mv -f tmp file_def_orchidee.xml
1092                sed -e 's@output_level="_AUTO_"@output_level="1"@' \
1093                    file_def_orchidee.xml > tmp
1094                \mv -f tmp file_def_orchidee.xml
1095                sed -e 's@output_freq="_AUTO_"@output_freq="1d"@' \
1096                    file_def_orchidee.xml > tmp
1097                \mv -f tmp file_def_orchidee.xml
1098                sed -e 's@compression_level="4"@compression_level="0"@' \
1099                    file_def_orchidee.xml > tmp
1100                \mv -f tmp file_def_orchidee.xml
1101                sed -e 's@XIOS_ORCHIDEE_OK = n@XIOS_ORCHIDEE_OK = y@' \
1102                    orchidee.def > tmp
1103                \mv -f tmp orchidee.def
1104            fi
1105        fi
1106
1107        fi
1108
1109        if [[ -f ../arch.env ]]; then source ../arch.env; fi
1110
1111        echo "STARTING BENCH"
1112        date
1113        if (! $bench_cmd &> out.bench) || ! (tail -n 1 listing | grep "Everything is cool"); then
1114            tail listing
1115            echo "Bench FAILED, exiting"; exit 1
1116        fi
1117        date
1118        tail listing
1119    fi
1120
1121    # 1D case
1122    if [[ $SCM = 1 ]]; then
1123        cd "$MODEL"
1124        myget 1D/1D.tar.gz
1125        tar xf 1D.tar.gz
1126        cd 1D
1127        if [[ $rad = "oldrad" ]]; then
1128            sed -i'' -e 's/^rad=.*$/rad=oldrad/' run.sh
1129            sed -i'' -e 's/^rad=.*$/rad=oldrad/' bin/compile
1130        elif [[ $rad = ecrad ]] ; then
1131                         sed -i'' -e 's/^rad=.*$/rad=ecrad/' run.sh
1132                         sed -i'' -e 's/^rad=.*$/rad=ecrad/' bin/compile
1133                     fi
1134        sed -i -e "s:^fcm=0:fcm=1:" bin/compile
1135        echo "Running 1D/run.sh, log in $(pwd)/run1d.log"
1136        ./run.sh &> "$(pwd)/run1d.log"
1137    fi
1138}
1139
1140#-----------------------------------------------------------------------------
1141function run_bench_icosa {
1142#-----------------------------------------------------------------------------
1143    local bench_cmd="./bench.sh"
1144
1145    if [[ $icolmdz = 1 ]]; then
1146      cd "$MODEL/modipsl/modeles/ICOSA_LMDZ"
1147      if [[ $bench = 1 ]]; then
1148        namebench="bench_icolmdz_nbp10_79"
1149        rm -rf $namebench
1150        myget "3DBenchs/$namebench.tar.gz"
1151        mkdir -p $namebench
1152        tar xf "$namebench.tar.gz" -C "$namebench" --strip-components=1
1153        cd $namebench
1154        # copy executables
1155        if [[ ! -x ../bin/icosa_lmdz.exe ]]; then
1156          echo "STOP in icosa_lmdz bench, icosa_lmdz.exe executable not present"; exit 1
1157        fi
1158        cp ../bin/icosa_lmdz.exe .
1159         if [[ ! -x ../../XIOS/bin/xios_server.exe ]]; then
1160          echo "STOP in icosa_lmdz bench, XIOS executable not present"; exit 1
1161        fi
1162        cp ../../XIOS/bin/xios_server.exe .
1163        echo "STARTING ICOSA_LMDZ BENCH"
1164        date
1165        if (! $bench_cmd &> out.bench) || ! ( grep "Time elapsed" listing); then
1166            tail listing
1167            echo "ICOSA_LMDZ bench FAILED, exiting"; exit 1
1168        fi
1169        date
1170        echo "ICOSA_LMDZ bench finished"
1171      fi
1172    fi
1173
1174}
1175# If sourced: returns, else run setup
1176if [[ ! "${BASH_SOURCE[0]}" = "$0" ]]; then return 0; fi
1177
1178echo "install_lmdz.sh DEBUT $(date)"
1179
1180#-----------------------------------------------------------------------------
1181function wrapper_run_all() {
1182#-----------------------------------------------------------------------------
1183    # This is a wrapper to catch any "unexpected" error due to set -e and avoid abrupt stoppage which may confuse the user
1184    set_default_params
1185    read_cmdline_args "$@"
1186    ensure_correct_option_combinations
1187    download_modipsl_tar
1188    get_lmdz_version
1189    get_dynamico_icosa_version
1190    get_orchidee_version
1191    init_arch
1192    check_available_software
1193    check_compiler
1194    install_netcdf
1195    install_IOIPSL
1196    install_XIOS
1197    compile_orchidee
1198
1199    # ==========================================================
1200    # AJOUT TEMPOREAIRE LE 9 FEVRIER 2025 - FH (Horrible là)
1201    # Inutile de compiler LMDZ quand pas de bench
1202    if [ $bench = 1 ] ; then compile_lmdz ; fi
1203    # A nettoyer apres discussion et choix sur les arch
1204    # Conservation des arch ayant servi à la compilation de LMDZ
1205    for e in "fcm" "env" "path"; do
1206      archf=$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.$e
1207      archf_from_get=$arch_dir/arch-$arch.$e
1208      if [ ! -f $archf ] ; then
1209          if [ -f $archf_from_get ] ; then
1210               cp $archf_from_get $archf
1211          else
1212               echo Problem with arch files ; exit 1
1213          fi
1214      fi
1215      cp "$archf" "$MODEL/modipsl/modeles/LMDZ/arch/arch-local.$e"
1216    done
1217    # FIN AJOUT TEMPORAIRE
1218    # ==========================================================
1219
1220    compile_icolmdzor
1221    run_bench
1222    run_bench_icosa
1223}
1224
1225set +e; (set -e && wrapper_run_all "$@"); err_status=$?; set -e  # workaround to make sure set -e is propagated to wrapper_run_all & check the result
1226if (($err_status)); then
1227   echo "EXIT (error)"
1228   echo "install_lmdz.sh failed"
1229   set +u
1230   if [[ $verbose != 1 ]]; then
1231       echo "Consider using the \`-verbose\` flag to see precisely where it stopped."
1232   fi
1233   exit 1
1234fi
Note: See TracBrowser for help on using the repository browser.