← Back to team overview

dolfin team mailing list archive

PyDolfin, extension

 

I've written some extensions to the NewMesh class in dolfin, such that the
lists of cells and vertices are exposted to Python as Numeric.arrays.

I haven't debugged the code with respect to memory leaks or other robustness
checks, so use it at your own risk :)

A simple test script would be:

from dolfin import *

us = NewUnitSqure(10,10)
print us.cells()
print us.vetices()

The next step on my agenda is to add support for the new mesh in our runtime
vtk-plotter, and issue a GPLed version to you guys.


Ola
%module(directors="1") dolfin

%feature("autodoc", "1");

%{
#include <dolfin.h>

#include "dolfin_glue.h"
#include <Numeric/arrayobject.h>

#include <string>
  
using namespace dolfin;
%}

%init%{
  import_array();
%}

%typemap(in) real = double; 
%typemap(out) real = double; 
%typemap(in) uint = int; 
%typemap(out) uint = int; 

%typemap(out) dolfin::Parameter {
  {
    // Custom typemap

    switch ( $1.type() )
    {
    case Parameter::type_real:
      
      $result = SWIG_From_double(*&($1));
      break;

    case Parameter::type_int:
      
      $result = SWIG_From_int((int)*&($1));
      break;
      
    case Parameter::type_bool:
      
      $result = SWIG_From_bool(*&($1));
      break;
      
    case Parameter::type_string:
      
      $result = SWIG_From_std_string(*&($1));
      break;
      
    default:
      dolfin_error("Unknown type for parameter.");
    }
  }
}


// Typemaps for dolfin::real array arguments in virtual methods
// probably not very safe
%typemap(directorin) dolfin::real [] {
  {
    // Custom typemap
    $input = SWIG_NewPointerObj((void *) $1_name, $1_descriptor, $owner);
  }
}

%typemap(directorin) dolfin::real const [] {
  {
    // Custom typemap
    $input = SWIG_NewPointerObj((void *) $1_name, $1_descriptor, $owner);
  }
}


%include "cpointer.i"
%include "typemaps.i"
%include "std_string.i"

%include "carrays.i"

%array_functions(dolfin::real, realArray);
%array_functions(int, intArray);

%pointer_class(int, intp);
%pointer_class(double, doublep);


//%feature("director") GenericVector;
%feature("director") Function;
%feature("director") BoundaryCondition;
%feature("director") ODE;
%feature("director") PDE;
%feature("director") TimeDependentPDE;

%ignore dolfin::dolfin_info;
%ignore dolfin::dolfin_info_aptr;


%import "dolfin.h"
%import "dolfin/constants.h"


// DOLFIN public interface 

// FIXME: Order matters, why? 

// main includes 

%include "dolfin/constants.h"
%include "dolfin/init.h"

// math includes 

%include "dolfin/basic.h"

// common includes 

%ignore dolfin::TimeDependent::TimeDependent(const real&);
%ignore dolfin::TimeDependent::sync(const real&);

%include "dolfin/Array.h"
%include "dolfin/List.h"
%include "dolfin/TimeDependent.h"
%include "dolfin/Variable.h"
%include "dolfin/utils.h"
%include "dolfin/timing.h"

%extend dolfin::TimeDependent {
  TimeDependent(double *t)
  {
    TimeDependent* td = new TimeDependent();
    td->sync(*t);
    return td;
  }
  void sync(double* t)
  {
    self->sync(*t);
  }
}

// log includes 

%include "dolfin/LoggerMacros.h"

// settings includes 

%rename(set) glueset;
%rename(get) glueget;

%include "dolfin/Parameter.h"

// io includes 

%include "dolfin/File.h"

// la includes 

%include "dolfin_la.i"

// function includes 

%rename(__call__) dolfin::Function::operator();
%rename(__getitem__) dolfin::Function::operator[];

%include "dolfin/Function.h"

// form includes

%include "dolfin/Form.h"
%include "dolfin/BilinearForm.h"
%include "dolfin/LinearForm.h"

// mesh includes

%include "dolfin_mesh.i"

// ode includes

%include "dolfin_ode.i"

// pde 

%include "dolfin/TimeDependentPDE.h"

// fem includes 

%include "dolfin/FiniteElement.h"
%include "dolfin/AffineMap.h"
%include "dolfin/BoundaryValue.h"
%include "dolfin/BoundaryCondition.h"
%include "dolfin/FEM.h"

%template(lump) dolfin::FEM::lump<Matrix, Vector>;

// glue 

%include "dolfin_glue.h"

// modules 

%include "dolfin/ElasticityUpdatedSolver.h" 

%rename(increment) dolfin::VertexIterator::operator++;
%rename(increment) dolfin::CellIterator::operator++;
%rename(increment) dolfin::EdgeIterator::operator++;

class NewMesh;

%extend dolfin::NewMesh {

PyObject * cells() {
    PyArrayObject *arr;
    int n[2];
    n[0] = self->numCells();
    n[1] = self->type().numVertices(self->dim());
    arr = (PyArrayObject *) PyArray_FromDimsAndData(2, n, PyArray_INT, (char *) self->cells());
    arr->flags |= OWN_DATA;
    Py_INCREF((PyObject *)arr);
    return (PyObject *)arr;
}

PyObject * vertices() {
    PyArrayObject *arr;
    int n[2];
    n[0] = self->numVertices();
    n[1] = self->geometry().dim();
    arr = (PyArrayObject *) PyArray_FromDimsAndData(2, n, PyArray_DOUBLE, (char *) self->vertices());
    arr->flags |= OWN_DATA;
    Py_INCREF((PyObject *)arr);
    return (PyObject *)arr;
}
};
 
%ignore dolfin::NewMesh::cells;
%ignore dolfin::NewMesh::vertices;

%include "dolfin/Mesh.h"
%include "dolfin/Boundary.h"
%include "dolfin/Point.h"
%include "dolfin/Vertex.h"
%include "dolfin/Edge.h"
%include "dolfin/Triangle.h"
%include "dolfin/Tetrahedron.h"
%include "dolfin/Cell.h"
%include "dolfin/Edge.h"
%include "dolfin/Face.h"
%include "dolfin/VertexIterator.h"
%include "dolfin/CellIterator.h"
%include "dolfin/EdgeIterator.h"
%include "dolfin/FaceIterator.h"
%include "dolfin/MeshIterator.h"
%include "dolfin/UnitSquare.h"
%include "dolfin/UnitCube.h"

%include "dolfin/MeshConnectivity.h"
%include "dolfin/MeshEditor.h"
%include "dolfin/MeshEntity.h"
%include "dolfin/MeshEntityIterator.h"
%include "dolfin/MeshGeometry.h"
%include "dolfin/MeshTopology.h"
%include "dolfin/NewMesh.h"
%include "dolfin/NewMeshData.h"
%include "dolfin/MeshFunction.h"
%include "dolfin/NewVertex.h"
%include "dolfin/NewEdge.h"
%include "dolfin/NewFace.h"
%include "dolfin/NewFacet.h"
%include "dolfin/NewCell.h"
%include "dolfin/TopologyComputation.h"
%include "dolfin/CellType.h"
%include "dolfin/Interval.h"
%include "dolfin/NewTriangle.h"
%include "dolfin/NewTetrahedron.h"
%include "dolfin/UniformMeshRefinement.h"
%include "dolfin/NewPoint.h"
%include "dolfin/BoundaryComputation.h"
%include "dolfin/BoundaryMesh.h"
%include "dolfin/NewUnitCube.h"
%include "dolfin/NewUnitSquare.h"