使用假定秩的Fortran数组作为派生类型组件 [英] Using assumed-rank Fortran array as derived type component

查看:222
本文介绍了使用假定秩的Fortran数组作为派生类型组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有一个派生类型(派生类型) 结果数据组件(除其他外):

 type,public :: Result 
private $ b $ class(*),allocatable :: data
...
结束类型

它由某个过程初始化(从接口 Result ):
$ b $ pre $ 函数init(数据)结果(this)
类型(结果):: this
类(*),intent(in):: data

allocate(this%data,source = data)
结束函数
pre>

这对于标量数据来说工作得很好,但我不确定如何设置它以适用于任意级别的数据。我知道过程可以接受假定的参数,所以 init(data)函数中的 data 可以是 data(..),但我不确定是否可以指定派生类型属性 this%data 作为假定rank?



另一种我认为这样做的做法是将任意级别的数据存储为标量,如果Fortran中可能存在这样的情况?这相当于使用 serialize() unserialize()函数。如果任何人有类似的方法的指导,我会非常感激。

最终,我想能够做到这样的事情:



$ p $ type(Result):: scalarResult
type(Result):: 2DResult

scalarResult = Result( data = 1.234)
2DResult = Result(data = [[1,2,3],[4,5,6]])


解决方案

不可能拥有假定的等级派生类型组件。你需要找到另一个解决方案,比如弗拉基米尔建议的。另一种想法是存储数据和等级的C_LOC,并调用一个C例程来构造一个适当的C描述符,然后用一个假定秩参数调用回Fortran例程。



请记住,您无法在Fortran中使用假定的排名做很多事情 - 直到F2015,SELECT RANK才会存在。


I'm looking for some general advice as to the best approach to creating a derived type that stores arbitrary data with arbitrary rank.

Say I have a derived type Result with a data component (amongst others):

type, public :: Result
    private
    class(*), allocatable :: data
    ...
end type

which is initialised by some procedure (called from an interface Result):

function init(data) result(this)
  type(Result) :: this
  class(*), intent(in) :: data

  allocate(this%data, source=data)
end function

This works fine for scalar data, but I'm unsure about how to go about setting it up for arbitrary-rank data. I know procedures can accept assumed-rank parameters, so data in the init(data) function could be data(..), but I'm not sure it's possible to specify the derived type property this%data as being a assumed rank?

The other way I thought of doing it would be to store the arbitrary rank data as a scalar, if such a thing is possible in Fortran? This would be equivalent to using the serialize() and unserialize() functions in PHP. If anyone has any guidance on a similar approach, I'd be very grateful.

Ultimately I'd like to be able to do something like this:

type(Result) :: scalarResult
type(Result) :: 2DResult

scalarResult = Result(data=1.234)
2DResult = Result(data=[[1,2,3],[4,5,6]])

解决方案

It's not possible to have an assumed rank derived type component. You'll need to find another solution, such as what Vladimir suggests. Another idea, which is sort of a kluge, is to store the C_LOC of the data and the rank, call out to a C routine that constructs an appropriate C descriptor and then calls back in to a Fortran routine with an assumed-rank argument.

Do keep in mind that you can't do a lot with assumed rank in Fortran - SELECT RANK doesn't exist until F2015.

这篇关于使用假定秩的Fortran数组作为派生类型组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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