我正在编写一个 Fortran 程序,其中一行是这样写的
open(unit=un1,file=filenm,form='unformatted',buffered='yes',status='replace',buffercount=127)
Run Code Online (Sandbox Code Playgroud)
我已经定义了
character*256 filenm.
un1=11
Run Code Online (Sandbox Code Playgroud)
但我仍然收到错误
"Syntax error at or near =" in that line..
Run Code Online (Sandbox Code Playgroud) 在我的程序中,我需要存储不同情况的结果文件。我决定创建单独的目录来存储这些结果文件。这里用伪代码来解释具体的情况。
do i=1,N ! N cases of my analysis
U=SPEED(i)
call write_files(U) !Create a new directory for this case and Open files (1 = a.csv, 2 = b.csv) to write data
call postprocess() !Write data in files (a.csv, b.csv)
call close_files() !Close all files (1,2)
end do
subroutine write_files(i)
!Make directory i
!Open file a.csv and b.csv with unit 1 & 2
!Write header information in file a.csv and b.csv
close subroutine
Run Code Online (Sandbox Code Playgroud)
我正在努力将实际变量 U 转换为字符变量,以便我可以用来call system('mkdir out/' trim(U))创建单独的文件夹来存储我的结果。 …
问题:我有一些我自己和其他人编写的代码,我使用了这些代码并使用 mpi 和 openmp 取得了很好的结果(有助于我在 Blue Gene/Q 上运行它)。
我不喜欢的一件事是,现在我无法在没有 -openmp 指令的情况下编译代码,因为为了获得我需要的加速,我使用了缩减变量。例子:
!$OMP parallel do schedule(DYNAMIC, 4) reduction(min:min_val)
....
min_val = some_expression(i)
....
!$OMP end parallel do
result = sqrt(min_val)
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似的东西:
!$OMP if OMP:
!$OMP min_val = some_expression(i)
!$OMP else:
if ( min_val .gt. some_expression(i) ) min_val = some_expression(i)
!$OMP end else
Run Code Online (Sandbox Code Playgroud)
有人知道这样的事情吗?请注意,如果没有 -openmp,!$OMP 行将被忽略,代码会正常运行,并给出正确的、呃相同的答案。
谢谢,
(是的,它是 FORTRAN 代码,但它与 C 和 C++ 几乎相同)
我在 Fortran90 中编码,即使我知道应该避免它,我也需要使用抽象类型。现在我希望定义一个函数,它可以在参数中采用抽象类型或派生类型。为此,我想创建两个函数的接口,但出现以下错误:
(1) 通用接口“初始化”中的模糊接口“初始化抽象”和“初始化逻辑”
代码如下所示:
type type1
contains
procedure, pass(transf) :: initialize_logic
procedure, pass(transf) :: initialize_abstract
end type
interface initialize
module procedure initialize_logic
module procedure initialize_abstract
end interface
function initialize_logic(element, element_logic)
type(type1), pointer :: element
type(type_logic), pointer :: element_logic
end function
function initialize_abstract(element, element_abstract)
type(type1), pointer :: element
class(type_abstract), pointer :: element_abstract
end function
Run Code Online (Sandbox Code Playgroud)
type_abstract 是从 type_logic 扩展而来的。
我不知道如何摆脱这个消息错误,我真的需要能够使用抽象类型或扩展类型(此处:“logic_type”)来调用它,例如:
class(type_abstract), pointer :: element_abstract
type(type_logic), pointer :: element_logic
type(type1) :: e1, e2
call e1%initialize(element_abstract)
call e2%initialize(element_logic)
Run Code Online (Sandbox Code Playgroud)
如果我只有一个初始化程序,将抽象类作为参数,在使用扩展类型进行初始化时,我会收到以下错误:
错误:(1) 处“element_abstract”的实际参数必须具有相同的声明类型
有任何想法吗 …
我有以下代码:
program main
character (len=15) :: abc = "te st tex t"
print *, trim(abc)
end program main
Run Code Online (Sandbox Code Playgroud)
哪些输出:
te st tex t
Run Code Online (Sandbox Code Playgroud)
我排除了所有要删除的空格,但事实并非如此。如何从字符串中删除所有空格?
我试图在 C++ Fortran 互操作程序中将多维 Fortran 数组传递给 C++ 程序。我对如何将数组从 Fortran 传递到 C++ 有一个基本的想法;您将数组的位置从 Fortran 传递给 C++。然后 C++ 采用扁平数组,你必须做一些代数计算才能找到给定多维数组中的元素。
我能够在标量数组上成功测试这个想法。在 C++ 中找出元素的索引并不难,因为它是从 Fortran 索引线性映射到 C++ 的,偏移量为 -1。Fortran 和 C++ 的示例代码是:
! Fortran main program
program fprogram
integer :: i
real*8 :: array(2)
array(1) = 1.0
array(2) = 2.0
! call cpp function
call cppfuncarray(array, 2)
write(*,*) array
end program
Run Code Online (Sandbox Code Playgroud)
! Fortran main program
program fprogram
integer :: i
real*8 :: array(2)
array(1) = 1.0
array(2) = 2.0
! call cpp function
call …Run Code Online (Sandbox Code Playgroud) 我使用 Fortran 90 和 gfortran 编译器作为 cygwin 的一部分。我想编写一个函数,该函数将在一个目录中创建一系列新文件夹,该目录也作为参数传递,并附带一个数字,该数字是新的连续编号文件夹的最大数量。由于我必须声明字符(即字符串)的长度,但也希望普遍能够传递不同的路径,因此我尝试将修剪后的字符串传递给函数。
program main
implicit none
character(len = 6) :: newdir
character(len = 27) :: path
newdir = "neu1A"
path = "c:/users/i/desktop/rainer"
print*,len_trim(path) !Outputs the correct length of 25
print*,len_trim(newdir) !Outputs the correct length of 5
call newdirec(trim(newdir),trim(path),5)
end program main
Run Code Online (Sandbox Code Playgroud)
但由于我必须在函数中重新声明参数,因此它们的长度在此过程中被覆盖/丢失。如何使用正确的字符串长度并保持函数的通用性?我必须使用长度,因为构建调用系统来创建目录的字符串需要格式化字符串。我使用 Fortran 90,因此一些选项不可用。
function newdirec(newdir,path, foldnum)
character (len = 27) :: path
character (len = 50) :: newdir
character (len = (len_trim(path) + len_trim(newdir))) :: newpath
character (len = 100) :: format_string, newdir_len_str, …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个从给定文件中提取指定行的函数。我这样做的函数需要两个参数:
我已经将这个函数封装在一个模块(routines.f95)中,如下所示:
module routines
contains
function getLine(fUnit, fLine)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Get the nth line of a file. It is assumed that the file is !
! numerical only. The first argument is the unit number of the !
! file, and the second number is the line number. If -1 is !
! passed to the second argument, then the program returns the !
! final line of the program. It is further assumed that …Run Code Online (Sandbox Code Playgroud) 我知道有些语言在编程中结合了许多不同范式的元素。有人告诉我 Fortran 是函数式语言的一个例子,但是我对它是否是纯函数式有点困惑,因为它似乎主要用于数学函数,但是我也读到它是也可以将面向对象的编程应用于 Fortran,那么它是某种混合吗?
paradigms fortran programming-languages functional-programming fortran90
我正在尝试编译fortran文件以及FORTRAN中的一些.h文件..h文件包含常见变量块的定义.当我在Fortran中编译它们时,我收到以下错误:
integer knue,ke,knumu,kmu,knutau,ktau,ku,kd,kc,ks,kt,kb,kgamma,
1
Error: Invalid character in name at (1)
Run Code Online (Sandbox Code Playgroud)
发生此错误的代码是,
现在我的问题是,这个"1"点是错误的吗?
这个错误指向的代码行是,
integer knue,ke,knumu,kmu,knutau,ktau,ku,kd,kc,ks,kt,kb,kgamma,
& kw,kz,kgluon,kh1,kh2,kh3,khc,ksnue,kse1,kse2,ksnumu,ksmu1,
& ksmu2,ksnutau,kstau1,kstau2,ksu1,ksu2,ksd1,ksd2,ksc1,ksc2,
& kss1,kss2,kst1,kst2,ksb1,ksb2,kn1,kn2,kn3,kn4,kcha1,kcha2,
& kgluin,kgold0,kgoldc
Run Code Online (Sandbox Code Playgroud)
此外,使用延续的方式是否有问题.我正在使用gfortran来编译这个文件.