Question

I'm working with fortran subroutines of a finite element analysis program. I have to share variables between the two subroutines so I'm using COMMON blocks (EDIT: module is better). The problem is that only some of the variables are passed to the other subroutine, others are not.

My code is like this: First subroutine:

real knom, krot

COMMON /kVAR/ kmom, krot
SAVE /kVAR/

Second subroutine I use the same syntax. I'm controlling the results by writing kmom and krot values in each subroutine to a txt file:

  write(6,*) 'I am in URDFIL', or 'I am in UFIELD'
1 KINC, kmom, krot

The results are:

I am in URDFIL           1      -16700 -2.3857285E-03
I am in UFIELD           2      -16700 -1155769886

So the value of krot is lost. Any advise is most welcome.

João

Solved:

module shared_var
    implicit none
    real*8 kmom, krot
    save
end module shared_var

And in each subroutine:

use shared_var
Was it helpful?

Solution

Did you include the declaration of knom, krot in the second routine? Probably you are getting implicit typing and krot is being output as an integer. And it appears that you have a typo: knom versus kmom. That is why kmom is output as an integer in both cases -- implicit typing as an integer since knom is the real. If implicit typing is in effect these variables will be integers since they begin with "k". My strong recommendation is to not use implicit typing unless it is too much work to remove from legacy code. It is highly recommended to use "implicit none" so that the compiler will warn you if you forget to type a variable or make a typo in a variable name. Most compilers have options that are equivalent to "implicit none".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top