Skip to content

Commit

Permalink
test 1
Browse files Browse the repository at this point in the history
  • Loading branch information
arynrosh committed Mar 16, 2024
1 parent 93beadc commit cde10df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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:
Expand All @@ -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)
return bound_to_180(angle_deg)
53 changes: 29 additions & 24 deletions src/boat_simulator/tests/unit/nodes/physics_engine/test_fluids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])),
])

Expand All @@ -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
pass

0 comments on commit cde10df

Please sign in to comment.