From 852a2fe6196ea717ee106a1cfd9e8473898bc61e Mon Sep 17 00:00:00 2001 From: Seung Hyun Kim Date: Wed, 29 May 2024 00:29:27 -0500 Subject: [PATCH 1/3] fix: replace inefficient @ operator --- .../parallel_connection.py | 84 +++++++++++++------ 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/elastica/experimental/connection_contact_joint/parallel_connection.py b/elastica/experimental/connection_contact_joint/parallel_connection.py index fc0340a2..6044aff0 100644 --- a/elastica/experimental/connection_contact_joint/parallel_connection.py +++ b/elastica/experimental/connection_contact_joint/parallel_connection.py @@ -7,6 +7,8 @@ from elastica._linalg import ( _batch_norm, _batch_matvec, + _batch_cross, + _batch_matrix_transpose, ) @@ -149,13 +151,13 @@ def _apply_forces( rod_two_external_forces, ): - rod_one_to_rod_two_connection_vec = ( - rod_one_director_collection[:, :, index_one].T - @ rod_one_direction_vec_in_material_frame + rod_one_to_rod_two_connection_vec = _batch_matvec( + _batch_matrix_transpose(rod_one_director_collection[:, :, index_one]), + rod_one_direction_vec_in_material_frame, ) - rod_two_to_rod_one_connection_vec = ( - rod_two_director_collection[:, :, index_two].T - @ rod_two_direction_vec_in_material_frame + rod_two_to_rod_one_connection_vec = _batch_matvec( + _batch_matrix_transpose(rod_two_director_collection[:, :, index_two]), + rod_two_direction_vec_in_material_frame, ) # Compute element positions @@ -212,33 +214,42 @@ def _apply_forces( # we apply a repulsive force. center_distance = rod_two_element_position - rod_one_element_position center_distance_unit_vec = center_distance / np.linalg.norm(center_distance) - penetration = np.linalg.norm(center_distance) - ( + penetration_strain = np.linalg.norm(center_distance) - ( rod_one_radius[index_one] + offset_rod_one + rod_two_radius[index_two] + offset_rod_two ) - round(penetration, 12) + np.round_(penetration_strain, 12, penetration_strain) # Contact present only if rods penetrate to each other - if penetration < 0: - # Hertzian contact - contact_force = ( - -k_repulsive * np.abs(penetration) ** (1.5) * center_distance_unit_vec - ) - else: - contact_force = np.zeros( - 3, - ) + idx_penetrate = np.where(penetration_strain < 0)[0] + k_contact = np.zeros(index_one.shape[0]) + k_contact_temp = -k_repulsive * np.abs(penetration_strain) ** (1.5) + k_contact[idx_penetrate] += k_contact_temp[idx_penetrate] + contact_force = k_contact * center_distance_unit_vec # Add contact forces total_force += contact_force # Re-distribute forces from elements to nodes. - rod_one_external_forces[..., index_one] += 0.5 * total_force - rod_one_external_forces[..., index_one + 1] += 0.5 * total_force - rod_two_external_forces[..., index_two] -= 0.5 * total_force - rod_two_external_forces[..., index_two + 1] -= 0.5 * total_force + block_size = index_one.shape[0] + for k in range(block_size): + rod_one_external_forces[0, index_one[k]] += 0.5 * total_force[0, k] + rod_one_external_forces[1, index_one[k]] += 0.5 * total_force[1, k] + rod_one_external_forces[2, index_one[k]] += 0.5 * total_force[2, k] + + rod_one_external_forces[0, index_one[k] + 1] += 0.5 * total_force[0, k] + rod_one_external_forces[1, index_one[k] + 1] += 0.5 * total_force[1, k] + rod_one_external_forces[2, index_one[k] + 1] += 0.5 * total_force[2, k] + + rod_two_external_forces[0, index_two[k]] -= 0.5 * total_force[0, k] + rod_two_external_forces[1, index_two[k]] -= 0.5 * total_force[1, k] + rod_two_external_forces[2, index_two[k]] -= 0.5 * total_force[2, k] + + rod_two_external_forces[0, index_two[k] + 1] -= 0.5 * total_force[0, k] + rod_two_external_forces[1, index_two[k] + 1] -= 0.5 * total_force[1, k] + rod_two_external_forces[2, index_two[k] + 1] -= 0.5 * total_force[2, k] return ( rod_one_rd2, @@ -278,12 +289,31 @@ def _apply_torques( torque_on_rod_one = np.cross(rod_one_rd2, spring_force) torque_on_rod_two = np.cross(rod_two_rd2, -spring_force) - torque_on_rod_one_material_frame = ( - rod_one_director_collection[:, :, index_one] @ torque_on_rod_one + torque_on_rod_one_material_frame = _batch_matvec( + rod_one_director_collection[:, :, index_one], torque_on_rod_one ) - torque_on_rod_two_material_frame = ( - rod_two_director_collection[:, :, index_two] @ torque_on_rod_two + torque_on_rod_two_material_frame = _batch_matvec( + rod_two_director_collection[:, :, index_two], torque_on_rod_two ) - rod_one_external_torques[..., index_one] += torque_on_rod_one_material_frame - rod_two_external_torques[..., index_two] += torque_on_rod_two_material_frame + blocksize = index_one.shape[0] + for k in range(blocksize): + rod_one_external_torques[ + 0, index_one[k] + ] += torque_on_rod_one_material_frame[0, k] + rod_one_external_torques[ + 1, index_one[k] + ] += torque_on_rod_one_material_frame[1, k] + rod_one_external_torques[ + 2, index_one[k] + ] += torque_on_rod_one_material_frame[2, k] + + rod_two_external_torques[ + 0, index_two[k] + ] += torque_on_rod_two_material_frame[0, k] + rod_two_external_torques[ + 1, index_two[k] + ] += torque_on_rod_two_material_frame[1, k] + rod_two_external_torques[ + 2, index_two[k] + ] += torque_on_rod_two_material_frame[2, k] From b0bb192d75e2dfd1e256a1fb41d19d55a0d6a7da Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:21:02 +0000 Subject: [PATCH 2/3] Bump sphinx-book-theme from 1.1.2 to 1.1.3 Bumps [sphinx-book-theme](https://github.com/executablebooks/sphinx-book-theme) from 1.1.2 to 1.1.3. - [Release notes](https://github.com/executablebooks/sphinx-book-theme/releases) - [Changelog](https://github.com/executablebooks/sphinx-book-theme/blob/master/CHANGELOG.md) - [Commits](https://github.com/executablebooks/sphinx-book-theme/compare/v1.1.2...v1.1.3) --- updated-dependencies: - dependency-name: sphinx-book-theme dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- poetry.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/poetry.lock b/poetry.lock index 089351af..a848f5da 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1749,23 +1749,23 @@ type-comment = ["typed-ast (>=1.5.4)"] [[package]] name = "sphinx-book-theme" -version = "1.1.2" +version = "1.1.3" description = "A clean book theme for scientific explanations and documentation with Sphinx" optional = true python-versions = ">=3.9" files = [ - {file = "sphinx_book_theme-1.1.2-py3-none-any.whl", hash = "sha256:cee744466fde48f50302b851291b208aa67e726ca31b7a3bfb9b6e6a145663e0"}, - {file = "sphinx_book_theme-1.1.2.tar.gz", hash = "sha256:7f3abcd146ca82e6f39d6db53711102b1c1d328d12f65e3e47ad9bf842614a49"}, + {file = "sphinx_book_theme-1.1.3-py3-none-any.whl", hash = "sha256:a554a9a7ac3881979a87a2b10f633aa2a5706e72218a10f71be38b3c9e831ae9"}, + {file = "sphinx_book_theme-1.1.3.tar.gz", hash = "sha256:1f25483b1846cb3d353a6bc61b3b45b031f4acf845665d7da90e01ae0aef5b4d"}, ] [package.dependencies] -pydata-sphinx-theme = ">=0.14" +pydata-sphinx-theme = ">=0.15.2" sphinx = ">=5" [package.extras] code-style = ["pre-commit"] doc = ["ablog", "folium", "ipywidgets", "matplotlib", "myst-nb", "nbclient", "numpy", "numpydoc", "pandas", "plotly", "sphinx-copybutton", "sphinx-design", "sphinx-examples", "sphinx-tabs", "sphinx-thebe", "sphinx-togglebutton", "sphinxcontrib-bibtex", "sphinxcontrib-youtube", "sphinxext-opengraph"] -test = ["beautifulsoup4", "coverage", "myst-nb", "pytest", "pytest-cov", "pytest-regressions", "sphinx_thebe"] +test = ["beautifulsoup4", "coverage", "defusedxml", "myst-nb", "pytest", "pytest-cov", "pytest-regressions", "sphinx_thebe"] [[package]] name = "sphinxcontrib-applehelp" From a6f92ce123a21fe644a1bf99947cc1ecdb3c5738 Mon Sep 17 00:00:00 2001 From: Seung Hyun Kim Date: Sat, 15 Jun 2024 00:19:42 +0900 Subject: [PATCH 3/3] fix: experimental interaction tool with deprecated functions --- elastica/experimental/interaction.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/elastica/experimental/interaction.py b/elastica/experimental/interaction.py index 25368897..cd2c8626 100644 --- a/elastica/experimental/interaction.py +++ b/elastica/experimental/interaction.py @@ -3,11 +3,11 @@ import numpy as np from elastica.external_forces import NoForces -from elastica.interaction import ( - find_slipping_elements, - apply_normal_force_numba_rigid_body, - InteractionPlaneRigidBody, +from elastica.contact_utils import ( + _find_slipping_elements, ) +from elastica.contact_functions import _calculate_contact_forces_cylinder_plane +from elastica.interaction import InteractionPlaneRigidBody from numba import njit from elastica._linalg import ( @@ -66,7 +66,7 @@ def apply_forces(self, system, time=0.0): ) -@njit(cache=True) +@njit(cache=True) # type: ignore def anisotropic_friction_numba_rigid_body( plane_origin, plane_normal, @@ -93,7 +93,7 @@ def anisotropic_friction_numba_rigid_body( ( plane_response_force_mag, no_contact_point_idx, - ) = apply_normal_force_numba_rigid_body( + ) = _calculate_contact_forces_cylinder_plane( plane_origin, plane_normal, surface_tol, @@ -122,7 +122,7 @@ def anisotropic_friction_numba_rigid_body( + kinetic_mu_backward * (1 - velocity_sign_along_axial_direction) ) # Call slip function to check if elements slipping or not - slip_function_along_axial_direction = find_slipping_elements( + slip_function_along_axial_direction = _find_slipping_elements( velocity_along_axial_direction, slip_velocity_tol ) kinetic_friction_force_along_axial_direction = -( @@ -151,7 +151,7 @@ def anisotropic_friction_numba_rigid_body( + kinetic_mu_backward * (1 - velocity_sign_along_binormal_direction) ) # Call slip function to check if elements slipping or not - slip_function_along_binormal_direction = find_slipping_elements( + slip_function_along_binormal_direction = _find_slipping_elements( velocity_along_binormal_direction, slip_velocity_tol ) kinetic_friction_force_along_binormal_direction = -(