This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

SubDomain with constructor

+3 votes

Hello
I am trying to define a SubDomain with some variables. So I tried to write a constructor

class OutFlow(SubDomain):
    def __init__(self,y1,y2):
        self.y1 = y1
        self.y2 = y2
    def inside(self, x, on_boundary):
        return (near(x[0],0.0) and 
               between(x[1], (self.y1, self.y2)) and
               on_boundary)

I tried to use it like this

mesh = UnitSquareMesh(n,n)

# Create mesh functions over the cell facets
sub_domains = FacetFunction("size_t", mesh)

# Mark all facets as sub domain 4, includes interior edges
sub_domains.set_all(4)
# Mark outflow
outflow = OutFlow(0.1, 0.4)
outflow.mark(sub_domains, 1)

but this gives an error

Traceback (most recent call last):
  File "grid.py", line 60, in <module>
    outflow.mark(sub_domains, 1)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.7/site-packages/dolfin/cpp/mesh.py", line 4426, in mark
    self._mark(*args)
TypeError: in method 'SubDomain__mark', argument 1 of type 'dolfin::SubDomain const *'
asked Jun 1, 2014 by praveen FEniCS User (2,760 points)

1 Answer

+1 vote
 
Best answer

Try:

class OutFlow(SubDomain):
def __init__(self,y1,y2):
    self.y1 = y1
    self.y2 = y2
    SubDomain.__init__(self) # Call base class constructor!
def inside(self, x, on_boundary):
    return (near(x[0],0.0) and 
           between(x[1], (self.y1, self.y2)) and
           on_boundary)
answered Jun 1, 2014 by johanhake FEniCS Expert (22,480 points)
selected Jun 1, 2014 by praveen
...