Defining subdomains for different materials, but with coefficients that aren't constant

Regarding this tutorial: Defining subdomains for different materials — FEniCSx tutorial

I could be missing something very obvious here, but I’ve been trying for a few days now to change the example from the tutorial in one simple way – I would like for the subdomain coefficients to not be constant but some other function.

I am importing the mesh from gmsh and xdmf and therefore have the cells tagged by their physical groups. For example, if we have just the two subdomains from the example, I want one of them to be filled with sin(x[0]), and the other with cos(x[0]). And that for every cell in the group/subdomain. Can that even be done?

I believe the answer lies in using the Expression class (dolfinx.fem — DOLFINx 0.8.0.0 documentation), since it can evaluate the expression at points in regards to cells:

def MyExpression(Expression):
    __init__(self, e, X):
       ...
    eval(self, mesh, cells, values):
       ...

Unfortunately, I cannot find any example of how it should be used, and would be extremely grateful if you could provide me with some.

To sum up, I would like to evaluate one expression (for example sin(x)) at a point if it is located inside a mesh cell tagged with one marker (located inside one subdomain/group), and the other expression (for example cos(x)) if the point is located inside a mesh cell tagged with another marker (located inside the other subdomain/group).

Two functions/expressions, two groups/subdomains, and a lot of mesh cells tagged with 0 or 1 depending on which group/subdomain they belong to.

Thank you so much in advance!

Hi, I think that we are on the same boat. Have you considered to first Read before posting: How do I get my question answered?. Good luck

You may want to consider How to split the weak form by subdomains? - #2 by edgar