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!

FORTRAN OPEN/READ Help

Status
Not open for further replies.

minger88

Technical User
Nov 16, 2006
7
US
First off, I would say my knowledge of FORTRAN is novice. I am working in CFD research. I have looked at some resources, and conceptually, I know how to go about opening and reading files, but I am completely stumped on this one. Here's what I need to do (it may be more familiar for people with Plot3D experience)

- I have a 2D grid which is unfortunately split into two blocks. Each block is just a collection of gridpoints. The file is like this:
- Line 1 - number of blocks
- Line 2 - xmax ymax zmax (where these are the maximum number of gridpoints in each direction for the first block.
- Line 3 - xmax ymax zmax (same thing, but I have two blocks)

Then it has four columns, and it lists all of the x values for the first block, going across (x value 1 is in column 1, row 1, the fourth value is in the fourth column, at which point it goes to column 1, row 2)

After it lists the x values, it lists the y values, and finally the z values.

Here is what I need to do. I need to read in all the values from both blocks, then put them into order (think of a square grid with a chunk taken out. The chunks gridpoints are listed after the rest, and I need to put the points in where they should be).

And then I need to re-export the points back in the same format.

Thanks in advance for any help.
 
Do it as you have said it
Code:
! declare the arrays
real, allocatable:: x(:), y(:), z(:)
! say the file has been opened on ifile
! read number of blocks
read(ifile, *) numblocks
! read gridpoints in 1st block
read(ifile, *) maxx1, maxy1, maxz1
! read gridpoints in 2nd block
read(ifile, *) maxx2, maxy2, maxz2
! declare array to hold points
allocate (x(maxx1 + maxx2))
allocate (y(maxy1 + maxy2))
allocate (z(maxz1 + maxz2))
! read first block
read (ifile, *) (x(i), i = 1, maxx1, 1)
read (ifile, *) (y(i), i = 1, maxy1, 1)
read (ifile, *) (z(i), i = 1, maxz1, 1)
! read second block
read (ifile, *) (x(i + maxx1), i = 1, maxx2, 1)
read (ifile, *) (y(i + maxy1), i = 1, maxy2, 1)
read (ifile, *) (z(i + maxz1), i = 1, maxz2, 1)
!
! Do whatever you need to
You didn't mention what the same format was - are you re-exporting in 2 blocks or one block? If in 2 blocks, is it with the same values in both blocks?


[/CODE]
 
Actually, I misunderstood the format. It actually has all of the points on one line.

So, the first line is the numblocks. The second line is the xmax, ymax, and zmax just like before. But, the third (or fourth in my case) line is one long line of all of the x points for the first block, the all of the y points for the first block, z, then the second block.

Anyways, I will be trying to export it as one block.

I think I have the information read in fine. I'm pretty sure I have 6 arrays, each with one blocks worth of one direction of points (x1, x2, etc).

The problem I'm having now is putting it all on one line. I'm assuming that I can dump all the points into another array, call it let's say x3, and y3, the write that array to a file, but I'm a very novice programmer and having a hard time. Right now I have something like this:

Code:
ALLOCATE(x3(0:xmax(1)+xmax(2),0:ymax(1))

x3(i) = (x1(i), i=0, imax(1))&(x2(i), i=0, imax(2))

The error I'm getting right now is
Code:
x3(i) = (x1(i), i=0, imax(1))&(x2(i), i=0, imax(2))
             1
Expecting right parenthesis at 1


thanks for the help
 
Put it in a couple of do loops. There may be more modern methods with the array processing stuff but I'm not familiar with those
Code:
do i = 0, imax(1)
   x3(i) = x1(i)
enddo

do i = 0, imax(2)
   x3(i + imax(1) + 1) = x2(i)
enddo
 
Just a thought: if you are starting from 0, then you need one more than imax(n) otherwise your array will go out of bounds. Your allocate should be
Code:
ALLOCATE(x3(0:xmax(1)+xmax(2) + 1,0:ymax(1))
Problem number two having had a look at this: x3 is a 2 dimensional array and your assignment is to a one dimensional x3. Are x1 and x2 one dimensional or two dimensional. If they are one dimensional, then it will be something like
Code:
do i = 0, imax(1)
   x3(i,0) = x1(i)
enddo

do i = 0, imax(2)
   x3(i + imax(1) + 1,0) = x2(i)
enddo
If you want to process it as an array assignment, have a look at the WHERE clause. I have seen it in use but I've never used it before and I don't know if it will apply in this case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top