|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from tools.math_tools.ndarray_tools.custom_msg import nan_infinity_console_warning |
| 6 | +from tools.math_tools.space_conversion_tools.time_to_delta_time import ( |
| 7 | + convert_state_time_to_state_delta_time, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def sombrero_projection_partial_derivative( |
| 12 | + xyz: np.ndarray, |
| 13 | + vx: float = 1.0, |
| 14 | + vy: float = 1.0, |
| 15 | + A: float = 1.0, |
| 16 | + sigma: float = 5.0, |
| 17 | + k: float = 2.0, |
| 18 | + dtype: np.dtype = np.float64, |
| 19 | + debug: bool = False, |
| 20 | +): |
| 21 | + """ |
| 22 | + Computes the partial derivatives for a sombrero projection system. |
| 23 | + This projects 2D linear motion onto a decaying ripple surface. |
| 24 | +
|
| 25 | + System equations: |
| 26 | + dx/dt = vx |
| 27 | + dy/dt = vy |
| 28 | + dz/dt = dz/dr * dr/dt |
| 29 | + where z = A * exp(-r^2 / sigma^2) * cos(k * r) and r = sqrt(x^2 + y^2) |
| 30 | +
|
| 31 | + :param xyz: An array containing the x, y, and z coordinates. |
| 32 | + :param vx: Velocity in x direction. |
| 33 | + :param vy: Velocity in y direction. |
| 34 | + :param A: Amplitude of the ripples. |
| 35 | + :param sigma: Decay constant (larger means slower decay). |
| 36 | + :param k: Radial frequency of the ripples. |
| 37 | + :param dtype: Data type for computations. |
| 38 | + :param debug: Warn if nan or infinity values are encountered. |
| 39 | + :return: An array containing the partial derivatives [x_dot, y_dot, z_dot]. |
| 40 | + """ |
| 41 | + xyz = np.nan_to_num(xyz) |
| 42 | + x, y, z = xyz.astype(dtype) |
| 43 | + |
| 44 | + r = np.sqrt(x**2 + y**2) |
| 45 | + |
| 46 | + x_dot = vx |
| 47 | + y_dot = vy |
| 48 | + |
| 49 | + # dz/dt = A * exp(-r^2/sigma^2) * [ (-2r/sigma^2)*cos(kr) - k*sin(kr) ] * dr/dt |
| 50 | + # dr/dt = (x*vx + y*vy) / r |
| 51 | + # dz/dt = A * exp(-r^2/sigma^2) * [ (-2/sigma^2)*cos(kr) - k*sin(kr)/r ] * (x*vx + y*vy) |
| 52 | + |
| 53 | + # Use sinc to handle r=0 safely: sinc(x) = sin(pi*x)/(pi*x) |
| 54 | + # sin(kr)/r = k * sin(pi * (kr/pi)) / (pi * (kr/pi)) = k * sinc(kr/pi) |
| 55 | + sinc_val = np.sinc(k * r / np.pi) |
| 56 | + |
| 57 | + decay = np.exp(-(r**2) / (sigma**2)) |
| 58 | + bracket = (-2.0 / sigma**2) * np.cos(k * r) - k * k * sinc_val |
| 59 | + z_dot = A * decay * bracket * (x * vx + y * vy) |
| 60 | + |
| 61 | + xyz_dot = np.array([x_dot, y_dot, z_dot], dtype=dtype) |
| 62 | + |
| 63 | + if debug and not np.all(np.isfinite(xyz_dot)): |
| 64 | + nan_infinity_console_warning("xyz_dot") |
| 65 | + |
| 66 | + xyz_dot = np.nan_to_num(xyz_dot) |
| 67 | + return xyz_dot.squeeze() |
| 68 | + |
| 69 | + |
| 70 | +def rollout_sombrero_projection_partial_derivative( |
| 71 | + time_space: np.ndarray, |
| 72 | + vx: float = 1.0, |
| 73 | + vy: float = 1.0, |
| 74 | + A: float = 1.0, |
| 75 | + sigma: float = 5.0, |
| 76 | + k: float = 2.0, |
| 77 | + initiale_coordinates=(0.0, 0.0, 1.0), # z(0,0) = A * exp(0) * cos(0) = A |
| 78 | + time_space_is_delta_time: bool = False, |
| 79 | + dtype: np.dtype = np.float64, |
| 80 | +) -> np.ndarray: |
| 81 | + """ |
| 82 | + Calculates the trajectory of a sombrero projection system over a given time space. |
| 83 | +
|
| 84 | + Assume `time_space` values are increasing if `time_space_is_delta_time=True` |
| 85 | +
|
| 86 | + :param time_space: Array representing time steps in wallclock time or delta time. |
| 87 | + :param vx: Velocity in x direction. |
| 88 | + :param vy: Velocity in y direction. |
| 89 | + :param A: Amplitude of the ripples. |
| 90 | + :param sigma: Decay constant. |
| 91 | + :param k: Radial frequency. |
| 92 | + :param initiale_coordinates: The state at timestep 0 |
| 93 | + :param time_space_is_delta_time: Set to True if `time_space` is an array of delta time. |
| 94 | + :param dtype: Data type for computations. |
| 95 | + :return: Array of computed x, y, z coordinates over the given time space. |
| 96 | + """ |
| 97 | + assert isinstance(initiale_coordinates, tuple) and len(initiale_coordinates) == 3 |
| 98 | + assert isinstance(time_space, np.ndarray) and time_space.ndim == 1 |
| 99 | + |
| 100 | + xyzs = np.empty((time_space.size, 3), dtype=dtype) |
| 101 | + xyzs_partial_derivative = np.empty_like(xyzs) |
| 102 | + |
| 103 | + xyzs[0] = initiale_coordinates |
| 104 | + xyzs_partial_derivative[0] = (0.0, 0.0, 0.0) |
| 105 | + |
| 106 | + if not time_space_is_delta_time: |
| 107 | + delta_time = convert_state_time_to_state_delta_time(time_space.astype(dtype)) |
| 108 | + else: |
| 109 | + delta_time = time_space.copy().astype(dtype) |
| 110 | + |
| 111 | + for i in np.arange(time_space.size - 1): |
| 112 | + xyzs_partial_derivative[i + 1] = sombrero_projection_partial_derivative( |
| 113 | + xyzs[i], vx, vy, A, sigma, k, dtype |
| 114 | + ) |
| 115 | + xyzs[i + 1] = xyzs[i] + xyzs_partial_derivative[i + 1] * delta_time[i + 1] |
| 116 | + |
| 117 | + assert np.all(np.isfinite(xyzs_partial_derivative)) |
| 118 | + assert np.all(np.isfinite(xyzs)) |
| 119 | + return xyzs |
0 commit comments