Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Reading 2D data to a 3D array

sb2601
Beginner
1,670 Views
I am writing a multiblock 2D cfd code and am having some trouble reading my 2D data from text files into a3D array.
Suppose I have three data files,
FileA having 11 rows and 11 columns ( i.e an11x11 array)
FileB having31 rows and 11 columns( i.e an11x31 array)
File C having 11 rows and 11 columns ( i.e an 11x11 array)
To avoid duplication of code I was going to read my three data files into a 3D array suchthat my solver canmake three calls to the same subroutine but where the last index is incremented by 1.
i.e something like
CALL READDATA (11,11,1)

CALL READDATA2 (11,31,2)

CALL READDATA (11,11,3)

Unfortunately when I then write out my individual 2D arrays from my 3D array I seem to be getting some overflow of data from file C into file B. Obviously my reading or writing of data to/from the 3D array is incorrect.

I'd be verygrateful for any advice as to how I may :

read my three 2D data files to a single 3D array

write the individual 2Ddata back from the 3D array to text file.

I've been at this a couple of days now and got nowhere, so any help would be greatly appreciated.

Thank you,

Simon.

0 Kudos
4 Replies
jim_dempsey
Beginner
1,670 Views
Sounds like you are mixing-up the indexes in your read routine. In your example you should only need one read routine.
subroutine READDATA(nCols, nRows, nFileNumber)
use ModuleWithArray
integer :: nCols, nRows, nFileNumber
integer :: nCol, nRow
! Test limits on input arguments
! then
call OpenFileByFileNumber(nFileNumber)
do nRow = 1,nRows
read(inUnit,*) array(1:nCols, nRow, nFileNumber)
end do
end subroutine
Jim Dempsey
0 Kudos
sb2601
Beginner
1,670 Views
Thank you Jim, much appreciated.
Whilst I am very grateful for your reply, I am not familiar with the library module file MODULEWITHARRAY so I don't really follow what you are telling me. Is there another way do this using simple READ and WRITE statements ? I don't mind my code being a little clumsy at first if I can just get it to work. Also my code may have to be compatible with F77 as it may be reviewed by others who aren't using the Intel Visual Fortran 8.1 so I will have to try andkeep it simple.
Thank you for replying,
Simon.
0 Kudos
onkelhotte
New Contributor II
1,670 Views
I think Jim means that ModuleWithArray is the module, where you have declared your 3D array so you can use it global. You want to use this Array not only in the readdata subroutine, dont you ;-)
btw, my boss always tells me that I should not use 3D arrays because it needs too much memory and it decreases speed (can anyone confirm this? The boss is not always right...) In your example you have two 11x11 and one 11x31 array. Of course it is easier to declare a 200x200x10 array to match all circumstances instead ofallocating the array on the fly, but it is nicer that way.
Markus
0 Kudos
sb2601
Beginner
1,670 Views

Markus,

Thank you for your reply. I will have a go at using MODULE, but for now I was hoping to simply useREAD and WRITE statementsto read my three 2D arrays into a single3D array. I'm a bit reluctant to use pointers to a large 2D array as for a multiblock grid having maybe 7 or more domains ofmaybe 300x300 cells eachthe size of the global 2D array will become very large.Also having to write code compatible withmy professor's F77 compilerprecludes the use of MODULE.

What I have to do for now is figure out the correct format of my READand WRITE statements.

Thank you,

Simon.

0 Kudos
Reply