24小时热门版块排行榜    

查看: 3890  |  回复: 59
本帖产生 3 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

Gina88

木虫 (正式写手)

[求助] 写了一个fortran90的小程序,编译通不过,请大侠帮忙

program cell-POSCAR

!announcement begin***********************************
implicit none
real(8)::cellcon(3,3)       !lattice constance
real(8),allocatable::atomcoor(:,  !direct coordinate of atoms
integer,allocatable::type(
integer i,j,k,m,n           !m:atom number,n:type number
character(len=1) nonsense    !to contain something that will not be used
character(len=2) position   !to read %
character(len=2) element1,element2  
!End announcement************************************

!Begin reading data from "cell"****************************
open(unit=18,file="cell",status="old"
read(18,*)nonsense
do i=1,3  
  read(18,*)cellcon(i,1),cellcon(i,2),cellcon(i,3)
end do
read(18,*)position
read(18,*)nonsense
read(18,*)nonsense
n=0;m=0
read(18,*)element2
do while(position/=element2)
  n=n+1
  element1=element2
  do while(element1==element2)
    m=m+1
    read(18,*)element2
  end do
end do
close(18)

allocate(type(n))
allocate(atomcoor(m,3))

open(unit=28,file="cell",status="old"
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)element2,atomcoor(1,1),atomcoor(1,2),atomcoor(1,3)
i=1;k=1
element1=element2
if (m==1) then
  type(1)=1
else if
  do j=2,m
    read(28,*)element2,atomcoor(j,1),atomcoor(j,2),atomcoor(j,3)
    if(element1==element2) then
      k=k+1
      if(m==j) type(i)=k
    else if
      type(i)=k
      i=i+1
      element1=element2
      k=1
      if(m==j) type(i)=k
    end if   
  end do
end if
close(28)
!End reading data from "cell"****************************

!Begin writing to "POSCAR"******************************
open(unit=38,file="POSCAR",status="replace"
write(38,10)System
write(38,100)cellcon(1,1)
do i=1,3
  write(38,200)(cellcon(i,j)/cellcon(1,1),j=1,3)
end do
write(38,60)(type(i),i=1,n)
write(38,20)Selective,dynamic
write(38,30)Direct
do i=1,m
  write(38,300)(atomcoor(i,j),j=1,3)
end do
10 FORMAT(1X,A6)
20 FORMAT(1X,A9,1X,A7)
30 FORMAT(1X,A6)
60 FORMAT(1X,n(I3,2X))
100 FORMAT(1X,F18.16)
200 FORMAT(1X,3(F18.16,3X))
300 FORMAT(1X,F18.16,2X,F18.16,2X,F18.16,2X,'T',2X,'T',2X,'T')
close(38)
!End writing to "POSCAR"********************************

end
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

★ ★
Gina88(金币+3): 2011-05-04 22:29:31
Gina88(金币+2): 2011-05-04 22:29:54
余泽成(金币+2): 谢谢参与应助! 2011-05-19 19:19:23
CODE:
program cell_POSCAR

!announcement begin***********************************
implicit none
real(8)::cellcon(3,3)       !lattice constance
real(8),allocatable::atomcoor(:,:)  !direct coordinate of atoms
integer,allocatable::type(:)
integer i,j,k,m,n           !m:atom number,n:type number
character(len=1) nonsense    !to contain something that will not be used
character(len=2) position   !to read %
character(len=2) element1,element2  
character(len=20) :: fm
!End announcement************************************

!Begin reading data from "cell"****************************
open(unit=18,file="cell",status="old")
read(18,*)nonsense
do i=1,3  
  read(18,*)cellcon(i,1),cellcon(i,2),cellcon(i,3)
end do
read(18,*)position
read(18,*)nonsense
read(18,*)nonsense
n=0;m=0
read(18,*)element2
do while(position/=element2)
  n=n+1
  element1=element2
  do while(element1==element2)
    m=m+1
    read(18,*)element2
  end do
end do
close(18)

allocate(type(n))
allocate(atomcoor(m,3))

open(unit=28,file="cell",status="old")
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)element2,atomcoor(1,1),atomcoor(1,2),atomcoor(1,3)
i=1;k=1
element1=element2
if (m==1) then
  type(1)=1
else
  do j=2,m
    read(28,*)element2,atomcoor(j,1),atomcoor(j,2),atomcoor(j,3)
    if(element1==element2) then
      k=k+1
      if(m==j) type(i)=k
    else
      type(i)=k
      i=i+1
      element1=element2
      k=1
      if(m==j) type(i)=k
    end if   
  end do
end if
close(28)
!End reading data from "cell"****************************

!Begin writing to "POSCAR"******************************
open(unit=38,file="POSCAR",status="replace")
write(38,10)'System'
write(38,100)cellcon(1,1)
do i=1,3
  write(38,200)(cellcon(i,j)/cellcon(1,1),j=1,3)
end do
write(fm,'(a,i0,a)') '(1x,',n,'(i3,2x))'
write(38,fm)(type(i),i=1,n)
write(38,20)'Selective','dynamic'
write(38,30)'Direct'
do i=1,m
  write(38,300)(atomcoor(i,j),j=1,3)
end do
10 FORMAT(1X,A6)
20 FORMAT(1X,A9,1X,A7)
30 FORMAT(1X,A6)
!60 FORMAT(1X,n(I3,2X))
100 FORMAT(1X,F18.15)
200 FORMAT(1X,3(F18.16,3X))
300 FORMAT(1X,F18.16,2X,F18.16,2X,F18.16,2X,'T',2X,'T',2X,'T')
close(38)
!End writing to "POSCAR"********************************

end

6楼2011-05-04 19:06:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

★ ★
余泽成(金币+2): 谢谢参与应助! 2011-05-19 19:19:36
谨慎地怀疑你以前没有写过 Fortran 的程序,或者也没有认真地看过书,否则不应该出现字符串两端不加引号,if 后面不跟判断语句的情况,呵呵……

你贴出来的程序根本就不能进行编译(即出现语法错误),那么要别人怎么帮你……
7楼2011-05-04 19:08:49
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

引用回帖:
Originally posted by Gina88 at 2011-05-04 22:28:31:
您说的没错,我的确没有写过fortran程序,也没有仔细看过书。
几天前都不知道fortran的那些东西。
但是因为知道一点C语言,前面为了研究,得到了别人的程序代码。
看了一下,不懂的查百度,然后觉得程序不满 ...

你用我贴出来的,和你原来的程序之间作一个 diff 就知道了啊,呵呵……要不我给你贴一个 diff?
9楼2011-05-04 22:38:39
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

★ ★ ★ ★ ★
余泽成(金币+5, 程序强帖+1): 辛苦了! 2011-05-04 22:55:55
zvv.f90 是你的程序
zv.f90 是我改后的程序

- 和 + 表示修改的部分,- 号代表你的程序,+ 代表修改后的程序……
CODE:
--- zvv.f90        2011-05-04 22:39:38.874875000 +0800
+++ zv.f90        2011-05-04 22:42:02.437375000 +0800
@@ -1,4 +1,4 @@
-program cell-POSCAR
+program cell_POSCAR

!announcement begin***********************************
implicit none
@@ -9,6 +9,7 @@
character(len=1) nonsense    !to contain something that will not be used
character(len=2) position   !to read %
character(len=2) element1,element2  
+character(len=20) :: fm
!End announcement************************************

!Begin reading data from "cell"****************************
@@ -48,13 +49,13 @@
element1=element2
if (m==1) then
   type(1)=1
-else if
+else
   do j=2,m
     read(28,*)element2,atomcoor(j,1),atomcoor(j,2),atomcoor(j,3)
     if(element1==element2) then
       k=k+1
       if(m==j) type(i)=k
-    else if
+    else
       type(i)=k
       i=i+1
       element1=element2
@@ -68,22 +69,23 @@

!Begin writing to "POSCAR"******************************
open(unit=38,file="POSCAR",status="replace")
-write(38,10)System
+write(38,10)'System'
write(38,100)cellcon(1,1)
do i=1,3
   write(38,200)(cellcon(i,j)/cellcon(1,1),j=1,3)
end do
-write(38,60)(type(i),i=1,n)
-write(38,20)Selective,dynamic
-write(38,30)Direct
+write(fm,'(a,i0,a)') '(1x,',n,'(i3,2x))'
+write(38,fm)(type(i),i=1,n)
+write(38,20)'Selective','dynamic'
+write(38,30)'Direct'
do i=1,m
   write(38,300)(atomcoor(i,j),j=1,3)
end do
10 FORMAT(1X,A6)
20 FORMAT(1X,A9,1X,A7)
30 FORMAT(1X,A6)
-60 FORMAT(1X,n(I3,2X))
-100 FORMAT(1X,F18.16)
+!60 FORMAT(1X,n(I3,2X))
+100 FORMAT(1X,F18.15)
200 FORMAT(1X,3(F18.16,3X))
300 FORMAT(1X,F18.16,2X,F18.16,2X,F18.16,2X,'T',2X,'T',2X,'T')
close(38)

10楼2011-05-04 22:43:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)


余泽成(金币+1): 谢谢参与应助! 2011-05-19 19:20:10
引用回帖:
Originally posted by Gina88 at 2011-05-04 23:23:38:
我都不知道还有这个diff的功能。
刚才我是一行一行用眼睛看了修改的。
能不能告诉我这个diff的步骤呢?

diff 是一个单独的工具,用来比较两个文本文件的差异的。
13楼2011-05-05 09:00:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

★ ★
余泽成(金币+2): 辛苦了! 2011-05-05 18:27:22
引用回帖:
Originally posted by Gina88 at 2011-05-04 23:19:07:
刚才先看的你后面一个帖子,回帖时没有看见你前面一个帖子。
非常感谢您!
现在已经可以用了。

但是还想问您一个问题:
那个cell文件,一般软件出来的时候就是名为***.cell的文件。
想问一下,我能不能 ...

这个用 Fortran 来实现好象不那么简单。但可以用 Linux 下的脚本或 Windows 下批处理来做,即把 cell 的文件名传给你的程序,然后你的程序再去读相应的文件。你的程序中作一个简单的修改即可……回头我再试一下,呵呵……
14楼2011-05-05 09:02:47
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

Gina88(金币+5): 2011-05-05 09:30:56
引用回帖:
Originally posted by Gina88 at 2011-05-04 23:19:07:
刚才先看的你后面一个帖子,回帖时没有看见你前面一个帖子。
非常感谢您!
现在已经可以用了。

但是还想问您一个问题:
那个cell文件,一般软件出来的时候就是名为***.cell的文件。
想问一下,我能不能 ...

在我前面那个文件上的修改部分如下。

你执行该程序的时候,只要 a.exe file1.cell 就可以了……
CODE:
--- zv.f90        2011-05-05 09:14:49.062500000 +0800
+++ zv_new.f90        2011-05-05 09:15:01.078125000 +0800
@@ -10,10 +10,13 @@
character(len=2) position   !to read %
character(len=2) element1,element2  
character(len=20) :: fm
+character(len=20) :: filename
!End announcement************************************

!Begin reading data from "cell"****************************
-open(unit=18,file="cell",status="old")
+!call getarg(1,filename)!! complier extension
+call GET_COMMAND_ARGUMENT(1,filename)!! Fortran 2003 standard
+open(unit=18,file=filename,status="old")
read(18,*)nonsense
do i=1,3  
   read(18,*)cellcon(i,1),cellcon(i,2),cellcon(i,3)
@@ -36,7 +39,7 @@
allocate(type(n))
allocate(atomcoor(m,3))

-open(unit=28,file="cell",status="old")
+open(unit=28,file=filename,status="old")
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
@@ -69,6 +72,8 @@

!Begin writing to "POSCAR"******************************
open(unit=38,file="POSCAR",status="replace")
+
+write(38,*) filename(1:len(trim(filename))-5)
write(38,10)'System'
write(38,100)cellcon(1,1)
do i=1,3

15楼2011-05-05 09:18:59
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

Gina88(金币+5): 2011-05-05 09:42:06
余泽成(程序强帖+1): 2011-05-19 19:20:22
再贴个完整的程序吧,呵呵……
CODE:
program cell_POSCAR

!announcement begin***********************************
implicit none
real(8)::cellcon(3,3)       !lattice constance
real(8),allocatable::atomcoor(:,:)  !direct coordinate of atoms
integer,allocatable::type(:)
integer i,j,k,m,n           !m:atom number,n:type number
character(len=1) nonsense    !to contain something that will not be used
character(len=2) position   !to read %
character(len=2) element1,element2  
character(len=20) :: fm
character(len=20) :: filename
!End announcement************************************

!Begin reading data from "cell"****************************
!call getarg(1,filename)
call GET_COMMAND_ARGUMENT(1,filename)
open(unit=18,file=filename,status="old")
read(18,*)nonsense
do i=1,3  
  read(18,*)cellcon(i,1),cellcon(i,2),cellcon(i,3)
end do
read(18,*)position
read(18,*)nonsense
read(18,*)nonsense
n=0;m=0
read(18,*)element2
do while(position/=element2)
  n=n+1
  element1=element2
  do while(element1==element2)
    m=m+1
    read(18,*)element2
  end do
end do
close(18)

allocate(type(n))
allocate(atomcoor(m,3))

open(unit=28,file=filename,status="old")
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)nonsense
read(28,*)element2,atomcoor(1,1),atomcoor(1,2),atomcoor(1,3)
i=1;k=1
element1=element2
if (m==1) then
  type(1)=1
else
  do j=2,m
    read(28,*)element2,atomcoor(j,1),atomcoor(j,2),atomcoor(j,3)
    if(element1==element2) then
      k=k+1
      if(m==j) type(i)=k
    else
      type(i)=k
      i=i+1
      element1=element2
      k=1
      if(m==j) type(i)=k
    end if   
  end do
end if
close(28)
!End reading data from "cell"****************************

!Begin writing to "POSCAR"******************************
open(unit=38,file="POSCAR",status="replace")

write(38,*) filename(1:len(trim(filename))-5)
write(38,10)'System'
write(38,100)cellcon(1,1)
do i=1,3
  write(38,200)(cellcon(i,j)/cellcon(1,1),j=1,3)
end do
write(fm,'(a,i0,a)') '(1x,',n,'(i3,2x))'
write(38,fm)(type(i),i=1,n)
write(38,20)'Selective','dynamic'
write(38,30)'Direct'
do i=1,m
  write(38,300)(atomcoor(i,j),j=1,3)
end do
10 FORMAT(1X,A6)
20 FORMAT(1X,A9,1X,A7)
30 FORMAT(1X,A6)
!60 FORMAT(1X,n(I3,2X))
100 FORMAT(1X,F18.15)
200 FORMAT(1X,3(F18.16,3X))
300 FORMAT(1X,F18.16,2X,F18.16,2X,F18.16,2X,'T',2X,'T',2X,'T')
close(38)
!End writing to "POSCAR"********************************

end

16楼2011-05-05 09:19:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

Gina88(金币+2): 2011-05-05 11:28:04
余泽成(程序强帖+1): 2011-05-05 18:28:00
引用回帖:
Originally posted by Gina88 at 2011-05-05 10:30:36:
这个好像编译通不过啊,他说没有定义GET_COMMAND_ARGUMENT。
另外文件名没有从众多的文件中筛选后缀名为cell的文件啊。

谢谢大侠了,你真是帮了我很大很大的忙,呵呵:)

我在 diff 中说了,GET_COMMAND_ARGUMENT 是 Fortran 2003 标准中的,如果你的编译器还不支持的话,就用 getarg,就是前面的那句。

至于从文件名中筛选,我前面的回复中也说了,你最好借助于操作系统提供的功能,如果是 Linux,下面的脚本应该是可行的……
CODE:
for i in `ls *.cell`; do
    ./a.exe $i;
done

如果是 Windows 的操作系统,应该也是类似的,不过我没有编写 .bat 的经验……

需要说明的是,你的 POSCAR 文件内容会被替代的……
18楼2011-05-05 10:52:28
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

snoopyzhao

至尊木虫 (职业作家)

【答案】应助回帖

Gina88(金币+1): 2011-05-05 16:19:41
引用回帖:
Originally posted by Gina88 at 2011-05-05 14:48:37:
cellcon(1,1)

在cell文件中,这个值是:
12.782529349912258
但是在POSCAR中,这个值变成:
12.7825293499122600

有没有办法使得POSCAR中的这个值和cell中的这个值一模一样?

我给出的程序中结果是一致的啊,呵呵……

那个地方,原先是 F18.16,我后来改为 F18.15。
20楼2011-05-05 15:44:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 Gina88 的主题更新
信息提示
请填处理意见