Search code examples
type-conversionfortrangfortran

fortran (std2003)conversion of logical to numeric type


I have some older program I try to compile. I get an error

            if (sum(npsortie>0)) write(*,'(a,i8,a,i8,a,i8,a,i8,a)') &
                  1
Error: 'array' argument of 'sum' intrinsic at (1) must have a numeric type
advect.f90:577:21:


           if(sum(int(mask_part))>0) then
                     1
Error: 'a' argument of 'int' intrinsic at (1) must have a numeric type
advect.f90:580:69:


             meanz=sum(ztra1(1:numpart),DIM=1,MASK=mask_part)/sum(int(mask_part))
                                                                     1
Error: 'a' argument of 'int' intrinsic at (1) must have a numeric type

mask_part and npsortie are arrays of LOGICAL. I use gfortran-7, this is the only compiler version I can use, because of libraries which are compiled with this compiler version.

The parameters to the compiler are

gfortran-7 -O2 -fopenmp -pedantic -std=f2003 -cpp -Wall -fconvert=swap -fall-intrinsics -fmax-errors=20 -I /usr/include -L /urs/include -c advect.f90

For a hint on how to fix this I would be very grateful. From the documentation I see that it used to work implicitly, but this seems to have changed.


Solution

  • Let's assume that npsortie and mask_part are indeed logical:

    if (sum(npsortie>0)) ...`
    

    this appears to query, whether there are any .true. elements of the logical array. For that one should use if (any(npsortie)) ....

    if(sum(int(mask_part))>0) ...
    

    this appears to do the same and if (any(mask_part)) ... should be equivalent.

    meanz=sum(ztra1(1:numpart),DIM=1,MASK=mask_part)/sum(int(mask_part))
    

    here sum(int(mask_part)) seems to be the number of .true. elements. For that use count(mask_part) instead. the count intrinsic counts the number of true elements in the array.


    If you had

    if (sum(numeric_array>0)) ...
    

    and you want to know the count of numeric elements that have a value higher than zero, use count(numeric_array>0).