Fortran将输入读入动态数组 [英] Fortran read input into dynamic array

查看:338
本文介绍了Fortran将输入读入动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从输入文件中读取的坐标.输入文件示例看起来像:

I would like to read coordinates from an from an input file. An example input file would look something like :

1    0.1542    0.2541    1.2451   N
12   4.5123    2.0014    2.0154   O
43   8.2145    0.2978    4.2165   H

等...此文件的大小是可变的.第一列是分配给原子的数字,第二列是其x,y,z坐标,最后一列是原子的元素符号.

etc... The size of this file is variable. The first column is a number assigned to an atom, the following columns are its x,y,z coordinates, and the final column is the elemental symbol of the atom.

我尝试了以下方法:

integer, allocatable :: atnum(:)
double precision, allocatable :: coord(:,:)
character(len=2), allocatable :: element(:)

open(unit=20, file='input', status='old',action='read')

read(20,*,end=200) atnum, coord(:,1:3), element
200 close(20)

这引发了我错误:

Fortran runtime error: Bad integer for item 2 in list input

我假设程序将第一项读入atnum(1),但随后尝试继续将第一行的第二项读入atnum(2).如何获取它才能正确读取输入?

I assume that the program read the first entry into atnum(1), but then tried to continue reading into the second entry of the first row into atnum(2). How can I get it to read the input correctly?

我还认为告诉程序将中间三列读入coord(:,1:3)可能存在问题.它可能会将前三个条目读入coord(1,1), coord(2,1), coord(3,1),然后碰到该行末尾的字符并再次感到困惑.我怎样才能告诉它修复该行的第一个下标,并读入另一个维度?还是我必须交换索引,例如coord(1:3,:)?那行得通吗?

I also think that there might be a problem with telling the program to read the middle three columns into coord(:,1:3). It is likely that it will read the first three entries into coord(1,1), coord(2,1), coord(3,1), then run into the character at the end of the line and become confused again. How can I tell it to fix the first subscript for the line, and read into the other dimension? Or will I have to swap the indices, like coord(1:3,:)? Will that work?

以上已由tpg2114回答,但我仍然有问题.在知道要读取多少组坐标之前,我无法分配数组,但是直到到达文件末尾,我才知道有多少个原子.如果我不分配atnum, coord and element,该程序可以正常编译,但是当我尝试运行它时会返回一个分段错误.如何在不事先分配动态数组的情况下将其读取到动态数组中?

The above has been answered by tpg2114, but I still have a problem. I can't allocate the the array until I know how many sets of coordinates are to be read, but I only know how many atoms there are until I reach the end of the file. The program compiles fine if I don't allocate atnum, coord and element, but returns a segmentation fault when I try to run it. How can I get it to read into the dynamic arrays without previously allocating them?

听起来与此问题类似:在Fortran中不分配变量的可变大小数组( )

It sounds similar to this question: Variable size arrays in Fortran without Allocate()

谢谢.

推荐答案

您需要遍历READ语句.单个READ只会到达记录的末尾,在本例中为该行.您会知道什么时候用完了,因为您输入了end = 200参数,所以它将跳到第200行(这里似乎没有).

You need to loop over the READ statement. A single READ will only go to the end of the record, in this case the line. You will know when you run out of lines because you put the end=200 argument, so it will jump to line 200 (which you don't seem to have here).

因此,您需要确保:

A)您的数组足够长,可以容纳文件(您没有显示它们的分配,我想您已经分配了它们?)

A) Your arrays are long enough to contain the file (you don't show the allocation for them, I assume you allocated them?)

B)您遍历READ语句

B) You loop over the READ statement

C)您为atnum参数以及coord的第一个参数和elem参数指定一个索引.

C) You specify an index for the atnum argument and the first argument of coord and the elem argument.

例如,假设声明相同,并且数组分配的空间足够大,而我是一个INTEGER

For example, assuming the declarations are the same and the arrays are allocated large enough, and I is an INTEGER

I = 1
DO 
  read(20,*,end=200) atnum(I), coord(I,1:3), elem(I)
  I = I + 1
END DO 
200 continue

此外,考虑删除end = 200部分并改用IOSTAT,因为这很现代并且行号已弃用.

Also, consider dropping the end=200 part and using IOSTAT instead since that is modern and line numbers are deprecated.

这篇关于Fortran将输入读入动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆