From cde10df032304f2dfd2735031389e68e8f8f3457 Mon Sep 17 00:00:00 2001 From: Aryan Roshan Date: Sat, 16 Mar 2024 15:57:57 -0700 Subject: [PATCH] test 1 --- .../nodes/physics_engine/fluid_generation.py | 13 ++--- .../unit/nodes/physics_engine/test_fluids.py | 53 ++++++++++--------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/boat_simulator/boat_simulator/nodes/physics_engine/fluid_generation.py b/src/boat_simulator/boat_simulator/nodes/physics_engine/fluid_generation.py index 8ec2b44a2..6ce214eab 100644 --- a/src/boat_simulator/boat_simulator/nodes/physics_engine/fluid_generation.py +++ b/src/boat_simulator/boat_simulator/nodes/physics_engine/fluid_generation.py @@ -24,7 +24,7 @@ def __init__(self, generator: VectorGenerator): def next(self) -> NDArray: """Generates the next velocity vector for the fluid simulation. - Returns: + Returns: NDArray: An array representing the updated velocity vector for the fluid simulation. """ @@ -34,8 +34,6 @@ def next(self) -> NDArray: self.__velocity = np.array(self.__generator.next()) return self.__velocity - raise NotImplementedError() - @property def velocity(self) -> NDArray: """Returns the fluid's current velocity vector. @@ -44,13 +42,10 @@ def velocity(self) -> NDArray: NDArray: The velocity vector of the fluid, expressed in meters per second (m/s) and ranging from negative infinity to positive infinity. """ - + # Return the current velocity vector return self.__velocity - - raise NotImplementedError() - @property def speed(self) -> Scalar: """Calculates the current speed of the fluid. @@ -64,8 +59,6 @@ def speed(self) -> Scalar: # Calculate the magnitude (speed) of the velocity vector return np.linalg.norm(self.__velocity) - - raise NotImplementedError() @property def direction(self) -> Scalar: @@ -84,4 +77,4 @@ def direction(self) -> Scalar: # Calculate the angle of the velocity vector in radians, then convert to degrees and adjust to [-180, 180) range angle_rad = np.arctan2(self.__velocity[1], self.__velocity[0]) angle_deg = rad_to_degrees(angle_rad) - return bound_to_180(angle_deg) \ No newline at end of file + return bound_to_180(angle_deg) diff --git a/src/boat_simulator/tests/unit/nodes/physics_engine/test_fluids.py b/src/boat_simulator/tests/unit/nodes/physics_engine/test_fluids.py index 6d3d8fd0e..19e076417 100644 --- a/src/boat_simulator/tests/unit/nodes/physics_engine/test_fluids.py +++ b/src/boat_simulator/tests/unit/nodes/physics_engine/test_fluids.py @@ -2,39 +2,44 @@ import numpy as np import pytest -from boat_simulator.common.generators import ConstantGenerator, GaussianGenerator, VectorGenerator +from boat_simulator.common.generators import ConstantGenerator, GaussianGenerator, VectorGenerator, MVGaussianGenerator from boat_simulator.nodes.physics_engine.fluid_generation import FluidGenerator class TestFluidGenerator: - @pytest.mark.parametrize("vector", [ (np.array([1, 0])), (np.array([0, 1])), (np.array([1, 0])), ]) - def test_velocity_constant(self, vector): - vector_generator = ConstantGenerator(vector) - fluid_generator = FluidGenerator(vector_generator) - generated_fluid_vector = fluid_generator.next() - assert np.all(generated_fluid_vector == vector) + vector_generator = ConstantGenerator(constant=vector) + fluid_generator = FluidGenerator(generator=vector_generator) + first_fluid_vector = fluid_generator.velocity + assert np.all(first_fluid_vector == vector) - def test_velocity_random(self): - vector_generator = VectorGenerator() + @pytest.mark.parametrize( + "mean, cov", + [ + (np.array([1, 2]), np.array([[2, 1], [1, 2]])), + (np.array([4, 5]), np.array([[3, 1], [1, 3]])), + (np.array([100, 50]), np.array([[10, 5], [5, 10]])), + (np.array([120, 130]), np.array([[10, 5], [5, 10]])), + ], + ) + def test_velocity_random(self, mean, cov): + vector_generator = MVGaussianGenerator(mean=mean,cov=cov) fluid_generator = FluidGenerator(vector_generator) - # generate first vector - generated_fluid_vector = fluid_generator.next() - # call it again (generate a new vector), and assign to different variable - # compare the two, they should not be equal - assert np.all(generated_fluid_vector == vector_generator) + first_fluid_vector = fluid_generator.velocity + next_fluid_vector = fluid_generator.next() + assert np.all(next_fluid_vector != first_fluid_vector) @pytest.mark.parametrize("vector", [ (np.array([1, 0])), (np.array([0, 1])), (np.array([-1, 0])), - (np.array([0, -1])), - (np.array([1, 1])), + (np.array([0, -1])), + (np.array([1, 1])), (np.array([-1, -1])), ]) @@ -46,19 +51,19 @@ def test_speed(self, vector): assert(np.linalg.norm(vector) == fluid_generator.speed) @pytest.mark.parametrize("vector, expected_direction", [ - (np.array([1, 0]), 0), # Rightward, 0 degrees - (np.array([0, 1]), 90), # Upward, 90 degrees - (np.array([-1, 0]), -180), # Leftward, 180 or -180 degrees, normalized to 180 in this context - (np.array([0, -1]), -90), # Downward, -90 degrees - (np.array([1, 1]), 45), # Diagonal (right-upward), 45 degrees - (np.array([-1, -1]), -135), # Diagonal (left-downward), -135 degrees + (np.array([1, 0]), 0), + (np.array([0, 1]), 90), + (np.array([-1, 0]), -180), + (np.array([0, -1]), -90), + (np.array([1, 1]), 45), + (np.array([-1, -1]), -135), ]) + def test_direction(self, vector, expected_direction): vector_generator = ConstantGenerator(constant=vector) fluid_generator = FluidGenerator(generator=vector_generator) - fluid_generator.next() # Generate the next fluid vector to set the internal state calculated_direction = fluid_generator.direction assert np.isclose(calculated_direction, expected_direction, atol=1e-7), f"Expected direction {expected_direction}, but got {calculated_direction}" class TestWindGenerator: - pass \ No newline at end of file + pass