Skip to content

Commit

Permalink
chore!: align format of units (#495)
Browse files Browse the repository at this point in the history
* chore: align format of units
BREAKING CHANGE: New unit-format for venting emitters, aligned to what is used in other parts of yaml-file.
ECALC-1059

---------

Co-authored-by: Jostein Solaas <[email protected]>
  • Loading branch information
frodehk and jsolaas committed May 24, 2024
1 parent 86bf31e commit ab43bd7
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
additional_dependencies: [tomli]
exclude: docs/docs/changelog/changelog.md
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.10.0
hooks:
- id: mypy
files: ^(src/)
Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/core/models/compressor/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ def _invalid_compressor_model_type(compressor_model_dto: Any) -> None:
def create_compressor_model(compressor_model_dto: dto.CompressorModel) -> CompressorModel:
return facility_model_map.get(compressor_model_dto.typ, _invalid_compressor_model_type)(
compressor_model_dto=compressor_model_dto,
) # type: ignore[call-arg]
)
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,9 @@ def methane_venting(regularity) -> YamlVentingEmitter:
emissions=[
YamlVentingEmission(
name="CH4",
rate=YamlEmissionRate(value="FLARE;METHANE_RATE", unit=Unit.KILO_PER_DAY, type=RateType.STREAM_DAY),
rate=YamlEmissionRate(
value="FLARE;METHANE_RATE", unit=Unit.KILO_PER_DAY.name, type=RateType.STREAM_DAY
),
)
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,5 +621,5 @@ INSTALLATIONS:
- NAME: CH4
RATE:
VALUE: FLARE;METHANE_RATE
UNIT: kg/d
UNIT: KILO_PER_DAY
TYPE: STREAM_DAY
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def ltp_oil_loaded_yaml_factory(
emission_factor: float,
rate_types: List[RateType],
fuel_rates: [float],
fuel_rates: List[float],
emission_name: str,
regularity: float,
categories: List[str],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def venting_emitter_yaml_factory(
rate_types: List[RateType],
units: List[Unit],
units: List[str],
emission_names: List[str],
regularity: float,
names: List[str],
Expand All @@ -30,7 +30,7 @@ def venting_emitter_yaml_factory(
installation_name: str = "minimal_installation",
emission_factors: List[float] = None,
oil_rates: List[float] = None,
units_oil_rates: List[Unit] = None,
units_oil_rates: List[str] = None,
include_emitters: bool = True,
include_fuel_consumers: bool = True,
) -> DTOCase:
Expand Down Expand Up @@ -120,7 +120,7 @@ def create_venting_emitters_yaml(
emitter_names: List[str],
emission_names: List[str],
emission_rates: List[float],
units: List[Unit],
units: List[str],
units_oil_rates: List[Unit],
emission_keyword_name: str,
emission_factors: List[float],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def get_emissions(
emission_rate = Rates.to_stream_day(
calendar_day_rates=np.asarray(emission_rate), regularity=regularity_evaluated
).tolist()

emission_rate = Unit.to(emission.rate.unit, Unit.TONS_PER_DAY)(emission_rate)
unit = emission.rate.unit.to_unit()
emission_rate = unit.to(Unit.TONS_PER_DAY)(emission_rate)

emissions[emission.name] = TimeSeriesStreamDayRate(
timesteps=variables_map.time_vector,
Expand Down Expand Up @@ -233,7 +233,8 @@ def get_emissions(
.evaluate(variables=variables_map.variables, fill_length=len(variables_map.time_vector))
.tolist()
)
oil_rates = Unit.to(self.volume.rate.unit, Unit.STANDARD_CUBIC_METER_PER_DAY)(oil_rates)
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 factor is kg/Sm3 and oil rate/volume is Sm3/d. Hence, the emission rate is in kg/d:
Expand Down Expand Up @@ -263,7 +264,8 @@ def get_oil_rates(
regularity=regularity.values,
).tolist()

oil_rates = self.volume.rate.unit.to(Unit.STANDARD_CUBIC_METER_PER_DAY)(oil_rates)
unit = self.volume.rate.unit.to_unit()
oil_rates = unit.to(Unit.STANDARD_CUBIC_METER_PER_DAY)(oil_rates)

return TimeSeriesStreamDayRate(
timesteps=variables_map.time_vector,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import enum
from typing import Literal, Optional

from pydantic import ConfigDict, Field
from typing_extensions import assert_never

from libecalc.common.units import Unit
from libecalc.common.utils.rates import RateType
Expand All @@ -22,17 +24,39 @@ class YamlRate(YamlTimeSeries):
type: Literal[RateType.STREAM_DAY, RateType.CALENDAR_DAY] = RateType.STREAM_DAY


class YamlEmissionRateUnits(enum.Enum):
KILO_PER_DAY = "KILO_PER_DAY"
TONS_PER_DAY = "TONS_PER_DAY"

def to_unit(self) -> Unit:
if self == YamlEmissionRateUnits.KILO_PER_DAY:
return Unit.KILO_PER_DAY
elif self == YamlEmissionRateUnits.TONS_PER_DAY:
return Unit.TONS_PER_DAY

assert_never(self)


class YamlEmissionRate(YamlTimeSeries):
model_config = ConfigDict(title="Rate")

unit: Literal[Unit.KILO_PER_DAY, Unit.TONS_PER_DAY] = Unit.KILO_PER_DAY
unit: YamlEmissionRateUnits = YamlEmissionRateUnits.KILO_PER_DAY
type: Literal[RateType.STREAM_DAY, RateType.CALENDAR_DAY] = RateType.STREAM_DAY


class YamlOilRateUnits(enum.Enum):
STANDARD_CUBIC_METER_PER_DAY = "STANDARD_CUBIC_METER_PER_DAY"

def to_unit(self) -> Unit:
if self == YamlOilRateUnits.STANDARD_CUBIC_METER_PER_DAY:
return Unit.STANDARD_CUBIC_METER_PER_DAY

assert_never(self)


class YamlOilVolumeRate(YamlTimeSeries):
model_config = ConfigDict(title="Rate")

unit: Literal[Unit.STANDARD_CUBIC_METER_PER_DAY]
unit: YamlOilRateUnits = YamlOilRateUnits.STANDARD_CUBIC_METER_PER_DAY
type: Literal[RateType.STREAM_DAY, RateType.CALENDAR_DAY] = RateType.STREAM_DAY


Expand Down
2 changes: 1 addition & 1 deletion src/tests/libecalc/dto/test_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class TestCategories:
def test_venting_emitter_categories(self):
emission = YamlVentingEmission(
name="CH4", rate=YamlEmissionRate(value=4, type=RateType.STREAM_DAY, unit=Unit.KILO_PER_DAY)
name="CH4", rate=YamlEmissionRate(value=4, type=RateType.STREAM_DAY, unit=Unit.KILO_PER_DAY.name)
)

# Check that illegal category raises error
Expand Down
8 changes: 4 additions & 4 deletions src/tests/libecalc/output/results/test_ltp.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_venting_emitters():
dto_sd_kg_per_day = venting_emitter_yaml_factory(
emission_rates=[emission_rate],
regularity=regularity,
units=[Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name],
emission_names=["ch4"],
rate_types=[RateType.STREAM_DAY],
names=["Venting emitter 1"],
Expand All @@ -232,7 +232,7 @@ def test_venting_emitters():
emission_rates=[emission_rate],
regularity=regularity,
rate_types=[RateType.STREAM_DAY],
units=[Unit.TONS_PER_DAY],
units=[Unit.TONS_PER_DAY.name],
emission_names=["ch4"],
names=["Venting emitter 1"],
path=Path(venting_emitters.__path__[0]),
Expand All @@ -242,7 +242,7 @@ def test_venting_emitters():
emission_rates=[emission_rate],
regularity=regularity,
rate_types=[RateType.CALENDAR_DAY],
units=[Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name],
emission_names=["ch4"],
names=["Venting emitter 1"],
path=Path(venting_emitters.__path__[0]),
Expand Down Expand Up @@ -300,7 +300,7 @@ def test_only_venting_emitters_no_fuelconsumers():
dto_case_emitters = venting_emitter_yaml_factory(
emission_rates=[emission_rate],
regularity=regularity,
units=[Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name],
emission_names=["ch4"],
rate_types=[RateType.STREAM_DAY],
include_emitters=True,
Expand Down
18 changes: 10 additions & 8 deletions src/tests/libecalc/presentation/yaml/test_venting_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
)
from libecalc.presentation.yaml.yaml_types.yaml_stream_conditions import (
YamlEmissionRate,
YamlEmissionRateUnits,
YamlOilRateUnits,
YamlOilVolumeRate,
)

Expand Down Expand Up @@ -63,7 +65,7 @@ def test_venting_emitter(variables_map):
name="ch4",
rate=YamlEmissionRate(
value="TSC1;Methane_rate {*} 1.02",
unit=Unit.KILO_PER_DAY,
unit=YamlEmissionRateUnits.KILO_PER_DAY,
type=RateType.STREAM_DAY,
),
)
Expand Down Expand Up @@ -104,7 +106,7 @@ def test_venting_emitter_oil_volume(variables_map):
volume=YamlVentingVolume(
rate=YamlOilVolumeRate(
value="TSC1;Oil_rate",
unit=Unit.STANDARD_CUBIC_METER_PER_DAY,
unit=YamlOilRateUnits.STANDARD_CUBIC_METER_PER_DAY,
type=RateType.STREAM_DAY,
),
emissions=[
Expand Down Expand Up @@ -181,7 +183,7 @@ def test_venting_emitters_direct_multiple_emissions_ltp():
dto_case = venting_emitter_yaml_factory(
emission_rates=emission_rates,
regularity=regularity,
units=[Unit.KILO_PER_DAY, Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name, Unit.KILO_PER_DAY.name],
emission_names=["co2", "ch4"],
rate_types=[RateType.STREAM_DAY],
emission_keyword_name="EMISSIONS",
Expand Down Expand Up @@ -218,8 +220,8 @@ def test_venting_emitters_volume_multiple_emissions_ltp():
path = Path(venting_emitters.__path__[0])
dto_case = venting_emitter_yaml_factory(
regularity=regularity,
units=[Unit.KILO_PER_DAY, Unit.KILO_PER_DAY],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY, Unit.STANDARD_CUBIC_METER_PER_DAY],
units=[Unit.KILO_PER_DAY.name, Unit.KILO_PER_DAY.name],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY.name, Unit.STANDARD_CUBIC_METER_PER_DAY.name],
emission_names=["ch4", "nmvoc"],
emitter_types=["OIL_VOLUME"],
rate_types=[RateType.CALENDAR_DAY],
Expand All @@ -233,7 +235,7 @@ def test_venting_emitters_volume_multiple_emissions_ltp():
dto_case_stream_day = venting_emitter_yaml_factory(
regularity=regularity,
units=[Unit.KILO_PER_DAY, Unit.KILO_PER_DAY],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY, Unit.STANDARD_CUBIC_METER_PER_DAY],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY.name, Unit.STANDARD_CUBIC_METER_PER_DAY.name],
emission_names=["ch4", "nmvoc"],
emitter_types=["OIL_VOLUME"],
rate_types=[RateType.STREAM_DAY],
Expand Down Expand Up @@ -285,7 +287,7 @@ def test_venting_emitters_direct_uppercase_emissions_name():
dto_case = venting_emitter_yaml_factory(
emission_rates=emission_rates,
regularity=regularity,
units=[Unit.KILO_PER_DAY, Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name, Unit.KILO_PER_DAY.name],
emission_names=["CO2", "nmVOC"],
rate_types=[RateType.STREAM_DAY],
emission_keyword_name="EMISSIONS",
Expand All @@ -310,7 +312,7 @@ def test_venting_emitters_volume_uppercase_emissions_name():
dto_case = venting_emitter_yaml_factory(
regularity=regularity,
units=[Unit.KILO_PER_DAY, Unit.KILO_PER_DAY],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY, Unit.STANDARD_CUBIC_METER_PER_DAY],
units_oil_rates=[Unit.STANDARD_CUBIC_METER_PER_DAY.name, Unit.STANDARD_CUBIC_METER_PER_DAY.name],
emission_names=["CO2", "nmVOC"],
emitter_types=["OIL_VOLUME"],
rate_types=[RateType.CALENDAR_DAY],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_wrong_keyword_name_emitters():
venting_emitter_yaml_factory(
emission_rates=[emission_rate],
regularity=regularity,
units=[Unit.KILO_PER_DAY],
units=[Unit.KILO_PER_DAY.name],
emission_names=["ch4"],
rate_types=[RateType.STREAM_DAY],
emission_keyword_name="EMISSION2",
Expand All @@ -41,7 +41,7 @@ def test_wrong_unit_emitters():
venting_emitter_yaml_factory(
emission_rates=[emission_rate],
regularity=regularity,
units=[Unit.STANDARD_CUBIC_METER_PER_DAY],
units=[Unit.STANDARD_CUBIC_METER_PER_DAY.name],
emission_names=["ch4"],
rate_types=[RateType.STREAM_DAY],
emission_keyword_name="EMISSIONS",
Expand All @@ -50,6 +50,30 @@ def test_wrong_unit_emitters():
)

assert (
"Venting emitter 1:\nDIRECT_EMISSION.EMISSIONS[0].RATE.UNIT:\tInput should be "
"<Unit.KILO_PER_DAY: 'kg/d'> or <Unit.TONS_PER_DAY: 't/d'>"
"\nVenting emitter 1:\nDIRECT_EMISSION.EMISSIONS[0].RATE.UNIT:\tInput should be "
f"'{Unit.KILO_PER_DAY.name}' or '{Unit.TONS_PER_DAY.name}'"
) in str(exc.value)


def test_wrong_unit_format_emitters():
"""Test error messages for yaml validation with wrong unit format."""

regularity = 0.2
emission_rate = 10

with pytest.raises(DtoValidationError) as exc:
venting_emitter_yaml_factory(
emission_rates=[emission_rate],
regularity=regularity,
units=["kg/d"],
emission_names=["ch4"],
rate_types=[RateType.STREAM_DAY],
emission_keyword_name="EMISSIONS",
names=["Venting emitter 1"],
path=Path(venting_emitters.__path__[0]),
)

assert (
"\nVenting emitter 1:\nDIRECT_EMISSION.EMISSIONS[0].RATE.UNIT:\tInput should be "
f"'{Unit.KILO_PER_DAY.name}' or '{Unit.TONS_PER_DAY.name}'"
) in str(exc.value)

0 comments on commit ab43bd7

Please sign in to comment.