473,671 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call Fortran subroutines by Python Using C++ interface

4 New Member
hey, there

I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it.

For example, in the following case:
I want to call fortran subroutine "realadd" from python , i wrote the following
codes, it seems it far from working. Is there anyone can help me out?
thanks a lot!


t1.py
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. print AddDouble(1.0, 2.0)
  3. ----------------------------------------------------------------------------
  4. com.f
  5. SubRoutine RealAdd(a,b,c)
  6. Implicit None
  7. Real *8 a, b, c
  8. c = a+ b
  9. Return
  10. End 
  11. ------------------------------------------------------------------------------
  12. fort.h 
  13. #define FORT(x) x##_
  14. -------------------------------------------------------------------------------
  15. fns.h
  16. #include "fort.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.      void FORT(realadd)(double *a, double *b, double *c)
  21. #ifdef __ cplussplus
  22. }
  23. #endif 
  24.  
  25. ----------------------------------------------------------------------------------
  26. extension.c
  27. #include <Python.h>
  28. #include "fort.h"
  29. #include "fns.h"
  30.  
  31. static PyObject *Add(PyObject *self, PyObject *args)
  32.  {
  33.      double a = 0.0;
  34.      double b = 0.0;
  35.      double c = -1.0e+23;
  36.      if(!PyArg_ParseTuple(args, "dd", &a, &b))
  37.          return 0;
  38.      FORT(realadd)(&a, &b, &c);
  39.      return Py_BuildValue("d", c);
  40.   }
Mar 29 '07 #1
1 2691
bartonc
6,596 Recognized Expert Expert
hey, there

I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it.

For example, in the following case:
I want to call fortran subroutine "realadd" from python , i wrote the following
codes, it seems it far from working. Is there anyone can help me out?
thanks a lot!


t1.py
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. print AddDouble(1.0, 2.0)
  3. ----------------------------------------------------------------------------
  4. com.f
  5. SubRoutine RealAdd(a,b,c)
  6. Implicit None
  7. Real *8 a, b, c
  8. c = a+ b
  9. Return
  10. End 
  11. ------------------------------------------------------------------------------
  12. fort.h 
  13. #define FORT(x) x##_
  14. -------------------------------------------------------------------------------
  15. fns.h
  16. #include "fort.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.      void FORT(realadd)(double *a, double *b, double *c)
  21. #ifdef __ cplussplus
  22. }
  23. #endif 
  24.  
  25. ----------------------------------------------------------------------------------
  26. extension.c
  27. #include <Python.h>
  28. #include "fort.h"
  29. #include "fns.h"
  30.  
  31. static PyObject *Add(PyObject *self, PyObject *args)
  32.  {
  33.      double a = 0.0;
  34.      double b = 0.0;
  35.      double c = -1.0e+23;
  36.      if(!PyArg_ParseTuple(args, "dd", &a, &b))
  37.          return 0;
  38.      FORT(realadd)(&a, &b, &c);
  39.      return Py_BuildValue("d", c);
  40.   }
That's one way, I suppose. There are two projects (Fortran is the only language that I know of that is supported this way). One is F2Py the other is PyFort. These may help more that trying to create a Fortran interface from scratch. Have fun.
Mar 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3308
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never really had to deal with it before. I'm programming a front-end for what is a compiled Fortran program. The VB source does the following to call the Fortran: 'Defines the subroutine. Declare Sub Cycle_DW Lib "cycdw.dll" Alias "CYCDW" (ByRef...
6
3689
by: Santosh | last post by:
Hello all, I am trying to interface c++ and fortran, the fortran code is already there and I was successful in calling the subroutines and functions, but was not able to send the fortran module data into my C++ code and viceversa. I used gcc 3.3 and g95 for c++ and fortran resp. Here is a little made up sample code so that u could understand my problem. $cat fort.f90
5
2850
by: NM | last post by:
Hi All, I am having a peculiar problem. I used to link between intel fortran 7.0 and g++ without any problem. Now the intel compiler is upgraded to version 9.0 and I am getting segmenatation fault. While debugging I found when data structures are allocated in C++ and passed to fortran subroutines the fortran subroutines are accessing a different location in the address space. For example I have created a very simple program that shows the...
2
7245
by: | last post by:
Help! I'm new to c++, and am breaking my teeth on MS Visual C++ (bundled within Visual Studio .NET 2003). Am trying to link simple c++ code to fortran dlls created in Compaq Visual Fortran (v6.1). Posts concerning this topic are common, but none of the posted solutions I've tried work correctly with the above software. The linker can't seem to find the dll (reports 'unresolved external symbol __imp__IMSL_FUN@8'; IMSL_FUN.dll is the f77...
21
2754
by: Cottonwood | last post by:
I want to call a C module from a Fortran program. Whatever I tried - the linker could not find the C module. I know about the leading underscore and switched even that off. I abstracted everything possible. When I replace the C module by a Fortran subroutine linking works. Here the Fortran subroutine that replaced the C module for testing: subroutine qqcprint return end
3
5167
by: unexpected | last post by:
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...
3
2103
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 something i would prefer to do in fortran (which i have never used, but will learn), but i have no experience of accessing non-python code from python. i am also fairly new to programming period, and am therefore tackling a fairly serious issue reletive...
0
1361
by: hanhaner | last post by:
hey, there I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it. For example, in the following case: I want to call fortran subroutine "realadd" from python , i wrote the following codes, it seems it far from working. Is there anyone can help me out? thanks a lot!
8
2082
by: Luna Moon | last post by:
Hi all, As a C/C++ programmer, there are a few reasons to use Fortran: (1) Fortran is very similar to Matlab and easy to port; (2) Fortran has support of complex numbers and vectorized numbers and the operations in Fortran are naturally element-wise, operating on a whole vector. (3) There are many scientific codes are in Fortran.
0
8474
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8912
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.