Passed
Pull Request — dev (#850)
by Uwe
01:30
created

v1_invest_optimize_all_technologies   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 87
dl 0
loc 235
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 152 2
1
# -*- coding: utf-8 -*-
2
3
"""
4
General description
5
-------------------
6
This example shows how to perform a capacity optimization for
7
an energy system with storage. The following energy system is modeled:
8
9
.. code-block:: text
10
11
                    input/output  bgas     bel
12
                         |          |        |
13
                         |          |        |
14
     wind(FixedSource)   |------------------>|
15
                         |          |        |
16
     pv(FixedSource)     |------------------>|
17
                         |          |        |
18
     gas_resource        |--------->|        |
19
     (Commodity)         |          |        |
20
                         |          |        |
21
     demand(Sink)        |<------------------|
22
                         |          |        |
23
                         |          |        |
24
     pp_gas(Transformer) |<---------|        |
25
                         |------------------>|
26
                         |          |        |
27
     storage(Storage)    |<------------------|
28
                         |------------------>|
29
30
The example exists in four variations. The following parameters describe
31
the main setting for the optimization variation 1:
32
33
    - optimize wind, pv, gas_resource and storage
34
    - set investment cost for wind, pv and storage
35
    - set gas price for kWh
36
37
    Results show an installation of wind and the use of the gas resource.
38
    A renewable energy share of 51% is achieved.
39
40
    Have a look at different parameter settings. There are four variations
41
    of this example in the same folder.
42
43
Data
44
----
45
storage_investment.csv
46
47
Installation requirements
48
-------------------------
49
50
This example requires oemof.solph (v0.5.x), install by:
51
52
    pip install oemof.solph[examples]
53
54
55
License
56
-------
57
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
58
59
"""
60
61
###############################################################################
62
# Imports
63
###############################################################################
64
65
import logging
66
import os
67
import pprint as pp
68
import warnings
69
70
import pandas as pd
71
72
# Default logger of oemof
73
from oemof.tools import economics
74
from oemof.tools import logger
75
76
from oemof import solph
77
78
79
def main():
80
    # Read data file
81
    filename = os.path.join(os.getcwd(), "storage_investmentd.csv")
82
    try:
83
        data = pd.read_csv(filename)
84
    except FileNotFoundError:
85
        msg = "Data file not found: {0}. Only one value used!"
86
        warnings.warn(msg.format(filename), UserWarning)
87
        data = pd.DataFrame(
88
            {"pv": [0.3, 0.5], "wind": [0.6, 0.8], "demand_el": [500, 600]}
89
        )
90
91
    number_timesteps = len(data)
92
93
    ##########################################################################
94
    # Initialize the energy system and read/calculate necessary parameters
95
    ##########################################################################
96
97
    logger.define_logging()
98
    logging.info("Initialize the energy system")
99
    date_time_index = solph.create_time_index(2012, number=number_timesteps)
100
    energysystem = solph.EnergySystem(
101
        timeindex=date_time_index, infer_last_interval=False
102
    )
103
104
    price_gas = 0.04
105
106
    # If the period is one year the equivalent periodical costs (epc) of an
107
    # investment are equal to the annuity. Use oemof's economic tools.
108
    epc_wind = economics.annuity(capex=1000, n=20, wacc=0.05)
109
    epc_pv = economics.annuity(capex=1000, n=20, wacc=0.05)
110
    epc_storage = economics.annuity(capex=1000, n=20, wacc=0.05)
111
112
    ##########################################################################
113
    # Create oemof objects
114
    ##########################################################################
115
116
    logging.info("Create oemof objects")
117
    # create natural gas bus
118
    bgas = solph.Bus(label="natural_gas")
119
120
    # create electricity bus
121
    bel = solph.Bus(label="electricity")
122
123
    energysystem.add(bgas, bel)
124
125
    # create excess component for the electricity bus to allow overproduction
126
    excess = solph.components.Sink(
127
        label="excess_bel", inputs={bel: solph.Flow()}
128
    )
129
130
    # create source object representing the gas commodity (annual limit)
131
    gas_resource = solph.components.Source(
132
        label="rgas", outputs={bgas: solph.Flow(variable_costs=price_gas)}
133
    )
134
135
    # create fixed source object representing wind power plants
136
    wind = solph.components.Source(
137
        label="wind",
138
        outputs={
139
            bel: solph.Flow(
140
                fix=data["wind"],
141
                investment=solph.Investment(ep_costs=epc_wind),
142
            )
143
        },
144
    )
145
146
    # create fixed source object representing pv power plants
147
    pv = solph.components.Source(
148
        label="pv",
149
        outputs={
150
            bel: solph.Flow(
151
                fix=data["pv"], investment=solph.Investment(ep_costs=epc_pv)
152
            )
153
        },
154
    )
155
156
    # create simple sink object representing the electrical demand
157
    demand = solph.components.Sink(
158
        label="demand",
159
        inputs={bel: solph.Flow(fix=data["demand_el"], nominal_value=1)},
160
    )
161
162
    # create simple transformer object representing a gas power plant
163
    pp_gas = solph.components.Transformer(
164
        label="pp_gas",
165
        inputs={bgas: solph.Flow()},
166
        outputs={bel: solph.Flow(nominal_value=10e10, variable_costs=0)},
167
        conversion_factors={bel: 0.58},
168
    )
169
170
    # create storage object representing a battery
171
    storage = solph.components.GenericStorage(
172
        label="storage",
173
        inputs={bel: solph.Flow(variable_costs=0.0001)},
174
        outputs={bel: solph.Flow()},
175
        loss_rate=0.00,
176
        initial_storage_level=0,
177
        invest_relation_input_capacity=1 / 6,
178
        invest_relation_output_capacity=1 / 6,
179
        inflow_conversion_factor=1,
180
        outflow_conversion_factor=0.8,
181
        investment=solph.Investment(ep_costs=epc_storage),
182
    )
183
184
    energysystem.add(excess, gas_resource, wind, pv, demand, pp_gas, storage)
185
186
    ##########################################################################
187
    # Optimise the energy system
188
    ##########################################################################
189
190
    logging.info("Optimise the energy system")
191
192
    # initialise the operational model
193
    om = solph.Model(energysystem)
194
195
    # if tee_switch is true solver messages will be displayed
196
    logging.info("Solve the optimization problem")
197
    om.solve(solver="cbc", solve_kwargs={"tee": True})
198
199
    ##########################################################################
200
    # Check and plot the results
201
    ##########################################################################
202
203
    # check if the new result object is working for custom components
204
    results = solph.processing.results(om)
205
206
    electricity_bus = solph.views.node(results, "electricity")
207
208
    meta_results = solph.processing.meta_results(om)
209
    pp.pprint(meta_results)
210
211
    my_results = electricity_bus["scalars"]
212
213
    # installed capacity of storage in GWh
214
    my_results["storage_invest_GWh"] = (
215
        results[(storage, None)]["scalars"]["invest"] / 1e6
216
    )
217
218
    # installed capacity of wind power plant in MW
219
    my_results["wind_invest_MW"] = (
220
        results[(wind, bel)]["scalars"]["invest"] / 1e3
221
    )
222
223
    # resulting renewable energy share
224
    my_results["res_share"] = (
225
        1
226
        - results[(pp_gas, bel)]["sequences"].sum()
227
        / results[(bel, demand)]["sequences"].sum()
228
    )
229
230
    pp.pprint(my_results)
231
232
233
if __name__ == "__main__":
234
    main()
235