|
[Sponsors] |
January 25, 2011, 21:33 |
Fortran programming style
|
#1 |
Member
Shenren Xu
Join Date: Jan 2011
Location: London, U.K.
Posts: 67
Rep Power: 15 |
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 |
|
January 25, 2011, 21:38 |
|
#2 |
Senior Member
Join Date: Nov 2009
Posts: 411
Rep Power: 20 |
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 |
|
January 26, 2011, 05:24 |
|
#3 | |
Member
Shenren Xu
Join Date: Jan 2011
Location: London, U.K.
Posts: 67
Rep Power: 15 |
Quote:
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 |
||
January 31, 2011, 09:53 |
|
#4 |
Senior Member
|
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 |
|
February 1, 2011, 10:08 |
|
#5 |
Senior Member
Rami Ben-Zvi
Join Date: Mar 2009
Posts: 155
Rep Power: 17 |
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
|
|
February 3, 2011, 00:00 |
|
#6 |
Member
Mohammad Reza Hadian
Join Date: Mar 2009
Location: Yazd, Iran
Posts: 52
Rep Power: 17 |
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.
|
|
February 4, 2011, 07:57 |
|
#7 |
New Member
lpk
Join Date: Apr 2009
Posts: 2
Rep Power: 0 |
Arrays and variables which don't change in "main loop", it is possible to make global. For example, grid nodes.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
programming language for CFD, C, C++, FORTRAN,.... | hadian | Main CFD Forum | 15 | September 9, 2008 23:19 |
GUI programming with FORTRAN | ztdep | Main CFD Forum | 1 | August 30, 2007 14:22 |
A problem in FORTRAN programming | Behafarid | Main CFD Forum | 6 | August 16, 2006 13:16 |
Fortran programming and 64 bit machine | Q | Main CFD Forum | 4 | November 7, 2005 06:18 |
C++ fortran mixed programming: allocatable array | vito | Main CFD Forum | 1 | April 19, 2005 12:11 |