Evalute derivative of a function

Asked by Melanie Jahny

I've got a function defined on a 2D array. I compute the spacial derivative of this function
by using the "dx(i)" function from FEniCS. It works.
But I further have to evalute the derivative function on a certain mesh point. How can I do that? I tried the
"eval" function but I did not succeed. I'm not sure if "eval" is the right function here.

Here is an example

mesh = Rectangle(0,0,1,1,10,10)
V = FunctionSpace(mesh, "CG", 2)
c = Function(V)

deriv_c = c.dx

# I like to get "deriv_c" at mesh point (0.1,0.1)

cord = [0.1, 0.1]
deriv_c.eval(values,cord) # does not work

Thanks for any help!

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Blechta
Solved:
Last query:
Last reply:
Revision history for this message
Jan Blechta (blechta) said :
#1

c.dx is UFL expression - so you need to project it to some Function (DG1 would be appropriate here) and use eval on it.
  Vd = FunctionSpace(mesh, "DG", 1)
  deriv_c = project(c.dx, Vd)

Second option is to use ufl.expr.Expr.evaluate() which, I believe, enables you to calculate values of derivatives of UFL expressions. But it is intended only for some small investigation or debugging, not for assembling forms.

Jan

Revision history for this message
Melanie Jahny (melanie-jahny) said :
#2

Thanks you, Jan, for your helpful advices. The projection works well.
But unfortunately I did not succeed in applying the eval function.
I don't understand which arguments it needs, if the coordinates
where I want to evaluate deriv_c are (0.5, 0.1) for example.
I tried:

mesh = Rectangle(0,0,1,1,10,10)
V = FunctionSpace(mesh, "CG", 2)
c = Function(V)
Vd = FunctionSpace(mesh, "DG", 1)
deriv_c = project(c.dx(0), Vd)

cords = np.array([0.5, 0.1])
values = np.array([])
deriv_c.eval(values, cords) # I don't understand the first argument

Revision history for this message
Best Jan Blechta (blechta) said :
#3

On Thu, 21 Mar 2013 16:56:08 -0000
Melanie Jahny <email address hidden> wrote:
> Question #224828 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224828
>
> Status: Answered => Open
>
> Melanie Jahny is still having a problem:
> Thanks you, Jan, for your helpful advices. The projection works well.
> But unfortunately I did not succeed in applying the eval function.
> I don't understand which arguments it needs, if the coordinates
> where I want to evaluate deriv_c are (0.5, 0.1) for example.
> I tried:
>
> mesh = Rectangle(0,0,1,1,10,10)
> V = FunctionSpace(mesh, "CG", 2)
> c = Function(V)
> Vd = FunctionSpace(mesh, "DG", 1)
> deriv_c = project(c.dx(0), Vd)
>
> cords = np.array([0.5, 0.1])
> values = np.array([])
> deriv_c.eval(values, cords) # I don't understand the first argument
>

values shape needs to correspond with deriv_c value shape:
  values = np.array([0.0])

Revision history for this message
Melanie Jahny (melanie-jahny) said :
#4

Thanks a lot, it works!

Revision history for this message
Johan Hake (johan-hake) said :
#5

On 03/21/2013 05:56 PM, Melanie Jahny wrote:
> Question #224828 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224828
>
> Status: Answered => Open
>
> Melanie Jahny is still having a problem:
> Thanks you, Jan, for your helpful advices. The projection works well.
> But unfortunately I did not succeed in applying the eval function.
> I don't understand which arguments it needs, if the coordinates
> where I want to evaluate deriv_c are (0.5, 0.1) for example.
> I tried:
>
> mesh = Rectangle(0,0,1,1,10,10)
> V = FunctionSpace(mesh, "CG", 2)
> c = Function(V)
> Vd = FunctionSpace(mesh, "DG", 1)
> deriv_c = project(c.dx(0), Vd)
>
> cords = np.array([0.5, 0.1])
> values = np.array([])
> deriv_c.eval(values, cords) # I don't understand the first argument

Try:

deriv_c(0.5, 0.1)

Johan

Revision history for this message
Melanie Jahny (melanie-jahny) said :
#6

That also works, thank you!
I tried that in my first tests, but without the projection
onto V_d I always got errors. But it is really an easy solution.
thank you.