← Back to team overview

dolfin team mailing list archive

PyDOLFIN progress

 

Hi everyone,

I have implemented a new tool called (for now) dolfin-swig which I
think is the final piece which now allows full development of
PDE solvers in PyDOLFIN. What dolfin-swig does is automatically
convert a (FFC-compiled) form into a Python module (using SWIG). This
makes it possible to develop PDE solvers in Python without having to
touch the main source tree of DOLFIN.

I will make an example using some existing forms. We can copy the
Poisson3D, Elasticity and Stokes3D forms to some directory where we
are doing PyDOLFIN development.

We compile the forms with FFC (the dolfin-swig format is just the
dolfin format with un-nested classes, to help SWIG):

ffc -l dolfin-swig Poisson3D.form
ffc -l dolfin-swig Elasticity.form
ffc -l dolfin-swig Stokes3D.form

We use dolfin-swig to turn the forms into Python modules:

dolfin-swig Poisson3D.h
dolfin-swig Elasticity.h
dolfin-swig Stokes3D.h

We now have 3 new Python modules called: poisson3dform, elasticityform
and stokes3dform.

We can now use them with PyDOLFIN:

python

>>>
>>> from dolfin import *
>>> mesh2D = UnitSquare(1, 1)
>>> mesh3D = UnitCube(1, 1, 1)
>>>
>>> import poisson3dform
>>> import elasticityform
>>> import stokes3dform
>>>
>>> apoisson = poisson3dform.Poisson3DBilinearForm()
>>> Apoisson = Matrix()
>>> FEM_assemble(apoisson, Apoisson, mesh3D)
>>>
>>> aelasticity = elasticityform.ElasticityBilinearForm(1.0, 1.0)
>>> Aelasticity = Matrix()
>>> FEM_assemble(aelasticity, Aelasticity, mesh3D)
>>>
>>> astokes = stokes3dform.Stokes3DBilinearForm()
>>> Astokes = Matrix()
>>> FEM_assemble(astokes, Astokes, mesh3D)
>>>

Then do something with the matrices.

As you can see, as long as we have a proper DOLFIN installation, we
don't have to touch the DOLFIN source tree, we are able to work only
with FFC and PyDOLFIN. And we can quickly and easily test forms, try
new forms, make test cases etc.


One extension of this concept is to provide a general dolfin-swig
(right now it only works for forms) which would allow any DOLFIN-based
code to be converted into a Python module. Then it would be possible
to write a small piece of C++ code implementing a coefficient function
or a boundary condition, convert it into a Python module using a
simple tool like this, and use it in a PDE solver in PyDOLFIN. This
would enable PDE solvers in PyDOLFIN to be exactly as efficient as
modules developed in pure C++.


I thought this would also allow run-time modification of forms. What
we should be able to do is:

>>> apoisson = poisson3dform.Poisson3DBilinearForm()
>>> Apoisson = Matrix()
>>> FEM_assemble(apoisson, Apoisson, mesh3D)
>>>

Then we discover some bug in the form perhaps, some element in the
matrix seems wrong. So we modify it (in some other window) and
recompile the form:

ffc -l dolfin-swig Poisson3D.form
dolfin-swig Poisson3D.h

And reload the module:

>>> reload(poisson3dform)

This works, but it seems the signatures of the objects in the new
module don't match the signatures in the old module, so for some
reason the reloaded module isn't compatible with the PyDOLFIN module
anymore (so we can't assemble the form). I'll try to find out what the
problem is, hopefully this is just some issue in SWIG that can be
fixed.

It's not so important though. We can just restart Python and run our
program again, I would say this is usually how development is done
anyway. But it can be important in the future, perhaps when a GUI is
wrapped around everything, then we probably don't want to break
run-time.


I will experiment and test PyDOLFIN with this new tool over the next
few weeks, and then hopefully it can be established as a real
(practical, efficient) environment for doing PDE solver development
in.

  Johan



Follow ups