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



我倾向于使用临时变量来";忽略";Fortran中的一些子程序输出。由于Fortran在Matlab中没有像波浪号(~(这样的命令,我必须从子程序中获得输出,但我将它们分配给相同大小的临时变量。我一直倾向于让东西看起来更干净,没有实际目的。

多年来,到目前为止,我对它没有任何问题;今天,我遇到了一些问题。事实证明,当我把输出分配给同一个变量(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中是被禁止的。因为您的代码不符合标准,所以输出可以是任何内容。

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

最新更新