monee 2.1.0__tar.gz → 2.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. {monee-2.1.0/src/monee.egg-info → monee-2.3.0}/PKG-INFO +13 -6
  2. {monee-2.1.0 → monee-2.3.0}/README.md +12 -5
  3. {monee-2.1.0 → monee-2.3.0}/pyproject.toml +1 -1
  4. {monee-2.1.0 → monee-2.3.0}/src/monee/__init__.py +4 -2
  5. {monee-2.1.0 → monee-2.3.0}/src/monee/express/__init__.py +124 -19
  6. {monee-2.1.0 → monee-2.3.0}/src/monee/io/from_pandapower.py +46 -23
  7. {monee-2.1.0 → monee-2.3.0}/src/monee/io/native.py +2 -0
  8. {monee-2.1.0 → monee-2.3.0}/src/monee/model/branch.py +61 -12
  9. {monee-2.1.0 → monee-2.3.0}/src/monee/model/child.py +34 -10
  10. {monee-2.1.0 → monee-2.3.0}/src/monee/model/core.py +50 -1
  11. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/islanding/el.py +5 -1
  12. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/linepack.py +22 -11
  13. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/ltc.py +20 -10
  14. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/__init__.py +5 -2
  15. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/bundles.py +43 -19
  16. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/common.py +85 -6
  17. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/milp/gas.py +1 -3
  18. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/milp/heat.py +15 -2
  19. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/__init__.py +2 -1
  20. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/convex/el.py +111 -26
  21. monee-2.3.0/src/monee/model/formulation/miqcqp/convex/gas.py +337 -0
  22. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/nonconvex/el.py +5 -2
  23. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/nonconvex/heat.py +115 -19
  24. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/nlp/gas.py +27 -17
  25. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/nlp/heat.py +55 -16
  26. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/registry.py +25 -2
  27. {monee-2.1.0 → monee-2.3.0}/src/monee/model/grid.py +1 -0
  28. {monee-2.1.0 → monee-2.3.0}/src/monee/model/multi.py +138 -21
  29. {monee-2.1.0 → monee-2.3.0}/src/monee/model/network.py +13 -0
  30. {monee-2.1.0 → monee-2.3.0}/src/monee/model/node.py +80 -29
  31. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/core/hydraulics.py +5 -2
  32. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/gf.py +21 -7
  33. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/smooth.py +64 -5
  34. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/wf.py +24 -13
  35. {monee-2.1.0 → monee-2.3.0}/src/monee/model/storage.py +26 -33
  36. {monee-2.1.0 → monee-2.3.0}/src/monee/network/__init__.py +1 -0
  37. {monee-2.1.0 → monee-2.3.0}/src/monee/network/mes.py +808 -655
  38. {monee-2.1.0 → monee-2.3.0}/src/monee/problem/core.py +283 -29
  39. monee-2.3.0/src/monee/problem/economic_dispatch.py +656 -0
  40. {monee-2.1.0 → monee-2.3.0}/src/monee/problem/metric.py +8 -3
  41. {monee-2.1.0 → monee-2.3.0}/src/monee/problem/min_load_shedding.py +410 -271
  42. monee-2.3.0/src/monee/problem/utils.py +447 -0
  43. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/multi_period.py +308 -90
  44. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/stepper.py +3 -3
  45. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/timeseries.py +165 -40
  46. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/casadi.py +275 -51
  47. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/core.py +500 -141
  48. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/dispatch.py +21 -13
  49. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/gurobipy.py +146 -34
  50. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/infeasibility/pyo.py +6 -5
  51. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/pyo.py +117 -39
  52. {monee-2.1.0 → monee-2.3.0/src/monee.egg-info}/PKG-INFO +13 -6
  53. {monee-2.1.0 → monee-2.3.0}/src/monee.egg-info/SOURCES.txt +2 -1
  54. monee-2.3.0/tests/test_version.py +13 -0
  55. monee-2.1.0/src/monee/model/formulation/miqcqp/convex/gas.py +0 -118
  56. monee-2.1.0/src/monee/problem/economic_dispatch.py +0 -133
  57. monee-2.1.0/src/monee/problem/utils.py +0 -156
  58. {monee-2.1.0 → monee-2.3.0}/LICENSE +0 -0
  59. {monee-2.1.0 → monee-2.3.0}/setup.cfg +0 -0
  60. {monee-2.1.0 → monee-2.3.0}/src/monee/express/structures.py +0 -0
  61. {monee-2.1.0 → monee-2.3.0}/src/monee/io/__init__.py +0 -0
  62. {monee-2.1.0 → monee-2.3.0}/src/monee/io/_utils.py +0 -0
  63. {monee-2.1.0 → monee-2.3.0}/src/monee/io/from_cim.py +0 -0
  64. {monee-2.1.0 → monee-2.3.0}/src/monee/io/from_esdl.py +0 -0
  65. {monee-2.1.0 → monee-2.3.0}/src/monee/io/from_simbench.py +0 -0
  66. {monee-2.1.0 → monee-2.3.0}/src/monee/io/matpower.py +0 -0
  67. {monee-2.1.0 → monee-2.3.0}/src/monee/model/__init__.py +0 -0
  68. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/__init__.py +0 -0
  69. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/core.py +0 -0
  70. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/islanding/__init__.py +0 -0
  71. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/islanding/core.py +0 -0
  72. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/islanding/gas.py +0 -0
  73. {monee-2.1.0 → monee-2.3.0}/src/monee/model/extension/islanding/water.py +0 -0
  74. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/core.py +0 -0
  75. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/milp/__init__.py +0 -0
  76. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/convex/__init__.py +0 -0
  77. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/nonconvex/__init__.py +0 -0
  78. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/miqcqp/nonconvex/gas.py +0 -0
  79. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/nlp/__init__.py +0 -0
  80. {monee-2.1.0 → monee-2.3.0}/src/monee/model/formulation/nlp/el.py +0 -0
  81. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/__init__.py +0 -0
  82. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/core/__init__.py +0 -0
  83. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/core/properties.py +0 -0
  84. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/islanding.py +0 -0
  85. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/linear/__init__.py +0 -0
  86. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/misoc/__init__.py +0 -0
  87. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/misoc/pf.py +0 -0
  88. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/__init__.py +0 -0
  89. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/ac.py +0 -0
  90. {monee-2.1.0 → monee-2.3.0}/src/monee/model/phys/nonlinear/hf.py +0 -0
  91. {monee-2.1.0 → monee-2.3.0}/src/monee/network/bench/__init__.py +0 -0
  92. {monee-2.1.0 → monee-2.3.0}/src/monee/network/bench/restoration.py +0 -0
  93. {monee-2.1.0 → monee-2.3.0}/src/monee/network/res.py +0 -0
  94. {monee-2.1.0 → monee-2.3.0}/src/monee/problem/__init__.py +0 -0
  95. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/__init__.py +0 -0
  96. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/core.py +0 -0
  97. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/result_utils.py +0 -0
  98. {monee-2.1.0 → monee-2.3.0}/src/monee/simulation/step_state.py +0 -0
  99. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/__init__.py +0 -0
  100. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/gekko.py +0 -0
  101. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/infeasibility/__init__.py +0 -0
  102. {monee-2.1.0 → monee-2.3.0}/src/monee/solver/infeasibility/apm.py +0 -0
  103. {monee-2.1.0 → monee-2.3.0}/src/monee/visualization/__init__.py +0 -0
  104. {monee-2.1.0 → monee-2.3.0}/src/monee/visualization/result_visualization.py +0 -0
  105. {monee-2.1.0 → monee-2.3.0}/src/monee/visualization/visualization.py +0 -0
  106. {monee-2.1.0 → monee-2.3.0}/src/monee.egg-info/dependency_links.txt +0 -0
  107. {monee-2.1.0 → monee-2.3.0}/src/monee.egg-info/requires.txt +0 -0
  108. {monee-2.1.0 → monee-2.3.0}/src/monee.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: monee
3
- Version: 2.1.0
3
+ Version: 2.3.0
4
4
  Summary: Framework for calculating the steady-state energy flow and for solving optimization problems in coupled energy grids (gas, heat, electricity)
5
5
  Author-email: Rico Schrage <rico.schrage@uol.de>
6
6
  License-Expression: MIT
@@ -70,7 +70,7 @@ Dynamic: license-file
70
70
 
71
71
  ---
72
72
 
73
- **monee** is a Python framework for steady-state simulation and optimal energy flow of **multi-energy systems (MES)**. It models electricity, gas, and district-heat networks in a single unified directed graph, couples them via standard conversion units (CHP, P2H, P2G, G2P, G2H), and exposes the coupled system to three solver back-ends (CasADi, GEKKO, and Pyomo). The framework is designed for research workflows that require flexible problem formulation, reproducible benchmarks, and straightforward integration with external optimisers.
73
+ **monee** is a Python framework for steady-state simulation and optimal energy flow of **multi-energy systems (MES)**. It models electricity, gas, and district-heat networks in a single unified directed graph, couples them via standard conversion units (CHP, P2H, P2G, G2P, G2H), and hands the coupled system to third-party solvers through four back-ends (for CasADi, GEKKO, Pyomo, and gurobipy). The framework is designed for research workflows that require flexible problem formulation, reproducible benchmarks, and straightforward integration with external optimisers.
74
74
 
75
75
  ## Capabilities
76
76
 
@@ -109,12 +109,12 @@ Requires **Python 3.11+**.
109
109
  pip install monee
110
110
  ```
111
111
 
112
- The core install bundles everything needed for both simulation and the default
113
- optimisation workflows:
112
+ The core install pulls in, as dependencies, the third-party packages needed for
113
+ both simulation and the default optimisation workflows:
114
114
 
115
- - **CasADi**: in-process IPOPT, the default solver when present.
115
+ - **CasADi**: runs the IPOPT solver in-process; monee uses it by default.
116
116
  - **GEKKO**: bundles its own APOPT/BPOPT/IPOPT binaries; used as the IPOPT fallback if CasADi is unavailable.
117
- - **Pyomo**: for MILP/MIQCP problems such as MISOCP optimal power flow.
117
+ - **Pyomo** and **pyscipopt**: hand MILP/MIQCP problems such as MISOCP optimal power flow to the SCIP solver, or to any other solver Pyomo supports.
118
118
 
119
119
  Optional solvers and import formats are pulled in only when you need them:
120
120
 
@@ -207,6 +207,13 @@ print(result.get(mm.PowerGenerator)[["p_mw"]])
207
207
  # 1 6.969836e-09
208
208
  ```
209
209
 
210
+ The same problem dispatches gas and heat as well: pass `gas_cost_default` and
211
+ `heat_cost_default` (prices per MW of gas energy and per MW of heat) and give
212
+ gas sources, heat generators and external grids their own `cost`; coupling
213
+ units such as CHPs then follow the merit order too. See the
214
+ [economic dispatch](https://monee.readthedocs.io/en/latest/problems/economic_dispatch.html)
215
+ page.
216
+
210
217
  For resilience studies, `create_min_load_shedding_problem` instead minimises
211
218
  curtailment with per-carrier operational bounds, returning a `regulation` per
212
219
  demand (`1.0` fully served, `0.0` completely shed). Both problems accept the
@@ -19,7 +19,7 @@
19
19
 
20
20
  ---
21
21
 
22
- **monee** is a Python framework for steady-state simulation and optimal energy flow of **multi-energy systems (MES)**. It models electricity, gas, and district-heat networks in a single unified directed graph, couples them via standard conversion units (CHP, P2H, P2G, G2P, G2H), and exposes the coupled system to three solver back-ends (CasADi, GEKKO, and Pyomo). The framework is designed for research workflows that require flexible problem formulation, reproducible benchmarks, and straightforward integration with external optimisers.
22
+ **monee** is a Python framework for steady-state simulation and optimal energy flow of **multi-energy systems (MES)**. It models electricity, gas, and district-heat networks in a single unified directed graph, couples them via standard conversion units (CHP, P2H, P2G, G2P, G2H), and hands the coupled system to third-party solvers through four back-ends (for CasADi, GEKKO, Pyomo, and gurobipy). The framework is designed for research workflows that require flexible problem formulation, reproducible benchmarks, and straightforward integration with external optimisers.
23
23
 
24
24
  ## Capabilities
25
25
 
@@ -58,12 +58,12 @@ Requires **Python 3.11+**.
58
58
  pip install monee
59
59
  ```
60
60
 
61
- The core install bundles everything needed for both simulation and the default
62
- optimisation workflows:
61
+ The core install pulls in, as dependencies, the third-party packages needed for
62
+ both simulation and the default optimisation workflows:
63
63
 
64
- - **CasADi**: in-process IPOPT, the default solver when present.
64
+ - **CasADi**: runs the IPOPT solver in-process; monee uses it by default.
65
65
  - **GEKKO**: bundles its own APOPT/BPOPT/IPOPT binaries; used as the IPOPT fallback if CasADi is unavailable.
66
- - **Pyomo**: for MILP/MIQCP problems such as MISOCP optimal power flow.
66
+ - **Pyomo** and **pyscipopt**: hand MILP/MIQCP problems such as MISOCP optimal power flow to the SCIP solver, or to any other solver Pyomo supports.
67
67
 
68
68
  Optional solvers and import formats are pulled in only when you need them:
69
69
 
@@ -156,6 +156,13 @@ print(result.get(mm.PowerGenerator)[["p_mw"]])
156
156
  # 1 6.969836e-09
157
157
  ```
158
158
 
159
+ The same problem dispatches gas and heat as well: pass `gas_cost_default` and
160
+ `heat_cost_default` (prices per MW of gas energy and per MW of heat) and give
161
+ gas sources, heat generators and external grids their own `cost`; coupling
162
+ units such as CHPs then follow the merit order too. See the
163
+ [economic dispatch](https://monee.readthedocs.io/en/latest/problems/economic_dispatch.html)
164
+ page.
165
+
159
166
  For resilience studies, `create_min_load_shedding_problem` instead minimises
160
167
  curtailment with per-carrier operational bounds, returning a `regulation` per
161
168
  demand (`1.0` fully served, `0.0` completely shed). Both problems accept the
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "monee"
7
- version = "2.1.0"
7
+ version = "2.3.0"
8
8
  authors = [
9
9
  { name="Rico Schrage", email="rico.schrage@uol.de" },
10
10
  ]
@@ -1,3 +1,5 @@
1
+ __version__ = "2.3.0"
2
+
1
3
  import monee.model as mm
2
4
  import monee.solver as ms
3
5
  import monee.problem as mp
@@ -231,7 +233,7 @@ def run_energy_flow_optimization(
231
233
  result = run_energy_flow_optimization(my_network, my_problem, max_iter=1000, tol=1e-6)
232
234
  """
233
235
  if copy:
234
- net = net.copy()
236
+ net = net._copy_for_solve()
235
237
  return solve(net, optimization_problem, solver, **kwargs)
236
238
 
237
239
 
@@ -278,7 +280,7 @@ def solve_load_shedding_problem(
278
280
  network (Network): The network to optimize, representing the system's topology and parameters.
279
281
  demand_weight: Objective weight per shed demand unit.
280
282
  generator_weight: Objective weight per shed generation unit.
281
- weight_for_load: Optional callable ``weight_for_load(model)`` returning a per-load weight (None falls back to ``demand_weight``).
283
+ weight_for_load: Optional callable ``weight_for_load(model)`` returning a per-load weight, used as given and never rescaled by the priority floor (None falls back to ``demand_weight``).
282
284
  bounds_vm (tuple): Per-unit voltage-magnitude bounds (min, max) for the electrical subsystem.
283
285
  bounds_pressure (tuple): Per-unit pressure bounds (min, max) for the gas subsystem.
284
286
  bounds_t (tuple): Per-unit temperature bounds (min, max) for the thermal subsystem.
@@ -1,3 +1,5 @@
1
+ import copy
2
+
1
3
  import monee.model as mm
2
4
  from monee.model.extension.islanding import GridFormingGenerator, GridFormingSource
3
5
 
@@ -110,7 +112,7 @@ def create_el_branch(
110
112
 
111
113
 
112
114
  def create_line(
113
- network: mm.Network,
115
+ network: mm.Network, # NOSONAR
114
116
  from_node_id,
115
117
  to_node_id,
116
118
  length_m,
@@ -123,10 +125,14 @@ def create_line(
123
125
  on_off=1,
124
126
  max_i_ka=None,
125
127
  max_s_mva=None,
128
+ backup=False,
129
+ c_nf_per_m=0,
126
130
  ):
127
131
  """Add a :class:`PowerLine` between two buses; missing buses are auto-created.
128
132
  ``max_i_ka`` / ``max_s_mva`` set the thermal rating (model defaults apply
129
- when omitted)."""
133
+ when omitted). ``backup=True`` marks a switchable line (e.g. a normally-open
134
+ tie with ``on_off=0``) that load shedding may close. ``c_nf_per_m`` is the
135
+ line capacitance in nF/m (charging susceptance)."""
130
136
  rating_kwargs = {}
131
137
  if max_i_ka is not None:
132
138
  rating_kwargs["max_i_ka"] = max_i_ka
@@ -138,7 +144,9 @@ def create_line(
138
144
  r_ohm_per_m,
139
145
  x_ohm_per_m,
140
146
  parallel,
147
+ backup=backup,
141
148
  on_off=on_off,
149
+ c_nf_per_m=c_nf_per_m,
142
150
  **rating_kwargs,
143
151
  ),
144
152
  from_node_id=from_node_id,
@@ -239,6 +247,47 @@ def create_water_pipe(
239
247
  )
240
248
 
241
249
 
250
+ def split_pipe(network: mm.Network, branch_id, n):
251
+ """Replace the pipe *branch_id* by *n* equal segments in series, joined by
252
+ ``n - 1`` new junctions on its grid. Each segment is a copy of the pipe's
253
+ model with ``length_m / n`` (diameter, roughness, insulation unchanged),
254
+ named ``<pipe name>.<i>``. Returns the ``n + 1`` node ids along the chain,
255
+ from the pipe's from-node to its to-node; ``n = 1`` leaves the pipe as is.
256
+ Works for water and gas pipes."""
257
+ if n < 1:
258
+ raise ValueError(f"split_pipe needs n >= 1 segments, got {n!r}.")
259
+ branch = network.branch_by_id(branch_id)
260
+ if n == 1:
261
+ return [branch.from_node_id, branch.to_node_id]
262
+
263
+ def name(i):
264
+ return None if branch.name is None else f"{branch.name}.{i}"
265
+
266
+ chain = [branch.from_node_id]
267
+ chain += [
268
+ network.node(mm.Junction(), grid=branch.grid, name=name(i)) for i in range(1, n)
269
+ ]
270
+ chain.append(branch.to_node_id)
271
+ network.remove_branch(branch_id)
272
+ for i in range(n):
273
+ model = copy.deepcopy(branch.model)
274
+ model.length_m = branch.model.length_m / n
275
+ segment = network.branch_by_id(
276
+ network.branch(
277
+ model,
278
+ chain[i],
279
+ chain[i + 1],
280
+ formulation=branch.formulation,
281
+ constraints=list(branch.constraints),
282
+ grid=branch.grid,
283
+ name=name(i),
284
+ )
285
+ )
286
+ segment.formulation_pinned = branch.formulation_pinned
287
+ segment.active = branch.active
288
+ return chain
289
+
290
+
242
291
  def create_el_child(
243
292
  network: mm.Network,
244
293
  model,
@@ -456,6 +505,9 @@ def create_ext_hydr_grid(
456
505
  free_pressure_bounds: (lo, hi) tuple in pu; instead of pinning the
457
506
  pressure at pressure_pu, leave it a Var bounded to [lo, hi].
458
507
  Default None (pressure pinned).
508
+ cost: forwarded to :class:`ExtHydrGrid`; the economic dispatch
509
+ price per MW of gas energy (gas grid) or of island heat (water
510
+ grid).
459
511
  """
460
512
  return network.child_to(
461
513
  mm.ExtHydrGrid(
@@ -498,6 +550,9 @@ def create_source(
498
550
  grid_key: grid key used when the junction is auto-created.
499
551
  t_k: temperature of the injected stream in K. Default None credits
500
552
  the injection at the junction's own mixed temperature.
553
+ cost: currency per MW of higher heating value, forwarded to
554
+ :class:`Source`; prices a gas source in the economic dispatch
555
+ objective.
501
556
  """
502
557
  return network.child_to(
503
558
  mm.Source(mass_flow_kgs, t_k=t_k, **kwargs),
@@ -580,7 +635,9 @@ def create_heat_generator(
580
635
 
581
636
  Part load in one line: drive the stored ``q_mw_heat`` attribute; a nodal
582
637
  heat source has no mass flow of its own, so the exchangers' constant-flow
583
- vs variable-flow distinction does not apply."""
638
+ vs variable-flow distinction does not apply. ``cost=`` (currency per MW
639
+ heat, forwarded to the model) prices it in the economic dispatch
640
+ objective."""
584
641
  return create_water_child(
585
642
  network,
586
643
  mm.HeatGenerator(q_mw=q_mw, **kwargs),
@@ -604,7 +661,16 @@ def create_heat_load(
604
661
 
605
662
  Part load in one line: drive the stored ``q_mw_heat`` attribute; a nodal
606
663
  heat load has no mass flow of its own, so the exchangers' constant-flow
607
- vs variable-flow distinction does not apply."""
664
+ vs variable-flow distinction does not apply.
665
+
666
+ The heat comes out of the junction's mixed stream (the nodal heat
667
+ balance), so every stream leaving the junction, the downstream supply
668
+ pipe included, leaves at the cooled temperature; a Sink on the same
669
+ junction drains water at that temperature and does not carry the heat
670
+ away separately. Along a supply line the temperature therefore drops at
671
+ every node-based consumer. A consumer that should cool only its own flow
672
+ is a branch from supply to return
673
+ (:func:`create_heat_exchanger` / :func:`create_passive_heat_exchanger`)."""
608
674
  return create_water_child(
609
675
  network,
610
676
  mm.HeatLoad(q_mw=q_mw, **kwargs),
@@ -615,6 +681,14 @@ def create_heat_load(
615
681
  )
616
682
 
617
683
 
684
+ def _reject_cost_on_consumer(cost, q_mw):
685
+ if cost is not None and q_mw > 0:
686
+ raise ValueError(
687
+ "cost prices heat supply; a consuming exchanger (q_mw > 0) cannot "
688
+ f"carry one, got q_mw={q_mw!r}, cost={cost!r}."
689
+ )
690
+
691
+
618
692
  def create_heat_exchanger(
619
693
  network: mm.Network,
620
694
  from_node_id,
@@ -625,10 +699,13 @@ def create_heat_exchanger(
625
699
  constraints=None,
626
700
  grid=None,
627
701
  name=None,
702
+ cost=None,
628
703
  ):
629
704
  """Add a heat-exchanger branch. ``q_mw > 0`` gives a
630
705
  :class:`HeatExchangerLoad` (consumption); ``q_mw < 0`` a
631
- :class:`HeatExchangerGenerator` (injection).
706
+ :class:`HeatExchangerGenerator` (injection). ``cost`` (currency per MW
707
+ heat) prices a generating exchanger in the economic dispatch objective; a
708
+ consuming one rejects it.
632
709
 
633
710
  This is the fixed-flow exchanger: a numeric ``q_mw`` pins the branch mass
634
711
  flow to the design flow ``|q_mw| * 1e6 / (c_p * T_delta_design_K)`` (a 30 K
@@ -645,6 +722,7 @@ def create_heat_exchanger(
645
722
  together (variable flow at the design temperature spread), while driving
646
723
  ``q_mw_set`` changes the duty at the constant design mass flow (the
647
724
  temperature spread moves instead of the flow)."""
725
+ _reject_cost_on_consumer(cost, q_mw)
648
726
  return network.branch(
649
727
  mm.HeatExchangerLoad(
650
728
  q_mw=-q_mw,
@@ -656,6 +734,7 @@ def create_heat_exchanger(
656
734
  q_mw=-q_mw,
657
735
  mass_flow_design_kgs=mass_flow_design_kgs,
658
736
  regulation=regulation,
737
+ cost=cost,
659
738
  ),
660
739
  from_node_id=from_node_id,
661
740
  to_node_id=to_node_id,
@@ -677,6 +756,7 @@ def create_passive_heat_exchanger(
677
756
  constraints=None,
678
757
  grid=None,
679
758
  name=None,
759
+ cost=None,
680
760
  ):
681
761
  """Add a passive heat-exchanger branch: a fixed ``q_mw`` injected into or
682
762
  extracted from the free-flowing water stream, with the temperature change
@@ -689,14 +769,20 @@ def create_passive_heat_exchanger(
689
769
 
690
770
  Part load in one line: drive ``q_mw_set`` or ``regulation`` (they multiply
691
771
  into the duty); the mass flow follows the surrounding hydraulics either
692
- way, so there is no constant-flow vs variable-flow choice here."""
772
+ way, so there is no constant-flow vs variable-flow choice here.
773
+
774
+ ``cost`` (currency per MW heat) prices a generating exchanger in the
775
+ economic dispatch objective; a consuming one rejects it."""
776
+ _reject_cost_on_consumer(cost, q_mw)
693
777
  # Pass -q_mw to match create_heat_exchanger: both model bases store
694
778
  # q_mw_set = -q_mw, so a positive public q_mw must be negated here too for
695
779
  # the active and passive wrappers to agree on the load/generator sign.
696
780
  return network.branch(
697
781
  mm.PassiveHeatExchangerLoad(-q_mw, diameter_m, temperature_ext_k)
698
782
  if q_mw > 0
699
- else mm.PassiveHeatExchangerGenerator(-q_mw, diameter_m, temperature_ext_k),
783
+ else mm.PassiveHeatExchangerGenerator(
784
+ -q_mw, diameter_m, temperature_ext_k, cost=cost
785
+ ),
700
786
  from_node_id=from_node_id,
701
787
  to_node_id=to_node_id,
702
788
  constraints=constraints,
@@ -1114,15 +1200,24 @@ def create_trafo(
1114
1200
  constraints=None,
1115
1201
  grid=None,
1116
1202
  name=None,
1203
+ max_i_ka=None,
1204
+ tap=1.0,
1117
1205
  ):
1118
- """Add a two-winding :class:`Trafo`. from-node = LV side, to-node = HV side."""
1206
+ """Add a two-winding :class:`Trafo`. from-node = LV side, to-node = HV side.
1207
+ ``max_i_ka`` sets the current rating behind the loading (model default when
1208
+ omitted). ``tap`` is the off-nominal turns ratio at the from-node; see
1209
+ :class:`~monee.model.branch.Trafo`."""
1210
+ model = mm.Trafo(
1211
+ vk_percent=vk_percent,
1212
+ vkr_percent=vkr_percent,
1213
+ sn_trafo_mva=sn_trafo_mva,
1214
+ shift=shift,
1215
+ tap=tap,
1216
+ )
1217
+ if max_i_ka is not None:
1218
+ model.max_i_ka = max_i_ka
1119
1219
  return network.branch(
1120
- mm.Trafo(
1121
- vk_percent=vk_percent,
1122
- vkr_percent=vkr_percent,
1123
- sn_trafo_mva=sn_trafo_mva,
1124
- shift=shift,
1125
- ),
1220
+ model,
1126
1221
  from_node_id=from_node_id,
1127
1222
  to_node_id=to_node_id,
1128
1223
  constraints=constraints,
@@ -1179,6 +1274,9 @@ def create_gas_ext_grid(
1179
1274
  free_pressure_bounds: (lo, hi) tuple in pu; instead of pinning the
1180
1275
  pressure at pressure_pu, leave it a Var bounded to [lo, hi].
1181
1276
  Default None (pressure pinned).
1277
+ cost: forwarded to :class:`ExtHydrGrid`; currency per MW of higher
1278
+ heating value in the economic dispatch objective (import charged,
1279
+ export credited).
1182
1280
  """
1183
1281
  return create_ext_hydr_grid(
1184
1282
  network,
@@ -1211,11 +1309,15 @@ def create_water_ext_grid(
1211
1309
  auto-creating a water junction if needed.
1212
1310
 
1213
1311
  On a heat network this slack behaves as an unlimited backup heat plant:
1214
- it delivers any mass flow at its pinned feed temperature, so lost heat
1215
- sources elsewhere are compensated silently instead of shedding heat load.
1216
- Cap it with ``max_import_kgs`` / ``max_export_kgs``, or with
1217
- ``bounds_ext_heat`` on the load-shedding problem, when that is not the
1218
- intended physics (see the concepts/multi_energy page).
1312
+ with its temperature pinned it supplies whatever heat the island balance
1313
+ needs, so lost heat sources elsewhere are compensated silently instead of
1314
+ shedding heat load. Cap it with ``bounds_ext_heat_mw`` on the
1315
+ load-shedding or economic dispatch problem, or pass
1316
+ ``pin_temperature=False``, when that is not the intended physics. On a
1317
+ closed loop (reference on the return) the heat is not carried by the
1318
+ exchanged mass flow, so ``max_import_kgs`` / ``max_export_kgs`` and the
1319
+ load-shedding ``bounds_ext_heat`` (kg/s) only limit it where the slack
1320
+ feeds the loop with mass (see the concepts/multi_energy page).
1219
1321
 
1220
1322
  Args:
1221
1323
  node_id: water junction to attach to; a missing node is auto-created.
@@ -1241,6 +1343,9 @@ def create_water_ext_grid(
1241
1343
  free_pressure_bounds: (lo, hi) tuple in pu; instead of pinning the
1242
1344
  pressure at pressure_pu, leave it a Var bounded to [lo, hi].
1243
1345
  Default None (pressure pinned).
1346
+ cost: forwarded to :class:`ExtHydrGrid`; currency per MW of heat
1347
+ the slack supplies to its island in the economic dispatch
1348
+ objective.
1244
1349
  """
1245
1350
  return create_ext_hydr_grid(
1246
1351
  network,
@@ -89,6 +89,47 @@ def _register_branch(table, row, pair, seen, kinds, meta):
89
89
  return bid
90
90
 
91
91
 
92
+ def _trafo_max_i_ka(net, row):
93
+ """hv-side rated current in kA of a pandapower trafo row (``sn_mva``
94
+ times ``parallel`` and ``df``), or None without a usable rating."""
95
+ sn_mva = _coerce_positive_float(getattr(row, "sn_mva", None), default=None)
96
+ vn_kv = _coerce_positive_float(net.bus.at[row.hv_bus, "vn_kv"], default=None)
97
+ if sn_mva is None or vn_kv is None:
98
+ return None
99
+ parallel = _coerce_positive_int(getattr(row, "parallel", 1))
100
+ df = _coerce_positive_float(getattr(row, "df", 1.0))
101
+ return sn_mva * parallel * df / (math.sqrt(3) * vn_kv)
102
+
103
+
104
+ def _trafo_and_impedance_overrides(net, pair_args, registry) -> dict:
105
+ """Ratings of the trafos; an impedance only takes its branch slot.
106
+ ``pair_args`` and ``registry`` are the trailing arguments of
107
+ :func:`_resolve_branch_pair` and :func:`_register_branch`."""
108
+ overrides = {}
109
+ # ppc branch order is line, trafo, trafo3w, impedance; trafo3w arms end on
110
+ # a unique star bus and never collide with these pairs, so counting
111
+ # impedances here keeps the parallel keys aligned with to_mpc.
112
+ for table, from_col, to_col in (
113
+ ("trafo", "hv_bus", "lv_bus"),
114
+ ("impedance", "from_bus", "to_bus"),
115
+ ):
116
+ for row in _in_service_rows(net, table):
117
+ pair = _resolve_branch_pair(
118
+ getattr(row, from_col), getattr(row, to_col), *pair_args
119
+ )
120
+ if pair is None:
121
+ continue
122
+ bid = _register_branch(table, row, pair, *registry)
123
+ # to_mpc writes a 100 MVA placeholder RATE_A for a trafo without
124
+ # max_loading_percent; rate it at its sn_mva like pandapower's own
125
+ # loading_percent (and like the lines), in the hv (from-side)
126
+ # current basis.
127
+ rating = _trafo_max_i_ka(net, row) if table == "trafo" else None
128
+ if rating is not None:
129
+ overrides[bid] = rating
130
+ return overrides
131
+
132
+
92
133
  def _pp_branch_max_i_ka_overrides(net, bus_to_node, monee_node_ids):
93
134
  overrides = {}
94
135
  kinds = {}
@@ -98,40 +139,22 @@ def _pp_branch_max_i_ka_overrides(net, bus_to_node, monee_node_ids):
98
139
  if not hasattr(net, "bus"):
99
140
  return overrides, kinds, meta
100
141
 
101
- live_buses = _live_bus_ids(net)
142
+ pair_args = (_live_bus_ids(net), bus_to_node, monee_node_ids)
143
+ registry = (seen, kinds, meta)
102
144
 
103
145
  skipped_lines = []
104
146
  # to_mpc drops out-of-service lines, so they consume no branch slot.
105
147
  for row in _in_service_rows(net, "line"):
106
- pair = _resolve_branch_pair(
107
- row.from_bus, row.to_bus, live_buses, bus_to_node, monee_node_ids
108
- )
148
+ pair = _resolve_branch_pair(row.from_bus, row.to_bus, *pair_args)
109
149
  if pair is None:
110
150
  skipped_lines.append(row.Index)
111
151
  continue
112
- bid = _register_branch("line", row, pair, seen, kinds, meta)
152
+ bid = _register_branch("line", row, pair, *registry)
113
153
  max_i_ka = float(row.max_i_ka)
114
154
  parallel = _coerce_positive_int(getattr(row, "parallel", 1))
115
155
  df = _coerce_positive_float(getattr(row, "df", 1.0))
116
156
  overrides[bid] = max_i_ka * parallel * df
117
-
118
- # ppc branch order is line, trafo, trafo3w, impedance; trafo3w arms end on
119
- # a unique star bus and never collide with these pairs, so counting
120
- # impedances here keeps the parallel keys aligned with to_mpc.
121
- for table, from_col, to_col in (
122
- ("trafo", "hv_bus", "lv_bus"),
123
- ("impedance", "from_bus", "to_bus"),
124
- ):
125
- for row in _in_service_rows(net, table):
126
- pair = _resolve_branch_pair(
127
- getattr(row, from_col),
128
- getattr(row, to_col),
129
- live_buses,
130
- bus_to_node,
131
- monee_node_ids,
132
- )
133
- if pair is not None:
134
- _register_branch(table, row, pair, seen, kinds, meta)
157
+ overrides.update(_trafo_and_impedance_overrides(net, pair_args, registry))
135
158
 
136
159
  if skipped_lines:
137
160
  warnings.warn(
@@ -489,6 +489,8 @@ def model_to_dict(model, include_solve_state=False):
489
489
  """
490
490
  result = {}
491
491
  for key, value in model.__dict__.items():
492
+ if key == "_solved_values" and not include_solve_state:
493
+ continue
492
494
  if _encodable(value):
493
495
  result[key] = _encode_value(value, include_solve_state)
494
496
  elif not key.startswith("_"):
@@ -85,6 +85,12 @@ class PowerBranch(GenericPowerBranch, ABC):
85
85
 
86
86
  @model
87
87
  class PowerLine(PowerBranch):
88
+ """Line or cable from per-metre data; ``c_nf_per_m`` is the operating
89
+ capacitance, split as half the charging susceptance onto each end (pi model)."""
90
+
91
+ # Networks saved before line charging existed restore without the attribute.
92
+ c_nf_per_m = 0
93
+
88
94
  def __init__(
89
95
  self,
90
96
  length_m,
@@ -93,6 +99,7 @@ class PowerLine(PowerBranch):
93
99
  parallel,
94
100
  backup=False,
95
101
  on_off=1,
102
+ c_nf_per_m=0,
96
103
  **kwargs,
97
104
  ) -> None:
98
105
  super().__init__(1, 0, backup=backup, on_off=on_off, **kwargs)
@@ -100,6 +107,7 @@ class PowerLine(PowerBranch):
100
107
  self.r_ohm_per_m = r_ohm_per_m
101
108
  self.x_ohm_per_m = x_ohm_per_m
102
109
  self.parallel = parallel
110
+ self.c_nf_per_m = c_nf_per_m
103
111
 
104
112
  def calc_r_x(self, grid: PowerGrid, from_node_model, to_node_model):
105
113
  base_r = from_node_model.base_kv**2 / grid.sn_mva
@@ -107,13 +115,33 @@ class PowerLine(PowerBranch):
107
115
  br_x_pu = self.x_ohm_per_m * self.length_m / base_r / self.parallel
108
116
  return (br_r_pu, br_x_pu)
109
117
 
118
+ def calc_b_pu(self, grid: PowerGrid, from_node_model):
119
+ """Charging susceptance at each end, per unit."""
120
+ base_r = from_node_model.base_kv**2 / grid.sn_mva
121
+ b_s_per_m = 2 * np.pi * grid.f_hz * self.c_nf_per_m * 1e-9
122
+ b_total_s = b_s_per_m * self.length_m * self.parallel
123
+ return b_total_s * base_r / 2
124
+
125
+ def equations(self, grid: PowerGrid, from_node_model, to_node_model, **kwargs):
126
+ self.b_fr_pu = self.b_to_pu = self.calc_b_pu(grid, from_node_model)
127
+ return super().equations(grid, from_node_model, to_node_model, **kwargs)
128
+
110
129
 
111
130
  @model
112
131
  class Trafo(PowerBranch):
132
+ """Two-winding transformer whose rated voltages equal the bus ``base_kv``, so
133
+ its per-unit impedance is ``vk_percent / 100 * sn_mva / sn_trafo_mva``.
134
+
135
+ ``tap`` is the off-nominal turns ratio at the from-node (an LV-side tap for
136
+ the usual from = LV wiring); the impedance sits on the to-node side. A
137
+ pandapower tap ``tap_pos`` with ``tap_step_percent`` on side ``s`` maps to
138
+ ``tap = 1 + tap_pos * tap_step_percent / 100`` with the from-node on side ``s``.
139
+ """
140
+
113
141
  def __init__(
114
- self, vk_percent=12.2, vkr_percent=0.25, sn_trafo_mva=160, shift=0
142
+ self, vk_percent=12.2, vkr_percent=0.25, sn_trafo_mva=160, shift=0, tap=1.0
115
143
  ) -> None:
116
- super().__init__(1, shift)
144
+ super().__init__(tap, shift)
117
145
  if (
118
146
  isinstance(vk_percent, (int, float))
119
147
  and isinstance(vkr_percent, (int, float))
@@ -126,13 +154,11 @@ class Trafo(PowerBranch):
126
154
  self.vk_percent = vk_percent
127
155
  self.vkr_percent = vkr_percent
128
156
  self.sn_trafo_mva = sn_trafo_mva
129
- self.vn_trafo_lv = 1
130
157
 
131
- def calc_r_x(self, grid: PowerGrid, lv_model, hv_model):
132
- tap_lv = np.square(lv_model.base_kv / hv_model.base_kv) * grid.sn_mva
133
- z_sc = self.vk_percent / 100.0 / self.sn_trafo_mva * tap_lv
134
- r_sc = self.vkr_percent / 100.0 / self.sn_trafo_mva * tap_lv
135
- x_sc = np.sign(z_sc) * np.sqrt((z_sc**2 - r_sc**2).astype(float))
158
+ def calc_r_x(self, grid: PowerGrid, from_node_model, to_node_model):
159
+ z_sc = self.vk_percent / 100.0 * grid.sn_mva / self.sn_trafo_mva
160
+ r_sc = self.vkr_percent / 100.0 * grid.sn_mva / self.sn_trafo_mva
161
+ x_sc = np.sign(z_sc) * np.sqrt(np.asarray(z_sc**2 - r_sc**2, dtype=float))
136
162
  return (r_sc, x_sc)
137
163
 
138
164
 
@@ -326,6 +352,16 @@ class HeatExchanger(BranchModel):
326
352
  self.t_from_pu = Var(1, min=0, max=2, name="t_from_pu")
327
353
  self.t_to_pu = Var(1, min=0, max=2, name="t_to_pu")
328
354
 
355
+ def scale_duty(self, factor):
356
+ """Scale the duty by *factor*: the setpoint, the design flow and the
357
+ warm-start seeds derived from them, so a part-load duty solves like a
358
+ heat exchanger built for it (``regulation`` scales the duty within one
359
+ solve instead)."""
360
+ self.q_mw_set = self.q_mw_set * factor
361
+ self.mass_flow_design_kgs = self.mass_flow_design_kgs * factor
362
+ self._q_design_mw = self._q_design_mw * factor
363
+ self._mass_flow_seed_kgs = self._mass_flow_seed_kgs * factor
364
+
329
365
  def equations(self, grid: WaterGrid, from_node_model, to_node_model, **kwargs):
330
366
  eqs = [
331
367
  IntermediateEq(
@@ -364,14 +400,19 @@ class HeatExchangerLoad(HeatExchanger):
364
400
  @model
365
401
  class HeatExchangerGenerator(HeatExchanger):
366
402
  """Heat exchanger that injects heat (``q_mw_set < 0``); the magnitude of
367
- ``q_mw`` is used, its sign is ignored."""
403
+ ``q_mw`` is used, its sign is ignored. ``cost`` (currency per MW heat) is
404
+ read by the economic dispatch objective."""
368
405
 
369
- def __init__(self, q_mw, mass_flow_design_kgs=None, regulation=1) -> None:
406
+ def __init__(
407
+ self, q_mw, mass_flow_design_kgs=None, regulation=1, cost=None
408
+ ) -> None:
370
409
  super().__init__(
371
410
  _normalize_he_q_mw(q_mw, load=False),
372
411
  mass_flow_design_kgs=mass_flow_design_kgs,
373
412
  regulation=regulation,
374
413
  )
414
+ if cost is not None:
415
+ self.cost = cost
375
416
 
376
417
 
377
418
  @model
@@ -462,10 +503,16 @@ class PassiveHeatExchangerLoad(PassiveHeatExchanger):
462
503
  @model
463
504
  class PassiveHeatExchangerGenerator(PassiveHeatExchanger):
464
505
  """Passive heat exchanger that injects heat (``q_mw_set < 0``); the
465
- magnitude of ``q_mw`` is used, its sign is ignored."""
506
+ magnitude of ``q_mw`` is used, its sign is ignored. ``cost`` (currency per
507
+ MW heat) is read by the economic dispatch objective."""
466
508
 
467
509
  def __init__(
468
- self, q_mw, diameter_m, temperature_ext_k=293, loss_coefficient=None
510
+ self,
511
+ q_mw,
512
+ diameter_m,
513
+ temperature_ext_k=293,
514
+ loss_coefficient=None,
515
+ cost=None,
469
516
  ) -> None:
470
517
  super().__init__(
471
518
  _normalize_he_q_mw(q_mw, load=False),
@@ -473,6 +520,8 @@ class PassiveHeatExchangerGenerator(PassiveHeatExchanger):
473
520
  temperature_ext_k=temperature_ext_k,
474
521
  loss_coefficient=loss_coefficient,
475
522
  )
523
+ if cost is not None:
524
+ self.cost = cost
476
525
 
477
526
 
478
527
  @model