source: BOL/script_install_amaury/install_lmdz.sh @ 5027

Last change on this file since 5027 was 5027, checked in by abarral, 2 months ago

update default tar

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