Call Fortran subroutines by Python Using C++ interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanhaner
    New Member
    • Mar 2007
    • 4

    #1

    Call Fortran subroutines by Python Using C++ interface

    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
    Code:
    #!/usr/bin/python
    print AddDouble(1.0, 2.0)
    ----------------------------------------------------------------------------
    com.f
    SubRoutine RealAdd(a,b,c)
    Implicit None
    Real *8 a, b, c
    c = a+ b
    Return
    End 
    ------------------------------------------------------------------------------
    fort.h 
    #define FORT(x) x##_
    -------------------------------------------------------------------------------
    fns.h
    #include "fort.h"
    #ifdef __cplusplus
    extern "C" {
    #endif
         void FORT(realadd)(double *a, double *b, double *c)
    #ifdef __ cplussplus
    }
    #endif 
    
    ----------------------------------------------------------------------------------
    extension.c
    #include <Python.h>
    #include "fort.h"
    #include "fns.h"
    
    static PyObject *Add(PyObject *self, PyObject *args)
     {
         double a = 0.0;
         double b = 0.0;
         double c = -1.0e+23;
         if(!PyArg_ParseTuple(args, "dd", &a, &b))
             return 0;
         FORT(realadd)(&a, &b, &c);
         return Py_BuildValue("d", c);
      }
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by hanhaner
    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
    Code:
    #!/usr/bin/python
    print AddDouble(1.0, 2.0)
    ----------------------------------------------------------------------------
    com.f
    SubRoutine RealAdd(a,b,c)
    Implicit None
    Real *8 a, b, c
    c = a+ b
    Return
    End 
    ------------------------------------------------------------------------------
    fort.h 
    #define FORT(x) x##_
    -------------------------------------------------------------------------------
    fns.h
    #include "fort.h"
    #ifdef __cplusplus
    extern "C" {
    #endif
         void FORT(realadd)(double *a, double *b, double *c)
    #ifdef __ cplussplus
    }
    #endif 
    
    ----------------------------------------------------------------------------------
    extension.c
    #include <Python.h>
    #include "fort.h"
    #include "fns.h"
    
    static PyObject *Add(PyObject *self, PyObject *args)
     {
         double a = 0.0;
         double b = 0.0;
         double c = -1.0e+23;
         if(!PyArg_ParseTuple(args, "dd", &a, &b))
             return 0;
         FORT(realadd)(&a, &b, &c);
         return Py_BuildValue("d", c);
      }
    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.

    Comment

    Working...