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

Build mixed function space with constrained domain (periodic bc)

0 votes

Hi everybody!

I want to solve a problem using a mixed function space $V \times W$ and I want to equip $V$ with periodic boundary conditions:

from dolfin import *

# define periodic bc
class PeriodicBC(SubDomain):
      def __init__(self, tolerance=DOLFIN_EPS, length = 1.0):
          SubDomain.__init__(self)
          self.tol = tolerance
          self.length = length
      def inside(self, x, on_boundary):
          return bool((near(x[0], 0) or near(x[1], 0) and on_boundary) and \
                      (not ((near(x[0], 0) and near(x[1], self.length)) or \
                      (near(x[0], self.length) and near(x[1], 0)))) and on_boundary)
      def map(self, x, y):
          L = self.length
          if near(x[0], L) and near(x[1], L):
             y[0] = x[0] - L
             y[1] = x[1] - L
          elif near(x[0], L):
             y[0] = x[0] - L
             y[1] = x[1]
          else:  
             y[0] = x[0]
             y[1] = x[1] - L


# create mesh
mesh = RectangleMesh(Point(0.0,0.0),Point(1.0,1.0),4,4,"left/right")

# build function space
PBC = PeriodicBC(length = 1.0)
V  = FunctionSpace(mesh, "CG", 1, constrained_domain=PBC)
W  = FunctionSpace(mesh, "CG", 1)          

MS = MixedFunctionSpace([V,W])

But I get the following error:

*** Error: Unable to create function space.
*** Reason: Nonmatching constrained_domain for mixed function space: [<dolfin.cpp.mesh.SubDomain; proxy of *' at 0x7fec5a8a0210> >, None].
*** Where: This error was encountered inside functionspace.py.
*** Process: 0


*** DOLFIN version: 1.6.0
*** Git changeset: unknown

What am I doing wrong? I would be very grateful if anyone could help.

Best regards,
Lisa

asked Oct 3, 2016 by lisa FEniCS Novice (280 points)

1 Answer

0 votes

Hi, consider

# build function space 
PBC = PeriodicBC(length = 1.0)
V  = FunctionSpace(mesh, "CG", 1, constrained_domain=PBC)
W  = FunctionSpace(mesh, "CG", 1, constrained_domain=PBC)          
MS = MixedFunctionSpace([V,W])
print MS.dim()

V = FiniteElement('Lagrange', mesh.ufl_cell(), 1)
W = MixedElement([V, V])
W = FunctionSpace(mesh, W, constrained_domain=PBC)          
print W.dim()
answered Oct 4, 2016 by MiroK FEniCS Expert (80,920 points)

Hi! Thank you for your quick response.

When I run your code I get the following error:

W = FunctionSpace(mesh, W, constrained_domain=PBC)          

TypeError: init() takes at least 4 arguments (4 given)

It works for me on dolfin version 2016.1.0.

Unfortunately, it does not work on FEniCS version 1.6.0.

Can you explain what's wrong or why my code does not work? That would be great.

Hi, the code shows two approaches which should be equivalent. If it is W = FunctionSpace(mesh, W, constrained_domain=PBC) which does not work use the constructor above. Or is it that both approaches fail?

I'm sorry. Now I see that I should have explained my original problem a bit more.

So the problem is, that I can not create a mixed function space from one function space with periodic boundary conditions (V) and one function space without periodic boundary conditions (W):

MS = MixedFunctionSpace([V,W])

But the code works, if I want to create a mixed function space from two function spaces, which have both periodic boundary conditions:

MS = MixedFunctionSpace([V,V])

and it works for a mixed space consisting of two spaces without periodic bc.

Do you have an idea what's wrong?

It is by design that you cannot create such a mixed space. Out of curiosity, what is the application where you need only one variable to be periodic? Also, you could have a look at cbc.block where you can assemble block matrices - mixed spaces are not necessary.

...