Problem setting DirichletBC with 'complex' condition

Hello,

I tried to select nodes for dirichlet condition as y=x and on_boundary with the following code:

from fenics import *

mesh = UnitSquareMesh(2,2)
V = FunctionSpace(mesh , 'P', 1)

def f(x, on_boundary):
    return near(x[1], x[0]) and on_boundary


dir = DirichletBC(V, 0, f)
print(dir.get_boundary_values())
# Output: {}

But I was not able to mark the nodes even if the method in DirichletBC is change to "pointwise".
I would have expect that the top-right and low-left nodes are returned.

Anyone know what is happening ?

Thanks.

When choosing ‘pointwise’, on_boundary is always False, i.e. your function f always returns False. To mark all dofs with x = y you can try

def f(x):
    return near(x[1], x[0])

bc = DirichletBC(V, Constant(0.0), f, method='pointwise')

Ok thank you !
But i don’t want the node at the center of the square (0.5, 0.5) to be constrain.
Is it possible ? Using near(x[1], x[0]) ?

def f(x):
    return near(x[1], x[0]) and (near(x[0],0) or near(x[0],1))

I don’t really understand why such condition with the near(x[1], x[0]) and on_boundary is not working.
Thank you for your reply !