File: test_interpolation_between_elements.py

package info (click to toggle)
fenics-basix 0.10.0.post0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,156 kB
  • sloc: cpp: 23,435; python: 10,829; makefile: 43; sh: 26
file content (316 lines) | stat: -rw-r--r-- 11,943 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright (c) 2021 Matthew Scroggs
# FEniCS Project
# SPDX-License-Identifier: MIT

import numpy as np
import pytest

import basix

from .utils import parametrize_over_elements


def run_test(lower_element, higher_element, power, value_size):
    l_points = lower_element.points
    l_eval = np.concatenate(
        [l_points[:, 0] ** power if i == 0 else 0 * l_points[:, 0] for i in range(value_size)]
    )
    l_coeffs = lower_element.interpolation_matrix @ l_eval

    i_m = basix.compute_interpolation_operator(lower_element, higher_element)
    h_coeffs = i_m @ l_coeffs

    h_points = higher_element.points
    h_eval = np.concatenate(
        [h_points[:, 0] ** power if i == 0 else 0 * h_points[:, 0] for i in range(value_size)]
    )
    h_coeffs2 = higher_element.interpolation_matrix @ h_eval
    assert np.allclose(h_coeffs, h_coeffs2)


@pytest.mark.parametrize(
    "cell_type",
    [
        basix.CellType.interval,
        basix.CellType.triangle,
        basix.CellType.tetrahedron,
        basix.CellType.quadrilateral,
        basix.CellType.hexahedron,
        basix.CellType.prism,
    ],
)
@pytest.mark.parametrize("orders", [(1, 2), (2, 4), (4, 5)])
def test_different_order_interpolation_lagrange(cell_type, orders):
    lower_element = basix.create_element(
        basix.ElementFamily.P, cell_type, orders[0], basix.LagrangeVariant.gll_warped
    )
    higher_element = basix.create_element(
        basix.ElementFamily.P, cell_type, orders[1], basix.LagrangeVariant.gll_warped
    )
    run_test(lower_element, higher_element, orders[0], lower_element.value_size)


@pytest.mark.parametrize(
    "variant1",
    [
        basix.LagrangeVariant.equispaced,
        basix.LagrangeVariant.gll_warped,
        basix.LagrangeVariant.gll_isaac,
    ],
)
@pytest.mark.parametrize(
    "variant2",
    [
        basix.LagrangeVariant.equispaced,
        basix.LagrangeVariant.gll_warped,
        basix.LagrangeVariant.gll_isaac,
    ],
)
@pytest.mark.parametrize(
    "cell_type",
    [
        basix.CellType.interval,
        basix.CellType.triangle,
        basix.CellType.tetrahedron,
        basix.CellType.quadrilateral,
        basix.CellType.hexahedron,
        basix.CellType.prism,
    ],
)
@pytest.mark.parametrize("order", [1, 4])
def test_different_variant_interpolation(cell_type, order, variant1, variant2):
    lower_element = basix.create_element(basix.ElementFamily.P, cell_type, order, variant1)
    higher_element = basix.create_element(basix.ElementFamily.P, cell_type, order, variant2)
    run_test(lower_element, higher_element, order, lower_element.value_size)


@pytest.mark.parametrize(
    "family, args",
    [
        [basix.ElementFamily.RT, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.N1E, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.BDM, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
        [basix.ElementFamily.N2E, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
    ],
)
@pytest.mark.parametrize(
    "cell_type",
    [
        basix.CellType.triangle,
        basix.CellType.tetrahedron,
        basix.CellType.quadrilateral,
        basix.CellType.hexahedron,
    ],
)
@pytest.mark.parametrize("orders", [(1, 2), (2, 4), (4, 5)])
def test_different_order_interpolation_vector(family, args, cell_type, orders):
    lower_element = basix.create_element(family, cell_type, orders[0], *args)
    higher_element = basix.create_element(family, cell_type, orders[1], *args)
    run_test(lower_element, higher_element, orders[0] - 1, lower_element.value_size)


@pytest.mark.parametrize("family, args", [[basix.ElementFamily.Regge, tuple()]])
@pytest.mark.parametrize("cell_type", [basix.CellType.triangle, basix.CellType.tetrahedron])
@pytest.mark.parametrize("orders", [(1, 2), (2, 4), (4, 5)])
def test_different_order_interpolation_matrix(family, args, cell_type, orders):
    lower_element = basix.create_element(family, cell_type, orders[0], *args)
    higher_element = basix.create_element(family, cell_type, orders[1], *args)
    run_test(lower_element, higher_element, orders[0] - 1, lower_element.value_size)


@pytest.mark.parametrize(
    "family1, args1",
    [
        [basix.ElementFamily.RT, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.N1E, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.BDM, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
        [basix.ElementFamily.N2E, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
    ],
)
@pytest.mark.parametrize(
    "family2, args2",
    [
        [basix.ElementFamily.RT, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.N1E, (basix.LagrangeVariant.legendre,)],
        [basix.ElementFamily.BDM, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
        [basix.ElementFamily.N2E, (basix.LagrangeVariant.legendre, basix.DPCVariant.legendre)],
    ],
)
@pytest.mark.parametrize(
    "cell_type",
    [
        basix.CellType.triangle,
        basix.CellType.tetrahedron,
        basix.CellType.quadrilateral,
        basix.CellType.hexahedron,
    ],
)
@pytest.mark.parametrize("order", [1, 4])
def test_different_element_interpolation(family1, args1, family2, args2, cell_type, order):
    lower_element = basix.create_element(family1, cell_type, order, *args1)
    higher_element = basix.create_element(family2, cell_type, order, *args2)
    run_test(lower_element, higher_element, order - 1, lower_element.value_size)


@pytest.mark.parametrize(
    "cell_type",
    [
        basix.CellType.triangle,
        basix.CellType.tetrahedron,
        basix.CellType.quadrilateral,
        basix.CellType.hexahedron,
    ],
)
@pytest.mark.parametrize("order", [1, 4])
def test_blocked_interpolation(cell_type, order):
    """Test interpolation of Nedelec's components into a Lagrange space."""
    nedelec = basix.create_element(
        basix.ElementFamily.N2E,
        cell_type,
        order,
        basix.LagrangeVariant.legendre,
        basix.DPCVariant.legendre,
    )
    lagrange = basix.create_element(
        basix.ElementFamily.P, cell_type, order, basix.LagrangeVariant.gll_isaac
    )

    n_points = nedelec.points
    if nedelec.value_size == 2:
        n_eval = np.concatenate([n_points[:, 0] ** order, n_points[:, 1] ** order])
    else:
        n_eval = np.concatenate(
            [n_points[:, 0] ** order, 0 * n_points[:, 0], n_points[:, 1] ** order]
        )
    n_coeffs = nedelec.interpolation_matrix @ n_eval

    l_points = lagrange.points
    if nedelec.value_size == 2:
        values = [l_points[:, 0] ** order, l_points[:, 1] ** order]
    else:
        values = [l_points[:, 0] ** order, 0 * l_points[:, 0], l_points[:, 1] ** order]
    l_coeffs = np.empty(lagrange.dim * nedelec.value_size)
    for i, v in enumerate(values):
        l_coeffs[i :: nedelec.value_size] = v

    # Test interpolation from Nedelec to blocked Lagrange
    i_m = basix.compute_interpolation_operator(nedelec, lagrange)
    assert np.allclose(l_coeffs, i_m @ n_coeffs)

    # Test interpolation from blocked Lagrange to Nedelec
    i_m = basix.compute_interpolation_operator(lagrange, nedelec)
    assert np.allclose(n_coeffs, i_m @ l_coeffs)


@parametrize_over_elements(5)
def test_degree_bounds(cell_type, degree, element_type, element_args):
    generator = np.random.default_rng(13)

    element = basix.create_element(element_type, cell_type, degree, *element_args)

    points = basix.create_lattice(cell_type, 10, basix.LatticeType.equispaced, True)
    tab = element.tabulate(0, points)[0]

    # Test that this element's basis functions are contained in Lagrange
    # space with degree element.embedded_superdegree
    coeffs = generator.random(element.dim)
    values = np.array([tab[:, :, i] @ coeffs for i in range(element.value_size)])

    if element.polyset_type == basix.PolysetType.standard:
        p_family = basix.ElementFamily.P
    elif element.polyset_type == basix.PolysetType.macroedge:
        p_family = basix.ElementFamily.iso

    if element.embedded_superdegree >= 0:
        # The element being tested should be a subset of this Lagrange space
        lagrange = basix.create_element(
            p_family,
            cell_type,
            element.embedded_superdegree,
            basix.LagrangeVariant.equispaced,
            discontinuous=True,
        )
        lagrange_coeffs = basix.compute_interpolation_operator(element, lagrange) @ coeffs
        lagrange_tab = lagrange.tabulate(0, points)[0]
        lagrange_values = np.array(
            [
                lagrange_tab[:, :, 0] @ lagrange_coeffs[i :: element.value_size]
                for i in range(element.value_size)
            ]
        )

        assert np.allclose(values, lagrange_values)

    if element.embedded_superdegree >= 1:
        # The element being tested should be NOT a subset of this
        # Lagrange space
        lagrange = basix.create_element(
            p_family,
            cell_type,
            element.embedded_superdegree - 1,
            basix.LagrangeVariant.equispaced,
            discontinuous=True,
        )
        lagrange_coeffs = basix.compute_interpolation_operator(element, lagrange) @ coeffs
        lagrange_tab = lagrange.tabulate(0, points)[0]
        lagrange_values = np.array(
            [
                lagrange_tab[:, :, 0] @ lagrange_coeffs[i :: element.value_size]
                for i in range(element.value_size)
            ]
        )

        assert not np.allclose(values, lagrange_values)

    # Test that the basis functions of Lagrange space with degree
    # element.embedded_subdegree are contained in this space

    if element.embedded_subdegree >= 0:
        # This Lagrange space should be a subset to the element being
        # tested
        lagrange = basix.create_element(
            p_family,
            cell_type,
            element.embedded_subdegree,
            basix.LagrangeVariant.equispaced,
            discontinuous=True,
        )
        lagrange_coeffs = generator.random(lagrange.dim * element.value_size)
        lagrange_tab = lagrange.tabulate(0, points)[0]
        lagrange_values = np.array(
            [
                lagrange_tab[:, :, 0] @ lagrange_coeffs[i :: element.value_size]
                for i in range(element.value_size)
            ]
        )
        coeffs = basix.compute_interpolation_operator(lagrange, element) @ lagrange_coeffs
        values = np.array([tab[:, :, i] @ coeffs for i in range(element.value_size)])
        assert np.allclose(values, lagrange_values)

    if element.polyset_type == basix.PolysetType.macroedge:
        if cell_type == basix.CellType.triangle and element.embedded_subdegree + 1 > 2:
            pytest.xfail("Cannot run test with macro polyset on a triangle with degree > 2")
        if cell_type == basix.CellType.tetrahedron and element.embedded_subdegree + 1 > 1:
            pytest.xfail("Cannot run test with macro polyset on a tetrahedron with degree > 1")

    if element.embedded_subdegree >= -1:
        # This Lagrange space should NOT be a subset to the element
        # being tested
        lagrange = basix.create_element(
            p_family,
            cell_type,
            element.embedded_subdegree + 1,
            basix.LagrangeVariant.equispaced,
            discontinuous=True,
        )
        lagrange_coeffs = generator.random(lagrange.dim * element.value_size)
        lagrange_tab = lagrange.tabulate(0, points)[0]
        lagrange_values = np.array(
            [
                lagrange_tab[:, :, 0] @ lagrange_coeffs[i :: element.value_size]
                for i in range(element.value_size)
            ]
        )
        coeffs = basix.compute_interpolation_operator(lagrange, element) @ lagrange_coeffs
        values = np.array([tab[:, :, i] @ coeffs for i in range(element.value_size)])
        assert not np.allclose(values, lagrange_values)