违反 Fortran 形状匹配规则

Fortran shape matching rules violated

我想达到什么目的

我正在尝试编写一个将矩阵(二维数组)作为输入并将其很好地打印到标准控制台输出的子例程。

问题

error #6634: The shape matching rules of actual arguments and dummy arguments have been violated.   ['U']

代码

打印矩阵的子程序在此模块中

模块

    MODULE LinearSystems
    IMPLICIT NONE
    private
    ...
    public showMatrix
    ...
    contains
    subroutine showMatrix(a, n, m, name)
    implicit none
    double precision, dimension(:,:), intent(in) :: a
    character, dimension(:), intent(in), optional :: name
    integer, intent(in) :: n,m
    integer :: i, j
    write(*,*) "*** Show Matrix ", name, " ***"
    do i = 1, n
        do j = 1, m
            write(*,'(F8.4)',advance="no") a(i,j)
        end do
        write(*,*)
    end do
    end subroutine showMatrix

主程序调用它

主程序

program PoisonEquation
    ...
    use LinearSystems
    implicit none
    double precision, dimension(:,:), allocatable :: u,...
    integer :: n = 700
    allocate(u(n-1,n-1))
    ...
    call showMatrix(u, n-1,n-1, "U")

我期待收到有关如何改进此代码片段并使其无错误的提示。

名称虚拟参数是一个假定的形状数组(参见 dimension(:) 声明)。用于实际参数的 "U" 文字是标量(错误消息指的是这个文字)。

如果伪参数是假定的形状数组,则实际参数的等级应与伪参数的等级相同(F2018 15.5.2.4p16)。

弄清楚你是想pass/receive一个数组还是一个标量,然后修改代码。

问题已解决

问题出在字符初始化中,在解决这个问题以及解决主程序中的一个问题后,导致 u 在传递给子程序之前被释放,代码现在可以工作,子程序可以打印看起来像这样:

subroutine showMatrix(a, name)
    implicit none
    double precision, dimension(:,:), intent(in) :: a
    character(*), intent(in), optional :: name
    integer :: i, j
    write(*,*) "*** Show Matrix ", name, " ***"
    do i = 1, size(a,1)
        do j = 1, size(a,2)
            write(*,'(F8.4)',advance="no") a(i,j)
        end do
        write(*,*)
    end do
end subroutine showMatrix