From: spxsjm@thor.cf.ac.uk (Mr S J Morris 92)
Newsgroups: comp.lang.fortran
Subject: Re: Data initialization: FORTRAN program called as SUBROUTINE
Date: 18 Nov 92 10:08:20 GMT
Organization: University of Wales College at Cardiff
X-Mailer: Cardiff Computing Maths PP Mail Open News Gateway

In article <1992Nov18.025912.13187@netcom.com> jchauvin@netcom.com (John H. Chauvin) writes:
|
|I am currently working on a mix language program in which a C program 
|calls a large FORTRAN program ( 15,000 lines , 100+ SUBROUTINES, 
|and extensive use of COMMON blocks). I have converted the main routine 
|from the FORTRAN into a SUBROUTINE and am calling the routine from 
|the C code. I pass file information from the C program to the FORTRAN 
|program using C data structures to FORTRAN common blocks. The 
|general program flow is: C calls FORTRAN , waits, FORTRAN updates C, 
|FORTRAN returns to C, C calls FORTRAN ,etc.. The C and FORTRAN 
|routines are linked into one large binary program. All FORTRAN variables 
|required SAVE status. The main advantage to the above approach is the 
|ease to which I can exchange data between the C (graphics front end) and 
|the FORTRAN (number crunchier).
|
|Everything works great until I try and call the FORTRAN a second time. 
|The variables and COMMON blocks still retain values from the first call. 
|(Not surprisingly) How can I reinitialize all the COMMON blocks and 
|variables to the state that existed at the beginning of the first call?  
|Normally the FORTRAN would be executed as a stand alone program 
|from the command line , loaded into memory and the variables initialized 
|each time it is used. This is the behavior I want to achieve with each call to 
|the FORTRAN. Initializing all the variables myself is out of the questions 
|because of the size of the code and the fact that all the variables have 
|been SAVEd. Is there a way to do this?  Any help or suggestions would be 
|GREATLY appreciated.
|
|
|Thanks
|
|jchauvin@netcom.com
|
|-------------------------- Configuration Information -------------------------------------
|Workstation:           Silicon Graphics Crimson Elan with 64 mb RAM
|Operating System      IRIX 4.0.5A (AT&T UNIX with Berkeley extensions)
|Development Options.....v4.1
|FORTRAN & C Compilers    v3.10
|--------------------------------------------------------------------------------------------------
|-- 
|John H. Chauvin jchauvin@netcom.COM
|Netcom - Online Communication Services San Jose, CA

One way you could do this would be to store the initial values in a
file, which could then be read at the beginning of each iteration of the
Fortran program. A flag passed from the C program could tell the Fortran
program whether it ought to initialise the variables itself or look for
them in the file, hence:

-----------------------C program-----------------------
C First iteration
         flag=0
         call [Fortran program]     {pardon my C!}

-----------------------Fortran program-----------------

         if (flag.eq.0) then
C Initialise variables
                .
                .
                .
         write (7) .....
         else
         read (7) .....
         endif

---------------------C program-------------------------
C Subsequent iterations
         flag=1
         call [Fortran prohram]

and so on...


Steve Morris,
Department of Physics, University of Wales College Cardiff.
Newsgroups: comp.lang.fortran