1+ from collections .abc import Callable
12from typing import Union
23
34from pydantic import ValidationError
1516from libecalc .domain .process .pump .pump import PumpModelDTO
1617from libecalc .domain .resource import Resource , Resources
1718from libecalc .presentation .yaml .file_context import FileContext , FileMark
18- from libecalc .presentation .yaml .mappers .energy_model_factory import EnergyModelFactory
1919from libecalc .presentation .yaml .mappers .utils import (
2020 YAML_UNIT_MAPPING ,
2121 chart_curves_as_resource_to_dto_format ,
4545# Used here to make pydantic understand which object to instantiate.
4646EnergyModelUnionType = Union [GeneratorSetModel , TabularEnergyFunction , CompressorTrainSampledDTO ]
4747
48- energy_model_type_map = {
48+ energy_model_type_map : dict [ str , EnergyModelType | ChartType ] = {
4949 EcalcYamlKeywords .facility_type_electricity2fuel : EnergyModelType .GENERATOR_SET_SAMPLED ,
5050 EcalcYamlKeywords .facility_type_pump_chart_single_speed : ChartType .SINGLE_SPEED ,
5151 EcalcYamlKeywords .facility_type_pump_chart_variable_speed : ChartType .VARIABLE_SPEED ,
@@ -76,7 +76,8 @@ def _get_column_or_none(resource: Resource, header: str) -> list[float | int | s
7676
7777
7878def _create_compressor_train_sampled_dto_model_data (
79- resource : Resource , facility_data : YamlCompressorTabularModel , ** kwargs
79+ resource : Resource ,
80+ facility_data : YamlCompressorTabularModel ,
8081) -> CompressorTrainSampledDTO :
8182 # kwargs just to allow this to be used with _default_facility_to_dto_model_data which needs type until we have
8283 # replaced _default_facility_to_dto_model_data and have separate functions for all types
@@ -115,7 +116,8 @@ def _create_compressor_train_sampled_dto_model_data(
115116
116117
117118def _create_pump_model_single_speed_dto_model_data (
118- resource : Resource , facility_data : YamlPumpChartSingleSpeed , ** kwargs
119+ resource : Resource ,
120+ facility_data : YamlPumpChartSingleSpeed ,
119121) -> PumpModelDTO :
120122 chart_data = get_single_speed_chart_data (resource = resource )
121123
@@ -144,7 +146,8 @@ def _create_pump_model_single_speed_dto_model_data(
144146
145147
146148def _create_pump_chart_variable_speed_dto_model_data (
147- resource : Resource , facility_data : YamlPumpChartVariableSpeed , ** kwargs
149+ resource : Resource ,
150+ facility_data : YamlPumpChartVariableSpeed ,
148151) -> PumpModelDTO :
149152 curves_data = chart_curves_as_resource_to_dto_format (resource = resource )
150153
@@ -177,47 +180,41 @@ def _create_pump_chart_variable_speed_dto_model_data(
177180 )
178181
179182
180- def _create_generator_set_dto_model_data (
181- resource : Resource , facility_data : YamlGeneratorSetModel , ** kwargs
183+ def _create_generator_set_model (
184+ resource : Resource ,
185+ facility_data : YamlGeneratorSetModel ,
182186) -> GeneratorSetModel :
183187 # Extract adjustment constants from facility data
184188 adjustment_constant = _get_adjustment_constant (facility_data )
185189 adjustment_factor = _get_adjustment_factor (facility_data )
186190
187- # Ensure the 'name' field is present in facility data
188- name = getattr (facility_data , "name" , "default_generator_set_name" )
189-
190191 # Create and return the GeneratorSetProcessUnit instance
191192 return GeneratorSetModel (
192- name = name ,
193+ name = facility_data . name ,
193194 resource = resource ,
194195 energy_usage_adjustment_constant = adjustment_constant ,
195196 energy_usage_adjustment_factor = adjustment_factor ,
196197 )
197198
198199
199- def _default_facility_to_dto_model_data (
200- resource : Resource , typ : EnergyModelType , facility_data : YamlFacilityModelBase
201- ) -> EnergyModelUnionType :
200+ def _create_tabulated_model (resource : Resource , facility_data : YamlFacilityModelBase ) -> EnergyModelUnionType :
202201 resource_headers = resource .get_headers ()
203- resource_data = [resource .get_column (header ) for header in resource_headers ]
204-
205- model_data = {
206- "typ" : typ ,
207- "headers" : resource_headers ,
208- "data" : resource_data ,
209- "energy_usage_adjustment_constant" : _get_adjustment_constant (data = facility_data ),
210- "energy_usage_adjustment_factor" : _get_adjustment_factor (data = facility_data ),
211- }
202+ resource_data = [resource .get_float_column (header ) for header in resource_headers ]
212203
213- return EnergyModelFactory .create (typ , model_data )
204+ return TabularEnergyFunction (
205+ headers = resource_headers ,
206+ data = resource_data ,
207+ energy_usage_adjustment_factor = _get_adjustment_constant (data = facility_data ),
208+ energy_usage_adjustment_constant = _get_adjustment_constant (data = facility_data ),
209+ )
214210
215211
216- facility_input_to_dto_map = {
212+ facility_input_to_dto_map : dict [ EnergyModelType | ChartType , Callable ] = {
217213 EnergyModelType .COMPRESSOR_SAMPLED : _create_compressor_train_sampled_dto_model_data ,
218- EnergyModelType .GENERATOR_SET_SAMPLED : _create_generator_set_dto_model_data ,
214+ EnergyModelType .GENERATOR_SET_SAMPLED : _create_generator_set_model ,
219215 ChartType .SINGLE_SPEED : _create_pump_model_single_speed_dto_model_data ,
220216 ChartType .VARIABLE_SPEED : _create_pump_chart_variable_speed_dto_model_data ,
217+ EnergyModelType .TABULATED : _create_tabulated_model ,
221218}
222219
223220
@@ -238,13 +235,17 @@ def from_yaml_to_dto(self, data: YamlFacilityModel) -> EnergyModel:
238235
239236 typ = energy_model_type_map .get (data .type )
240237
238+ if typ is None :
239+ raise DataValidationError (
240+ data = data .model_dump (),
241+ message = f"Unsupported facility input type '{ data .type } '" ,
242+ dump_flow_style = DumpFlowStyle .BLOCK ,
243+ )
244+
241245 try :
242- return facility_input_to_dto_map .get (
243- typ , # type: ignore[operator, arg-type]
244- _default_facility_to_dto_model_data ,
245- )(
246+ assert typ in facility_input_to_dto_map
247+ return facility_input_to_dto_map [typ ](
246248 resource = resource ,
247- typ = typ ,
248249 facility_data = data ,
249250 )
250251 except ValidationError as ve :
0 commit comments