# How to choose a specific quadrature rule?

**URL:** <https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527>\
**Category:** General\
**Created:** [February 26, 2023, 7:25am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527 "2023-02-26T07:25:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Discoverer](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Discoverer](https://fenicsproject.discourse.group/u/Discoverer)\
**Post date:** [February 26, 2023, 7:25am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/1 "2023-02-26T07:25:16Z")

</div>

I have a question about the codes as follows which is in Fenics tutorial of hyperelasticity

```auto
metadata={"quadrature_degree":4}
ds=ufl.Measure('ds',domain=domain,subdomain_data=facet_tag,metadata=metadata)
dx=ufl.Measure('dx',domain=domain,metadata=metadata)

```

I want to know how metadata is implemented in ufl.Measure?And what is the quadrature rule used here?(a 4th degree rule?)  
Is there a way to change to different quadrature rules in Dolfinx?

In addition, I try to use the following codes to try to compare the degree of the qudrature rules but the result is the same for degree from 1 to 4, so it seems that I should also change the functionspace where the function f lives ?

```auto
from mpi4py import MPI
from dolfinx import fem,mesh
import ufl

domain=mesh.create_interval(MPI.COMM_WORLD,1,[0,1])
V=fem.FunctionSpace(domain,("CG",1))

#I want to intergate f=x^2 from 0 to 1
f=fem.Function(V)
u=lambda x: x[0]**2
f.interpolate(u)

metadata={"qudrature_degree":4} 
a=f*ufl.dx(domain=domain,metadata=metadata)
a_=fem.form(a)
b=fem.assemble_scalar(a_)

print(b)

```

---

<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:** [February 26, 2023, 8:38am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/2 "2023-02-26T08:38:35Z")

</div>

For the first set of questions, I would recommend [Total number of gauss point - #2 by dokken](https://fenicsproject.discourse.group/t/total-number-of-gauss-point/8904/2)

> [@Discoverer](#):
>
> In addition, I try to use the following codes to try to compare the degree of the qudrature rules but the result is the same for degree from 1 to 4, so it seems that I should also change the functionspace where the function f lives ?

Yes, to see an effect of the quadrature choice, you cannot just integrate a CG-1 function, as it is exact with degree=1. If you instead use

```python
x=ufl.SpatialCoordinate(domain)
f = x[0]**2

```

you should see an effect with degree=1 and all the others.

---

<div class="post-metadata">

**Author:** ![Discoverer](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Discoverer](https://fenicsproject.discourse.group/u/Discoverer)\
**Post date:** [February 26, 2023, 11:10am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/3 "2023-02-26T11:10:30Z")

</div>

Thanks a lot.Now I change the code to

```auto
x=ufl.SpatialCoordinate(domain)
f=x[0]**2
for k in rang(3):
    meta={"qudrature_degree":k}
    dx=ufl.Measure('dx',domain=domain,metadata=meta)
    a=f*ufl.dx
    b=fem.assemble_scalar(fem.form(a))
    print(b)

```

but I get the same results, is there something wrong?

---

<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:** [February 26, 2023, 11:48am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/4 "2023-02-26T11:48:47Z")

</div>

Im not at a computer, but it might be super-convergence due to the mesh being structured. Either try with a higher order polynomial or an unstructured mesh.

---

<div class="post-metadata">

**Author:** ![Discoverer](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Discoverer](https://fenicsproject.discourse.group/u/Discoverer)\
**Post date:** [February 27, 2023, 9:02am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/5 "2023-02-27T09:02:03Z")

</div>

Thanks a lot.I will have a try

---

<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:** [February 27, 2023, 10:36am UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/6 "2023-02-27T10:36:50Z")

</div>

For example:

```python
from mpi4py import MPI
from dolfinx import fem, mesh
import ufl

domain = mesh.create_interval(MPI.COMM_WORLD, 1, [0, 1])

# I want to intergate f=x^2 from 0 to 1
x = ufl.SpatialCoordinate(domain)
f = x[0]**2
for i in range(5):
    metadata = {"quadrature_degree": i}
    a = f*ufl.dx(domain=domain, metadata=metadata)
    a_ = fem.form(a)
    b = fem.assemble_scalar(a_)
    print(i, f"{b:.5e}")

```

gives:

```bash
0 2.50000e-01
1 2.50000e-01
2 3.33333e-01
3 3.33333e-01
4 3.33333e-01

```

Note that you have a typo in

> [@Discoverer](#):
>
> ` meta={"qudrature_degree":k}`

---

<div class="post-metadata">

**Author:** ![Discoverer](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Discoverer](https://fenicsproject.discourse.group/u/Discoverer)\
**Post date:** [February 27, 2023, 1:14pm UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/7 "2023-02-27T13:14:46Z")

</div>

Great thanks!  
Sorry for questions about this typo 🥲  
when I use “qudrature\_degree” it sends no error ,so what is the reason here?It seems that something is implicitly implemented here.

---

<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:** [February 27, 2023, 1:23pm UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/8 "2023-02-27T13:23:58Z")

</div>

Why should it? DOLFINx only parses the `metadata` for known keys, if they are not found it uses default values. Potentially your dict could have thousands of entries, that we would not want to parse to check the validity of each key, or making educated guesses on what a key should be interpreted as.

---

<div class="post-metadata">

**Author:** ![Discoverer](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Discoverer](https://fenicsproject.discourse.group/u/Discoverer)\
**Post date:** [February 27, 2023, 1:52pm UTC](https://fenicsproject.discourse.group/t/how-to-choose-a-specific-quadrature-rule/10527/9 "2023-02-27T13:52:11Z")

</div>

Thanks.I am more clear about it.
