简体   繁体   English

有没有一种方法可以使用名称列表I / O功能读取具有可分配组件的派生类型?

[英]Is there a way to use the namelist I/O feature to read in a derived type with allocatable components?

Is there a way to use the namelist I/O feature to read in a derived type with allocatable components? 有没有一种方法可以使用名称列表I / O功能读取具有可分配组件的派生类型?

The only thing I've been able to find about it is https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/269585 which ended on an fairly unhelpful note. 我唯一能找到的是https://software.intel.com/zh-cn/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/269585以一个毫无帮助的音符结尾。

Edit: 编辑:

I have user-defined derived types that need to get filled with information from an input file. 我有用户定义的派生类型,需要用输入文件中的信息填充。 So, I'm trying to find a convenient way of doing that. 因此,我正在尝试找到一种方便的方法。 Namelist seems like a good route because it is so succinct (basically two lines). 名称列表似乎是一条不错的路线,因为它是如此简洁(基本上是两行)。 One to create the namelist and then a namelist read. 一个创建名称列表,然后读取一个名称列表。 Namelist also seems like a good choice because in the text file it forces you to very clearly show where each value goes which I find highly preferable to just having a list of values that the compiler knows the exact order of. Namelist似乎也是一个不错的选择,因为在文本文件中,它迫使您非常清楚地显示每个值的去向,而我发现最好仅列出编译器知道确切顺序的值列表。 This makes it much more work if I or anyone else needs to know which value corresponds to which variable, and much more work to keep clean when inevitably a new value is needed. 如果我或其他任何人需要知道哪个值对应于哪个变量,这将使工作量增加,并且在不可避免地需要新值时保持清洁的工作量也就增加了。

I'm trying to do something of the basic form: 我正在尝试做一些基本的形式:

!where myType_T is a type that has at least one allocatable array in it
type(myType_T) :: thing 

namelist /nmlThing/ thing

open(1, file"input.txt")

read(1, nml=nmlThing)

I may be misunderstanding user-defined I/O procedures, but they don't seem to be a very generic solution. 我可能会误解用户定义的I / O过程,但是它们似乎不是一个非常通用的解决方案。 It seems like I would need to write a new one any time I need to do this action, and they don't seem to natively support the 似乎我每次需要执行此操作时都需要写一个新的,而他们似乎并不本地支持

&nmlThing

  thing%name = "thing1"
  thing%siblings(1) = "thing2"
  thing%siblings(2) = "thing3"
  thing%siblings(3) = "thing4"
  !siblings is an allocatable array
/

syntax that I find desirable. 我认为理想的语法。

There are a few solutions I've found to this problem, but none seem to be very succinct or elegant. 我找到了一些解决此问题的方法,但是似乎没有一个非常简洁或优雅的方法。 Currently, I have a dummy user-defined type that has arrays that are way large instead of allocatable and then I write a function to copy the information from the dummy namelist friendly type to the allocatable field containing type. 当前,我有一个虚拟的用户定义类型,该类型的数组很大,而不是可分配的,然后编写一个函数将信息从虚拟名称列表友好类型复制到包含该类型的可分配字段。 It works just fine, but it is ugly and I'm up to about 4 places were I need to do this same type of operation in the code. 它可以正常工作,但是很丑陋,我需要在代码中执行大约4种操作。

Hence trying to find a good solution. 因此,试图找到一个好的解决方案。

If you want to use allocatable components, then you need to have an accessible generic interface for a user defined derived type input/output procedure (typically by the type having a generic binding for such a procedure). 如果要使用可分配的组件,则需要为用户定义的派生类型输入/输出过程提供一个可访问的通用接口(通常由具有此类过程的通用绑定的类型)。 You link to a thread with an example with such a procedure. 您可以通过带有此类过程的示例链接到线程。

Once invoked, that user defined derived type input/output procedure is then responsible for reading and writing the data. 一旦调用,该用户定义的派生类型输入/输出过程便负责读取和写入数据。 That can include invoking namelist input/output on the components of the derived type. 这可以包括在派生类型的组件上调用名称列表输入/输出。

Fortran 2003 also offers derived types with length parameters. Fortran 2003还提供带有长度参数的派生类型。 These may offer a solution without the need for a user defined derived type input/output procedure. 这些可以提供解决方案,而无需用户定义派生类型输入/输出过程。 However, use of derived types with length parameters, in combination with namelist, will put you firmly in the "highly experimental" category with respect to the current compiler implementation. 但是,将带有长度参数的派生类型与名称列表结合使用,相对于当前的编译器实现,将使您牢牢地处于“高度试验”类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM