代码结果一直为零
在我的程序当中,调用子程序输出计算结果时,结果一直为0,不知道是哪里出现了问题,会是子例程出现了错误吗?Program workfile
Implicit none
integer , parameter :: q = 1000
real , parameter :: t1 = 3.12 , t2 = 0.29 , t3 = -0.0103
complex , parameter :: i = (0, 1) , t0 = (0,0)
real , parameter :: PI = acos(-1.0) , a0 = 1.42e-10
real :: b1(2), b2(2) , a1(2) , a2(2) , m1 , m2
real , allocatable :: k(:,:,:)
complex :: f1 , f2
integer :: x , y
complex :: f1_kx, f1_ky, f2_kx, f2_ky
allocate(k(2,q-1,q-1))
b1 = [ 2*PI/a0*sqrt(3.0) , 2*PI/a0 ]
b2 = [ 2*PI/a0*sqrt(3.0) , -2*PI/a0 ]
a1 = [ a0*sqrt(3.0)/2 , a0/2 ]
a2 = [ a0*sqrt(3.0)/2 , -a0/2 ]
Forall(x=1:size(k,2), y=1:size(k,3))
k(:,x,y) = (b1*x+b2*y)/q
End Forall
! print*,k(:,1,2)
m1 = dot_product(a1 , k(:,1,2))
m2 = dot_product(a2 , k(:,1,2))
f1 = 1+exp(i*m1)+exp(i*m2)
f2 = 1+exp(-i*m1)+exp(-i*m2)
call diff(i,a0,k(1,1,2),k(2,1,2))
print *, f1_kx,f1_ky, f2_kx, f2_ky
contains
subroutine diff(i,a,x,y)
implicit none
real,intent(in) :: a, x, y
complex :: i, f1_kx, f1_ky, f2_kx, f2_ky
f1_kx = (sqrt(3.0)/2)*a*i*exp(a*i*(sqrt(3.0)*x/2+y/2))+(sqrt(3.0)/2)*a*i*exp(a*i*(sqrt(3.0)*x/2-y/2))
f2_kx = -(sqrt(3.0)/2)*a*i*exp(a*i*(sqrt(3.0)*x/2+y/2))-(sqrt(3.0)/2)*a*i*exp(a*i*(sqrt(3.0)*x/2-y/2))
f1_ky = (-1/2)*a*i*exp(a*i*(sqrt(3.0)*x/2-y/2))+(1/2)*a*i*exp(a*i*(sqrt(3.0)*x/2+y/2))
f2_ky = (1/2)*a*i*exp(a*i*(sqrt(3.0)*x/2-y/2))-(1/2)*a*i*exp(a*i*(sqrt(3.0)*x/2+y/2))
return
endsubroutine
结果如下:
(0.00000000,0.00000000) (0.00000000,0.00000000) (0.00000000,0.00000000) (0.00000000,0.00000000)
34行定义的变量只在subroutien diff内部有效,出了作用域就无效了。如果要访问主程序中的变量,注释这四个变量。另外整数做除法只能得到整数 1/2=0 Transpose 发表于 2022-3-16 16:16
34行定义的变量只在subroutien diff内部有效,出了作用域就无效了。如果要访问主程序中的变量,注释这四个 ...
请问一下,什么叫做注释这四个变量呀。我在主程序当中不是也定义了相对应的变量嘛 变量有自己的作用域,子程序中的那几个变量(虽然名字一样)和主程序没什么关系。所以相当于主程序的变量没有使用。 Transpose 发表于 2022-3-16 19:29
变量有自己的作用域,子程序中的那几个变量(虽然名字一样)和主程序没什么关系。所以相当于主程序的变量没有 ...
哦哦,我知道啦。也就是说虽然定义的是一样的名字,但是真正赋值的时候还是需要对主程序当中的变量赋值,所以再次定义就会出现两个变量是不一致的。
但是我在注销34行的变量之后,任然出现了报错
78 | subroutine diff(i,a,x,y)
| 1
Error: Symbol ‘i’ at (1) has no IMPLICIT type
workfile_ed4.f90:73:33:
73 | call diff(i,a0,k(1,1,2),k(2,1,2))
| 1
Error: Type mismatch in argument ‘i’ at (1); passed COMPLEX(4) to UNKNOWN
这是怎么回事呢 Transpose 发表于 2022-3-16 19:29
变量有自己的作用域,子程序中的那几个变量(虽然名字一样)和主程序没什么关系。所以相当于主程序的变量没有 ...
sorry,我已经知道了,变量i,还是仍需在子程序当中定义的
页:
[1]