Skip to content

Commit

Permalink
type: feature API np.floating -> float
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Jun 25, 2024
1 parent 256d85a commit 210d172
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 111 deletions.
4 changes: 2 additions & 2 deletions elastica/_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from numpy import zeros, empty
from numpy.typing import NDArray
import numba
from numba import njit, float64
from numba import njit
from elastica.reset_functions_for_block_structure._reset_ghost_vector_or_scalar import (
_reset_vector_ghost,
)
Expand All @@ -22,7 +22,7 @@ def _get_zero_array(dim: int, ndim: int) -> Union[float, NDArray[np.floating], N


@njit(cache=True) # type: ignore
def _trapezoidal(array_collection: NDArray[np.float64]) -> NDArray[np.float64]:
def _trapezoidal(array_collection: NDArray[np.floating]) -> NDArray[np.floating]:
"""
Simple trapezoidal quadrature rule with zero at end-points, in a dimension agnostic way
Expand Down
27 changes: 9 additions & 18 deletions elastica/boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ def constrain_rates(
pass


class FreeRod(FreeBC):
# Please clear this part beyond version 0.3.0
"""Deprecated 0.2.1: Same implementation as FreeBC"""
warnings.warn(
"FreeRod is deprecated and renamed to FreeBC. The deprecated name will be removed in the future.",
DeprecationWarning,
)


class OneEndFixedBC(ConstraintBase):
"""
This boundary condition class fixes one end of the rod. Currently,
Expand Down Expand Up @@ -702,9 +693,9 @@ def __init__(
position_end: NDArray[np.floating],
director_start: NDArray[np.floating],
director_end: NDArray[np.floating],
twisting_time: np.floating,
slack: np.floating,
number_of_rotations: np.floating,
twisting_time: float,
slack: float,
number_of_rotations: float,
**kwargs: Any,
) -> None:
"""
Expand Down Expand Up @@ -734,12 +725,12 @@ def __init__(
Number of rotations applied to rod.
"""
super().__init__(**kwargs)
self.twisting_time = twisting_time
self.twisting_time = np.float64(twisting_time)

angel_vel_scalar = (
2.0 * number_of_rotations * np.pi / self.twisting_time
) / 2.0
shrink_vel_scalar = slack / (self.twisting_time * 2.0)
angel_vel_scalar = np.float64(
(2.0 * number_of_rotations * np.pi / self.twisting_time) / 2.0
)
shrink_vel_scalar = np.float64(slack / (self.twisting_time * 2.0))

direction = (position_end - position_start) / np.linalg.norm(
position_end - position_start
Expand All @@ -751,7 +742,7 @@ def __init__(
self.ang_vel = angel_vel_scalar * direction
self.shrink_vel = shrink_vel_scalar * direction

theta = number_of_rotations * np.pi
theta = np.float64(number_of_rotations * np.pi)

self.final_start_directors = (
_get_rotation_matrix(theta, direction.reshape(3, 1)).reshape(3, 3)
Expand Down
60 changes: 30 additions & 30 deletions elastica/contact_forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ class RodCylinderContact(NoContact):

def __init__(
self,
k: np.floating,
nu: np.floating,
k: float,
nu: float,
velocity_damping_coefficient: float = 0.0,
friction_coefficient: float = 0.0,
) -> None:
Expand All @@ -216,10 +216,10 @@ def __init__(
For Coulombic friction coefficient for rigid-body and rod contact.
"""
super(RodCylinderContact, self).__init__()
self.k = k
self.nu = nu
self.velocity_damping_coefficient = velocity_damping_coefficient
self.friction_coefficient = friction_coefficient
self.k = np.float64(k)
self.nu = np.float64(nu)
self.velocity_damping_coefficient = np.float64(velocity_damping_coefficient)
self.friction_coefficient = np.float64(friction_coefficient)

@property
def _allowed_system_two(self) -> list[Type]:
Expand Down Expand Up @@ -287,7 +287,7 @@ class RodSelfContact(NoContact):
"""

def __init__(self, k: np.floating, nu: np.floating) -> None:
def __init__(self, k: float, nu: float) -> None:
"""
Parameters
Expand All @@ -298,8 +298,8 @@ def __init__(self, k: np.floating, nu: np.floating) -> None:
Contact damping constant.
"""
super(RodSelfContact, self).__init__()
self.k = k
self.nu = nu
self.k = np.float64(k)
self.nu = np.float64(nu)

def _check_systems_validity(
self,
Expand Down Expand Up @@ -364,8 +364,8 @@ class RodSphereContact(NoContact):

def __init__(
self,
k: np.floating,
nu: np.floating,
k: float,
nu: float,
velocity_damping_coefficient: float = 0.0,
friction_coefficient: float = 0.0,
) -> None:
Expand All @@ -383,9 +383,9 @@ def __init__(
For Coulombic friction coefficient for rigid-body and rod contact.
"""
super(RodSphereContact, self).__init__()
self.k = k
self.nu = nu
self.velocity_damping_coefficient = velocity_damping_coefficient
self.k = np.float64(k)
self.nu = np.float64(nu)
self.velocity_damping_coefficient = np.float64(velocity_damping_coefficient)
self.friction_coefficient = np.float64(friction_coefficient)

@property
Expand Down Expand Up @@ -465,8 +465,8 @@ class RodPlaneContact(NoContact):

def __init__(
self,
k: np.floating,
nu: np.floating,
k: float,
nu: float,
) -> None:
"""
Parameters
Expand All @@ -477,9 +477,9 @@ def __init__(
Contact damping constant.
"""
super(RodPlaneContact, self).__init__()
self.k = k
self.nu = nu
self.surface_tol = 1e-4
self.k = np.float64(k)
self.nu = np.float64(nu)
self.surface_tol = np.float64(1.0e-4)

@property
def _allowed_system_two(self) -> list[Type]:
Expand Down Expand Up @@ -535,9 +535,9 @@ class RodPlaneContactWithAnisotropicFriction(NoContact):

def __init__(
self,
k: np.floating,
nu: np.floating,
slip_velocity_tol: np.floating,
k: float,
nu: float,
slip_velocity_tol: float,
static_mu_array: NDArray[np.floating],
kinetic_mu_array: NDArray[np.floating],
) -> None:
Expand All @@ -558,9 +558,9 @@ def __init__(
[forward, backward, sideways] kinetic friction coefficients.
"""
super(RodPlaneContactWithAnisotropicFriction, self).__init__()
self.k = k
self.nu = nu
self.surface_tol = 1e-4
self.k = np.float64(k)
self.nu = np.float64(nu)
self.surface_tol = np.float64(1.0e-4)
self.slip_velocity_tol = slip_velocity_tol
(
self.static_mu_forward,
Expand Down Expand Up @@ -635,8 +635,8 @@ class CylinderPlaneContact(NoContact):

def __init__(
self,
k: np.floating,
nu: np.floating,
k: float,
nu: float,
) -> None:
"""
Parameters
Expand All @@ -647,9 +647,9 @@ def __init__(
Contact damping constant.
"""
super(CylinderPlaneContact, self).__init__()
self.k = k
self.nu = nu
self.surface_tol = 1e-4
self.k = np.float64(k)
self.nu = np.float64(nu)
self.surface_tol = np.float64(1.0e-4)

@property
def _allowed_system_one(self) -> list[Type]:
Expand Down
2 changes: 1 addition & 1 deletion elastica/dissipation.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class AnalyticalLinearDamper(DamperBase):
"""

def __init__(
self, damping_constant: np.floating, time_step: np.floating, **kwargs: Any
self, damping_constant: float, time_step: float, **kwargs: Any
) -> None:
"""
Analytical linear damper initializer
Expand Down
52 changes: 28 additions & 24 deletions elastica/external_forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self) -> None:
"""
pass

def apply_forces(self, system: S, time: float = 0.0) -> None:
def apply_forces(self, system: S, time: np.floating = np.float64(0.0)) -> None:
"""Apply forces to a rod-like object.
In NoForces class, this routine simply passes.
Expand All @@ -49,7 +49,7 @@ def apply_forces(self, system: S, time: float = 0.0) -> None:
"""
pass

def apply_torques(self, system: S, time: float = 0.0) -> None:
def apply_torques(self, system: S, time: np.floating = np.float64(0.0)) -> None:
"""Apply torques to a rod-like object.
In NoForces class, this routine simply passes.
Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(
self.acc_gravity = acc_gravity

def apply_forces(
self, system: "RodType | RigidBodyType", time: float = 0.0
self, system: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:
self.compute_gravity_forces(
self.acc_gravity, system.mass, system.external_forces
Expand Down Expand Up @@ -160,10 +160,10 @@ def __init__(
self.start_force = start_force
self.end_force = end_force
assert ramp_up_time > 0.0
self.ramp_up_time = ramp_up_time
self.ramp_up_time = np.float64(ramp_up_time)

def apply_forces(
self, system: "RodType | RigidBodyType", time: float = 0.0
self, system: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:
self.compute_end_point_forces(
system.external_forces,
Expand All @@ -179,7 +179,7 @@ def compute_end_point_forces(
external_forces: NDArray[np.floating],
start_force: NDArray[np.floating],
end_force: NDArray[np.floating],
time: float,
time: np.floating,
ramp_up_time: np.floating,
) -> None:
"""
Expand Down Expand Up @@ -234,7 +234,7 @@ def __init__(
self.torque = torque * direction

def apply_torques(
self, system: "RodType | RigidBodyType", time: float = 0.0
self, system: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:
n_elems = system.n_elems
torque_on_one_element = (
Expand Down Expand Up @@ -273,7 +273,9 @@ def __init__(
super(UniformForces, self).__init__()
self.force = (force * direction).reshape(3, 1)

def apply_forces(self, rod: "RodType | RigidBodyType", time: float = 0.0) -> None:
def apply_forces(
self, rod: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:
force_on_one_element = self.force / rod.n_elems

rod.external_forces += force_on_one_element
Expand Down Expand Up @@ -310,14 +312,14 @@ class MuscleTorques(NoForces):

def __init__(
self,
base_length: np.floating,
base_length: float, # TODO: Is this necessary?
b_coeff: NDArray[np.floating],
period: np.floating,
wave_number: np.floating,
phase_shift: np.floating,
period: float,
wave_number: float,
phase_shift: float,
direction: NDArray[np.floating],
rest_lengths: NDArray[np.floating],
ramp_up_time: np.floating,
ramp_up_time: float,
with_spline: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -346,12 +348,12 @@ def __init__(
super(MuscleTorques, self).__init__()

self.direction = direction # Direction torque applied
self.angular_frequency = 2.0 * np.pi / period
self.wave_number = wave_number
self.phase_shift = phase_shift
self.angular_frequency = np.float64(2.0 * np.pi / period)
self.wave_number = np.float64(wave_number)
self.phase_shift = np.float64(phase_shift)

assert ramp_up_time > 0.0
self.ramp_up_time = ramp_up_time
self.ramp_up_time = np.float64(ramp_up_time)

# s is the position of nodes on the rod, we go from node=1 to node=nelem-1, because there is no
# torques applied by first and last node on elements. Reason is that we cannot apply torque in an
Expand All @@ -370,7 +372,9 @@ def __init__(
else:
self.my_spline = np.full_like(self.s, fill_value=1.0)

def apply_torques(self, rod: "RodType | RigidBodyType", time: float = 0.0) -> None:
def apply_torques(
self, rod: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:
self.compute_muscle_torques(
time,
self.my_spline,
Expand Down Expand Up @@ -501,8 +505,8 @@ class EndpointForcesSinusoidal(NoForces):

def __init__(
self,
start_force_mag: np.floating,
end_force_mag: np.floating,
start_force_mag: float,
end_force_mag: float,
ramp_up_time: float = 0.0,
tangent_direction: NDArray[np.floating] = np.array([0, 0, 1]),
normal_direction: NDArray[np.floating] = np.array([0, 1, 0]),
Expand All @@ -526,18 +530,18 @@ def __init__(
"""
super(EndpointForcesSinusoidal, self).__init__()
# Start force
self.start_force_mag = start_force_mag
self.end_force_mag = end_force_mag
self.start_force_mag = np.float64(start_force_mag)
self.end_force_mag = np.float64(end_force_mag)

# Applied force directions
self.normal_direction = normal_direction
self.roll_direction = np.cross(normal_direction, tangent_direction)

assert ramp_up_time >= 0.0
self.ramp_up_time = ramp_up_time
self.ramp_up_time = np.float64(ramp_up_time)

def apply_forces(
self, system: "RodType | RigidBodyType", time: float = 0.0
self, system: "RodType | RigidBodyType", time: np.floating = np.float64(0.0)
) -> None:

if time < self.ramp_up_time:
Expand Down
Loading

0 comments on commit 210d172

Please sign in to comment.