Fortran子例程中的数组分配 [英] Array allocation in Fortran subroutine

查看:86
本文介绍了Fortran子例程中的数组分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是有关Fortran中的数组分配.

My question is about array allocation in Fortran.

我有一个子程序,例如 readParams ,我想从文件中读取一些动态大小的数组.这些也可在子例程外部使用.处理此问题的最佳方法是什么?

I have a subroutine, say readParams, where I want to read some dynamically sized arrays from files. These are also used outside the subroutine. What is the best way to handle this?

在F95中,似乎不可能在子例程中进行分配,并将填充了值的数组传递回主程序.但是,如果我在主程序中分配它并在子例程中使用"intent(inout)",它也将被释放.

In F95 it seems to be impossible to allocate within the subroutine and pass the arrays, filled with values, back to the main program. But if I allocate it in the main program and use "intent(inout)" in the subroutine it also gets deallocated there.

(我在这里使用的是F90/95,但是由于代码不大,我也可以将其修改为更新的版本...对于Fortran来说我还很陌生,所以我不确定是否可以改进数组处理是值得的时间投资^^

(I'm using F90/95 here, but since the code is not large I could also modify it to a newer version... I'm rather new to Fortran, so I'm unsure if an improvement of array handling is worthwhile the time investment^^

编辑:感谢您的提示.我不是试图在子例程中取消分配我的数组.

EDIT Thanks for the hint. I am not trying to deallocate my arrays within a subroutine though.

问题是:我有一个数组,需要在我的 main 程序中分配 somewhere .只有在我从子例程 readArgs 的输入中读取了数组大小后,该数组大小才知道.因此,我使数组可分配".一旦分配,该状态就永远不能再改变.数组在子例程 readParams 中填充了值.我是否最好在 main readParams 中分配它?如何分配?

The problem is: I have an array which I need to allocate somewhere within my main program. The arraysize is known only after I read it from an input in subroutine readArgs. Therefore I make the array "allocatable". Once allocated that status must never change again. The array is filled with values in a subroutine readParams. Do I allocate it best in main or in readParams and how?

...现在,我已将子例程放入模块中,并从那里使用它们.目前,我在 main 中进行分配,将数组传递给我的子例程,并删除了该子例程中数组声明中的"allocatable"语句.似乎可行,但我仍然不太了解这是否是可行的方法.

... I have now put my subroutines in a module and use them from there. At the moment I do the allocation in main, pass the arrays to my subroutine and have removed the "allocatable" statement in the array declaration in the subroutine. It seems to work but I still don't really understand if this is the way to go.

推荐答案

在我看来,问题是但是我很反对这种观点,所以,如果您的代码看起来与此类似,则可能是正确的:

But I am alone with this opinion against many, so if your code looks similar to this it is probably correct:

您可以在子程序中读取大小并在主程序中分配它:

You can either read the size in a subroutine and allocate it in the main program:

module subs
contains
  subroutine readArgs(n)
    integer, intent(out) :: n
    !read n here
  end subroutine

  subroutine readParams(a)
    real :: a(:)

    do i = 1, size(a)
      !read values of a
      a(i) =
    end do
  end subroutine
end module

program main

  use subs

  integer :: n

  readArgs(n)

  allocate(a(n))

  readParams(n)
end program

或者您可以在子例程中分配它.您必须满足

Or you can allocate it in the subroutine. You must fulfil all the requirements in Can a input argument of a Fortran subroutine be deallocated and allocated in the body of the subroutine?

module subs
contains
  subroutine readArgs(n)
    integer, intent(out) :: n
    !read n here
  end subroutine

  subroutine readParams(n, a)
    real, allocatable :: a(:)

    allocate(a(n))

    do i = 1, size(a)
      !read values of a
      a(i) =
    end do
  end subroutine
end module

program main

  use subs

  integer :: n

  readArgs(n)

  readParams(n)
end program

做什么都没关系,两种方法都很好.

It doesn't matter what you do, both approaches are pefectly fine.

这篇关于Fortran子例程中的数组分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆