Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions smart_control/simulator/weather_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
)


def seconds_to_rads(seconds_in_day: int) -> float:
"""Returns radians corresponding to number of second in the day.

Args:
seconds_in_day: Seconds that have passed so far in the day.
"""
return (seconds_in_day / _SECONDS_IN_A_DAY) * (
_MAX_RADIANS - _MIN_RADIANS
) + _MIN_RADIANS


@gin.configurable
class BaseWeatherController(metaclass=abc.ABCMeta):
"""Represents the weather on any specific time."""
Expand Down Expand Up @@ -78,16 +89,6 @@ def __init__(
f'Low temp cannot be greater than high temp for special day: {day}.'
)

def seconds_to_rads(self, seconds_in_day: int) -> float:
"""Returns radians corresponding to number of second in the day.

Args:
seconds_in_day: Seconds that have passed so far in the day.
"""
return (seconds_in_day / _SECONDS_IN_A_DAY) * (
_MAX_RADIANS - _MIN_RADIANS
) + _MIN_RADIANS

def get_current_temp(self, timestamp: pd.Timestamp) -> float:
"""Returns current temperature in K.

Expand Down Expand Up @@ -116,7 +117,7 @@ def get_current_temp(self, timestamp: pd.Timestamp) -> float:
seconds_in_day = (
timestamp - pd.Timestamp(timestamp.date())
).total_seconds()
rad = self.seconds_to_rads(seconds_in_day)
rad = seconds_to_rads(seconds_in_day)
temp = 0.5 * (math.sin(rad) + 1) * (high - low) + low
return temp

Expand Down Expand Up @@ -296,4 +297,4 @@ def get_current_humidity(self, timestamp: pd.Timestamp) -> float:

# pylint: disable=unused-argument
def get_air_convection_coefficient(self, timestamp: pd.Timestamp) -> float:
return self.convection_coefficient
return self.convection_coefficient
12 changes: 2 additions & 10 deletions smart_control/simulator/weather_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,13 @@ def test_init_raises_error_special_day_temp(self):

self.assertRaises(ValueError, create_weather_fn)

@parameterized.named_parameters(
@parameterized.named_parameters(
('min_rad', 0.0, -math.pi / 2),
('max_rad', 3600.0 * 24, 3 * math.pi / 2),
('mid_rad', 3600 * 12, math.pi / 2),
)
def test_seconds_to_rad(self, seconds, expected):
low_temp = 40.5
high_temp = 62.5
special_days = {110: (30, 70)}

weather = weather_controller.WeatherController(
low_temp, high_temp, special_days
)

rads = weather.seconds_to_rads(seconds)
rads = weather_controller.seconds_to_rads(seconds)

self.assertEqual(rads, expected)

Expand Down
Loading