/
test_chen_holst_odt.py
179 lines (145 loc) · 4.32 KB
/
test_chen_holst_odt.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
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
# -*- coding: utf-8 -*-
#
import os.path
import numpy
import meshio
import optimesh
from helpers import download_mesh
def test_simple1():
X = numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.4, 0.5, 0.0],
]
)
cells = numpy.array([[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]])
X, cells = optimesh.chen_holst.odt(X, cells, tol=1.0e-5, max_num_steps=100)
# Test if we're dealing with the mesh we expect.
nc = X.flatten()
norm1 = numpy.linalg.norm(nc, ord=1)
norm2 = numpy.linalg.norm(nc, ord=2)
normi = numpy.linalg.norm(nc, ord=numpy.inf)
tol = 1.0e-12
ref = 5.0
assert abs(norm1 - ref) < tol * ref
ref = 2.1213203435596424
assert abs(norm2 - ref) < tol * ref
ref = 1.0
assert abs(normi - ref) < tol * ref
return
def test_simple2():
X = numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.7, 0.5, 0.0],
[1.7, 0.5, 0.0],
]
)
cells = numpy.array([[0, 1, 4], [1, 5, 4], [2, 4, 5], [2, 3, 4], [3, 0, 4]])
X, cells = optimesh.chen_holst.odt(X, cells, 1.0e-3, 100)
# Test if we're dealing with the mesh we expect.
nc = X.flatten()
norm1 = numpy.linalg.norm(nc, ord=1)
norm2 = numpy.linalg.norm(nc, ord=2)
normi = numpy.linalg.norm(nc, ord=numpy.inf)
tol = 1.0e-12
ref = 7.374074074074074
assert abs(norm1 - ref) < tol * ref
ref = 2.8007812940925643
assert abs(norm2 - ref) < tol * ref
ref = 1.7
assert abs(normi - ref) < tol * ref
return
def test_simple3():
X = numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.7, 0.5, 0.0],
[1.7, 0.5, 0.0],
]
)
cells = numpy.array(
[
[0, 1, 6],
[1, 7, 6],
[1, 2, 7],
[2, 3, 7],
[3, 4, 7],
[4, 6, 7],
[4, 5, 6],
[5, 0, 6],
]
)
X, cells = optimesh.chen_holst.odt(X, cells, 1.0e-3, 100)
# Test if we're dealing with the mesh we expect.
nc = X.flatten()
norm1 = numpy.linalg.norm(nc, ord=1)
norm2 = numpy.linalg.norm(nc, ord=2)
normi = numpy.linalg.norm(nc, ord=numpy.inf)
tol = 1.0e-12
ref = 12.000268061419682
assert abs(norm1 - ref) < tol * ref
ref = 3.9829396222966804
assert abs(norm2 - ref) < tol * ref
ref = 2.0
assert abs(normi - ref) < tol * ref
return
def test_circle():
filename = "circle.vtk"
if not os.path.isfile(filename):
import pygmsh
geom = pygmsh.built_in.Geometry()
geom.add_circle(
[0.0, 0.0, 0.0],
1.0,
5.0e-3,
num_sections=4,
# If compound==False, the section borders have to be points of the
# discretization. If using a compound circle, they don't; gmsh can
# choose by itself where to point the circle points.
compound=True,
)
X, cells, _, _, _ = pygmsh.generate_mesh(
geom, fast_conversion=True, remove_faces=True
)
meshio.write_points_cells(filename, X, cells)
mesh = meshio.read(filename)
c = mesh.cells["triangle"].astype(numpy.int)
X, cells = optimesh.chen_holst.odt(mesh.points, c, 1.0e-3, 100)
return
def test_pacman():
filename = download_mesh(
"pacman.vtk", "19a0c0466a4714b057b88e339ab5bd57020a04cdf1d564c86dc4add6"
)
mesh = meshio.read(filename)
X, cells = optimesh.chen_holst.odt(mesh.points, mesh.cells["triangle"], 1.0e-3, 500)
# Test if we're dealing with the mesh we expect.
nc = X.flatten()
norm1 = numpy.linalg.norm(nc, ord=1)
norm2 = numpy.linalg.norm(nc, ord=2)
normi = numpy.linalg.norm(nc, ord=numpy.inf)
tol = 1.0e-10
ref = 1918.756560192194
assert abs(norm1 - ref) < tol * ref
ref = 75.21580844291586
assert abs(norm2 - ref) < tol * ref
ref = 5.0
assert abs(normi - ref) < tol * ref
return
if __name__ == "__main__":
# test_simple1()
# test_simple2()
# test_simple3()
# test_circle()
test_pacman()