CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   SWIG wrappers interactive OpenFOAM (

mariam February 17, 2009 06:47

Dear all, being not too fluen
Dear all,
being not too fluent in C, I had tried to wrap the OpenFOAM functions in order to use it from python. While this helped me substantially with other simulation software, I was unable to get a working OpenFOAM wrapper. Unfortunately I failed at the very basic GeometricField classes.

I was happy to read Ivor in thread, who mentioned that he was working on such a wrapper. Can I find somewhere Ivor's wrapper or the code it is based on, for helping my OpenFOAM experience with a more user-friendly programming environment? Assuming that it is quite some work to wrap all OpenFOAM classes, I'm also interested to contribute some parts, probably related to, but not limited to turbomachinery. However, as I said, I couldn't get started on my own and I need help with the basic SWIG interface setup.


olesen February 17, 2009 06:51

Since OpenFOAM makes extensive
Since OpenFOAM makes extensive use of C++ templates, I can imagine that wrapping them with SWIG might require a fair amount of attention.

A quick google turned up this as a starting point for you:

mariam February 17, 2009 07:03

Mark, yes the templates are th
Mark, yes the templates are the cause of my problems. But apparently Ivor and Hrv have built a wrapper despite of the difficulties.
I am less asking about advice how to solve each individual problem (have worked with SWIG and templates a lot, but never had similar problems). Rather I hope I can build upon previous code.


elorriaux February 17, 2009 10:38

Hello, I've wrapped the Geo

I've wrapped the GeometricField with swig :

template<class> class PatchField, class GeoMesh>
class GeometricField{
const IOobject&,
const GeoMesh::Mesh&

%template(volScalarField) GeometricField<scalar,fvpatchfield,volmesh>;
%template(volVectorField) GeometricField<vector,fvpatchfield,volmesh>;
%template(surfaceScalarField) GeometricField<scalar,fvspatchfield,surfacemesh>;
%template(surfaceVectorField) GeometricField<vector,fvspatchfield,surfacemesh>;

It seems to work, i'm able to run icoFoam and I'm getting the same results than the original solver.

But swig is too limited, i'm thinking to switch to boost. For the moment, i'm able to run almost everything i need with swig wrappers (great difficulties with the dytnamicFvMesh class), but i fear that i will we locked with this project in the future.

Note that i'm working with OF-1.4.1-dev for this wrapping project, i've not upgraded my wrappers to 1.5-dev yet.


josp February 19, 2009 07:40

Mariam, Etienne! I'm working o
Mariam, Etienne! I'm working on some boost-python wrapping of OpenFOAM. But I have a different approach in that I have embedded an interactive python prompt into parallel runs of OpenFOAM. So OpenFOAM is still started as usual with foamJob -p solverName

It's possible to inspect the properties of (say) a scalarField at runtime and use all the great python tools like numpy/pylab to process the data.

I think boost-python is a more suitable tool but swig will probably work aswell.

I wrapped volScalarField like this:
/* Foam volScalarField */

"volScalarField", "__doc__ volScalarField",
init <const GeometricField<

Still, embedding/extending or plain extending does not really matter since the wrapping procedure is the same. I'll upload the code (very early development) somewhere as soon as I can!

Best wishes


All times are GMT -4. The time now is 05:33.