Skip to content

Commit

Permalink
Merge pull request #45 from openego/dev
Browse files Browse the repository at this point in the history
Release 0.3
  • Loading branch information
lukasol authored Sep 8, 2017
2 parents 0e47b71 + b8ea6aa commit d000a25
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
etrago/cluster/__pycache__/networkclustering.cpython-36.pyc
92 changes: 69 additions & 23 deletions etrago/appl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,76 @@
from numpy import genfromtxt
np.random.seed()
from egopowerflow.tools.tools import oedb_session
from egopowerflow.tools.io import NetworkScenario
from egopowerflow.tools.io import NetworkScenario, results_to_oedb
import time
from egopowerflow.tools.plot import (plot_line_loading, plot_stacked_gen,
add_coordinates, curtailment, gen_dist,
storage_distribution)
from etrago.extras.utilities import load_shedding, data_manipulation_sh, results_to_csv, parallelisation, pf_post_lopf

from etrago.extras.utilities import load_shedding, data_manipulation_sh, results_to_csv, parallelisation, pf_post_lopf, loading_minimization, calc_line_losses, kmean_clustering, group_parallel_lines

from etrago.cluster.networkclustering import busmap_from_psql, cluster_on_extra_high_voltage
from pypsa.networkclustering import busmap_by_kmeans, get_clustering_from_busmap
import pandas as pd

args = {'network_clustering':False,
'db': 'oedb', # db session
'gridversion':None, #None for model_draft or Version number (e.g. v0.2.10) for grid schema
'gridversion':'v0.2.11', #None for model_draft or Version number (e.g. v0.2.10) for grid schema
'method': 'lopf', # lopf or pf
'pf_post_lopf':False , #state whether you want to perform a pf after a lopf simulation
'start_h': 2323,
'end_h' : 2324,
'scn_name': 'SH Status Quo',
'ormcls_prefix': 'EgoGridPfHv', #if gridversion:'version-number' then 'EgoPfHv', if gridversion:None then 'EgoGridPfHv'
'pf_post_lopf': False, #state whether you want to perform a pf after a lopf simulation
'start_snapshot': 2320,
'end_snapshot' : 2321,
'scn_name': 'SH NEP 2035',
'lpfile': False, # state if and where you want to save pyomo's lp file: False or '/path/tofolder'
'results': False , # state if and where you want to save results as csv: False or '/path/tofolder'
'export': False, # state if you want to export the results back to the database
'solver': 'gurobi', #glpk, cplex or gurobi
'branch_capacity_factor': 1, #to globally extend or lower branch capacities
'storage_extendable':True,
'load_shedding':True,
'generator_noise':True,
'parallelisation':False}
'minimize_loading':False,
'k_mean_clustering': False,
'parallelisation':False,
'line_grouping': False,
'comments': None}


def etrago(args):
session = oedb_session(args['db'])

# additional arguments cfgpath, version, prefix
if args['gridversion'] == None:
args['ormcls_prefix'] = 'EgoGridPfHv'
else:
args['ormcls_prefix'] = 'EgoPfHv'

scenario = NetworkScenario(session,
version=args['gridversion'],
prefix=args['ormcls_prefix'],
method=args['method'],
start_h=args['start_h'],
end_h=args['end_h'],
start_snapshot=args['start_snapshot'],
end_snapshot=args['end_snapshot'],
scn_name=args['scn_name'])

network = scenario.build_network()

# add coordinates
network = add_coordinates(network)

# create generator noise
noise_values = network.generators.marginal_cost + abs(np.random.normal(0,0.001,len(network.generators.marginal_cost)))
np.savetxt("noise_values.csv", noise_values, delimiter=",")
noise_values = genfromtxt('noise_values.csv', delimiter=',')

# TEMPORARY vague adjustment due to transformer bug in data processing
#network.transformers.x=network.transformers.x*0.01


if args['branch_capacity_factor']:
network.lines.s_nom = network.lines.s_nom*args['branch_capacity_factor']
network.transformers.s_nom = network.transformers.s_nom*args['branch_capacity_factor']


if args['generator_noise']:
# create generator noise
noise_values = network.generators.marginal_cost + abs(np.random.normal(0,0.001,len(network.generators.marginal_cost)))
Expand All @@ -75,15 +94,19 @@ def etrago(args):

if args['storage_extendable']:
# set virtual storages to be extendable
network.storage_units.p_nom_extendable = True
if network.storage_units.source.any()=='extendable_storage':
network.storage_units.p_nom_extendable = True
# set virtual storage costs with regards to snapshot length
network.storage_units.capital_cost = (network.storage_units.capital_cost /
(8760//(args['end_h']-args['start_h']+1)))

network.storage_units.capital_cost = (network.storage_units.capital_cost /
(8760//(args['end_snapshot']-args['start_snapshot']+1)))

# for SH scenario run do data preperation:
if args['scn_name'] == 'SH Status Quo':
if args['scn_name'] == 'SH Status Quo' or args['scn_name'] == 'SH NEP 2035':
data_manipulation_sh(network)

# grouping of parallel lines
if args['line_grouping']:
group_parallel_lines(network)

#load shedding in order to hunt infeasibilities
if args['load_shedding']:
Expand All @@ -94,33 +117,56 @@ def etrago(args):
network.generators.control="PV"
busmap = busmap_from_psql(network, session, scn_name=args['scn_name'])
network = cluster_on_extra_high_voltage(network, busmap, with_time=True)


# k-mean clustering
if args['k_mean_clustering']:
network = kmean_clustering(network)

# Branch loading minimization
if args['minimize_loading']:
extra_functionality = loading_minimization
else:
extra_functionality=None

# parallisation
if args['parallelisation']:
parallelisation(network, start_h=args['start_h'], end_h=args['end_h'],group_size=1, solver_name=args['solver'])
parallelisation(network, start_snapshot=args['start_snapshot'], end_snapshot=args['end_snapshot'],group_size=1, solver_name=args['solver'], extra_functionality=extra_functionality)
# start linear optimal powerflow calculations
elif args['method'] == 'lopf':
x = time.time()
network.lopf(scenario.timeindex, solver_name=args['solver'])
network.lopf(scenario.timeindex, solver_name=args['solver'], extra_functionality=extra_functionality)
y = time.time()
z = (y - x) / 60 # z is time for lopf in minutes
# start non-linear powerflow simulation
elif args['method'] == 'pf':
network.pf(scenario.timeindex)
# calc_line_losses(network)

if args['pf_post_lopf']:
pf_post_lopf(network, scenario)

calc_line_losses(network)

# provide storage installation costs
if sum(network.storage_units.p_nom_opt) != 0:
installed_storages = network.storage_units[ network.storage_units.p_nom_opt!=0]
storage_costs = sum(installed_storages.capital_cost * installed_storages.p_nom_opt)
print("Investment costs for all storages in selected snapshots [EUR]:",round(storage_costs,2))

# write lpfile to path
if not args['lpfile'] == False:
network.model.write(args['lpfile'], io_options={'symbolic_solver_labels':
True})
# write PyPSA results back to database
if args['export']:
results_to_oedb(session, network, 'hv', args)

# write PyPSA results to csv to path
if not args['results'] == False:
results_to_csv(network, args['results'])

return network


# execute etrago function
network = etrago(args)

Expand All @@ -135,6 +181,6 @@ def etrago(args):
# plot to show extendable storages
storage_distribution(network)


# close session
#session.close()

6 changes: 4 additions & 2 deletions etrago/cluster/networkclustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ def busmap_by_shortest_path(network, session, scn_name, fromlvl, tolvl,
df.rename(columns={'source': 'bus0', 'target': 'bus1'}, inplace=True)
df.set_index(['scn_name', 'bus0', 'bus1'], inplace=True)

df.to_sql('ego_grid_pf_hv_busmap', if_exists='append',
con=session.bind, schema='model_draft')
for i, d in df.reset_index().iterrows():
session.add(EgoGridPfHvBusmap(**d.to_dict()))

session.commit()

return

Expand Down
Loading

0 comments on commit d000a25

Please sign in to comment.