### evaluating all trial functions at a handful of points

145

views

0

Given a FunctionSpace `V`, `V.dim()==n`, I need to evaluate all `n` trial functions at a handful (`m`) of points and build a sparse `m`-by-`n` matrix from it. (I can then use that matrix to evaluate any linear combination of trial functions at those points.)

Any hint on how to do this?

Any hint on how to do this?

Community: FEniCS Project

### 3 Answers

2

Alright, I think I've got it now. You'll first have to find the cell in which the point sits (using BoundingBoxTree), and once you've got that, it's simply a matter of calling evaluate_basis_all and get_vertex_coordinates:

```
bbt = BoundingBoxTree()
bbt.build(mesh)
dofmap = self.V.dofmap()
el = self.V.element()
for xy in centers:
cell_id = bbt.compute_first_entity_collision(Point(*xy))
cell = Cell(mesh, cell_id)
coordinate_dofs = cell.get_vertex_coordinates()
v = numpy.zeros(1, dtype=float)
el.evaluate_basis_all(
v, xy, coordinate_dofs, cell_id
)
print(v[0])
```

2

dolfinx is 'vectorising' many of these functions in the NumPy sense, which will make to much more efficient when calling them from Python.

written
4 months ago by
Garth Wells

2

See the function

`FiniteElement::evaluate_basis_all`

.
Sounds promising. Is there any documentation on the arguments? Also, do I assume correctly that I'll first have to extract the cell_id of the cell in which the point sits using BoundingBoxTree?

written
4 months ago by
Nico Schlömer

Never mind, I think I got it now. Thanks for the pointer, this is exactly what I needed.

written
4 months ago by
Nico Schlömer

-1

Hi Nico,

I had a hard time making out what you meant by your question. Is it that you have a function space V over a mesh that naturally allows n nodes, and you wish to evaluate "a" trial function defined over that space V, only on a subset of those nodes, (say) m points in all? If so, I believe that you need to have a function space that admits just those m points, and interpolate (or project your trial function to that space. But I admit, this reply is kind of groping in the dark.

What do you mean by making a matrix mxn. Do you have a form, that needs to be assembled? Some details might probably help.

Cheers,

Hisham
Not $n$ nodes, $n$ degrees of freedom; $V.dim()==n$. I want to evaluate all basis functions at a few (let's say one) particular point. That point is no necessarily a node in mesh.

I had a hard time making out what you meant by your question. Is it that you have a function space V over a mesh that naturally allows n nodes, and you wish to evaluate "a" trial function defined over that space V, only on a subset of those nodes, (say) m points in all? If so, I believe that you need to have a function space that admits just those m points, and interpolate (or project your trial function to that space. But I admit, this reply is kind of groping in the dark.

What do you mean by making a matrix mxn. Do you have a form, that needs to be assembled? Some details might probably help.

Cheers,

Hisham

written
4 months ago by
Nico Schlömer

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

https://github.com/hippylib/hippylib/blob/master/hippylib/pointwiseObservation.py

and the associated C++ files

https://github.com/hippylib/hippylib/blob/master/hippylib/AssemblePointwiseObservation/AssemblePointwiseObservation.h

https://github.com/hippylib/hippylib/blob/master/hippylib/AssemblePointwiseObservation/AssemblePointwiseObservation.cpp

for the extension module.