A collapsed function space lacks the python layer

Bug #855362 reported by Martin Sandve Alnæs
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
DOLFIN
Fix Released
Critical
Johan Hake

Bug Description

When trying to convert a VariationalProblem based solve
to using solve(), I created a Function for the solution,
and got the message that I should collapse the function
space (which was a subspace). So I call V.collapse(),
but the new function space is not wrapped in the python
layer. Details:

In [23]: V = VectorFunctionSpace(mesh, "CG", 1)

In [24]: U = V.sub(0)

In [25]: W = U.collapse()

In [26]: f = Function(V)

In [27]: g = Function(U)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)

/home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()

/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/function.pyc in __init__(self, *args)
    139 if len(args) == 1:
    140 # Instantiate cpp base classes

--> 141 cpp.Function.__init__(self, V)
    142 elif len(args) == 2:
    143 # If passing FunctionSpace together with cpp.Function

/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/cpp.pyc in __init__(self, *args)
  16161
  16162 """
> 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
  16164 __swig_destroy__ = _cpp.delete_Function
  16165 def assign(self, *args):

RuntimeError: *** Error: Cannot create Functions using subspaces. Consider collapsing the FunctionSpace.

In [28]: h = Function(W)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()

/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/function.pyc in __init__(self, *args)
    104
    105 if not isinstance(args[0], (FunctionSpaceBase, Function)):
--> 106 raise TypeError, "expected a FunctionSpace or a Function as argument 1"
    107
    108 # If using the copy constuctor

TypeError: expected a FunctionSpace or a Function as argument 1

In [29]: type(V)
Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>

In [30]: type(U)
Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>

In [31]: type(W)
Out[31]: <class 'dolfin.cpp.FunctionSpace'>

Changed in dolfin:
importance: Undecided → Critical
milestone: none → 1.0-beta2
Johan Hake (johan-hake)
Changed in dolfin:
status: New → Confirmed
status: Confirmed → In Progress
assignee: nobody → Johan Hake (johan-hake)
Revision history for this message
Garth Wells (garth-wells) wrote :

Would it help if 'collapse' returned a shared_ptr rather than a plain pointer, as it does now?

Revision history for this message
Johan Hake (johan-hake) wrote : Re: [Bug 855362] Re: A collapsed function space lacks the python layer

On Wednesday September 21 2011 12:08:51 Garth Wells wrote:
> Would it help if 'collapse' returned a shared_ptr rather than a plain
> pointer, as it does now?

AFAIK it already returns a shared_ptr version.

We have these kindof problems when we have built a Python class around the cpp
class. We just need to manually rewrap the returned object to the Python
class. For FunctionSpace we have a particular class to handle this situation
FunctionSpaceFromCpp.

I will soon commit a fix.

Johan

> > 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>
> 16164 __swig_destroy__ = _cpp.delete_Function
> 16165 def assign(self, *args):
>
> RuntimeError: *** Error: Cannot create Functions using subspaces.
> Consider collapsing the FunctionSpace.
>
> In [28]: h = Function(W)
>
> --------------------------------------------------------------------------
> - TypeError Traceback (most recent call
> last)
>
> /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
>
>
> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/f
> unction.pyc in __init__(self, *args) 104
> 105 if not isinstance(args[0], (FunctionSpaceBase,
> Function)): --> 106 raise TypeError, "expected a FunctionSpace
> or a Function as argument 1" 107
> 108 # If using the copy constuctor
>
>
> TypeError: expected a FunctionSpace or a Function as argument 1
>
> In [29]: type(V)
> Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>
>
> In [30]: type(U)
> Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>
>
> In [31]: type(W)
> Out[31]: <class 'dolfin.cpp.FunctionSpace'>
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions

Changed in dolfin:
status: In Progress → Fix Committed
Revision history for this message
Garth Wells (garth-wells) wrote :

On 21 September 2011 20:34, Johan Hake <email address hidden> wrote:
> On Wednesday September 21 2011 12:08:51 Garth Wells wrote:
>> Would it help if 'collapse' returned a shared_ptr rather than a plain
>> pointer, as it does now?
>
> AFAIK it already returns a shared_ptr version.
>

It's a raw pointer. I guess we could run into some memory issues.

Garth

> We have these kindof problems when we have built a Python class around the cpp
> class. We just need to manually rewrap the returned object to the Python
> class. For FunctionSpace we have a particular class to handle this situation
> FunctionSpaceFromCpp.
>
> I will soon commit a fix.
>
> Johan
>
>
>>   > 16163         _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>>
>>     16164     __swig_destroy__ = _cpp.delete_Function
>>     16165     def assign(self, *args):
>>
>>   RuntimeError: *** Error: Cannot create Functions using subspaces.
>>   Consider collapsing the FunctionSpace.
>>
>>   In [28]: h = Function(W)
>>
>> --------------------------------------------------------------------------
>> - TypeError                                 Traceback (most recent call
>> last)
>>
>>   /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
>>
>>
>> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/f
>> unction.pyc in __init__(self, *args) 104
>>       105         if not isinstance(args[0], (FunctionSpaceBase,
>> Function)): --> 106             raise TypeError, "expected a FunctionSpace
>> or a Function as argument 1" 107
>>       108         # If using the copy constuctor
>>
>>
>>   TypeError: expected a FunctionSpace or a Function as argument 1
>>
>>   In [29]: type(V)
>>   Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>
>>
>>   In [30]: type(U)
>>   Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>
>>
>>   In [31]: type(W)
>>   Out[31]: <class 'dolfin.cpp.FunctionSpace'>
>>
>> To manage notifications about this bug go to:
>> https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions
>
>
> ** Changed in: dolfin
>       Status: In Progress => Fix Committed
>
> --
> You received this bug notification because you are a member of DOLFIN
> Core Team, which is subscribed to DOLFIN.
> https://bugs.launchpad.net/bugs/855362
>
> Title:
>  A collapsed function space lacks the python layer
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions
>

--
Garth N. Wells
Department of Engineering, University of Cambridge
http://www.eng.cam.ac.uk/~gnw20

Revision history for this message
Johan Hake (johan-hake) wrote :
Download full text (4.3 KiB)

On Wednesday September 21 2011 12:48:59 Garth Wells wrote:
> On 21 September 2011 20:34, Johan Hake <email address hidden> wrote:
> > On Wednesday September 21 2011 12:08:51 Garth Wells wrote:
> >> Would it help if 'collapse' returned a shared_ptr rather than a plain
> >> pointer, as it does now?
> >
> > AFAIK it already returns a shared_ptr version.
>
> It's a raw pointer. I guess we could run into some memory issues.

We are not talking about the same collapse :)

I guess you are talking about DofMap::collapse, and this bug is about
FunctionSpace::collapse.

Using a shared_ptr version of the DofMap::collapse would make sense. However,
we do not provide a typemap for boost::unordered_set so it is not usable from
Python anyhow. But that can be fixed :)

Johan

> Garth
>
> > We have these kindof problems when we have built a Python class around
> > the cpp class. We just need to manually rewrap the returned object to
> > the Python class. For FunctionSpace we have a particular class to handle
> > this situation FunctionSpaceFromCpp.
> >
> > I will soon commit a fix.
> >
> > Johan
> >
> >> > 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
> >>
> >> 16164 __swig_destroy__ = _cpp.delete_Function
> >> 16165 def assign(self, *args):
> >>
> >> RuntimeError: *** Error: Cannot create Functions using subspaces.
> >> Consider collapsing the FunctionSpace.
> >>
> >> In [28]: h = Function(W)
> >>
> >> ------------------------------------------------------------------------
> >> -- - TypeError Traceback (most recent
> >> call last)
> >>
> >> /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
> >>
> >>
> >> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions
> >> /f unction.pyc in __init__(self, *args) 104
> >> 105 if not isinstance(args[0], (FunctionSpaceBase,
> >> Function)): --> 106 raise TypeError, "expected a
> >> FunctionSpace or a Function as argument 1" 107
> >> 108 # If using the copy constuctor
> >>
> >>
> >> TypeError: expected a FunctionSpace or a Function as argument 1
> >>
> >> In [29]: type(V)
> >> Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>
> >>
> >> In [30]: type(U)
> >> Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>
> >>
> >> In [31]: type(W)
> >> Out[31]: <class 'dolfin.cpp.FunctionSpace'>
> >>
> >> To manage notifications about this bug go to:
> >> https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions
> >
> > ** Changed in: dolfin
> > Status: In Progress => Fix Committed
> >
> > --
> > You received this bug notification because you are a member of DOLFIN
> > Core Team, which is subscribed to DOLFIN.
> > https://bugs.launchpad.net/bugs/855362
> >
> > Title:
> > A collapsed function space lacks the python layer
> >
> > To manage notifications about this bug go to:
> > https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions
> >
> > 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>
> 16164 __swig_destroy__ = _cpp.delete_Function
> 16165 ...

Read more...

Revision history for this message
Martin Sandve Alnæs (martinal) wrote :

Now Function doesn't see that a function space is collapsed, and I'm back to the original problem, not being able to create a Function from a subspace:

    V = u.function_space().sub(0)
    ...
    V2 = V.collapse()
    psi = Function(V2)

Traceback (most recent call last):
  File "./ns", line 141, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./ns", line 124, in main
    u, p = solver.solve(problem)
  File "/home/martinal/dev/cbc/nsbench/work/solvers/ipcs.py", line 108, in solve
    self.update(problem, t, u1, p1)
  File "/home/martinal/dev/cbc/nsbench/work/solvers/solverbase.py", line 75, in update
    M = problem.functional(t, u, p)
  File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line 61, in functional
    psi = StreamFunction(u)
  File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line 103, in StreamFunction
    psi = Function(V2)
  File "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/function.py", line 141, in __init__
    cpp.Function.__init__(self, V)
  File "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/cpp.py", line 16532, in __init__
    _cpp.Function_swiginit(self,_cpp.new_Function(*args))
RuntimeError: *** Error: Cannot create Functions using subspaces. Consider collapsing the FunctionSpace.

The code is from
  lp:~martinal/nsbench/cleanup
I run it with
  ./ns drivencavity ipcs

Revision history for this message
Johan Hake (johan-hake) wrote :

Then I think the bug has changed to:

  Cannont create Function from subspaces.

Johan

On Thursday September 22 2011 01:25:09 Martin Sandve Alnæs wrote:
> Now Function doesn't see that a function space is collapsed, and I'm
> back to the original problem, not being able to create a Function from a
> subspace:
>
> V = u.function_space().sub(0)
> ...
> V2 = V.collapse()
> psi = Function(V2)
>
> Traceback (most recent call last):
> File "./ns", line 141, in <module>
> sys.exit(main(sys.argv[1:]))
> File "./ns", line 124, in main
> u, p = solver.solve(problem)
> File "/home/martinal/dev/cbc/nsbench/work/solvers/ipcs.py", line 108, in
> solve self.update(problem, t, u1, p1)
> File "/home/martinal/dev/cbc/nsbench/work/solvers/solverbase.py", line
> 75, in update M = problem.functional(t, u, p)
> File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line
> 61, in functional psi = StreamFunction(u)
> File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line
> 103, in StreamFunction psi = Function(V2)
> File
> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/
> function.py", line 141, in __init__ cpp.Function.__init__(self, V)
> File
> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/cpp.py",
> line 16532, in __init__
> _cpp.Function_swiginit(self,_cpp.new_Function(*args))
> RuntimeError: *** Error: Cannot create Functions using subspaces. Consider
> collapsing the FunctionSpace.
>
>
> The code is from
> lp:~martinal/nsbench/cleanup
> I run it with
> ./ns drivencavity ipcs
>
> > 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>
> 16164 __swig_destroy__ = _cpp.delete_Function
> 16165 def assign(self, *args):
>
> RuntimeError: *** Error: Cannot create Functions using subspaces.
> Consider collapsing the FunctionSpace.
>
> In [28]: h = Function(W)
>
> --------------------------------------------------------------------------
> - TypeError Traceback (most recent call
> last)
>
> /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
>
>
> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/f
> unction.pyc in __init__(self, *args) 104
> 105 if not isinstance(args[0], (FunctionSpaceBase,
> Function)): --> 106 raise TypeError, "expected a FunctionSpace
> or a Function as argument 1" 107
> 108 # If using the copy constuctor
>
>
> TypeError: expected a FunctionSpace or a Function as argument 1
>
> In [29]: type(V)
> Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>
>
> In [30]: type(U)
> Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>
>
> In [31]: type(W)
> Out[31]: <class 'dolfin.cpp.FunctionSpace'>
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/dolfin/+bug/855362/+subscriptions

Revision history for this message
Martin Sandve Alnæs (martinal) wrote :
Download full text (6.1 KiB)

On 22 September 2011 17:02, Johan Hake <email address hidden> wrote:
> Then I think the bug has changed to:
>
>  Cannont create Function from subspaces.
>
> Johan

No, not really. The way I understand the message, is that collapse is
supposed to return something that is _not_ a subspace. So your new
python layer fix for collapse doesn't quite work.

Martin

> On Thursday September 22 2011 01:25:09 Martin Sandve Alnæs wrote:
>> Now Function doesn't see that a function space is collapsed, and I'm
>> back to the original problem, not being able to create a Function from a
>> subspace:
>>
>>     V   = u.function_space().sub(0)
>>     ...
>>     V2 = V.collapse()
>>     psi = Function(V2)
>>
>> Traceback (most recent call last):
>>   File "./ns", line 141, in <module>
>>     sys.exit(main(sys.argv[1:]))
>>   File "./ns", line 124, in main
>>     u, p = solver.solve(problem)
>>   File "/home/martinal/dev/cbc/nsbench/work/solvers/ipcs.py", line 108, in
>> solve self.update(problem, t, u1, p1)
>>   File "/home/martinal/dev/cbc/nsbench/work/solvers/solverbase.py", line
>> 75, in update M = problem.functional(t, u, p)
>>   File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line
>> 61, in functional psi = StreamFunction(u)
>>   File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py", line
>> 103, in StreamFunction psi = Function(V2)
>>   File
>> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/
>> function.py", line 141, in __init__ cpp.Function.__init__(self, V)
>>   File
>> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/cpp.py",
>> line 16532, in __init__
>> _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>> RuntimeError: *** Error: Cannot create Functions using subspaces. Consider
>> collapsing the FunctionSpace.
>>
>>
>> The code is from
>>   lp:~martinal/nsbench/cleanup
>> I run it with
>>   ./ns drivencavity ipcs
>>
>>   > 16163         _cpp.Function_swiginit(self,_cpp.new_Function(*args))
>>
>>     16164     __swig_destroy__ = _cpp.delete_Function
>>     16165     def assign(self, *args):
>>
>>   RuntimeError: *** Error: Cannot create Functions using subspaces.
>>   Consider collapsing the FunctionSpace.
>>
>>   In [28]: h = Function(W)
>>
>> --------------------------------------------------------------------------
>> - TypeError                                 Traceback (most recent call
>> last)
>>
>>   /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
>>
>>
>> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions/f
>> unction.pyc in __init__(self, *args) 104
>>       105         if not isinstance(args[0], (FunctionSpaceBase,
>> Function)): --> 106             raise TypeError, "expected a FunctionSpace
>> or a Function as argument 1" 107
>>       108         # If using the copy constuctor
>>
>>
>>   TypeError: expected a FunctionSpace or a Function as argument 1
>>
>>   In [29]: type(V)
>>   Out[29]: <class 'dolfin.functions.functionspace.VectorFunctionSpace'>
>>
>>   In [30]: type(U)
>>   Out[30]: <class 'dolfin.functions.functionspace.FunctionSpaceFromCpp'>
>>
>>   In [31]: type(W)
>>   Out[31]: <class 'dolfin.cpp.Function...

Read more...

Revision history for this message
Johan Hake (johan-hake) wrote :
Download full text (7.9 KiB)

On Thursday September 22 2011 13:54:59 Martin Sandve Alnæs wrote:
> On 22 September 2011 17:02, Johan Hake <email address hidden> wrote:
> > Then I think the bug has changed to:
> >
> > Cannont create Function from subspaces.
> >
> > Johan
>
> No, not really. The way I understand the message, is that collapse is
> supposed to return something that is _not_ a subspace. So your new
> python layer fix for collapse doesn't quite work.

Well, _now_ it should be fixed... A small bug was introduced and hidden by an
erronious unit test :P

Johan

> Martin
>
> > On Thursday September 22 2011 01:25:09 Martin Sandve Alnæs wrote:
> >> Now Function doesn't see that a function space is collapsed, and I'm
> >> back to the original problem, not being able to create a Function from a
> >> subspace:
> >>
> >> V = u.function_space().sub(0)
> >> ...
> >> V2 = V.collapse()
> >> psi = Function(V2)
> >>
> >> Traceback (most recent call last):
> >> File "./ns", line 141, in <module>
> >> sys.exit(main(sys.argv[1:]))
> >> File "./ns", line 124, in main
> >> u, p = solver.solve(problem)
> >> File "/home/martinal/dev/cbc/nsbench/work/solvers/ipcs.py", line 108,
> >> in solve self.update(problem, t, u1, p1)
> >> File "/home/martinal/dev/cbc/nsbench/work/solvers/solverbase.py", line
> >> 75, in update M = problem.functional(t, u, p)
> >> File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py",
> >> line 61, in functional psi = StreamFunction(u)
> >> File "/home/martinal/dev/cbc/nsbench/work/problems/drivencavity.py",
> >> line 103, in StreamFunction psi = Function(V2)
> >> File
> >> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/function
> >> s/ function.py", line 141, in __init__ cpp.Function.__init__(self, V)
> >> File
> >> "/opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/cpp.py",
> >> line 16532, in __init__
> >> _cpp.Function_swiginit(self,_cpp.new_Function(*args))
> >> RuntimeError: *** Error: Cannot create Functions using subspaces.
> >> Consider collapsing the FunctionSpace.
> >>
> >>
> >> The code is from
> >> lp:~martinal/nsbench/cleanup
> >> I run it with
> >> ./ns drivencavity ipcs
> >>
> >> > 16163 _cpp.Function_swiginit(self,_cpp.new_Function(*args))
> >>
> >> 16164 __swig_destroy__ = _cpp.delete_Function
> >> 16165 def assign(self, *args):
> >>
> >> RuntimeError: *** Error: Cannot create Functions using subspaces.
> >> Consider collapsing the FunctionSpace.
> >>
> >> In [28]: h = Function(W)
> >>
> >> ------------------------------------------------------------------------
> >> -- - TypeError Traceback (most recent
> >> call last)
> >>
> >> /home/martinal/dev/cbc/nsbench/work/<ipython console> in <module>()
> >>
> >>
> >> /opt/fenics/dorsal-unstable/lib/python2.7/site-packages/dolfin/functions
> >> /f unction.pyc in __init__(self, *args) 104
> >> 105 if not isinstance(args[0], (FunctionSpaceBase,
> >> Function)): --> 106 raise TypeError, "expected a
> >> FunctionSpace or a Function as argument 1" 107
> >> 108 # If using the copy constuctor
> >>...

Read more...

Changed in dolfin:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.