Skip to content

Commit

Permalink
fix: requested pressures not always an attribute (#155)
Browse files Browse the repository at this point in the history
CompressorSampled does not have a suction pressure and crashed ecalc
before this change.
  • Loading branch information
jsolaas authored Sep 8, 2023
1 parent b61a0fe commit 0078405
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/ecalc/libraries/libecalc/common/libecalc/core/graph_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import operator
from datetime import datetime
from functools import reduce
from typing import Any, Dict, List
from typing import Dict, List

import libecalc
from libecalc import dto
Expand All @@ -23,6 +23,7 @@
from libecalc.core.result.emission import EmissionResult
from libecalc.dto.base import ComponentType
from libecalc.dto.graph import Graph
from libecalc.dto.models import EnergyUsageModel
from libecalc.dto.result.emission import EmissionIntensityResult
from libecalc.dto.result.results import CompressorResult
from libecalc.dto.types import RateType
Expand Down Expand Up @@ -204,25 +205,26 @@ def _parse_emissions(emissions: Dict[str, EmissionResult]) -> Dict[str, libecalc
}

@staticmethod
def extract_pressures_from_models(
energy_usage_model: Dict[datetime, Any],
def extract_requested_pressures_from_model(
energy_usage_model: Dict[datetime, EnergyUsageModel],
pressure_type: CompressorPressureType,
) -> TemporalModel[Expression]:
"""Get temporal model for compressor inlet- and outlet pressures.
The pressures are the actual pressures defined by user in input.
"""Get compressor inlet- and outlet pressures.
The pressures are the requested pressures defined by user in input.
:param energy_usage_model: Temporal energy model
:param energy_usage_model: Temporal energy usage model
:param pressure_type: Compressor pressure type, inlet- or outlet
:return: Temporal model with pressures as expressions
"""
evaluated_temporal_energy_usage_models = {}
for period, model in TemporalModel(energy_usage_model).items():
pressures = model.suction_pressure

if pressure_type.value == CompressorPressureType.OUTLET_PRESSURE:
pressures = model.discharge_pressure
pressures = getattr(model, "discharge_pressure", None)
else:
pressures = getattr(model, "suction_pressure", None)

if pressures is None:
# Handle when pressure attribute does not exist and if pressure attribute is None
pressures = math.nan

if not isinstance(pressures, Expression):
Expand Down Expand Up @@ -271,12 +273,12 @@ def get_asset_result(self) -> libecalc.dto.result.EcalcModelResult:
component = self.graph.get_component(consumer_id)

# Get temporal expression for inlet- and outlet pressures
inlet_pressure_eval = GraphResult.extract_pressures_from_models(
inlet_pressure_eval = GraphResult.extract_requested_pressures_from_model(
energy_usage_model=component.energy_usage_model,
pressure_type=CompressorPressureType.INLET_PRESSURE,
)

outlet_pressure_eval = GraphResult.extract_pressures_from_models(
outlet_pressure_eval = GraphResult.extract_requested_pressures_from_model(
energy_usage_model=component.energy_usage_model,
pressure_type=CompressorPressureType.OUTLET_PRESSURE,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@
CompressorSystemConsumerFunction,
TabulatedConsumerFunction,
]

EnergyUsageModel = Union[
FuelEnergyUsageModel,
ElectricEnergyUsageModel,
]

0 comments on commit 0078405

Please sign in to comment.