|
| 1 | +.. only:: html |
| 2 | + |
| 3 | + .. contents:: |
| 4 | + |
| 5 | +Quadrature rules |
| 6 | +================ |
| 7 | + |
| 8 | +To numerically compute the integrals in the variational formulation, |
| 9 | +a quadrature rule is required. |
| 10 | +By default, Firedrake obtains a quadrature rule by estimating the polynomial |
| 11 | +degree of the integrands within a :py:class:`ufl.Form`. Sometimes |
| 12 | +this estimate might be quite large, and a warning like this one will be raised: |
| 13 | + |
| 14 | +.. code-block:: |
| 15 | +
|
| 16 | + tsfc:WARNING Estimated quadrature degree 13 more than tenfold greater than any argument/coefficient degree (max 1) |
| 17 | +
|
| 18 | +For integrals with very complicated nonlinearities, the estimated quadrature |
| 19 | +degree might be in the hundreds or thousands, rendering the integration |
| 20 | +prohibitively expensive, or leading to segfaults. |
| 21 | + |
| 22 | +Specifying the quadrature rule in the variational formulation |
| 23 | +------------------------------------------------------------- |
| 24 | + |
| 25 | +To manually override the default, the |
| 26 | +quadrature degree can be prescribed on each integral :py:class:`~ufl.measure.Measure`, |
| 27 | + |
| 28 | +.. code-block:: python3 |
| 29 | +
|
| 30 | + inner(sin(u)**4, v) * dx(degree=4) |
| 31 | +
|
| 32 | +Setting ``degree=4`` means that the quadrature rule will be exact only for integrands |
| 33 | +of total polynomial degree up to 4. This, of course, will introduce a greater numerical error than the default. |
| 34 | + |
| 35 | +For integrals that do not specify a quadrature degree, the default may be keyed as |
| 36 | +``"quadrature_degree"`` in the ``form_compiler_parameters`` dictionary passed on to |
| 37 | +:py:func:`~.solve`, :py:func:`~.project`, or :py:class:`~.NonlinearVariationalProblem`. |
| 38 | + |
| 39 | +.. code-block:: python3 |
| 40 | +
|
| 41 | + F = inner(grad(u), grad(v))*dx(degree=0) + inner(exp(u), v)*dx - inner(1, v)*dx |
| 42 | +
|
| 43 | + solve(F == 0, u, form_compiler_parameters={"quadrature_degree": 4}) |
| 44 | +
|
| 45 | +In the example above, only the integrals with unspecified quadrature degree |
| 46 | +will be computed on a quadrature rule that exactly integrates polynomials of |
| 47 | +the degree set in ``form_compiler_parameters``. |
| 48 | + |
| 49 | +Another way to specify the quadrature rule is through the ``scheme`` keyword. This could be |
| 50 | +either a :py:class:`~finat.quadrature.QuadratureRule`, or a string. Supported string values |
| 51 | +are ``"default"``, ``"canonical"``, and ``"KMV"``. For more details see |
| 52 | +:py:func:`~FIAT.quadrature_schemes.create_quadrature`. |
| 53 | + |
| 54 | +Lumped quadrature schemes |
| 55 | +------------------------- |
| 56 | + |
| 57 | +Spectral elements, such as Gauss-Legendre-Lobatto and `KMV`_, may be used with |
| 58 | +lumped quadrature schemes to produce a diagonal mass matrix. |
| 59 | + |
| 60 | +.. literalinclude:: ../../tests/firedrake/regression/test_quadrature_manual.py |
| 61 | + :language: python3 |
| 62 | + :dedent: |
| 63 | + :start-after: [test_lump_scheme 1] |
| 64 | + :end-before: [test_lump_scheme 2] |
| 65 | + |
| 66 | +.. Note:: |
| 67 | + |
| 68 | + To obtain the lumped mass matrix with ``scheme="KMV"``, |
| 69 | + the ``degree`` argument should match the degree of the :py:class:`~finat.ufl.finiteelement.FiniteElement`. |
| 70 | + |
| 71 | +The Quadrature space |
| 72 | +-------------------- |
| 73 | + |
| 74 | +It is possible to define a finite element :py:class:`~.Function` on a quadrature rule. |
| 75 | +The ``"Quadrature"`` and ``"Boundary Quadrature"`` spaces are useful to |
| 76 | +interpolate data at quadrature points on cell interiors and cell boundaries, |
| 77 | +respectively. |
| 78 | + |
| 79 | +.. literalinclude:: ../../tests/firedrake/regression/test_quadrature_manual.py |
| 80 | + :language: python3 |
| 81 | + :dedent: |
| 82 | + :start-after: [test_quadrature_space 1] |
| 83 | + :end-before: [test_quadrature_space 2] |
| 84 | + |
| 85 | +The ``quad_scheme`` keyword argument again may be either |
| 86 | +:py:class:`~finat.quadrature.QuadratureRule` or a string. |
| 87 | +If a :py:class:`~.Function` in the ``"Quadrature"`` space appears within an |
| 88 | +integral, Firedrake will automatically select the quadrature rule that corresponds |
| 89 | +to ``dx(degree=quad_degree, scheme=quad_scheme)`` to match the one associated |
| 90 | +with the quadrature space. |
| 91 | + |
| 92 | +.. _element_quad_scheme: |
| 93 | + |
| 94 | +Specifying the quadrature for integral-type degrees of freedom |
| 95 | +-------------------------------------------------------------- |
| 96 | + |
| 97 | +Finite element spaces with :ref:`integral-type degrees of freedom <element_variants>` |
| 98 | +support different quadrature rules. |
| 99 | +These are selected by passing a string to the ``"quad_scheme"`` keyword argument of |
| 100 | +the :py:class:`~finat.ufl.finiteelement.FiniteElement` or |
| 101 | +:py:func:`~.FunctionSpace` constructors. For example, to construct a |
| 102 | +Crouzeix-Raviart space with degrees of freedom consisting of integrals along the edges |
| 103 | +computed from a 2-point average at the endpoints, one can set ``quad_scheme="KMV"``: |
| 104 | + |
| 105 | +.. code-block:: python3 |
| 106 | +
|
| 107 | + fe = FiniteElement("Crouzeix-Raviart", triangle, 1, variant="integral", quad_scheme="KMV") |
| 108 | +
|
| 109 | +.. Note:: |
| 110 | + |
| 111 | + Finite elements with integral-type degrees of freedom only accept string |
| 112 | + values for ``quad_scheme``, since it does not make sense to specify a |
| 113 | + concrete :py:class:`~finat.quadrature.QuadratureRule` when the degrees of |
| 114 | + freedom are defined on cell entities of different dimensions. |
| 115 | + |
| 116 | + |
| 117 | +.. _KMV : https://defelement.org/elements/kong-mulder-veldhuizen.html |
0 commit comments