user157588
user157588

Reputation: 259

alternatives of using real as array index

I wish to solve the schrodinger time dependent equation . In my code, I introduced two arrays, namely yc and yr, for the complex and real part of wavefunction . Later I tried to store the values in the array in the format yc(x(i),t(j)) as y(x,t) function .There is a warning showing that I am using real as index of an array . I understand where the problem lies, but what is the way out ? Can I define a function whose values I can assign during my program discreetly as an alternative to that array ?

I have googled about this could not find any solution .

function v(x) result(s)
real::s,x
if (x<0) then
s=0
else
s=1
end if
end function v

real::t(10000),x(10000),yc(10000,10000),yr(10000,10000),tf,xi,xf,d
integer::i,j,k,l,m
write(*,*) "tf,xi,xf,step size"
read(*,*) tf,xi,xf,d

x(1)=xi
t(1)=0

i=1
1   if(x(i).lt.xf) then
x(i+1)=x(i)+d
i=i+1
goto 1
end if


do j=1,i
yr(x(j),0)=exp(-x(j)**2)             !initial wavefunction
yc(x(j),0)=0
end do

do l=1,i
k=1
3    if(t(k).lt.tf) then


yr(x(l),t(k+1))=yr(x(l),t(k))-(yc(x(l)+2*d,t(k))-2*yc(x(l)+d,t(k))+yc(x(l),t(k)))/d&
+v(x(l))*yc(x(l),t(k))*d

yc(x(l),t(k+1))=yc(x(l),t(k))+(yr(x(l)+2*d,t(k))-2*yr(x(l)+d,t(k))+yr(x(l),t(k)))/d&
-v(x(l))*yr(x(l),t(k))*d

k=k+1
goto 3

end if
end do



open(1,file="q.dat")
do m=1,i
write(1,*) x(m),yr(x(m),t(1))**2+yc(x(m),t(1))**2
end do
close(1)

end










 expected result :$ yi(x,t)^2+yc(x,t)^2  versus x at different t
obtained error : 
 yr(x(j),0)=exp(-x(j)**2)             !initial wavefunction
   1
Warning: Legacy Extension: REAL array index at (1)
schrodinger.f90:27:8:

 yr(x(j),0)=exp(-x(j)**2)             !initial wavefunction
        1
Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 2
schrodinger.f90:28:3:    and so on`enter code here`

I have googled how to use real as index but no use .

function v(x) result(s)
real::s,x
if (x<0) then
s=0
else
s=1
end if
end function v

real::t(10000),x(10000),yc(10000,10000),yr(10000,10000),tf,xi,xf,d
integer::i,j,k,l,m
write(*,*) "tf,xi,xf,step size"
read(*,*) tf,xi,xf,d

x(1)=xi
t(1)=0

i=1
1   if(x(i).lt.xf) then
x(i+1)=x(i)+d
i=i+1
goto 1
end if


do j=1,i
yr(x(j),0)=exp(-x(j)**2)             !initial wavefunction
yc(x(j),0)=0
end do

do l=1,i
k=1
3    if(t(k).lt.tf) then
yr(x(l),t(k+1))=yr(x(l),t(k))-(yc(x(l)+2*d,t(k))-2*yc(x(l)+d,t(k))+yc(x(l),t(k)))/d&
+v(x(l))*yc(x(l),t(k))*d

yc(x(l),t(k+1))=yc(x(l),t(k))+(yr(x(l)+2*d,t(k))-2*yr(x(l)+d,t(k))+yr(x(l),t(k)))/d&
-v(x(l))*yr(x(l),t(k))*d

k=k+1
goto 3

end if
end do



open(1,file="q.dat")
do m=1,i
write(1,*) x(m),yr(x(m),t(1))**2+yc(x(m),t(1))**2
end do
close(1)

end

expected : data files with wavefunction at different time .

obtained : warning - using real as indices

Upvotes: 0

Views: 374

Answers (1)

RussF
RussF

Reputation: 721

Just reference your arrays using the integers i,j etc. If you have x(i),t(j) then yr(i,j) is the corresponding value. To get the offsets of +2*d etc you only need to use use +2 instead. e.g. yr(l+2,k) rather than yr(x(l)+2*d,t(k)).

Also, please use implicit none for a start and get a hold of a modern Fortran reference book or similar. All those go tos are a bit hard on the eyes.

Upvotes: 2

Related Questions