Community
cancel
Showing results for
Did you mean:
Highlighted
Beginner
20 Views

## Pointer array of pointer

Thanks for all. This is my first post.
I have a customized type such as

type dhd_particle
real (kind=8) :: postn(3)=0.D0  ! position
real (kind=8) :: veloc(3)=0.D0  ! velocity
real (kind=8) :: vorti(3)=0.D0  ! vorticity
real (kind=8) :: force(3)=0.D0  ! force
real (kind=8) :: torqu(3)=0.D0  ! torque
end type

and make 10 of them

type (dhd_particle) :: sphere(10)

Now I have pointer array such as

real(kind=8),pointer :: x(10),y(10),z(10),r(30)

then link them like

x => sphere(:)%postn(1)
y => sphere(:)%postn(2)
z => sphere(:)%postn(3)

up to which I did not have any problem.  What I want to have is a pointer of "r", of which components are

r(1) = x(1)
r(2) = y(1)
r(3) = z(1)
r(4) = x(2)
...
r(30) = z(10)

How could I make the pointer array r either using x, y, and z or directly form sphere array?
I first would like to know this is possible or not. If possible, how do I write a code?

Any help will be highly appreciated.

Albert

Tags (1)
5 Replies
Highlighted
Valued Contributor II
20 Views

You should realise that a declaration like:

real, dimension(:), pointer :: p

does not create an array of pointers, but a pointer to an array.

To get an array of pointers you will have to use a derived type like:

type some_pointer
real, pointer :: p
end type some_pointer

type(some_pointer), dimension(10) :: p

Individual pointers are then accessed as: p(1)%p.

This might help build the sort of data structure you are looking for, but could you explain the purpose? It seems to me a trifle complicated - you may have a hard time trying to keep track of these things.

Alternative that comes to mind is the use of the ASSOCIATE construct. That allows the sort of things you are looking for directly. Well, I didn't check very carefully, but I am fairly positive.

Highlighted
Beginner
20 Views

Dear Arjen:

Thank you so much.  I will definitely look at those.
The purpose is as follows.  I do particle dynamics simulation, similar to molecular dynamics, solving N-body, F=ma. That is why I have the above specific derived data type. But, when I include fluid-particle interaction, I need a vector (1D array) consisting of 3N components for position, velocity, force and so forth. The first three is for particle 1, the second three (4-6) is for particle 2, and so forth. Copying from individual object to a vector might take long time for mem_copy, so I thought pointer might save some running time.

Thanks a lot.

Albert

Highlighted
Valued Contributor II
20 Views

hm, it may actually be easier to turn it around:

real(kind=8), dimension(30) :: r

type dhd_particle
real(kind=8), dimension(:), pointer :: postn
...
end type dhd_particle

sphere(i)%postn => r(3*(i-1)+1:3*i) ! loop over i
x => r(1::3)
y => r(2::3)
z => r(3::3)

Highlighted
Beginner
20 Views

Oh!!!!!
This is a genius concept!!
What I need the most is line 08.  I cannot thank enough. ^^

I did have any experience dealing with pointers in MPI (fortran) since I have to broadcast particles of data type "DHD_PARTICLE".  I hope it works. Anyway your suggestion is a real conceptual breakthrough to me.

I truly appreciate it.

Albert

Highlighted
Valued Contributor II
20 Views

You're welcome :).

As for MPI: MPI sends data, not pointers, so I do not think that will be a problem.