-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtest_from_array.py
72 lines (61 loc) · 2.49 KB
/
test_from_array.py
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
import helpers
import numpy as np
import pygalmesh
def test_from_array():
n = 200
shape = (n, n, n)
h = (1.0 / shape[0], 1.0 / shape[1], 1.0 / shape[2])
vol = np.zeros(shape, dtype=np.uint16)
i, j, k = np.arange(shape[0]), np.arange(shape[1]), np.arange(shape[2])
ii, jj, kk = np.meshgrid(i, j, k)
vol[ii * ii + jj * jj + kk * kk < n**2] = 1
vol[ii * ii + jj * jj + kk * kk < (0.5 * n) ** 2] = 2
mesh = pygalmesh.generate_from_array(
vol,
h,
max_cell_circumradius=100 * min(h),
max_facet_distance=min(h),
verbose=False,
)
tol = min(h)
ref = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
assert abs(max(mesh.points[:, 0]) - ref[0]) < (1.0 + ref[0]) * tol
assert abs(min(mesh.points[:, 0]) - ref[1]) < (1.0 + ref[1]) * tol
assert abs(max(mesh.points[:, 1]) - ref[2]) < (1.0 + ref[2]) * tol
assert abs(min(mesh.points[:, 1]) - ref[3]) < (1.0 + ref[3]) * tol
assert abs(max(mesh.points[:, 2]) - ref[4]) < (1.0 + ref[4]) * tol
assert abs(min(mesh.points[:, 2]) - ref[5]) < (1.0 + ref[5]) * tol
vol = sum(helpers.compute_volumes(mesh.points, mesh.get_cells_type("tetra")))
ref = 1.0 / 6.0 * np.pi
# Debian needs 2.0e-2 here.
# <https://github.com/nschloe/pygalmesh/issues/60>
assert abs(vol - ref) < ref * 2.0e-2
def test_from_array_with_subdomain_sizing():
n = 200
shape = (n, n, n)
h = (1.0 / shape[0], 1.0 / shape[1], 1.0 / shape[2])
vol = np.zeros(shape, dtype=np.uint16)
i, j, k = np.arange(shape[0]), np.arange(shape[1]), np.arange(shape[2])
ii, jj, kk = np.meshgrid(i, j, k)
vol[ii * ii + jj * jj + kk * kk < n**2] = 1
vol[ii * ii + jj * jj + kk * kk < (0.5 * n) ** 2] = 2
mesh = pygalmesh.generate_from_array(
vol,
h,
max_cell_circumradius={1: 100 * min(h), 2: 10 * min(h)},
max_facet_distance=min(h),
verbose=False,
)
tol = min(h)
ref = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
assert abs(max(mesh.points[:, 0]) - ref[0]) < tol
assert abs(min(mesh.points[:, 0]) - ref[1]) < tol
assert abs(max(mesh.points[:, 1]) - ref[2]) < tol
assert abs(min(mesh.points[:, 1]) - ref[3]) < tol
assert abs(max(mesh.points[:, 2]) - ref[4]) < tol
assert abs(min(mesh.points[:, 2]) - ref[5]) < tol
vol = sum(helpers.compute_volumes(mesh.points, mesh.get_cells_type("tetra")))
ref = 1.0 / 6.0 * np.pi
# Debian needs 2.0e-2 here.
# <https://github.com/nschloe/pygalmesh/issues/60>
assert abs(vol - ref) < ref * 2.0e-2