← Back to team overview

dolfin team mailing list archive

Re: Arrays and pyDOLFIN

 



Martin Sandve Alnæs wrote:
2008/8/23 Garth N. Wells <gnw20@xxxxxxxxx>:

Martin Sandve Alnæs wrote:
2008/8/23 Garth N. Wells <gnw20@xxxxxxxxx>:
Martin Sandve Alnæs wrote:
2008/8/23 Garth N. Wells <gnw20@xxxxxxxxx>:
Martin Sandve Alnæs wrote:
Add an instantiation of Array<DirichletBC*> to the .i files beside
ArrayFunctionPtr.
I added this.

An use cpp_DirichletBC.
I tried this is demo/pde/sym-dirichlet-bc/python/demo.py, but I get

Traceback (most recent call last):
 File "demo.py", line 55, in <module>
 bc = cpp_DirichletBC(u0, mesh, boundary)
NameError: name 'cpp_DirichletBC' is not defined
from dolfin.dolfin import cpp_DirichletBC

Note that cpp_DirichletBC won't hold on to python referenced objects,
so you need to keep some python references to your meshfunctions
and vectors etc. to avoid garbage collection.

As you say, this is a problem with this
"inheritance pattern".

Is there a way to 'up cast' Python objects. For example, DirichletBC is
derived from cpp_DirichletBC, so can I get a cast a DirichletBC object
to
a
cpp_DirichletBC object?

Garth
The DirichletBC object is already a cpp_DirichletBC object, it obviously
works fine for single objects, so the problem is in the vector typemap.
Maybe you can create an empty ArrayDirichletBC() and add one item
at a time to it?

a = ArrayDirichletBC()
for b in bcs:
  a.push_back(b)

No luck. I've pushed some code, but it doesn't work. Try running

 demo/pde/sym-dirichlet-bc/python/demo.py

Garth
I'll try it. But this should work, we do the exact same thing with
functions in assemble.py.

   _coefficients = ArrayFunctionPtr()
...
           for c in form_data.coefficients:
               _coefficients.push_back(c.f)

form_data is from ffc.jit, so c.f is a dolfin.Function, the kind that
inherits ffc.Function as well as cpp_Function.

I included Array.h in assemble.cpp and that did the trick (I've since moved
the #include to Assemble.h). I have no idea why this makes it work.

Garth

Are you sure it wasn't the change I checked in?
SWIG doesn't look at the .cpp files.


Yes. Have a go.

Garth

I instantiated std::vector<...> in addition to the
dolfin::Array<...>line you added first.
Otherwise SWIG wouldn't know the details of the base class for dolfin::Array.

--
Martin




References