Skip to content

Commit 0cad8de

Browse files
committed
The Weights API is now used in the modeling equations:
Changes made: 1. elements.py - Flow tracking: # Before: flow_hours = self.flow_rate * self._model.timestep_duration weighted_flow_hours = flow_hours * self._model.cluster_weight tracked_expression=weighted_flow_hours.sum(self._model.temporal_dims) # After: tracked_expression=self._model.weights.sum_temporal(self.flow_rate) 2. elements.py - Load factor total hours: # Before: total_hours = (self._model.timestep_duration * self._model.cluster_weight).sum(self._model.temporal_dims) # After: total_hours = self._model.weights.temporal.sum(self._model.weights.temporal_dims) 3. features.py - Status tracking: # Before: active_hours = self.status * self._model.timestep_duration weighted_active_hours = active_hours * self._model.cluster_weight tracked_expression=weighted_active_hours.sum(self._model.temporal_dims) # After: tracked_expression=self._model.weights.sum_temporal(self.status) 4. features.py - Temporal effects summing (only needs cluster weight since already per-timestep): # Before: weighted_per_timestep = self.total_per_timestep * self._model.cluster_weight temporal_dims = [d for d in self.total_per_timestep.dims if d not in ('period', 'scenario')] # After: weighted_per_timestep = self.total_per_timestep * self._model.weights.cluster self._eq_total.lhs -= weighted_per_timestep.sum(dim=self._model.weights.temporal_dims)
1 parent 9d00c80 commit 0cad8de

2 files changed

Lines changed: 8 additions & 16 deletions

File tree

flixopt/elements.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,10 @@ def _do_modeling(self):
677677
self._constraint_flow_rate()
678678

679679
# Total flow hours tracking (per period)
680-
# Sum over temporal dimensions, weighted by cluster_weight
681-
flow_hours = self.flow_rate * self._model.timestep_duration
682-
weighted_flow_hours = flow_hours * self._model.cluster_weight
683680
ModelingPrimitives.expression_tracking_variable(
684681
model=self,
685682
name=f'{self.label_full}|total_flow_hours',
686-
tracked_expression=weighted_flow_hours.sum(self._model.temporal_dims),
683+
tracked_expression=self._model.weights.sum_temporal(self.flow_rate),
687684
bounds=(
688685
self.element.flow_hours_min if self.element.flow_hours_min is not None else 0,
689686
self.element.flow_hours_max if self.element.flow_hours_max is not None else None,
@@ -840,8 +837,8 @@ def _create_bounds_for_load_factor(self):
840837
# Get the size (either from element or investment)
841838
size = self.investment.size if self.with_investment else self.element.size
842839

843-
# Sum over temporal dimensions, weighted by cluster_weight
844-
total_hours = (self._model.timestep_duration * self._model.cluster_weight).sum(self._model.temporal_dims)
840+
# Total hours in the period (sum of temporal weights)
841+
total_hours = self._model.weights.temporal.sum(self._model.weights.temporal_dims)
845842

846843
# Maximum load factor constraint
847844
if self.element.load_factor_max is not None:

flixopt/features.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,11 @@ def _do_modeling(self):
196196
inactive = self.add_variables(binary=True, short_name='inactive', coords=self._model.get_coords())
197197
self.add_constraints(self.status + inactive == 1, short_name='complementary')
198198

199-
# 3. Total duration tracking using existing pattern
200-
# Sum over temporal dimensions, weighted by cluster_weight
201-
active_hours = self.status * self._model.timestep_duration
202-
weighted_active_hours = active_hours * self._model.cluster_weight
203-
total_hours = (self._model.timestep_duration * self._model.cluster_weight).sum(self._model.temporal_dims)
199+
# 3. Total duration tracking
200+
total_hours = self._model.weights.temporal.sum(self._model.weights.temporal_dims)
204201
ModelingPrimitives.expression_tracking_variable(
205202
self,
206-
tracked_expression=weighted_active_hours.sum(self._model.temporal_dims),
203+
tracked_expression=self._model.weights.sum_temporal(self.status),
207204
bounds=(
208205
self.parameters.active_hours_min if self.parameters.active_hours_min is not None else 0,
209206
self.parameters.active_hours_max
@@ -630,10 +627,8 @@ def _do_modeling(self):
630627

631628
# Add it to the total (cluster_weight handles cluster representation, defaults to 1.0)
632629
# Sum over all temporal dimensions (time, and cluster if present)
633-
weighted_per_timestep = self.total_per_timestep * self._model.cluster_weight
634-
# Get temporal_dims from total_per_timestep (linopy Variable) - its coords are the actual dims
635-
temporal_dims = [d for d in self.total_per_timestep.dims if d not in ('period', 'scenario')]
636-
self._eq_total.lhs -= weighted_per_timestep.sum(dim=temporal_dims)
630+
weighted_per_timestep = self.total_per_timestep * self._model.weights.cluster
631+
self._eq_total.lhs -= weighted_per_timestep.sum(dim=self._model.weights.temporal_dims)
637632

638633
def add_share(
639634
self,

0 commit comments

Comments
 (0)