| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
from numpy.typing import ArrayLike |
| 5 |
|
| 6 |
|
| 7 |
# backwards compatibility |
| 8 |
def rectangle( |
| 9 |
x0: float, |
| 10 |
x1: float, |
| 11 |
y0: float, |
| 12 |
y1: float, |
| 13 |
nx: int, |
| 14 |
ny: int, |
| 15 |
): |
| 16 |
x_range = np.linspace(x0, x1, nx + 1) |
| 17 |
y_range = np.linspace(y0, y1, ny + 1) |
| 18 |
return rectangle_tri(x_range, y_range) |
| 19 |
|
| 20 |
|
| 21 |
def rectangle_quad(x_range: ArrayLike, y_range: ArrayLike, cell_type: str = "quad4"): |
| 22 |
x_range = np.asarray(x_range) |
| 23 |
y_range = np.asarray(y_range) |
| 24 |
|
| 25 |
nx = len(x_range) |
| 26 |
ny = len(y_range) |
| 27 |
|
| 28 |
if cell_type == "quad4": |
| 29 |
points = np.array(np.meshgrid(x_range, y_range)).reshape(2, -1).T |
| 30 |
a = np.arange(nx * ny).reshape(ny, nx).T |
| 31 |
|
| 32 |
cells = ( |
| 33 |
np.array([a[:-1, :-1], a[1:, :-1], a[1:, 1:], a[:-1, 1:]]).reshape(4, -1).T |
| 34 |
) |
| 35 |
elif cell_type == "quad8": |
| 36 |
k = 0 |
| 37 |
points_corner = np.array(np.meshgrid(x_range, y_range)).reshape(2, -1).T |
| 38 |
a_corner = np.arange(nx * ny).reshape(ny, nx).T |
| 39 |
k += a_corner.size |
| 40 |
|
| 41 |
x_mid = (x_range[:-1] + x_range[1:]) / 2 |
| 42 |
points_xmid = np.array(np.meshgrid(x_mid, y_range)).reshape(2, -1).T |
| 43 |
a_xmid = k + np.arange((nx - 1) * ny).reshape(ny, nx - 1).T |
| 44 |
k += a_xmid.size |
| 45 |
|
| 46 |
y_mid = (y_range[:-1] + y_range[1:]) / 2 |
| 47 |
points_ymid = np.array(np.meshgrid(x_range, y_mid)).reshape(2, -1).T |
| 48 |
a_ymid = k + np.arange(nx * (ny - 1)).reshape(ny - 1, nx).T |
| 49 |
|
| 50 |
points = np.row_stack([points_corner, points_xmid, points_ymid]) |
| 51 |
|
| 52 |
cells = ( |
| 53 |
np.array( |
| 54 |
[ |
| 55 |
a_corner[:-1, :-1], |
| 56 |
a_corner[1:, :-1], |
| 57 |
a_corner[1:, 1:], |
| 58 |
a_corner[:-1, 1:], |
| 59 |
a_xmid[:, :-1], |
| 60 |
a_ymid[1:, :], |
| 61 |
a_xmid[:, 1:], |
| 62 |
a_ymid[:-1, :], |
| 63 |
] |
| 64 |
) |
| 65 |
.reshape(8, -1) |
| 66 |
.T |
| 67 |
) |
| 68 |
else: |
| 69 |
assert cell_type == "quad9" |
| 70 |
k = 0 |
| 71 |
points_corner = np.array(np.meshgrid(x_range, y_range)).reshape(2, -1).T |
| 72 |
a_corner = np.arange(nx * ny).reshape(ny, nx).T |
| 73 |
k += a_corner.size |
| 74 |
|
| 75 |
x_mid = (x_range[:-1] + x_range[1:]) / 2 |
| 76 |
points_xmid = np.array(np.meshgrid(x_mid, y_range)).reshape(2, -1).T |
| 77 |
a_xmid = k + np.arange((nx - 1) * ny).reshape(ny, nx - 1).T |
| 78 |
k += a_xmid.size |
| 79 |
|
| 80 |
y_mid = (y_range[:-1] + y_range[1:]) / 2 |
| 81 |
points_ymid = np.array(np.meshgrid(x_range, y_mid)).reshape(2, -1).T |
| 82 |
a_ymid = k + np.arange(nx * (ny - 1)).reshape(ny - 1, nx).T |
| 83 |
k += a_ymid.size |
| 84 |
|
| 85 |
points_center = np.array(np.meshgrid(x_mid, y_mid)).reshape(2, -1).T |
| 86 |
a_center = k + np.arange((nx - 1) * (ny - 1)).reshape(ny - 1, nx - 1).T |
| 87 |
|
| 88 |
points = np.row_stack([points_corner, points_xmid, points_ymid, points_center]) |
| 89 |
|
| 90 |
cells = ( |
| 91 |
np.array( |
| 92 |
[ |
| 93 |
a_corner[:-1, :-1], |
| 94 |
a_corner[1:, :-1], |
| 95 |
a_corner[1:, 1:], |
| 96 |
a_corner[:-1, 1:], |
| 97 |
a_xmid[:, :-1], |
| 98 |
a_ymid[1:, :], |
| 99 |
a_xmid[:, 1:], |
| 100 |
a_ymid[:-1, :], |
| 101 |
a_center, |
| 102 |
] |
| 103 |
) |
| 104 |
.reshape(9, -1) |
| 105 |
.T |
| 106 |
) |
| 107 |
|
| 108 |
return points, cells |
| 109 |
|
| 110 |
|
| 111 |
def rectangle_tri(x_range: ArrayLike, y_range: ArrayLike, variant: str = "zigzag"): |
| 112 |
x_range = np.asarray(x_range) |
| 113 |
y_range = np.asarray(y_range) |
| 114 |
|
| 115 |
nx = len(x_range) |
| 116 |
ny = len(y_range) |
| 117 |
|
| 118 |
# Create the vertices. |
| 119 |
points = np.array(np.meshgrid(x_range, y_range)).reshape(2, -1).T |
| 120 |
|
| 121 |
a = np.arange(nx * ny).reshape(ny, nx).T |
| 122 |
|
| 123 |
# indices of corners |
| 124 |
# |
| 125 |
# c[3] c[2] |
| 126 |
# _____ |
| 127 |
# | | |
| 128 |
# |___| |
| 129 |
# |
| 130 |
# c[0] c[1] |
| 131 |
# |
| 132 |
c = [ |
| 133 |
a[:-1, :-1], |
| 134 |
a[1:, :-1], |
| 135 |
a[1:, 1:], |
| 136 |
a[:-1, 1:], |
| 137 |
] |
| 138 |
|
| 139 |
if variant == "up": |
| 140 |
cells = [ |
| 141 |
[c[0], c[1], c[2]], |
| 142 |
[c[0], c[2], c[3]], |
| 143 |
] |
| 144 |
elif variant == "down": |
| 145 |
cells = [ |
| 146 |
[c[0], c[1], c[3]], |
| 147 |
[c[1], c[2], c[3]], |
| 148 |
] |
| 149 |
elif variant == "zigzag": |
| 150 |
# https://stackoverflow.com/a/68550456/353337 |
| 151 |
idx = np.ones((nx - 1, ny - 1), dtype=bool) |
| 152 |
idx[1::2, ::2] = False |
| 153 |
idx[::2, 1::2] = False |
| 154 |
cells = [ |
| 155 |
# up |
| 156 |
[c[0][idx], c[1][idx], c[2][idx]], |
| 157 |
[c[0][idx], c[2][idx], c[3][idx]], |
| 158 |
# down |
| 159 |
[c[0][~idx], c[1][~idx], c[3][~idx]], |
| 160 |
[c[1][~idx], c[2][~idx], c[3][~idx]], |
| 161 |
] |
| 162 |
else: |
| 163 |
assert variant == "center" |
| 164 |
idx = np.ones(((nx - 1), (ny - 1)), dtype=bool) |
| 165 |
idx[: (nx - 1) // 2, : (ny - 1) // 2] = False |
| 166 |
idx[(nx - 1) // 2 :, (ny - 1) // 2 :] = False |
| 167 |
|
| 168 |
cells = [ |
| 169 |
# up |
| 170 |
[c[0][idx], c[1][idx], c[2][idx]], |
| 171 |
[c[0][idx], c[2][idx], c[3][idx]], |
| 172 |
# down |
| 173 |
[c[0][~idx], c[1][~idx], c[3][~idx]], |
| 174 |
[c[1][~idx], c[2][~idx], c[3][~idx]], |
| 175 |
] |
| 176 |
|
| 177 |
cells = np.column_stack([np.array(c).reshape(3, -1) for c in cells]).T |
| 178 |
|
| 179 |
return points, cells |