Fortran Passing variables between subroutines in different files using Fortran

AI Thread Summary
The discussion revolves around a user modifying a Monte Carlo program (MCCCS Towhee) in Fortran and encountering issues with passing a 2x1 array between subroutines in different files, leading to a segmentation fault error. The user has verified type declarations but remains uncertain about the cause of the error, especially since they are new to Fortran. A response suggests that the user should compile the program by including all relevant files in the command line using gfortran, ensuring that the main program and subroutine files are correctly linked. It also recommends exploring Fortran modules for better organization of subroutines and mentions the importance of properly setting up the Makefile to handle file dependencies and compilation flags. The advice emphasizes using the -c flag for compiling individual files and the -o flag for creating the final executable, along with the -ffree-form option for compatibility.
SheldonM
Messages
1
Reaction score
0
Good day,

I'm modifying a Monte Carlo program (MCCCS Towhee) using Fortran and am having trouble passing an argument (a 2x1 array) from a subroutine withing one file (the existing main loop that 'drives' the code) to another subroutine written in another file (an energy calculation, also part of the existing program package). I've written another subroutine (call it 'AAA') in the main loop that receives the array, assigns it to another variable name (same type, size); then, in the other file, I call AAA to receive the array. The code compiles and runs fine until the call to the subroutine is made, and then it exits with a segmentation fault error (which is strange because I've checked and rechecked the type declarations - they seem fine). I'm a newbie to Fortran, so I'm probably not seeing a very simple solution. Since this program has a sophisticated configure and Makefile setup, and if the solution to my problem means that I must create a new *.F file, what changes (if any) must be be made to the files associated with configure and make?

Many thanks,
Sheldon
 
Technology news on Phys.org
Hi Sheldon-

I hope this answers your question since it worked for me. I run my FORTRAN code on Snow Leopard. Now I have multiple subroutines in different files that I call in my main program. The way I made it work was to do:

gfortran -o mainExec mainprogram.f file1.f file2.f ...

So mainprogram.f is obviously the main program. file1.f has some number of subroutines while file2.f has some other number of different subroutines etc. Once you then run the executable (mainExec) everything should be fine. Hope that helps.
 
When including multiple files, look up MODULES for Fortran. However, I believe you can only use them in Fortran 90 and up. Either way, they make it easier to allow the programmer to package subroutines in a common file. There are also a few different things such as interfaces which allow for even greater functionality.

As smigs said, in your Makefile, each file needs to be compiled with the correct dependencies with the -c flag, and then the final executable, linking the objects with the -o flag.
 
check with -ffree-form when compiling.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
25
Views
3K
Replies
59
Views
11K
Replies
8
Views
4K
Replies
2
Views
2K
Replies
4
Views
2K
Back
Top