Hello everybody,
I've started using Fenics for my PhD, and after being able to resolve all issues myself so far, I got stuck on a seemingly simple problem. As a rookie, I would appreciate any help or pointer in the right direction.
Background: I have defined a measure for the deviation of the solution (vector valued function) u from a target at a discrete set of points
$$ p = \sum\limits_{k=1}^K \beta_k \left( \left( u_x \left( x_k \right) - \hat{y}_{x,k} \right)^2 + \left( u_y \left( x_k \right) - \hat{y}_{y,k} \right)^2 \right) $$
I need to evaluate the derivative wrt to the N expansion coefficients of u at K values x_k. I need to do so often, and cheaply. To this end I would like to assemble a matrix containg the derivatives of u wrt expansion coefficients U_j at x_k
$$ \frac{\partial}{\partial U_j} u \left( x_k \right) = \frac{\partial}{\partial U_j} \left( \sum\limits_{i=1}^N \phi_i \left( x_k \right) U_{i} \right) = \phi_j \left( x_k \right) \qquad \forall x_k, k=1,...,K $$
for the x and y "subspace" of a Function, separately (excuse the sloppy notation). Or at least I would need to be able to keep account which dof belongs to x and y. This would allow me to get the gradient by a matrix-vector multiplication.
Approaches considered so far:
- Set all but the j-th vector value of a function to zero and evaluate the function at all K points. Issue : prohibitive, as it takes N*K function evaluations (K approx N, i.e. N^2).
- I'm able to evaluate all basis functions of an element in any given cell, but that doesn't help. Looking at the C++ implementation eval() of a Function, there's probably some way to do this
- I was also not able to piece together a solution ussing differentiate() or diff(), or think of a creative way to assemble this KxN matrix
Maybe a little code example:
mesh = UnitSquareMesh(10,10)
V = VectorFunctionSpace(mesh,'P',2)
F = Function(V)
x_k = (0.5,0.5)
u_k = F(x_k)
=> How does u_k[0] and u_k[1] depend on the expansion coefficients F.vector(), with correct ordering of the dofs?
In case anybody finds the time to help me out, thanks a lot.
Best Regards