MCPcopy Create free account
hub / github.com/FEniCS/dolfinx / test_complex_assembly

Function test_complex_assembly

python/test/unit/fem/test_complex_assembler.py:27–74  ·  view source on GitHub ↗

Test assembly of complex matrices and vectors.

(complex_dtype)

Source from the content-addressed store, hash-verified

25
26@pytest.mark.parametrize("complex_dtype", [np.complex64, np.complex128])
27def test_complex_assembly(complex_dtype):
28 """Test assembly of complex matrices and vectors."""
29 real_dtype = np.real(complex_dtype(1.0)).dtype
30 mesh = create_unit_square(MPI.COMM_WORLD, 10, 10, dtype=real_dtype)
31 P2 = element("Lagrange", mesh.basix_cell(), 2, dtype=real_dtype)
32 V = functionspace(mesh, P2)
33 u = ufl.TrialFunction(V)
34 v = ufl.TestFunction(V)
35 g = -2 + 3.0j
36
37 a_real = form(inner(u, v) * dx, dtype=complex_dtype)
38 L1 = form(inner(g, v) * dx, dtype=complex_dtype)
39
40 b = assemble_vector(L1)
41 b.scatter_reverse(la.InsertMode.add)
42 bnorm = la.norm(b, la.Norm.l1)
43 b_norm_ref = abs(-2 + 3.0j)
44 assert bnorm == pytest.approx(b_norm_ref, rel=1e-5)
45
46 A = assemble_matrix(a_real)
47 A.scatter_reverse()
48 A0_norm = A.squared_norm()
49
50 x = ufl.SpatialCoordinate(mesh)
51
52 a_imag = form(1j * inner(u, v) * dx, dtype=complex_dtype)
53 f = 1j * ufl.sin(2 * np.pi * x[0])
54 L0 = form(inner(f, v) * dx, dtype=complex_dtype)
55 A = assemble_matrix(a_imag)
56 A.scatter_reverse()
57 A1_norm = A.squared_norm()
58 assert A0_norm == pytest.approx(A1_norm)
59
60 b = assemble_vector(L0)
61 b.scatter_reverse(la.InsertMode.add)
62 b1_norm = la.norm(b)
63
64 a_complex = form((1 + 1j) * inner(u, v) * dx, dtype=complex_dtype)
65 f = ufl.sin(2 * np.pi * x[0])
66 L2 = form(inner(f, v) * dx, dtype=complex_dtype)
67 A = assemble_matrix(a_complex)
68 A.scatter_reverse()
69 A2_norm = A.squared_norm()
70 assert A1_norm == pytest.approx(A2_norm / 2)
71 b = assemble_vector(L2)
72 b.scatter_reverse(la.InsertMode.add)
73 b2_norm = la.norm(b, la.Norm.l2)
74 assert b2_norm == pytest.approx(b1_norm)
75
76
77@pytest.mark.parametrize("complex_dtype", [np.complex64, np.complex128])

Callers

nothing calls this directly

Calls 9

create_unit_squareFunction · 0.90
functionspaceFunction · 0.90
formFunction · 0.90
assemble_vectorFunction · 0.90
assemble_matrixFunction · 0.90
basix_cellMethod · 0.80
normMethod · 0.80
scatter_reverseMethod · 0.45
squared_normMethod · 0.45

Tested by

no test coverage detected