Skip to content

Commit

Permalink
refactor: don't use dto in DirectExpressionConsumerFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolaas committed Sep 24, 2024
1 parent b4928ee commit 42b6774
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@
get_power_loss_factor_from_expression,
)
from libecalc.core.models.results import EnergyFunctionGenericResult
from libecalc.dto import DirectConsumerFunction
from libecalc.expression import Expression


class DirectExpressionConsumerFunction(ConsumerFunction):
def __init__(
self,
data_transfer_object: DirectConsumerFunction,
energy_usage_type: EnergyUsageType,
condition: Optional[Expression] = None,
fuel_rate: Optional[Expression] = None,
load: Optional[Expression] = None,
power_loss_factor: Optional[Expression] = None,
consumption_rate_type: RateType = RateType.STREAM_DAY,
):
expression = (
data_transfer_object.fuel_rate
if data_transfer_object.energy_usage_type == EnergyUsageType.FUEL.value
else data_transfer_object.load
)
condition_expression = data_transfer_object.condition
power_loss_factor_expression = data_transfer_object.power_loss_factor
direct_consumer_consumption_rate_type = data_transfer_object.consumption_rate_type

self.data_transfer_object = data_transfer_object
expression = fuel_rate if energy_usage_type == EnergyUsageType.FUEL.value else load
condition_expression = condition
power_loss_factor_expression = power_loss_factor
direct_consumer_consumption_rate_type = consumption_rate_type
self._energy_usage_type = energy_usage_type
self._expression = expression
self._convert_to_stream_day = direct_consumer_consumption_rate_type == RateType.CALENDAR_DAY
self._condition_expression = condition_expression
self._power_loss_factor_expression = power_loss_factor_expression

@property
def is_electrical_consumer(self) -> bool:
return self.data_transfer_object.energy_usage_type == EnergyUsageType.POWER
return self._energy_usage_type == EnergyUsageType.POWER

@property
def is_fuel_consumer(self) -> bool:
return self.data_transfer_object.energy_usage_type == EnergyUsageType.FUEL
return self._energy_usage_type == EnergyUsageType.FUEL

@property
def energy_usage_unit(self) -> Unit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@


def create_direct_consumer_function(model_dto: DirectConsumerFunction) -> DirectExpressionConsumerFunction:
return DirectExpressionConsumerFunction(model_dto)
return DirectExpressionConsumerFunction(
energy_usage_type=model_dto.energy_usage_type,
condition=model_dto.condition,
fuel_rate=model_dto.fuel_rate,
load=model_dto.load,
power_loss_factor=model_dto.power_loss_factor,
consumption_rate_type=model_dto.consumption_rate_type,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import libecalc.common.energy_usage_type
import libecalc.common.utils.rates
from libecalc import dto
from libecalc.common.component_type import ComponentType
from libecalc.common.consumption_type import ConsumptionType
from libecalc.common.temporal_model import TemporalModel
Expand All @@ -25,18 +24,17 @@ def direct_variables_map():

def test_direct_expression_consumer_function():
time_series_name = "SIM1"
fuel_rate_function_data = dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(time_series_name + ";Flare {+} " + time_series_name + ";Vent"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)

# Test evaluation
variables_map = VariablesMap(
time_vector=[datetime(2000, 1, 1, 0, 0), datetime(2001, 1, 1, 0, 0)],
variables={"SIM1;Flare": [10.0, 3.0], "SIM1;Vent": [5.0, 2.0]},
)

result = DirectExpressionConsumerFunction(fuel_rate_function_data).evaluate(
result = DirectExpressionConsumerFunction(
fuel_rate=Expression.setup_from_expression(time_series_name + ";Flare {+} " + time_series_name + ";Vent"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
).evaluate(
variables_map=variables_map,
regularity=[1.0] * len(variables_map.time_vector),
)
Expand All @@ -49,7 +47,14 @@ def test_direct_expression_consumer_function():
name="Flare",
component_type=ComponentType.GENERIC,
energy_usage_model=TemporalModel(
{datetime(1900, 1, 1): DirectExpressionConsumerFunction(fuel_rate_function_data)}
{
datetime(1900, 1, 1): DirectExpressionConsumerFunction(
fuel_rate=Expression.setup_from_expression(
time_series_name + ";Flare {+} " + time_series_name + ";Vent"
),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
}
),
consumes=ConsumptionType.FUEL,
regularity=TemporalModel({datetime(1900, 1, 1): Expression.setup_from_expression(1)}),
Expand All @@ -65,10 +70,8 @@ def test_direct_expression_consumer_function():
# Constant
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="2"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value="2"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -80,10 +83,8 @@ def test_direct_expression_consumer_function():
# When expression string is float even if it should be string
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value=2.1),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value=2.1),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -95,10 +96,8 @@ def test_direct_expression_consumer_function():
# Expression with numbers only
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="2 {+} 3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value="2 {+} 3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -110,10 +109,8 @@ def test_direct_expression_consumer_function():
# Expression with time series input
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="0 {*} " + time_series_name + ";Flare"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value="0 {*} " + time_series_name + ";Flare"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -125,10 +122,8 @@ def test_direct_expression_consumer_function():
# Expression 0
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="0"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value="0"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -140,11 +135,9 @@ def test_direct_expression_consumer_function():
# With condition
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="2 {+} 3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
condition=Expression.setup_from_expression(value="2 < 1"),
)
fuel_rate=Expression.setup_from_expression(value="2 {+} 3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
condition=Expression.setup_from_expression(value="2 < 1"),
)
.evaluate(
variables_map=variables_map,
Expand All @@ -155,14 +148,12 @@ def test_direct_expression_consumer_function():
)
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
condition=Expression.multiply(
Expression.setup_from_expression(value="2 > 1"),
Expression.setup_from_expression(value=time_series_name + ";Flare > 4"),
),
)
fuel_rate=Expression.setup_from_expression(value="3.1"),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
condition=Expression.multiply(
Expression.setup_from_expression(value="2 > 1"),
Expression.setup_from_expression(value=time_series_name + ";Flare > 4"),
),
)
.evaluate(
variables_map=variables_map,
Expand All @@ -174,11 +165,9 @@ def test_direct_expression_consumer_function():
# With power loss factor
np.testing.assert_allclose(
actual=DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
fuel_rate=Expression.setup_from_expression(value="2"),
power_loss_factor=Expression.setup_from_expression(value=0.2),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
fuel_rate=Expression.setup_from_expression(value="2"),
power_loss_factor=Expression.setup_from_expression(value=0.2),
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.FUEL,
)
.evaluate(
variables_map=variables_map,
Expand All @@ -197,20 +186,16 @@ def test_direct_expression_consumer_function_consumption_rate_type(direct_variab
# The stream day function passes through the evaluated expression directly
# with no modification from regularity - as this is already of "stream day" type
stream_day_function = DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
load=Expression.setup_from_expression(stream_day_consumption),
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.POWER,
),
load=Expression.setup_from_expression(stream_day_consumption),
consumption_rate_type=libecalc.common.utils.rates.RateType.STREAM_DAY,
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.POWER,
)
# The calendar day function, divides the evaluated expression by regularity
# to obtain "stream day" type as it is of "calendar day" type
calendar_day_function = DirectExpressionConsumerFunction(
dto.DirectConsumerFunction(
load=Expression.setup_from_expression(calendar_day_consumption),
consumption_rate_type=libecalc.common.utils.rates.RateType.CALENDAR_DAY,
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.POWER,
)
load=Expression.setup_from_expression(calendar_day_consumption),
consumption_rate_type=libecalc.common.utils.rates.RateType.CALENDAR_DAY,
energy_usage_type=libecalc.common.energy_usage_type.EnergyUsageType.POWER,
)

evaluated_regularity = [regularity] * len(direct_variables_map.time_vector)
Expand Down

0 comments on commit 42b6774

Please sign in to comment.