source: BOL/script_install/install_lmdz.sh @ 5537

Last change on this file since 5537 was 5533, checked in by fhourdin, 40 hours ago

Gestion de la compilation pour LMDZ_Setup

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