integration of scalar field; derivative with respect to scalar

Asked by R. Tavakoli

Dear All,

Consider "u" as a scalar field approximated by a picewise polynomial interpolation within FEniCS python environment.

1) Assume that functional J is defined as

J = \int u dx and v = dJ/du (derivative means gateaux derivative here), then how to compute v?

2) If we assume field v is defined on nodal values similar to u, then considering quadrature based interpolation,computation of J will something like this:

J = sum_i (c[i]*u[i]), where "i" iterates on mesh vertexes and (.)[i] is the field value at vertex i-th.

Considering this simplification,

v[i] = c[i] (i.e., v = c)

then what is the simplest way to compute vector "c" to use out of FEniCS?

Thanks

RT

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Kent-Andre Mardal
Solved:
Last query:
Last reply:
Revision history for this message
Kent-Andre Mardal (kent-and) said :
#1

On 22 March 2012 01:30, R. Tavakoli <email address hidden>wrote:

> New question #191352 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/191352
>
> Dear All,
>
> Consider "u" as a scalar field approximated by a picewise polynomial
> interpolation within FEniCS python environment.
>
> 1) Assume that functional J is defined as
>
> J = \int u dx and v = dJ/du (derivative means gateaux derivative
> here), then how to compute v?
>
>
u = Function(...)
v = TestFunction(...)
J = u*dx
dJ = derivative(J, u, v)
dj = Function(...)
solve(dJ == 0, dj)

The UFL and Dolfin chapters contain more info about what happens,
see the manual or book at http://fenicsproject.org/documentation/.

Kent

2) If we assume field v is defined on nodal values similar to u, then
> considering quadrature based interpolation,computation of J will something
> like this:
>
> J = sum_i (c[i]*u[i]), where "i" iterates on mesh vertexes and (.)[i] is
> the field value at vertex i-th.
>
> Considering this simplification,
>
> v[i] = c[i] (i.e., v = c)
>
> then what is the simplest way to compute vector "c" to use out of FEniCS?
>
> Thanks
>
> RT
>
>
>
> --
> 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
R. Tavakoli (rtav20) said :
#2

Dear Kent

thanks for your comment, it helps a lot.

In regard to part 2 of my former question, I would like to know that how we could do integration procedure explicitly without encapsulation within "assemble" function.

To do loop on elements and their vertices is illustrated in manual, however it is not clear how to loop on quadrature points, computation of a field Jacobin matrix at quadtature points?

For instance here is a part of my code for desired issue implemented in Deal II library, i'm looking for its equivalent within FEniCS environment:

  ...

for (element = 0; element!=end_element; ++element) {

  fe_values.reinit (element);

  ...

  for (i=0; i<dofs_per_cell; ++i)

    for (q_point=0; q_point<n_q_points; ++q_point)

      ... = fe_values.shape_value(i, q_point) * fe_values.JxW(q_point);

   ...
}

Thanks

RT

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

The equivalent is within the code generated by FFC. You can modify that manually, but that is painful.

But why would you want to do that? One could argue that the defining feature of FEniCS is that you do not have to work at that detail level.

Maybe if you can explain your intent, there may be a different way of doing what you want in FEniCS.

Revision history for this message
R. Tavakoli (rtav20) said :
#4

Martin,

I ask for it mainly to improve performance. I will take a look at library more carefully to check whether such procedure are somewhat dirty and non-trivial.

btw, I just tried to implement the method suggested by Kent, however it does not work. I read book chapter too.

I think that in Kent suggestion something is wrung as my desired derivative does not need to solve for a variational problem. In fact there is not bilinear form corresponde to it, more clearly, lets to repeat my question

J = \int u dx

using finite dimensional interpolation in interpolation space "V_h", we will have

J_h = E^T . U_h

where "U_h" is a vector corresponding to discritized form of "u" and "E" is a vector representing the coefficients of the constant function "1" with respect to the basis functions in "V_h".

Then the gateaux derivative of J with respect to u will be equal to vector "E", implies there is no need to solve a variational problem; unlike Kent suggested way. I do not know the meaning of "solve(dJ == 0, dj)", Kent: could you please elaborate this issue?

another question: derivative(...) function return a form, in my case the gateaux derivative will be constant vector (independent of "u"), how I could extract it form from produced by derivative(...) function?

Thanks

RT

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

I see. I think you are asking for something that FEniCS does not do. The form language (UFL) does not deal with the linear algebra level.

Revision history for this message
R. Tavakoli (rtav20) said :
#6

Martin,

considering your comment, I have a little experience with FEniCS, does your comment implies that to compute such a simple derivative is not possible using traditional functionality available within python FEniCS shell?

If so, what is your suggestion to do such computation?

Revision history for this message
Best Kent-Andre Mardal (kent-and) said :
#7

I am not sure I understand you correctly.
You can compute the derivative. You should however
be aware that in the finite element method you may
get the derivative either in nodal or dual form. The following
example illustrate the difference. You get a vector contain 1 only in the
nodal form.

from dolfin import *
mesh = UnitSquare(4,4)
V = FunctionSpace(mesh, "CG", 1)

v = TestFunction(V)
U = Function(V)

f = U*dx
L = derivative(f,U,v)

b = assemble(L)

w1 = Function(V)
w1.vector()[:] = b[:]

plot(w1)

u = TrialFunction(V)
m = u*v*dx

w2 = Function(V)
solve(m == L, w2)

plot(w2)
interactive()

On 22 March 2012 17:00, R. Tavakoli <email address hidden>wrote:

> Question #191352 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/191352
>
> Status: Needs information => Open
>
> R. Tavakoli gave more information on the question:
> Martin,
>
> I ask for it mainly to improve performance. I will take a look at
> library more carefully to check whether such procedure are somewhat
> dirty and non-trivial.
>
> btw, I just tried to implement the method suggested by Kent, however it
> does not work. I read book chapter too.
>
> I think that in Kent suggestion something is wrung as my desired
> derivative does not need to solve for a variational problem. In fact
> there is not bilinear form corresponde to it, more clearly, lets to
> repeat my question
>
>
> J = \int u dx
>
> using finite dimensional interpolation in interpolation space "V_h", we
> will have
>
> J_h = E^T . U_h
>
> where "U_h" is a vector corresponding to discritized form of "u" and "E"
> is a vector representing the coefficients of the constant function "1"
> with respect to the basis functions in "V_h".
>
> Then the gateaux derivative of J with respect to u will be equal to
> vector "E", implies there is no need to solve a variational problem;
> unlike Kent suggested way. I do not know the meaning of "solve(dJ == 0,
> dj)", Kent: could you please elaborate this issue?
>
> another question: derivative(...) function return a form, in my case
> the gateaux derivative will be constant vector (independent of "u"), how
> I could extract it form from produced by derivative(...) function?
>
> Thanks
>
> RT
>
> --
> 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
R. Tavakoli (rtav20) said :
#8

Dear Kent,

Many thanks for your comments, the first part of your given script was exactly solution to my question.

Best regards

RT

Revision history for this message
R. Tavakoli (rtav20) said :
#9

Thanks Kent-Andre Mardal, that solved my question.