def create_energy_system(scenario): """Creates oemof energy system from JSON scenario definition Parameters ---------- scenario: dict Dictionary representing the scenario element containing the scenario information and the children Returns ------- es : oemof.solph.EnergySystem EnergySystem object with timeindex and additional scenario information """ first = pd.to_datetime(scenario['tags'].get('scenario_year' + '0101', '2016')) start = first + pd.DateOffset( hours=int(scenario['tags'].get('start_timestep', 1))-1) end = first + pd.DateOffset( hours=int(scenario['tags'].get('end_timestep', 8760))-1) timeindex = pd.date_range(start=start, end=end, freq='H') # create energy sytem and disable automatic registry of node objects es = EnergySystem(groupings=GROUPINGS, timeindex=timeindex) Node.registry = None es.scenario_description = scenario['tags'].get('scenario_description', 'No description provided.') es.scenario_name = scenario['name'] return es
def create_energysystem(nodes, **arguments): """Create the energysystem. """ datetime_index = pd.date_range(start=arguments['--start'], end=arguments['--end'], freq=arguments['--freq']) es = EnergySystem(entities=nodes, timeindex=datetime_index, groupings=GROUPINGS) es.timestamp = time.strftime("%Y%m%d-%H:%M:%S") return es
def run_investment_example(): logger.define_logging() # %% model creation and solving date_from = '2050-01-01 00:00:00' date_to = '2050-01-01 23:00:00' datetime_index = pd.date_range(date_from, date_to, freq='60min') es = EnergySystem(timeindex=datetime_index) data_path = os.path.join(os.path.dirname(__file__), 'data') nodes = NodesFromCSV(file_nodes_flows=os.path.join(data_path, 'nodes_flows.csv'), file_nodes_flows_sequences=os.path.join( data_path, 'nodes_flows_seq.csv'), delimiter=',') stopwatch() om = OperationalModel(es) logging.info('OM creation time: ' + stopwatch()) #om.receive_duals() om.solve(solver='glpk', solve_kwargs={'tee': True}) logging.info('Optimization time: ' + stopwatch()) logging.info('Done! \n Check the results')
def run_example(config): # misc. datetime_index = pd.date_range(config['date_from'], config['date_to'], freq='60min') # model creation and solving logging.info('Starting optimization') es = EnergySystem(timeindex=datetime_index) NodesFromCSV(file_nodes_flows=os.path.join(config['scenario_path'], config['nodes_flows']), file_nodes_flows_sequences=os.path.join( config['scenario_path'], config['nodes_flows_sequences']), delimiter=',') om = OperationalModel(es) om.receive_duals() om.solve(solver=config['solver'], solve_kwargs={'tee': config['verbose']}) logging.info('Done! \n Check the results') # create pandas dataframe with results results = ResultsDataFrame(energy_system=es) rdict = {'objective': es.results.objective, 'time_series': results} return rdict
def main(**scenario_assumptions): print('Postprocessing') dirs = get_experiment_dirs(scenario_assumptions['scenario']) subdir = os.path.join(dirs['postprocessed'], 'sequences') if not os.path.exists(subdir): os.mkdir(subdir) # restore EnergySystem with results es = EnergySystem() es.restore(dirs['optimised']) sequences = write_results(es, dirs['postprocessed']) capacities = get_capacities(es) capacities = cap_el_to_cap_th(capacities) capacity_cost = get_capacity_cost(es) carrier_cost = get_carrier_cost(es) marginal_cost = get_marginal_cost(es) yearly_electricity = get_yearly_sum(sequences['electricity'], var_name='yearly_electricity') heat_sequences = pd.concat([sequences['heat_central'], sequences['heat_decentral']], 1) yearly_heat = get_yearly_sum(heat_sequences, var_name='yearly_heat') # full_load_hours = get_flh(capacities, yearly_sum) share_el_heat = get_share_el_heat(yearly_heat) spec_cost_of_heat = get_spec_cost_of_heat(yearly_heat, capacity_cost, carrier_cost, marginal_cost) scalars = pd.concat( [capacities, yearly_electricity, yearly_heat, capacity_cost, carrier_cost, marginal_cost, share_el_heat, spec_cost_of_heat], 0) # some of the rows carry the component classes as index. This maps all index labels to str. scalars = multi_index_dtype_to_str(scalars) scalars.sort_values(by=['name', 'var_name', 'type', 'carrier', 'tech'], inplace=True) scalars.to_csv(os.path.join(dirs['postprocessed'], 'scalars.csv'))
def restore_es(self, filename=None): if filename is None: filename = self.results_fn else: self.results_fn = filename if self.es is None: self.es = EnergySystem() f = open(filename, "rb") self.meta = pickle.load(f) self.es.__dict__ = pickle.load(f) f.close() self.results = self.es.results["main"] logging.info("Results restored from {0}.".format(filename))
def setUpClass(cls): cls.period = 24 cls.es = EnergySystem(timeindex=pandas.date_range( '2016-01-01', periods=cls.period, freq='H')) # BUSSES b_el1 = Bus(label="b_el1") b_el2 = Bus(label="b_el2") b_diesel = Bus(label='b_diesel', balanced=False) cls.es.add(b_el1, b_el2, b_diesel) # TEST DIESEL: dg = Transformer( label='diesel', inputs={b_diesel: Flow(variable_costs=2)}, outputs={ b_el1: Flow(variable_costs=1, investment=Investment(ep_costs=0.5)) }, conversion_factors={b_el1: 2}, ) batt = GenericStorage( label='storage', inputs={b_el1: Flow(variable_costs=3)}, outputs={b_el2: Flow(variable_costs=2.5)}, capacity_loss=0.00, initial_capacity=0, invest_relation_input_capacity=1 / 6, invest_relation_output_capacity=1 / 6, inflow_conversion_factor=1, outflow_conversion_factor=0.8, fixed_costs=35, investment=Investment(ep_costs=0.4), ) cls.demand_values = [100] * 8760 cls.demand_values[0] = 0.0 demand = Sink(label="demand_el", inputs={ b_el2: Flow(nominal_value=1, actual_value=cls.demand_values, fixed=True) }) cls.es.add(dg, batt, demand) cls.om = Model(cls.es) cls.om.receive_duals() cls.om.solve() cls.mod = Model(cls.es) cls.mod.solve()
def run_investment_example(solver='cbc', verbose=True, nologg=False): if not nologg: logger.define_logging() # %% model creation and solving date_from = '2050-01-01 00:00:00' date_to = '2050-01-01 23:00:00' datetime_index = pd.date_range(date_from, date_to, freq='60min') es = EnergySystem(timeindex=datetime_index) data_path = os.path.join(os.path.dirname(__file__), 'data') NodesFromCSV(file_nodes_flows=os.path.join(data_path, 'nodes_flows.csv'), file_nodes_flows_sequences=os.path.join( data_path, 'nodes_flows_seq.csv'), delimiter=',') stopwatch() om = OperationalModel(es) logging.info('OM creation time: ' + stopwatch()) om.receive_duals() om.solve(solver=solver, solve_kwargs={'tee': verbose}) logging.info('Optimization time: ' + stopwatch()) results = ResultsDataFrame(energy_system=es) results_path = os.path.join(os.path.expanduser("~"), 'csv_invest') if not os.path.isdir(results_path): os.mkdir(results_path) results.to_csv(os.path.join(results_path, 'results.csv')) logging.info("The results can be found in {0}".format(results_path)) logging.info("Read the documentation (outputlib) to learn how" + " to process the results.") logging.info("Or search the web to learn how to handle a MultiIndex" + "DataFrame with pandas.") logging.info('Done!')
def initialise_energy_system(self): if self.debug is True: number_of_time_steps = 3 else: try: if calendar.isleap(self.year): number_of_time_steps = 8784 else: number_of_time_steps = 8760 except TypeError: msg = ("You cannot create an EnergySystem with self.year={0}, " "of type {1}.") raise TypeError(msg.format(self.year, type(self.year))) date_time_index = pd.date_range("1/1/{0}".format(self.year), periods=number_of_time_steps, freq="H") return EnergySystem(timeindex=date_time_index)
def run_example(config): # creation of an hourly datetime_index datetime_index = pd.date_range(config['date_from'], config['date_to'], freq='60min') # model creation and solving logging.info('Starting optimization') # initialisation of the energy system es = EnergySystem(timeindex=datetime_index) # adding all nodes and flows to the energy system # (data taken from csv-file) NodesFromCSV(file_nodes_flows=os.path.join(config['scenario_path'], config['nodes_flows']), file_nodes_flows_sequences=os.path.join( config['scenario_path'], config['nodes_flows_sequences']), delimiter=',') # creation of a least cost model from the energy system om = OperationalModel(es) om.receive_duals() # solving the linear problem using the given solver om.solve(solver=config['solver'], solve_kwargs={'tee': config['verbose']}) logging.info("Done!") # create pandas dataframe with results results = ResultsDataFrame(energy_system=es) # write results for selected busses to single csv files results.bus_balance_to_csv(bus_labels=['R1_bus_el', 'R2_bus_el'], output_path=config['results_path']) logging.info("The results can be found in {0}".format( config['results_path'])) logging.info("Read the documentation (outputlib) to learn how" + " to process the results.") rdict = {'objective': es.results.objective, 'time_series': results} return rdict
def create_energysystem(nodes, **arguments): """Creates the energysystem. Parameters ---------- nodes: A list of entities that comprise the energy system **arguments : key word arguments Arguments passed from command line """ datetime_index = pd.date_range(arguments['--date-from'], arguments['--date-to'], freq='60min') es = EnergySystem(entities=nodes, groupings=GROUPINGS, timeindex=datetime_index) return es
def create_energysystem(datapackage, **arguments): """Creates the energysystem. Parameters ---------- datapackage: str path to datapackage metadata file in JSON format **arguments : key word arguments Arguments passed from command line """ es = EnergySystem.from_datapackage(arguments['DATAPACKAGE'], attributemap={}, typemap=options.typemap) es._typemap = options.typemap end = es.timeindex.get_loc(es.timeindex[int(arguments['--t_end'])]) + 1 es.timeindex = es.timeindex[int(arguments['--t_start']):end] return es
def optimize(input_data_dir, results_data_dir, solver='cbc', debug=False): r""" Takes the specified datapackage, creates an energysystem and solves the optimization problem. """ # create energy system object logging.info("Creating EnergySystem from datapackage") es = EnergySystem.from_datapackage( os.path.join(input_data_dir, "datapackage.json"), attributemap={}, typemap=TYPEMAP, ) # create model from energy system (this is just oemof.solph) logging.info("Creating the optimization model") m = Model(es) # if you want dual variables / shadow prices uncomment line below # m.receive_duals() # save lp file together with optimization results if debug: lp_file_dir = os.path.join(results_data_dir, 'model.lp') logging.info(f"Saving the lp-file to {lp_file_dir}") m.write(lp_file_dir, io_options={'symbolic_solver_labels': True}) # select solver 'gurobi', 'cplex', 'glpk' etc logging.info(f'Solving the problem using {solver}') m.solve(solver=solver) # get the results from the the solved model(still oemof.solph) es.results = m.results() es.params = outputlib.processing.parameter_as_dict(es) # now we use the write results method to write the results in oemof-tabular # format logging.info(f'Writing the results to {results_data_dir}') es.dump(results_data_dir)
#!/usr/bin/env python import pandas as pd import matplotlib.pyplot as plt from oemof.solph import (Sink, Source, Transformer, Bus, Flow, Model, EnergySystem, Investment) import oemof.outputlib as outputlib from oemof.tools import economics solver = 'cbc' # initialize energysytem datetimeindex = pd.date_range('1/1/2016', periods=24 * 365, freq='H') energysystem = EnergySystem(timeindex=datetimeindex) # load data filename = '' data = pd.read_csv(filename, sep=';', decimal=',') print(data.head()) # create buses bus_coal = Bus(label='coal', balanced=False) bus_gas = Bus(label='gas', balanced=False) bus_el = Bus(label='electricity') # create sources wind = Source(label='wind', outputs={ bus_el: Flow(actual_value=data['wind'], nominal_value=1, fixed=True)
nx.draw(grph, pos=pos, **options) # add edge labels for all edges if edge_labels is True and plt: labels = nx.get_edge_attributes(grph, 'weight') nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) # show output if plot is True: plt.show() timeindex = pd.date_range('1/1/2017', periods=8760, freq='H') energysystem = EnergySystem(timeindex=timeindex) Node.registry = energysystem ################################################################# # data ################################################################# # Read data file full_filename = os.path.join(os.path.dirname(__file__), 'timeseries.csv') timeseries = pd.read_csv(full_filename, sep=',') costs = { 'pp_wind': { 'epc': economics.annuity(capex=1000, n=20, wacc=0.05) }, 'pp_pv': { 'epc': economics.annuity(capex=750, n=20, wacc=0.05) },
https://www.pypsa.org/examples/minimal_example_lopf.html Thanks at Tom, Jonas and others! """ from renpass import facades as fc from renpass.components import electrical as elec from oemof.solph import EnergySystem, Model from oemof.network import Bus, Node, Edge import pandas as pd # initialise oemof energy system object es = EnergySystem() el0 = elec.ElectricalBus('el0') el1 = elec.ElectricalBus('el1') el2 = elec.ElectricalBus('el2') line0 = elec.Line(from_bus=el0, to_bus=el1, capacity=60, reactance=0.0001) line1 = elec.Line(from_bus=el1, to_bus=el2, capacity=60, reactance=0.0001) line2 = elec.Line(from_bus=el2, to_bus=el0, capacity=60, reactance=0.0001) gen0 = fc.Dispatchable("gen0", capacity=100, bus=el0, marginal_cost=50, carrier='coal') gen1 = fc.Dispatchable("gen1",
import os import oemof.tabular.datapackage from oemof.solph import EnergySystem, Model from oemof.tabular.facades import TYPEMAP import oemof.tabular.tools.postprocessing as pp # create path for results (we use the datapackage_dir to store results) results_path = 'results' if not os.path.exists(results_path): os.makedirs(results_path) # create energy system object es = EnergySystem.from_datapackage( os.path.join("./datapackage", "datapackage.json"), attributemap={}, typemap=TYPEMAP, ) # create model from energy system (this is just oemof.solph) m = Model(es) # if you want dual variables / shadow prices uncomment line below # m.receive_duals() # select solver 'gurobi', 'cplex', 'glpk' etc m.solve("glpk") # get the results from the the solved model(still oemof.solph) m.results = m.results()
nx.draw(grph, pos=pos, **options) # add edge labels for all edges if edge_labels is True and plt: labels = nx.get_edge_attributes(grph, 'weight') nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) # show output if plot is True: plt.show() datetimeindex = pd.date_range('1/1/2017', periods=2, freq='H') es = EnergySystem(timeindex=datetimeindex) b_0 = Bus(label='b_0') b_1 = Bus(label='b_1') es.add(b_0, b_1) es.add(custom.Link(label="line_0", inputs={ b_0: Flow(), b_1: Flow()}, outputs={ b_1: Flow(investment=Investment()), b_0: Flow(investment=Investment())}, conversion_factors={ (b_0, b_1): 0.95, (b_1, b_0): 0.9}))
nx.draw(grph, pos=pos, **options) # add edge labels for all edges if edge_labels is True and plt: labels = nx.get_edge_attributes(grph, "weight") nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) # show output if plot is True: plt.show() datetimeindex = pd.date_range("1/1/2017", periods=2, freq="H") es = EnergySystem(timeindex=datetimeindex) b_el0 = custom.ElectricalBus(label="b_0", v_min=-1, v_max=1) b_el1 = custom.ElectricalBus(label="b_1", v_min=-1, v_max=1) b_el2 = custom.ElectricalBus(label="b_2", v_min=-1, v_max=1) es.add(b_el0, b_el1, b_el2) es.add( custom.ElectricalLine( input=b_el0, output=b_el1, reactance=0.0001, investment=Investment(ep_costs=10),
# Precalculation u_value = calculate_storage_u_value(input_data['s_iso'], input_data['lamb_iso'], input_data['alpha_inside'], input_data['alpha_outside']) # Set up an energy system model solver = 'cbc' periods = 100 datetimeindex = pd.date_range('1/1/2019', periods=periods, freq='H') demand_timeseries = np.zeros(periods) demand_timeseries[-5:] = 1 heat_feedin_timeseries = np.zeros(periods) heat_feedin_timeseries[:10] = 1 energysystem = EnergySystem(timeindex=datetimeindex) bus_heat = Bus(label='bus_heat') heat_source = Source( label='heat_source', outputs={bus_heat: Flow(nominal_value=1, fix=heat_feedin_timeseries)}) shortage = Source(label='shortage', outputs={bus_heat: Flow(variable_costs=1e6)}) excess = Sink(label='excess', inputs={bus_heat: Flow()}) heat_demand = Sink( label='heat_demand', inputs={bus_heat: Flow(nominal_value=1, fix=demand_timeseries)})
from oemof.solph import (EnergySystem, MultiPeriodModel, Flow, Source, Sink, Bus, RollingHorizon) from oemof.solph.components import (GenericCHP, GenericStorage) from oemof.outputlib import processing, views from time import time # read sequence data full_filename = os.path.join(os.path.dirname(__file__), 'data.csv') data = pd.read_csv(full_filename, sep=";") # select total_time_steps total_time_steps = 24 * 7 # create an energy system idx = pd.date_range('1/1/2017', periods=total_time_steps, freq='H') start = time() es = EnergySystem(timeindex=idx) Node.registry = es # resources bgas = Bus(label='bgas') rgas = Source(label='rgas', outputs={bgas: Flow()}) # heat bth = Bus(label='bth') # dummy source at high costs that serves the residual load source_th = Source(label='source_th', outputs={bth: Flow(variable_costs=1000)}) demand_th = Sink(label='demand_th', inputs={
import os import pandas as pd from oemof.solph import (Sink, Source, Transformer, Bus, Flow, Model, EnergySystem) from oemof.outputlib import views import matplotlib.pyplot as plt solver = 'cbc' # Create an energy system and optimize the dispatch at least costs. # ####################### initialize and provide data ##################### datetimeindex = pd.date_range('1/1/2016', periods=24 * 10, freq='H') energysystem = EnergySystem(timeindex=datetimeindex) filename = os.path.join(os.path.dirname(__file__), 'input_data.csv') data = pd.read_csv(filename, sep=",") # ######################### create energysystem components ################ # resource buses bcoal = Bus(label='coal', balanced=False) bgas = Bus(label='gas', balanced=False) boil = Bus(label='oil', balanced=False) blig = Bus(label='lignite', balanced=False) # electricity and heat bel = Bus(label='bel') bth = Bus(label='bth')
#!/usr/bin/env python import pandas as pd import matplotlib.pyplot as plt from oemof.solph import Sink, Source, Transformer, Bus, Flow, EnergySystem, Model from oemof.solph.components import GenericStorage import oemof.outputlib as outputlib # ## Specify solver solver = 'cbc' # ## Create an energy system and load data datetimeindex = pd.date_range('1/1/2016', periods=24 * 365, freq='H') energysystem = EnergySystem(timeindex=datetimeindex) filename = '' data = pd.read_csv(filename, sep=";", decimal=',') data = data.dropna(axis=1) print(data.head()) # ## Create Buses # resource buses bus_gas = Bus(label='gas') bus_coal = Bus(label='coal') # electricity and heat buses bus_el = Bus(label='electricity') bus_th = Bus(label='heat')
import oemof import pandas as pd import numpy as np import matplotlib.pyplot as plt from oemof.solph import Sink, Source, Transformer, Bus, Flow, EnergySystem, Model from oemof.solph.components import GenericStorage import oemof.outputlib as outputlib oemof.tools.logger.define_logging(logfile='oemof example.log', screen_level=logging.INFO, file_level=logging.DEBUG) # Creating the energy system date_time_index = pd.date_range('1/1/2018', periods=24*365, freq='H') es = EnergySystem(timeindex=date_time_index) filename = 'data_timeseries.csv' data = pd.read_csv(filename, sep=",") logging.info('Energy system created and initialized') # Creating the necessary buses elbus = Bus(label='electricity') gasbus = Bus(label='gas') thbus = Bus(label='heat') logging.info('Necessary buses for the system created') # Now creating the necessary components for the system
# -*- coding: utf-8 -*- """ This module is designed to contain classes that act as simplified / reduced energy specific interfaces (facades) for solph components to simplify its application and work with the oemof datapackage - reader functionality SPDX-License-Identifier: GPL-3.0-or-later """ from renpass import facades as fc from oemof.solph import EnergySystem, Model import pandas as pd es = EnergySystem(timeindex=pd.date_range('2018', freq='H', periods=3)) # buses el1 = fc.Bus('electricity1') el2 = fc.Bus('electricity2') heat = fc.Bus('heat') biomass = fc.Bus('biomass', balanced=False) gas = fc.Bus('gas', balanced=False) st = fc.Dispatchable('st', bus=el1, carrier='biogas', tech='st',
def diesel_only(mode, feedin, initial_batt_cap, cost, iterstatus=None, PV_source=True, storage_source=True, logger=False): if logger == 1: logger.define_logging() ##################################### Initialize the energy system################################################## # times = pd.DatetimeIndex(start='04/01/2017', periods=10, freq='H') times = feedin.index energysystem = EnergySystem(timeindex=times) # switch on automatic registration of entities of EnergySystem-object=energysystem Node.registry = energysystem # add components b_el = Bus(label='electricity') b_dc = Bus(label='electricity_dc') b_oil = Bus(label='diesel_source') demand_feedin = feedin['demand_el'] Sink(label='demand', inputs={ b_el: Flow(actual_value=demand_feedin, nominal_value=1, fixed=True) }) Sink(label='excess', inputs={b_el: Flow()}) Source(label='diesel', outputs={b_oil: Flow()}) generator1 = custom.DieselGenerator( label='pp_oil_1', fuel_input={b_oil: Flow(variable_costs=cost['pp_oil_1']['var'])}, electrical_output={ b_el: Flow(nominal_value=186, min=0.3, max=1, nonconvex=NonConvex(om_costs=cost['pp_oil_1']['o&m']), fixed_costs=cost['pp_oil_1']['fix']) }, fuel_curve={ '1': 42, '0.75': 33, '0.5': 22, '0.25': 16 }) generator2 = custom.DieselGenerator( label='pp_oil_2', fuel_input={b_oil: Flow(variable_costs=cost['pp_oil_2']['var'])}, electrical_output={ b_el: Flow(nominal_value=186, min=0.3, max=1, nonconvex=NonConvex(om_costs=cost['pp_oil_2']['o&m']), fixed_costs=cost['pp_oil_2']['fix'], variable_costs=0) }, fuel_curve={ '1': 42, '0.75': 33, '0.5': 22, '0.25': 16 }) generator3 = custom.DieselGenerator( label='pp_oil_3', fuel_input={b_oil: Flow(variable_costs=cost['pp_oil_3']['var'])}, electrical_output={ b_el: Flow(nominal_value=320, min=0.3, max=1, nonconvex=NonConvex(om_costs=cost['pp_oil_3']['o&m']), fixed_costs=cost['pp_oil_3']['fix'], variable_costs=0) }, fuel_curve={ '1': 73, '0.75': 57, '0.5': 38, '0.25': 27 }) # List all generators in a list called gen_set gen_set = [generator1, generator2, generator3] sim_params = get_sim_params(cost) if mode == 'simulation': nominal_cap_pv = sim_params['pv']['nominal_capacity'] inv_pv = None nominal_cap_batt = sim_params['storage']['nominal_capacity'] inv_batt = None elif mode == 'investment': nominal_cap_pv = None inv_pv = sim_params['pv']['investment'] nominal_cap_batt = None inv_batt = sim_params['storage']['investment'] else: raise ( UserWarning, 'Energysystem cant be build. Check if mode is spelled correctely. ' 'It can be either [simulation] or [investment]') if PV_source == 1: PV = Source(label='PV', outputs={ b_dc: Flow(nominal_value=nominal_cap_pv, fixed_costs=cost['pv']['fix'], actual_value=feedin['PV'], fixed=True, investment=inv_pv) }) else: PV = None if storage_source == 1: storage = components.GenericStorage( label='storage', inputs={b_dc: Flow()}, outputs={ b_dc: Flow(variable_costs=cost['storage']['var'], fixed_costs=cost['storage']['fix']) }, nominal_capacity=nominal_cap_batt, capacity_loss=0.00, initial_capacity=initial_batt_cap, nominal_input_capacity_ratio=0.546, nominal_output_capacity_ratio=0.546, inflow_conversion_factor=0.92, outflow_conversion_factor=0.92, capacity_min=0.5, capacity_max=1, investment=inv_batt, initial_iteration=iterstatus) else: storage = None if storage_source == 1 or PV_source == 1: inverter1 = add_inverter(b_dc, b_el, 'Inv_pv') ################################# optimization ############################ # create Optimization model based on energy_system logging.info("Create optimization problem") m = Model(energysystem) ################################# constraints ############################ sr_requirement = 0.2 sr_limit = demand_feedin * sr_requirement rm_requirement = 0.4 rm_limit = demand_feedin * rm_requirement constraints.spinning_reserve_constraint(m, sr_limit, groups=gen_set, storage=storage) # constraints.n1_constraint(m, demand_feedin, groups=gen_set) constraints.gen_order_constraint(m, groups=gen_set) constraints.rotating_mass_constraint(m, rm_limit, groups=gen_set, storage=storage) return [m, gen_set]
def test_dispatch_example(solver='cbc', periods=24*5): """Create an energy system and optimize the dispatch at least costs.""" Node.registry = None filename = os.path.join(os.path.dirname(__file__), 'input_data.csv') data = pd.read_csv(filename, sep=",") # ######################### create energysystem components ################ # resource buses bcoal = Bus(label='coal', balanced=False) bgas = Bus(label='gas', balanced=False) boil = Bus(label='oil', balanced=False) blig = Bus(label='lignite', balanced=False) # electricity and heat bel = Bus(label='b_el') bth = Bus(label='b_th') # an excess and a shortage variable can help to avoid infeasible problems excess_el = Sink(label='excess_el', inputs={bel: Flow()}) # shortage_el = Source(label='shortage_el', # outputs={bel: Flow(variable_costs=200)}) # sources ep_wind = economics.annuity(capex=1000, n=20, wacc=0.05) wind = Source(label='wind', outputs={bel: Flow( fix=data['wind'], investment=Investment(ep_costs=ep_wind, existing=100))}) ep_pv = economics.annuity(capex=1500, n=20, wacc=0.05) pv = Source(label='pv', outputs={bel: Flow( fix=data['pv'], investment=Investment(ep_costs=ep_pv, existing=80))}) # demands (electricity/heat) demand_el = Sink(label='demand_elec', inputs={bel: Flow(nominal_value=85, fix=data['demand_el'])}) demand_th = Sink(label='demand_therm', inputs={bth: Flow(nominal_value=40, fix=data['demand_th'])}) # power plants pp_coal = Transformer(label='pp_coal', inputs={bcoal: Flow()}, outputs={bel: Flow(nominal_value=20.2, variable_costs=25)}, conversion_factors={bel: 0.39}) pp_lig = Transformer(label='pp_lig', inputs={blig: Flow()}, outputs={bel: Flow(nominal_value=11.8, variable_costs=19)}, conversion_factors={bel: 0.41}) pp_gas = Transformer(label='pp_gas', inputs={bgas: Flow()}, outputs={bel: Flow(nominal_value=41, variable_costs=40)}, conversion_factors={bel: 0.50}) pp_oil = Transformer(label='pp_oil', inputs={boil: Flow()}, outputs={bel: Flow(nominal_value=5, variable_costs=50)}, conversion_factors={bel: 0.28}) # combined heat and power plant (chp) pp_chp = Transformer(label='pp_chp', inputs={bgas: Flow()}, outputs={bel: Flow(nominal_value=30, variable_costs=42), bth: Flow(nominal_value=40)}, conversion_factors={bel: 0.3, bth: 0.4}) # heatpump with a coefficient of performance (COP) of 3 b_heat_source = Bus(label='b_heat_source') heat_source = Source(label='heat_source', outputs={b_heat_source: Flow()}) cop = 3 heat_pump = Transformer(label='el_heat_pump', inputs={bel: Flow(), b_heat_source: Flow()}, outputs={bth: Flow(nominal_value=10)}, conversion_factors={ bel: 1/3, b_heat_source: (cop-1)/cop}) datetimeindex = pd.date_range('1/1/2012', periods=periods, freq='H') energysystem = EnergySystem(timeindex=datetimeindex) energysystem.add(bcoal, bgas, boil, bel, bth, blig, excess_el, wind, pv, demand_el, demand_th, pp_coal, pp_lig, pp_oil, pp_gas, pp_chp, b_heat_source, heat_source, heat_pump) # ################################ optimization ########################### # create optimization model based on energy_system optimization_model = Model(energysystem=energysystem) # solve problem optimization_model.solve(solver=solver) # write back results from optimization object to energysystem optimization_model.results() # ################################ results ################################ # generic result object results = processing.results(om=optimization_model) # subset of results that includes all flows into and from electrical bus # sequences are stored within a pandas.DataFrames and scalars e.g. # investment values within a pandas.Series object. # in this case the entry data['scalars'] does not exist since no investment # variables are used data = views.node(results, 'b_el') # generate results to be evaluated in tests comp_results = data['sequences'].sum(axis=0).to_dict() comp_results['pv_capacity'] = results[(pv, bel)]['scalars'].invest comp_results['wind_capacity'] = results[(wind, bel)]['scalars'].invest test_results = { (('wind', 'b_el'), 'flow'): 9239, (('pv', 'b_el'), 'flow'): 1147, (('b_el', 'demand_elec'), 'flow'): 7440, (('b_el', 'excess_el'), 'flow'): 6261, (('pp_chp', 'b_el'), 'flow'): 477, (('pp_lig', 'b_el'), 'flow'): 850, (('pp_gas', 'b_el'), 'flow'): 934, (('pp_coal', 'b_el'), 'flow'): 1256, (('pp_oil', 'b_el'), 'flow'): 0, (('b_el', 'el_heat_pump'), 'flow'): 202, 'pv_capacity': 44, 'wind_capacity': 246, } for key in test_results.keys(): eq_(int(round(comp_results[key])), int(round(test_results[key])))
def run_add_constraints_example(solver='cbc', nologg=False): if not nologg: logging.basicConfig(level=logging.INFO) # ##### creating an oemof solph optimization model, nothing special here ## # create an energy system object for the oemof solph nodes es = EnergySystem(timeindex=pd.date_range('1/1/2017', periods=4, freq='H')) # add some nodes boil = Bus(label="oil", balanced=False) blig = Bus(label="lignite", balanced=False) b_el = Bus(label="b_el") es.add(boil, blig, b_el) sink = Sink(label="Sink", inputs={ b_el: Flow(nominal_value=40, actual_value=[0.5, 0.4, 0.3, 1], fixed=True) }) pp_oil = Transformer( label='pp_oil', inputs={boil: Flow()}, outputs={b_el: Flow(nominal_value=50, variable_costs=25)}, conversion_factors={b_el: 0.39}) pp_lig = Transformer( label='pp_lig', inputs={blig: Flow()}, outputs={b_el: Flow(nominal_value=50, variable_costs=10)}, conversion_factors={b_el: 0.41}) es.add(sink, pp_oil, pp_lig) # create the model om = Model(energysystem=es) # add specific emission values to flow objects if source is a commodity bus for s, t in om.flows.keys(): if s is boil: om.flows[s, t].emission_factor = 0.27 # t/MWh if s is blig: om.flows[s, t].emission_factor = 0.39 # t/MWh emission_limit = 60e3 # add the outflow share om.flows[(boil, pp_oil)].outflow_share = [1, 0.5, 0, 0.3] # Now we are going to add a 'sub-model' and add a user specific constraint # first we add a pyomo Block() instance that we can use to add our # constraints. Then, we add this Block to our previous defined # Model instance and add the constraints. myblock = po.Block() # create a pyomo set with the flows (i.e. list of tuples), # there will of course be only one flow inside this set, the one we used to # add outflow_share myblock.MYFLOWS = po.Set(initialize=[ k for (k, v) in om.flows.items() if hasattr(v, 'outflow_share') ]) # pyomo does not need a po.Set, we can use a simple list as well myblock.COMMODITYFLOWS = [ k for (k, v) in om.flows.items() if hasattr(v, 'emission_factor') ] # add the sub-model to the oemof Model instance om.add_component('MyBlock', myblock) def _inflow_share_rule(m, s, e, t): """pyomo rule definition: Here we can use all objects from the block or the om object, in this case we don't need anything from the block except the newly defined set MYFLOWS. """ expr = (om.flow[s, e, t] >= om.flows[s, e].outflow_share[t] * sum(om.flow[i, o, t] for (i, o) in om.FLOWS if o == e)) return expr myblock.inflow_share = po.Constraint(myblock.MYFLOWS, om.TIMESTEPS, rule=_inflow_share_rule) # add emission constraint myblock.emission_constr = po.Constraint( expr=(sum(om.flow[i, o, t] for (i, o) in myblock.COMMODITYFLOWS for t in om.TIMESTEPS) <= emission_limit)) # solve and write results to dictionary # you may print the model with om.pprint() om.solve(solver=solver) logging.info("Successfully finished.")
from oemof.solph import (Sink, Source, Bus, Flow, Model, EnergySystem) import oemof.outputlib as outputlib import oemof.solph as solph import numpy as np import matplotlib.pyplot as plt solver = 'cbc' # set timeindex and create data periods = 20 datetimeindex = pd.date_range('1/1/2019', periods=periods, freq='H') step = 5 demand = np.arange(0, step * periods, step) # set up EnergySystem energysystem = EnergySystem(timeindex=datetimeindex) b_gas = Bus(label='gas', balanced=False) b_el = Bus(label='electricity') energysystem.add(b_gas, b_el) energysystem.add( Source(label='shortage', outputs={b_el: Flow(variable_costs=1e6)})) energysystem.add( Sink(label='demand', inputs={b_el: Flow(nominal_value=1, actual_value=demand, fixed=True)})) conv_func = lambda x: 0.01 * x**2 in_breakpoints = np.arange(0, 110, 25) pwltf = solph.custom.PiecewiseLinearTransformer( label='pwltf',
c) add the components to the energy system """ import pandas as pd import matplotlib.pyplot as plt from oemof.solph import Sink, Source, Transformer, Bus, Flow, EnergySystem, Model from oemof.solph.components import GenericStorage import oemof.outputlib as outputlib # ## Specify solver solver = 'cbc' # ## Create an energy system and load data datetimeindex = pd.date_range('1/1/2016', periods=24 * 365, freq='H') energysystem = EnergySystem(timeindex=datetimeindex) filename = '' data = pd.read_csv(filename, sep=",") # ## Create Buses # ## Create components # ## Add all to the energysystem energysystem.add() # ## Create an Optimization Model and solve it # create optimization model based on energy_system optimization_model = Model(energysystem=energysystem) # solve problem