Periodic Boundary class for 2 target domains

Hey there,

my domain is [0, length] x [0, length] x [0,length] cube created with mshr. I want that the opposite xz and yz planes of the domain are periodic and did the following:

class PeriodicBoundary(SubDomain):
def inside(self, y, on_boundary):
    #left and front boundary are target domain
    return bool((near(y[0],0) and on_boundary) \
            or (near(y[1],0) and on_boundary)) \
            and (not(near(y[0],length) or near(y[1],length)))

def map(self,x,y):
    #map right and back boundary to target domain
    if near(x[0], length):
        y[0] = x[0] - length
        y[1] = x[1]
        y[2] = x[2]
    elif near(x[1], length):
        y[0] = x[0]
        y[1] = x[1] - length
        y[2] = x[2]
        y[0] = -1000
        y[1] = -1000
        y[2] = -1000​

I included the else condition because I get otherwise the following error:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error:   Unable to periodic boundary mapping.
*** Reason:  Need to set coordinate 0 in
*** Where:   This error was encountered inside PeriodicBoundaryComputation.cpp.
*** Process: 0
*** DOLFIN version: 2018.1.0
*** Git changeset:  948dc42cc4e06ed9227d0201ad50f94ac94cbf9f
*** -------------------------------------------------------------------------

I found this, which was my guidance:

Why do I have to include the else condition? Even with the else condition the code doesn’t work as I want to. I tried to set one of each periodic pair to 0, but the dofs are not mapped to each other and therefore only one wall is 0. What am I missing?

I copied this from the allanswered.

Thanks in advance
Kind regards

You should first check the code with a mesh which you know is periodic (e.g. ´UnitCubeMesh´). I suspect yours is not.

Thanks MiroK for your comment. I’ve tried it with the UnitCubeMesh and indeed the periodic b.c. works but only with the else statement. Otherwise I get the very same error. Can you tell me why the else is needed?

Thanks and kind regards

Else clause is there to have the point always end up mapped somewhere, y \neq nan here.

Hi, Do you know how we can have periodic boundary conditions for aperiodic mesh?