Completed
Push — dev ( 8582b4...82307e )
by
unknown
30s queued 19s
created

custom_extra_functionality()   B

Complexity

Conditions 5

Size

Total Lines 100
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 100
rs 7.646
c 0
b 0
f 0
cc 5
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
# SPDX-FileCopyrightText: : 2023- The PyPSA-Eur Authors
3
#
4
# SPDX-License-Identifier: MIT
5
6
from pypsa.optimization.compat import get_var, define_constraints, linexpr
7
8
def custom_extra_functionality(n, snapshots, snakemake):
9
    """
10
    Add custom extra functionality constraints.
11
    """
12
    n.model.constraints.remove("Generator-e_sum_max")
13
    
14
15
    #if n.meta["wildcards"]['planning_horizons'] == "2045":
16
    #    min_wind_onshore = 100e6
17
    #    min_solar = 200e6
18
    #    min_wind_offshore = 50e6
19
    if n.meta["wildcards"]['planning_horizons'] == "2035":
20
        min_wind_onshore = 111309
21
        min_solar = 232228
22
        min_wind_offshore = 39122
23
    elif n.meta["wildcards"]['planning_horizons'] == "2030":
24
        min_wind_onshore = 86583
25
        min_solar = 165173
26
        min_wind_offshore = 27239
27
    elif n.meta["wildcards"]['planning_horizons'] == "2025":
28
        min_wind_onshore = 61856
29
        min_solar = 98119
30
        min_wind_offshore = 15356
31
    
32
    wind_offshore_ext = list(
33
        n.generators.index[
34
            (n.generators.index.str.contains("offwind")
35
             & (n.generators.bus.str.contains("DE"))
36
             & (n.generators.p_nom_extendable)
37
             )
38
        ]
39
    )
40
    
41
    wind_offshore_fixed = n.generators[
42
            (n.generators.index.str.contains("offwind")
43
             & (n.generators.bus.str.contains("DE"))
44
             & (n.generators.p_nom_extendable==False)
45
             )
46
        ].p_nom.sum()
47
    
48
    
49
    wind_onshore_ext = list(
50
        n.generators.index[
51
            (n.generators.index.str.contains("onwind")
52
             & (n.generators.bus.str.contains("DE")))
53
            & (n.generators.p_nom_extendable)
54
        ]
55
    )
56
    
57
    wind_onshore_fixed = n.generators[
58
            (n.generators.index.str.contains("onwind")
59
             & (n.generators.bus.str.contains("DE"))
60
             & (n.generators.p_nom_extendable==False)
61
             )
62
        ].p_nom.sum()
63
    
64
    
65
    solar_ext = list(
66
        n.generators.index[
67
            (n.generators.carrier.isin(["solar", "solar-hst", "solar rooftop"])
68
             & (n.generators.bus.str.contains("DE"))
69
             & (n.generators.p_nom_extendable))
70
        ]
71
    )
72
    
73
    solar_fixed = n.generators[
74
            (n.generators.carrier.isin(["solar", "solar-hst", "solar rooftop"])
75
             & (n.generators.bus.str.contains("DE"))
76
             & (n.generators.p_nom_extendable==False)
77
             )
78
        ].p_nom.sum()
79
    
80
    if n.meta["wildcards"]['planning_horizons'] == "2045":
81
        min_wind_onshore = wind_onshore_fixed
82
        min_solar = solar_fixed
83
        min_wind_offshore = wind_offshore_fixed
84
85
86
    define_constraints(n, 
87
                       get_var(n, "Generator", "p_nom").loc[wind_offshore_ext].sum() ,
88
                       ">=",
89
                       min_wind_offshore - wind_offshore_fixed ,
0 ignored issues
show
introduced by
The variable min_wind_offshore does not seem to be defined for all execution paths.
Loading history...
90
                       "Global",
91
                       "min_offwind_de"
92
                       )
93
    
94
    define_constraints(n, 
95
                       get_var(n, "Generator", "p_nom").loc[wind_onshore_ext].sum() ,
96
                       ">=",
97
                       min_wind_onshore - wind_onshore_fixed,
0 ignored issues
show
introduced by
The variable min_wind_onshore does not seem to be defined for all execution paths.
Loading history...
98
                       "Global",
99
                       "min_onwind_de"
100
                       )
101
    
102
    define_constraints(n, 
103
                       get_var(n, "Generator", "p_nom").loc[solar_ext].sum() ,
104
                       ">=",
105
                       min_solar - solar_fixed,
0 ignored issues
show
introduced by
The variable min_solar does not seem to be defined for all execution paths.
Loading history...
106
                       "Global",
107
                       "min_solar_de"
108
                       )
109
    
110