| 1 |
""" |
| 2 |
.. _warp_by_vector_eigenmodes_example: |
| 3 |
|
| 4 |
Display Eigenmodes of Vibration |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This example applies the :func:`warp_by_vector |
| 8 |
<pyvista.DataSetFilters.warp_by_vector>` filter to a cube whose eigenmodes have |
| 9 |
been computed using the Ritz method, as outlined in Visscher, William M., |
| 10 |
Albert Migliori, Thomas M. Bell, et Robert A. Reinert. "On the normal modes of |
| 11 |
free vibration of inhomogeneous and anisotropic elastic objects". The Journal |
| 12 |
of the Acoustical Society of America 90, n.4 (October 1991): 2154-62. |
| 13 |
https://asa.scitation.org/doi/10.1121/1.401643. |
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
# %% |
| 18 |
# First, let's solve the eigenvalue problem for a vibrating cube. We use |
| 19 |
# a crude approximation (by choosing a low max polynomial order) to get a fast |
| 20 |
# computation. |
| 21 |
from itertools import product |
| 22 |
|
| 23 |
import numpy as np |
| 24 |
import pyvista as pv |
| 25 |
from scipy.linalg import eigh |
| 26 |
|
| 27 |
|
| 28 |
def analytical_integral_rppd(p, q, r, *, a, b, c): |
| 29 |
"""Return the analytical value of the RPPD integral. |
| 30 |
|
| 31 |
This is the integral of x**p * y**q * z**r for (x, -a, a), (y, -b, b), (z, -c, c). |
| 32 |
""" |
| 33 |
if p < 0 or q < 0 or r < 0.0: |
| 34 |
return 0.0 |
| 35 |
else: |
| 36 |
return ( |
| 37 |
a ** (p + 1) |
| 38 |
* b ** (q + 1) |
| 39 |
* c ** (r + 1) |
| 40 |
* ((-1) ** p + 1) |
| 41 |
* ((-1) ** q + 1) |
| 42 |
* ((-1) ** r + 1) |
| 43 |
/ ((p + 1) * (q + 1) * (r + 1)) |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def make_cijkl_E_nu(E=200, nu=0.3): |
| 48 |
""" |
| 49 |
Make cijkl from E and nu. |
| 50 |
|
| 51 |
Default values for steel are: E=200 GPa, nu=0.3. |
| 52 |
""" |
| 53 |
lambd = E * nu / (1 + nu) / (1 - 2 * nu) |
| 54 |
mu = E / 2 / (1 + nu) |
| 55 |
cij = np.zeros((6, 6)) |
| 56 |
cij[(0, 1, 2), (0, 1, 2)] = lambd + 2 * mu |
| 57 |
cij[(0, 0, 1, 1, 2, 2), (1, 2, 0, 2, 0, 1)] = lambd |
| 58 |
cij[(3, 4, 5), (3, 4, 5)] = mu |
| 59 |
# check symmetry |
| 60 |
assert np.allclose(cij, cij.T) |
| 61 |
# convert to order 4 tensor |
| 62 |
coord_mapping = { |
| 63 |
(1, 1): 1, |
| 64 |
(2, 2): 2, |
| 65 |
(3, 3): 3, |
| 66 |
(2, 3): 4, |
| 67 |
(1, 3): 5, |
| 68 |
(1, 2): 6, |
| 69 |
(2, 1): 6, |
| 70 |
(3, 1): 5, |
| 71 |
(3, 2): 4, |
| 72 |
} |
| 73 |
|
| 74 |
cijkl = np.zeros((3, 3, 3, 3)) |
| 75 |
for i, j, k, l in product(range(3), repeat=4): |
| 76 |
u = coord_mapping[i + 1, j + 1] |
| 77 |
v = coord_mapping[k + 1, l + 1] |
| 78 |
cijkl[i, j, k, l] = cij[u - 1, v - 1] |
| 79 |
return cijkl, cij |
| 80 |
|
| 81 |
|
| 82 |
def get_first_N_above_thresh(*, N, freqs, thresh, decimals=3): |
| 83 |
"""Return first N unique frequencies with amplitude>thresh based on first decimals.""" |
| 84 |
unique_freqs, unique_indices = np.unique( |
| 85 |
np.round(freqs, decimals=decimals), return_index=True |
| 86 |
) |
| 87 |
nonzero = unique_freqs > thresh |
| 88 |
unique_freqs, unique_indices = unique_freqs[nonzero], unique_indices[nonzero] |
| 89 |
return unique_freqs[:N], unique_indices[:N] |
| 90 |
|
| 91 |
|
| 92 |
def assemble_mass_and_stiffness(*, N, F, geom_params, cijkl): |
| 93 |
""" |
| 94 |
Assemble the mass and stiffness matrix. |
| 95 |
|
| 96 |
It first builds an index of basis functions as a quadruplet of |
| 97 |
component and polynomial order for (x^p, y^q, z^r) of maximum order N. |
| 98 |
|
| 99 |
This routine only builds the symmetric part of the matrix to speed |
| 100 |
things up. |
| 101 |
""" |
| 102 |
# building coordinates |
| 103 |
triplets = [ |
| 104 |
(p, q, r) |
| 105 |
for p in range(N + 1) |
| 106 |
for q in range(N - p + 1) |
| 107 |
for r in range(N - p - q + 1) |
| 108 |
] |
| 109 |
assert len(triplets) == (N + 1) * (N + 2) * (N + 3) // 6 |
| 110 |
|
| 111 |
quadruplets = [] |
| 112 |
for i, triplet in product(range(3), triplets): |
| 113 |
quadruplets.append((i, *triplet)) |
| 114 |
assert len(quadruplets) == 3 * (N + 1) * (N + 2) * (N + 3) // 6 |
| 115 |
|
| 116 |
# assembling the mass and stiffness matrix in a single loop |
| 117 |
R = len(triplets) |
| 118 |
E = np.zeros((3 * R, 3 * R)) # the mass matrix |
| 119 |
G = np.zeros((3 * R, 3 * R)) # the stiffness matrix |
| 120 |
for index1, quad1 in enumerate(quadruplets): |
| 121 |
I, p1, q1, r1 = quad1 |
| 122 |
for index2, quad2 in enumerate(quadruplets[index1:]): |
| 123 |
index2 = index2 + index1 # noqa: PLW2901 |
| 124 |
J, p2, q2, r2 = quad2 |
| 125 |
G[index1, index2] = ( |
| 126 |
cijkl[I, 1 - 1, J, 1 - 1] |
| 127 |
* p1 |
| 128 |
* p2 |
| 129 |
* F(p1 + p2 - 2, q1 + q2, r1 + r2, **geom_params) |
| 130 |
+ cijkl[I, 1 - 1, J, 2 - 1] |
| 131 |
* p1 |
| 132 |
* q2 |
| 133 |
* F(p1 + p2 - 1, q1 + q2 - 1, r1 + r2, **geom_params) |
| 134 |
+ cijkl[I, 1 - 1, J, 3 - 1] |
| 135 |
* p1 |
| 136 |
* r2 |
| 137 |
* F(p1 + p2 - 1, q1 + q2, r1 + r2 - 1, **geom_params) |
| 138 |
+ cijkl[I, 2 - 1, J, 1 - 1] |
| 139 |
* q1 |
| 140 |
* p2 |
| 141 |
* F(p1 + p2 - 1, q1 + q2 - 1, r1 + r2, **geom_params) |
| 142 |
+ cijkl[I, 2 - 1, J, 2 - 1] |
| 143 |
* q1 |
| 144 |
* q2 |
| 145 |
* F(p1 + p2, q1 + q2 - 2, r1 + r2, **geom_params) |
| 146 |
+ cijkl[I, 2 - 1, J, 3 - 1] |
| 147 |
* q1 |
| 148 |
* r2 |
| 149 |
* F(p1 + p2, q1 + q2 - 1, r1 + r2 - 1, **geom_params) |
| 150 |
+ cijkl[I, 3 - 1, J, 1 - 1] |
| 151 |
* r1 |
| 152 |
* p2 |
| 153 |
* F(p1 + p2 - 1, q1 + q2, r1 + r2 - 1, **geom_params) |
| 154 |
+ cijkl[I, 3 - 1, J, 2 - 1] |
| 155 |
* r1 |
| 156 |
* q2 |
| 157 |
* F(p1 + p2, q1 + q2 - 1, r1 + r2 - 1, **geom_params) |
| 158 |
+ cijkl[I, 3 - 1, J, 3 - 1] |
| 159 |
* r1 |
| 160 |
* r2 |
| 161 |
* F(p1 + p2, q1 + q2, r1 + r2 - 2, **geom_params) |
| 162 |
) |
| 163 |
G[index2, index1] = G[index1, index2] # since stiffness matrix is symmetric |
| 164 |
if I == J: |
| 165 |
E[index1, index2] = F(p1 + p2, q1 + q2, r1 + r2, **geom_params) |
| 166 |
E[index2, index1] = E[index1, index2] # since mass matrix is symmetric |
| 167 |
return E, G, quadruplets |
| 168 |
|
| 169 |
|
| 170 |
N = 8 # maximum order of x^p y^q z^r polynomials |
| 171 |
rho = 8.0 # g/cm^3 |
| 172 |
|
| 173 |
# Due to a cube's symmetry there will be repeated pairs or triplets for certain |
| 174 |
# eigenmodes. Here we introduce slight asymmetry to ensure that we select the |
| 175 |
# same first "repeated" mode for the symmetric mode sets. |
| 176 |
l1, l2, l3 = 0.200001, 0.200002, 0.200003 # all in cm |
| 177 |
geometry_parameters = {'a': l1 / 2.0, 'b': l2 / 2.0, 'c': l3 / 2.0} |
| 178 |
cijkl, cij = make_cijkl_E_nu(200, 0.3) # Gpa, without unit |
| 179 |
E, G, quadruplets = assemble_mass_and_stiffness( |
| 180 |
N=N, |
| 181 |
F=analytical_integral_rppd, |
| 182 |
geom_params=geometry_parameters, |
| 183 |
cijkl=cijkl, |
| 184 |
) |
| 185 |
|
| 186 |
# solving the eigenvalue problem using symmetric solver |
| 187 |
w, vr = eigh(a=G, b=E) |
| 188 |
omegas = np.sqrt(np.abs(w) / rho) * 1e5 # convert back to Hz |
| 189 |
freqs = omegas / (2 * np.pi) |
| 190 |
# expected values from (Bernard 2014, pl.14), |
| 191 |
# error depends on polynomial order ``N`` |
| 192 |
expected_freqs_kHz = np.array( # noqa: N816 |
| 193 |
[704.8, 949.0, 965.2, 1096.3, 1128.4, 1182.8, 1338.9, 1360.9] |
| 194 |
) |
| 195 |
computed_freqs_kHz, mode_indices = get_first_N_above_thresh( # noqa: N816 |
| 196 |
N=8, freqs=freqs / 1e3, thresh=1, decimals=1 |
| 197 |
) |
| 198 |
print('found the following first unique eigenfrequencies:') |
| 199 |
for ind, (freq1, freq2) in enumerate( |
| 200 |
zip(computed_freqs_kHz, expected_freqs_kHz, strict=True) |
| 201 |
): |
| 202 |
error = np.abs(freq2 - freq1) / freq1 * 100.0 |
| 203 |
print( |
| 204 |
f'freq. {ind + 1:1}: {freq1:8.1f} kHz, ' |
| 205 |
f'expected: {freq2:8.1f} kHz, ' |
| 206 |
f'error: {error:.2f} %' |
| 207 |
) |
| 208 |
|
| 209 |
# %% |
| 210 |
# Now, let's display a mode on a mesh of the cube. |
| 211 |
|
| 212 |
# Create the 3D NumPy array of spatially referenced data |
| 213 |
# (nx by ny by nz) |
| 214 |
nx, ny, nz = 30, 31, 32 |
| 215 |
|
| 216 |
x = np.linspace(-l1 / 2.0, l1 / 2.0, nx) |
| 217 |
y = np.linspace(-l2 / 2.0, l2 / 2.0, ny) |
| 218 |
x, y = np.meshgrid(x, y) |
| 219 |
z = np.zeros_like(x) + l3 / 2.0 |
| 220 |
grid = pv.StructuredGrid(x, y, z) |
| 221 |
|
| 222 |
slices = [] |
| 223 |
for zz in np.linspace(-l3 / 2.0, l3 / 2.0, nz)[::-1]: |
| 224 |
slice_ = grid.points.copy() |
| 225 |
slice_[:, -1] = zz |
| 226 |
slices.append(slice_) |
| 227 |
|
| 228 |
vol = pv.StructuredGrid() |
| 229 |
vol.points = np.vstack(slices) |
| 230 |
vol.dimensions = [*grid.dimensions[0:2], nz] |
| 231 |
|
| 232 |
for i, mode_index in enumerate(mode_indices): |
| 233 |
eigenvector = vr[:, mode_index] |
| 234 |
displacement_points = np.zeros_like(vol.points) |
| 235 |
|
| 236 |
for weight, (component, p, q, r) in zip(eigenvector, quadruplets, strict=True): |
| 237 |
displacement_points[:, component] += ( |
| 238 |
weight * vol.points[:, 0] ** p * vol.points[:, 1] ** q * vol.points[:, 2] ** r |
| 239 |
) |
| 240 |
|
| 241 |
# normalize magnitude |
| 242 |
displacement_magnitude = np.linalg.norm(displacement_points, axis=1) |
| 243 |
max_magnitude = np.max(displacement_magnitude) |
| 244 |
if max_magnitude > 0.0: |
| 245 |
displacement_points /= max_magnitude |
| 246 |
|
| 247 |
# for repeatability, ensure that the first point always has positive displacement |
| 248 |
if displacement_points[0, 0] < 0.0: |
| 249 |
displacement_points = -displacement_points |
| 250 |
|
| 251 |
vol[f'eigenmode_{i:02}'] = displacement_points |
| 252 |
|
| 253 |
warpby = 'eigenmode_00' |
| 254 |
warped = vol.warp_by_vector(warpby, factor=0.04) |
| 255 |
warped.translate([-1.5 * l1, 0.0, 0.0], inplace=True) |
| 256 |
pl = pv.Plotter() |
| 257 |
pl.add_mesh( |
| 258 |
vol, style='wireframe', scalars=warpby, show_scalar_bar=False, clim=(0.0, 1.0) |
| 259 |
) |
| 260 |
pl.add_mesh(warped, scalars=warpby, clim=(0.0, 1.0)) |
| 261 |
pl.show() |
| 262 |
|
| 263 |
# %% |
| 264 |
# Finally, let's make a gallery of the first 8 unique eigenmodes. |
| 265 |
|
| 266 |
|
| 267 |
pl = pv.Plotter(shape=(2, 4)) |
| 268 |
for i, j in product(range(2), range(4)): |
| 269 |
pl.subplot(i, j) |
| 270 |
current_index = 4 * i + j |
| 271 |
vector = f'eigenmode_{current_index:02}' |
| 272 |
pl.add_text( |
| 273 |
f'mode {current_index}, freq. {computed_freqs_kHz[current_index]:.1f} kHz', |
| 274 |
font_size=10, |
| 275 |
) |
| 276 |
pl.add_mesh( |
| 277 |
vol.warp_by_vector(vector, factor=0.03), |
| 278 |
scalars=vector, |
| 279 |
show_scalar_bar=False, |
| 280 |
clim=(0.0, 1.0), |
| 281 |
) |
| 282 |
pl.show() |