Reputation: 77
Can anyone one modify my code or show me how to make an "1D array that can be accessible"? For now, I couldn't access each element in 1D array.
Code:
MODULE PARAMETERS
INTEGER :: FILE
INTEGER :: X=3,Y=3,Z=9
INTEGER :: I, J, K, L = 1
INTEGER :: B(9)
INTEGER :: A(3,3)
DATA A/ 1,2,3,4,5,6,7,8,9/
END MODULE PARAMETERS
PROGRAM CLUSTER
USE PARAMETERS
IMPLICIT NONE
OPEN(UNIT=11,FILE="2D.TXT",ACTION="WRITE")
DO J = 1,Y
DO I = 1,X
B(L) = A(I,J)
WRITE(11,*) , B(L)
END DO
L = L + 1
END DO
CLOSE(11)
END PROGRAM CLUSTER
Upvotes: 0
Views: 1301
Reputation: 6915
If you are just trying to put your 2d array into a 1d array, use the reshape
intrinsic. Code equivalent to your self-answer is:
PROGRAM CLUSTER
USE PARAMETERS
IMPLICIT NONE
OPEN(UNIT=11,FILE="2D.TXT",ACTION="WRITE")
B = reshape(A, [ 9 ])
write (11,'(i8)'), (b(i),i=1,9)
CLOSE(11)
END PROGRAM CLUSTER
As noted in comments to both your answer and your question, this reshaping is probably unnecessary for your intended use with MPI, where you can directly use A.
Upvotes: 1
Reputation: 77
I fixed it and this is how the main program should be...
PROGRAM CLUSTER
USE PARAMETERS
IMPLICIT NONE
OPEN(UNIT=11,FILE="2D.TXT",ACTION="WRITE")
L = 1
IF ( L .LT. 10) THEN
DO J = 1,Y
DO I = 1,X
B(L) = A(I,J)
WRITE(11,*) , B(L)
L = L + 1
END DO
END DO
ELSE
WRITE(11,*), "NOTHING TO DO HERE"
END IF
CLOSE(11)
END PROGRAM CLUSTER
Upvotes: 1