Skip to content

Commit

Permalink
Greatly improved the schematic device plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
SanPen committed Sep 26, 2024
1 parent bd50554 commit 9b2196a
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 213 deletions.
42 changes: 17 additions & 25 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/GridCal/Gui/Diagrams/MapWidget/grid_map_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from __future__ import annotations
import os
from typing import Union, List, Tuple, Dict
from typing import Union, List, Tuple, Dict, TYPE_CHECKING
import json
import numpy as np
import math
Expand Down Expand Up @@ -59,6 +60,9 @@
from GridCal.Gui.Diagrams.base_diagram_widget import BaseDiagramWidget
from GridCal.Gui.messages import error_msg

if TYPE_CHECKING:
from GridCal.Gui.Main.GridCalMain import MainGUI

MAP_BRANCH_GRAPHIC_TYPES = Union[
MapAcLine, MapDcLine, MapHvdcLine, MapFluidPathLine
]
Expand Down Expand Up @@ -203,6 +207,7 @@ class GridMapWidget(BaseDiagramWidget):
"""

def __init__(self,
gui: MainGUI,
tile_src: Tiles,
start_level: int,
longitude: float,
Expand All @@ -226,6 +231,7 @@ def __init__(self,
"""

BaseDiagramWidget.__init__(self,
gui=gui,
circuit=circuit,
diagram=MapDiagram(name=name,
tile_source=tile_src.TilesetName,
Expand Down
51 changes: 27 additions & 24 deletions src/GridCal/Gui/Diagrams/SchematicWidget/schematic_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from __future__ import annotations
import sys
import os
import json
import numpy as np
import pandas as pd
from typing import List, Dict, Union, Tuple
from typing import List, Dict, Union, Tuple, TYPE_CHECKING
from collections.abc import Callable
from warnings import warn
import networkx as nx
Expand Down Expand Up @@ -53,7 +54,8 @@
from GridCalEngine.Devices.Fluid import FluidNode, FluidPath
from GridCalEngine.Devices.Diagrams.schematic_diagram import SchematicDiagram
from GridCalEngine.Devices.Diagrams.graphic_location import GraphicLocation
from GridCalEngine.enumerations import DeviceType, SimulationTypes
from GridCalEngine.Simulations import PowerFlowTimeSeriesResults
from GridCalEngine.enumerations import DeviceType, SimulationTypes, ResultTypes
from GridCalEngine.basic_structures import Vec, CxVec, IntVec, Logger

from GridCal.Gui.Diagrams.SchematicWidget.terminal_item import BarTerminalItem, RoundTerminalItem
Expand Down Expand Up @@ -81,6 +83,9 @@
import GridCalEngine.Devices.Diagrams.palettes as palettes
from GridCal.Gui.messages import info_msg, error_msg, warning_msg, yes_no_question

if TYPE_CHECKING:
from GridCal.Gui.Main.GridCalMain import MainGUI

BRANCH_GRAPHICS = Union[
LineGraphicItem,
WindingGraphicItem,
Expand Down Expand Up @@ -335,6 +340,7 @@ class SchematicWidget(BaseDiagramWidget):
"""

def __init__(self,
gui: MainGUI,
circuit: MultiCircuit,
diagram: Union[SchematicDiagram, None],
default_bus_voltage: float = 10.0,
Expand All @@ -351,6 +357,7 @@ def __init__(self,
"""

BaseDiagramWidget.__init__(self,
gui=gui,
circuit=circuit,
diagram=diagram,
library_model=SchematicLibraryModel(),
Expand Down Expand Up @@ -3700,15 +3707,21 @@ def get_boundaries(self):

return min_x, max_x, min_y, max_y

def plot_bus(self, i, api_object: Bus):
def plot_bus(self, i: int, api_object: Bus):
"""
Plot branch results
:param i: branch index (not counting HVDC lines because those are not real Branches)
:param i: bus index
:param api_object: Bus API object
:return:
"""
fig = plt.figure(figsize=(12, 8))
ax_1 = fig.add_subplot(211)
ax_1.set_title('Power', fontsize=14)
ax_1.set_ylabel('Injections [MW]', fontsize=11)

ax_2 = fig.add_subplot(212, sharex=ax_1)
ax_2.set_title('Time', fontsize=14)
ax_2.set_ylabel('Voltage [p.u]', fontsize=11)

# set time
x = self.circuit.get_time_array()
Expand All @@ -3719,18 +3732,16 @@ def plot_bus(self, i, api_object: Bus):
# Get all devices grouped by bus
all_data = self.circuit.get_injection_devices_grouped_by_bus()

# filter injections by bus
bus_devices = all_data.get(api_object, None)

voltage = dict()

for key, driver in self.results_dictionary.items():
if hasattr(driver, 'results'):
if driver.results is not None:
if key == SimulationTypes.PowerFlowTimeSeries_run:
voltage[key] = np.abs(driver.results.voltage[:, i])
# search drivers for voltage data
for driver, results in self.gui.session.drivers_results_iter():
if results is not None:
if isinstance(results, PowerFlowTimeSeriesResults):
table = results.mdl(result_type=ResultTypes.BusVoltageModule)
table.plot(ax=ax_2, selected_col_idx=[i])

# Injections
# filter injections by bus
bus_devices = all_data.get(api_object, None)
if bus_devices:

power_data = dict()
Expand All @@ -3752,23 +3763,14 @@ def plot_bus(self, i, api_object: Bus):
raise Exception("Missing shunt device for plotting")

df = pd.DataFrame(data=power_data, index=x)
ax_1.set_title('Power', fontsize=14)
ax_1.set_ylabel('Injections [MW]', fontsize=11)

try:
# yt area plots
df.plot.area(ax=ax_1)
except ValueError:
# use regular plots
df.plot(ax=ax_1)

# voltage
if len(voltage.keys()):
ax_2 = fig.add_subplot(212, sharex=ax_1)
df = pd.DataFrame(data=voltage, index=x)
ax_2.set_title('Time', fontsize=14)
ax_2.set_ylabel('Voltage [p.u]', fontsize=11)
df.plot(ax=ax_2)

plt.legend()
fig.suptitle(api_object.name, fontsize=20)

Expand Down Expand Up @@ -4115,6 +4117,7 @@ def copy(self) -> "SchematicWidget":
logger=self.logger)

return SchematicWidget(
gui=self.gui,
circuit=self.circuit,
diagram=self.diagram,
default_bus_voltage=self.default_bus_voltage,
Expand Down
Loading

0 comments on commit 9b2196a

Please sign in to comment.