Skip to content

Commit

Permalink
fix: expression type in system v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolaas committed Sep 12, 2023
1 parent ab25e05 commit 89de208
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
RIGHT_PARENTHESIS_TOKEN = Token(tag=TokenTag.operator, value=Operators.right_parenthesis.value)
MULTIPLICATION_TOKEN = Token(tag=TokenTag.operator, value=Operators.multiply.value)

ExpressionType = Union[str, float, int]


class Expression:
def __init__(
Expand All @@ -28,7 +30,7 @@ def __init__(
@classmethod
def setup_from_expression(
cls,
value: Union[str, float, int],
value: ExpressionType,
) -> Expression:
tokens = cls.validate(value)
return cls(tokens=tokens)
Expand Down Expand Up @@ -62,7 +64,7 @@ def multiply(cls, expression1: Expression, expression2: Expression) -> Expressio
return cls(tokens=tokens_multiplied)

@classmethod
def validate(cls, expression: Union[str, float, int]) -> List[Token]:
def validate(cls, expression: ExpressionType) -> List[Token]:
expression = _expression_as_number_if_number(expression_input=expression)

if not isinstance(expression, (str, float, int)):
Expand Down Expand Up @@ -123,7 +125,7 @@ def __get_validators__(cls):
yield cls.validator


def _expression_as_number_if_number(expression_input: Union[str, float, int]) -> Union[str, float, int]:
def _expression_as_number_if_number(expression_input: ExpressionType) -> ExpressionType:
"""Expressions may be either pure numbers, booleans or strings which define a combination of numbers, operators and
references as a string. If very small numbers are parsed and represented in scientific notation, the expression
parsing will wrongfully treat these as expressions with references/operators instead of pure numeric values. Thus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional

from libecalc.expression.expression import ExpressionType
from libecalc.input.yaml_types import YamlBase
from pydantic import Field

Expand All @@ -21,7 +22,7 @@ def to_dto(self, **kwargs):


class OperationalConditionBase(YamlBase):
condition: Optional[str] = Field(
condition: Optional[ExpressionType] = Field(
None,
title="Condition",
description="""
Expand All @@ -32,7 +33,7 @@ class OperationalConditionBase(YamlBase):
but is required to evaluate to True/False or 1/0.\n
""",
)
power_loss_factor: Optional[str] = Field(
power_loss_factor: Optional[ExpressionType] = Field(
None,
title="Power loss factor",
alias="POWERLOSSFACTOR", # Legacy support
Expand All @@ -49,7 +50,7 @@ def to_dto(self):


class ConsumerSystemOperationalConditionBase(OperationalConditionBase):
conditions: Optional[str] = Field(
conditions: Optional[ExpressionType] = Field(
None,
title="Conditions",
description="""
Expand All @@ -60,7 +61,7 @@ class ConsumerSystemOperationalConditionBase(OperationalConditionBase):
but is required to evaluate to True/False or 1/0.\n
""",
)
power_loss_factors: Optional[List[str]] = Field(
power_loss_factors: Optional[List[ExpressionType]] = Field(
None,
title="Power loss factors",
alias="POWERLOSSFACTORS", # Legacy support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from libecalc.dto.base import ComponentType
from libecalc.dto.types import ConsumptionType
from libecalc.expression import Expression
from libecalc.expression.expression import ExpressionType
from libecalc.input.mappers.utils import resolve_and_validate_reference
from libecalc.input.yaml_entities import References
from libecalc.input.yaml_types.components.base import (
Expand All @@ -16,11 +17,11 @@
from libecalc.input.yaml_types.temporal_model import TemporalModel
from pydantic import Field, confloat, root_validator, validator

opt_expr_list = Optional[List[str]]
opt_expr_list = Optional[List[ExpressionType]]


class OperationalSettings(ConsumerSystemOperationalConditionBase):
total_system_rate: Optional[str] = Field(
total_system_rate: Optional[ExpressionType] = Field(
None,
title="Total system rate",
description="The total system rate expression."
Expand All @@ -38,7 +39,7 @@ class OperationalSettings(ConsumerSystemOperationalConditionBase):
title="Rates",
description="Rates [Sm3/day] as a list of expressions" "\n\nThis is mutually exclusive with RATE_FRACTIONS.",
)
inlet_pressure: Optional[str] = Field(
inlet_pressure: Optional[ExpressionType] = Field(
None,
title="Inlet pressure",
description="Inlet pressure [bara] as a single expression"
Expand All @@ -47,7 +48,7 @@ class OperationalSettings(ConsumerSystemOperationalConditionBase):
inlet_pressures: opt_expr_list = Field(
None, title="Inlet pressures", description="Inlet pressures [bara] as a list of expressions."
)
outlet_pressure: Optional[str] = Field(
outlet_pressure: Optional[ExpressionType] = Field(
None,
title="Outlet pressure",
description="Outlet pressure [bara] as a single expression"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from libecalc.dto.base import ComponentType
from libecalc.dto.types import ConsumptionType
from libecalc.expression import Expression
from libecalc.expression.expression import ExpressionType
from libecalc.input.mappers.utils import resolve_and_validate_reference
from libecalc.input.yaml_entities import References
from libecalc.input.yaml_types.components.base import (
Expand All @@ -16,7 +17,7 @@
from libecalc.input.yaml_types.temporal_model import TemporalModel
from pydantic import Field, confloat, root_validator, validator

opt_expr_list = Optional[List[str]]
opt_expr_list = Optional[List[ExpressionType]]


class OperationalSettings(ConsumerSystemOperationalConditionBase):
Expand All @@ -38,7 +39,7 @@ class OperationalSettings(ConsumerSystemOperationalConditionBase):
title="Rates",
description="Rates [Sm3/day] as a list of expressions" "\n\nThis is mutually exclusive with RATE_FRACTIONS.",
)
inlet_pressure: Optional[str] = Field(
inlet_pressure: Optional[ExpressionType] = Field(
None,
title="Inlet pressure",
description="Inlet pressure [bara] as a single expression"
Expand All @@ -47,7 +48,7 @@ class OperationalSettings(ConsumerSystemOperationalConditionBase):
inlet_pressures: opt_expr_list = Field(
None, title="Inlet pressures", description="Inlet pressures [bara] as a list of expressions."
)
outlet_pressure: Optional[str] = Field(
outlet_pressure: Optional[ExpressionType] = Field(
None,
title="Outlet pressure",
description="Outlet pressure [bara] as a single expression"
Expand All @@ -56,7 +57,7 @@ class OperationalSettings(ConsumerSystemOperationalConditionBase):
outlet_pressures: opt_expr_list = Field(
None, title="Outlet pressures", description="Outlet pressures [bara] as a list of expressions."
)
fluid_density: Optional[str] = Field(
fluid_density: Optional[ExpressionType] = Field(
None, title="FluidStream density", description="The fluid density [kg/m3] as a single expression."
)
fluid_densities: opt_expr_list = Field(
Expand Down Expand Up @@ -204,7 +205,8 @@ def to_dto(
else:
rates = [
Expression.multiply(
Expression.setup_from_expression(self.rate), Expression.setup_from_expression(rate_fraction)
Expression.setup_from_expression(operational_setting.total_system_rate),
Expression.setup_from_expression(rate_fraction),
)
for rate_fraction in operational_setting.rate_fractions
]
Expand Down
Loading

0 comments on commit 89de208

Please sign in to comment.