Fortran 90 - 将主子例程的值传递给函数和其他子例程 [英] Fortran 90 - to transmit values from main subroutine to the functions and other subroutines

查看:176
本文介绍了Fortran 90 - 将主子例程的值传递给函数和其他子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望大家都做得很好。我目前有一个项目正在工作,我很难处理一些编程技术。

总结我的项目,我必须修改Fortran上的一些代码,以便它可以适用于名为PRO / II的仿真软件。所有的函数和子程序都已经写好了。然而,为了使代码与PRO / II兼容,我必须改变分配一些输入数据(输入由用户自己)在Fortran上。事实上,用户在之前将数据输入到由fortran子程序读取的文本文件中。不过,现在,数据直接输入到仿真软件中。我设法编写一个代码来记录子程序中的所有输入数据。但是,当仿真在PRO / II上运行时,它只将输入数据归入主子程序。主子程序之外的任何函数或子程序都无法访问这些值。实际上,PRO / II只给出我主子程序的参数。



从那里开始,当从主子程序调用一个函数时,没有问题。这是调用其他函数或子例程的问题的函数。我会尽量让自己变得尽可能清楚。假设我有一个子程序X和许多函数和子程序如下:
结束子程序


函数A(变量)

使用函数B和C

结束函数

函数B(变量)

使用函数D和E

结束函数

函数C(变量)

使用函数D和E

结束函数

函数D(变量)

结束函数

函数D(变量)

结束函数

函数E(变量)

结束函数

所以,问题是我在主子例程中计算的值或者在PRO / II中输入的值传输到Fortran程序时,无法访问函数D和E.因此,我尝试从主子例程中拷贝所需的所有值到文本文件中,并且每次通过不同的函数和子例程读取所有值。但是,PRO / II运行模拟将永远持续下去。我有80个函数和20个子程序,每次调用它们时,它们都会打开文本文件以读取值。



有没有办法让我拥有由所有函数和子例程读取的值无需从文本文件读取?换句话说,有没有办法让我的主子程序中计算出的所有变量都变成我程序中的每个函数和子程序?



我真的很难找出答案。



如果你们不明白问题或有任何问题,请让我知道。



预先感谢您的帮助。

解决方案您可以将您的值放入模块中的变量,并且使用该模块在所有功能和子程序中,以及在主程序中!



以下是一个小例子:

  module globVar 
隐式无

integer :: var1
结束模块

模块微积分

包含
函数doStuff(输入)
使用globVar,仅:var1
隐式none
整数,意图(in)::输入
整数:: doStuff

doStuff =输入* var1
结束函数
结束模块微积分


程序测试
使用globVar
使用微积分

隐式无

写入(*,*)'输入'var1''
读取*,var1

写入(*,*)doStuff(2)
结束程序


I hope everyone's doing good. I presently have a project at work and I'm having a hard time dealing with some programming techniques.

To summarize my project, I have to modify some codes on Fortran so that it can be adapted to be used on a simulation software called PRO/II. All the functions and subroutines have already been written.

However, to make the codes compatible with PRO/II, I have to change the way of assigning some input data (input by the user himself) on Fortran. In fact, before, the user entered the data on a text file which was then read by a fortran subroutine.

However, now, the data is input in the simulation software directly. I managed to write a code to record all the input data in a subroutine. But when the simulation is ran on PRO/II, it only attributes the input data to the "MAIN SUBROUTINE". The values are not accessible to any functions or subroutines outside the main subroutine. In fact, PRO/II gives the values to the arguments of my main subroutine only.

As from there, when a function is called from the main subroutine, there's no problem. It's the function that calls other functions or subroutines that are the issues. I'll try to make myself as clear as possible. So let's say I have a subroutine X and many functions and subroutines as follows:

Subroutine X


End Subroutine


Function A(variables)

Uses Functions B and C

End Function

Function B(variables)

Uses Function D and E

End Function

Function C(variables)

Uses functions D and E

End Function

Function D(variables)

End function

Function D(variables)

End Function

Function E(variables)

End Function

So, the problem is that the values I calculated in my main subroutine or the values I input in PRO/II which are transmitted to the Fortran program are not accessible to functions D and E. So, I tried copying all the values needed to a text file from the main subroutine and reading all the values each time by the different functions and subroutines. But it's taking forever for the simulation to run by PRO/II. I have like 80 functions and 20 subroutines, and each time they are called, they open the text file to read the values.

Is there a way for me to have the values read by all functions and subroutines without having to read from the text file? In other words, is there a way to make all the variables I've calculated in my main subroutine to every function and subroutine in my program?

I'm really having a hard time figuring that out.

If you guys don't understand the problem or have any questions, please let me know.

Thanks in advance for your help guys.

解决方案

You could put your values into variables in a module, and use that module in all functions and subroutines, as well as in the main program!

Here is a small example:

module globVar
  implicit none

  integer :: var1
end module

module calculus

contains
  function doStuff(input)
    use globVar, only: var1
    implicit none
    integer,intent(in)  :: input
    integer             :: doStuff

    doStuff = input*var1
  end function
end module calculus


program test
  use globVar
  use calculus

  implicit none

  write(*,*) 'Enter "var1"'
  read *,var1

  write(*,*) doStuff(2)
end program

这篇关于Fortran 90 - 将主子例程的值传递给函数和其他子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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