gpt4 book ai didi

fortran - 变量定义上下文中的 INTENT(IN) 虚拟参数

转载 作者:行者123 更新时间:2023-12-01 23:29:35 25 4
gpt4 key购买 nike

当我运行我的代码时,我在我的子例程中收到一条错误消息。

此代码来自 Kincaid & Cheney 的书中关于求解椭圆偏微分方程的 Gauss Seidel 方法的练习。

错误信息是:

dummy argument 'u' with INTENT(IN) in variable definition context (assignment) at (1).



我在下面的代码中引用了(1)。如何修复子例程,以免出现错误消息?
subroutine seidel(ax,ay,nx,ny,h,itmax,u)     
real, dimension(0:nx,0:ny), intent(in) :: u
real, intent(in) :: ax,ay, h
integer, intent(in) :: nx, ny, itmax
integer:: i,j,k

do k = 1,itmax
do j = 1,ny-1
y = ay + real(j)*h
do i = 1,nx-1
x = ax + real(i)*h
v = u(i+1,j) + u(i-1,j) + u(i,j+1) + u(i,j-1)
u(i,j) = (v - h*h*g(x,y))/(4.0 - h*h*f(x,y)) (1)
end do
end do
end do
end subroutine seidel

最佳答案

intent(in)是对编译器的 promise ,即例程不会尝试更新参数的值。您显示的代码打破了这一 promise :

u(i,j) = (v - h*h*g(x,y))/(4.0 - h*h*f(x,y))

通过不违背您的 promise 来解决此问题,或者在这种情况下可能更合适,使意图 inout , 像这样
real, dimension(0:nx,0:ny), intent(inout) :: u        
inout告诉编译器该例程将被传递参数并可对其进行修改。

(我认为这可能是重复的,但还找不到。)

关于fortran - 变量定义上下文中的 INTENT(IN) 虚拟参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41782442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com