小编fra*_*lus的帖子

用变量值初始化常量

program main
   real, parameter :: a = 1 
   !real :: a
   !a=1

   res = func(a)
   write(*,*) res

end program main

function func(a)
   real, parameter :: b=a+1  !(*)
   func = b
   return
end function func
Run Code Online (Sandbox Code Playgroud)

我的编译器在标记为(*)的行处抱怨.有没有办法用一个超出该函数的值来设置常量的值?

fortran

4
推荐指数
2
解决办法
9990
查看次数

Fortran可分配数组和指针之间的等价性

我有一个带有可分配数组的Fortran程序,A如下所示:

real, dimension(:,:) allocatable :: A
...
allocate(A(x0:x1;y0:y1))
Run Code Online (Sandbox Code Playgroud)

该数组最终作为参数传递给子程序,看起来像

subroutine my_subroutine(arr)
   real, dimension(x0:x1,y0:y1) :: arr
   ...
end subroutine my_subroutine
Run Code Online (Sandbox Code Playgroud)

我想用C库中实现allocate的自定义内存分配函数替换Fortran语句my_alloc.我将第一个代码示例更改为:

type(c_ptr) :: cptr
real, pointer, dimension(:,:) :: A
...
cptr = my_alloc(...)
call c_f_pointer(cptr,A,[x1-x0+1,y1-y0+1])
Run Code Online (Sandbox Code Playgroud)

这很好,除了通过在c_f_pointer函数中指定范围而不是下限/上限,我丢失了数组的原始形状(x0:x1,y0:y1).但这不是一个大问题:指针作为子例程的参数传递,子例程需要一个数组,并将指针视为一个数组,具有适当的边界.

我真正的问题是:当我还要重写子程序的代码以使用指针而不是数组时.

subroutine my_subroutine(arr)
   real, pointer, dimension(x0:x1,y0:y1) :: arr
   ...
end subroutine my_subroutine
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用; 格福兰说

Array pointer 'arr' at (1) must have a deferred shape
Run Code Online (Sandbox Code Playgroud)

可以编译以下代码

subroutine my_subroutine(arr)
   real, pointer, dimension(:,:) :: arr
   ...
end subroutine my_subroutine
Run Code Online (Sandbox Code Playgroud)

但是当我尝试执行从x0到x1以及从y0到y1的循环时,它不提供边界和程序崩溃. …

arrays fortran pointers

4
推荐指数
1
解决办法
3553
查看次数

错误:格式字符串中所需的非负宽度(1)

我正在尝试使用gfortran编译一段代码,但是它失败并出现以下错误:

Error: Nonnegative width required in format string at (1)
../src/powmes.f90:410.20:
     write(lunit,'(I,E,E,E)') wavenum(k),power(k),nmodes(k),errorexpan(k)

414   if (filepower_fold(1:1) /= '#') then
415      fileout=trim(filepower_fold)//'.waven'
416      if (verbose) write(*,*) 'Output '//trim(fileout)
417      open(file=fileout,form='formatted',status='unknown',unit=lunit,err=2)
418      do k=0,ngrid/2
419         do ifold=0,nfoldpow-1
420            write(lunit,'(I,$)') waven(k,ifold)
421         enddo
422         write(lunit,'(I)') waven(k,nfoldpow)
423      enddo
424      close(lunit)
Run Code Online (Sandbox Code Playgroud)

我该怎么编译呢?

fortran gfortran

4
推荐指数
2
解决办法
1万
查看次数

将类型绑定过程作为参数传递

我试图将类型绑定过程作为参数传递给另一个子例程.我想知道Fortran中是否可行.这是一个代码片段,显示了我想要做的事情.

module type_definitions 
 type test_type 
 integer :: i1, i2,i3
 contains 
   procedure :: add_integers_up 
 end type test_type
 contains 
   subroutine add_integers_up(this,i4,ans)
       class(test_type) :: this 
       integer :: i4,ans 
       ans = this%i1+this%i2+this%i3+i4
    end subroutine add_integers_up

subroutine print_result_of_subroutine(i4,random_subroutine) 
  integer :: i4,ans 

  interface 
     subroutine  random_subroutine(i1,i2) 
       integer:: i1,i2
     end subroutine random_subroutine
  end interface

  call random_subroutine(i4,ans) 
  write(*,*) ans 


end subroutine print_result_of_subroutine


end module type_definitions


program main 
   use type_definitions
   implicit none 
   integer :: i1,i2,i3,i4
   integer :: ans 
   type(test_type) :: test_obj

   i1 =1; i2=2; i3=3
   test_obj%i1 = i1 
   test_obj%i2 = …
Run Code Online (Sandbox Code Playgroud)

fortran function user-defined-types fortran2003

4
推荐指数
1
解决办法
1507
查看次数

Fortran类似于python的super()?

在Fortran 2003/8中使用类,尤其是扩展类型时,是否有任何模拟python的super()函数可用于从扩展类型中调用的扩展类型中调用方法?

python fortran

4
推荐指数
1
解决办法
185
查看次数

第一栏中的"d"标签,Fortran 77

我正在修改一个用Fortran 77编写的模型中的代码,但我遇到了一件奇怪的事情.在某些文件中,行的第一列中有一个标签"d",如下例所示:

d     real*8    co2rootfix,co2rootloss,co2shootfix
d     character komma*1

d     open(unit=87,file='crop_CO2.csv',status='unknown')
d     write(87,*) 'date,co2rootfix,co2rootloss,co2shootfix'
d     open(unit=88,file='crop_dm.csv',status='unknown')
d     write(88,*) 'date,wrtpot,wrt,wstpot,wst,rdpot,rd,laipot,lai,
d    &gwrt,gwst,drrt,drlv,drst'
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它是由英特尔的ifort编译器成功编译的.但是,gfortran逻辑上返回以下错误:

错误:语句标签中的非数字字符(1)

我想知道:

  1. 这个标签的含义;
  2. 为什么它只被ifort认可;
  3. 我如何才能与gfortran合作.

fortran gfortran fortran77 intel-fortran

4
推荐指数
1
解决办法
482
查看次数

如何比较Fortran c_ptr和null

我有一些需要组合的Fortran和C代码.

我正在使用看起来像这样的Fortran界面:

module bridge
  use, intrinsic::iso_c_binding, only : c_ptr, c_null_ptr
  implicit none

  type(c_ptr) :: instance

  interface

    function c_init() result(this) bind(C, name="bridge_init")
      import
      type(c_ptr) :: this
    end function c_init

  end interface

contains

    subroutine init() 
      instance = c_init()
    end subroutine init

end module bridge
Run Code Online (Sandbox Code Playgroud)

我的问题是我想在init子程序中检查初始化,例如

subroutine init()
  if( instance .eq. c_null_ptr ) then
    instance = c_init()
  end if
end subroutine
Run Code Online (Sandbox Code Playgroud)

但这给了我一个Syntax error, found END-OF-STATEMENT when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE INTEGER REAL COMPLEX BYTE CHARACTER CLASS …

fortran fortran-iso-c-binding

4
推荐指数
1
解决办法
1077
查看次数

如何在 Fortran 派生类型中存储对过程的引用

我可以在 Fortran 类型中存储对过程的引用吗?

我的目标是通过将重复的参数分组为一个类型来减少 Fortran 子例程中的重复参数。但是 Fortran 不允许我对外部过程执行此操作。

这是我尝试做的一个简化示例:

module my_functions
    type mytype
        external :: f
    end type
contains
    subroutine fa()
        WRITE(*,*) "yiha"
    end subroutine

    subroutine fb(t)
        type(mytype) t
        call t%f()
    end subroutine
end module

program test
    use my_functions
    type(mytype) :: m
    m%f = fa
    call fb(m)
end program
Run Code Online (Sandbox Code Playgroud)

然而 gfortran 给了我

     external :: f
                 1
Error: Unexpected attribute declaration statement at (1)
Run Code Online (Sandbox Code Playgroud)

fortran

4
推荐指数
1
解决办法
225
查看次数

Fortran 修剪或调整在使用两次时不起作用

我正在尝试将trim/adjustl用于以下代码。似乎我得到了X_eq_ 10.0.dat或者X_eq_10.0 .dat作为输出文件的名称,我期望它是X_eq_10.0.dat(没有空格)。有什么补救办法吗?

           Program Test
           double precision:: X
           character (len=10) :: tag
           character (len=100) :: outfile

           X=10.0

           write(tag,'(f10.1)') X
           print*,'tag=',tag

           outfile='X_eq_'//trim(tag)//'.dat'
           print*,'Output file: ',outfile

           outfile='X_eq_'//trim(tag)//trim('.dat')
           print*,'Output file: ',outfile

           outfile='X_eq_'//adjustl(trim(tag))//adjustl(trim('.dat'))
           print*,'Output file: ',outfile

           End Program Test
Run Code Online (Sandbox Code Playgroud)

我曾经gfortran作为编译器使用过。

fortran trim fortran90

4
推荐指数
1
解决办法
495
查看次数

Fortran 中的“elseif”或“else if”

Fortran 中elseif和之间有什么区别else if吗?同样适用于endifandend ifenddoand end do

我在我正在处理的代码中找到了这两个选项,并想知道它在 Fortran 中是否有任何不同;此外,如果一种选择比另一种更可取。

syntax fortran

4
推荐指数
1
解决办法
251
查看次数