fortran问题

更新时间:2024-04-08 00:03:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

imsl7.0中用use linear_operators这句话是会出错的,当时intel论坛上也有人问,后来intel给出了个X64 imsl的补丁,但是32位的没有。

要使用包含在linear_operators这个库中的函数时,要用use+原函数 例如:上面那个例子把use linear_operators改成use operation_xt即可 另外,imsl7.0引用函数和以前版本不一样的 补充一点:

linear_operators这个文件是这样子的(一看就知道原因了): modulelinear_operators

usecond_int usedet_int usediag_int

usediagonals_int usefft_int useifft_int useeye_int

uselin_eig_self_int uselin_sol_self_int usenorm_int useoperation_i useoperation_ix useoperation_t useoperation_h useoperation_tx useoperation_hx useoperation_x useoperation_xi useoperation_xt useoperation_xh useorth_int userand_int userank_int usesvd_int useunit_int useeig_int usechol_int useisnan_int

end module

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 doi=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,*) \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,*) \write(*,form) data stop end

4. MS POWERSTATION4.0是不是很垃圾?

POWERSTATION4.0是垃圾,其中Bug太多,多到不可用的地步! 在这个主题里,换了CVF后问题就没了的人已有相当的数目。

如果你用POWERSTATION4.0,遇到莫名其妙的错误,建议换CVF6.6,这是一个比较成熟的编译器。

5. 如何用F90/95生成随机数? 注意:

现在计算机产生的随机数都是伪随机数。

random_number(x) 产生一个0到1之间的随机数(x可以是向量),但是每次总是那几个数。 用了random_seed ()后,系统根据日期和时间随机地提供种子,使得随机数更随机了。 program random implicit none real :: x

callrandom_seed () ! 系统根据日期和时间随机地提供种子 callrandom_number (x) ! 每次的随机数就都不一样了 write(*,*) x stop

end program random

6. 函数/子程序超载的例子

设要编一个两个变量值互换的子程序swap(a,b),哑元a,b可能是实型数,整型数,数组,矩阵,字符串,派生类型等等。但是希望只用一个子程序接口swap(a,b)来实现。F90可以 用类属接口来实现这种子程序超载: module Utilities implicit none

privateI_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

subroutinei_swap (a,b) ! 整型置换 integer (ikind),intent(in out) :: a,b integer (ikind) :: t 。。。 ! 略 end subroutine i_swap

subroutiner_swap (a,b) ! 实型置换 real (rkind), intent(in out) :: a,b real (rkind) :: t t = a a = b b = t return

end subroutine r_swap

subroutineRVec_swap (a,b) ! 实型向量置换 real (rkind), intent(in out) :: a(:),b(:)

integer (ikind) :: i doi=1, size(a)

callR_Swap (a(i),b(i)) end do return

end subroutine RVec_swap

subroutineRMat_swap (a,b) ! 实型矩阵置换 。。。 ! 略 end subroutine RMat_swap

subroutineType_swap (a,b) ! 派生类型置换 。。。 ! 略 end subroutine Type_swap end module Utilities

7. 如何在CVF中为多行代码加注释? 做法:

(1) 在..\\Microsoft Visual Studio\\Common\\MSDEV98\\MACROS文件夹下生成文件GrpComment.dsm

(2) 用文本编辑器打开该文件,将以下所附的代码贴在其中,保存(注意保留.dsm后缀) (3) 启动CVF,选Tools=>Customize=>Add-ins and Macro Files (4) 在GrpComment前打勾,去掉其他的勾

(5) 在同一对话框中选Commands=>Macros,此时在右边可以看见CommentDel和CommentOut

(6) 选中CommentOut,拖到CVF的工具栏上去(添加工具钮),会弹出Button Appearance对话框

(7) 选Image and text,在下边Button text框中输入名称(默认是CommentOut),如“加注释”

(8) 类似的方法再将CommentDel命令以工具钮的形式添加到工具栏上,名称可取为“去注释”

这时,工具栏上应该多了两个工具钮:“加注释”和“去注释”。 用法:

加注释:选择要加注释的多行代码,点击“加注释”按钮即可; 去注释:选择已经注释的多行代码,点击“去注释”按钮即可。 适用:后缀为f90或f77的代码文件。 Enjoy!!!

VBscript代码:

Function FileType (ByVal doc) ext = doc.Name FileType = 0

pos = Instr(ext, \ifpos> 0 then

Do While pos<> 1

ext = Mid(ext, pos, Len(ext) - pos + 1)

pos = Instr(ext, \ Loop ext = LCase(ext) end if

If ext = \FileType = 8

ElseIfext = \FileType = 9 Else FileType = 0 End If End Function

Sub CommentOut ()

'DESCRIPTION: 为所选的多行代码加注释 Dim win

set win = ActiveWindow ifwin.type<> \

MsgBox \else

TypeOfFile = FileType(ActiveDocument)

If TypeOfFile = 8 OrTypeOfFile = 9 Then If TypeOfFile = 8 Then CommentType = \ Else

CommentType = \ ' Fortran 77 file End If

StartLine = ActiveDocument.Selection.TopLine EndLine = ActiveDocument.Selection.BottomLine If EndLine

If EndLine = StartLine Then ActiveDocument.Selection.SelectLine

ActiveDocument.Selection = CommentType + ActiveDocument.Selection Else

For i = StartLineToEndLine ActiveDocument.Selection.GoToLinei ActiveDocument.Selection.SelectLine

ActiveDocument.Selection = CommentType + _ ActiveDocument.Selection

Next End If else

MsgBox(\

\ \ \ End If End If End Sub

Sub CommentDel ()

'DESCRIPTION: 去除所选的多行代码的注释 Dim win

set win = ActiveWindow ifwin.type<> \

MsgBox \else

TypeOfFile = FileType(ActiveDocument)

If TypeOfFile = 8 OrTypeOfFile = 9 Then StartLine = ActiveDocument.Selection.TopLine EndLine = ActiveDocument.Selection.BottomLine If EndLine

If EndLine = StartLine Then ActiveDocument.Selection.SelectLine

ActiveDocument.Selection = mid(ActiveDocument.Selection, 3) Else

For i = StartLineToEndLine ActiveDocument.Selection.GoToLinei ActiveDocument.Selection.SelectLine

ActiveDocument.Selection = mid(ActiveDocument.Selection, 3) Next End If else

MsgBox(\

\ \ \ End If End If

End Sub

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。

9. 将字符串改为大写的子程序 subroutineUpCase (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) doicha=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

10. CVF中源代码信息浏览

默认情况下,代码信息浏览是失效的(Go to Definition/Reference都不起作用),你可以将其启用:

1. Project/Settings/Fortran, 选中Generate Source Browse Information。 2. 选BrowseInfo卡页,选中Build Browse info file,点击OK。 3. Build程序。代码浏览器仅当成功Build程序后才可应用。 4. Tools/Source Browser。

5. 在Browse对话框底部,找到Case sensitive复选框。Fortran是大小写不敏感的语言,去除Case sensitive的选中。点击OK。

6. 现在可以用Browse对话框来浏览函数调用关系以及变量声明和引用关系。 7. 光标置于一个变量名或函数名中,鼠标右键:Go to Definition使光标跳到该变量声明或函数定义语句;Go to Reference使光标跳到该变量的引用或函数引用语句。 注:(Addison提供)

IVF中现在不适用,因为此技术是属于MS的,不是INTEL的,所以,intel正在与MS就有关问题进行磋商,目前的进展比较顺利,可能要在9.1以后的版本中支持,目前还没有时间表 。

11. 如何在CVF中检验代码是否符合F90/95标准?

为了检验你的代码是否符合F90或F95标准,在CVF中选: Project/Settings... 选Fortran页卡

Category下拉框选:Compilation Diagnostics

Fortran Standard Checking下拉框选:Fortran 90 或 Fortran 95 这样,凡是CVF自家扩展的语法就都会有提示了。 例: type test

integer, allocatable :: a(:) integer, pointer :: b(:)

end type test

若选了标准检验,会提示:

Warning: Allocatable fields of derived types are non-standard [A] 也就是说按照F90/95标准,type中不允许用allocatable。 臭石头雪球提供:

VS2005 + IVF9.1 组合的方法差不多:

项目属性,Fortran,Diagnostics,Warn For Non-standard Fortran 选择为Fortran 90 或 Fortran 95

12. 如何输出tab字符? char(9)。

13. 如何在CVF中设置把Tab转为空格

Tab字符不在Fortran标准中,在不同环境下会有不同的显示,不推荐使用。 采用下列方法,可将Tab键转为3个空格: 依次选菜单Tools/Option, 选Tab卡项,

File type选Fortran, 选Insert spaces, Tab size选3, Indent size选3。

14. 如何实现“代码自动补全” 在CVF中: 做法:

(1) 启动CVF,选Tools=>Customize=>Add-ins and Macro Files,在SAMPLE.DSM前打勾; (2) 在同一对话框(Customize)中,选Keyboard=>Category:Macro=>Commands:AutoCompleteFromFile; (4) 在Press new shortcut中输入你希望用的快捷键,俺用的是Alt+Space,(Ctrl+Space已经用于英汉语言切换了)

(5) 点击Assign=>Close。 注意:

如果你已经安装了多行注释的宏命令GrpComment.dsm,则

(1) 在..\\Microsoft Visual Studio\\Common\\MSDEV98\\MACROS文件夹下找到SAMPLE.DSM; (2) 用文本编辑器打开该文件,将其中的Sub CommentOut ()中的子过程名改为CommentOut0 (CommentOut和多行注释的子过程名相重了) (3) 保存该文件即可。 用法:

在当前的代码文件中,您键入一个字词的一两个字母,按快捷键Alt+Space,则可启动该宏命令在你的文件中搜寻你要键入的字词,并自动补全显示;再按快捷键则显示下一个可选 的字词。

例如,你当前的代码文件中有: implicit none integer :: ix,iy

则你键入i后,按快捷键,则自动将i补全为implicit,再按快捷键,则依次改换为integer=>ix=>iy=>implicit;

若你键入in后,按快捷键,则直接出现integer. 在IVF中: 做法:

为保险起见,可在Microsoft Visual Studio 8中找到Samples.vsmacros做一个备份。 (I) 修改宏命令

在VS2005中选的宏资源管理器中装入Samples,选中打开DevStudio6Editor。 下面对其中的几条命令作修改: (1) Sub AutoCompleteFromFile中: (改1)

origLine = sel.CurrentLine origCol = sel.CurrentColumn

sel.WordLeft(DsMovementOptions.dsExtend) 后添加

If origLine<>sel.CurrentLine Then sel.Cancel()

sel.MoveToLineAndOffset(origLine, origCol) Exit Sub End If

(改2)

FillCompletionWords(sel.Text)

sel.MoveToLineAndOffset(origLine, origCol) sel.WordLeft(DsMovementOptions.dsExtend) SuggestNextCompletionWord()

改为

If sel.Text<>previousSelectionOrcompletionWords = \FillCompletionWords(sel.Text)

sel.MoveToLineAndOffset(origLine, origCol) sel.WordLeft(DsMovementOptions.dsExtend) End If

SuggestNextCompletionWord() (2) Sub FillCompletionWords中: (改1)

searchString = \

改为

searchString = \(改2)

sel.WordRight()

sel.WordLeft(DsMovementOptions.dsExtend) 改为

sel.WordLeft()

sel.WordRight(DsMovementOptions.dsExtend) (3) Function SuggestNextCompletionWord中:

selection.WordLeft(DsMovementOptions.dsExtend) 封掉

'selection.WordLeft(DsMovementOptions.dsExtend)

(II) 添加工具钮、快捷键

感谢hanronggui提供的方法:

VS2005中选:工具-自定义-命令中选择宏

在右边的面板中找到Samples.DevStudio6Editor.AutoCompleteFromFile,拖到工具栏上; 在“自定义”中点“修改选中的内容”按钮, 将名称改为:自动补码(&A); 关闭自定义 用法:

在当前的代码文件中,您键入一个字词的一两个字母,按该按钮或者快捷键Alt+a,则可启动该宏命令在你的文件中搜寻你要键入的字词,并自动补全显示;再按快捷键则显示下一 个可选的字词。

例如,你当前的代码文件中有: implicit none integer :: ix,iy

则你键入i后,按快捷键,则自动将i补全为implicit,再按快捷键,则依次改换为integer=>ix=>iy=>implicit;

若你键入in后,按快捷键,则直接出现integer.

selection.WordLeft(DsMovementOptions.dsExtend) 封掉

'selection.WordLeft(DsMovementOptions.dsExtend)

(II) 添加工具钮、快捷键

感谢hanronggui提供的方法:

VS2005中选:工具-自定义-命令中选择宏

在右边的面板中找到Samples.DevStudio6Editor.AutoCompleteFromFile,拖到工具栏上; 在“自定义”中点“修改选中的内容”按钮, 将名称改为:自动补码(&A); 关闭自定义 用法:

在当前的代码文件中,您键入一个字词的一两个字母,按该按钮或者快捷键Alt+a,则可启动该宏命令在你的文件中搜寻你要键入的字词,并自动补全显示;再按快捷键则显示下一 个可选的字词。

例如,你当前的代码文件中有: implicit none integer :: ix,iy

则你键入i后,按快捷键,则自动将i补全为implicit,再按快捷键,则依次改换为integer=>ix=>iy=>implicit;

若你键入in后,按快捷键,则直接出现integer.

本文来源:https://www.bwwdw.com/article/d0rr.html

Top