Fortran Fortran90: Reading Mixed-Type Input File

  • Thread starter Thread starter typeon
  • Start date Start date
  • Tags Tags
    File Fortran90
AI Thread Summary
To read a mixed-type input file in Fortran90, a derived type structure can be used to hold a character and three real numbers. The structure should be defined to accommodate the data types correctly, with real numbers instead of integers for the coordinates. Reading the entire file into memory can be achieved by defining an array of the derived type, rather than individual variables for each line. When reading from the file, each field must be accessed individually using the derived type's fields. Proper format statements are necessary to ensure the output displays the desired number of decimal places.
typeon
Messages
3
Reaction score
0
Hi everyone,

I am kind of new in fortran90 and I am struggling with writing this basic programme:
I have an input file with a fixed format . It looks like this:

c 23.345 45.342 34.345
c 12.345 56.235 67.234
c 45.567 44.345 78.345
c 56.567 78.563 97.342

where the first column is always a character and columns 2 to 4 are real numbers.
Since, it is a mixture of types I am wondering how to programme this so that it reads the information from an input file : cartesian.xyz
Should I use "derived data types" to define a matrix that is composed of one character and 3 real numbers? or is there another way?.

Thanks!
 
Technology news on Phys.org
typeon said:
Hi everyone,

I am kind of new in fortran90 and I am struggling with writing this basic programme:
I have an input file with a fixed format . It looks like this:

c 23.345 45.342 34.345
c 12.345 56.235 67.234
c 45.567 44.345 78.345
c 56.567 78.563 97.342

where the first column is always a character and columns 2 to 4 are real numbers.
Since, it is a mixture of types I am wondering how to programme this so that it reads the information from an input file : cartesian.xyz
Should I use "derived data types" to define a matrix that is composed of one character and 3 real numbers? or is there another way?.
You can't have a matrix that is composed of different types (i.e., characters and reals). You can, however, define a structure that can hold a character and three real numbers, and then define an array whose members are structures.

You didn't mention what your program is going to do with the values in the input file, so you may or may not need to read all of the file contents into memory (into an array as described above). It might be sufficient to read in one row of values, do you computations, and move on to the next row to do some more work.
 
Hi,

The input file is a molecule, a finite flat sheet of graphene. So once the whole input is read a table with each carbon atom and its neighbours is generated. It will also calculate whether the carbon atom is in the middle or at the edge of the flake. In short, I really think it has to read the whole input and keep it in the memory.
Can I generate a structure that can hold a character and three real numbers using the following module?

module test
type mytype
sequence
character(len=6):: carbon
integer :: x
integer :: y
integer :: z
end type mytype
end module test
 
typeon said:
Hi,

The input file is a molecule, a finite flat sheet of graphene. So once the whole input is read a table with each carbon atom and its neighbours is generated. It will also calculate whether the carbon atom is in the middle or at the edge of the flake. In short, I really think it has to read the whole input and keep it in the memory.
Can I generate a structure that can hold a character and three real numbers using the following module?

module test
type mytype
sequence
character(len=6):: carbon
integer :: x
integer :: y
integer :: z
end type mytype
end module test

More like this. In your first post you showed the numeric data as being real, not integer, so I changed the fields to take this into account.

BTW, the carbon field as you have it doesn't hold a single character - it can hold six characters.
Code:
module test
type molecule_position
   sequence
   character(len=6):: carbon
   real*8  :: x
   real*8  :: y
   real*8  :: z
end type molecule_position
end module test[/QUOTE]

You can define an array whose elements are of this type.
 
I have made the programme and I have managed to read the xyz input ( file : cartesian) and print it to the
unit 55.: fort.55

module test
type molecule_position
sequence
character(len=1):: carbon
real*8 :: x
real*8 :: y
real*8 :: z
end type molecule_position
end module test

program xyz
use test
implicit none
! here I define the 4 lines of my xyz file
type(molecule_position) :: line1
type(molecule_position) :: line2
type(molecule_position) :: line3
type(molecule_position) :: line4

! I open the file with the coordinates and read every line
open(12,file='cartesian', status='old')
read(12,*) line1, line2, line3, line4
! This format I made it but it s not working
!1 format(A1,1x,f6.3,1x,f6.3,1x,f6.3)

write(55,*) line1, line2, line3, line4
end program xyz

1) Do I have to define each line every time I want to add a new line or is there a way
to tell the code that the number of lines is, say, 50, and I want it to create 50 variables like
type(molecule_position) : from line1 to line50

2) I tried to print it out with only 3 decimals but my format is faulty

c 23.344999999999999 45.341999999999999 34.344999999999999
c 12.345000000000001 56.234999999999999 67.233999999999995
c 45.567000000000000 44.344999999999999 78.344999999999999
c 56.567000000000000 78.563000000000002 97.341999999999999
 
You can't READ a whole derived type at once - you need to READ a field at a time. To access the fields of a derived type, use %. This code should read one line of data in your input file.

Code:
read (12, 1) line1%carbon, line1%x, line1%y, line1%z
1 format(A1,1x,f6.3,1x,f6.3,1x,f6.3)

Do the same sort of thing to write out the data. With these changes, your format statements should work.

It's been a long while since I did any fortran programming, and I don't recall doing anything with derived types. The information I presented here comes from a fortran 90 tutorial I found at http://web.mse.uiuc.edu/courses/mse485/comp_info/derived.html .
 
Last edited by a moderator:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
11
Views
3K
Replies
7
Views
3K
Replies
5
Views
2K
Replies
11
Views
8K
Replies
2
Views
2K
Replies
6
Views
2K
Replies
1
Views
2K
Back
Top