Fortran 90 - to transmit values from main subroutine to the functions and other subroutines

醉酒当歌 提交于 2020-01-15 05:14:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/19476699/fortran-90-to-transmit-values-from-main-subroutine-to-the-functions-and-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!