如何在Fortran中明确使用继承的变量? [英] How to explicitely use inherited variables in Fortran?

查看:251
本文介绍了如何在Fortran中明确使用继承的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于模型/变量用法的最佳实践的问题:假设我有一个包含一些变量/参数定义和一些子例程的模块,这些子例程使用这些模块/变量用法变量。



我不需要在子例程中明确使用这些变量,因为它们是从父模块继承的,但是这样做会更好吗?


$ b

示例:

 模块测试
隐式无

integer,parameter :: a = 1
real :: x

包含

子程序idk(y,z)
真实,意图(in):: y
real,intent(out):: z

if(a .eq。1)then
z = x * y + 5.
else
z = x * y - 5.
end如果
结束子程序idk

结束模块测试

上面的例子应该可以正常工作,但是最好添加

 使用测试,只有:a,x 

到子程序<$ c的声明部分


$ b $ p















$ b 1)Pro:显式添加这一行让我们很容易看到哪些变量实际上在子程序中需要。
在许多情况下,模块包含许多变量,但每个子程序只需要少量变量。因此,为了更好的理解,加入这条线将是有益的。





<2> Contra :在很多情况下,需要上面声明的很多变量/参数(有时编号超过100个参数)。明确地在子程序的开头使用这些代码只是不必要地混淆了代码,降低了代码的可读性。



如果只需要包含几个变量,而如果需要包含许多变量,则点2仅非常重要。但是我认为,为一些变量做一件事而为很多人做另一件事会很愚蠢 - 一旦你选择了一个约定,你应该坚持它恕我直言......



是否有关于此的最佳做法?

另外:
或者,可以将子例程声明为

 子程序idk(b,w,y,z)

然后称之为 idk(a,x,y,z)



<一方面,如果我后来决定将 idk 与其他变量一起使用,这会给我更大的灵活性。


$ b另一方面,如果我稍后改变某些东西(比如说,我意识到我不需要参数 a )作为一个条件,它也会增加犯错的风险但是参数 c 。在第一种情况下,我只需将 a - > c 在子程序中,但在最后一种情况下,我需要将每个调用改为 idk(c,...),如果有很多这些ca lls,这很容易出错)



我真的很感谢你的意见!谢谢!

解决方案

绝对没有理由使用模块目前正在定义。这是非法的。如果模块是之前编译的,编译器可以找到 .mod 文件,但是文件,但除此之外,它可能会发生编译。



您应该会遇到如下错误:

  ifort -c assoc.f90 
assoc.f90(10):错误#6928:程序单元中的USE语句上的模块名称不能是任何包含范围单元的名称。 [测试]
使用测试
------ ^

模块子程序通过 host association 获取主机模块的变量,而 use 语句用于使用关联。这些是两个不同的东西,不应混用。



如果您想避免全局变量,请将它们作为参数传递。这是一般性建议。什么是最好的取决于每个案例和程序员,并且不能一般回答。

I have a question regarding best practices of model/variable usage:

Let's assume I have a module containing a few variable/parameter definitions and some subroutines that use these variables.

I do not need to explicitly use these variables in the subroutines since they are inherited from the parent module - but would it be better practice to do so?

Example:

module test
implicit none

integer, parameter :: a = 1
real               :: x

contains

subroutine idk(y,z)
  real, intent(in)  :: y
  real, intent(out) :: z

  if(a .eq. 1) then
    z = x*y + 5.
  else 
    z = x*y - 5.
  end if
end subroutine idk

end module test

The above example should work just fine but would it be better to add

use test, only: a,x

to the declaration part of subroutine idk?

In my reasoning, there are two main points here:

1) Pro: Explicitly adding this line let's me easily see which variables are actually needed in the subroutine. In many cases, the module contains quite a number of variables but only a few are needed in each subroutine. So for reasons of better comprehensibility, it would be beneficial to add this line.

BUT

2) Contra: In quite a few cases, one needs a lot of the variables/parameters declared above (sometimes numbering more than 100 parameters). Explicitly using these at the beginning of the subroutine just unnecessarily clutters the code, reducing the readability of the code.

Point 1 matters mostly if only a few variables need to be included, whereas point 2 is only important if many variables need to be included. But I think it would be silly to do one thing for few variables and another for many - once you have picked a convention, you should stick to it IMHO...

Is there a best practice regarding this?

Addition: Alternatively, one could declare the subroutine as

subroutine idk(b,w,y,z)

and then call it as idk(a,x,y,z).

On the one hand, this would give me greater flexibility if I later decide that I want to use idk with other variables.

On the other hand, it also increases the risk of mistakes if I change something later (say, I realize I don't need parameter a as a condition but parameter c. In the first cases, I simply switch out a -> c in the subroutine. But in the last case, I need to change every call to idk(c,...). If there are a lot of these calls, this is prone to mistakes)

I would really appreciate your input! Thank you!

解决方案

There is absolutely no reason to use the module currently being defined. It is illegal. It may happen to compile if the module was compiled before and the compiler can find the .mod file, but file, but other than that it is wrong.

You should expect error such as

    ifort -c assoc.f90
assoc.f90(10): error #6928: The module-name on a USE statement in a program unit cannot be the name of any encompassing scoping unit.   [TEST]
  use test
------^

The module subroutine gets the variables from the host module through host association and the use statement is for use association. These are two different things and should not be mixed.

If you want to avoid global variables, pass them as arguments. This is a general advice. What is best depends on each case and the programmer and cannot be answered generally.

这篇关于如何在Fortran中明确使用继承的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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