-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmesh.py
68 lines (50 loc) · 2.5 KB
/
mesh.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
import numpy as np
def coordify(array):
""" Changes [a, b, c] to [ [a], [b], [c] ]"""
return array[:, np.newaxis] if array.ndim == 1 else array
# return np.atleast_2d(array).T
def spacing(m):
""" Returns spaces around vertices """
spaces = np.zeros_like(m).astype("float")
for i, e in enumerate(m[1:-1], start=1):
spaces[i] = (m[i+1] - m[i-1]) / 2.0
spaces[0] = m[1] - m[0]
spaces[-1] = m[-1] - m[-2]
return spaces
def GC(order, element_size, domain_size, domain_start = 0):
""" Returns coordinates for a 2d Gauss-Chebyshev mesh. """
assert(domain_size % element_size == 0)
nodes = np.arange( 1, order + 1 )
cheb = 0.5 * element_size * np.cos( np.pi * (2*nodes-1)/ (2*order) )
coords = np.array([])
for element in range(int(domain_size/element_size)):
coords = np.append(coords, cheb + ((0.5+element) * element_size))
coords += domain_start
np.ndarray.sort(coords)
# print("Gauss-Chebyshev mesh with", len(coords), "points, h_max =", np.max(coords[1:] - coords[:-1]))
return coords
def GaussChebyshev_1D(order, element_size, domain_size, domain_start = 0):
""" Returns coordinates for a 1d Gauss-Chebyshev mesh. """
# assert(domain_size % element_size == 0) // uncommented, because of floating point issue
nodes = np.polynomial.chebyshev.chebgauss(order)[0] # Get GC points on [-1;1]
nodes *= element_size / 2 # Scale from [-1;1] to element_size
coords = np.array([])
for element in range(int(domain_size // element_size)):
coords = np.append(coords, nodes + (0.5 + element) * element_size)
coords += domain_start
np.ndarray.sort(coords)
# print("Gauss-Chebyshev mesh with", len(coords), "points, h_max =", np.max(coords[1:] - coords[:-1]))
return coords
def GaussChebyshev_2D(order, element_size, domain_size, domain_start = 0):
""" Returns a 2d quadratic Gauss-Chebyshev mesh grid """
x = GaussChebyshev_1D(order, element_size, domain_size, domain_start)
y = GaussChebyshev_1D(order, element_size, domain_size, domain_start)
xx, yy = np.meshgrid(x,y)
return xx, yy
if __name__ == "__main__":
import pdb; pdb.set_trace()
assert(np.all(GaussChebyshev_1D(4, 2, 2, 0) == GC(4, 2, 2, 0)))
assert(np.all(GaussChebyshev_1D(4, 2, 2, 5) == GC(4, 2, 2, 5)))
assert(np.all(GaussChebyshev_1D(10, 2, 2, 5) == GC(10, 2, 2, 5)))
assert(np.all(GaussChebyshev_1D(4, 1, 2, 0) == GC(4, 1, 2, 0)))
assert(np.all(GaussChebyshev_1D(6, 3, 9, 8) == GC(6, 3, 9, 8)))