### How to compute the gradient of a bilinear form in C++?

271

views

1

Hi all,

I need to compute the gradient of a bilinear form.

A little bit of theory, in order to make you understand better.

Given a bilinear form, continuous and coercive on a Hilbert space H:

$a:HxH\rightarrow R$

The Gateaux derivative of this (in a "point" u) is a linear functional defined on H such that:

$L\psi=a\left(u,\psi\right)\forall\psi\in H$

By the Riesz's theorem, there exists a representative of H, say J, called "the gradient of a(.,.)" such that:

$\left(J,\psi\right)_H=a\left(u,\psi\right)\forall\psi\in H$(

Where (.,.)

Now, when everything is discretized using FEniCS, this representative should be a dofin::Function J

$\left[J_h\right]_i=a\left(u,\psi_i\right)$[

where Jh

Now, my question is: how do I compute this quantity? I tried to use the "NonlinearVariationalProblem.h" header, but it does not seem to have the tools we need. Also I tried some tool to extract all the basis function and compute that quantity "by hand", but I can't accomplish this task.

Thanks for your time.

I need to compute the gradient of a bilinear form.

A little bit of theory, in order to make you understand better.

Given a bilinear form, continuous and coercive on a Hilbert space H:

$a:HxH\rightarrow R$

`a`:`H``x``H`→`R`The Gateaux derivative of this (in a "point" u) is a linear functional defined on H such that:

$L\psi=a\left(u,\psi\right)\forall\psi\in H$

`L``ψ`=`a`(`u`,`ψ`)∀`ψ`∈`H`By the Riesz's theorem, there exists a representative of H, say J, called "the gradient of a(.,.)" such that:

$\left(J,\psi\right)_H=a\left(u,\psi\right)\forall\psi\in H$(

`J`,`ψ`)_{H}=`a`(`u`,`ψ`)∀`ψ`∈`H`Where (.,.)

_{H}is the inner product (L^{2}in our case) in H.Now, when everything is discretized using FEniCS, this representative should be a dofin::Function J

_{h}such that:$\left[J_h\right]_i=a\left(u,\psi_i\right)$[

`J`_{h}]_{i}=`a`(`u`,`ψ`_{i})where Jh

_{i}is the i^{th}coefficient of Jh relative to the basis function phi_{i}of the discretized space, and "a" our dolfin::Form.Now, my question is: how do I compute this quantity? I tried to use the "NonlinearVariationalProblem.h" header, but it does not seem to have the tools we need. Also I tried some tool to extract all the basis function and compute that quantity "by hand", but I can't accomplish this task.

Thanks for your time.

Community: FEniCS Project

### 2 Answers

3

This should work

```
u = Function(V)
v = TestFunction(V)
# Define a = a(u,v)
J = assemble(a)
```

1

I don't know if this is the only way, but see the following for my solution (answer at bottom) to computing the gradient of an arbitrary scalar function in Fenics (in C++).

https://fenicsproject.org/qa/10947/computing-derivatives-in-c

--JW

https://fenicsproject.org/qa/10947/computing-derivatives-in-c

--JW

Please login to add an answer/comment or follow this question.

I am coding in C++. Should this be included in the .ufl file?