| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Tests of table manipulation utilities. |
| 4 |
""" |
| 5 |
|
| 6 |
from ufl_legacy import triangle |
| 7 |
from ffc.uflacs.elementtables import equal_tables, strip_table_zeros, build_unique_tables, get_ffc_table_values |
| 8 |
|
| 9 |
import numpy as np |
| 10 |
default_tolerance = 1e-14 |
| 11 |
|
| 12 |
|
| 13 |
def old_strip_table_zeros(table, eps): |
| 14 |
"Quick fix translating new version to old behaviour to keep tests without modification." |
| 15 |
dofrange, dofmap, stripped_table = strip_table_zeros(table, False, eps) |
| 16 |
begin, end = dofrange |
| 17 |
return begin, end, stripped_table |
| 18 |
|
| 19 |
|
| 20 |
def test_equal_tables(): |
| 21 |
a = np.zeros((2, 3)) |
| 22 |
b = np.zeros((2, 3)) |
| 23 |
assert equal_tables(a, b, default_tolerance) |
| 24 |
|
| 25 |
a = np.ones((2, 3)) |
| 26 |
b = np.zeros((2, 3)) |
| 27 |
assert not equal_tables(a, b, default_tolerance) |
| 28 |
|
| 29 |
a = np.ones((2, 3)) |
| 30 |
b = np.ones((3, 2)) |
| 31 |
assert not equal_tables(a, b, default_tolerance) |
| 32 |
|
| 33 |
a = np.ones((2, 3))*1.1 |
| 34 |
b = np.ones((2, 3)) |
| 35 |
assert not equal_tables(a, b, default_tolerance) |
| 36 |
|
| 37 |
# Checking for all ones within given tolerance: |
| 38 |
eps = 1e-4 |
| 39 |
a = np.ones((2, 3))*(1.0+eps) |
| 40 |
assert equal_tables(a, np.ones(a.shape), 10*eps) |
| 41 |
|
| 42 |
|
| 43 |
def test_strip_table_zeros(): |
| 44 |
# Can strip entire table: |
| 45 |
a = np.zeros((2, 3)) |
| 46 |
e = np.zeros((2, 0)) |
| 47 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 48 |
assert begin == end # This is a way to check for all-zero table |
| 49 |
assert begin == 0 |
| 50 |
assert end == 0 |
| 51 |
assert equal_tables(b, e, default_tolerance) |
| 52 |
|
| 53 |
# Can keep entire nonzero table: |
| 54 |
a = np.ones((2, 3)) |
| 55 |
e = np.ones((2, 3)) |
| 56 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 57 |
assert begin != end |
| 58 |
assert begin == 0 |
| 59 |
assert end == a.shape[-1] |
| 60 |
assert equal_tables(b, e, default_tolerance) |
| 61 |
|
| 62 |
# Can strip one left side column: |
| 63 |
a = np.ones((2, 3)) |
| 64 |
a[:, 0] = 0.0 |
| 65 |
e = np.ones((2, 2)) |
| 66 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 67 |
assert begin != end |
| 68 |
assert begin == 1 |
| 69 |
assert end == a.shape[-1] |
| 70 |
assert equal_tables(b, e, default_tolerance) |
| 71 |
|
| 72 |
# Can strip one right side column: |
| 73 |
a = np.ones((2, 3)) |
| 74 |
a[:, 2] = 0.0 |
| 75 |
e = np.ones((2, 2)) |
| 76 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 77 |
assert begin != end |
| 78 |
assert begin == 0 |
| 79 |
assert end == a.shape[-1]-1 |
| 80 |
assert equal_tables(b, e, default_tolerance) |
| 81 |
|
| 82 |
# Can strip two columns on each side: |
| 83 |
a = np.ones((2, 5)) |
| 84 |
a[:, 0] = 0.0 |
| 85 |
a[:, 1] = 0.0 |
| 86 |
a[:, 3] = 0.0 |
| 87 |
a[:, 4] = 0.0 |
| 88 |
e = np.ones((2, 1)) |
| 89 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 90 |
assert begin != end |
| 91 |
assert begin == 2 |
| 92 |
assert end == a.shape[-1]-2 |
| 93 |
assert equal_tables(b, e, default_tolerance) |
| 94 |
|
| 95 |
# Can strip two columns on each side of rank 1 table: |
| 96 |
a = np.ones((5,)) |
| 97 |
a[..., 0] = 0.0 |
| 98 |
a[..., 1] = 0.0 |
| 99 |
a[..., 3] = 0.0 |
| 100 |
a[..., 4] = 0.0 |
| 101 |
e = np.ones((1,)) |
| 102 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 103 |
assert begin != end |
| 104 |
assert begin == 2 |
| 105 |
assert end == a.shape[-1]-2 |
| 106 |
assert equal_tables(b, e, default_tolerance) |
| 107 |
|
| 108 |
# Can strip two columns on each side of rank 3 table: |
| 109 |
a = np.ones((3, 2, 5)) |
| 110 |
a[..., 0] = 0.0 |
| 111 |
a[..., 1] = 0.0 |
| 112 |
a[..., 3] = 0.0 |
| 113 |
a[..., 4] = 0.0 |
| 114 |
e = np.ones((3, 2, 1)) |
| 115 |
begin, end, b = old_strip_table_zeros(a, default_tolerance) |
| 116 |
assert begin != end |
| 117 |
assert begin == 2 |
| 118 |
assert end == a.shape[-1]-2 |
| 119 |
assert equal_tables(b, e, default_tolerance) |
| 120 |
|
| 121 |
# Can strip columns on each side and compress the one in the middle: |
| 122 |
a = np.ones((2, 5)) |
| 123 |
a[:, 0] = 0.0 |
| 124 |
a[:, 1] = 1.0 |
| 125 |
a[:, 2] = 0.0 |
| 126 |
a[:, 3] = 3.0 |
| 127 |
a[:, 4] = 0.0 |
| 128 |
e = np.ones((2, 2)) |
| 129 |
e[:, 0] = 1.0 |
| 130 |
e[:, 1] = 3.0 |
| 131 |
dofrange, dofmap, b = strip_table_zeros(a, True, default_tolerance) |
| 132 |
begin, end = dofrange |
| 133 |
assert begin != end |
| 134 |
assert begin == 1 |
| 135 |
assert end == a.shape[-1] - 1 |
| 136 |
assert list(dofmap) == [1, 3] |
| 137 |
assert equal_tables(b, e, default_tolerance) |
| 138 |
|
| 139 |
|
| 140 |
def test_unique_tables_some_equal(): |
| 141 |
tables = [ |
| 142 |
np.zeros((2,)), |
| 143 |
np.ones((2,)), |
| 144 |
np.zeros((3,)), |
| 145 |
np.ones((2,)), |
| 146 |
np.ones((2,))*2, |
| 147 |
np.ones((2,))*2, |
| 148 |
] |
| 149 |
unique, mapping = build_unique_tables(tables, default_tolerance) |
| 150 |
expected_unique = [ |
| 151 |
np.zeros((2,)), |
| 152 |
np.ones((2,)), |
| 153 |
np.zeros((3,)), |
| 154 |
# np.ones((2,)), |
| 155 |
np.ones((2,))*2, |
| 156 |
# np.ones((2,))*2, |
| 157 |
] |
| 158 |
expected_mapping = dict((i, v) for i, v in enumerate([0, 1, 2, 1, 3, 3])) |
| 159 |
assert mapping == expected_mapping |
| 160 |
assert len(set(mapping.values())) == len(unique) |
| 161 |
for i, t in enumerate(tables): |
| 162 |
assert equal_tables(t, unique[mapping[i]], default_tolerance) |
| 163 |
|
| 164 |
|
| 165 |
def test_unique_tables_all_equal(): |
| 166 |
tables = [np.ones((3, 5))*2.0]*6 |
| 167 |
unique, mapping = build_unique_tables(tables, default_tolerance) |
| 168 |
expected_unique = [tables[0]] |
| 169 |
expected_mapping = dict((i, v) for i, v in enumerate([0]*6)) |
| 170 |
assert mapping == expected_mapping |
| 171 |
assert len(set(mapping.values())) == len(unique) |
| 172 |
for i, t in enumerate(tables): |
| 173 |
assert equal_tables(t, unique[mapping[i]], default_tolerance) |
| 174 |
|
| 175 |
def test_unique_tables_all_different(): |
| 176 |
tables = [ |
| 177 |
np.ones((2,)), |
| 178 |
np.ones((2, 3)), |
| 179 |
np.ones((2, 3, 4)), |
| 180 |
np.ones((2, 3, 4, 5)), |
| 181 |
] |
| 182 |
unique, mapping = build_unique_tables(tables, default_tolerance) |
| 183 |
expected_unique = tables |
| 184 |
expected_mapping = dict((i, i) for i in range(len(tables))) |
| 185 |
assert mapping == expected_mapping |
| 186 |
assert len(set(mapping.values())) == len(unique) |
| 187 |
for i, t in enumerate(tables): |
| 188 |
assert equal_tables(t, unique[mapping[i]], default_tolerance) |
| 189 |
|
| 190 |
def test_unique_tables_string_keys(): |
| 191 |
tables = { |
| 192 |
'a': np.zeros((2,)), |
| 193 |
'b': np.ones((2,)), |
| 194 |
'c': np.zeros((3,)), |
| 195 |
'd': np.ones((2,)), |
| 196 |
'e': np.ones((2,))*2, |
| 197 |
'f': np.ones((2,))*2, |
| 198 |
} |
| 199 |
unique, mapping = build_unique_tables(tables, default_tolerance) |
| 200 |
expected_unique = [ |
| 201 |
np.zeros((2,)), |
| 202 |
np.ones((2,)), |
| 203 |
np.zeros((3,)), |
| 204 |
# np.ones((2,)), |
| 205 |
np.ones((2,))*2, |
| 206 |
# np.ones((2,))*2, |
| 207 |
] |
| 208 |
expected_mapping = { 'a': 0, 'b': 1, 'c': 2, 'd': 1, 'e': 3, 'f': 3 } |
| 209 |
assert mapping == expected_mapping |
| 210 |
assert len(set(mapping.values())) == len(unique) |
| 211 |
for i, t in tables.items(): |
| 212 |
assert equal_tables(t, unique[mapping[i]], default_tolerance) |
| 213 |
|
| 214 |
|
| 215 |
def xtest_get_ffc_table_values_scalar_cell(): |
| 216 |
cell = triangle |
| 217 |
integral_type = "cell" |
| 218 |
entitytype = "cell" |
| 219 |
|
| 220 |
class MockElement: |
| 221 |
def value_shape(self): return () |
| 222 |
|
| 223 |
element = MockElement() |
| 224 |
component = () |
| 225 |
|
| 226 |
avg = None |
| 227 |
entity = 0 |
| 228 |
for num_points in (1, 3): |
| 229 |
for num_dofs in (1, 5): |
| 230 |
arr = np.ones((num_dofs, num_points)) |
| 231 |
for derivatives in [(0, 0), (1, 1)]: |
| 232 |
# Mocking table built by ffc |
| 233 |
ffc_tables = { |
| 234 |
num_points: { |
| 235 |
element: { |
| 236 |
avg: { # avg |
| 237 |
entity: { # entityid |
| 238 |
derivatives: arr |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
table = get_ffc_table_values(ffc_tables, |
| 245 |
cell, integral_type, |
| 246 |
element, avg, |
| 247 |
entitytype, derivatives, component, |
| 248 |
default_tolerance) |
| 249 |
assert equal_tables(table[0, ...], np.transpose(arr), default_tolerance) |
| 250 |
|
| 251 |
|
| 252 |
def xtest_get_ffc_table_values_vector_facet(): |
| 253 |
cell = triangle |
| 254 |
integral_type = "exterior_facet" |
| 255 |
entitytype = "facet" |
| 256 |
num_entities = 3 |
| 257 |
|
| 258 |
class MockElement: |
| 259 |
def value_shape(self): return (2,) |
| 260 |
|
| 261 |
element = MockElement() |
| 262 |
num_components = 2 |
| 263 |
|
| 264 |
avg = None |
| 265 |
for num_points in (1, 5): |
| 266 |
for num_dofs in (4, 7): |
| 267 |
# Make ones array of the right shape (all dimensions differ to detect algorithm bugs better) |
| 268 |
arr1 = np.ones((num_dofs, num_components, num_points)) |
| 269 |
arrays = [] |
| 270 |
for i in range(num_entities): |
| 271 |
arr = (i+1.0)*arr1 # Make first digit the facet number (1,2,3) |
| 272 |
for j in range(num_components): |
| 273 |
arr[:, j,:] += 0.1*(j+1.0) # Make first decimal the component number (1,2) |
| 274 |
for j in range(num_dofs): |
| 275 |
arr[j,:,:] += 0.01*(j+1.0) # Make second decimal the dof number |
| 276 |
for j in range(num_points): |
| 277 |
arr[:,:, j] += 0.001*(j+1.0) # Make third decimal the point number |
| 278 |
arrays.append(arr) |
| 279 |
|
| 280 |
for derivatives in [(), (0,)]: |
| 281 |
# Mocking table built by ffc |
| 282 |
ffc_tables = { |
| 283 |
num_points: { |
| 284 |
element: { |
| 285 |
avg: { |
| 286 |
entity: { derivatives: arrays[entity] } |
| 287 |
for entity in range(num_entities) |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
# Tables use flattened component, so we can loop over them as integers: |
| 293 |
for component in range(num_components): |
| 294 |
table = get_ffc_table_values(ffc_tables, |
| 295 |
cell, integral_type, |
| 296 |
element, avg, |
| 297 |
entitytype, derivatives, component, |
| 298 |
default_tolerance) |
| 299 |
for i in range(num_entities): |
| 300 |
# print table[i,...] |
| 301 |
assert equal_tables(table[i, ...], np.transpose(arrays[i][:, component,:]), default_tolerance) |