Debian Codegraph @

src/meshzoo/_disk.py

raw
1 import numpy as np
2
3 from ._helpers import _compose_from_faces
4 from ._rectangle import rectangle_quad
5
6
7 def disk(p: int, n: int, offset: float = np.pi / 2):
8 x = np.linspace(offset, offset + 2 * np.pi, p, endpoint=False)
9 corners = np.vstack(
10 [
11 [[0.0, 0.0]],
12 np.array([np.cos(x), np.sin(x)]).T,
13 ]
14 )
15 faces = [(0, k + 1, k + 2) for k in range(p - 1)] + [(0, p, 1)]
16
17 def edge_adjust(edge, verts):
18 if 0 in edge:
19 return verts
20 dist = np.sqrt(np.einsum("ij,ij->i", verts, verts))
21 return verts / dist[:, None]
22
23 def face_adjust(face, bary, verts, corner_verts):
24 assert face[0] == 0
25 z = np.zeros_like(bary[1])
26 edge_proj_bary = np.array([z, bary[1], bary[2]]) / (bary[1] + bary[2])
27 edge_proj_cart = np.dot(corner_verts.T, edge_proj_bary).T
28 dist = np.sqrt(np.einsum("ij,ij->i", edge_proj_cart, edge_proj_cart))
29 return verts / dist[:, None]
30
31 return _compose_from_faces(
32 corners, faces, n, edge_adjust=edge_adjust, face_adjust=face_adjust
33 )
34
35
36 def disk_quad(n: int, cell_type: str = "quad4"):
37 a = 1 / np.sqrt(2)
38 nodes, cells = rectangle_quad(
39 np.linspace(-a, a, n + 1), np.linspace(-a, a, n + 1), cell_type
40 )
41
42 # Inflate the nodes towards the circle boundary.
43 # Inflate each point such that the 2-norm of the new point is the max-norm of the
44 # old.
45 alpha = np.max(np.abs(nodes), axis=1)
46 beta = np.linalg.norm(nodes, axis=1)
47 idx = beta > 1.0e-13
48 nodes[idx] = (nodes[idx].T * (alpha[idx] / beta[idx])).T
49
50 return nodes, cells