dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #09323
Re: Arrays and pyDOLFIN
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.
--
Martin
Follow ups
References