978142355 发表于 2016-3-5 10:53:00

做动力学结束后产生的文件读取问题

做分子动力学结束后会产生一个文件,本人想把其中有用的信息提取出,但是代码编完后只读取了其中的一个模块,后面的部分并没有读入进去,求助大家,是什么地方出了问题?    program readdata
    implicit none
    character(len=15)::str
    real*8,allocatable::time(:),kinet(:),inter(:),temp(:),dipole(:), &
    totenergy(:),poten(:),k(:),pressure(:),rd(:),density(:)
    integer::ierr,count,i,j
    open(10,file='mdstat.txt',status='old')
    open(11,file='mdstat.tem',status='unknown')
    count=1
    do
      read(10,"(a15)",iostat=ierr) str
      if(ierr/=0) exit
      if (str=='Simulation Time') then
            count=count+1
      end if
    end do
    allocate(time(count))
    allocate(kinet(count))
    allocate(inter(count))
    allocate(temp(count))
    allocate(dipole(count))
    allocate(totenergy(count))
    allocate(poten(count))
    allocate(pressure(count))
    allocate(density(count))
    allocate(k(count))
    allocate(rd(count))
    rewind(10)

    do i=1,count
      do j=1,3
            read(10,*)
      end do
      read(10,200) time(i)
      read(10,200) totenergy(i)
      read(10,200) poten(i)
      read(10,200) kinet(i)
      read(10,200) inter(i)
      read(10,200) temp(i)
      read(10,200) pressure(i)
      read(10,200) density(i)
      read(10,200) dipole(i)
      !        if(dipole(i).gt.32.0)then
      !        dipole(i)=dipole(i)-4.0
      !        endif
      !        if(dipole(i).lt.16.0)then
      !        dipole(i)=dipole(i)+4.0
      !        endif
    end do
200 format(22x,f14.4)
    do i=1,count
      write(11,"(3x,9f14.4)") time(i),totenergy(i),poten(i),kinet(i),&
      inter(i), temp(i),pressure(i),density(i),dipole(i)
    end do
    end program readdata

fcode 发表于 2016-3-5 18:27:43

你这种数据结构,用派生类型会比较方便。

以下代码,你的问题我都在注释里说明了

Program Readdata
Implicit None
Character (Len=64) :: str ! //修改为64
Type :: OneRec
    Real (Kind=8) :: time, totenergy, poten, kinet, inter, temp
    Real (Kind=8) :: pressure, density, dipole
End Type OneRec
Integer :: ierr, count, i
Type (OneRec), Allocatable :: myrec(:)
Open (10, File='mdstat.txt', Status='old')
Open (11, File='mdstat.tem', Status='unknown')
count = 0 !// 初值为 0
Do
    Read (10, '(a16)', Iostat=ierr) str ! //修改为16
    If (ierr/=0) Exit
    If (str(1:16)==' Simulation Time') count = count + 1 ! //前面加个空格
End Do
Allocate (myrec(count))
Rewind (10)
i = 1
Do
    Read (10, '(a64)', Iostat=ierr) str
    If (ierr/=0) Exit
    If (str(1:16)==' Simulation Time') Then
      Read (str(22:), *) myrec(i)%time
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%totenergy
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%poten
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%kinet
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%inter
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%temp
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%pressure
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%density
      Read (10, '(a64)', Iostat=ierr) str
      Read (str(22:), *) myrec(i)%dipole
      i = i + 1
    End If
End Do
Do i = 1, count
    Write (11, '(3x,9f14.4)') myrec(i)
End Do
End Program Readdata

978142355 发表于 2016-3-5 20:23:14

谢谢老大,我这编程经验还是太少了,虽然现在已经基本掌握了规则,可是编的程序感觉还是比较繁琐,不过老大的好,我再慢慢积累吧以及多读一读优秀的代码。谢谢老大:-handshake

深流水静水流深 发表于 2016-3-30 12:56:47

涨姿势了:-)
{:5_121:}

978142355 发表于 2016-3-31 09:48:22

深流水静水流深 发表于 2016-3-30 12:56
涨姿势了

当然,老大的方法总是很好,想达到他的境界得常看书,常写码。

深流水静水流深 发表于 2016-4-1 11:55:57

呵呵,大哥所言极是!
{:5_121:}

978142355 发表于 2016-4-2 09:52:20

深流水静水流深 发表于 2016-4-1 11:55
呵呵,大哥所言极是!

O(∩_∩)O~天天升群旗,唱群歌~守护群

深流水静水流深 发表于 2016-4-2 22:10:33

天天升群旗,唱群歌~守护群
{:5_121:}

978142355 发表于 2016-4-3 08:37:40

深流水静水流深 发表于 2016-4-2 22:10
天天升群旗,唱群歌~守护群

赞,大赞,超赞:-lol

深流水静水流深 发表于 2016-4-4 16:26:42

呵呵
{:5_121:}
页: [1] 2
查看完整版本: 做动力学结束后产生的文件读取问题