分享

Fortran 语法备忘录 (高级)

 mtjs 2010-02-21

  • 混合语言编程  Mixed-Language-Programming
    用Fortran调用C++语言的程序
    例如一个简单的C++语言程序,求直角三角形斜边长度的达哥拉斯公式:保存为crout.cpp文件。
    !-----------------------------------------------------------------------
    // C++ - Routine
    #include <iostream.h>
    #include <math.h>
    extern "C" void pythagoras (float a, float b, float *c)
    {
     *c = (float) sqrt(a*a+b*b);
    }
    !-----------------------------------------------------------------------
    注:如果用C语言,则用stdio.h代替iostream.h,并且不需要extern "C",其余过程类似。


    Fortran程序:保存为fmain.f90
    !-----------------------------------------------------------------------
    ! Demoprogramm:
    ! Aufrufen einer C-Routine von Fortran aus
    ! Die C-Routine wird innerhalb eines Modules deklariert
       module cproc
          interface  ! definierte Schnittstelle 
             subroutine pythagoras (a, b, res)
       !DEC$ ATTRIBUTES C :: pythagoras
       !DEC$ ATTRIBUTES REFERENCE :: res
                 real :: a, b, res
             end subroutine
          end interface
     end module


    program fmain
        use cproc 
     implicit none
     real :: x, y, z
     

     write(*,*) ' Berechnung der Hypotenusenlaenge eines rechtwickligen Dreiecks'
     write(*,*) ' Geben Sie die beiden Seitenlaengen ein, die den rechten Winkel'
     write(*,*) ' einschliessen:'
     read(*,*) x, y
     
     call pythagoras(x,y,z)
     write(*,*) ' Die Laenge der Hypotenuse betraegt: ', z 
    end program fmain
    !-----------------------------------------------------------------------
    Fortran程序中添加的接口程序interface,对调用的C++与Fortran的数据传递进行说明。
    接口程序写在Module当中,Fortran程序引用了该Module,就可以使用了。
    参考链接:
    http://www.math./software/c-with-fortran.html
    http://www./support/mixing_f77_c_cpp/mixing_f77_c_cpp.html
    http://www./fortran_and_cplusplus.html
    http:///software/cppf77/cppf77.htm
    一个VC++调用Fortran动态链接库的例子:
    http://www./code/viewcode.asp?id=2052
    http://zhidao.baidu.com/question/652950.html?fr=qrl3


  • 系统时间日期的子程序
    date_and_time([date][,time][,zone][,values]) 
          date     至少8位字符,最左边的8位字符为JJJJMMTT (年月日) 
          time     至少10位字符,最左边的10位字符为hhmmss.sss (时分秒.毫秒)
          zone    至少5位字符,最左边的5位字符为±hhmm (时分) 相对于世界标准时间
          values  一维整型数组,长度至少为8,分别为:年,月,日,时区,小时,分,秒,毫秒
    例如:
    !-----------------------------------------------------------------------
    character(len=10)    :: d,t
    integer,dimension(8) :: V
    d = ""
    call date_and_time(date=d,time=t)
    call date_and_time(values=V)
    PRINT *, "Date=",d,"Time=",t
    PRINT *, V
    end
    !-----------------------------------------------------------------------

  • 系统时钟子程序
    system_clock ([count] [,count_rate] [,count_max]) 
          count           与计算机型号有关,基于计算机时钟,
          count_rate   一秒钟count的增加量
          count_max   count的最大值
    三个数字均为数字,可以用来计算一个程序运行的时间:秒数=(t1-t0)/Δt
    !-----------------------------------------------------------------------
    integer :: n_p_sec, ia, ie; real :: t
    call system_clock(count_rate=n_p_sec)!once
    call system_clock(count=ia)
       DO i=1,10000
         j=i
       END DO                     ! 被测试d程序块
    call system_clock(count=ie)
    t = (ie-ia)/real(n_p_sec)
    write(unit=*,fmt=*)  "Zeit in Sekunden:",t
    end
    !-----------------------------------------------------------------------


     (以下程序转自 编程爱好者论坛 — Fortran讨论区 ) 
  • 1. 如何加大Stack size?
    选Project => Settings => Link => Category: Output => 
    Stack allocations
    Reserve: 这里填新值(默认为1M,若需要10M,则填10000000)

  • 2. 如何用Fortran批量生成文件?
    设要生成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

  • 3. 如何用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

  • 将字符串改为大写的子程序
    subroutine UpCase (str)
       !=========================================
       !  change to upper case
       !=========================================
          character(len=*),intent(in out) :: str
          integer(4)                  :: icha,LL,icval
          integer(4),parameter        :: diff = ichar('a') - ichar('A')
          LL = len_trim(str)
          do icha=1,LL
             icval = ichar(str(icha:icha))
             if (icval>=ichar('a') .and. icval<=ichar('z'))  then
                str(icha:icha) = char(icval-diff)
             end if
          end do
          return
       end subroutine UpCase

  • 5. 如何用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

  • 6. 函数/子程序超载的例子
    设要编一个两个变量值互换的子程序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
  • 8. 推荐好的代码风格
    根据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。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多