Fortran90: Reading Mixed-Type Input File

  • Context: Fortran 
  • Thread starter Thread starter typeon
  • Start date Start date
  • Tags Tags
    File Fortran90
Click For Summary

Discussion Overview

The discussion revolves around reading a mixed-type input file in Fortran90, specifically a file containing character and real number data representing molecular coordinates. Participants explore methods for defining data structures to accommodate this mixed data type and address challenges related to reading and processing the input file.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant inquires about using derived data types to read a fixed-format input file containing a character and three real numbers.
  • Another participant suggests defining a structure to hold a character and three real numbers, and proposes defining an array of such structures.
  • A later reply emphasizes the need to read each field of a derived type individually, rather than attempting to read the entire structure at once.
  • Concerns are raised about the format used for reading and writing data, particularly regarding the precision of the output and the need for a proper format statement.
  • One participant shares a module definition for a structure that holds the required data types, noting the need to adjust the character length for proper representation.
  • Another participant questions how to handle multiple lines of input without defining each line individually, seeking a more efficient approach.

Areas of Agreement / Disagreement

Participants express varying opinions on the best approach to read and store mixed-type data, with no consensus reached on a single method. There are differing views on the necessity of reading the entire file into memory versus processing it line by line.

Contextual Notes

Participants note limitations regarding the handling of derived types and the precision of floating-point representations in Fortran, as well as the need for clarity in format statements for input and output operations.

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:

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K