source: BOL/script_install/install_lmdz.sh @ 5590

Last change on this file since 5590 was 5567, checked in by asima, 9 months ago

For Jean-Jay, do not install NetCDF95 next to modipsl.
netcdf95 is loaded as a module in the arch*.env
(also, minor : since a getlog variable is defined, let's use it :) )

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