Skip to content

Commit

Permalink
Moved NDArray typings to basic structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Santi committed Jun 22, 2023
1 parent f0171d5 commit 624dfe3
Show file tree
Hide file tree
Showing 7 changed files with 1,167 additions and 671 deletions.
1,716 changes: 1,111 additions & 605 deletions .idea/workspace.xml

Large diffs are not rendered by default.

58 changes: 18 additions & 40 deletions src/GridCal/Engine/Core/multi_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import cmath
import numpy as np
import pandas as pd
import nptyping as npt
from typing import List, Dict, Tuple, Union
from uuid import getnode as get_mac, uuid4
from datetime import timedelta, datetime
import networkx as nx
from matplotlib import pyplot as plt
from scipy.sparse import csc_matrix, lil_matrix

from GridCal.Engine.basic_structures import DateVec, IntVec, StrVec, Vec, Mat, CxVec, IntMat, CxMat
import GridCal.Engine.Devices as dev
import GridCal.Engine.basic_structures as bs
import GridCal.Engine.Core.topology as tp
Expand Down Expand Up @@ -185,10 +185,6 @@ class constructor
# logger of events
self.logger: bs.Logger = bs.Logger()

# Object with the necessary inputs for a power flow study
# todo: ¿se puede borrar?
self.numerical_circuit: Union["NumericalCircuit", None] = None

# Bus-Branch graph
self.graph = None

Expand All @@ -205,8 +201,7 @@ class constructor
self.branch_names: Union[List[str], None] = None

# master time profile
# todo typing this
self.time_profile = None
self.time_profile: DateVec = None

# contingencies
self.contingencies: List[dev.Contingency] = list()
Expand Down Expand Up @@ -313,7 +308,7 @@ def get_bus_number(self) -> int:
"""
return len(self.buses)

def get_bus_default_types(self) -> npt.NDArray[npt.Shape['*'], npt.Int]:
def get_bus_default_types(self) -> IntVec:
"""
Return an array of bus types
:return: number
Expand Down Expand Up @@ -345,7 +340,7 @@ def get_branch_names_wo_hvdc(self) -> List[str]:
names.append(elm.name)
return names

def get_branch_lists(self) -> List[Union[dev.Branch, dev.HvdcLine]]:
def get_branch_lists(self) -> List[Union[List[dev.Branch], List[dev.HvdcLine]]]:
"""
GEt list of the branch lists
:return:
Expand Down Expand Up @@ -408,7 +403,7 @@ def get_dimensions(self):
"""
return self.get_bus_number(), self.get_branch_number(), self.get_time_number()

def get_branch_active_time_array(self) -> npt.NDArray[npt.Shape['*, *'], npt.Int]:
def get_branch_active_time_array(self) -> IntMat:
"""
Get branch active matrix
:return: array with branch active status
Expand Down Expand Up @@ -480,9 +475,6 @@ def clear(self):
# List of transformer types
self.transformer_types = list()

# Object with the necessary inputs for a power flow study
self.numerical_circuit = None

# Bus-Branch graph
self.graph = None

Expand Down Expand Up @@ -598,7 +590,7 @@ def get_loads_number(self) -> int:
val = val + len(bus.loads)
return val

def get_load_names(self) -> npt.NDArray[npt.Shape['*'], npt.String]:
def get_load_names(self) -> StrVec:
"""
Returns a list of :ref:`Load<load>` names.
"""
Expand All @@ -619,7 +611,7 @@ def get_external_grids(self) -> List[dev.ExternalGrid]:
lst = lst + bus.external_grids
return lst

def get_external_grid_names(self) -> npt.NDArray[npt.Shape['*'], npt.String]:
def get_external_grid_names(self) -> StrVec:
"""
Returns a list of :ref:`ExternalGrid<external_grid>` names.
"""
Expand All @@ -640,7 +632,7 @@ def get_static_generators(self) -> List[dev.StaticGenerator]:
lst = lst + bus.static_generators
return lst

def get_static_generators_names(self) -> npt.NDArray[npt.Shape['*'], npt.String]:
def get_static_generators_names(self) -> StrVec:
"""
Returns a list of :ref:`StaticGenerator<static_generator>` names.
"""
Expand Down Expand Up @@ -679,7 +671,7 @@ def get_calculation_loads_number(self) -> int:
val = val + len(bus.loads)
return val

def get_calculation_load_names(self) -> npt.NDArray[npt.Shape['*'], npt.String]:
def get_calculation_load_names(self) -> StrVec:
"""
Returns a list of :ref:`Load<load>` names.
"""
Expand Down Expand Up @@ -970,8 +962,6 @@ def copy(self):

cpy.time_profile = self.time_profile.copy()

# cpy.numerical_circuit = self.numerical_circuit.copy()

return cpy

def get_catalogue_dict(self, branches_only=False):
Expand Down Expand Up @@ -2907,7 +2897,7 @@ def set_contingencies(self, contingencies: List[dev.Contingency]):

return logger

def get_voltage_guess(self) -> npt.NDArray[npt.Shape['*'], npt.Complex]:
def get_voltage_guess(self) -> CxVec:
"""
Get the buses stored voltage guess
:return: array of complex voltages per bus
Expand All @@ -2920,19 +2910,7 @@ def get_voltage_guess(self) -> npt.NDArray[npt.Shape['*'], npt.Complex]:

return v

def get_Sbus(self) -> npt.NDArray[npt.Shape['*'], npt.Complex]:
"""
Get the complex bus power injections
:return: (ntime, nbus) [MW + j MVAr]
"""
val = np.zeros(self.get_bus_number(), dtype=complex)

for i, bus in enumerate(self.buses):
val[i] = bus.get_Sbus()

return val

def get_Sbus_prof(self) -> npt.NDArray[npt.Shape['*, *'], npt.Complex]:
def get_Sbus(self) -> CxVec:
"""
Get the complex bus power injections
:return: (ntime, nbus) [MW + j MVAr]
Expand All @@ -2944,7 +2922,7 @@ def get_Sbus_prof(self) -> npt.NDArray[npt.Shape['*, *'], npt.Complex]:

return val

def get_Sbus_prof(self) -> np.ndarray:
def get_Sbus_prof(self) -> CxMat:
"""
Get the complex bus power injections
:return: (ntime, nbus) [MW + j MVAr]
Expand All @@ -2956,13 +2934,13 @@ def get_Sbus_prof(self) -> np.ndarray:

return val

def get_Pbus(self):
def get_Pbus(self) -> Vec:
return self.get_Sbus().real

def get_Pbus_prof(self):
def get_Pbus_prof(self) -> Mat:
return self.get_Sbus_prof().real

def get_branch_rates_prof_wo_hvdc(self) -> np.ndarray:
def get_branch_rates_prof_wo_hvdc(self) -> Mat:
"""
Get the complex bus power injections
:return: (ntime, nbr) [MVA]
Expand All @@ -2974,7 +2952,7 @@ def get_branch_rates_prof_wo_hvdc(self) -> np.ndarray:

return val

def get_branch_rates_wo_hvdc(self) -> np.ndarray:
def get_branch_rates_wo_hvdc(self) -> Vec:
"""
Get the complex bus power injections
:return: (nbr) [MVA]
Expand All @@ -2986,7 +2964,7 @@ def get_branch_rates_wo_hvdc(self) -> np.ndarray:

return val

def get_branch_contingency_rates_prof_wo_hvdc(self) -> np.ndarray:
def get_branch_contingency_rates_prof_wo_hvdc(self) -> Mat:
"""
Get the complex bus power injections
:return: (ntime, nbr) [MVA]
Expand All @@ -2998,7 +2976,7 @@ def get_branch_contingency_rates_prof_wo_hvdc(self) -> np.ndarray:

return val

def get_branch_contingency_rates_wo_hvdc(self) -> np.ndarray:
def get_branch_contingency_rates_wo_hvdc(self) -> Vec:
"""
Get the complex bus power injections
:return: (nbr) [MVA]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import nptyping as npt

import GridCal.Engine.basic_structures as bs
from GridCal.Engine.basic_structures import DateVec, IntVec, StrVec, Vec, Mat, CxVec, IntMat, CxMat
from GridCal.Engine.Core.multi_circuit import MultiCircuit
from GridCal.Engine.Simulations.LinearFactors.linear_analysis_ts_driver import LinearAnalysisTimeSeriesDriver, LinearAnalysisOptions
from GridCal.Engine.Simulations.ContingencyAnalysis.contingency_analysis_driver import ContingencyAnalysisOptions, ContingencyAnalysisDriver
Expand Down Expand Up @@ -122,7 +123,7 @@ def __init__(
self,
grid: MultiCircuit,
options: Union[ContingencyAnalysisOptions, LinearAnalysisOptions],
time_indices: npt.NDArray[npt.Shape['*'], npt.Int],
time_indices: IntVec,
clustering_results: Union["ClusteringResults", None] = None,
):
"""
Expand Down Expand Up @@ -155,10 +156,7 @@ def __init__(
con_names=()
)

self.branch_names: npt.NDArray[nptyping.Shape['*'], npt.String] = np.empty(
shape=grid.get_branch_number_wo_hvdc(),
dtype=str,
)
self.branch_names: StrVec = np.empty(shape=grid.get_branch_number_wo_hvdc(), dtype=str)

def n_minus_k(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import numpy as np
import time
import nptyping as npt
from typing import Dict, Union
from GridCal.Engine.basic_structures import DateVec, IntVec, StrVec, Vec, Mat, CxVec
from GridCal.Engine.Core.multi_circuit import MultiCircuit
from GridCal.Engine.Simulations.LinearFactors.linear_analysis import LinearAnalysis
from GridCal.Engine.Simulations.LinearFactors.linear_analysis_driver import LinearAnalysisOptions
Expand All @@ -37,7 +37,7 @@ def __init__(
self,
grid: MultiCircuit,
options: LinearAnalysisOptions,
time_indices: npt.NDArray[npt.Shape['*'], npt.Int],
time_indices: IntVec,
clustering_results: Union[ClusteringResults, None] = None,
):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
import nptyping as npt
import GridCal.Engine.basic_structures as bs
from GridCal.Engine.basic_structures import DateVec, IntVec, StrVec, Vec, Mat, CxVec
from GridCal.Engine.Simulations.result_types import ResultTypes
from GridCal.Engine.Simulations.results_table import ResultsTable
from GridCal.Engine.Simulations.results_template import ResultsTemplate
Expand All @@ -30,10 +31,10 @@ def __init__(
self,
n: int,
m: int,
time_array: npt.NDArray[npt.Shape['*'], npt.Datetime64],
bus_names: npt.NDArray[npt.Shape['*'], npt.String],
bus_types: npt.NDArray[npt.Shape['*'], npt.Int],
branch_names: npt.NDArray[npt.Shape['*'], npt.String],
time_array: DateVec,
bus_names: StrVec,
bus_types: IntVec,
branch_names: StrVec,
):
"""
Constructor
Expand Down Expand Up @@ -68,25 +69,25 @@ def __init__(
self.nt: int = len(time_array)
self.m: int = m
self.n: int = n
self.time: npt.NDArray[npt.Shape['*'], npt.Datetime64] = time_array
self.time: DateVec = time_array

self.bus_names: npt.NDArray[npt.Shape['*'], npt.String] = bus_names
self.bus_names: StrVec = bus_names

self.bus_types: npt.NDArray[npt.Shape['*'], npt.Int] = bus_types
self.bus_types: IntVec = bus_types

self.branch_names: npt.NDArray[npt.Shape['*'], npt.String] = branch_names
self.branch_names: StrVec = branch_names

self.voltage: npt.NDArray[npt.Shape['*, *'], npt.Complex] = np.ones((self.nt, n), dtype=complex)
self.voltage: CxVec = np.ones((self.nt, n), dtype=complex)

self.S: npt.NDArray[npt.Shape['*, *'], npt.Complex] = np.zeros((self.nt, n), dtype=complex)
self.S: CxVec = np.zeros((self.nt, n), dtype=complex)

self.Sf: npt.NDArray[npt.Shape['*, *'], npt.Complex] = np.zeros((self.nt, m), dtype=complex)
self.Sf: CxVec = np.zeros((self.nt, m), dtype=complex)

self.loading: npt.NDArray[npt.Shape['*, *'], npt.Float] = np.zeros((self.nt, m), dtype=float)
self.loading: Vec = np.zeros((self.nt, m), dtype=float)

self.losses: npt.NDArray[npt.Shape['*, *'], npt.Float] = np.zeros((self.nt, m), dtype=float)
self.losses: CxVec = np.zeros((self.nt, m), dtype=float)

def apply_new_time_series_rates(self, nc: NumericalCircuit) -> npt.NDArray[npt.Shape['*, *'], npt.Float]:
def apply_new_time_series_rates(self, nc: NumericalCircuit) -> Mat:
rates = nc.Rates.T
self.loading = self.Sf / (rates + 1e-9)

Expand Down
11 changes: 6 additions & 5 deletions src/GridCal/Engine/Simulations/driver_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np
import nptyping as npt
from typing import List, Dict, Union
from GridCal.Engine.basic_structures import DateVec, IntVec, StrVec, Vec, Mat, CxVec, IntMat, CxMat
from GridCal.Engine.Simulations.driver_types import SimulationTypes
from GridCal.Engine.basic_structures import Logger
from GridCal.Engine.Core.multi_circuit import MultiCircuit
Expand Down Expand Up @@ -86,7 +87,7 @@ class TimeSeriesDriverTemplate(DriverTemplate):
def __init__(
self,
grid: MultiCircuit,
time_indices: npt.NDArray[npt.Shape['*'], npt.Int],
time_indices: IntVec,
clustering_results: Union["ClusteringResults", None] = None,
engine: bs.EngineType = bs.EngineType.GridCal):
"""
Expand All @@ -99,12 +100,12 @@ def __init__(
DriverTemplate.__init__(self, grid=grid, engine=engine)

if clustering_results:
self.time_indices = clustering_results.time_indices
self.sampled_probabilities = clustering_results.sampled_probabilities
self.time_indices: IntVec = clustering_results.time_indices
self.sampled_probabilities: Vec = clustering_results.sampled_probabilities

else:
self.time_indices = time_indices
self.sampled_probabilities = np.ones(shape=len(self.time_indices)) / len(self.time_indices)
self.time_indices: IntVec = time_indices
self.sampled_probabilities: Vec = np.ones(shape=len(self.time_indices)) / len(self.time_indices)

self.topologic_groups: Dict[int, List[int]] = self.get_topologic_groups()

Expand Down
12 changes: 12 additions & 0 deletions src/GridCal/Engine/basic_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@
import pandas as pd
import numpy as np
import datetime
import nptyping as npt

from GridCal.Gui.plot_config import LINEWIDTH, plt


DateVec = npt.NDArray[npt.Shape['*'], npt.Datetime64]
IntVec = npt.NDArray[npt.Shape['*'], npt.Int]
Vec = npt.NDArray[npt.Shape['*'], npt.Double]
CxVec = npt.NDArray[npt.Shape['*'], npt.Complex]
StrVec = npt.NDArray[npt.Shape['*'], npt.String]
Mat = npt.NDArray[npt.Shape['*, *'], npt.Double]
CxMat = npt.NDArray[npt.Shape['*, *'], npt.Complex]
IntMat = npt.NDArray[npt.Shape['*, *'], npt.Int]
StrMat = npt.NDArray[npt.Shape['*, *'], npt.String]


class BusMode(Enum):
PQ = 1
PV = 2
Expand Down

0 comments on commit 624dfe3

Please sign in to comment.