Completed
Push — dev ( 68ddc7...49e927 )
by Patrik
58s queued 48s
created

tests.test_models   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 64
dl 0
loc 111
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A test_infeasible_model() 0 26 3
A test_unbounded_model() 0 19 2
A test_multi_period_default_discount_rate() 0 42 2
1
# -*- coding: utf-8 -
2
3
"""Basic tests.
4
5
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
6
by the contributors recorded in the version control history of the file,
7
available from its original location oemof/tests/basic_tests.py
8
9
SPDX-License-Identifier: MIT
10
"""
11
12
import warnings
13
14
import pandas as pd
15
import pytest
16
17
from oemof import solph
18
19
20
def test_infeasible_model():
21
    es = solph.EnergySystem(timeincrement=[1])
22
    bel = solph.buses.Bus(label="bus")
23
    es.add(bel)
24
    es.add(
25
        solph.components.Sink(
26
            inputs={bel: solph.flows.Flow(nominal_capacity=5, fix=[1])}
27
        )
28
    )
29
    es.add(
30
        solph.components.Source(
31
            outputs={
32
                bel: solph.flows.Flow(nominal_capacity=4, variable_costs=5)
33
            }
34
        )
35
    )
36
    m = solph.Model(es)
37
    with pytest.warns(
38
        UserWarning, match="The solver did not return an optimal solution"
39
    ):
40
        m.solve(solver="cbc", allow_nonoptimal=True)
41
42
    with pytest.raises(
43
        RuntimeError, match="The solver did not return an optimal solution"
44
    ):
45
        m.solve(solver="cbc", allow_nonoptimal=False)
46
47
48
def test_unbounded_model():
49
    es = solph.EnergySystem(timeincrement=[1])
50
    bel = solph.buses.Bus(label="bus")
51
    es.add(bel)
52
    # Add a Sink with a higher demand
53
    es.add(solph.components.Sink(inputs={bel: solph.flows.Flow()}))
54
55
    # Add a Source with a very high supply
56
    es.add(
57
        solph.components.Source(
58
            outputs={bel: solph.flows.Flow(variable_costs=-5)}
59
        )
60
    )
61
    m = solph.Model(es)
62
63
    with pytest.raises(
64
        RuntimeError, match="The solver did not return an optimal solution"
65
    ):
66
        m.solve(solver="cbc", allow_nonoptimal=False)
67
68
69
@pytest.mark.filterwarnings(
70
    "ignore:Ensure that your timeindex and timeincrement are"
71
    " consistent.:UserWarning"
72
)
73
@pytest.mark.filterwarnings(
74
    "ignore:CAUTION! You specified the 'periods' attribute:UserWarning"
75
)
76
def test_multi_period_default_discount_rate():
77
    """Test error being thrown for default multi-period discount rate"""
78
    timeindex = pd.date_range(start="2017-01-01", periods=100, freq="D")
79
    es = solph.EnergySystem(
80
        timeindex=timeindex,
81
        timeincrement=[1] * len(timeindex),
82
        periods=[timeindex],
83
        infer_last_interval=False,
84
    )
85
    bel = solph.buses.Bus(label="bus")
86
    es.add(bel)
87
    es.add(
88
        solph.components.Sink(
89
            label="sink",
90
            inputs={
91
                bel: solph.flows.Flow(
92
                    nominal_capacity=5, fix=[1] * len(timeindex)
93
                )
94
            },
95
        )
96
    )
97
    es.add(
98
        solph.components.Source(
99
            label="source",
100
            outputs={
101
                bel: solph.flows.Flow(nominal_capacity=4, variable_costs=5)
102
            },
103
        )
104
    )
105
    msg = (
106
        "By default, a discount_rate of 0.02 is used for a multi-period model."
107
    )
108
    with warnings.catch_warnings(record=True) as w:
109
        solph.Model(es)
110
        assert msg in str(w[0].message)
111