Skip to content

Commit

Permalink
refactor: move VariablesMap
Browse files Browse the repository at this point in the history
Temporarily move VariablesMap to avoid cyclic imports. It will either
end up in core or not used in core at all later.
  • Loading branch information
jsolaas committed Sep 23, 2024
1 parent 63010f3 commit 1a1e2a2
Show file tree
Hide file tree
Showing 42 changed files with 92 additions and 84 deletions.
4 changes: 2 additions & 2 deletions examples/simple_python_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"from libecalc.application.graph_result import GraphResult\n",
"from libecalc.application.energy_calculator import EnergyCalculator\n",
"from datetime import datetime\n",
"from libecalc.common.variables import VariablesMap\n",
"from libecalc.dto import (\n",
" DirectConsumerFunction,\n",
" Asset,\n",
Expand All @@ -103,7 +104,6 @@
" FuelConsumer,\n",
" Emission,\n",
" FuelType,\n",
" VariablesMap,\n",
")"
]
},
Expand Down Expand Up @@ -895,7 +895,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions src/libecalc/application/energy_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from libecalc.common.temporal_model import TemporalModel
from libecalc.common.units import Unit
from libecalc.common.utils.rates import TimeSeriesInt, TimeSeriesString
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.consumer_system import ConsumerSystem
from libecalc.core.consumers.factory import create_consumer
from libecalc.core.consumers.generator_set import Genset
Expand Down Expand Up @@ -42,7 +43,7 @@ def __init__(
):
self._graph = graph

def evaluate_energy_usage(self, variables_map: dto.VariablesMap) -> Dict[str, EcalcModelResult]:
def evaluate_energy_usage(self, variables_map: VariablesMap) -> Dict[str, EcalcModelResult]:
component_ids = list(reversed(self._graph.sorted_node_ids))
component_dtos = [self._graph.get_node(component_id) for component_id in component_ids]

Expand Down Expand Up @@ -175,7 +176,7 @@ def evaluator(priority: PriorityID):
return Numbers.format_results_to_precision(consumer_results, precision=6)

def evaluate_emissions(
self, variables_map: dto.VariablesMap, consumer_results: Dict[str, EcalcModelResult]
self, variables_map: VariablesMap, consumer_results: Dict[str, EcalcModelResult]
) -> Dict[str, Dict[str, EmissionResult]]:
"""
Calculate emissions for fuel consumers and emitters
Expand Down
6 changes: 3 additions & 3 deletions src/libecalc/application/graph_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel

from libecalc import dto
from libecalc.common.variables import VariablesMap
from libecalc.core.result import ComponentResult, EcalcModelResult
from libecalc.core.result.emission import EmissionResult
from libecalc.dto.component_graph import ComponentGraph
Expand All @@ -11,7 +11,7 @@
class EnergyCalculatorResult(BaseModel):
consumer_results: Dict[str, EcalcModelResult]
emission_results: Dict[str, Dict[str, EmissionResult]]
variables_map: dto.VariablesMap
variables_map: VariablesMap


class GraphResult:
Expand All @@ -20,7 +20,7 @@ def __init__(
graph: ComponentGraph,
consumer_results: Dict[str, EcalcModelResult],
emission_results: Dict[str, Dict[str, EmissionResult]],
variables_map: dto.VariablesMap,
variables_map: VariablesMap,
):
self.graph = graph
self.consumer_results = consumer_results
Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/common/temporal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, Generic, Iterator, List, Tuple, TypeVar

from libecalc.common.time_utils import Period
from libecalc.dto.variables import VariablesMap
from libecalc.common.variables import VariablesMap
from libecalc.expression import Expression

ModelType = TypeVar("ModelType")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/libecalc/core/consumers/base/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from libecalc.common.stream_conditions import TimeSeriesStreamConditions
from libecalc.common.utils.rates import TimeSeriesFloat
from libecalc.common.variables import VariablesMap
from libecalc.core.result import EcalcModelResult
from libecalc.dto import VariablesMap


class BaseConsumer(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/core/consumers/generator_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
TimeSeriesBoolean,
TimeSeriesStreamDayRate,
)
from libecalc.common.variables import VariablesMap
from libecalc.core.models.generator import GeneratorModelSampled
from libecalc.core.result import GeneratorSetResult
from libecalc.dto.variables import VariablesMap


class Genset:
Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/core/consumers/legacy_consumer/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TimeSeriesInt,
TimeSeriesStreamDayRate,
)
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.base import BaseConsumer
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
Expand All @@ -43,7 +44,6 @@
)
from libecalc.dto.base import ComponentType
from libecalc.dto.types import ConsumptionType
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from libecalc.common.utils.rates import Rates
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
ConsumerFunctionResult,
Expand All @@ -21,7 +22,6 @@
VariableSpeedCompressorTrainCommonShaftMultipleStreamsAndPressures,
)
from libecalc.core.models.results import CompressorTrainResult
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from abc import ABC, abstractmethod
from typing import List

from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function.results import (
ConsumerFunctionResult,
)
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from libecalc.common.list.list_utils import array_to_list
from libecalc.common.utils.rates import Rates
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
ConsumerFunctionResult,
Expand All @@ -19,7 +20,6 @@
Variable,
VariableExpression,
)
from libecalc.dto import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from libecalc.common.list.list_utils import array_to_list
from libecalc.common.units import Unit
from libecalc.common.utils.rates import Rates, RateType
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
ConsumerFunctionResult,
Expand All @@ -17,7 +18,6 @@
get_power_loss_factor_from_expression,
)
from libecalc.core.models.results import EnergyFunctionGenericResult
from libecalc.dto.variables import VariablesMap


class DirectExpressionConsumerFunction(ConsumerFunction):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from libecalc.common.utils.rates import Rates
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function import (
ConsumerFunction,
ConsumerFunctionResult,
Expand All @@ -14,7 +15,6 @@
get_power_loss_factor_from_expression,
)
from libecalc.core.models.pump import PumpModel
from libecalc.dto import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from numpy.typing import NDArray

from libecalc.dto import VariablesMap
from libecalc.common.variables import VariablesMap
from libecalc.expression import Expression


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from libecalc.common.errors.exceptions import IncompatibleDataError
from libecalc.common.list.list_utils import array_to_list
from libecalc.common.logger import logger
from libecalc.common.variables import VariablesMap
from libecalc.core.consumers.legacy_consumer.consumer_function import ConsumerFunction
from libecalc.core.consumers.legacy_consumer.consumer_function.utils import (
apply_condition,
Expand Down Expand Up @@ -35,7 +36,6 @@
)
from libecalc.core.models.compressor.base import CompressorModel
from libecalc.core.models.pump import PumpModel
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression


Expand Down
3 changes: 2 additions & 1 deletion src/libecalc/core/models/fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from libecalc.common.time_utils import Period
from libecalc.common.units import Unit
from libecalc.common.utils.rates import TimeSeriesStreamDayRate
from libecalc.common.variables import VariablesMap
from libecalc.core.result.emission import EmissionResult


Expand All @@ -24,7 +25,7 @@ def __init__(self, fuel_time_function_dict: Dict[datetime, dto.FuelType]):
self.temporal_fuel_model = TemporalModel(fuel_time_function_dict)

def evaluate_emissions(
self, variables_map: dto.VariablesMap, fuel_rate: NDArray[np.float64]
self, variables_map: VariablesMap, fuel_rate: NDArray[np.float64]
) -> Dict[str, EmissionResult]:
"""Evaluate fuel related expressions and results for a TimeSeriesCollection and a
fuel_rate array.
Expand Down
1 change: 0 additions & 1 deletion src/libecalc/dto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@
)
from libecalc.dto.result_options import ResultOptions
from libecalc.dto.types import FuelType, TimeSeriesType
from libecalc.dto.variables import VariablesMap
2 changes: 1 addition & 1 deletion src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TimeSeriesFloat,
TimeSeriesStreamDayRate,
)
from libecalc.common.variables import VariablesMap
from libecalc.dto.base import (
Component,
ComponentType,
Expand All @@ -40,7 +41,6 @@
convert_expression,
validate_temporal_model,
)
from libecalc.dto.variables import VariablesMap
from libecalc.expression import Expression
from libecalc.presentation.yaml.ltp_validation import (
validate_generator_set_power_from_shore,
Expand Down
3 changes: 2 additions & 1 deletion src/libecalc/fixtures/case_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, NamedTuple, TextIO

from libecalc import dto
from libecalc.common.variables import VariablesMap
from libecalc.presentation.yaml.yaml_entities import MemoryResource


Expand All @@ -21,4 +22,4 @@ def main_file(self) -> TextIO:

class DTOCase(NamedTuple):
ecalc_model: dto.Asset
variables: dto.VariablesMap
variables: VariablesMap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from libecalc import dto
from libecalc.common.units import Unit
from libecalc.common.variables import VariablesMap
from libecalc.expression import Expression
from libecalc.presentation.yaml.mappers.fluid_mapper import MEDIUM_MW_19P4

Expand All @@ -31,7 +32,7 @@ def all_energy_usage_models_variables():
datetime(2019, 1, 1, 0, 0),
datetime(2020, 1, 1, 0, 0),
]
return dto.VariablesMap(time_vector=time_vector, variables=variables)
return VariablesMap(time_vector=time_vector, variables=variables)


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from libecalc.common.string.string_utils import generate_id
from libecalc.common.units import Unit
from libecalc.common.utils.rates import RateType
from libecalc.common.variables import VariablesMap
from libecalc.dto import (
CompressorSystemCompressor,
CompressorSystemConsumerFunction,
Expand Down Expand Up @@ -645,7 +646,7 @@ def consumer_system_v2_dto() -> DTOCase:
)
],
),
variables=dto.VariablesMap(
variables=VariablesMap(
time_vector=[
datetime(2022, 1, 1, 0, 0),
datetime(2024, 1, 1, 0, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from libecalc import dto
from libecalc.common.variables import VariablesMap
from libecalc.dto.base import (
ComponentType,
ConsumerUserDefinedCategoryType,
Expand Down Expand Up @@ -292,5 +293,5 @@ def consumer_with_time_slots_models_dto(
)
],
),
variables=dto.VariablesMap(time_vector=time_vector, variables={"RATE": [5000] * number_of_years}),
variables=VariablesMap(time_vector=time_vector, variables={"RATE": [5000] * number_of_years}),
)
5 changes: 2 additions & 3 deletions src/libecalc/fixtures/cases/ltp_export/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from libecalc.application.energy_calculator import EnergyCalculator
from libecalc.application.graph_result import GraphResult
from libecalc.common.time_utils import Frequency
from libecalc.common.variables import VariablesMap
from libecalc.presentation.exporter.configs.configs import LTPConfig


def get_consumption(
model: Union[dto.Installation, dto.Asset], variables: dto.VariablesMap, time_vector: List[datetime]
):
def get_consumption(model: Union[dto.Installation, dto.Asset], variables: VariablesMap, time_vector: List[datetime]):
model = model
graph = model.get_graph()
energy_calculator = EnergyCalculator(graph=graph)
Expand Down
11 changes: 6 additions & 5 deletions src/libecalc/presentation/json_result/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TimeSeriesInt,
TimeSeriesRate,
)
from libecalc.common.variables import VariablesMap
from libecalc.core.result.emission import EmissionResult
from libecalc.dto import CompressorSystemConsumerFunction
from libecalc.dto.base import ComponentType
Expand Down Expand Up @@ -265,7 +266,7 @@ def _compute_aggregated_power(


def _evaluate_installations(
graph_result: GraphResult, variables_map: libecalc.dto.VariablesMap
graph_result: GraphResult, variables_map: VariablesMap
) -> List[libecalc.presentation.json_result.result.InstallationResult]:
"""
All subcomponents have already been evaluated, here we basically collect and aggregate the results
Expand Down Expand Up @@ -807,7 +808,7 @@ def get_asset_result(graph_result: GraphResult) -> libecalc.presentation.json_re
timesteps=model.timesteps,
values=[math.nan] * len(model.timesteps),
unit=Unit.ACTUAL_VOLUMETRIC_M3_PER_HOUR,
), # not relevant for compressor train, only for stage
), # not relevant for compressor train, only for stage
standard_rate_sm3_per_day=TimeSeriesRate(
timesteps=model.timesteps,
values=model.inlet_stream_condition.standard_rate_sm3_per_day
Expand All @@ -823,7 +824,7 @@ def get_asset_result(graph_result: GraphResult) -> libecalc.presentation.json_re
unit=Unit.STANDARD_CUBIC_METER_PER_DAY,
rate_type=RateType.STREAM_DAY,
regularity=regularity.for_timesteps(model.timesteps).values,
), # not relevant for compressor train, only for stage
), # not relevant for compressor train, only for stage
kappa=TimeSeriesFloat(
timesteps=model.timesteps,
values=model.inlet_stream_condition.kappa
Expand Down Expand Up @@ -875,7 +876,7 @@ def get_asset_result(graph_result: GraphResult) -> libecalc.presentation.json_re
timesteps=model.timesteps,
values=[math.nan] * len(model.timesteps),
unit=Unit.ACTUAL_VOLUMETRIC_M3_PER_HOUR,
), # not relevant for compressor train, only for stage
), # not relevant for compressor train, only for stage
standard_rate_sm3_per_day=TimeSeriesRate(
timesteps=model.timesteps,
values=model.outlet_stream_condition.standard_rate_sm3_per_day
Expand All @@ -891,7 +892,7 @@ def get_asset_result(graph_result: GraphResult) -> libecalc.presentation.json_re
unit=Unit.STANDARD_CUBIC_METER_PER_DAY,
rate_type=RateType.STREAM_DAY,
regularity=regularity.for_timesteps(model.timesteps).values,
), # not relevant for compressor train, only for stage
), # not relevant for compressor train, only for stage
kappa=TimeSeriesFloat(
timesteps=model.timesteps,
values=model.outlet_stream_condition.kappa
Expand Down
Loading

0 comments on commit 1a1e2a2

Please sign in to comment.