|
| 1 | +from firedrake import * |
| 2 | +from firedrake.interpolation import ( |
| 3 | + MixedInterpolator, SameMeshInterpolator, CrossMeshInterpolator, |
| 4 | + get_interpolator, VomOntoVomInterpolator, |
| 5 | +) |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def params(): |
| 10 | + params = [] |
| 11 | + for mat_type in [None, "aij"]: |
| 12 | + params.append(pytest.param(mat_type, None, id=f"mat_type={mat_type}")) |
| 13 | + for sub_mat_type in [None, "aij", "baij"]: |
| 14 | + params.append(pytest.param("nest", sub_mat_type, id=f"nest_sub_mat_type={sub_mat_type}")) |
| 15 | + return params |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parallel([1, 2]) |
| 19 | +@pytest.mark.parametrize("value_shape", ["scalar", "vector"], ids=lambda v: f"fs_type={v}") |
| 20 | +@pytest.mark.parametrize("mat_type", [None, "aij", "baij"], ids=lambda v: f"mat_type={v}") |
| 21 | +def test_same_mesh_mattype(value_shape, mat_type): |
| 22 | + if COMM_WORLD.size > 1: |
| 23 | + prefix = "mpi" |
| 24 | + else: |
| 25 | + prefix = "seq" |
| 26 | + mesh = UnitSquareMesh(4, 4) |
| 27 | + if value_shape == "scalar": |
| 28 | + fs_type = FunctionSpace |
| 29 | + else: |
| 30 | + fs_type = VectorFunctionSpace |
| 31 | + V1 = fs_type(mesh, "CG", 1) |
| 32 | + V2 = fs_type(mesh, "CG", 2) |
| 33 | + |
| 34 | + u = TrialFunction(V1) |
| 35 | + |
| 36 | + interp = interpolate(u, V2) |
| 37 | + assert isinstance(get_interpolator(interp), SameMeshInterpolator) |
| 38 | + res = assemble(interp, mat_type=mat_type) |
| 39 | + |
| 40 | + if value_shape == "scalar": |
| 41 | + # Always seqaij for scalar |
| 42 | + assert res.petscmat.type == prefix + "aij" |
| 43 | + else: |
| 44 | + # Defaults to seqaij |
| 45 | + assert res.petscmat.type == prefix + (mat_type if mat_type else "aij") |
| 46 | + |
| 47 | + with pytest.raises(NotImplementedError): |
| 48 | + # MatNest only implemented for interpolation between MixedFunctionSpaces |
| 49 | + assemble(interp, mat_type="nest") |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("value_shape", ["scalar", "vector"], ids=lambda v: f"fs_type={v}") |
| 53 | +@pytest.mark.parametrize("mat_type", [None, "aij"], ids=lambda v: f"mat_type={v}") |
| 54 | +def test_cross_mesh_mattype(value_shape, mat_type): |
| 55 | + mesh1 = UnitSquareMesh(1, 1) |
| 56 | + mesh2 = UnitSquareMesh(1, 1) |
| 57 | + if value_shape == "scalar": |
| 58 | + fs_type = FunctionSpace |
| 59 | + else: |
| 60 | + fs_type = VectorFunctionSpace |
| 61 | + V1 = fs_type(mesh1, "CG", 1) |
| 62 | + V2 = fs_type(mesh2, "CG", 1) |
| 63 | + |
| 64 | + u = TrialFunction(V1) |
| 65 | + |
| 66 | + interp = interpolate(u, V2) |
| 67 | + assert isinstance(get_interpolator(interp), CrossMeshInterpolator) |
| 68 | + res = assemble(interp, mat_type=mat_type) |
| 69 | + |
| 70 | + # only aij for cross-mesh |
| 71 | + assert res.petscmat.type == "seqaij" |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("value_shape", ["scalar", "vector"], ids=lambda v: f"fs_type={v}") |
| 75 | +@pytest.mark.parametrize("mat_type", [None, "aij", "baij", "matfree"], ids=lambda v: f"mat_type={v}") |
| 76 | +def test_vomtovom_mattype(value_shape, mat_type): |
| 77 | + mesh = UnitSquareMesh(1, 1) |
| 78 | + points = [[0.1, 0.1]] |
| 79 | + vom = VertexOnlyMesh(mesh, points) |
| 80 | + if value_shape == "scalar": |
| 81 | + fs_type = FunctionSpace |
| 82 | + else: |
| 83 | + fs_type = VectorFunctionSpace |
| 84 | + P0DG = fs_type(vom, "DG", 0) |
| 85 | + P0DG_io = fs_type(vom.input_ordering, "DG", 0) |
| 86 | + |
| 87 | + u = TrialFunction(P0DG) |
| 88 | + interp = interpolate(u, P0DG_io) |
| 89 | + assert isinstance(get_interpolator(interp), VomOntoVomInterpolator) |
| 90 | + res = assemble(interp, mat_type=mat_type) |
| 91 | + if not mat_type or mat_type == "matfree": |
| 92 | + assert res.petscmat.type == "python" |
| 93 | + else: |
| 94 | + if value_shape == "scalar": |
| 95 | + # Always seqaij for scalar |
| 96 | + assert res.petscmat.type == "seqaij" |
| 97 | + else: |
| 98 | + # Defaults to seqaij |
| 99 | + assert res.petscmat.type == "seq" + (mat_type if mat_type else "aij") |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize("value_shape", ["scalar", "vector"], ids=lambda v: f"fs_type={v}") |
| 103 | +@pytest.mark.parametrize("mat_type", [None, "aij", "baij"], ids=lambda v: f"mat_type={v}") |
| 104 | +def test_point_eval_mattype(value_shape, mat_type): |
| 105 | + mesh = UnitSquareMesh(1, 1) |
| 106 | + points = [[0.1, 0.1], [0.5, 0.5], [0.9, 0.9]] |
| 107 | + vom = VertexOnlyMesh(mesh, points) |
| 108 | + if value_shape == "scalar": |
| 109 | + fs_type = FunctionSpace |
| 110 | + else: |
| 111 | + fs_type = VectorFunctionSpace |
| 112 | + P0DG = fs_type(vom, "DG", 0) |
| 113 | + V = fs_type(mesh, "CG", 1) |
| 114 | + |
| 115 | + u = TrialFunction(V) |
| 116 | + interp = interpolate(u, P0DG) |
| 117 | + assert isinstance(get_interpolator(interp), SameMeshInterpolator) |
| 118 | + res = assemble(interp, mat_type=mat_type) |
| 119 | + |
| 120 | + if value_shape == "scalar": |
| 121 | + # Always seqaij for scalar |
| 122 | + assert res.petscmat.type == "seqaij" |
| 123 | + else: |
| 124 | + # Defaults to seqaij |
| 125 | + assert res.petscmat.type == "seq" + (mat_type if mat_type else "aij") |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.parametrize("value_shape", ["scalar", "vector"], ids=lambda v: f"fs_type={v}") |
| 129 | +@pytest.mark.parametrize("mat_type,sub_mat_type", params()) |
| 130 | +def test_mixed_same_mesh_mattype(value_shape, mat_type, sub_mat_type): |
| 131 | + mesh = UnitSquareMesh(1, 1) |
| 132 | + if value_shape == "scalar": |
| 133 | + fs_type = FunctionSpace |
| 134 | + else: |
| 135 | + fs_type = VectorFunctionSpace |
| 136 | + V1 = fs_type(mesh, "CG", 1) |
| 137 | + V2 = fs_type(mesh, "CG", 2) |
| 138 | + V3 = fs_type(mesh, "CG", 3) |
| 139 | + V4 = fs_type(mesh, "CG", 4) |
| 140 | + |
| 141 | + W = V1 * V2 |
| 142 | + U = V3 * V4 |
| 143 | + |
| 144 | + w = TrialFunction(W) |
| 145 | + w0, w1 = split(w) |
| 146 | + if value_shape == "scalar": |
| 147 | + expr = as_vector([w0 + w1, w0 + w1]) |
| 148 | + else: |
| 149 | + w00, w01 = split(w0) |
| 150 | + w10, w11 = split(w1) |
| 151 | + expr = as_vector([w00 + w10, w00 + w10, w01 + w11, w01 + w11]) |
| 152 | + interp = interpolate(expr, U) |
| 153 | + assert isinstance(get_interpolator(interp), MixedInterpolator) |
| 154 | + res = assemble(interp, mat_type=mat_type, sub_mat_type=sub_mat_type) |
| 155 | + if not mat_type or mat_type == "aij": |
| 156 | + # Defaults to seqaij |
| 157 | + assert res.petscmat.type == "seqaij" |
| 158 | + else: |
| 159 | + assert res.petscmat.type == "nest" |
| 160 | + for (i, j) in [(0, 0), (0, 1), (1, 0), (1, 1)]: |
| 161 | + sub_mat = res.petscmat.getNestSubMatrix(i, j) |
| 162 | + if value_shape == "scalar": |
| 163 | + # Always seqaij for scalar |
| 164 | + assert sub_mat.type == "seqaij" |
| 165 | + else: |
| 166 | + # matnest sub_mat_type defaults to baij |
| 167 | + assert sub_mat.type == "seq" + (sub_mat_type if sub_mat_type else "baij") |
| 168 | + |
| 169 | + with pytest.raises(NotImplementedError): |
| 170 | + assemble(interp, mat_type="baij") |
| 171 | + |
| 172 | + with pytest.raises(NotImplementedError): |
| 173 | + assemble(interp, mat_type="matfree") |
0 commit comments