forked from GazzolaLab/PyElastica
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructured mesh_rigid_body.py and test_mesh_rigid_body.py
- Loading branch information
Showing
2 changed files
with
291 additions
and
1,238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,17 @@ def __init__( | |
self.density = density | ||
self.volume = volume | ||
self.mass = np.array([self.volume * self.density]) | ||
self.mass_second_moment_of_inertia = mass_second_moment_of_inertia.reshape( | ||
MaxDimension.value(), MaxDimension.value(), 1 | ||
) | ||
|
||
self.inv_mass_second_moment_of_inertia = np.linalg.inv( | ||
mass_second_moment_of_inertia | ||
).reshape(MaxDimension.value(), MaxDimension.value(), 1) | ||
normal = np.array([1.0, 0.0, 0.0]).reshape(3, 1) | ||
tangents = np.array([0.0, 0.0, 1.0]).reshape(3, 1) | ||
binormal = _batch_cross(tangents, normal) | ||
|
||
# initialize material frame to be the same as lab frame | ||
self.director_collection = np.zeros( | ||
(MaxDimension.value(), MaxDimension.value(), 1) | ||
|
@@ -48,51 +56,46 @@ def __init__( | |
self.n_faces = mesh.faces.shape[-1] | ||
self.face_centers = np.array(mesh.face_centers.copy()) | ||
self.face_normals = np.array(mesh.face_normals.copy()) | ||
self.distance_to_face_centers = _batch_norm( | ||
|
||
# since material frame is the same as lab frame initially, no need to convert this from lab to material | ||
self.distance_to_face_centers_from_center_of_mass = _batch_norm( | ||
self.face_centers - center_of_mass.reshape(3, 1) | ||
) | ||
self.direction_to_face_centers = ( | ||
self.direction_to_face_centers_from_center_of_mass_in_material_frame = ( | ||
self.face_centers - center_of_mass.reshape(3, 1) | ||
) / self.distance_to_face_centers | ||
self.distance_to_faces = np.zeros((MaxDimension.value(), self.n_faces)) | ||
self.direction_to_faces = np.zeros( | ||
) / self.distance_to_face_centers_from_center_of_mass | ||
self.distance_to_faces_from_center_of_mass = np.zeros( | ||
(MaxDimension.value(), self.n_faces) | ||
) | ||
self.direction_to_faces_from_center_of_mass_in_material_frame = np.zeros( | ||
(MaxDimension.value(), MaxDimension.value(), self.n_faces) | ||
) | ||
for i in range(MaxDimension.value()): | ||
for k in range(self.n_faces): | ||
self.distance_to_faces[i, k] = np.linalg.norm( | ||
self.distance_to_faces_from_center_of_mass[i, k] = np.linalg.norm( | ||
self.faces[:, i, k] - center_of_mass | ||
) | ||
self.direction_to_faces[:, i, k] = ( | ||
self.direction_to_faces_from_center_of_mass_in_material_frame[ | ||
:, i, k | ||
] = ( | ||
self.faces[:, i, k] - center_of_mass | ||
) / self.distance_to_faces[i, k] | ||
) / self.distance_to_faces_from_center_of_mass[ | ||
i, k | ||
] | ||
|
||
self.mass_second_moment_of_inertia = mass_second_moment_of_inertia.reshape( | ||
MaxDimension.value(), MaxDimension.value(), 1 | ||
self.face_normals_in_material_frame = np.zeros( | ||
(MaxDimension.value(), self.n_faces) | ||
) | ||
self.face_normals_in_material_frame = self.face_normals.copy() | ||
|
||
self.inv_mass_second_moment_of_inertia = np.linalg.inv( | ||
mass_second_moment_of_inertia | ||
).reshape(MaxDimension.value(), MaxDimension.value(), 1) | ||
|
||
# position is at the center | ||
# position is at the center of mass | ||
self.position_collection = np.zeros((MaxDimension.value(), 1)) | ||
self.position_collection[:, 0] = center_of_mass | ||
|
||
self.velocity_collection = np.zeros((MaxDimension.value(), 1)) | ||
self.omega_collection = np.zeros((MaxDimension.value(), 1)) | ||
self.acceleration_collection = np.zeros((MaxDimension.value(), 1)) | ||
self.alpha_collection = np.zeros((MaxDimension.value(), 1)) | ||
self.face_normals_lagrangian = np.zeros((MaxDimension.value(), self.n_faces)) | ||
|
||
# self.face_normals_lagrangian = self.director_collection[...,0][email protected]_normals | ||
# for i in range(3): | ||
# for j in range(3): | ||
# for k in range(self.n_faces): | ||
# self.face_normals_lagrangian[i, k] += ( | ||
# self.director_collection[j, i, 0] * self.face_normals[j, k] | ||
# ) | ||
self.face_normals_lagrangian = self.face_normals.copy() | ||
|
||
self.external_forces = np.zeros((MaxDimension.value())).reshape( | ||
MaxDimension.value(), 1 | ||
|
@@ -106,22 +109,22 @@ def update_faces(self): | |
self.director_collection, | ||
self.faces, | ||
self.position_collection, | ||
self.distance_to_faces, | ||
self.direction_to_faces, | ||
self.distance_to_faces_from_center_of_mass, | ||
self.direction_to_faces_from_center_of_mass_in_material_frame, | ||
self.n_faces, | ||
) | ||
_update_face_centers( | ||
self.director_collection, | ||
self.face_centers, | ||
self.position_collection, | ||
self.distance_to_face_centers, | ||
self.direction_to_face_centers, | ||
self.distance_to_face_centers_from_center_of_mass, | ||
self.direction_to_face_centers_from_center_of_mass_in_material_frame, | ||
self.n_faces, | ||
) | ||
_update_face_normals( | ||
self.director_collection, | ||
self.face_normals, | ||
self.face_normals_lagrangian, | ||
self.face_normals_in_material_frame, | ||
self.n_faces, | ||
) | ||
|
||
|
@@ -131,8 +134,8 @@ def _update_face_centers( | |
director_collection, | ||
face_centers, | ||
center_of_mass, | ||
distance_to_face_centers, | ||
direction_to_face_centers, | ||
distance_to_face_centers_from_center_of_mass, | ||
direction_to_face_centers_from_center_of_mass_in_material_frame, | ||
n_faces, | ||
): | ||
face_centers[:] = np.zeros((3, n_faces)) | ||
|
@@ -141,9 +144,11 @@ def _update_face_centers( | |
face_centers[i, k] += center_of_mass[i, 0] | ||
for j in range(3): | ||
face_centers[i, k] += ( | ||
distance_to_face_centers[i] | ||
distance_to_face_centers_from_center_of_mass[i] | ||
* director_collection[i, j, 0] | ||
* direction_to_face_centers[j, k] | ||
* direction_to_face_centers_from_center_of_mass_in_material_frame[ | ||
j, k | ||
] | ||
) | ||
|
||
|
||
|
@@ -152,8 +157,8 @@ def _update_faces( | |
director_collection, | ||
faces, | ||
center_of_mass, | ||
distance_to_faces, | ||
direction_to_faces, | ||
distance_to_faces_from_center_of_mass, | ||
direction_to_faces_from_center_of_mass_in_material_frame, | ||
n_faces, | ||
): | ||
faces[:] = np.zeros((3, 3, n_faces)) # dim,vertices,faces | ||
|
@@ -163,17 +168,19 @@ def _update_faces( | |
faces[i, m, k] += center_of_mass[i, 0] | ||
for j in range(3): | ||
faces[i, m, k] += ( | ||
distance_to_faces[m, k] | ||
distance_to_faces_from_center_of_mass[m, k] | ||
* director_collection[i, j, 0] | ||
* direction_to_faces[j, m, k] | ||
* direction_to_faces_from_center_of_mass_in_material_frame[ | ||
j, m, k | ||
] | ||
) | ||
|
||
|
||
@numba.njit(cache=True) | ||
def _update_face_normals( | ||
director_collection, | ||
face_normals, | ||
face_normals_lagrangian, | ||
face_normals_in_material_frame, | ||
n_faces, | ||
): | ||
|
||
|
@@ -182,5 +189,5 @@ def _update_face_normals( | |
for j in range(3): | ||
for k in range(n_faces): | ||
face_normals[i, k] += ( | ||
director_collection[i, j, 0] * face_normals_lagrangian[j, k] | ||
director_collection[i, j, 0] * face_normals_in_material_frame[j, k] | ||
) |
Oops, something went wrong.