17 February 2010

SWIG iterators and generators, continued

This is a continuation of my previous post on Python generators with SWIG. I've improved the code, added an iterator class so that the same generator method can be called multiple times concurrently, and added a couple of macros to allow easy instantiation of the SWIG code.

A SWIG file (use with the %include directive) is available for download, and further updates will be posted on my projects page.

As an example of how this would be used, see an excerpt of a 2D diffusion input file that uses my mesh library:

for c in mesh.cells():
    source[c] = max(  math.cos( c.getCenter()[0] / 2 * math.pi )
                    + math.cos( c.getCenter()[1] / 2 * math.pi ),
                  0)

UPDATE 4/25/2011: my project that uses this file is now posted on github.

0 comments:

Post a Comment