Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nesnoj committed Nov 5, 2018
2 parents 9c76a6f + 1acd9ec commit 7c6e693
Show file tree
Hide file tree
Showing 26 changed files with 546 additions and 131 deletions.
29 changes: 27 additions & 2 deletions ding0/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ding0.core.powerflow import *
from ding0.tools import pypsa_io
from ding0.tools.animation import AnimationDing0
from ding0.tools.plots import plot_mv_topology
from ding0.flexopt.reinforce_grid import *

import os
Expand Down Expand Up @@ -102,7 +103,7 @@ def orm(self):
"""Returns ORM data"""
return self._orm

def run_ding0(self, session, mv_grid_districts_no=None, debug=False):
def run_ding0(self, session, mv_grid_districts_no=None, debug=False, export_figures=False):
""" Let DING0 run by shouting at this method (or just call
it from NetworkDing0 instance). This method is a wrapper
for the main functionality of DING0.
Expand All @@ -116,6 +117,8 @@ def run_ding0(self, session, mv_grid_districts_no=None, debug=False):
all grid_districts & stations are imported)
debug : bool, defaults to False
If True, information is printed during process
export_figures : bool, defaults to False
If True, figures are shown or exported (default path: ~/.ding0/) during run.
Returns
-------
Expand Down Expand Up @@ -213,29 +216,51 @@ def run_ding0(self, session, mv_grid_districts_no=None, debug=False):
self.build_lv_grids()

# STEP 6: Build MV grids
self.mv_routing(debug=False, animation=False)
self.mv_routing(debug=False)
if export_figures:
grid = self._mv_grid_districts[0].mv_grid
plot_mv_topology(grid, subtitle='Routing completed', filename='1_routing_completed.png')

# STEP 7: Connect MV and LV generators
self.connect_generators(debug=False)
if export_figures:
plot_mv_topology(grid, subtitle='Generators connected', filename='2_generators_connected.png')

# STEP 8: Set IDs for all branches in MV and LV grids
self.set_branch_ids()

# STEP 9: Relocate switch disconnectors in MV grid
self.set_circuit_breakers(debug=debug)
if export_figures:
plot_mv_topology(grid, subtitle='Circuit breakers relocated', filename='3_circuit_breakers_relocated.png')

# STEP 10: Open all switch disconnectors in MV grid
self.control_circuit_breakers(mode='open')

# STEP 11: Do power flow analysis of MV grid
self.run_powerflow(session, method='onthefly', export_pypsa=False, debug=debug)
if export_figures:
plot_mv_topology(grid, subtitle='PF result (load case)',
filename='4_PF_result_load.png',
line_color='loading', node_color='voltage', testcase='load')
plot_mv_topology(grid, subtitle='PF result (feedin case)',
filename='5_PF_result_feedin.png',
line_color='loading', node_color='voltage', testcase='feedin')

# STEP 12: Reinforce MV grid
self.reinforce_grid()

# STEP 13: Close all switch disconnectors in MV grid
self.control_circuit_breakers(mode='close')

if export_figures:
plot_mv_topology(grid, subtitle='Final grid PF result (load case)',
filename='6_final_grid_PF_result_load.png',
line_color='loading', node_color='voltage', testcase='load')
plot_mv_topology(grid, subtitle='Final grid PF result (feedin case)',
filename='7_final_grid_PF_result_feedin.png',
line_color='loading', node_color='voltage', testcase='feedin')

if debug:
logger.info('Elapsed time for {0} MV Grid Districts (seconds): {1}'.format(
str(len(mv_grid_districts_no)), time.time() - start))
Expand Down
5 changes: 4 additions & 1 deletion ding0/core/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ def graph_add_node(self, node_object):
def graph_draw(self, mode):
""" Draws grid graph using networkx
This method is for debugging purposes only.
Use ding0.tools.plots.plot_mv_topology() for advanced plotting.
Parameters
----------
mode : str
Mode selection 'MV' or 'LV'. #TODO: check
Mode selection 'MV' or 'LV'.
Notes
-----
Expand Down
6 changes: 1 addition & 5 deletions ding0/examples/example_single_grid_district.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@
nd = NetworkDing0(name='network')

# choose MV Grid Districts to import
mv_grid_districts = [3040]
mv_grid_districts = [460]

# run DING0 on selected MV Grid District
nd.run_ding0(session=session,
mv_grid_districts_no=mv_grid_districts)

# export grids to database
# nd.export_mv_grid(conn, mv_grid_districts)
# nd.export_mv_grid_new(conn, mv_grid_districts)

# export grid to file (pickle)
save_nd_to_pickle(nd, filename='ding0_grids_example.pkl')
10 changes: 9 additions & 1 deletion ding0/grid/mv_grid/solvers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,18 @@ def draw_network(self, anim):
g.add_edges_from(e)

plt.figure()
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

if anim is not None:
nx.draw_networkx(g, nodes_pos, with_labels=False, node_size=50)
plt.savefig(anim.file_path + anim.file_prefix + (4 - len(str(anim.counter))) * '0' + str(anim.counter) + '.png')
plt.savefig(anim.file_path +
anim.file_prefix +
(4 - len(str(anim.counter))) * '0' +
str(anim.counter) + '.png',
dpi=150,
bbox_inches='tight')
anim.counter += 1
plt.close()
else:
Expand Down
12 changes: 6 additions & 6 deletions ding0/tools/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
__author__ = "nesnoj, gplssm"


import ding0
from ding0.tools import config as cfg_ding0
from ding0.tools.logger import get_default_home_dir

import os


class AnimationDing0:
""" Class for visual animation of routing process.
""" Class for visual animation of the routing process (solving CVRP).
(basically a central place to store information about output file and count of saved images).
Use argument 'animation=True' of method 'NetworkDing0.mv_routing()' to enable image export.
The images are exported to ding0's home dir which is usually ~/.ding0/ .
Subsequently, FFMPEG can be used to convert images to animation, e.g.
ffmpeg -r 10 -i mv-routing_ani_%04d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p mv-routing_ani.mp4
ffmpeg -r 5 -i mv-routing_ani_%04d.png -vframes 200 -r 15 -vcodec libx264 -y -an mv-routing_ani.mp4 -s 640x480
See Also
--------
Expand All @@ -36,8 +37,7 @@ class AnimationDing0:

def __init__(self, **kwargs):
output_animation_file_prefix = cfg_ding0.get('output', 'animation_file_prefix')
package_path = ding0.__path__[0]

self.file_path = os.path.join(package_path, 'output/animation/')
self.file_path = os.path.join(get_default_home_dir(), 'animation/')
self.file_prefix = output_animation_file_prefix
self.counter = 1
Loading

0 comments on commit 7c6e693

Please sign in to comment.