Getting values from UFL operators

Asked by Robert Crim

I'm new to the FEnICS package, so hopefully this isn't too far off base, but there are cases where I just want to compute something from a simple UFL operation (like sqrt, dot) without assembly.

Here is a sample program that probably explains what I'm trying to do most simple:

from dolfin import *

mesh = Interval(100, 0, 2*pi)
V = FunctionSpace(mesh, 'CG', 1)
fexp = Expression("sin(x[0])")
f = interpolate(fexp, V)

a = dot(f,f)

print a

I can't get dolfin to give me the value, I just get something that's useful in an assemble() call, like assemble(a*dx).. But I just want to get value of the dot product.

There are places where I'm trying to verify things, but I'm stuck with things Sum(...), Dot(...), Sqrt(...), and I'd really like to be able to perform the computation described and inspect the values.

Thanks a lot.

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Marie Rognes (meg-simula) said :
#1

In short, I don't think that you can.

When you say you want "to get the value of the dot product", what do you expect to get? With f as a finite element function in a space V, dot(f, f) would be a function in some other space W. However, UFL is a language for working with forms, and so the "value" in the UFL sense is indeed Dot(...) and so on.

If you want to evaluate the function g = dot(f, f) at certain points, you can project this onto some other appropriately defined finite element space, and then evaluate the resulting function at those points.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#2

Technically, UFL has functionality for computing the value of
expressions at a point x, but that requires the user to provide
code for computing the value of coefficients and their derivatives.
Thus you could achieve this by writing some ufl/dolfin integration
code. This is really cumbersome though.

It should be possible to make this more usable in the future,
though it will always be an expensive operation.

Martin

On 7 August 2011 15:21, Marie Rognes
<email address hidden> wrote:
> Question #166754 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/166754
>
>    Status: Open => Answered
>
> Marie Rognes proposed the following answer:
>
> In short, I don't think that you can.
>
> When you say you want "to get the value of the dot product", what do you
> expect to get?  With f as a finite element function in a space V, dot(f,
> f) would be a function in some other space W.  However, UFL is a
> language for working with forms, and so the "value" in the UFL sense is
> indeed Dot(...) and so on.
>
> If you want to evaluate the function g = dot(f, f) at certain points,
> you can project this onto some other appropriately defined finite
> element space, and then evaluate the resulting function at those points.
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Neilen Marais (neilenmarais) said :
#3

Hi,

On Sun, Aug 7, 2011 at 3:21 PM, Marie Rognes
<email address hidden> wrote:
> Question #166754 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/166754
>
>    Status: Open => Answered
>
> Marie Rognes proposed the following answer:
>
> In short, I don't think that you can.
>
> When you say you want "to get the value of the dot product", what do you
> expect to get?  With f as a finite element function in a space V, dot(f,
> f) would be a function in some other space W.  However, UFL is a
> language for working with forms, and so the "value" in the UFL sense is
> indeed Dot(...) and so on.

I ran into a similar issue when trying to do a fairly complicated
postprocessing functional. I set up Expression() objects for various
vector quantities, then used dot() and cross() on that. This is before
involving any function spaces. At that point it would be nice to be
able to numerically evaluate the combination of Expression objects and
dot/cross operators to ascertain the correctness of the Expressions.
The expressions I implemented would not be exactly representable by
any standard FEM function space, which means you can't test their
exact correctness by first projecting onto a function space. I hope
this makes sense :)

This is related to my question here:
https://answers.launchpad.net/dolfin/+question/162499

Cheers
Neilen

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#4

With the latest dev versions of ufl and dolfin, you should be able to do this.

Runnable example:

from dolfin import *
mesh = UnitCube(20, 20, 20)
f = Expression(["sin(6.28*x[0])", "cos(6.28*x[1])", "0.0"])
g = Expression(["cos(6.28*x[0])", "-sin(6.28*x[1])", "0.0"])
plot(f, title='f', mesh=mesh)
plot(g, title='g', mesh=mesh)
plot(cross(f, g), title='f x g', mesh=mesh)
interactive()

Can you help with this problem?

Provide an answer of your own, or ask Robert Crim for more information if necessary.

To post a message you must log in.