dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #12087
Re: Image to Function data structure conversion
>>
>> Johan: Why the extensions in dolfin/site-packages/function.py are not
>> implemented in c++ and then wrapped in python? It seems focusing some
>> parts of the development in the python layer has caused some stuff to
>> be missed in the c++ core. For reference I copy the previous
>> communication with Kent:
>
> I fell that I am jumping into a discussion I do not have the beginning of, but
> first of all:
>
> The thing we do in dolfin/site-packages/function.py is to be able to
> dynamically create python function classes that inherits both cpp.Function,
> i.e., the c++ version _and_ ffc.Function. Having this in place we are now
> able to define a Function like:
>
> V = FunctionSpace(UnitSquare(10,10),"CG",1)
> f = Function(V,"sin(x[0])")
>
> where f can be used both to define variational forms, and efficiently be used
> in the assemble loop, where the compiled c++ version of the function is used.
> This means that you can e.g. write
>
> a = assembe(f*dx)
>
> What we are doing in dolfin/site-packages/function.py is however probably
> _not_ causing your error, as it should still be possible to use pure
> cpp.Functions or subclasses of such, in PyDOLFIN.
>
> To assemble a form using such function you have to instantiate a ffc.Function
> and a DolfinImageFunction, where the former is used defining the form and the
> latter is passed to
>
> assemble(...,coefficients=[your_dolfin_image_function])
>
>> ----------------------------
>> There are some issues with coding dolfin under c++, a code which works
>> with python is difficult, if not impossible, to re-produce in c++. I
>> tried sub-classing
>> dolfin::Function as itk::DolfinImageFunction:
>>
>> http://code.google.com/p/wrapitk/source/browse/trunk/ExternalProjects/ItkDo
>>lfin/src/itkDolfinImageFunction.h
>>
>> After wrapping in python, using wrapped itk::DolfinImageFunction works fine
>> in python, but the same procedure fails when implemented in c++, as
>> demonstrated here:
>
> I am not quite sure I understand this, but are you saying that you are able
> to:
>
> V = FunctionSpace(UnitSquare(10,10),"Lagrange",1)
> f = DolfinImageFunction(V,someimage)
>
> but not by calling your wrapped ImageToDolfinFunction.DolfinImageFunction()?
Yes, I can call wrapped DolfinImageFunction in python with no problem,
but when using it in pure c++ (itk::ImageToDolfinFunction) an then
calling ImageToDolfinFunction after wrapped in python generated the
error:
RuntimeError: *** Error: Unable to assign to function, missing
coefficients (user-defined function).
why v._vector is not null in the first case? I also tried the shared
pointer ctors and ended up with the same result. Moreover, I tried
sub-classing FunctionSpace (see
http://code.google.com/p/wrapitk/source/browse/trunk/ExternalProjects/ItkDolfin/src/itkDolfinImageFunctionSpace.h),
with and without shared pointer ctors, again the same error.
>
> First I assume that you properly instantiate the templates somewhere else in
> your build script, as itkDolfin.swg does not define any such templates.
That's correct, wrapitk instantiates the templates automatically.
>
> Second you should probably return a pointer to the created function in
> DolfinImageFunction(), and set
>
> %newobject itk::ImageToDolfinFunction<YourImageTypes>.DolfinImageFunction;
>
> or define a copy constructor to your DolfinImageFunction, as you are returning
> by value now.
I've tried a simple copy ctor like this:
DolfinImageFunction(const DolfinImageFunction &v)
{
*this = v;
}
which does get called, but didn't help.
Just to mention that one problem with c++ dolfin classes is that their
member variables are mostly private, causing restrictions on the
sub-classes. Is there a specific reason for having private variables
instead of protected? For instance, DolfinImageFunction ctors are not
able to initialise _function_space or _vector.
>
> Hope this helps!
>
> Johan
>
>> http://code.google.com/p/wrapitk/source/browse/trunk/ExternalProjects/ItkDo
>>lfin/src/itkImageToDolfinFunction.h#56
>>
>> which generates this error in python:
>>
>> File
>> "/usr/lib/InsightToolkit/WrapITK/lib/itkImageToDolfinFunctionPython.py",
>> line 273, in Convert
>> return
>> _itkImageToDolfinFunctionPython.itkImageToDolfinFunctionID2_Convert(*args)
>> RuntimeError: *** Error: Unable to assign to function, missing
>> coefficients (user-defined function).
>>
>> Here is the python test generating the above error:
>>
>> http://code.google.com/p/wrapitk/source/browse/trunk/ExternalProjects/ItkDo
>>lfin/tests/test-01.py
>
>
>
Follow ups
References