I am new to Fortran and am developing a code in which I frequently do operations on two similar derived types (in this case representing models of particles in a scientific computing code). The two types are identical except one has only translational position and velocity components whereas the other also includes angular positions and velocities.
type(particle_A)
real :: position(3)
real :: velocity(3)
end type
type(particle_B)
real :: position(6)
real :: velocity(6)
end type
I have another derived type which groups arrays of both kinds of particles (together with some other data in the actual application):
type(particleGroup)
type(particle_A), allocatable :: particleA_array(:)
integer :: NparticlesA
type(particle_B), allocatable :: particleB_array(:)
integer :: NparticlesB
character(len=30) :: particle_kind
end type
Where both arrays are allocated and re-allocated during runtime if more particles enter the simulation on this particular process. Depending on what particle_kind is set to (during initialization) I want to loop over one of these two arrays and call some subroutine (e.g. for updating the position and velocity). Something like this (in case of looping over the first array)
do iParticle = 1, nParticlesA
call updateVelocity( particleGroup%particleA_array(iParticle) )
end do
and this for looping over the second array
do iParticle = 1, nParticlesB
call updateVelocity( particleGroup%particleB_array(iParticle) )
end do
with the generic procedure updateVelocity implemented using an interface block.
Is there a way in Fortran to store which array to loop over during initialization (as a pointer?), so that I do not have to check particle_kind at every loop iteration? Or is there some better way of handling this problem?