Create indicator function for boundary segment

Hello!

I want to create an indicator function that is 1 on some chosen boundary segment and 0 elsewhere.
For this purpose, I have attempted to use a GridFunction defined using the following code:

from netgen.geom2d import unit_square
import ngsolve as ng
from ngsolve.webgui import Draw

domain = unit_square
mesh = ng.Mesh(domain.GenerateMesh(maxh=1./32))
fes = ng.H1(mesh, order=1, dirichlet=“top|bottom|left|right”)
v = ng.GridFunction(fes)
v.Set(1, definedon=mesh.Boundaries(“top”))
Draw(v, mesh)

I have two one questions regarding the output of this code (see link at bottom).

  1. Why does the non-zero region extend so far into the domain interior? I would assume that it is at least possible for the elements not touching the boundary to be all zero.
  2. Is it possible to force v to be zero on the entirety of the left boundary?

Any help would be much appreciated!

Best regards,
Sindre

[s]Link to image: https://github.com/blakseth/images/blob/main/indicator.PNG[/s]
Link to new image: images/indicator2.PNG at main · blakseth/images · GitHub

you don’t define fes here - maybe you have defined it somewhere else, on a coarse mesh ?

one on top, zero on the left, and continuous is not possible alltogether.

Joachim

Thank you for the reply, and my apologies for the error in the MWE! The MWE has been fixed and I have uploaded a new image showing the new output.
It seems your suspicion was correct.

So is it correct then that the answer to question two is “no” because the Grid Function must be continuous?

Best,
Sindre

You can use a FacetFESpace(mesh, order=0) instead of the H1.
Then you have a independent constant (polynomial of order 0) for every edge.

It allows 1 on top and 0 on the left.

That works, thank you!
Best,
Sindre