source: BOL/script_install_amaury/install_lmdz.sh @ 5220

Last change on this file since 5220 was 5187, checked in by abarral, 11 days ago

minor cleanup

  • Property svn:executable set to *
File size: 36.4 KB
Line 
1#!/bin/bash
2set -eu  # error on command failure, and on unset variables
3export LC_ALL=# standardize awk format
4
5###########################################################################
6# Author : Laurent Fairhead et Frédéric Hourdin
7# Usage  : install_lmdz.sh -help
8#
9# bash installation script of the LMDZ model on different computer types :
10# Linux PC, "mesocentre" (IPSL-UPMC, IPSL-X), super-computer (IDRIS)
11#
12# The model is downloaded in the following directory tree
13# $MODEL/modipsl/modeles/...
14# using the "modipsl" infrastructure created by the "IPSL"
15# for coupled (atmosphere/ocean/vegetation/chemistry) climate modeling
16# activities.
17# Here we only download atmospheric (LMDZ) and vegetation (ORCHIDEE)
18# components.
19#
20# The sources of the models can be found in the "modeles" directory.
21# In the present case, LMDZ, ORCHIDEE, and IOIPSL or XIOS (handling of
22# input-outputs using the NetCDF library).
23#
24# The script downloads various source files (including a version of NetCDF)
25# and utilities, compiles the model, and runs a test simulation in a
26# minimal configuration.
27#
28# Prerequisites : gfortran, bash or ksh, wget, gunzip, tar, ...
29#
30# Modif 18/11/2011
31#    changes for option real 8.
32#      We compile with -r8 (or equivalent) and -DNC_DOUBLE for the GCM
33#      but with -r4 for netcdf. Variable real must be set to
34#      r4 or r8 at the beginning of the script below.
35#
36###########################################################################
37
38### Functions
39
40function myget {
41  # Get a file from LMDZ repository, make it executable if .(ba)sh
42  local url=$1
43
44  local filename
45  filename=$(basename "$url")
46
47  wget --no-check-certificate -nv "http://lmdz.lmd.jussieu.fr/pub/$url"
48  if [[ $filename =~ .*\.(ba)?sh ]]; then chmod +x "$filename"; fi
49}
50
51function get_svn_branch {
52  local url=$1
53  local rev=$2
54  local res
55
56  res=$(svn log -v -q "$url" -r "$rev" -l 1 | cut -d "/" -f -4)
57  if echo "$res" | grep -q "/trunk/"; then
58    res=$(echo "$res" | grep "/trunk/" | head -1 | cut -d "/" -f 2-3)
59  elif echo "$res" | grep -q "/branches/"; then
60    res=$(echo "$res" | grep "/branches/" | head -1 | cut -d "/" -f 2-4)
61  else
62    echo "Could not determine svn branch for $url, r=$rev"
63  fi
64  echo "$res"
65}
66
67function set_default_params {
68    # Valeur par défaut des parametres
69    svn_lmdz=""
70    version="20240508.CM7"
71
72    netcdf=1
73    bench=1
74    SCM=0
75    veget="none"
76    grid_resolution="32x32x39"
77    benchphysiq=""
78    compphysiq="lmd"
79    is_1D="n"
80    fortran_file="gcm"
81
82    parallel="none"
83    trusting="testing"
84    MODEL=""
85
86    with_xios=0
87    opt_makelmdz_xios=""
88
89    rad="rrtm"
90
91    compile_with_fcm=1
92
93    cosp="none"
94    aerosols=0
95    inlandsis=0
96
97    make_j=8
98    clean_install=1
99
100    # Check if on a Mac /!\ Probably doesn't work anymore, to fix one day...
101    if [[ $(uname) = "Darwin" ]]; then
102        export MAKE="make"
103    fi
104
105    optim_flag="-prod"
106    arch="local"
107
108    arch="local-gfortran"
109    arch_dir=""
110
111    jobcmd=""
112}
113
114function read_cmdline_args {
115    while (($# > 0)); do
116        case $1 in
117            "-h") cat <<........fin
118        $0 [ -v version ] [ -r svn_release ]
119               [ -parallel PARA ] [ -d GRID_RESOLUTION ] [ -bench 0/1 ]
120               [-name LOCAL_MODEL_NAME] [-rad RADIATIF]
121
122        -v       "version" like 20150828.trunk, see http://www.lmd.jussieu.fr/~lmdz/Distrib/LISMOI.trunk (default <$version>)
123
124        -r       "svn_release" : either the svn release number or "last" (default <$svn_lmdz>)
125
126        -parallel parallel support: mpi, omp, mpi_omp (mpi with openMP) or none (default: <$parallel>)
127
128        -d        "grid resolution": should be among the available benchs if -bench 1 (valid values: 48x36x19, 48x36x39) (default : <$grid_resolution>)
129
130        -bench     activating the bench or not (0/1) (default: <$bench>)
131
132        -unstable  use unstable tar instead of testing
133
134        -name      name of the folder to install to (default <$MODEL>)
135
136        -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>)
137
138        -xios      use (download and compile) the XIOS library (will compile the parallel NetCDF4-HDF5 library) (requires to also have -parallel mpi_omp)
139
140        -cosp       to run with cospv1 or cospv2 [none/v1/v2] (default <$cosp>)
141
142        -rad        radiative code: oldrad, rrtm or ecrad (default <$rad>)
143
144        -nofcm      to compile without fcm
145
146        -SCM        install 1D version automatically
147
148        -debug      compile everything in debug mode
149
150        -benchphysiq   to choose which physics.def package to use in the bench (default <$benchphysiq>)
151
152        -compilephysiq   physics to compile the model with (default <$compphysiq>)
153
154        -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)
155
156        -spla       activate interactive aerosols
157
158        -inlandsis  activate new snow scheme
159
160        -arch       name of the arch to use (default <$arch>)
161
162        -arch_dir   where to find the arch files (default <$arch_dir>)
163
164        -make_j     number of processes to parallelize installations (default <$make_j>)
165
166        -jobcmd     command prepended to fcm compile jobs, e.g. "srun" (default <$jobcmd>)
167
168        -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)
169
170........fin
171                  exit 1;;
172            "-v") version=$2; shift; shift;;
173            "-r") svn_lmdz=$2; shift; shift;;
174            "-d") grid_resolution=$2; shift; shift;;
175            "-unstable") trusting="unstable"; shift;;
176            "-cosp") cosp=$2
177                     case $cosp in
178                         "none"|"v1"|"v2") cosp=$2; shift; shift;;
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; shift;;
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; shift;;
191                             *) echo "Only none mpi omp mpi_omp for the parallel option"; exit 1
192                         esac;;
193            "-bench") bench=$2; shift; shift;;
194            "-debug") optim_flag="-debug"; shift;;
195            "-name") MODEL=$2; shift; shift;;
196            "-netcdf") netcdf=$2; shift; shift;;
197            "-benchphysiq") benchphysiq=$2; shift; shift;;
198            "-compilephysiq") compphysiq=$2; shift; shift;;
199            "-xios") with_xios=1; shift;;
200            "-arch") arch=$2; shift; shift;;
201            "-arch_dir") arch_dir=$2; shift; shift;;
202            "-veget") veget=$2; shift; shift;;
203            "-spla") aerosols=1; shift;;
204            "-inlandsis") inlandsis=1; shift;;
205            "-make_j") make_j=$2; shift; shift;;
206            "-jobcmd") jobcmd=$2; shift; shift;;
207            "-noclean") clean_install=0; shift;;
208            *)  bash install_lmdz.sh -h; exit 1
209        esac
210    done
211
212    # Isotopes : Compile and run with isotopes if lmdz_phys="lmdiso" in main.sh
213    if [[ $compphysiq = "lmdiso" ]]; then isotopes=1; else isotopes=0; fi
214
215    # Option de compilation pour Cosp
216    case $cosp in
217        v1) opt_cosp="-cosp true";;
218        v2) opt_cosp="-cospv2 true";;
219        *) opt_cosp=""
220    esac
221
222    #Define veget-related suffix for gcm name
223    if [[ $veget = 'none' ]]; then
224        suff_orc=''
225    else
226        suff_orc='_orch'
227    fi
228
229
230    if [[ $parallel = "none" ]]; then
231        suff_para='_seq'
232    else
233        suff_para='_para_mem'
234    fi
235
236    if [[ $with_xios = 1 ]]; then opt_makelmdz_xios="-io xios"; fi
237
238    if [[ $aerosols = 1 ]]; then
239      opt_aer="-dust true"; suff_aer="_spla"
240    else
241      opt_aer=""; suff_aer=""
242    fi
243
244    if [[ $inlandsis = 1 ]]; then
245       opt_inlandsis="-inlandsis true"
246    else
247       opt_inlandsis=""
248    fi
249
250    if [[ $isotopes = 1 ]]; then
251      opt_isotopes="-isotopes true"; suff_iso="_iso"
252    else
253      opt_isotopes="" ; suff_iso=""
254    fi
255
256    # set default arch if parallel
257    if [[ $arch = "local-gfortran" && $parallel != "none" ]]; then
258      arch="local-gfortran-parallel"
259      echo "Switching default arch to $arch"
260    fi
261
262    # Name of the model's folder. The convention taken here is that models that requires different compilation sources should have a different names.
263    local xios_name=""
264    if [[ $with_xios = 1 ]]; then xios_name="XIOS"; fi
265    if [[ $MODEL = "" ]]; then MODEL="./LMDZ$version${svn_lmdz}OR$veget$xios_name"; fi
266
267    if [[ $arch_dir = "" ]]; then
268      arch_dir="$MODEL/modipsl/config/IPSLCM7/ARCH/";
269    elif ! readlink -fe "$arch_dir" >/dev/null; then
270      echo "STOP: no arch dir <$arch_dir>"; exit 1
271    fi
272
273    if ! (echo "$grid_resolution" | grep -q "x"); then
274      is_1D="y"
275      fortran_file="lmdz1d"
276    fi
277}
278
279function ensure_correct_option_combinations {
280    # Check on veget version
281    if [[ $veget != 'none' && $veget != "CMIP6" && $veget != "orch2.0" && $veget != "orch2.2" ]]; then
282        re='^[0-9]+$'
283        if ! [[ $veget =~ $re ]]; then
284            echo 'Valeur de l option veget non valable'; exit 1
285        fi
286    fi
287
288    ## if also compiling XIOS, parallel must be mpi_omp
289    if [[ $with_xios = 1 && $parallel != "mpi_omp" ]]; then
290        echo "Error, you must set -parallel mpi_omp if you want XIOS"; exit 1
291    fi
292
293    if [[ $cosp = "v2" && $with_xios = 0 ]]; then
294        echo "Error, Cospv2 cannot run without Xios"; exit 1
295    fi
296
297    # STOP if trying to use both ORCHIDEE and Isotopes :
298    if [[ $isotopes = 1 && $veget != "none" ]]; then
299      echo "STOP: You cannot run LMDZ with ORCHIDEE and ISOtopes at the same time"; exit 1
300    fi
301
302    # STOP if trying to use both SPLA and Isotopes :
303    if [[ $isotopes = 1 && $aerosols = 1 ]]; then
304      echo "STOP: You cannot run LMDZ with Isotopes and aerosols=spla at the same time"; exit 1
305    fi
306
307    # (Temporary) STOP if trying to use Isotopes with XIOS :
308    if [[ $isotopes = 1 && $with_xios = 1 ]]; then
309      echo "STOP: Isotopes cannont yet be run with XIOS"; exit 1
310    fi
311
312    if [[ $aerosols = 1 && $rad != "rrtm" ]]; then
313      echo "STOP: For the time being, <aerosols=spla> requires <rad=rrtm>"; exit 1
314    fi
315}
316
317function check_available_software {
318    local required_soft=("wget" "tar" "gzip" "make" "gcc" "cmake" "m4" "c++")
319    echo "Checking if required software is available (${required_soft[*]})"
320    for logiciel in "${required_soft[@]}"; do
321        if [[ $(which "$logiciel") = "" ]]; then
322            echo "You must first install $logiciel on your system"; exit 1
323        fi
324    done
325
326    cat <<eod > tt.f90
327print*,'coucou'
328end
329eod
330    $compiler tt.f90 || a.out
331    ./a.out >| tt
332    if [[ $(< tt sed -e 's/ //g' ) != "coucou" ]]; then
333        echo "problem installing with compiler $compiler"; exit 1
334    fi
335    rm tt a.out tt.f90
336}
337
338function download_model {
339    if [[ $clean_install = 1 ]]; then rm -rf "$MODEL"; fi
340
341    mkdir -p "$MODEL"
342    MODEL=$(readlink -e -f "$MODEL"); echo "MODEL: $MODEL"  # absolute path
343    if [[ ! -d "$MODEL/modipsl" ]]; then
344        echo "Downloading a slightly modified version of modipsl+LMDZ"
345        cd "$MODEL"
346        getlog="$(pwd)/get.log"
347        echo "logfile : $getlog"
348        myget "src_archives/$trusting/modipsl.$version.tar.gz" &>> get.log
349        echo "install_lmdz.sh wget_OK $(date)"
350
351        gunzip "modipsl.$version.tar.gz" &>> get.log
352        tar xf "modipsl.$version.tar" &>> get.log
353        rm "modipsl.$version.tar"
354
355        cd - &> /dev/null
356    fi
357}
358
359function init_arch {
360    set +e; arch_dir=$(readlink -f "$arch_dir"); set -e  # full path. readlink must be called *after* the path is created
361
362    # Check where default fcm, path, env are located - by default in $arch_path, instead in $MODEL/modipsl/modeles/LMDZ/arch/
363    local i fcm_path path_path env_path
364    for i in "path" "fcm" "env"; do
365      local varname=${i}_path
366      if [[ -f $arch_dir/arch-$arch.$i ]]; then
367          declare $varname="$arch_dir/arch-$arch.$i"
368      else
369          declare $varname="$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.$i"
370          if [[ ! -f ${!varname} ]]; then
371              echo "STOP: no ${!varname}"; exit 1
372          fi
373      fi
374    done
375    default_fcm_path=$fcm_path
376    default_path_path=$path_path
377    default_env_path=$env_path
378
379    # check compiler
380    compiler=$(< "$default_fcm_path" grep "%COMPILER" | sed -e "s/%COMPILER\s*//")
381
382    # load env
383    # shellcheck disable=SC1090
384    if [[ -f $default_env_path ]]; then source "$default_env_path"; fi
385
386    local mpi_file  # can't be done before as it depends on sourcing the env
387    mpi_file=$(readlink -e -f "$(which mpif90)")
388    path_mpi=$(dirname "$mpi_file")
389    root_mpi=$(dirname "$path_mpi")
390}
391
392function install_arch {
393    local component=$1
394
395    # Use same .env for all components (for module compatibility)
396    cp -f "$default_env_path" "$MODEL/modipsl/modeles/$component/arch" &> /dev/null || true  # allow failure if we're copying the file to itself
397
398    # Use local .path and .fcm is available, otherwise default
399    if [[ ! -f "$MODEL/modipsl/modeles/$component/arch/arch-$arch.path" ]]; then
400        cp -f "$default_path_path" "$MODEL/modipsl/modeles/$component/arch"
401
402        if [[ $component = "ORCHIDEE" ]]; then
403            if [[ $orcbranch = "/tags/ORCHIDEE_2_0/ORCHIDEE" ]]; then  # 2.0 and before have a different fcm convention
404                sed -i'' -e "s/-I//" -e "s/-L//" "$MODEL/modipsl/modeles/ORCHIDEE/arch/arch-$arch.path"  # /!\ we only replace first occurence on purpose
405            fi
406        fi
407    fi
408    if [[ ! -f "$MODEL/modipsl/modeles/$component/arch/arch-$arch.fcm" ]]; then
409        cp -f "$default_fcm_path"  "$MODEL/modipsl/modeles/$component/arch"
410
411        if [[ $component = "XIOS" ]]; then
412            # Adapt for XIOS, which uses different naming conventions
413            sed -i'' -e "s/%COMPILER/%FCOMPILER/" -e "s/%LINK/%LINKER/" -e "s/-fdefault-real-8//" "$MODEL/modipsl/modeles/XIOS/arch/arch-$arch.fcm"
414        fi
415    fi
416}
417
418function install_netcdf {
419    echo "Installing Netcdf"
420    local ncdf_compiler="$compiler"
421
422    if [[ $netcdf = 0 ]]; then
423        ncdfdir=$(nf-config --prefix)
424    else
425        cd "$MODEL"
426
427        # Convert non-basic compiler
428        case $compiler in
429            mpif90) ncdf_compiler=$($compiler --version | head -n 1 | cut -d " " -f -1)
430        esac
431
432        case $ncdf_compiler in
433            gfortran | GNU) ncdf_compiler="gfortran"; opt1="-compiler gnu"; opt2="-CC gcc -FC gfortran -CXX g++";;
434            *)      echo "unexpected compiler $ncdf_compiler for netcdf"; exit 1
435        esac
436
437        case $with_xios in
438            0) script_install_netcdf="install_netcdf4_hdf5_seq.bash"
439               ncdfdir="netcdf4_hdf5_seq"
440               opt_="$opt1";;
441            1) script_install_netcdf="install_netcdf4_hdf5.bash"
442               ncdfdir="netcdf4_hdf5"
443               opt_="$opt2 -MPI $root_mpi";;
444            *) echo "with_xios=$with_xios, should be 0 or 1"; exit 1
445        esac
446        if [[ $netcdf = 1 ]]; then
447           ncdfdir="$MODEL/$ncdfdir"
448        else
449           mkdir -p "$netcdf"; ncdfdir="$netcdf/$ncdfdir"
450        fi
451
452        echo "Repertoire netcdf $ncdfdir"
453        if [[ ! -d $ncdfdir ]]; then
454            netcdflog=$(pwd)/netcdf.log
455            echo "----------------------------------------------------------"
456            echo "Compiling the Netcdf library"
457            echo "----------------------------------------------------------"
458            echo "log file : $netcdflog"
459            myget script_install/$script_install_netcdf &>> "$netcdflog"
460            chmod u=rwx $script_install_netcdf
461            # shellcheck disable=SC2086
462            ./$script_install_netcdf -prefix "$ncdfdir" $opt_ &>> "$netcdflog"
463        fi
464
465        # Add to path
466        export PATH="$ncdfdir/bin:$PATH"
467
468        #----------------------------------------------------------------------------
469        # LF rajout d'une verrue, pour une raison non encore expliquee,
470        # la librairie est parfois rangée dans lib64 et non dans lib
471        # par certains compilateurs
472        if [[ ! -e lib && -d lib64 ]]; then ln -s lib64 lib; fi
473        #----------------------------------------------------------------------------
474
475        echo "install_lmdz.sh netcdf_OK $(date)"
476    fi
477
478    cat >test_netcdf90.f90 <<EOF
479    use netcdf
480    print *, "NetCDF library version: ", nf90_inq_libvers()
481    end
482EOF
483
484    if $ncdf_compiler -I"$ncdfdir"/include test_netcdf90.f90 -L"$ncdfdir"/lib -lnetcdff -lnetcdf -Wl,-rpath="$ncdfdir"/lib && ./a.out; then
485        rm test_netcdf90.f90 a.out
486    else
487        cat <<EOF
488Failed test program using NetCDF-Fortran. You can:
489- check that you have NetCDF-Fortran installed in your system
490- or specify an installation directory with option -netcdf of install_lmdz.sh
491- or download and compile NetCDF-Fortran with option -netcdf 1 of install_lmdz.sh
492EOF
493        exit 1
494    fi
495
496    # Compile NetCDF95
497    cd "$MODEL/modipsl/modeles/LMD"*
498    echo "Installing NetCDF95"
499    cd "$MODEL"
500    if [[ ! -d "NetCDF95-0.3" ]]; then
501        myget src_archives/netcdf/NetCDF95-0.3.tar.gz
502        tar -xf NetCDF95-0.3.tar.gz
503        rm NetCDF95-0.3.tar.gz
504        cd NetCDF95-0.3
505        mkdir -p build && cd build
506        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"
507        make install
508    fi
509}
510
511function install_IOIPSL {
512    install_arch "IOIPSL"
513
514    cd "$MODEL/modipsl/modeles/IOIPSL"
515    ioipsllog="$(pwd)/ioipsl.log"
516    echo "Compiling IOIPSL, the interface library with Netcdf $(date) (log: $ioipsllog)"
517
518    echo "$jobcmd ./makeioipsl_fcm -j $make_j -arch $arch $optim_flag" > compile.sh
519    chmod +x compile.sh
520    echo "Compiling IOIPSL using $(\cat compile.sh)"
521    if ! ./compile.sh &> "$ioipsllog"; then
522        echo "STOP: IOIPSL compile failed, exiting"; exit 1
523    fi
524
525    # Link to modipsl/bin
526    cp -f bin/* ../../bin
527
528    echo "IOIPSL compiled $(date)"
529}
530
531function install_XIOS {
532    if [[ $with_xios = 1 ]]; then
533        cd "$MODEL/modipsl/modeles"
534        xioslog="$(pwd)/XIOS/xios.log"
535        echo "##########################################################"
536        echo "Compiling XIOS (log $xioslog) $(date)"
537        echo "##########################################################"
538
539        # Download XIOS
540        local xios_http="http://forge.ipsl.fr/ioserver/svn/XIOS2/branches/xios-2.6"
541        local xios_rev="2568"
542       
543        cd "$MODEL/modipsl/modeles"
544        set +e; svn co -r $xios_rev $xios_http XIOS; set -e
545
546        cd XIOS
547
548        install_arch "XIOS"
549
550        echo "$jobcmd ./make_xios --job $make_j --arch $arch" > compile.sh
551        chmod +x compile.sh
552        echo "$(date) Compiling XIOS using $(\cat compile.sh) (log in $xioslog)"
553        if ! ./compile.sh &> "$xioslog"; then
554            echo "STOP: XIOS compilation failed, exiting"; exit 1
555        fi
556
557        echo "Compiled XIOS $(date)"
558    fi
559}
560
561function get_orchidee_version {  # Set / Check ORCHIDEE version
562    echo "Checking Orchidee source version"
563    local fetch_rev=""
564    orcbranch=""
565    case $veget in
566        "none") fcm_veget_version="false";;
567        "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
568        "orch2.2") fcm_veget_version=orchidee2.1; orcbranch="/branches/ORCHIDEE_2_2/ORCHIDEE" ;; # the bundled version
569        *) fetch_rev=$veget; fcm_veget_version=orchidee2.1;;  # /!\ arbitary rev only works for orch>=2.1
570          # /!\ Note: for orch>=4, we should be using fcm_vegt_version="orchideetrunk". Below copied comment by Adriana in LMDZ_Setup docs:
571#          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
572#Si on voudrait activer landice_opt=2 , il faut compiler avec -v orchideetrunk.
573
574    esac
575
576    if [[ -n $fetch_rev ]]; then
577        echo "IF YOU INSTALL ORCHIDEE THE VERY FIRST TIME, ASK for PASSWORD at orchidee-help@listes.ipsl.fr"
578        local curr_rev
579        curr_rev=$(svn info "$MODEL/modipsl/modeles/ORCHIDEE" | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
580        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}')
581        if [[ $fetch_rev != "$curr_rev" ]]; then
582            echo "Fetching orch $fetch_rev from the repository (curr: $curr_rev)"
583            echo "branch is $orcbranch"
584            if [[ $fetch_rev -lt 4465 ]]; then echo 'ORCHIDEE version must be >=4465, exiting'; exit 1; fi
585            cd "$MODEL/modipsl/modeles"
586            rm -rf ORCHIDEE
587            svn co -r "$fetch_rev" "svn://forge.ipsl.fr/orchidee/$orcbranch"
588            cd - > /dev/null
589        fi
590    fi
591
592    # Check parallel LMDZ+ORCH
593    if [[ (! $veget = "none") && $parallel = "none" && ($used_lmdz_rev -lt 4894) ]]; then
594        echo "LMDZ revision must be >=4894 for orchidee without parallel support. Upgrade lmdz or use -parallel mpi_omp."; exit 1
595    fi
596}
597
598function compile_orchidee {
599    install_arch "ORCHIDEE"
600
601    if [[ $veget != "none" ]]; then
602        cd "$MODEL/modipsl/modeles/ORCHIDEE"
603
604        orchideelog="$(pwd)/orchidee.log"
605        echo "Compiling ORCHIDEE, the continental surface model (log $orchideelog) $(date)"
606
607        local xios_orchid
608        if [[ $with_xios = 1 ]]; then
609            xios_orchid="-xios";
610        else
611            xios_orchid="-noxios"
612        fi
613
614        if [[ $parallel != "none" && ! -d src_parallel ]]; then
615           echo "STOP: Orchidee version too old for parallel support"; exit 1
616        fi
617        echo "$jobcmd ./makeorchidee_fcm -j $make_j $xios_orchid $optim_flag -parallel $parallel -arch $arch" > compile.sh
618        chmod +x compile.sh
619        echo "Compiling ORCHIDEE using $(\cat compile.sh)"
620        if ! ./compile.sh &> "$orchideelog"; then
621            echo "STOP: ORCHIDEE compilation failed, exiting"; exit 1
622        fi
623        echo "Compiled ORCHIDEE $(date)"
624    fi
625}
626
627function get_lmdz_version {
628    echo "Checking LMDZ source version"
629    LMDZPATH=$(readlink -e -f "$MODEL/modipsl/modeles/LMD"*)
630    cd "$LMDZPATH"
631    lmdzlog="$(pwd)/lmdz.log"
632
633    if [[ -n $svn_lmdz ]]; then
634        local curr_rev
635        curr_rev=$(svn info "$MODEL/modipsl/modeles/LMD"* | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
636        if [[ $svn_lmdz != "$curr_rev" ]]; then
637            local lmdzbranch
638            echo "Fetching LMDZ $svn_lmdz from the repository"
639            lmdzbranch=$(get_svn_branch "https://svn.lmd.jussieu.fr/LMDZ" "$svn_lmdz")
640            echo "branch is $lmdzbranch"
641            cd "$MODEL/modipsl/modeles"
642            rm -rf LMD*
643            svn co -r "$svn_lmdz" "https://svn.lmd.jussieu.fr/LMDZ/$lmdzbranch" LMDZ
644            cd - > /dev/null
645        fi
646        used_lmdz_rev=$svn_lmdz
647    else  # get svn from info
648        set +e; used_lmdz_rev=$(svn info | grep "Last Changed Rev" | cut -c 19-); set -e
649        if [[ -z $used_lmdz_rev ]]; then  # svn info failed
650            used_lmdz_rev=$(grep 'Revision: [0-9]' "$MODEL"/Read*.md | awk ' { print $2 } ' 2>/dev/null)
651            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
652        fi
653    fi
654}
655
656function compile_lmdz {
657    install_arch "LMDZ"
658    cd "$LMDZPATH"
659
660    if [[ $used_lmdz_rev -le 4185 ]]; then
661        exe_name="bin/${fortran_file}_${grid_resolution}_phy${compphysiq}_${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
662    else
663        exe_name="bin/${fortran_file}_${grid_resolution}_phy${compphysiq}_${rad}${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
664    fi
665
666    local opt_rad
667    case $rad in
668        oldrad) iflag_rrtm=0; NSW=2; opt_rad="";;
669        rrtm)   iflag_rrtm=1; NSW=6
670            if [[ $used_lmdz_rev -le 4185 ]]; then
671                opt_rad="-rrtm true"
672            else
673                opt_rad="-rad rrtm"
674            fi;;
675        ecrad)  iflag_rrtm=2; NSW=6; opt_rad="-rad ecrad";;
676        *) echo "Only oldrad rrtm ecrad for rad option"; exit 1
677    esac
678    if [[ $used_lmdz_rev -le 4185 && $rad = "ecrad" ]]; then
679        echo "ecrad only available for LMDZ rev starting with 4186 "; exit 1
680    fi
681
682    # Compile
683    makelmdz="makelmdz_fcm $optim_flag -arch $arch -j $make_j"
684    local para_compile_opt="-mem -parallel $parallel"; if [[ $parallel = "none" ]]; then para_compile_opt=""; fi
685    echo "$jobcmd ./$makelmdz $opt_rad $opt_cosp $opt_makelmdz_xios $opt_aer $opt_inlandsis $opt_isotopes -p $compphysiq -d ${grid_resolution} -v $fcm_veget_version $para_compile_opt $fortran_file" > compile.sh
686    echo "Compiling lmdz using $(\cat compile.sh) (log: $lmdzlog) $(date)"
687    chmod +x ./compile.sh
688    if ! ./compile.sh &> "$lmdzlog"; then
689        echo "STOP: LMDZ compilation failed, exiting"; exit 1
690    fi
691    echo "Finished LMDZ compilation $(date)"
692
693    # Check executable
694    if [[ ! -f $exe_name ]]; then
695        echo "STOP: Compilation failed, can't find the executable"; exit 1
696    else
697        echo "Compilation successful, the executable is $exe_name $(date)"
698    fi
699}
700
701function run_bench {
702    local bench_cmd="./bench.sh"
703
704    cd "$MODEL/modipsl/modeles/LMDZ"*
705
706    if [[ $bench = "tuto" ]]; then
707        myget "Training/tutorial.tar"; tar xf tutorial.tar; cd TUTORIAL
708        ./init.sh
709    elif [[ $bench = 1 ]]; then
710      rm -rf "BENCH${grid_resolution}"
711      local bench=bench_lmdz_${grid_resolution}
712      if [[ $compphysiq = "lmdiso" ]]; then bench=bench_lmdz_iso_${grid_resolution}; fi
713
714      if [[ $is_1D = "y" ]] ; then  # 1D
715        myget "1D/1D.tar.gz"
716        mkdir -p "BENCH${grid_resolution}"
717        tar xf "1D.tar.gz" -C "BENCH${grid_resolution}" --strip-components=1
718        cd "BENCH${grid_resolution}"
719        # Below: ugly, but until we rewrite the 1D case...
720        sed -i'' -e "s:^listecas=.*$:listecas=ARMCU/REF:" -e "s:cd \$local/bin ; ./compile -L \$L:#cd \$local/bin ; ./compile -L \$L:" \
721            -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:" \
722            run.sh
723        cp "../$exe_name" bin/
724        bench_cmd="./run.sh -rad $rad"  # suppress ferret commands that launch after bench is "over"
725      else
726        myget "3DBenchs/$bench.tar.gz"
727        mkdir "BENCH${grid_resolution}"
728        tar xf "$bench.tar.gz" -C "BENCH${grid_resolution}" --strip-components=1
729
730        if [[ $cosp = "v1" || $cosp = "v2" ]]; then
731            cd "BENCH${grid_resolution}"
732            # copier les fichiers namelist input et output our COSP
733            cp ../DefLists/cosp*_input_nl.txt .
734            cp ../DefLists/cosp*_output_nl.txt .
735            # Activer la cles ok_cosp pour tourner avec COSP
736            sed -e 's@ok_cosp=n@ok_cosp=y@' config.def > tmp
737            \mv -f tmp config.def
738            cd ..
739        fi
740
741        if [[ -n "$benchphysiq" ]]; then
742            cd "BENCH${grid_resolution}"
743            if [[ -f "physiq.def_${benchphysiq}" ]]; then
744                cp "physiq.def_${benchphysiq}" benchphysiq.def
745                echo "using physiq.def_${benchphysiq}"
746            else
747                echo "using standard physiq.def"
748            fi
749            cd ..
750        else
751            echo "using standard physiq.def"
752        fi
753
754        if [[ $with_xios = 1 ]]; then
755            cd "BENCH${grid_resolution}"
756            cp ../DefLists/iodef.xml .
757            cp ../DefLists/context_lmdz.xml .
758            cp ../DefLists/field_def_lmdz.xml .
759            # A raffiner par la suite
760            echo "A FAIRE : Copier les *xml en fonction de l option cosp"
761            cp ../DefLists/field_def_cosp*.xml .
762            cp ../DefLists/file_def_hist*xml .
763            # adapt iodef.xml to use attached mode
764            sed -e 's@"using_server" type="bool">true@"using_server" type="bool">false@' \
765                iodef.xml > tmp
766            \mv -f tmp iodef.xml
767
768            # and convert all the enabled="_AUTO_" (for libIGCM) to enabled=.FALSE.
769            # except for histday
770            for histfile in file_def_hist*xml; do
771                if [[ "$histfile" = "file_def_histday_lmdz.xml" ]]; then
772                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".TRUE."@' \
773                        "$histfile" > tmp
774                    \mv -f tmp "$histfile"
775                    sed -e 's@output_level="_AUTO_"@output_level="5"@' "$histfile" \
776                        > tmp
777                    \mv -f tmp "$histfile"
778                    sed -e 's@compression_level="2"@compression_level="0"@' \
779                        "$histfile" > tmp
780                    \mv -f tmp "$histfile"
781                else
782                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
783                        "$histfile" > tmp
784                    \mv -f tmp "$histfile"
785                fi
786            done
787            # and add option "ok_all_xml=y" in config.def
788            echo "### XIOS outputs" >> config.def
789            echo 'ok_all_xml=.true.' >> config.def
790
791            #activer les sorties pour Cosp
792            if [[ "$cosp" = "v1" ]]; then
793                sed -i'' -e 's@enabled=".FALSE."@enabled=".TRUE."@' \
794                         -e 's@output_level="_AUTO_"@output_level="5"@' \
795                         -e 's@compression_level="2"@compression_level="0"@' \
796                         file_def_histdayCOSP_lmdz.xml
797            fi
798            if [[ "$cosp" = "v2" ]]; then
799                sed -i'' -e 's@compression_level="2"@compression_level="0"@' file_def_histdayCOSPv2_lmdz.xml
800                for type_ in hf day mth; do
801                    file=file_def_hist${type_}COSP
802                    sed -i'' -e 's@src="./'${file}'_lmdz.xml"@src="./'${file}'v2_lmdz.xml"@' context_lmdz.xml
803                done
804                sed -i'' -e 's@field_def_cosp1.xml@field_def_cospv2.xml@' field_def_lmdz.xml
805            fi
806
807            cd ..
808        fi
809
810        # Cas Bench avec ecrad
811        if [[ $rad = "ecrad" ]]; then
812            cd "BENCH${grid_resolution}"
813            cp  ../DefLists/namelist_ecrad .
814            cp -r ../libf/phylmd/ecrad/data .
815            cd ..
816        fi
817
818        # Adjusting bench physiq.def to radiative code chosen
819        cd "BENCH${grid_resolution}"
820        sed -e 's/iflag_rrtm=.*.$/iflag_rrtm='$iflag_rrtm'/' \
821            -e 's/NSW=.*.$/NSW='$NSW'/' physiq.def > tmpdef
822        \mv tmpdef physiq.def
823        cd ..
824
825        cp "$exe_name" "BENCH${grid_resolution}/gcm.e"
826        cd "BENCH${grid_resolution}"
827
828        if [[ ${parallel:0:3} = "mpi" ]]; then
829            # Lancement avec deux procs mpi et 2 openMP
830            echo "export OMP_STACKSIZE=800M" > bench.sh
831            if [[ "${parallel:4:3}" = "omp" ]]; then
832                echo "export OMP_NUM_THREADS=2" >> bench.sh
833            fi
834            if [[ $cosp = "v1" || $cosp = "v2" ]]; then
835                echo "ulimit -s 200000" >> bench.sh
836            else
837                echo "ulimit -s unlimited" >> bench.sh
838            fi
839            if which mpirun &> /dev/null; then
840                echo "mpirun -np 2 gcm.e &> listing" >> bench.sh
841            elif grep -q "Adastra" /etc/motd; then
842                local account
843                account=$(/usr/sbin/my_project.py -l 2>&1 | head -1 | cut -d " " -f 3- | cut -c 5-)
844                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"
845                echo "./gcm.e &> listing" >> bench.sh
846            else
847                echo "Error: No command found to run parallel bench"; exit 1
848            fi
849            if [[ $(hostname | cut -c -6) = "spirit" ]]; then  # ulimit unlimited segfaults on Spirit
850                sed -i'' "s/ulimit -s unlimited/ulimit -Ss 8000/" bench.sh
851            fi
852            # Add rebuild, using reb.sh if it is there
853            cat <<EOF >> bench.sh
854if [[ -f reb.sh ]]; then
855  ./reb.sh histday; ./reb.sh histmth; ./reb.sh histhf;
856  ./reb.sh histins; ./reb.sh stomate_history;
857  ./reb.sh sechiba_history; ./reb.sh sechiba_out_2
858fi
859EOF
860        else
861            echo "./gcm.e &> listing" > bench.sh
862        fi
863        chmod +x bench.sh
864        # Getting orchidee stuff
865        if [[ $veget = 'CMIP6' || $veget = "orch2.0" ]]; then  # TODO once we have a 2.2 bench, add it here (or in planned separate bench script)
866            echo 'myget 3DBenchs/BENCHCMIP6.tar.gz'
867            myget 3DBenchs/BENCHCMIP6.tar.gz
868            tar xvzf BENCHCMIP6.tar.gz
869            sed -e "s:VEGET=n:VEGET=y:" config.def > tmp
870            mv -f tmp config.def
871            if [[ $with_xios = 1 ]]; then
872                cp ../../ORCHIDEE/src_xml/context_orchidee.xml .
873                echo '<context id="orchidee" src="./context_orchidee.xml"/>' > add.tmp
874                cp ../../ORCHIDEE/src_xml/field_def_orchidee.xml .
875                cp ../../ORCHIDEE/src_xml/file_def_orchidee.xml .
876                cp ../../ORCHIDEE/src_xml/file_def_input_orchidee.xml .
877                if [[ -f ../../ORCHIDEE/src_xml/context_input_orchidee.xml ]]; then
878                       cp ../../ORCHIDEE/src_xml/context_input_orchidee.xml .
879                       echo '<context id="orchidee" src="./context_input_orchidee.xml"/>' >> add.tmp
880                fi
881                sed -e '/id="LMDZ"/r add.tmp' iodef.xml > tmp
882                mv tmp iodef.xml
883                sed -e'{/sechiba1/ s/enabled="_AUTO_"/type="one_file" enabled=".TRUE."/}' \
884                    file_def_orchidee.xml > tmp
885                \mv -f tmp file_def_orchidee.xml
886                sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
887                    file_def_orchidee.xml > tmp
888                \mv -f tmp file_def_orchidee.xml
889                sed -e 's@output_level="_AUTO_"@output_level="1"@' \
890                    file_def_orchidee.xml > tmp
891                \mv -f tmp file_def_orchidee.xml
892                sed -e 's@output_freq="_AUTO_"@output_freq="1d"@' \
893                    file_def_orchidee.xml > tmp
894                \mv -f tmp file_def_orchidee.xml
895                sed -e 's@compression_level="4"@compression_level="0"@' \
896                    file_def_orchidee.xml > tmp
897                \mv -f tmp file_def_orchidee.xml
898                sed -e 's@XIOS_ORCHIDEE_OK = n@XIOS_ORCHIDEE_OK = y@' \
899                    orchidee.def > tmp
900                \mv -f tmp orchidee.def
901            fi
902        fi
903
904        fi
905
906        if [[ -f ../arch.env ]]; then source ../arch.env; fi
907
908        echo "STARTING BENCH"
909        date
910        if (! $bench_cmd &> out.bench) || ! (tail -n 1 listing | grep "Everything is cool"); then
911            tail listing
912            echo "Bench FAILED, exiting"; exit 1
913        fi
914        date
915        tail listing
916    fi
917
918    # 1D case
919    if [[ $SCM = 1 ]]; then
920        cd "$MODEL"
921        myget 1D/1D.tar.gz
922        tar xf 1D.tar.gz
923        cd 1D
924        for e in "fcm" "env" "path"; do
925          cp "$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.$e" "$MODEL/modipsl/modeles/LMDZ/arch/arch-local.$e"
926        done
927        if [[ $rad = "oldrad" ]]; then
928            sed -i'' -e 's/^rad=.*$/rad=oldrad/' run.sh
929            sed -i'' -e 's/^rad=.*$/rad=oldrad/' bin/compile
930        elif [[ $rad = ecrad ]] ; then
931                        sed -i'' -e 's/^rad=.*$/rad=ecrad/' run.sh
932                        sed -i'' -e 's/^rad=.*$/rad=ecrad/' bin/compile
933                    fi
934        echo "Running 1D/run.sh, log in $(pwd)/run1d.log"
935        ./run.sh &> "$(pwd)/run1d.log"
936    fi
937}
938
939# If sourced: returns, else run setup
940if [[ ! "${BASH_SOURCE[0]}" = "$0" ]]; then return 0; fi
941
942echo "install_lmdz.sh DEBUT $(date)"
943
944set_default_params
945read_cmdline_args "$@"
946ensure_correct_option_combinations
947download_model
948init_arch
949check_available_software
950get_lmdz_version
951get_orchidee_version
952install_netcdf
953install_IOIPSL
954install_XIOS
955compile_orchidee
956compile_lmdz
957run_bench
Note: See TracBrowser for help on using the repository browser.