source: BOL/script_install/install_lmdz.sh @ 5969

Last change on this file since 5969 was 5762, checked in by Laurent Fairhead, 7 months ago

Modifications brought about by new options in makelmdz_fcm
LF

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