Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory 当多个输出被分配给主程序中的同一变量时,Fortran子例程NaN出现问题_Memory_Gfortran_Subroutine - Fatal编程技术网

Memory 当多个输出被分配给主程序中的同一变量时,Fortran子例程NaN出现问题

Memory 当多个输出被分配给主程序中的同一变量时,Fortran子例程NaN出现问题,memory,gfortran,subroutine,Memory,Gfortran,Subroutine,我倾向于使用临时变量来“忽略”Fortran中的一些子程序输出。由于Fortran在Matlab中没有像tilde(~)这样的命令,我必须从子程序中获取输出,但我将它们分配给具有正确大小的相同临时变量。我一直倾向于让事情看起来更干净,没有实际意义 到目前为止,多年来我对它没有任何问题;今天,我遇到了一些问题。事实证明,当我将输出分配给同一个变量(temp1)时,C_DU是一个全为零的矩阵,尽管它不应该是这样 我的问题是,将多个子例程输出分配给主代码中的同一个变量是否不切实际?为什么当我这么做的时

我倾向于使用临时变量来“忽略”Fortran中的一些子程序输出。由于Fortran在Matlab中没有像tilde(~)这样的命令,我必须从子程序中获取输出,但我将它们分配给具有正确大小的相同临时变量。我一直倾向于让事情看起来更干净,没有实际意义

到目前为止,多年来我对它没有任何问题;今天,我遇到了一些问题。事实证明,当我将输出分配给同一个变量(temp1)时,C_DU是一个全为零的矩阵,尽管它不应该是这样

我的问题是,将多个子例程输出分配给主代码中的同一个变量是否不切实际?为什么当我这么做的时候,一切都突然变成了零?谢谢你的帮助

以下是一个最低限度的工作示例:

program example
    implicit none

    real(kind=8) :: temp1(3,3)
    real(kind=8) :: temp2(3,3)
    real(kind=8) :: temp3(3,3)
    real(kind=8) :: temp4(3,3)
    real(kind=8) :: temp5(3,3)
    real(kind=8) :: r_PN_U(3)

    call r_calc(4.2d0, &
        0d0, 0d0, &
        0d0, 0d0, &
        0d0, 0d0, &
        0d0,      &
        0d0, 0d0, &
        0d0,      &
        0d0, 0d0, &
        0d0, 0d0, &
        r_PN_U, &
        temp1, temp1, temp1, temp1, temp1)

    call r_calc(4.2d0, &
        0d0, 0d0, &
        0d0, 0d0, &
        0d0, 0d0, &
        0d0,      &
        0d0, 0d0, &
        0d0,      &
        0d0, 0d0, &
        0d0, 0d0, &
        r_PN_U, &
        temp1, temp2, temp3, temp4, temp5)
end program example

subroutine r_calc(rin, &
    e,     ed,     &
    f,     fd,     &
    v,     vd,     &
    vp,            &
    w,     wd,     &
    wp,            &
    theta, thetad, &
    y_pos, z_pos, &
    r_PN_U, &
    C_DU_theta, C_DU_beta, C_DU_zeta, C_DU, C_UD)
    implicit none

    real(kind=8), intent(in) :: rin

    real(kind=8), intent(in) :: e,     ed
    real(kind=8), intent(in) :: f,     fd
    real(kind=8), intent(in) :: v,     vd
    real(kind=8), intent(in) :: vp
    real(kind=8), intent(in) :: w,     wd
    real(kind=8), intent(in) :: wp
    real(kind=8), intent(in) :: theta, thetad

    real(kind=8), intent(in) :: y_pos, z_pos

    real(kind=8), intent(out) :: r_PN_U(3)

    real(kind=8), intent(out) :: C_DU_theta(3,3)
    real(kind=8), intent(out) :: C_DU_beta (3,3)
    real(kind=8), intent(out) :: C_DU_zeta (3,3)
    real(kind=8), intent(out) :: C_DU      (3,3)
    real(kind=8), intent(out) :: C_UD      (3,3)

    real(kind=8) :: beta, zeta

    beta  = -atan(wp) ! [rad], flap down angle
    zeta  =  atan(vp) ! [rad], lead angle

    call angle2dcm(theta, beta, zeta, C_DU_theta, C_DU_beta, C_DU_zeta, C_DU)

    print *, C_DU ! gives all zero in the first call, correct values in the second call

    C_UD = transpose(C_DU)

    r_PN_U = [rin+e+f, v, w] + matmul(C_UD, [0d0, y_pos, z_pos])

end subroutine r_calc

subroutine angle2dcm(phi, theta, psi, C_phi, C_theta, C_psi, C_out)
    implicit none

    ! Calculates the direction cosine matrix in psi - theta - phi (3 - 2 - 1) order
    ! Difference from "angle2dcm" subroutine is the extra outputs

    real(kind=8), intent(in)  :: phi, theta, psi

    real(kind=8), intent(out) :: C_psi(3,3), C_theta(3,3), C_phi(3,3), C_out(3,3)

    C_phi(1,1:3) = [1d0,       0d0,      0d0]
    C_phi(2,1:3) = [0d0,  cos(phi), sin(phi)]
    C_phi(3,1:3) = [0d0, -sin(phi), cos(phi)]

    C_theta(1,1:3) = [cos(theta), 0d0, -sin(theta)]
    C_theta(2,1:3) = [       0d0, 1d0,         0d0]
    C_theta(3,1:3) = [sin(theta), 0d0,  cos(theta)]

    C_psi(1,1:3) = [ cos(psi),  sin(psi), 0d0]
    C_psi(2,1:3) = [-sin(psi),  cos(psi), 0d0]
    C_psi(3,1:3) = [      0d0,       0d0, 1d0]

    C_out = matmul(C_phi, matmul(C_theta,C_psi)) ! psi - theta - phi (3 - 2 - 1) order

end subroutine angle2dcm

将同一变量指定为可能在子例程中更改的多个实际参数是非法的。这称为别名,在Fortran中是禁止的。因为您的代码不符合标准,所以输出可以是任何内容


为每个参数使用不同的变量。

将同一变量指定为可能在子例程中更改的多个实际参数是非法的。这称为别名,在Fortran中是禁止的。因为您的代码不符合标准,所以输出可以是任何内容


每个参数使用不同的变量。

我不知道。谢谢你的回答。同样的情况对子程序的输入有效吗?我可以在主程序中使用同一个变量作为子程序的两个或多个不同输入吗?可以,但不能更改此类输入参数。可以使用目标和指针进行进一步的控制。我不知道。谢谢你的回答。同样的情况对子程序的输入有效吗?我可以在主程序中使用同一个变量作为子程序的两个或多个不同输入吗?可以,但不能更改此类输入参数。可以使用目标和指针进行进一步的控制。