473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Integrating Python with Fortran

Hi all,

I'm currently working on a large, legacy Fortran application. I would
like to start new development in Python (as it is mainly I/O related).
In order to do so, however, the whole project needs to be able to
compile in Fortran.

I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.

I realize that all of this is highly dependent on the libraries I use,
etc, but I'm just looking for general strategies to attack the problem
or someone to tell me that this is impossible.

Oct 31 '06 #1
3 5149
unexpected schrieb:
I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.

I realize that all of this is highly dependent on the libraries I use,
etc, but I'm just looking for general strategies to attack the problem
or someone to tell me that this is impossible.
Please take a look at the "extending and embedding" tutorial. This
explains you how to integrate Python code into a C application.
If you think this could work for you if just your application was C, I
think the Python-Fortran people can give you precise instructions on how
to integrate Python code into a Fortran program.

In the simplest embedding example, you just link the Python VM itself
into the hosting application. The actual Python files stay on disk, and
invoking a Python function from C/Fortran will just end up doing a
regular Python import (with searching sys.path and everything).

If, for packaging reasons, you prefer to have the Fortran program
stand-alone, I recommend to use freeze. freeze gives you indeed .o files
for Python files, plus a global table of all frozen modules. Then, when
doing an import, the interpreter won't go to disk anymore, but it will
import the byte code "from memory" (it still would be a regular import
operation). Freeze, of course, requires you to recompile/relink your
application every time you change a Python source file.

HTH,
Martin
Oct 31 '06 #2
MC
Hi!

in afternoon: http://calcul.math.cnrs.fr/article.php3?id_article=150

--
@-salutations

Michel Claveau
Nov 1 '06 #3
unexpected wrote:
Hi all,

I'm currently working on a large, legacy Fortran application. I would
like to start new development in Python (as it is mainly I/O related).
In order to do so, however, the whole project needs to be able to
compile in Fortran.

I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.
There are no tools specifically for embedded Python in Fortran. However, there
is Elmer, which helps to create a C API for a Python library (essentially the
inverse of SWIG). One can then wrap that C API with a Fortran-compliant
interface in the fairly standard ways.

http://elmer.sourceforge.net/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael Chermside | last post by:
richardc writes: > Im no expert but wouldnt you accept that Python has 'borrowed' FORTRAN's > fixed format syntax. I can only think of Python and FORTRAN off the top of > my head which use...
44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
38
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in...
29
by: Bart Nessux | last post by:
Just fooling around this weekend. Wrote and timed programs in C, Perl and Python. Each Program counts to 1,000,000 and prints each number to the console as it counts. I was a bit surprised. I'm not...
7
by: stormslayer | last post by:
Folks: I've been considering a shift to python. I currently use c++builder (borland) or perl. I do computational models / statistics programming, and was interested in python b/c it a. has...
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
3
by: sam | last post by:
hello all, i am currently in the process of planning a piece of software to model polymerisation kinetics, and intend to use python for all the high-level stuff. the number-crunching is...
2
by: mwojc | last post by:
Hi! I released feed-forward neural network for python (ffnet) project at sourceforge. Implementation is extremelly fast (code written mostly in fortran with thin python interface, scipy optimizers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.