simon2010
Technical User
- Apr 16, 2009
- 7
Hi there. I am trying to modularize my code as it is getting complicated. However, the code is causing me problems. Below is a simplified version of the problem. It would be great if anyone knows a solution or even a completely alternative approach. Thanks in advance even just for comments!
I have some data. Each set "data_set=1,nset" of data has "idata=1,npoints(data_set)" data points. For each data point there corresponds a 5 dimensional array of numbers which I use for a calculation related to that data point.
So to store all these numbers I use the object "numbers_data(5,nset)", whose type is an rank-1 allocatable
array "nd_part)", by allocating "numbers_data(i,data_set)%nd_part", "i=1,5", for each value of "data_set" to have "npoints(data_set)" components. Here is the program:
module type_nd
type nd
complex*16, allocatable :: nd_part)
end type
end module type_nd
use type_nd
implicit none
integer nset
parameter(nset=2)
type(nd) :: numbers_data(5,nset)
integer data_set,idata,npoints(nset),i
npoints(1)=3
npoints(2)=9
do data_set=1,nset
do i=1,5
allocate(numbers_data(i,data_set)%nd_part(npoints(data_set)))
enddo
enddo
c Now let's assign the components of the arrays to something
do data_set=1,nset
do i=1,5
do idata=1,npoints(data_set)
numbers_data(i,data_set)%nd_part(idata)=dfloat(idata)
enddo
enddo
enddo
c Now print some out to check (the result should be equal to 4 in each case)
do i=1,5
print*, 'numbers_data=',numbers_data(i,2)%nd_part(4)
enddo
end
The above program works with no problem. However, I want to do something more involved. As I said, the 5-dimensional array corresponding to each data point needs to go into a calculation which is independent of the value of "idata" and "data_set". What I could do is, for each value of "idata" and "data_set", copy the 5 components of numbers_data to a simple 5 dimensional array which is to be used in the calculation. However, this takes up too much valuable time. So instead I use a pointer, "numbers". Now in the declaration above, "type(nd) :: numbers_data(5,nset)",
"numbers_data" becomes a "target":
type(nd), target :: numbers_data(5,nset)
and I add the declaration
complex*16, pointer :: numbers)
So now suppose I want to pick out the 5 numbers for the data point "data_set=2", "idata=4". Then, at the end of the above program, I insert
data_set=2
idata=2
numbers=>numbers_data(1:5,data_set)%nd_part(idata)
Now when I print them out I should get the answer "2" again (because "idata=2"):
do i=1,5
print*, 'dd=',numbers(i)
enddo
But I don't (actually I get 0, 5, 0, 4, 0 in that order!), and I have no idea why.
Furthermore, I have another problem, maybe related. If I instead take "idata=4" above, i.e.
data_set=2
idata=4
numbers=>numbers_data(1:5,data_set)%nd_part(idata)
then I get the error message "0: Subscript out of range for array numbers_data%nd_part (temp.f: 52) <newline> subscript=4, lower bound=1, upper bound=3, dimension=1"
(I compiled using pgf95_7.1 -Mbounds -Mextend -o temp temp.f. If I omit the "-Mbounds" flag the error message goes away but then I get 0, 7, 0, 6, 0 as output.)