# Mapped quadrature points, weights and solution at these quadrature points

**URL:** <https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113>\
**Category:** General\
**Created:** [January 9, 2023, 1:51am UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113 "2023-01-09T01:51:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ac101](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@ac101](https://fenicsproject.discourse.group/u/ac101)\
**Post date:** [January 9, 2023, 1:51am UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/1 "2023-01-09T01:51:20Z")

</div>

Hi,

I want to evaluate the mapped quadrature points, mapped quadrature weights i.e. quadrature weight X det(jacobian), and the value of the solution at each of the quadrature points in every element. I need this for postprocessing.

Currently, first I obtain the quadrature points and weight on the reference triangle and then perform the required affine transformation to get the mapped points and multiply the weights with det(Jacobian) to obtain the mapped weights. But is there any internal functionality within fenicsx where I can loop over elements and obtain the quadrature weights, points, and values of my approximate solution at these quadrature points?

regards  
Ankit

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 10, 2023, 1:22pm UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/2 "2023-01-10T13:22:11Z")

</div>

Consider the following:

```python
import dolfinx
from mpi4py import MPI
import ufl
import basix

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2, 2)
quadrature_degree = 1
V = dolfinx.fem.FunctionSpace(mesh, ufl.FiniteElement(
    "Quadrature", mesh.ufl_cell(), quadrature_degree, quad_scheme="default"))

quadrature_points, wts = basix.make_quadrature(
    basix.cell.string_to_type(mesh.topology.cell_name()), quadrature_degree)
x = ufl.SpatialCoordinate(mesh)
x_expr = dolfinx.fem.Expression(x, quadrature_points)

detJ = dolfinx.fem.Expression(ufl.JacobianDeterminant(mesh), quadrature_points)

for i in range(mesh.topology.index_map(mesh.topology.dim).size_local):
    print(
        f"Cell {i}, quadrature points {x_expr.eval([i])}, detJ {detJ.eval([i])}")

```

Returning

```bash
Cell 0, quadrature points [[0.83333333 0.16666667]], detJ [[0.25]]
Cell 1, quadrature points [[0.66666667 0.33333333]], detJ [[-0.25]]
Cell 2, quadrature points [[0.33333333 0.16666667]], detJ [[0.25]]
Cell 3, quadrature points [[0.83333333 0.66666667]], detJ [[0.25]]
Cell 4, quadrature points [[0.16666667 0.33333333]], detJ [[-0.25]]
Cell 5, quadrature points [[0.66666667 0.83333333]], detJ [[-0.25]]
Cell 6, quadrature points [[0.33333333 0.66666667]], detJ [[0.25]]
Cell 7, quadrature points [[0.16666667 0.83333333]], detJ [[-0.25]]

```

for quadrature degree 1  
and

```bash
Cell 0, quadrature points [[0.9454805 0.11596668 0.88403332 0.0545195 0.9454805 0.32951381
  0.67048619 0.0545195 0.88403332 0.32951381 0.67048619 0.11596668]], detJ [[0.25 0.25 0.25 0.25 0.25 0.25]]
Cell 1, quadrature points [[0.61596668 0.4454805 0.5545195 0.38403332 0.82951381 0.4454805
  0.5545195 0.17048619 0.82951381 0.38403332 0.61596668 0.17048619]], detJ [[-0.25 -0.25 -0.25 -0.25 -0.25 -0.25]]
Cell 2, quadrature points [[0.4454805 0.11596668 0.38403332 0.0545195 0.4454805 0.32951381
  0.17048619 0.0545195 0.38403332 0.32951381 0.17048619 0.11596668]], detJ [[0.25 0.25 0.25 0.25 0.25 0.25]]
Cell 3, quadrature points [[0.9454805 0.61596668 0.88403332 0.5545195 0.9454805 0.82951381
  0.67048619 0.5545195 0.88403332 0.82951381 0.67048619 0.61596668]], detJ [[0.25 0.25 0.25 0.25 0.25 0.25]]
Cell 4, quadrature points [[0.11596668 0.4454805 0.0545195 0.38403332 0.32951381 0.4454805
  0.0545195 0.17048619 0.32951381 0.38403332 0.11596668 0.17048619]], detJ [[-0.25 -0.25 -0.25 -0.25 -0.25 -0.25]]
Cell 5, quadrature points [[0.61596668 0.9454805 0.5545195 0.88403332 0.82951381 0.9454805
  0.5545195 0.67048619 0.82951381 0.88403332 0.61596668 0.67048619]], detJ [[-0.25 -0.25 -0.25 -0.25 -0.25 -0.25]]
Cell 6, quadrature points [[0.4454805 0.61596668 0.38403332 0.5545195 0.4454805 0.82951381
  0.17048619 0.5545195 0.38403332 0.82951381 0.17048619 0.61596668]], detJ [[0.25 0.25 0.25 0.25 0.25 0.25]]
Cell 7, quadrature points [[0.11596668 0.9454805 0.0545195 0.88403332 0.32951381 0.9454805
  0.0545195 0.67048619 0.32951381 0.88403332 0.11596668 0.67048619]], detJ [[-0.25 -0.25 -0.25 -0.25 -0.25 -0.25]]

```

for quadrature degree 3. Note that if you know that your domain is affine (i.e. a first order triangle or a tetrahedra, you only need to compute detJ at one point, not every quadrature point).

For further questions it would be great if you could describe in more detail what you want to achieve with your post-processing, as there might be built-in functions for what you want already

---

<div class="post-metadata">

**Author:** ![chan](https://avatars.discourse-cdn.com/v4/letter/c/4491bb/32.png) [@chan](https://fenicsproject.discourse.group/u/chan)\
**Post date:** [March 19, 2024, 3:43am UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/3 "2024-03-19T03:43:51Z")

</div>

Hello everyone,

I get an error when I execute the code, it’s about the inside of the print part

* * *

TypeError Traceback (most recent call last)  
Cell In[34], line 19  
16 detJ = dolfinx.fem.Expression(ufl.JacobianDeterminant(mesh), quadrature\_points)  
18 for i in range(mesh.topology.index\_map(mesh.topology.dim).size\_local):  
—\> 19 print(f"Cell {i}, quadrature points {x\_expr.eval([i])}, detJ {detJ.eval([i])}")

I get the error TypeError: Expression.eval() missing 1 required positional argument: ‘cells’.

Isn’t this code compatible with Fenicsx and Dolfinx? If it is not compatible, how can I perform the mapping of quadrature points, weights, and solution asked in this question in Fenicsx and Dolfinx?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 19, 2024, 7:59am UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/4 "2024-03-19T07:59:49Z")

</div>

Please note that the answer to this question is over a year old, using an older version of DOLFINx, it is quite straightforward to make it compatible with 0.7.x, i.e.

```python
import dolfinx
from mpi4py import MPI
import ufl
import basix

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2, 2)
quadrature_degree = 1
V = dolfinx.fem.FunctionSpace(mesh, ufl.FiniteElement(
    "Quadrature", mesh.ufl_cell(), quadrature_degree, quad_scheme="default"))

quadrature_points, wts = basix.make_quadrature(
    basix.cell.string_to_type(mesh.topology.cell_name()), quadrature_degree)
x = ufl.SpatialCoordinate(mesh)
x_expr = dolfinx.fem.Expression(x, quadrature_points)

detJ = dolfinx.fem.Expression(ufl.JacobianDeterminant(mesh), quadrature_points)

for i in range(mesh.topology.index_map(mesh.topology.dim).size_local):
    print(
        f"Cell {i}, quadrature points {x_expr.eval(mesh, [i])}, detJ {detJ.eval(mesh, [i])}")

```

gives

```bash
Cell 0, quadrature points [[0.83333333 0.16666667]], detJ [[0.25]]
Cell 1, quadrature points [[0.66666667 0.33333333]], detJ [[-0.25]]
Cell 2, quadrature points [[0.33333333 0.16666667]], detJ [[0.25]]
Cell 3, quadrature points [[0.83333333 0.66666667]], detJ [[0.25]]
Cell 4, quadrature points [[0.16666667 0.33333333]], detJ [[-0.25]]
Cell 5, quadrature points [[0.66666667 0.83333333]], detJ [[-0.25]]
Cell 6, quadrature points [[0.33333333 0.66666667]], detJ [[0.25]]
Cell 7, quadrature points [[0.16666667 0.83333333]], detJ [[-0.25]]

```

---

<div class="post-metadata">

**Author:** ![edgar](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@edgar](https://fenicsproject.discourse.group/u/edgar)\
**Post date:** [April 24, 2024, 12:49pm UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/5 "2024-04-24T12:49:32Z")

</div>

Hi! Thank you @dokken for your answer. As I am trying to use it, I wonder when it is convenient and not convenient to add the ghost cells here:

```python
for i in range(mesh.topology.index_map(mesh.topology.dim).size_local):

```

like so:

```python
map_c = mesh.topology.index_map(mesh.topology.dim)
for i in range(map_c.size_local + map_c.num_ghosts):

```

I am also going to add this little note in case that it helps others (and myself) when looking for info:

# Getting the coordinates of the quadrature points by element

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 24, 2024, 5:01pm UTC](https://fenicsproject.discourse.group/t/mapped-quadrature-points-weights-and-solution-at-these-quadrature-points/10113/6 "2024-04-24T17:01:50Z")

</div>

This depends on what you want to use the quadrature points for, if you want to integrate over non-owned cells on every process or not.
