Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading large text file into Fortran code 3

Status
Not open for further replies.

Kurylyk

Technical User
Jul 13, 2011
2
CA
Hi all,

I am a complete novice on Fortran. I am running a model that was written in Fortran to solve a partial differential equation (finite difference solution)over multiple time steps. I want to read time dependent boundary conditions into the Fortran program. I have created a large text file ("bc.text")that has three columns and up to about 14,000 rows. The first column contains the time. The second column contains one boundary condition, and the third column contains my second boundary condition. I want to write a subroutine within my program that links to this text file and reads the two boundary conditions, but only for the given time step. For example, if I am on time step 99, I want the file to read in the second and third values on the 99th row. Any suggestions would be very much appreciated!


E.g. File

1 5.6 7.3
2 4.5 6.5
3 6.7 7.6
4 4.5 5.6
.
.
.

 
Looks like homework, open the file and leave it open, in the subroutine you have to make a while loop and do an empty "READ(unit,*)" until you're just one time step before the one you want.
Next, in the same subroutine you read: "READ(unit,*)itimestep,bc:))" in which bc:)) is a REAL array of size 2 that you let leave the SUBROUTINE by declaring it with an INTENT(OUT) statement.
Before leaving the subroutine you rewind your file "REWIND(unit)" as you're going to call it more than once.
 
Thanks GerritGroot

I have been doing some more thinking. As I mentioned, the file may contain about 14,000 rows. Each of my iterations is fairly time consuming, and I would like to save time reading in my boundary conditions. (if that is possible). Is there any way, I can read the entire text in as an array so that I do not have to keep looping through it?
 
14000 rows = 14000 * 2 * 8 bytes if you are storing the numbers as DP. That is roughly 1Mb. You may be able to read all that into an array.

Another alternative is to read formatted text file and write it as an unformatted binary file. Then close the unformatted file and reopen it as a direct access file. If your time step is regular, you could use that as the index into the binary file.
 
just read it into an array where the time step is the index of it...what do the boundary conditions looks like? like those that you show...then, I don't think they need to be stored in double precision; besides, 1MB is nothing, you computer has lots RAM by now...like 2GB? Granted, some is already in use by the OS, but nevertheless
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top