-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: move expression evaluator to components #776
Open
frodehk
wants to merge
4
commits into
main
Choose a base branch
from
expression-evaluator-in-components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
167 changes: 167 additions & 0 deletions
167
src/libecalc/domain/infrastructure/emitters/venting_emitter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
from typing import Optional | ||
|
||
import numpy as np | ||
|
||
from libecalc.application.energy.component_energy_context import ComponentEnergyContext | ||
from libecalc.application.energy.emitter import Emitter | ||
from libecalc.application.energy.energy_component import EnergyComponent | ||
from libecalc.application.energy.energy_model import EnergyModel | ||
from libecalc.common.component_type import ComponentType | ||
from libecalc.common.string.string_utils import generate_id | ||
from libecalc.common.temporal_model import TemporalModel | ||
from libecalc.common.time_utils import Period | ||
from libecalc.common.units import Unit | ||
from libecalc.common.utils.rates import Rates, RateType, TimeSeriesFloat, TimeSeriesStreamDayRate | ||
from libecalc.common.variables import ExpressionEvaluator | ||
from libecalc.core.result.emission import EmissionResult | ||
from libecalc.dto.types import ConsumerUserDefinedCategoryType | ||
from libecalc.dto.utils.validators import convert_expression | ||
from libecalc.expression import Expression | ||
from libecalc.presentation.yaml.yaml_types.emitters.yaml_venting_emitter import ( | ||
YamlVentingEmission, | ||
YamlVentingType, | ||
YamlVentingVolume, | ||
) | ||
|
||
|
||
class VentingEmitter(Emitter, EnergyComponent): | ||
def __init__( | ||
self, | ||
name: str, | ||
expression_evaluator: ExpressionEvaluator, | ||
component_type: ComponentType, | ||
user_defined_category: dict[Period, ConsumerUserDefinedCategoryType], | ||
emitter_type: YamlVentingType, | ||
): | ||
self.name = name | ||
self.expression_evaluator = expression_evaluator | ||
self.component_type = component_type | ||
self.user_defined_category = user_defined_category | ||
self.emitter_type = emitter_type | ||
self._regularity_evaluated = None | ||
|
||
@property | ||
def id(self) -> str: | ||
return generate_id(self.name) | ||
|
||
@property | ||
def regularity_evaluated(self): | ||
return self.expression_evaluator.evaluate(expression=TemporalModel(self._regularity)).tolist() | ||
|
||
def evaluate_emissions( | ||
self, | ||
energy_context: ComponentEnergyContext, | ||
energy_model: EnergyModel, | ||
) -> Optional[dict[str, EmissionResult]]: | ||
self._regularity = energy_model.get_regularity(self.id) | ||
venting_emitter_results = {} | ||
emission_rates = self.get_emissions() | ||
|
||
for emission_name, emission_rate in emission_rates.items(): | ||
emission_result = EmissionResult( | ||
name=emission_name, | ||
periods=self.expression_evaluator.get_periods(), | ||
rate=emission_rate, | ||
) | ||
venting_emitter_results[emission_name] = emission_result | ||
return venting_emitter_results | ||
|
||
def get_emissions(self) -> dict[str, TimeSeriesStreamDayRate]: | ||
raise NotImplementedError("Subclasses should implement this method") | ||
|
||
def _evaluate_emission_rate(self, emission): | ||
emission_rate = self.expression_evaluator.evaluate(Expression.setup_from_expression(value=emission.rate.value)) | ||
if emission.rate.type == RateType.CALENDAR_DAY: | ||
emission_rate = Rates.to_stream_day( | ||
calendar_day_rates=np.asarray(emission_rate), regularity=self.regularity_evaluated | ||
).tolist() | ||
unit = emission.rate.unit.to_unit() | ||
emission_rate = unit.to(Unit.TONS_PER_DAY)(emission_rate) | ||
return emission_rate | ||
|
||
def _create_time_series(self, emission_rate): | ||
return TimeSeriesStreamDayRate( | ||
periods=self.expression_evaluator.get_periods(), | ||
values=emission_rate, | ||
unit=Unit.TONS_PER_DAY, | ||
) | ||
|
||
def is_fuel_consumer(self) -> bool: | ||
return False | ||
|
||
def is_electricity_consumer(self) -> bool: | ||
return False | ||
|
||
def get_component_process_type(self) -> ComponentType: | ||
return self.component_type | ||
|
||
def get_name(self) -> str: | ||
return self.name | ||
|
||
def is_provider(self) -> bool: | ||
return False | ||
|
||
def is_container(self) -> bool: | ||
return False | ||
|
||
|
||
class DirectVentingEmitter(VentingEmitter): | ||
def __init__(self, emissions: list[YamlVentingEmission], **kwargs): | ||
super().__init__(**kwargs) | ||
self.emissions = emissions | ||
self.emitter_type = YamlVentingType.DIRECT_EMISSION | ||
|
||
def get_emissions(self) -> dict[str, TimeSeriesStreamDayRate]: | ||
emissions = {} | ||
for emission in self.emissions: | ||
emission_rate = self._evaluate_emission_rate(emission) | ||
emissions[emission.name] = self._create_time_series(emission_rate) | ||
return emissions | ||
|
||
|
||
class OilVentingEmitter(VentingEmitter): | ||
def __init__(self, volume: YamlVentingVolume, **kwargs): | ||
super().__init__(**kwargs) | ||
self.volume = volume | ||
self.emitter_type = YamlVentingType.OIL_VOLUME | ||
|
||
def get_emissions(self) -> dict[str, TimeSeriesStreamDayRate]: | ||
oil_rates = self.expression_evaluator.evaluate( | ||
expression=Expression.setup_from_expression(value=self.volume.rate.value) | ||
) | ||
if self.volume.rate.type == RateType.CALENDAR_DAY: | ||
oil_rates = Rates.to_stream_day( | ||
calendar_day_rates=np.asarray(oil_rates), regularity=self.regularity_evaluated | ||
).tolist() | ||
Comment on lines
+132
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use get_oil_rates here? |
||
emissions = {} | ||
for emission in self.volume.emissions: | ||
factors = self.expression_evaluator.evaluate( | ||
Expression.setup_from_expression(value=emission.emission_factor) | ||
) | ||
unit = self.volume.rate.unit.to_unit() | ||
oil_rates = unit.to(Unit.STANDARD_CUBIC_METER_PER_DAY)(oil_rates) | ||
emission_rate = [oil_rate * factor for oil_rate, factor in zip(oil_rates, factors)] | ||
emission_rate = Unit.KILO_PER_DAY.to(Unit.TONS_PER_DAY)(emission_rate) | ||
emissions[emission.name] = self._create_time_series(emission_rate) | ||
return emissions | ||
|
||
def get_oil_rates( | ||
self, | ||
regularity: TimeSeriesFloat, | ||
) -> TimeSeriesStreamDayRate: | ||
oil_rates = self.expression_evaluator.evaluate(expression=convert_expression(self.volume.rate.value)) | ||
|
||
if self.volume.rate.type == RateType.CALENDAR_DAY: | ||
oil_rates = Rates.to_stream_day( | ||
calendar_day_rates=np.asarray(oil_rates), | ||
regularity=regularity.values, | ||
).tolist() | ||
|
||
unit = self.volume.rate.unit.to_unit() | ||
oil_rates = unit.to(Unit.STANDARD_CUBIC_METER_PER_DAY)(oil_rates) | ||
|
||
return TimeSeriesStreamDayRate( | ||
periods=self.expression_evaluator.get_periods(), | ||
values=oil_rates, | ||
unit=Unit.STANDARD_CUBIC_METER_PER_DAY, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ def __init__( | |
ComponentType.COMPRESSOR_SYSTEM, | ||
], | ||
energy_usage_model: dict[Period, ElectricEnergyUsageModel], | ||
expression_evaluator: ExpressionEvaluator, | ||
consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY, | ||
): | ||
self.name = name | ||
|
@@ -47,6 +48,7 @@ def __init__( | |
self.energy_usage_model = self.check_energy_usage_model(energy_usage_model) | ||
self._validate_el_consumer_temporal_model(self.energy_usage_model) | ||
self._check_model_energy_usage(self.energy_usage_model) | ||
self.expression_evaluator = expression_evaluator | ||
self.consumes = consumes | ||
self.component_type = component_type | ||
|
||
|
@@ -78,9 +80,10 @@ def get_component_process_type(self) -> ComponentType: | |
def get_name(self) -> str: | ||
return self.name | ||
|
||
def evaluate_energy_usage( | ||
self, expression_evaluator: ExpressionEvaluator, context: ComponentEnergyContext | ||
) -> dict[str, EcalcModelResult]: | ||
def set_consumer_results(self, consumer_results: dict[str, EcalcModelResult]): | ||
self.consumer_results = consumer_results | ||
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
|
||
def evaluate_energy_usage(self, context: ComponentEnergyContext) -> dict[str, EcalcModelResult]: | ||
consumer_results: dict[str, EcalcModelResult] = {} | ||
consumer = ConsumerEnergyComponent( | ||
id=self.id, | ||
|
@@ -95,7 +98,7 @@ def evaluate_energy_usage( | |
} | ||
), | ||
) | ||
consumer_results[self.id] = consumer.evaluate(expression_evaluator=expression_evaluator) | ||
consumer_results[self.id] = consumer.evaluate(expression_evaluator=self.expression_evaluator) | ||
|
||
return consumer_results | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass regularity into init instead?
If I remember correctly
get_regularity
for EcalcModel was kind of a workaround since we didn't have the flexibility in these classes as we have now. I don't think we have to pass energy_model into evaluate_emissions any more.