How to use dolfinx::fem::Expression

Hello, I want to ask a question about the dolfinx::fem::Expression. I have read the demo
But this expression is much more complicated than I need. Basically, I only need g = f’, where f and g are 1D functions, so I create my expression as follows

phi = Coefficient(V)
E1 = -phi.dx(0)
expressions = [(E1)]

But get the error message:

weights = numpy.array([1.0] * points.shape[0])
IndexError: tuple index out of range

So I’m wondering what is the problem about it? Do I have to multiply it with a test function?
Thanks!

For expressions you need to provide the points on the reference element where you want to evaluate the expression (usually the interpolation points of another function space).

Another option is to choose your own points, as done in:

1 Like

Ok, so I guess, [0.25 0.25 0,25] is the interpolate point? Then it seems that they are only interpolate at one single point.

I tried with:

coord_element = create_vector_element("Lagrange", cell, 3)
mesh = Mesh(coord_element)
expressions = [(E1, mesh)]

but still not help, is it some issue related the shape of vector, should I do some resize?

Thanks!

This depends on what function space you want to interpolate the expression into. As you are working with an interval, do you want to evaluate the derivative at the midpoint of the cell?

Or are you using expression for something else than interpolation, like evaluating the derivative in a subset of cells at some coordinates?

yes, so I have a function defined in the continuous Lagrange(1) space, and I want to obtain its derivative, which is a function in DG(0)

Then you should send in the point [[0.5]]] which is the Interpolation point of DG-0 on an interval.

You can also access it through

Sorry, I’m a little bit confused about the number “0.5”, is it a local coordinates? Since I have several sub intervals, and it is not necessarily a unit interval.

0.5 is the coordinate in the reference element yes.

I would strongly suggest reading the documentation:
https://docs.fenicsproject.org/dolfinx/v0.7.3/cpp/doxygen/d2/db9/classdolfinx_1_1fem_1_1Expression.html#details

1 Like