File: test_custom_element.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 (574 lines) | stat: -rw-r--r-- 17,056 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# Copyright (c) 2022 Matthew Scroggs
# FEniCS Project
# SPDX-License-Identifier: MIT

import numpy as np
import pytest

import basix
from basix import CellType


def test_lagrange_custom_triangle_degree1():
    """Test Lagrange custom element.

    Test that Lagrange element created as a custom element agrees with
    built-in Lagrange.
    """
    wcoeffs = np.eye(3)
    z = np.zeros((0, 2))
    x = [
        [np.array([[0.0, 0.0]]), np.array([[1.0, 0.0]]), np.array([[0.0, 1.0]])],
        [z, z, z],
        [z],
        [],
    ]
    z = np.zeros((0, 1, 0, 1))
    M = [[np.array([[[[1.0]]]]), np.array([[[[1.0]]]]), np.array([[[[1.0]]]])], [z, z, z], [z], []]

    lagrange = basix.create_element(basix.ElementFamily.P, CellType.triangle, 1)
    element = basix.create_custom_element(
        CellType.triangle,
        [],
        wcoeffs,
        x,
        M,
        0,
        basix.MapType.identity,
        basix.SobolevSpace.H1,
        False,
        1,
        1,
        basix.PolysetType.standard,
    )
    points = basix.create_lattice(CellType.triangle, 5, basix.LatticeType.equispaced, True)
    assert np.allclose(lagrange.tabulate(1, points), element.tabulate(1, points))
    assert np.allclose(lagrange.base_transformations(), element.base_transformations())


def test_lagrange_custom_triangle_degree1_l2piola():
    """Test a custom element with a L2 Piola map."""

    wcoeffs = np.eye(3)
    z = np.zeros((0, 2))
    x = [
        [np.array([[0.0, 0.0]]), np.array([[1.0, 0.0]]), np.array([[0.0, 1.0]])],
        [z, z, z],
        [z],
        [],
    ]
    z = np.zeros((0, 1, 0, 1))
    M = [[np.array([[[[1.0]]]]), np.array([[[[1.0]]]]), np.array([[[[1.0]]]])], [z, z, z], [z], []]

    lagrange = basix.create_element(basix.ElementFamily.P, CellType.triangle, 1)
    element = basix.create_custom_element(
        CellType.triangle,
        [],
        wcoeffs,
        x,
        M,
        0,
        basix.MapType.L2Piola,
        basix.SobolevSpace.L2,
        False,
        1,
        1,
        basix.PolysetType.standard,
    )
    points = basix.create_lattice(CellType.triangle, 5, basix.LatticeType.equispaced, True)
    assert np.allclose(lagrange.tabulate(1, points), element.tabulate(1, points))
    assert np.allclose(lagrange.base_transformations(), element.base_transformations())


def test_lagrange_custom_triangle_degree4():
    """Test that Lagrange element created as a custom element agrees with built-in Lagrange."""
    wcoeffs = np.eye(15)
    x = [
        [np.array([[0.0, 0.0]]), np.array([[1.0, 0.0]]), np.array([[0.0, 1.0]])],
        [
            np.array([[0.75, 0.25], [0.5, 0.5], [0.25, 0.75]]),
            np.array([[0.0, 0.25], [0.0, 0.5], [0.0, 0.75]]),
            np.array([[0.25, 0.0], [0.5, 0.0], [0.75, 0.0]]),
        ],
        [np.array([[0.25, 0.25], [0.5, 0.25], [0.25, 0.5]])],
        [],
    ]
    ident = np.array([[[[1.0], [0.0], [0.0]]], [[[0.0], [1.0], [0.0]]], [[[0.0], [0.0], [1.0]]]])
    M = [
        [np.array([[[[1.0]]]]), np.array([[[[1.0]]]]), np.array([[[[1.0]]]])],
        [ident, ident, ident],
        [ident],
        [],
    ]

    lagrange = basix.create_element(
        basix.ElementFamily.P, CellType.triangle, 4, basix.LagrangeVariant.equispaced
    )
    element = basix.create_custom_element(
        CellType.triangle,
        [],
        wcoeffs,
        x,
        M,
        0,
        basix.MapType.identity,
        basix.SobolevSpace.H1,
        False,
        4,
        4,
        basix.PolysetType.standard,
    )

    points = basix.create_lattice(CellType.triangle, 5, basix.LatticeType.equispaced, True)
    assert np.allclose(lagrange.tabulate(1, points), element.tabulate(1, points))
    assert np.allclose(lagrange.base_transformations(), element.base_transformations())


def test_lagrange_custom_quadrilateral_degree1():
    """Test that Lagrange element created as a custom element agrees with built-in Lagrange."""
    wcoeffs = np.eye(4)
    z = np.zeros((0, 2))
    x = [
        [
            np.array([[0.0, 0.0]]),
            np.array([[1.0, 0.0]]),
            np.array([[0.0, 1.0]]),
            np.array([[1.0, 1.0]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]

    lagrange = basix.create_element(basix.ElementFamily.P, CellType.quadrilateral, 1)
    element = basix.create_custom_element(
        CellType.quadrilateral,
        [],
        wcoeffs,
        x,
        M,
        0,
        basix.MapType.identity,
        basix.SobolevSpace.H1,
        False,
        1,
        1,
        basix.PolysetType.standard,
    )
    points = basix.create_lattice(CellType.quadrilateral, 5, basix.LatticeType.equispaced, True)
    assert np.allclose(lagrange.tabulate(1, points), element.tabulate(1, points))
    assert np.allclose(lagrange.base_transformations(), element.base_transformations())


def test_raviart_thomas_triangle_degree1():
    """Test custom  Raviart-Thomas element.

    Test that Raviart-Thomas element created as a custom element agrees
    with built-in Raviart-Thomas.
    """
    wcoeffs = np.zeros((3, 6))
    wcoeffs[0, 0] = 1
    wcoeffs[1, 3] = 1

    pts, wts = basix.make_quadrature(CellType.triangle, 2)
    poly = basix.tabulate_polynomials(basix.PolynomialType.legendre, CellType.triangle, 1, pts)
    for i in range(3):
        wcoeffs[2, i] = sum(pts[:, 0] * poly[i, :] * wts)
        wcoeffs[2, 3 + i] = sum(pts[:, 1] * poly[i, :] * wts)

    pts, wts = basix.make_quadrature(CellType.interval, 2)

    x = [[], [], [], []]
    for _ in range(3):
        x[0].append(np.zeros((0, 2)))
    x[1].append(np.array([[1 - p[0], p[0]] for p in pts]))
    x[1].append(np.array([[0, p[0]] for p in pts]))
    x[1].append(np.array([[p[0], 0] for p in pts]))
    x[2].append(np.zeros((0, 2)))

    M = [[], [], [], []]
    for _ in range(3):
        M[0].append(np.zeros((0, 2, 0, 1)))
    for normal in [[-1, -1], [-1, 0], [0, 1]]:
        mat = np.empty((1, 2, len(wts), 1))
        mat[0, 0, :, 0] = normal[0] * wts
        mat[0, 1, :, 0] = normal[1] * wts
        M[1].append(mat)
    M[2].append(np.zeros((0, 2, 0, 1)))

    element = basix.create_custom_element(
        CellType.triangle,
        [2],
        wcoeffs,
        x,
        M,
        0,
        basix.MapType.contravariantPiola,
        basix.SobolevSpace.HDiv,
        False,
        0,
        1,
        basix.PolysetType.standard,
    )
    rt = basix.create_element(basix.ElementFamily.RT, CellType.triangle, 1)
    points = basix.create_lattice(CellType.triangle, 5, basix.LatticeType.equispaced, True)
    assert np.allclose(rt.tabulate(1, points), element.tabulate(1, points))
    assert np.allclose(rt.base_transformations(), element.base_transformations())


def create_lagrange1_quad(
    cell_type=CellType.quadrilateral,
    degree=1,
    wcoeffs=None,
    x=None,
    M=None,
    value_shape=None,
    interpolation_nderivs=0,
    discontinuous=False,
):
    """Attempt to create a Lagrange 1 element on a quad."""
    if wcoeffs is None:
        wcoeffs = np.eye(4)
    if x is None:
        z = np.zeros((0, 2))
        x = [
            [
                np.array([[0.0, 0.0]]),
                np.array([[1.0, 0.0]]),
                np.array([[0.0, 1.0]]),
                np.array([[1.0, 1.0]]),
            ],
            [z, z, z, z],
            [z],
            [],
        ]
    if M is None:
        z = np.zeros((0, 1, 0, 1))
        M = [
            [
                np.array([[[[1.0]]]]),
                np.array([[[[1.0]]]]),
                np.array([[[[1.0]]]]),
                np.array([[[[1.0]]]]),
            ],
            [z, z, z, z],
            [z],
            [],
        ]
    if value_shape is None:
        value_shape = []
    basix.create_custom_element(
        cell_type,
        value_shape,
        wcoeffs,
        x,
        M,
        interpolation_nderivs,
        basix.MapType.identity,
        basix.SobolevSpace.H1,
        discontinuous,
        1,
        degree,
        basix.PolysetType.standard,
    )


def test_create_lagrange1_quad():
    """Test that the above function works if no inputs are modified."""
    create_lagrange1_quad()


def test_wcoeffs_wrong_shape():
    """Test that a runtime error is thrown when wcoeffs is the wrong shape."""
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of"):
        create_lagrange1_quad(wcoeffs=np.eye(3))


def test_wcoeffs_too_few_cols():
    """Test that a runtime error is thrown when wcoeffs has too few columns."""
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of"):
        create_lagrange1_quad(
            wcoeffs=np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]])
        )


def test_wcoeffs_too_few_rows():
    """Test that a runtime error is thrown when wcoeffs has too few rows."""
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of"):
        create_lagrange1_quad(
            wcoeffs=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 1.0]])
        )


def test_wcoeffs_zero_row():
    """Test that a runtime error is thrown when wcoeffs has a row of zeros."""
    with pytest.raises(
        RuntimeError, match="Cannot orthogonalise the rows of a matrix with incomplete row rank"
    ):
        create_lagrange1_quad(
            wcoeffs=np.array(
                [
                    [1.0, 0.0, 0.0, 0.0],
                    [0.0, 1.0, 0.0, 0.0],
                    [0.0, 0.0, 1.0, 0.0],
                    [0.0, 0.0, 0.0, 0.0],
                ]
            )
        )


def test_wcoeffs_equal_rows():
    """Test that a runtime error is thrown when wcoeffs has two equal rows."""
    with pytest.raises(
        RuntimeError, match="Cannot orthogonalise the rows of a matrix with incomplete row rank"
    ):
        create_lagrange1_quad(
            wcoeffs=np.array(
                [
                    [1.0, 0.0, 0.0, 0.0],
                    [0.0, 1.0, 0.0, 0.0],
                    [0.0, 0.0, 1.0, 1.0],
                    [0.0, 0.0, 1.0, 1.0],
                ]
            )
        )


def test_x_wrong_tdim():
    """Test that a runtime error is thrown when a point in x has the wrong tdim."""
    z = np.zeros((0, 2))
    x = [
        [
            np.array([[0.0, 0.0, 0.0]]),
            np.array([[1.0, 0.0]]),
            np.array([[0.0, 1.0]]),
            np.array([[1.0, 1.0]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="x has a point with the wrong tdim"):
        create_lagrange1_quad(x=x)


def test_x_too_many_points():
    """Test that a runtime error is thrown when x has too many points."""
    z = np.zeros((0, 2))
    x = [
        [
            np.array([[0.0, 0.0]]),
            np.array([[1.0, 0.0]]),
            np.array([[0.0, 1.0]]),
            np.array([[1.0, 1.0]]),
        ],
        [z, z, z, z],
        [np.array([[0.5, 0.5]])],
        [],
    ]
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(x=x)


def test_x_point_tdim_too_high():
    """Test that exception is raised when x has a point in an entity with a too high dimension."""
    z = np.zeros((0, 2))
    x = [
        [np.array([[0.0, 0.0]]), np.array([[1.0, 0.0]]), np.array([[0.0, 1.0]]), z],
        [z, z, z, z],
        [z],
        [np.array([[1.0, 1.0]])],
    ]
    with pytest.raises(RuntimeError, match="x has the wrong number of entities"):
        create_lagrange1_quad(x=x)


def test_x_wrong_entity_count():
    """Test that a runtime error is thrown when x has the wrong number of edges."""
    z = np.zeros((0, 2))
    x = [
        [
            np.array([[0.0, 0.0]]),
            np.array([[1.0, 0.0]]),
            np.array([[0.0, 1.0]]),
            np.array([[1.0, 1.0]]),
        ],
        [z, z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="x has the wrong number of entities"):
        create_lagrange1_quad(x=x)


def test_M_wrong_value_size():
    """Test that a runtime error is thrown when M has the wrong shape."""
    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0], [1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(M=M)


def test_M_too_many_points():
    """Test that a runtime error is thrown when M is the wrong shape."""
    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
        ],
        [z, z, z, z],
        [np.array([[[[1.0]]]])],
        [],
    ]
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of rows"):
        create_lagrange1_quad(M=M)

    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0], [1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(M=M)


def test_M_wrong_entities():
    """Test that a runtime error is thrown when the shape of M does not match x."""
    z = np.zeros((0, 1, 0, 1))
    M = [
        [np.array([[[[1.0]]]]), np.array([[[[1.0]]]]), np.array([[[[1.0]]], [[[1.0]]]]), z],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(M=M)


def test_M_too_many_derivs():
    """Test that a runtime error is thrown when M is the wrong shape."""
    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0, 1.0]]]]),
            np.array([[[[1.0]]]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(M=M)


def test_M_zero_row():
    """Test that a runtime error is thrown when M has a zero row."""
    z = np.zeros((0, 1, 0, 1))
    M = [
        [
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[1.0]]]]),
            np.array([[[[0.0]]]]),
        ],
        [z, z, z, z],
        [z],
        [],
    ]
    with pytest.raises(RuntimeError, match="Dual matrix is singular"):
        create_lagrange1_quad(M=M)


def test_wrong_value_shape():
    """Test that a runtime error is thrown when value shape is wrong."""
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of columns"):
        create_lagrange1_quad(value_shape=[2])


def test_wrong_cell_type():
    """Test that a runtime error is thrown when cell type is wrong."""
    with pytest.raises(RuntimeError, match="x has a point with the wrong tdim"):
        create_lagrange1_quad(cell_type=CellType.hexahedron)


def test_wrong_degree():
    """Test that a runtime error is thrown when degree is wrong."""
    with pytest.raises(RuntimeError, match="wcoeffs has the wrong number of columns"):
        create_lagrange1_quad(degree=0)


def test_wrong_discontinuous():
    """Test that a runtime error is thrown when discontinuous is wrong."""
    with pytest.raises(RuntimeError, match="Discontinuous element can only have interior DOFs"):
        create_lagrange1_quad(discontinuous=True)


def test_wrong_interpolation_nderivs():
    """Test that a runtime error is thrown when number of interpolation derivatives is wrong."""
    with pytest.raises(RuntimeError, match="M has the wrong shape"):
        create_lagrange1_quad(interpolation_nderivs=1)


@pytest.mark.parametrize("dim", range(4))
@pytest.mark.parametrize("component", range(3))
def test_point_outside_cell_gives_warning(dim, component):
    """Test the defining an element with a point outside the cell gives a warning."""
    lagrange = basix.create_element(
        basix.ElementFamily.P, CellType.tetrahedron, 4, basix.LagrangeVariant.gll_warped
    )
    wcoeffs = lagrange.wcoeffs

    x = [[j.copy() for j in i] for i in lagrange.x]
    M = lagrange.M

    x[dim][0][0][component] = -1.0

    with pytest.warns(UserWarning):
        basix.create_custom_element(
            CellType.tetrahedron,
            [],
            wcoeffs,
            x,
            M,
            0,
            basix.MapType.identity,
            basix.SobolevSpace.H1,
            False,
            4,
            4,
            basix.PolysetType.standard,
        )