Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

fortran - Proper way of doing the systematic computations / initializations at the beginning of a program

I am writing a program that I will use in two main cases: 1D and 2D. The dimension ndim is loaded through a read of an external file, this read I want to do in module param.

I have various declarations of vectors whose sizes depend on the dimension. So obviously I have to compute these sizes at the beginning of my main program. What is the proper way of doing so? I want to use a module but I fail.

module dimensions
  use prec
  use param
  implicit none
  integer ( int32 ), parameter :: nip = 49   ! Number of interior points
contains
subroutine calc_neqn ( )
  use prec
  implicit none
  integer ( int32 ) :: nup                   ! Number of unknown points
  integer ( int32 ) :: neqn                  ! Number of equations

! compute number of unknown points
  nup = (nip+2)**ndim
! compute number of equations
  neqn = 2*nup

end subroutine calc_neqn
! systematic computations
  call calc_neqn ( )
end module dimensions

I get the following - very clear - error message:

Error: Unexpected CALL statement in CONTAINS section at (1)

In other words, can a module call a subroutine? And what would be the proper syntax?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, the module itself cannot perform any executable code. There are programming languages that allow a default initialization when using a module, but Fortran is not one of them.

You should place your initialization code inside a subroutine and you must remember to call this subroutine from the main program or from some other procedure.

The object oriented approach would be to use an object instead of a module and to have the initialization code in the object initializer procedure.

If your ndim was a parameter, you could evaluate the constant precision at compile time. If it is not, you must call calc_neqn when you get ndim yourself. Then you can allocate your vector (which you do not show) as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...