我正在尝试解析Abaqus输入文件,并将所需的节点读取到数组中。我首先尝试比较一个字符串,如果匹配,那么我想从下一行读取未知数量的整数值。
文本文件的示例如下所示。我想读取nset = bf1下的值:
** PARTS
*Part, name=Beam
*Nset, nset=bf1, instance=Beam-1
1, 2, 5, 43, 45, 48, 49, 50, 105, 106, 1189, 1190, 1191, 1192, 1193, 1194
5275, 5276, 5277, 5278
*Elset, elset=_Surf-3_SNEG, internal, instance=Beam-1
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
我现在正在做的是这个
program nset_parser
implicit none
integer, parameter :: rk = selected_real_kind(10,40)
real(kind=rk), dimension(:), allocatable :: values
integer :: ios,l,val, n, nlines
character(len=80) :: file_name = 'code_check.inp'
character (len=*), parameter :: search_str = "bf1"
character(len=5096) :: input_line, line
character(len=10) :: keyword, get_keyword
logical :: h_nset=.false.
l = len(search_str)
open(unit=10, file=file_name, status="old", action='read') ! open file
do
read(10, '(a)',iostat=ios) input_line ! read the lines
if (ios /=0) exit
input_line = adjustl(input_line)
if (input_line(1:1) == '*' .and. input_line(2:2) /= '*') then ! check if it is a comment line or data line
keyword=get_keyword(input_line)
if (trim(keyword) == 'Nset') then
read(input_line,'(a)') line
val = index(line, search_str)
if (val /= 0) then
h_nset=.true.
print*, line(val:val+l-1)
end if
end if
end if
end do
close(10)
end program nset_parser
但是在此之后,我有些沮丧。
我将我的答案改写得更加笼统。此代码将处理“ nset = bf1”行与以“ *”开头的任何其他行之间的多行整数。
由于整数行的长度可以变化,因此2D整数数组是不合适的。相反,我将所有int存储在一个1D数组中,并存储每个新行的数组索引。
为避免两次读取所有内容,如有必要,我会即时增加数组的大小。
请注意,如果你的编译器不支持MOVE_ALLOC(Fortran 2003),则只需将每个调用替换为“ deallocate,allocate,deallocate”集。
program nset_parser
implicit none
INTEGER :: ios,l,val, i, n, nlines, comma_idx, next_idx
INTEGER, ALLOCATABLE :: my_ints(:), line_starts(:), work_ints(:)
character(len=80) :: file_name = 'code_check.inp'
character (len=*), parameter :: search_str = "bf1"
character(len=5096) :: input_line
character(len=10) :: keyword
logical :: h_nset=.false.
l = len(search_str)
ALLOCATE(line_starts(10), my_ints(1000))
next_idx = 1
nlines = 0
open(unit=10, file=file_name, status="old", action='read') ! open file
DO
! Read and adjust the line
read(10, '(a)',iostat=ios) input_line
if (ios /=0) exit
input_line = adjustl(input_line)
! Ignore blank lines
IF(LEN(TRIM(input_line))==0) CYCLE
! This section picks up any line beginning *
! and either sets or unsets h_nset
IF (input_line(1:1) == '*' .AND. input_line(2:2) /= '*') THEN ! check if it is a comment line or data line
! Use of keyword below is a little unsafe if
! keywords can be longer than 10 chars (the length of 'keyword')
! Instead, better to check if input_line(2:5) == Nset
! I left it like this in case you want to be able to check multiple
! keywords or something.
comma_idx = INDEX(input_line, ',')
keyword = input_line(2:comma_idx-1)
IF (TRIM(keyword) == 'Nset') THEN
val = INDEX(input_line, search_str)
IF (val /= 0) THEN
h_nset=.TRUE. ! Switch on when nset=bf1 line is found
CYCLE
END IF
END IF
h_nset = .FALSE. ! Only reach this point if we are in a * line which isn't nset=bf1
END IF
IF(h_nset) THEN
n = COUNT(TRANSFER(input_line, 'a', LEN(input_line)) == ",") + 1 !cast to chars and count occurrence of comma
nlines = nlines + 1
! Double size of arrays on the fly if necessary
IF(nlines > SIZE(line_starts)) THEN
ALLOCATE(work_ints(nlines * 2))
work_ints(1:SIZE(line_starts)) = line_starts
CALL MOVE_ALLOC(work_ints, line_starts)
END IF
IF(next_idx+n > SIZE(my_ints)) THEN
ALLOCATE(work_ints(2*SIZE(my_ints)))
work_ints(1:SIZE(my_ints)) = my_ints
CALL MOVE_ALLOC(work_ints, my_ints)
END IF
line_starts(nlines) = next_idx
READ(input_line, *) my_ints(next_idx:next_idx+n-1)
next_idx = next_idx + n
END IF
END DO
close(10)
! This helps with the printing below
line_starts(nlines+1) = line_starts(nlines) + n
DO i=1, nlines
PRINT *, my_ints(line_starts(i):line_starts(i+1)-1)
END DO
end program nset_parser
我认为op非常关心输入文件的实际结构。因此,您的代码不包含以下两个概念:(1)查找该
nset=bf1
部分。(2)多行有整数,您的代码只能读一行。好点。我真的只是想回答“读取一行整数”的问题,因为我认为他们已经有了识别“ bf1”部分的代码。
我认为扫描多行整数并非易事。也许您可以扩展答案以一次读取多行,例如,扫描直到下一行以开头
*Elset
。这是正确的。我的代码扫描文件中的字符串,但是扫描下面的整数行是问题。另外,我在示例中仅显示了两行,但是行数可以为'n'。我一直在尝试这样做,但是到目前为止,对我来说似乎有点困难。
谢谢你。它可以正常工作,我从代码中了解了如何解决此类问题。另外,如果我最终发表科学文章,我想感谢您对我的工作作出的贡献?你能给我你的名字吗?