Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.fortran

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2012, 03:10 AM
Joćo André
Guest
 
Posts: n/a
Default Issue using common blocks in Fortran

down vote favorite
share [g+] share [fb] share [tw]


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. 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.

Thanks,

Joćo

P.S.: I've posted this question in http://stackoverflow.com but I haven't got any reply
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-08-2012, 03:34 AM
Rafik Zurob
Guest
 
Posts: n/a
Default Re: Issue using common blocks in Fortran

Hi

You forgot to declare krot in UFIELD so it was implicitly declared as
integer(*). Adding "real krot" to UFIELD should fix the problem. If this
is new code, I highly recommend adding implicit none to avoid these kinds of
problems.

Regards

Rafik
Visit the Fortran Cafe at http://ibm.com/rational/cafe/

* I'm basing my guess on the fact that the binary representations
for -2.3857285E-03 and -1155769886 are the same.

"Joćo André" <jpcgandre@gmail.com> wrote in message
news:22375789.11.1333854629187.JavaMail.geo-discussion-forums@vbbfj25...
down vote favorite
share [g+] share [fb] share [tw]


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. 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.

Thanks,

Joćo

P.S.: I've posted this question in http://stackoverflow.com but I haven't
got any reply


Reply With Quote
  #3 (permalink)  
Old 04-08-2012, 05:43 AM
Richard Maine
Guest
 
Posts: n/a
Default Re: Issue using common blocks in Fortran

Joćo André <jpcgandre@gmail.com> wrote:

> 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


Rafik gave the answer to your problem. Clearly, you didn't actually use
completely "the same syntax" in both routines - just part of the same
syntax, evidently missing the real declaration for krot.

Note also the suspicious difference between the real declaration of knom
versus the kmom in the COMMON block.

Let me just note that this illustrates yet again the reasons for posting
the whole actual code instead of excerpts and descriptions of it. The
description turned out to be wrong here, and that error was exactly the
problem. This case happened to be easy to diagnose anyway; others
aren't.

I'll second Rafik's suggestion to use implicit none. I'll go further and
suggest using f90 modules instead of common blocks. Comon blocks are a
*LOT* more error prone (particularly without implicit none). Some cases
can be awfully tricky to diagnose.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:47 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.