Skip to content

Commit 1ad03c9

Browse files
committed
Improve fit_to_model_coords
1 parent 0fd69b0 commit 1ad03c9

6 files changed

Lines changed: 35 additions & 31 deletions

File tree

flixopt/components.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,29 +223,29 @@ def transform_data(self, flow_system: 'FlowSystem') -> None:
223223
)
224224
if not isinstance(self.initial_charge_state, str):
225225
self.initial_charge_state = flow_system.fit_to_model_coords(
226-
f'{self.label_full}|initial_charge_state', self.initial_charge_state, has_time_dim=False
226+
f'{self.label_full}|initial_charge_state', self.initial_charge_state, dims=['year', 'scenario']
227227
)
228228
self.minimal_final_charge_state = flow_system.fit_to_model_coords(
229-
f'{self.label_full}|minimal_final_charge_state', self.minimal_final_charge_state, has_time_dim=False
229+
f'{self.label_full}|minimal_final_charge_state', self.minimal_final_charge_state, dims=['year', 'scenario']
230230
)
231231
self.maximal_final_charge_state = flow_system.fit_to_model_coords(
232-
f'{self.label_full}|maximal_final_charge_state', self.maximal_final_charge_state, has_time_dim=False
232+
f'{self.label_full}|maximal_final_charge_state', self.maximal_final_charge_state, dims=['year', 'scenario']
233233
)
234234
self.relative_minimum_final_charge_state = flow_system.fit_to_model_coords(
235235
f'{self.label_full}|relative_minimum_final_charge_state',
236236
self.relative_minimum_final_charge_state,
237-
has_time_dim=False,
237+
dims=['year', 'scenario'],
238238
)
239239
self.relative_maximum_final_charge_state = flow_system.fit_to_model_coords(
240240
f'{self.label_full}|relative_maximum_final_charge_state',
241241
self.relative_maximum_final_charge_state,
242-
has_time_dim=False,
242+
dims=['year', 'scenario'],
243243
)
244244
if isinstance(self.capacity_in_flow_hours, InvestParameters):
245245
self.capacity_in_flow_hours.transform_data(flow_system, f'{self.label_full}|InvestParameters')
246246
else:
247247
self.capacity_in_flow_hours = flow_system.fit_to_model_coords(
248-
f'{self.label_full}|capacity_in_flow_hours', self.capacity_in_flow_hours, has_time_dim=False
248+
f'{self.label_full}|capacity_in_flow_hours', self.capacity_in_flow_hours, dims=['year', 'scenario']
249249
)
250250

251251
def _plausibility_checks(self) -> None:

flixopt/effects.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,30 @@ def transform_data(self, flow_system: 'FlowSystem'):
105105
)
106106

107107
self.minimum_operation = flow_system.fit_to_model_coords(
108-
f'{self.label_full}|minimum_operation', self.minimum_operation, has_time_dim=False
108+
f'{self.label_full}|minimum_operation', self.minimum_operation, dims=['year', 'scenario']
109109
)
110110
self.maximum_operation = flow_system.fit_to_model_coords(
111-
f'{self.label_full}|maximum_operation', self.maximum_operation, has_time_dim=False
111+
f'{self.label_full}|maximum_operation', self.maximum_operation, dims=['year', 'scenario']
112112
)
113113
self.minimum_invest = flow_system.fit_to_model_coords(
114-
f'{self.label_full}|minimum_invest', self.minimum_invest, has_time_dim=False
114+
f'{self.label_full}|minimum_invest', self.minimum_invest, dims=['year', 'scenario']
115115
)
116116
self.maximum_invest = flow_system.fit_to_model_coords(
117-
f'{self.label_full}|maximum_invest', self.maximum_invest, has_time_dim=False
117+
f'{self.label_full}|maximum_invest', self.maximum_invest, dims=['year', 'scenario']
118118
)
119119
self.minimum_total = flow_system.fit_to_model_coords(
120120
f'{self.label_full}|minimum_total',
121121
self.minimum_total,
122-
has_time_dim=False,
122+
dims=['year', 'scenario'],
123123
)
124124
self.maximum_total = flow_system.fit_to_model_coords(
125-
f'{self.label_full}|maximum_total', self.maximum_total, has_time_dim=False
125+
f'{self.label_full}|maximum_total', self.maximum_total, dims=['year', 'scenario']
126126
)
127127
self.specific_share_to_other_effects_invest = flow_system.fit_effects_to_model_coords(
128-
f'{self.label_full}|invest->', self.specific_share_to_other_effects_invest, 'invest', has_time_dim=False
128+
f'{self.label_full}|invest->',
129+
self.specific_share_to_other_effects_invest,
130+
'invest',
131+
dims=['year', 'scenario'],
129132
)
130133

131134
def create_model(self, model: FlowSystemModel) -> 'EffectModel':

flixopt/elements.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,24 @@ def transform_data(self, flow_system: 'FlowSystem'):
252252
self.label_full, self.effects_per_flow_hour, 'per_flow_hour'
253253
)
254254
self.flow_hours_total_max = flow_system.fit_to_model_coords(
255-
f'{self.label_full}|flow_hours_total_max', self.flow_hours_total_max, has_time_dim=False
255+
f'{self.label_full}|flow_hours_total_max', self.flow_hours_total_max, dims=['year', 'scenario']
256256
)
257257
self.flow_hours_total_min = flow_system.fit_to_model_coords(
258-
f'{self.label_full}|flow_hours_total_min', self.flow_hours_total_min, has_time_dim=False
258+
f'{self.label_full}|flow_hours_total_min', self.flow_hours_total_min, dims=['year', 'scenario']
259259
)
260260
self.load_factor_max = flow_system.fit_to_model_coords(
261-
f'{self.label_full}|load_factor_max', self.load_factor_max, has_time_dim=False
261+
f'{self.label_full}|load_factor_max', self.load_factor_max, dims=['year', 'scenario']
262262
)
263263
self.load_factor_min = flow_system.fit_to_model_coords(
264-
f'{self.label_full}|load_factor_min', self.load_factor_min, has_time_dim=False
264+
f'{self.label_full}|load_factor_min', self.load_factor_min, dims=['year', 'scenario']
265265
)
266266

267267
if self.on_off_parameters is not None:
268268
self.on_off_parameters.transform_data(flow_system, self.label_full)
269269
if isinstance(self.size, InvestParameters):
270270
self.size.transform_data(flow_system, self.label_full)
271271
else:
272-
self.size = flow_system.fit_to_model_coords(f'{self.label_full}|size', self.size, has_time_dim=False)
272+
self.size = flow_system.fit_to_model_coords(f'{self.label_full}|size', self.size, dims=['year', 'scenario'])
273273

274274
def _plausibility_checks(self) -> None:
275275
# TODO: Incorporate into Variable? (Lower_bound can not be greater than upper bound

flixopt/flow_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def connect_and_transform(self):
427427
logger.debug('FlowSystem already connected and transformed')
428428
return
429429

430-
self.weights = self.fit_to_model_coords('weights', self.weights, has_time_dim=False)
430+
self.weights = self.fit_to_model_coords('weights', self.weights, dims=['year', 'scenario'])
431431
if self.weights is not None and self.weights.sum() != 1:
432432
logger.warning(
433433
f'Scenario weights are not normalized to 1. This is recomended for a better scaled model. '

flixopt/interface.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ def __init__(self, start: TemporalDataUser, end: TemporalDataUser):
3333
self.has_time_dim = False
3434

3535
def transform_data(self, flow_system: 'FlowSystem', name_prefix: str):
36-
self.start = flow_system.fit_to_model_coords(f'{name_prefix}|start', self.start, has_time_dim=self.has_time_dim)
37-
self.end = flow_system.fit_to_model_coords(f'{name_prefix}|end', self.end, has_time_dim=self.has_time_dim)
36+
dims = None if self.has_time_dim else ['year', 'scenario']
37+
self.start = flow_system.fit_to_model_coords(f'{name_prefix}|start', self.start, dims=dims)
38+
self.end = flow_system.fit_to_model_coords(f'{name_prefix}|end', self.end, dims=dims)
3839

3940

4041
@register_class_for_io
@@ -189,33 +190,33 @@ def transform_data(self, flow_system: 'FlowSystem', name_prefix: str):
189190
label_prefix=name_prefix,
190191
effect_values=self.fix_effects,
191192
label_suffix='fix_effects',
192-
has_time_dim=False,
193+
dims=['year', 'scenario'],
193194
)
194195
self.divest_effects = flow_system.fit_effects_to_model_coords(
195196
label_prefix=name_prefix,
196197
effect_values=self.divest_effects,
197198
label_suffix='divest_effects',
198-
has_time_dim=False,
199+
dims=['year', 'scenario'],
199200
)
200201
self.specific_effects = flow_system.fit_effects_to_model_coords(
201202
label_prefix=name_prefix,
202203
effect_values=self.specific_effects,
203204
label_suffix='specific_effects',
204-
has_time_dim=False,
205+
dims=['year', 'scenario'],
205206
)
206207
if self.piecewise_effects is not None:
207208
self.piecewise_effects.has_time_dim = False
208209
self.piecewise_effects.transform_data(flow_system, f'{name_prefix}|PiecewiseEffects')
209210

210211
self.minimum_size = flow_system.fit_to_model_coords(
211-
f'{name_prefix}|minimum_size', self.minimum_size, has_time_dim=False
212+
f'{name_prefix}|minimum_size', self.minimum_size, dims=['year', 'scenario']
212213
)
213214
self.maximum_size = flow_system.fit_to_model_coords(
214-
f'{name_prefix}|maximum_size', self.maximum_size, has_time_dim=False
215+
f'{name_prefix}|maximum_size', self.maximum_size, dims=['year', 'scenario']
215216
)
216217
if self.fixed_size is not None:
217218
self.fixed_size = flow_system.fit_to_model_coords(
218-
f'{name_prefix}|fixed_size', self.fixed_size, has_time_dim=False
219+
f'{name_prefix}|fixed_size', self.fixed_size, dims=['year', 'scenario']
219220
)
220221

221222
def _plausibility_checks(self, flow_system):
@@ -312,13 +313,13 @@ def transform_data(self, flow_system: 'FlowSystem', name_prefix: str):
312313
f'{name_prefix}|consecutive_off_hours_max', self.consecutive_off_hours_max
313314
)
314315
self.on_hours_total_max = flow_system.fit_to_model_coords(
315-
f'{name_prefix}|on_hours_total_max', self.on_hours_total_max, has_time_dim=False
316+
f'{name_prefix}|on_hours_total_max', self.on_hours_total_max, dims=['year', 'scenario']
316317
)
317318
self.on_hours_total_min = flow_system.fit_to_model_coords(
318-
f'{name_prefix}|on_hours_total_min', self.on_hours_total_min, has_time_dim=False
319+
f'{name_prefix}|on_hours_total_min', self.on_hours_total_min, dims=['year', 'scenario']
319320
)
320321
self.switch_on_total_max = flow_system.fit_to_model_coords(
321-
f'{name_prefix}|switch_on_total_max', self.switch_on_total_max, has_time_dim=False
322+
f'{name_prefix}|switch_on_total_max', self.switch_on_total_max, dims=['year', 'scenario']
322323
)
323324

324325
@property

flixopt/structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def get_coords(
178178
def weights(self) -> Union[int, xr.DataArray]:
179179
"""Returns the scenario weights of the FlowSystem. If None, return weights that are normalized to 1 (one)"""
180180
if self.flow_system.weights is None:
181-
weights = self.flow_system.fit_to_model_coords('weights', 1, has_time_dim=False)
181+
weights = self.flow_system.fit_to_model_coords('weights', 1, dims=['year', 'scenario'])
182182

183183
return weights / weights.sum()
184184

0 commit comments

Comments
 (0)