[v4.0.x] Compilation with AGRIF fails for `jpksed` in *sedchem.f90*

I would like to run NEMO 4.0.6 with both AGRIF and TOP, which should be possible according to
But unfortunately I don’t manage to compile with both key_top and key_agrif activated.

I can compile AGRIF_DEMO and ORCA2_ICE_PISCES without problems, but if I try to combine them like this

$ ./makenemo -r ORCA2_ICE_PISCES -n ORCA2_ICE_PISCES_AGRIF -m XC50_dmi_fast add_key 'key_agrif'

or like this

$ ./makenemo -r AGRIF_DEMO -n AGRIFTOP -m XC50_dmi_fast -d 'OCE TOP ICE NST' add_key 'key_top'

I get the following error message:

touch /home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/flags/FFLAGS__nemo__sedchem.flags
fcm_internal compile:F nemo /home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/ppsrc/nemo/sedchem.f90 sedchem.o
ftn -o sedchem.o -I/home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/inc -i4 -r8 -O2 -fp-model fast -fpp -init=zero -I/data/ang/NEMO/sourcecode/xios-2.5/inc -I/opt/cray/pe/netcdf-hdf5parallel/ -I/opt/cray/pe/hdf5-parallel/ -c /home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/ppsrc/nemo/sedchem.f90
/home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/ppsrc/nemo/sedchem.f90(337): error #7915: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association - may be used in multiple contained procedures.   [JPKSED]
     &1:jpksed) :: p_hini
/home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/ppsrc/nemo/sedchem.f90(337): error #6404: This name does not have a type, and must have an explicit type.   [JPKSED]
     &1:jpksed) :: p_hini
compilation aborted for /home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/ppsrc/nemo/sedchem.f90 (code 1)
fcm_internal compile failed (256)
/home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/Makefile:2745: recipe for target 'sedchem.o' failed
gmake: *** [sedchem.o] Error 1
gmake -f /home/ang/nemoSourcecode/release4.0.6_r15206/cfgs/AGRIFTOP/BLD/Makefile -j 1 all failed (2) at /netapp/research/ang/nemoSourcecode/release4.0.6_r15206/ext/FCM/bin/../lib/Fcm/Build.pm line 597.
->Make: 877 seconds
->TOTAL: 896 seconds
Build failed on Thu Sep  2 13:29:44 2021.

Looking into ppsrc/nemo/sedchem.f90 I get the impression that maybe the variable jpksed didn’t get replaced properly by the Agrif-preprocessor??

332    SUBROUTINE ahini_for_at_sed(p_hini)                                                                                                                                                                         333                                                                                                                                                                                                               
335       use Agrif_Util                                                                                                                                                                                           336       real(wp), intent(OUT), dimension(1: Agrif_tabvars_i(927)% iarray0,&                                                                                                                                     
337      &1:jpksed) :: p_hini   

This line corresponds to

REAL(wp), DIMENSION(jpoce,jpksed), INTENT(OUT)  ::  p_hini

in src/TOP/PISCES/SED/sedchem.F90

I would be happy if someone has a hint for me…

PS: I use the intel compiler and this is my arch-file:

%NCDF_HOME           /opt/cray/pe/netcdf-hdf5parallel/       
%HDF5_HOME           /opt/cray/pe/hdf5-parallel/ 
%NCDF_INC            -I%NCDF_HOME/include -I%HDF5_HOME/include 
%NCDF_LIB            -L%HDF5_HOME/lib -L%NCDF_HOME/lib -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lz

%XIOS_HOME           /data/ang/NEMO/sourcecode/xios-2.5
%XIOS_INC            -I%XIOS_HOME/inc
%XIOS_LIB            -L%XIOS_HOME/lib -lxios

%CPP                 cpp -Dkey_nosignedzero -DAGRIF_MPI
%FC                  ftn
%FCFLAGS             -i4 -r8 -O2 -fp-model fast -fpp -init=zero 
%FFLAGS              %FCFLAGS
%LD                  CC -Wl,"--allow-multiple-definition"
%FPPFLAGS            -P -C -traditional -x f95-cpp-input
%AR                  ar
%ARFLAGS             rs
%MK                  gmake

%CC                  cc
%CFLAGS              -O0

Hi Andrea, There does indeed appear to be a strange scoping problem with the SED modules when compiled with AGRIF. It isn’t clear whether or not jpksed needs to be replaced by the AGRIF preprocessor. I would assume it is the same in all nests. However, it can be set via the namelist (not very coding convention compliant!). Regardless, it should be known to in sedchem.F90 from the par_sed module (which is included in the SED module). Perhaps the layers of modules have confused the scope?

I can get your first example to compile with these two additional USE statements:

Index: TOP/PISCES/SED/sedchem.F90
--- TOP/PISCES/SED/sedchem.F90	(revision 15228)
+++ TOP/PISCES/SED/sedchem.F90	(working copy)
@@ -5,6 +5,7 @@
    !! sediment :   Variable for chemistry of the CO2 cycle
    !!   modules used
+   USE par_sed, ONLY : jpksed
    USE sed     ! sediment global variable
    USE sedarr
    USE eosbn2, ONLY : neos
Index: TOP/PISCES/SED/seddta.F90
--- TOP/PISCES/SED/seddta.F90	(revision 15228)
+++ TOP/PISCES/SED/seddta.F90	(working copy)
@@ -7,6 +7,7 @@
    !! * Modules used
    USE sed
    USE sedarr
+   USE par_pisces
    USE phycst, ONLY : rday
    USE iom
    USE lib_mpp         ! distribued memory computing library

-neither of which should be necessary. No guarantee this will run and it’ll take a real AGRIF expert to provide an explanation and proper solution, but you could try this in the absence of any other ideas.

My experience is that using layers of modules to define a variable does not work with AGRIF pre-processing. Using par_sed in sedchem.F90 appears unavoidable. Looking at the trunk, which has an AGRIF-TOP reference configuration (e.g. AGRIF_DEMO, running Age tracer only though), par_sed is indeed used.
This said, @acc, why did you have to use par_pisces in seddta.F90 ?

Using par_sed directly fixed sedchem.F90 but then I encountered issues with seddta.F90 that claimed not to know any of the tracer indices (jpno3 etc.). Another layered modules issue, I believe (although it isn’t immediately obvious how even the non-AGRIF version of seddta.F90 is supposed to know?).

Thanks Andrew: I raise a ticket. @andrea.gierisch, you’ll find the above compilation fixes in the NEMO r4.0-HEAD (which will become NEMO 4.0.7 very soon).

I did the changes for both the trunk and 4.0-HEAD. I just run a quick (ha ha ha) sette to see if it breaks anything and then I’ll commit

ok, refers to ticket #2719 then.
But it should already be fine in the trunk ?

I’ll put the exact same USE in 4.0 and the trunk (not the case right now).