I have 3 allocatable 1D arrays in a Fortran routine, VX(:), VY(:), VZ(:), all with the same size.
I need to aggregate them in a 2D array named VARXYZ and send it to a routine that modifies the 'matrix'. The code below works but forces to double the memory size.
SUBROUTINE TEST(VX,VY,VZ)
REAL(8), INTENT(INOUT), DIMENSION(:) :: VX,VY,VZ ! They've been allocated with size N in the main
REAL(8), ALLOCATABLE, DIMENSION(:,:) :: VARXYZ ! The 'matrix'
ALLOCATE(VARXYZ(3,N))
VARXYZ(1,:)=VX(:)
VARXYZ(2,:)=VY(:)
VARXYZ(3,:)=VZ(:)
CALL CHANGE_MATRIX(VARXYZ)
VX(:)=VARXYZ(1,:)
VY(:)=VARXYZ(2,:)
VZ(:)=VARXYZ(3,:)
...
To avoid the 'double allocation', my first bad reflex was to use an EQUIVALENCE between the 1D arrays and the 3 'columns' of the matrix, but it is not allowed apparently.
After some reading, I have seen people recommending using pointers and the TRANSFER intrinsic function, but I have no idea of how to use them here.
Could you please give an example of how to mimic this EQUIVALENCE mechanism I need?
change_matrix
works. Can you explain what that subroutine does, and whether you can change it? – Incisetransfer
is a copying mechanism so cannot be useful if the goal is to have no extra memory use. – InciseVARXYZ(1,:)
in place ofVX(:)
in the main code? It's hard to pretend you have a 2D array when you actually have three 1D arrays, but easy to pretend you have three 1D arrays when you actually have a 2D array. – Ballistaassociate(VX => VARXYZ(1,:))
. – Ballista