Skip to content
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

fix: full run with system v2 components #147

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
17 changes: 17 additions & 0 deletions src/ecalc/libraries/libecalc/common/libecalc/core/ecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from libecalc.core.result import EcalcModelResult
from libecalc.core.result.emission import EmissionResult
from libecalc.dto.graph import Graph
from libecalc.dto.types import ConsumptionType


class EnergyCalculator:
Expand Down Expand Up @@ -72,6 +73,15 @@ def evaluate_energy_usage(self, variables_map: dto.VariablesMap) -> Dict[str, Ec
def evaluate_emissions(
self, variables_map: dto.VariablesMap, consumer_results: Dict[str, EcalcModelResult]
) -> Dict[str, Dict[str, EmissionResult]]:
"""
Calculate emissions for fuel consumers and emitters

Args:
variables_map:
consumer_results:

Returns: a mapping from consumer_id to emissions
"""
emission_results: Dict[str, Dict[str, EmissionResult]] = {}
for consumer_dto in self._graph.components.values():
if isinstance(consumer_dto, (dto.FuelConsumer, dto.GeneratorSet)):
Expand All @@ -81,6 +91,13 @@ def evaluate_emissions(
variables_map=variables_map,
fuel_rate=np.asarray(energy_usage.values),
)
elif isinstance(consumer_dto, (dto.components.CompressorSystem, dto.components.PumpSystem)):
if consumer_dto.consumes == ConsumptionType.FUEL:
fuel_model = FuelModel(consumer_dto.fuel)
energy_usage = consumer_results[consumer_dto.id].component_result.energy_usage
emission_results[consumer_dto.id] = fuel_model.evaluate_emissions(
variables_map=variables_map, fuel_rate=np.asarray(energy_usage.values)
)
elif isinstance(consumer_dto, dto.DirectEmitter):
emission_results[consumer_dto.id] = DirectEmitter(consumer_dto).evaluate(variables_map=variables_map)
return emission_results
17 changes: 10 additions & 7 deletions src/ecalc/libraries/libecalc/common/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ class BaseConsumer(BaseEquipment, ABC):
consumes: ConsumptionType
fuel: Optional[Dict[datetime, FuelType]]

@validator("fuel")
def validate_fuel_exist(cls, fuel, values):
"""
Make sure fuel is set if consumption type is FUEL.
"""
if values.get("consumes") == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1):
msg = f"Missing fuel for fuel consumer '{values.get('name')}'"
raise ValueError(msg)
return fuel


class ElectricityConsumer(BaseConsumer):
consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY
Expand Down Expand Up @@ -115,13 +125,6 @@ class FuelConsumer(BaseConsumer):
lambda data: check_model_energy_usage_type(data, EnergyUsageType.FUEL)
)

@validator("fuel")
def validate_fuel_exist(cls, fuel, values):
if len(fuel) < 1:
msg = f"Missing fuel for fuel consumer '{values.get('name')}'"
raise ValueError(msg)
return fuel


Consumer = Annotated[Union[FuelConsumer, ElectricityConsumer], Field(discriminator="consumes")]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ class GeneratorSetResult(EquipmentResultBase):


class ConsumerSystemResult(EquipmentResultBase):
componentType: Literal[ComponentType.PUMP_SYSTEM, ComponentType.COMPRESSOR_SYSTEM]
componentType: Literal[
ComponentType.PUMP_SYSTEM,
ComponentType.COMPRESSOR_SYSTEM,
ComponentType.COMPRESSOR_SYSTEM_V2,
ComponentType.PUMP_SYSTEM_V2,
]

operational_settings_used: Optional[TimeSeriesInt] = Field(
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,22 @@ def from_yaml_to_dto(
consumes: ConsumptionType,
default_fuel: Optional[str] = None,
) -> dto.components.Consumer:
fuel = None
if consumes == ConsumptionType.FUEL:
try:
fuel = _resolve_fuel(
data.get(EcalcYamlKeywords.fuel), default_fuel, self.__references, target_period=self._target_period
)
except ValueError as e:
raise DataValidationError(
data=data,
message=f"Fuel '{data.get(EcalcYamlKeywords.fuel)}' does not exist",
error_key="fuel",
) from e

if EcalcYamlKeywords.type in data:
if data[EcalcYamlKeywords.type] == ComponentType.COMPRESSOR_SYSTEM_V2:
compressor_system_yaml = YamlCompressorSystem(**data)

fuel = None
if consumes == ConsumptionType.FUEL:
try:
fuel = _resolve_fuel(
data.get(EcalcYamlKeywords.fuel),
default_fuel,
self.__references,
target_period=self._target_period,
)
except ValueError as e:
raise DataValidationError(
data=data,
message=f"Fuel '{data.get(EcalcYamlKeywords.fuel)}' does not exist",
error_key="fuel",
) from e

try:
return compressor_system_yaml.to_dto(
consumes=consumes,
Expand All @@ -126,22 +122,6 @@ def from_yaml_to_dto(
if data[EcalcYamlKeywords.type] == ComponentType.PUMP_SYSTEM_V2:
pump_system_yaml = YamlPumpSystem(**data)

fuel = None
if consumes == ConsumptionType.FUEL:
try:
fuel = _resolve_fuel(
data.get(EcalcYamlKeywords.fuel),
default_fuel,
self.__references,
target_period=self._target_period,
)
except ValueError as e:
raise DataValidationError(
data=data,
message=f"Fuel '{data.get(EcalcYamlKeywords.fuel)}' does not exist",
error_key="fuel",
) from e

try:
return pump_system_yaml.to_dto(
consumes=consumes,
Expand All @@ -164,17 +144,6 @@ def from_yaml_to_dto(
raise DtoValidationError(data=data, validation_error=e) from e

if consumes == ConsumptionType.FUEL:
try:
fuel = _resolve_fuel(
data.get(EcalcYamlKeywords.fuel), default_fuel, self.__references, target_period=self._target_period
)
except ValueError as e:
raise DataValidationError(
data=data,
message=f"Fuel '{data.get(EcalcYamlKeywords.fuel)}' does not exist",
error_key="fuel",
) from e

try:
fuel_consumer_name = data.get(EcalcYamlKeywords.name)
return dto.FuelConsumer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,5 @@ def to_dto(
consumes=consumes,
operational_settings=parsed_operational_settings,
compressors=compressors,
fuel=fuel,
)
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,5 @@ def to_dto(
consumes=consumes,
operational_settings=parsed_operational_settings,
pumps=pumps,
fuel=fuel,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,63 @@
"2026-01-01 00:00:00"
]
}
},
"cbc0427cf665c504a8081b89cf167ee3": {
"co2": {
"name": "co2",
"quota": {
"regularity": [
1.0,
1.0
],
"timesteps": [
"2022-01-01 00:00:00",
"2026-01-01 00:00:00"
],
"typ": "STREAM_DAY",
"unit": "NOK/d",
"values": [
0.0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 co2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 quota for co2, 85.8 tons co2 (rate below)

0.0
]
},
"rate": {
"regularity": [
1.0,
1.0
],
"timesteps": [
"2022-01-01 00:00:00",
"2026-01-01 00:00:00"
],
"typ": "STREAM_DAY",
"unit": "t/d",
"values": [
85.8,
85.8
]
},
"tax": {
"regularity": [
1.0,
1.0
],
"timesteps": [
"2022-01-01 00:00:00",
"2026-01-01 00:00:00"
],
"typ": "STREAM_DAY",
"unit": "NOK/d",
"values": [
58890.0,
58890.0
]
},
"timesteps": [
"2022-01-01 00:00:00",
"2026-01-01 00:00:00"
]
}
}
},
"variables_map": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def consumer_system_v2_dto() -> DTOCase:
user_defined_category={datetime(2022, 1, 1): ConsumerUserDefinedCategoryType.COMPRESSOR},
regularity=regularity,
consumes=ConsumptionType.FUEL,
fuel=fuel,
operational_settings={
datetime(2022, 1, 1, 0, 0): [
dto.components.CompressorSystemOperationalSetting(
Expand Down