Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test for constraints #1297

Draft
wants to merge 2 commits into
base: pypsa_linopy_update
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added test/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import pandas as pd
import pypsa
import pytest


@pytest.fixture
def network():
n = pypsa.Network()
n.set_snapshots(pd.date_range("2022-01-01", periods=24, freq="H"))
n.add("Bus", "bus1")
n.add(
"Link",
"battery discharger",
bus0="bus1",
bus1="bus1",
p_nom_extendable=True,
efficiency=0.9,
)
n.add("Link", "battery charger", bus0="bus1", bus1="bus1", p_nom_extendable=True)

# Add links
n.add(
"Link",
"urban central CHP electric",
bus0="bus0",
bus1="bus1",
p_nom_extendable=True,
efficiency=0.9,
p_nom_ratio=1.0,
)
n.add(
"Link",
"urban central CHP heat",
bus0="bus1",
bus1="bus0",
p_nom_extendable=True,
efficiency=0.9,
)

n.optimize.create_model()

return n
51 changes: 51 additions & 0 deletions test/test_solve_network_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
import sys

import pypsa
import pytest

sys.path.append("./scripts")
from test.conftest import network

import _helpers
from solve_network import add_battery_constraints, add_chp_constraints


def test_add_battery_constraints(network):

# Apply the function
add_battery_constraints(network)

constraints = network.model.constraints["Link-charger_ratio"]
assert constraints is not None

# Check that the constraints were added
assert "Link-charger_ratio" in network.model.constraints

# Check the constraint expression
lhs = network.model.constraints["Link-charger_ratio"][0].lhs

assert lhs.equals(
network.model["Link-p_nom"].loc["battery charger"]
- 0.9 * network.model["Link-p_nom"].loc["battery discharger"]
)


def test_chp_constraints(network):

# Apply the function
add_chp_constraints(network)

constraints = network.model.constraints["Link-CHP_ratio"]
assert constraints is not None

# Check that the constraints were added
assert "Link-CHP_ratio" in network.model.constraints

# Check the constraint expression
lhs = network.model.constraints["Link-CHP_ratio"][0].lhs

assert lhs.equals(
network.model["Link-p_nom"].loc["urban central CHP heat"]
- 0.9 * network.model["Link-p_nom"].loc["urban central CHP electric"]
)