CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   Fortran programming style (http://www.cfd-online.com/Forums/main/84306-fortran-programming-style.html)

Shenren_CN January 25, 2011 21:33

Fortran programming style
 
Hi,

I'm new to writing CFD code in Fortran. I saw two different styles in writing code in Fortran lately and have been wondering what's the pros and cons of both.

One of them uses global variables substantially, and when a subroutine is called, there's normally no parameters pass; the other uses local variables, and every time a subroutine is called, a long list of parameters are passed, which is giving me trouble in easily keeping track of the variables being passed and used.

Intuitively, I think the 1st approach is better, apparently more readable. I wonder what's your opinion on this, and any suggestion on which style I should adopt when I write my code.

Cheers,
SX

DoHander January 25, 2011 21:38

If you want to be able to parallelize your code at a later time, you will avoid the use of global variables.

(personal opinion of course)

Do

Shenren_CN January 26, 2011 05:24

Quote:

Originally Posted by DoHander (Post 292173)
If you want to be able to parallelize your code at a later time, you will avoid the use of global variables.

(personal opinion of course)

Do


Thanks DoHander,
Could you please elaborate a little bit on this? Since I don't know anything about parallelization yet, why would global variables cause trouble for doing that?

Regards,
SX

ztdep January 31, 2011 09:53

for large cfd procedure, do not use global variable heaviley.
feel free to use class, and subroutine to exchange infromation.
look like a black box to other people

Rami February 1, 2011 10:08

Hello Shenren Xu,

Another reason to avoid global variables is they make code debugging difficult, as when some variable changes, it is difficult to trace where it was changed.
Rami

hadian February 3, 2011 00:00

though passing variables to a subroutine by its argument is more recommended by programmers ( and i like it more), but the code run faster when you use global variables. In C family languages the pointer targeting the first member of a array in passed to a function, but if FORTRAN other information is passed and this can slow down the code. Moreover, the memory of the system should be considered too. when you use local variables, you will use extra memory for those variables when the sub-program is running.

Mich February 4, 2011 07:57

Arrays and variables which don't change in "main loop", it is possible to make global. For example, grid nodes.


All times are GMT -4. The time now is 02:36.