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

Periodic BCs

0 votes

Dear all,

I was trying to implement periodic boundary conditions on both the sides of a UnitSquareMesh. This is my code:

# Sub domain for Periodic boundary condition
class PeriodicBoundaryLR(SubDomain):
   # Left boundary is "target domain" G
   def inside(self, x, on_boundary):
       return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)
   # Map right boundary (H) to left boundary (G)
     def map(self, x, y):
        y[0] = x[0] - 1.0
        y[1] = x[1]

 # Sub domain for Periodic boundary condition
 class PeriodicBoundaryBT(SubDomain):
 # Bottom boundary is "target domain" G
    def inside(self, x, on_boundary):
       return bool(x[1] < DOLFIN_EPS and x[1] > -DOLFIN_EPS and on_boundary)
 # Map bottom boundary (H) to top boundary (G)
    def map(self, x, y):
       y[0] = x[0]
       y[1] = x[1] - 1.0

As you see, I replicated the demo of the Poisson problem. However, I have the following error:

*** Error:   Unable to create function space.
*** Reason:  Illegal argument, not a subdomain: [<__main__.PeriodicBoundaryLR; proxy of ...      

Any advice would be really appreciated :)
Thanks,
Pietro

asked Apr 7, 2014 by piemasky FEniCS Novice (160 points)

You need to post complete code, and take of the indentation level.

1 Answer

0 votes

Thank you for your reply; here is my code,

from dolfin import *
# Sub domain for Periodic boundary condition
class PeriodicBoundaryLR(SubDomain):

    # Left boundary is "target domain" G
    def inside(self, x, on_boundary):
          return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)

    # Map right boundary (H) to left boundary (G)
    def map(self, x, y):
          y[0] = x[0] - 1.0
          y[1] = x[1]

# Sub domain for Periodic boundary condition
class PeriodicBoundaryBT(SubDomain):

    # Bottom boundary is "target domain" G
    def inside(self, x, on_boundary):
          return bool(x[1] < DOLFIN_EPS and x[1] > -DOLFIN_EPS and on_boundary)

    # Map bottom boundary (H) to top boundary (G)
    def map(self, x, y):
          y[0] = x[0]
          y[1] = x[1] - 1.0

# Mesh
nx = ny = 100
mesh = UnitSquareMesh(nx,ny)

# Periodic BCs
pbcLR = PeriodicBoundaryLR()
pbcBT = PeriodicBoundaryBT()

bcs = [pbcLR,pbcBT]

V = FunctionSpace(mesh,'Lagrange',1,constrained_domain=bcs)

(I have not written yet the variational problem)
Thank you!
Pietro

answered Apr 8, 2014 by piemasky FEniCS Novice (160 points)

Dear all, I followed the procedure in

http://fenicsproject.org/qa/262/possible-specify-more-than-one-periodic-boundary-condition

I had seen that link before but I had completely misunderstood the reply.
Anyway, now the code works, thank you!!!

Pietro

...