Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Move calculation of interval_length to units module.
  • Loading branch information
lumbric committed May 7, 2024
1 parent 80e0f6f commit 34c9bbd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 4 additions & 8 deletions syfop/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

import linopy
import networkx as nx
import numpy as np
import pandas as pd
from networkx.drawing.nx_agraph import graphviz_layout

from syfop.node_base import NodeInputBase, NodeOutputBase
from syfop.units import default_units, ureg
from syfop.units import default_units, interval_length, ureg
from syfop.util import DEFAULT_NUM_TIME_STEPS, timeseries_variable


Expand Down Expand Up @@ -360,25 +359,22 @@ def total_costs(self):
continue
input_flows = list(node.input_flows.values())
assert len(input_flows) == 1, "only one input_flow is supported"
input_flow = input_flows[0]

# this is just a check: atm we support only Node, so input_flows should be plain linopy
# variables without units, but in case of a NodeScalableInput or a NodeFixInput we
# would need to strip units here and use the magnitude.
assert isinstance(input_flows[0], linopy.Variable), "unexpected input_flow type"

# see also https://stackoverflow.com/a/78373992/859591
interval_lengths = np.diff(self.time_coords)
# this works only if equidistant... should have been checked before already.
assert (interval_lengths == interval_lengths[0]).all(), "timestes are not equidistant"
interval_length_h = interval_lengths[0] / np.timedelta64(1, "h")
interval_length_h = interval_length(self.time_coords).to(ureg.h).magnitude

input_flow_unit = default_units[node.input_commodities[0]] # something like MW
input_flow_costs_mag = node.input_flow_costs.to(
self.total_cost_unit / (input_flow_unit * ureg.h)
).magnitude

# something like: EUR/MWh * x h * MW
costs = costs + input_flow_costs_mag * interval_length_h * input_flows[0].sum()
costs = costs + input_flow_costs_mag * interval_length_h * input_flow.sum()

costs = costs + storage_costs

Expand Down
11 changes: 11 additions & 0 deletions syfop/units.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pint
import pint_xarray # noqa: F401

Expand Down Expand Up @@ -50,3 +51,13 @@ def strip_unit(x, commodity):
return x.to(default_units[commodity]).magnitude
else:
return x


def interval_length(time_coords):
# see also https://stackoverflow.com/a/78373992/859591
interval_lengths = np.diff(time_coords)
# this works only if equidistant... should have been checked before already.
assert (interval_lengths == interval_lengths[0]).all(), "timestes are not equidistant"
interval_length_h = interval_lengths[0] / np.timedelta64(1, "h")

return interval_length_h * ureg.h

0 comments on commit 34c9bbd

Please sign in to comment.