Color Scheme
Find People
Username
Password
Sign Up
old site http://222.195.74.11/
中文版 Location: Homepage >> Yanqing Su : CV - Blog

Ubuntu

Poster:

1. [转载]如何用Fortran批量生成文件
如何用Fortran批量生成文件?

From: http://blog.sina.com.cn/s/blog_52e8ac6201009ewn.html


设要生成4000个文件,文件名为AA1-AA4000,如何写循环生成文件,而不用写4000次write命令呢?
用内部文件:
character(len=80) :: filename,form
integer :: i
do i=1,4000
  select case (i)
  case (1:9)
     write(form,'(i1)') i
  case (10:99)
     write(form,'(i2)') i
  case (100:999)
     write(form,'(i3)') i
  case (1000:9999)
     write(form,'(i4)') i
  end select
  write(filename,*) "AA",trim(form),".TXT"
  open(10,file=filename)
  write(10,*) i
  close(10)
end do  
stop
end



如何用Fortran动态生成输出格式?
设有一个数组data(100),输出时,希望每行输出num个数,而num由用户输入,如何实现?
用内部文件:
character(len=80) :: form
real :: data(100)
integer :: i,num
data = (/ (i,i=1,100) /)/10.0
read(*,*) num
write(form,*) "(",num,"f10.3)"
write(*,form) data
stop
end




如何用F90/95生成随机数?
注意:
现在计算机产生的随机数都是伪随机数。
random_number(x) 产生一个0到1之间的随机数(x可以是向量),但是每次总是那几个数。
用了random_seed ()后,系统根据日期和时间随机地提供种子,使得随机数更随机了。
program random
  implicit none
  real :: x
  call random_seed ()     ! 系统根据日期和时间随机地提供种子
  call random_number (x)  ! 每次的随机数就都不一样了
  write(*,*) x
  stop
end program random




函数/子程序超载的例子
设要编一个两个变量值互换的子程序swap(a,b),哑元a,b可能是实型数,整型数,数组,矩阵,字符串,派生类型等等。但是希望只用一个子程序接口swap(a,b)来实现。F90可以用类属接口来实现这种子程序超载:
module Utilities
  implicit none
  private I_Swap,R_Swap,RVec_Swap,RMat_Swap,Type_Swap
  public  :: Swap
  interface Swap
     module procedure I_Swap,R_Swap,RVec_Swap,RMat_Swap,Type_Swap
  end interface
  contains
  subroutine i_swap (a,b)  ! 整型置换
     integer (ikind),intent(in out)  ::  a,b
     integer (ikind)                 ::  t
     。。。                ! 略
  end subroutine i_swap
  subroutine r_swap (a,b)  ! 实型置换
     real (rkind), intent(in out)    :: a,b
     real (rkind)                    :: t
     t = a
     a = b
     b = t
     return
  end subroutine r_swap
  subroutine RVec_swap (a,b)  ! 实型向量置换
     real (rkind), intent(in out)    :: a(:),b(:)
     integer (ikind)                 :: i
     do i=1, size(a)
        call R_Swap (a(i),b(i))
     end do
     return
  end subroutine RVec_swap
  subroutine RMat_swap (a,b)   ! 实型矩阵置换
     。。。                ! 略
  end subroutine RMat_swap
  subroutine Type_swap (a,b)   ! 派生类型置换
     。。。                ! 略
  end subroutine Type_swap
end module Utilities




推荐好的代码风格
根据F90子集语言ELF90和F的要求整理(部分)。
“强迫用”的语言特性:
+ F90的自由格式的源代码。
+ implicit none。
+ 子过程的哑元都要有intent属性。
+ 函数子程序的哑元必须指定为intent(in)。
+ 所有子程序和函数都放在模块(module)中,然后引用(use)该模块;或者放在program中。
+ 数组哑元要求是假定形状的,或者有固定的维数和大小。字符哑元要求是假定长度的。
+ 对于recursive function(递归函数)语句,必须有result子句。
+ 在所有派生类型(type)的定义语句中,必须用双冒号分隔符(::)。
+ 主程序要求有program语句。
+ 在程序单元的end语句中要求后跟程序单元的类型和名称。
+ 在end type语句中要求后跟类型的名称。
+ end program前必须有stop语句以表示停止执行。
+ 子过程中必须有return语句,以表示返回。
+ subroutine s( )并且call s( ),即必须有括号。
“不得用”的语言特性:
- allocatable、intent、pointer、save、dimension、parameter和target语句形式。(用属性形式代替。)
- external语句形式。(用显式的接口代替。)
- assign、赋值go to、交错return、continue、entry、和计算go to 语句。
- include文件。(用模块代替。)
- data和block data。(在类型声明语句中进行初始化或赋值。)
- common块。(将全局数据放在模块中,用模块代替。)
- equivalence。(被认为是许多难以查找的编程错误的来源。)
- double precision语句。(用real语句声明双精度的实型数。)
- 语句函数。(用内部函数代替。)
- 专用固有函数。(用类属函数代替。)
- 假定大小数组。(用假定形状数组代替。)
- do n (其中n为语句标号)。(用do和end do代替。)
- 非整数do变量和表达式。
- 同一行上多条语句。
- 逻辑型case表达式。
- 从if块外面分支到end if。
- where语句形式。(用where结构形式。)
- 在open和inquire语句中的blank= 说明符。
- 双字关键词之间要求有空格:in out,go to。不能写为inout,goto。
Yanqing Su
Posted at 2011-05-06 11:14 CST
Last modified at 2011-05-06 11:14 CST
Link to this poster
Top


Locations | Statistics | FAQ
Best view under the 1024x768 resolution and with the normal size of fonts.
Designed & developed by Yuming Wang, run for 6350 days since December 15, 2006, and last upgraded on May 28th, 2009.
Powered by Apache + PHP coded cOpfOs 3.11.1 last upgraded on February 22nd, 2010.
Copyright © 2006, all rights reserved.