How to use `CoefficientFunction` to represent a function locally defined via barycentric coordinates

I would like to define a function in NGsolve that is locally expressed in terms of barycentric coordinates.

Let Ω be a triangulated domain with a mesh T_h = ⋃ K . Suppose ( f ) is a function on Ω that is locally defined on each triangle K ∈ T_h through its barycentric coordinates (λ_1, λ_2, λ_3), i.e., for each triangle K ∈ T_h

f|_K = f(λ_1, λ_2, λ_3),

where (λ_1, λ_2, λ_3) are the barycentric coordinates of ( K ).

I wonder how to use CoefficientFunction in NGsolve to represent such a function ( f ).

Any suggestions would be greatly appreciated!

You can use discontinuous H1 to represent the barycentrics

fes = Discontinuous(H1(mesh, order=1))
lam1 = GridFunction(fes)
lam2 = GridFunction(fes)
lam3 = GridFunction(fes)

lam1.vec.FV().NumPy()[0::3] = 1
lam2.vec.FV().NumPy()[1::3] = 1
lam3.vec.FV().NumPy()[2::3] = 1

def f(l1,l2,l3): return l1*l2
Draw (f(lam1,lam2,lam3), mesh, order=3)
1 Like

Thank you very much! This method works well.

Sorry, I have a further question.

When defining a finite element space as

fes = Discontinuous(H1(mesh, order=1))

it seems that this does not work properly for a surface mesh. Could you suggest an alternative for Discontinuous(H1(mesh, order=1)) that works in the case of a surface mesh?

Here is my code:

from netgen.occ import *
from netgen import meshing
from ngsolve import *
from ngsolve.webgui import Draw

MESHSURF = meshing.MeshingStep.MESHSURFACE

sp = Sphere(Pnt(0,0,0), r=1)
mesh = Mesh(OCCGeometry(sp).GenerateMesh(maxh=0.2,perfstepsend=MESHSURF))
mesh.Curve(1);

Draw (mesh, min=0.9, max=1.1);

fes = Discontinuous(H1(mesh, order=1))
lam1 = GridFunction(fes)
lam2 = GridFunction(fes)
lam3 = GridFunction(fes)

lam1.vec.FV().NumPy()[0::3] = 1
lam2.vec.FV().NumPy()[1::3] = 1
lam3.vec.FV().NumPy()[2::3] = 1

def f(l1,l2,l3): return l1*l2
Draw (f(lam1,lam2,lam3), mesh)

Thank you in advance!