source: BOL/script_install_amaury/install_lmdz.sh @ 5008

Last change on this file since 5008 was 5008, checked in by abarral, 5 weeks ago

fix lmdz rev retrieval

  • Property svn:executable set to *
File size: 34.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="20231022.trunk"
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    fi
332}
333
334function create_model_tar {  # TODO TEMP while waiting for tar: we copy the base from a local path = IPSLCM7 + ORCH2.2 8504
335    if [[ ! -d ../MODEL_REF ]]; then
336        cd ..
337        mkdir MODEL_REF
338        cd MODEL_REF
339        # Base is IPSLCM7
340        svn co --username icmc_users https://forge.ipsl.fr/igcmg/svn/modipsl/trunk modipsl
341        cd modipsl/util
342        ./model IPSLCM7_work
343        # Remove unused components
344        rm -rf ../oasis3-mct
345        cd ../modeles
346        rm -rf ICOSA_LMDZ ORCHIDEE_4 ORCHIDEE_2_2 NEMO DYNAMICO XIOS INCA
347        # Retrieve orch version
348        svn co svn://forge.ipsl.fr/orchidee/branches/ORCHIDEE_2_2/ORCHIDEE -r 8504
349        # Correct orchidee bug in 8504
350        sed -i"" -e "s/ts.second=dt_routing/ts%second=dt_routing/" -e "380,390 s/USE grid/USE grid, temp_disabled => contfrac/" ORCHIDEE/src_sechiba/routing_native_flow.f90
351
352        echo "STOP: INITIALIZED, PLEASE RELAUNCH ONCE"; exit 1
353    fi
354
355    if [[ ! -d "$MODEL" ]]; then
356        \cp -r ../MODEL_REF .
357        mv MODEL_REF "$MODEL"
358    fi
359    MODEL=$(readlink -e -f "$MODEL"); echo "$MODEL"  # absolute path
360}
361
362function init_arch {
363    arch_dir=$(readlink -e -f "$arch_dir")  # full path. readlink must be called *after* the path is created
364    if [[ ! -d $arch_dir ]]; then
365        echo "STOP: no arch dir $arch_dir"; exit 1
366    fi
367
368    local fcm_path
369    if [[ -f $arch_dir/arch-$arch.fcm ]]; then
370        fcm_path="$arch_dir/arch-$arch.fcm"
371    else
372        fcm_path="$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.fcm"
373        if [[ ! -f $fcm_path ]]; then
374            echo "STOP: no $fcm_path"; exit 1
375        fi
376    fi
377
378    local env_path
379    if [[ -f $arch_dir/arch-$arch.env ]]; then
380        env_path="$arch_dir/arch-$arch.env"
381    else
382        env_path="$MODEL/modipsl/modeles/LMDZ/arch/arch-$arch.env"
383        if [[ ! -f $env_path ]]; then
384            echo "STOP: no $env_path"; exit 1
385        fi
386    fi
387
388    # check compiler
389    compiler=$(< "$fcm_path" grep "%COMPILER" | sed -e "s/%COMPILER\s*//")
390
391    # load env
392    # shellcheck disable=SC1090
393    if [[ -f $env_path ]]; then source "$env_path"; fi
394}
395
396function install_arch {
397    local component=$1
398
399    if [[ -f "$arch_dir/arch-$arch.env" ]]; then
400        cp -f "$arch_dir/arch-$arch."{fcm,path,env} "$MODEL/modipsl/modeles/$component/arch" &> /dev/null || true  # allow failure if doesn't exist
401
402        if [[ $component = "XIOS" ]]; then
403            # Adapt for XIOS, which uses different naming conventions
404            sed -i'' -e "s/%COMPILER/%FCOMPILER/" -e "s/%LINK/%LINKER/" -e "s/-fdefault-real-8//" "$MODEL/modipsl/modeles/XIOS/arch/arch-$arch.fcm"
405        elif [[ $component = "ORCHIDEE" ]]; then
406            if [[ $orcbranch = "/tags/ORCHIDEE_2_0/ORCHIDEE" ]]; then  # 2.0 and before have a different fcm convention
407                sed -i'' -e "s/-I//" -e "s/-L//" "$MODEL/modipsl/modeles/ORCHIDEE/arch/arch-$arch.path"  # /!\ we only replace first occurence on purpose
408            fi
409        fi
410    fi
411}
412
413function install_netcdf {
414    echo "Installing Netcdf"
415    local ncdf_compiler="$compiler"
416
417    if [[ $netcdf = 0 ]]; then
418        ncdfdir=$(nf-config --prefix)
419    else
420        cd "$MODEL"
421
422        # Convert non-basic compiler
423        case $compiler in
424            mpif90) ncdf_compiler=$($compiler --version | head -n 1 | cut -d " " -f -1)
425        esac
426
427        case $ncdf_compiler in
428            gfortran | GNU) ncdf_compiler="gfortran"; opt1="-compiler gnu"; opt2="-CC gcc -FC gfortran -CXX g++";;
429            *)      echo "unexpected compiler $ncdf_compiler for netcdf"; exit 1
430        esac
431
432        case $with_xios in
433            0) script_install_netcdf="install_netcdf4_hdf5_seq.bash"
434               ncdfdir="netcdf4_hdf5_seq"
435               opt_="$opt1";;
436            1) script_install_netcdf="install_netcdf4_hdf5.bash"
437               ncdfdir="netcdf4_hdf5"
438               opt_="$opt2 -MPI $root_mpi";;
439            *) echo "with_xios=$with_xios, should be 0 or 1"; exit 1
440        esac
441        if [[ $netcdf = 1 ]]; then
442           ncdfdir="$MODEL/$ncdfdir"
443        else
444           mkdir -p "$netcdf"; ncdfdir="$netcdf/$ncdfdir"
445        fi
446
447        echo "Repertoire netcdf $ncdfdir"
448        if [[ ! -d $ncdfdir ]]; then
449            netcdflog=$(pwd)/netcdf.log
450            echo "----------------------------------------------------------"
451            echo "Compiling the Netcdf library"
452            echo "----------------------------------------------------------"
453            echo "log file : $netcdflog"
454            myget script_install/$script_install_netcdf &>> "$netcdflog"
455            chmod u=rwx $script_install_netcdf
456            # shellcheck disable=SC2086
457            ./$script_install_netcdf -prefix "$ncdfdir" $opt_ &>> "$netcdflog"
458        fi
459
460        # Add to path
461        export PATH="$ncdfdir/bin:$PATH"
462
463        #----------------------------------------------------------------------------
464        # LF rajout d'une verrue, pour une raison non encore expliquee,
465        # la librairie est parfois rangée dans lib64 et non dans lib
466        # par certains compilateurs
467        if [[ ! -e lib && -d lib64 ]]; then ln -s lib64 lib; fi
468        #----------------------------------------------------------------------------
469
470        echo "install_lmdz.sh netcdf_OK $(date)"
471    fi
472
473    cat >test_netcdf90.f90 <<EOF
474    use netcdf
475    print *, "NetCDF library version: ", nf90_inq_libvers()
476    end
477EOF
478
479    if $ncdf_compiler -I"$ncdfdir"/include test_netcdf90.f90 -L"$ncdfdir"/lib -lnetcdff -lnetcdf -Wl,-rpath="$ncdfdir"/lib && ./a.out; then
480        \rm test_netcdf90.f90 a.out
481    else
482        cat <<EOF
483Failed test program using NetCDF-Fortran. You can:
484- check that you have NetCDF-Fortran installed in your system
485- or specify an installation directory with option -netcdf of install_lmdz.sh
486- or download and compile NetCDF-Fortran with option -netcdf 1 of install_lmdz.sh
487EOF
488        exit 1
489    fi
490
491    # Compile NetCDF95
492    cd "$MODEL/modipsl/modeles/LMD"*
493    echo "Installing NetCDF95"
494    cd "$MODEL"
495    if [[ ! -d "NetCDF95-0.3" ]]; then
496        myget src_archives/netcdf/NetCDF95-0.3.tar.gz
497        tar -xf NetCDF95-0.3.tar.gz
498        \rm NetCDF95-0.3.tar.gz
499        cd NetCDF95-0.3
500        mkdir -p build && cd build
501        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"
502        make install
503    fi
504}
505
506function install_IOIPSL {
507    install_arch "IOIPSL"
508
509    cd "$MODEL/modipsl/modeles/IOIPSL"
510    ioipsllog="$(pwd)/ioipsl.log"
511    echo "Compiling IOIPSL, the interface library with Netcdf $(date) (log: $ioipsllog)"
512
513    echo "$jobcmd ./makeioipsl_fcm -j $make_j -arch $arch" > compile.sh
514    chmod +x compile.sh
515    echo "Compiling IOIPSL using $(\cat compile.sh)"
516    if ! ./compile.sh &> "$ioipsllog"; then
517        echo "STOP: IOIPSL compile failed, exiting"; exit 1
518    fi
519
520    # Link to modipsl/bin
521    cp -f bin/* ../../bin
522
523    echo "IOIPSL compiled $(date)"
524}
525
526function install_XIOS {
527    if [[ $with_xios = 1 ]]; then
528        cd "$MODEL/modipsl/modeles"
529        xioslog="$(pwd)/XIOS/xios.log"
530        echo "##########################################################"
531        echo "Compiling XIOS (log $xioslog) $(date)"
532        echo "##########################################################"
533
534        # Download XIOS
535        local xios_http="http://forge.ipsl.fr/ioserver/svn/XIOS2/branches/xios-2.6"
536        local xios_rev="2568"
537       
538        cd "$MODEL/modipsl/modeles"
539        set +e; svn co -r $xios_rev $xios_http XIOS; set -e
540
541        cd XIOS
542
543        install_arch "XIOS"
544
545        echo "Starting XIOS compilation"
546        if ! ./make_xios --arch "$arch" --job 8 &> "$xioslog"; then
547            echo "STOP: XIOS compilation failed, exiting"; exit 1
548        fi
549
550        echo "Compiled XIOS $(date)"
551    fi
552}
553
554function get_orchidee_version {  # Set / Check ORCHIDEE version
555    local fetch_rev=""
556    orcbranch=""
557    case $veget in
558        "none") fcm_veget_version="false";;
559        "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
560        "orch2.2") fcm_veget_version=orchidee2.1; orcbranch="/branches/ORCHIDEE_2_2/ORCHIDEE" ;; # the bundled version
561        *) fetch_rev=$veget; fcm_veget_version=orchidee2.1;;  # /!\ arbitary rev only works for orch>=2.1
562    esac
563
564    if [[ -n $fetch_rev ]]; then
565        local curr_rev
566        curr_rev=$(svn info "$MODEL/modipsl/modeles/ORCHIDEE" | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
567        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}')
568        if [[ $fetch_rev != "$curr_rev" ]]; then
569            echo "Fetching orch $fetch_rev from the repository (curr: $curr_rev)"
570            echo "branch is $orcbranch"
571            if [[ $fetch_rev -lt 4465 ]]; then echo 'ORCHIDEE version must be >=4465, exiting'; exit 1; fi
572            echo "IF YOU INSTALL ORCHIDEE THE VERY FIRST TIME, ASK for PASSWORD at orchidee-help@listes.ipsl.fr"
573            cd "$MODEL/modipsl/modeles"
574            rm -rf ORCHIDEE
575            svn co -r "$fetch_rev" "svn://forge.ipsl.fr/orchidee/$orcbranch"
576            cd - > /dev/null
577        fi
578    fi
579
580    # Check parallel LMDZ+ORCH
581    if [[ (! $veget = "none") && $parallel = "none" && ($used_lmdz_rev -lt 4894) ]]; then
582        echo "LMDZ revision must be >=4894 for orchidee without parallel support. Upgrade lmdz or use -parallel mpi_omp."; exit 1
583    fi
584}
585
586function compile_orchidee {
587    install_arch "ORCHIDEE"
588
589    if [[ $veget != "none" ]]; then
590        cd "$MODEL/modipsl/modeles/ORCHIDEE"
591
592        orchideelog="$(pwd)/orchidee.log"
593        echo "Compiling ORCHIDEE, the continental surface model (log $orchideelog) $(date)"
594
595        local xios_orchid opt_orc
596        if [[ $with_xios = 1 ]]; then
597            xios_orchid="-xios";
598        else
599            xios_orchid="-noxios"
600        fi
601        if [[ $optim_debug = "-debug" ]]; then
602            opt_orc="-debug";
603        else
604            opt_orc="-prod"
605        fi
606
607        if [[ $parallel != "none" && ! -d src_parallel ]]; then
608           echo "STOP: Orchidee version too old for parallel support"; exit 1
609        fi
610        echo "$jobcmd ./makeorchidee_fcm -j $make_j $xios_orchid $opt_orc -parallel $parallel -arch $arch" > compile.sh
611        chmod +x compile.sh
612        echo "Compiling ORCHIDEE using $(\cat compile.sh)"
613        if ! ./compile.sh &> "$orchideelog"; then
614            echo "STOP: ORCHIDEE compilation failed, exiting"; exit 1
615        fi
616        echo "Compiled ORCHIDEE $(date)"
617    fi
618}
619
620function get_lmdz_version {
621    LMDZPATH=$(readlink -e -f "$MODEL/modipsl/modeles/LMD"*)
622    cd "$LMDZPATH"
623    lmdzlog="$(pwd)/lmdz.log"
624
625    if [[ -n $svn_lmdz ]]; then
626        local curr_rev
627        curr_rev=$(svn info "$MODEL/modipsl/modeles/LMD"* | grep Revision: | cut -d ":" -f 2 | cut -c 2-)
628        if [[ $svn_lmdz != "$curr_rev" ]]; then
629            local lmdzbranch
630            echo "Fetching LMDZ $svn_lmdz from the repository"
631            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}')
632            cd "$MODEL/modipsl/modeles"
633            rm -rf LMD*
634            svn co -r "$svn_lmdz" "https://svn.lmd.jussieu.fr/LMDZ$lmdzbranch" LMDZ
635            cd - > /dev/null
636        fi
637        used_lmdz_rev=$svn_lmdz
638    else  # get svn from info
639        set +e; used_lmdz_rev=$(svn info | grep "Last Changed Rev" | cut -c 19-); set -e
640        if [[ -z $used_lmdz_rev ]]; then  # svn info failed
641            used_lmdz_rev=$(grep 'Revision: [0-9]' "$MODEL"/Read*.md | awk ' { print $2 } ' 2>/dev/null)
642            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
643        fi
644    fi
645}
646
647function compile_lmdz {
648    install_arch "LMDZ"
649    cd "$LMDZPATH"
650
651    if [[ $used_lmdz_rev -le 4185 ]]; then
652        exe_name="bin/gcm_${grid_resolution}_phy${compphysiq}_${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
653    else
654        exe_name="bin/gcm_${grid_resolution}_phy${compphysiq}_${rad}${suff_para}${suff_orc}${suff_aer}${suff_iso}.e"
655    fi
656
657    local opt_rad
658    case $rad in
659        oldrad) iflag_rrtm=0; NSW=2; opt_rad="";;
660        rrtm)   iflag_rrtm=1; NSW=6
661            if [[ $used_lmdz_rev -le 4185 ]]; then
662                opt_rad="-rrtm true"
663            else
664                opt_rad="-rad rrtm"
665            fi;;
666        ecrad)  iflag_rrtm=2; NSW=6; opt_rad="-rad ecrad";;
667        *) echo "Only oldrad rrtm ecrad for rad option"; exit 1
668    esac
669    if [[ $used_lmdz_rev -le 4185 && $rad = "ecrad" ]]; then
670        echo "ecrad only available for LMDZ rev starting with 4186 "; exit 1
671    fi
672
673    # Compile
674    makelmdz="makelmdz_fcm $optim_debug -arch $arch -j $make_j"
675    local para_compile_opt="-mem -parallel $parallel"; if [[ $parallel = "none" ]]; then para_compile_opt=""; fi
676    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
677    echo "Compiling lmdz using $(\cat compile.sh) (log: $lmdzlog) $(date)"
678    chmod +x ./compile.sh
679    if ! ./compile.sh &> "$lmdzlog"; then
680        echo "STOP: LMDZ compilation failed, exiting"; exit 1
681    fi
682    echo "Finished LMDZ compilation $(date)"
683
684    # Check executable
685    if [[ ! -f $exe_name ]]; then
686        echo "STOP: Compilation failed, can't find the executable"; exit 1
687    else
688        echo "Compilation successfull, the executable is $exe_name $(date)"
689    fi
690}
691
692function run_bench {
693    cd "$MODEL/modipsl/modeles/LMDZ"*
694
695    if [[ $bench = "tuto" ]]; then
696        myget "Training/tutorial.tar"; tar xf tutorial.tar; cd TUTORIAL
697        ./init.sh
698    elif [[ $bench = 1 ]]; then
699        \rm -rf "BENCH${grid_resolution}"
700        local bench=bench_lmdz_${grid_resolution}
701        myget "3DBenchs/$bench.tar.gz"
702        tar xf "$bench.tar.gz"
703
704        if [[ $cosp = "v1" || $cosp = "v2" ]]; then
705            cd "BENCH${grid_resolution}"
706            # copier les fichiers namelist input et output our COSP
707            cp ../DefLists/cosp*_input_nl.txt .
708            cp ../DefLists/cosp*_output_nl.txt .
709            # Activer la cles ok_cosp pour tourner avec COSP
710            sed -e 's@ok_cosp=n@ok_cosp=y@' config.def > tmp
711            \mv -f tmp config.def
712            cd ..
713        fi
714
715        if [[ -n "$benchphysiq" ]]; then
716            cd "BENCH${grid_resolution}"
717            if [[ -f "physiq.def_${benchphysiq}" ]]; then
718                cp "physiq.def_${benchphysiq}" benchphysiq.def
719                echo "using physiq.def_${benchphysiq}"
720            else
721                echo "using standard physiq.def"
722            fi
723            cd ..
724        else
725            echo "using standard physiq.def"
726        fi
727
728        if [[ $with_xios = 1 ]]; then
729            cd "BENCH${grid_resolution}"
730            cp ../DefLists/iodef.xml .
731            cp ../DefLists/context_lmdz.xml .
732            cp ../DefLists/field_def_lmdz.xml .
733            # A raffiner par la suite
734            echo "A FAIRE : Copier les *xml en fonction de l option cosp"
735            cp ../DefLists/field_def_cosp*.xml .
736            cp ../DefLists/file_def_hist*xml .
737            # adapt iodef.xml to use attached mode
738            sed -e 's@"using_server" type="bool">true@"using_server" type="bool">false@' \
739                iodef.xml > tmp
740            \mv -f tmp iodef.xml
741
742            # and convert all the enabled="_AUTO_" (for libIGCM) to enabled=.FALSE.
743            # except for histday
744            for histfile in file_def_hist*xml; do
745                if [[ "$histfile" = "file_def_histday_lmdz.xml" ]]; then
746                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".TRUE."@' \
747                        "$histfile" > tmp
748                    \mv -f tmp "$histfile"
749                    sed -e 's@output_level="_AUTO_"@output_level="5"@' "$histfile" \
750                        > tmp
751                    \mv -f tmp "$histfile"
752                    sed -e 's@compression_level="2"@compression_level="0"@' \
753                        "$histfile" > tmp
754                    \mv -f tmp "$histfile"
755                else
756                    sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
757                        "$histfile" > tmp
758                    \mv -f tmp "$histfile"
759                fi
760            done
761            # and add option "ok_all_xml=y" in config.def
762            echo "### XIOS outputs" >> config.def
763            echo 'ok_all_xml=.true.' >> config.def
764
765            #activer les sorties pour Cosp
766            if [[ "$cosp" = "v1" ]]; then
767                sed -i'' -e 's@enabled=".FALSE."@enabled=".TRUE."@' \
768                         -e 's@output_level="_AUTO_"@output_level="5"@' \
769                         -e 's@compression_level="2"@compression_level="0"@' \
770                         file_def_histdayCOSP_lmdz.xml
771            fi
772            if [[ "$cosp" = "v2" ]]; then
773                sed -i'' -e 's@compression_level="2"@compression_level="0"@' file_def_histdayCOSPv2_lmdz.xml
774                for type_ in hf day mth; do
775                    file=file_def_hist${type_}COSP
776                    sed -i'' -e 's@src="./'${file}'_lmdz.xml"@src="./'${file}'v2_lmdz.xml"@' context_lmdz.xml
777                done
778                sed -i'' -e 's@field_def_cosp1.xml@field_def_cospv2.xml@' field_def_lmdz.xml
779            fi
780
781            cd ..
782        fi
783
784        # Cas Bench avec ecrad
785        if [[ $rad = "ecrad" ]]; then
786            cd "BENCH${grid_resolution}"
787            cp  ../DefLists/namelist_ecrad .
788            cp -r ../libf/phylmd/ecrad/data .
789            cd ..
790        fi
791
792        # Adjusting bench physiq.def to radiative code chosen
793        cd "BENCH${grid_resolution}"
794        sed -e 's/iflag_rrtm=.*.$/iflag_rrtm='$iflag_rrtm'/' \
795            -e 's/NSW=.*.$/NSW='$NSW'/' physiq.def > tmpdef
796        \mv tmpdef physiq.def
797        cd ..
798
799        cp "$exe_name" "BENCH${grid_resolution}/gcm.e"
800        cd "BENCH${grid_resolution}"
801
802        local bench_cmd="./bench.sh"
803        if [[ ${parallel:0:3} = "mpi" ]]; then
804            # Lancement avec deux procs mpi et 2 openMP
805            echo "export OMP_STACKSIZE=800M" > bench.sh
806            if [[ "${parallel:4:3}" = "omp" ]]; then
807                echo "export OMP_NUM_THREADS=2" >> bench.sh
808            fi
809            if [[ $cosp = "v1" || $cosp = "v2" ]]; then
810                echo "ulimit -s 200000" >> bench.sh
811            else
812                echo "ulimit -s unlimited" >> bench.sh
813            fi
814            if which mpirun &> /dev/null; then
815                echo "mpirun -np 2 gcm.e &> listing" >> bench.sh
816            elif grep -q "Adastra" /etc/motd; then
817                local account
818                account=$(/usr/sbin/my_project.py -l 2>&1 | head -1 | cut -d " " -f 3- | cut -c 5-)
819                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"
820                echo "./gcm.e &> listing" >> bench.sh
821            else
822                echo "Error: No command found to run parallel bench"; exit 1
823            fi
824            if [[ $(hostname | cut -c -6) = "spirit" ]]; then  # ulimit unlimited segfaults on Spirit
825                sed -i'' "s/ulimit -s unlimited/ulimit -Ss 8000/" bench.sh
826            fi
827            # Add rebuild, using reb.sh if it is there
828            cat <<EOF >> bench.sh
829if [[ -f reb.sh ]]; then
830  ./reb.sh histday; ./reb.sh histmth; ./reb.sh histhf;
831  ./reb.sh histins; ./reb.sh stomate_history;
832  ./reb.sh sechiba_history; ./reb.sh sechiba_out_2
833fi
834EOF
835        else
836            echo "./gcm.e &> listing" > bench.sh
837        fi
838        # Getting orchidee stuff
839        if [[ $veget = 'CMIP6' || $veget = "orch2.0" ]]; then  # TODO once we have a 2.2 bench, add it here (or in planned separate bench script)
840            echo 'myget 3DBenchs/BENCHCMIP6.tar.gz'
841            myget 3DBenchs/BENCHCMIP6.tar.gz
842            tar xvzf BENCHCMIP6.tar.gz
843            sed -e "s:VEGET=n:VEGET=y:" config.def > tmp
844            mv -f tmp config.def
845            if [[ $with_xios = 1 ]]; then
846                cp ../../ORCHIDEE/src_xml/context_orchidee.xml .
847                echo '<context id="orchidee" src="./context_orchidee.xml"/>' > add.tmp
848                cp ../../ORCHIDEE/src_xml/field_def_orchidee.xml .
849                cp ../../ORCHIDEE/src_xml/file_def_orchidee.xml .
850                cp ../../ORCHIDEE/src_xml/file_def_input_orchidee.xml .
851                if [[ -f ../../ORCHIDEE/src_xml/context_input_orchidee.xml ]]; then
852                       cp ../../ORCHIDEE/src_xml/context_input_orchidee.xml .
853                       echo '<context id="orchidee" src="./context_input_orchidee.xml"/>' >> add.tmp
854                fi
855                sed -e '/id="LMDZ"/r add.tmp' iodef.xml > tmp
856                mv tmp iodef.xml
857                sed -e'{/sechiba1/ s/enabled="_AUTO_"/type="one_file" enabled=".TRUE."/}' \
858                    file_def_orchidee.xml > tmp
859                \mv -f tmp file_def_orchidee.xml
860                sed -e 's@enabled="_AUTO_"@type="one_file" enabled=".FALSE."@' \
861                    file_def_orchidee.xml > tmp
862                \mv -f tmp file_def_orchidee.xml
863                sed -e 's@output_level="_AUTO_"@output_level="1"@' \
864                    file_def_orchidee.xml > tmp
865                \mv -f tmp file_def_orchidee.xml
866                sed -e 's@output_freq="_AUTO_"@output_freq="1d"@' \
867                    file_def_orchidee.xml > tmp
868                \mv -f tmp file_def_orchidee.xml
869                sed -e 's@compression_level="4"@compression_level="0"@' \
870                    file_def_orchidee.xml > tmp
871                \mv -f tmp file_def_orchidee.xml
872                sed -e 's@XIOS_ORCHIDEE_OK = n@XIOS_ORCHIDEE_OK = y@' \
873                    orchidee.def > tmp
874                \mv -f tmp orchidee.def
875            fi
876        fi
877
878        if [[ -f ../arch.env ]]; then source ../arch.env; fi
879
880        echo "EXECUTION DU BENCH"
881        date
882        if (! $bench_cmd &> out.bench) || ! (tail -n 1 listing | grep "Everything is cool"); then
883            tail listing
884            echo "Bench FAILED, exiting"; exit 1
885        fi
886        date
887        tail listing
888    fi
889
890    # 1D case
891    if [[ $SCM = 1 ]]; then
892        cd "$MODEL"
893        myget 1D/1D.tar.gz
894        tar xf 1D.tar.gz
895        cd 1D
896        if [[ $rad = "oldrad" ]]; then
897            sed -i'' -e 's/^rad=.*$/rad=oldrad/' run.sh
898            sed -i'' -e 's/^rad=.*$/rad=oldrad/' bin/compile
899        elif [[ $rad = ecrad ]] ; then
900                        sed -i'' -e 's/^rad=.*$/rad=ecrad/' run.sh
901                        sed -i'' -e 's/^rad=.*$/rad=ecrad/' bin/compile
902                    fi
903        echo "Running 1D/run.sh, log in $(pwd)/run1d.log"
904        ./run.sh &> "$(pwd)/run1d.log"
905    fi
906}
907
908# If sourced: returns, else run setup
909if [[ ! "${BASH_SOURCE[0]}" = "$0" ]]; then return 0; fi
910
911echo "install_lmdz.sh DEBUT $(date)"
912
913set_default_params
914read_cmdline_args "$@"
915ensure_correct_option_combinations
916#download_model
917create_model_tar
918init_arch
919check_available_software
920get_lmdz_version
921get_orchidee_version
922install_netcdf
923install_IOIPSL
924install_XIOS
925compile_orchidee
926compile_lmdz
927run_bench
Note: See TracBrowser for help on using the repository browser.