FunctionSpace should take a reference to its constrained_domain argument

Issue #71 resolved
Patrick Farrell created an issue

I think the constrained_domain argument to FunctionSpace is passed straight into C++ without Python taking a reference: this makes usage like

V = FunctionSpace(mesh, "CG", 1, constrained_domain=MyDomain())

dangerous, because the garbage collector will remove the instance of MyDomain().

To reproduce, apply the following patch:

diff --git a/demo/undocumented/periodic/python/demo_periodic.py b/demo/undocumented/periodic/python/demo_periodic.py
index bab6b55..ddc906d 100644
--- a/demo/undocumented/periodic/python/demo_periodic.py
+++ b/demo/undocumented/periodic/python/demo_periodic.py
@@ -60,7 +60,8 @@ pbc = PeriodicBoundary()

 # Create mesh and finite element
 mesh = UnitSquareMesh(32, 32)
-V = FunctionSpace(mesh, "CG", 1, constrained_domain=pbc)
+V = FunctionSpace(mesh, "CG", 1, constrained_domain=PeriodicBoundary())
+Z = MixedFunctionSpace([V, V])


 # Create Dirichlet boundary condition

This errors with:

Traceback (most recent call last):
  File "demo_periodic.py", line 63, in <module>
    Z = MixedFunctionSpace([V, V])
  File "/usr/lib/python2.7/dist-packages/dolfin/functions/functionspace.py", line 492, in __init__
    FunctionSpaceBase.__init__(self, spaces[0].mesh(), element, constrained_domain=spaces[0].dofmap().constrained_domain)
  File "/usr/lib/python2.7/dist-packages/dolfin/functions/functionspace.py", line 89, in __init__
    dolfin_dofmap  = cpp.DofMap(ufc_dofmap, mesh, constrained_domain)
  File "/usr/lib/python2.7/dist-packages/dolfin/cpp/fem.py", line 682, in __init__
    _fem.DofMap_swiginit(self,_fem.new_DofMap(*args))
AttributeError: 'MixedFunctionSpace' object has no attribute 'inside'

Passing constrained_domain=pbc instead makes the creation of the MixedFunctionSpace work.

Comments (11)

  1. Prof Garth Wells

    I have a FIXME on this in some local code. I was wondering how long it would take someone to hit this problem!

  2. Prof Garth Wells

    I can't reproduce this locally. The problem may have been fixed in SWIG (testing with 2.0.10).

    Have pushed code to buildbots to test.

  3. Matthias Liertzer

    I just hit the exact same bug. The problem, as already stated in the original report, only appears if a MixedFunctionSpace is constructed from the constrained function spaces.

    The following code will still fail:

    from dolfin import *
    
    mesh = UnitIntervalMesh(50)
    
    class PeriodicBoundary(SubDomain):
        def inside(self, x, on_boundary):
            return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)
        def map(self, x, y):
            y[0] = x[0] - 1.0
    
    V=FunctionSpace(mesh, "CG", 1, constrained_domain=PeriodicBoundary())
    W=MixedFunctionSpace([V,V])
    
  4. Prof Garth Wells

    Fix Issue #71.

    SWIG does not handle shared_ptr and directors properly, hence we need to somethimes store a reference to an object in the Python wrappers.

    → <<cset f6ccf5364879>>

  5. Log in to comment