File: test_measures.py

package info (click to toggle)
fenics-ufl 2025.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,176 kB
  • sloc: python: 25,267; makefile: 170
file content (190 lines) | stat: -rwxr-xr-x 6,523 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
"""Tests of the various ways Measure objects can be created and used."""

from mockobjects import MockMesh, MockMeshFunction
from utils import LagrangeElement

from ufl import Cell, Coefficient, FunctionSpace, Measure, Mesh, as_ufl, dC, dI, dO, triangle


def test_construct_forms_from_default_measures():
    # Create defaults:
    dx = Measure("dx")
    # dE = Measure("dE")

    ds = Measure("ds")
    dS = Measure("dS")

    dl = Measure("dr")

    dP = Measure("dP")
    # dV = Measure("dV")

    dc = Measure("dc")
    # dC = Measure("dC")
    # dO = Measure("dO")
    # dI = Measure("dI")

    ds_b = Measure("ds_b")
    ds_t = Measure("ds_t")
    ds_v = Measure("ds_v")
    dS_h = Measure("dS_h")
    dS_v = Measure("dS_v")

    # Check that names are mapped properly
    assert dx.integral_type() == "cell"
    # assert dE.integral_type() == "macro_cell"

    assert ds.integral_type() == "exterior_facet"
    assert dS.integral_type() == "interior_facet"

    assert dl.integral_type() == "ridge"

    assert dP.integral_type() == "vertex"
    # TODO: Change dP to dV:
    # assert dP.integral_type() == "point"
    # assert dV.integral_type() == "vertex"

    assert dc.integral_type() == "custom"
    assert dC.integral_type() == "cutcell"
    assert dO.integral_type() == "overlap"
    assert dI.integral_type() == "interface"

    # TODO: Remove firedrake hacks:
    assert ds_b.integral_type() == "exterior_facet_bottom"
    assert ds_t.integral_type() == "exterior_facet_top"
    assert ds_v.integral_type() == "exterior_facet_vert"
    assert dS_h.integral_type() == "interior_facet_horiz"
    assert dS_v.integral_type() == "interior_facet_vert"

    # Check that defaults are set properly
    assert dx.ufl_domain() is None
    assert dx.metadata() == {}

    # Check that we can create a basic form with default measure
    one = as_ufl(1)
    one * dx(Mesh(LagrangeElement(triangle, 1, (2,))))


def test_foo():
    # Define a manifold domain, allows checking gdim/tdim mixup errors
    gdim = 3
    tdim = 2
    cell = Cell("triangle")
    mymesh = MockMesh(9)
    mydomain = Mesh(LagrangeElement(cell, 1, (gdim,)), ufl_id=9, cargo=mymesh)

    assert cell.topological_dimension() == tdim
    assert cell.cellname() == "triangle"
    assert mydomain.topological_dimension() == tdim
    assert mydomain.geometric_dimension() == gdim
    assert mydomain.ufl_cell() == cell
    assert mydomain.ufl_id() == 9
    assert mydomain.ufl_cargo() == mymesh

    # Define a coefficient for use in tests below
    V = FunctionSpace(mydomain, LagrangeElement(cell, 1))
    f = Coefficient(V)

    # Test definition of a custom measure with explicit parameters
    metadata = {"opt": True}
    mydx = Measure("dx", domain=mydomain, subdomain_id=3, metadata=metadata)
    assert mydx.ufl_domain().ufl_id() == mydomain.ufl_id()
    assert mydx.metadata() == metadata
    M = f * mydx

    # Compatibility:
    dx = Measure("dx")
    # domain=None,
    # subdomain_id="everywhere",
    # metadata=None)
    assert dx.ufl_domain() is None
    assert dx.subdomain_id() == "everywhere"

    # Set subdomain_id to "everywhere", still no domain set
    dxe = dx()
    assert dxe.ufl_domain() is None
    assert dxe.subdomain_id() == "everywhere"

    # Set subdomain_id to 5, still no domain set
    dx5 = dx(5)
    assert dx5.ufl_domain() is None
    assert dx5.subdomain_id() == 5

    # Check that original dx is untouched
    assert dx.ufl_domain() is None
    assert dx.subdomain_id() == "everywhere"

    # Set subdomain_id to (2,3), still no domain set
    dx23 = dx((2, 3))
    assert dx23.ufl_domain() is None
    assert dx23.subdomain_id(), 2 == 3

    # Map metadata to metadata, ffc interprets as before
    dxm = dx(metadata={"dummy": 123})
    # assert dxm.metadata() == {"dummy":123}
    assert dxm.metadata() == {"dummy": 123}  # Deprecated, TODO: Remove

    assert dxm.ufl_domain() is None
    assert dxm.subdomain_id() == "everywhere"

    # dxm = dx(metadata={"dummy":123})
    # assert dxm.metadata() == {"dummy":123}
    dxm = dx(metadata={"dummy": 123})
    assert dxm.metadata() == {"dummy": 123}

    assert dxm.ufl_domain() is None
    assert dxm.subdomain_id() == "everywhere"

    # Mock some dolfin data structures
    dx = Measure("dx")
    ds = Measure("ds")
    dS = Measure("dS")
    mesh = MockMesh(8)
    cell_domains = MockMeshFunction(1, mesh)
    exterior_facet_domains = MockMeshFunction(2, mesh)
    interior_facet_domains = MockMeshFunction(3, mesh)

    dxd = dx(subdomain_data=cell_domains)
    dsd = ds(subdomain_data=exterior_facet_domains)
    dSd = dS(subdomain_data=interior_facet_domains)
    # Current behaviour: no domain created, measure domain data is a single
    # object not a full dict
    assert dxd.ufl_domain() is None
    assert dsd.ufl_domain() is None
    assert dSd.ufl_domain() is None
    assert dxd.subdomain_data() is cell_domains
    assert dsd.subdomain_data() is exterior_facet_domains
    assert dSd.subdomain_data() is interior_facet_domains

    # Create some forms with these measures (used in checks below):
    Mx = f * dxd
    Ms = f**2 * dsd
    MS = f("+") * dSd
    M = f * dxd + f**2 * dsd + f("+") * dSd

    # Test extracting domain data from a form for each measure:
    (domain,) = Mx.ufl_domains()
    assert domain.ufl_id() == mydomain.ufl_id()
    assert domain.ufl_cargo() == mymesh
    assert len(Mx.subdomain_data()[mydomain]["cell"]) == 1
    assert Mx.subdomain_data()[mydomain]["cell"][0] == cell_domains

    (domain,) = Ms.ufl_domains()
    assert domain.ufl_cargo() == mymesh
    assert len(Ms.subdomain_data()[mydomain]["exterior_facet"]) == 1
    assert Ms.subdomain_data()[mydomain]["exterior_facet"][0] == exterior_facet_domains

    (domain,) = MS.ufl_domains()
    assert domain.ufl_cargo() == mymesh
    assert len(MS.subdomain_data()[mydomain]["interior_facet"]) == 1
    assert MS.subdomain_data()[mydomain]["interior_facet"][0] == interior_facet_domains

    # Test joining of these domains in a single form
    (domain,) = M.ufl_domains()
    assert domain.ufl_cargo() == mymesh
    assert len(M.subdomain_data()[mydomain]["cell"]) == 1
    assert M.subdomain_data()[mydomain]["cell"][0] == cell_domains
    assert len(M.subdomain_data()[mydomain]["exterior_facet"]) == 1
    assert M.subdomain_data()[mydomain]["exterior_facet"][0] == exterior_facet_domains
    assert len(M.subdomain_data()[mydomain]["interior_facet"]) == 1
    assert M.subdomain_data()[mydomain]["interior_facet"][0] == interior_facet_domains