Skip to content

Commit

Permalink
Added logger to the numerical circuit compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
SanPen committed Jul 8, 2024
1 parent 5561819 commit 909aa4e
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/GridCalEngine/DataStructures/numerical_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,8 @@ def compile_numerical_circuit_at(circuit: MultiCircuit,
opf_results: Union[OptimalPowerFlowResults, None] = None,
use_stored_guess=False,
bus_dict: Union[Dict[Bus, int], None] = None,
areas_dict: Union[Dict[Area, int], None] = None) -> NumericalCircuit:
areas_dict: Union[Dict[Area, int], None] = None,
logger=Logger()) -> NumericalCircuit:
"""
Compile a NumericalCircuit from a MultiCircuit
:param circuit: MultiCircuit instance
Expand All @@ -2006,11 +2007,10 @@ def compile_numerical_circuit_at(circuit: MultiCircuit,
:param use_stored_guess: use the storage voltage guess?
:param bus_dict (optional) Dict[Bus, int] dictionary
:param areas_dict (optional) Dict[Area, int] dictionary
:param logger: Logger instance
:return: NumericalCircuit instance
"""

logger = Logger()

if circuit.get_connectivity_nodes_number() + circuit.get_switches_number():
# process topology, this
circuit.process_topology_at(t_idx=t_idx, logger=logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def run(self):
idx2b = self.options.bus_idx_to

# declare the numerical circuit
nc = compile_numerical_circuit_at(circuit=self.grid, t_idx=None)
nc = compile_numerical_circuit_at(circuit=self.grid, t_idx=None, logger=self.logger)

# declare the linear analysis
linear = LinearAnalysis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def run(self) -> None:
la_driver.run()

# get the branch indices to analyze
nc = compile_numerical_circuit_at(self.grid)
nc = compile_numerical_circuit_at(self.grid, logger=self.logger)
br_idx = nc.branch_data.get_monitor_enabled_indices()
con_br_idx = nc.branch_data.get_contingency_enabled_indices()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def run_at(self, t_idx: Union[int, None] = None) -> ContinuationPowerFlowResults
t_idx=t_idx,
apply_temperature=self.pf_options.apply_temperature_correction,
branch_tolerance_mode=self.pf_options.branch_impedance_tolerance_mode,
opf_results=self.opf_results)
opf_results=self.opf_results,
logger=self.logger)

islands = nc.split_into_islands(ignore_single_node_islands=self.pf_options.ignore_single_node_islands)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def run(self):
nc = compile_numerical_circuit_at(
circuit=self.grid,
t_idx=None,
opf_results=self.opf_results
opf_results=self.opf_results,
logger=self.logger
)

analysis = LinearAnalysis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def run(self):

nc = compile_numerical_circuit_at(circuit=self.grid,
t_idx=t,
opf_results=self.opf_time_series_results)
opf_results=self.opf_time_series_results,
logger=self.logger)

driver_ = LinearAnalysis(
numerical_circuit=nc,
Expand Down
3 changes: 2 additions & 1 deletion src/GridCalEngine/Simulations/NTC/ntc_opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,8 @@ def run_linear_ntc_opf_ts(grid: MultiCircuit,
nc: NumericalCircuit = compile_numerical_circuit_at(circuit=grid,
t_idx=t, # yes, this is not a bug
bus_dict=bus_dict,
areas_dict=areas_dict)
areas_dict=areas_dict,
logger=logger)

# formulate the bus angles ---------------------------------------------------------------------------------
for k in range(nc.bus_data.nbus):
Expand Down
2 changes: 1 addition & 1 deletion src/GridCalEngine/Simulations/NTC/ntc_ts_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def opf(self):
print('Compiling cicuit...')

tm0 = time.time()
nc = compile_numerical_circuit_at(self.grid, t_idx=None)
nc = compile_numerical_circuit_at(self.grid, t_idx=None, logger=self.logger)
self.logger.add_info(f'Time circuit compiled in {time.time() - tm0:.2f} scs')
print(f'Time circuit compiled in {time.time() - tm0:.2f} scs')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ def run_nonlinear_opf(grid: MultiCircuit,
"""

# compile the system
nc = compile_numerical_circuit_at(circuit=grid, t_idx=t_idx)
nc = compile_numerical_circuit_at(circuit=grid, t_idx=t_idx, logger=logger)

if pf_init:
if Sbus_pf0 is None:
Expand Down
3 changes: 2 additions & 1 deletion src/GridCalEngine/Simulations/OPF/linear_opf_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,8 @@ def run_linear_opf_ts(grid: MultiCircuit,
nc: NumericalCircuit = compile_numerical_circuit_at(circuit=grid,
t_idx=global_t_idx, # yes, this is not a bug
bus_dict=bus_dict,
areas_dict=areas_dict)
areas_dict=areas_dict,
logger=logger)

# formulate the bus angles ---------------------------------------------------------------------------------
for k in range(nc.bus_data.nbus):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,12 @@ def run_nonlinear_opf(grid: MultiCircuit,
:param Sbus_pf0: Sbus initial solution
:param voltage_pf0: Voltage initial solution
:param plot_error: Plot the error evolution
:param use_bound_slacks: add voltage module and branch loading slack variables? (default true)
:param logger: Logger object
:return: NonlinearOPFResults
"""

# compile the system
nc = compile_numerical_circuit_at(circuit=grid, t_idx=t_idx)
nc = compile_numerical_circuit_at(circuit=grid, t_idx=t_idx, logger=logger)

if pf_init:
if Sbus_pf0 is None:
Expand Down
3 changes: 2 additions & 1 deletion src/GridCalEngine/Simulations/PowerFlow/power_flow_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,8 @@ def multi_island_pf(multi_circuit: MultiCircuit,
opf_results=opf_results,
use_stored_guess=options.use_stored_guess,
bus_dict=bus_dict,
areas_dict=areas_dict
areas_dict=areas_dict,
logger=logger,
)
# print("Normal PowerFlow")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ def run(self):
t_idx=None,
apply_temperature=self.pf_options.apply_temperature_correction,
branch_tolerance_mode=self.pf_options.branch_impedance_tolerance_mode,
opf_results=self.opf_results)
opf_results=self.opf_results,
logger=self.logger)

calculation_inputs = numerical_circuit.split_into_islands(
ignore_single_node_islands=self.pf_options.ignore_single_node_islands)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def multi_island_sigma(multi_circuit: MultiCircuit,
nc = compile_numerical_circuit_at(circuit=multi_circuit,
apply_temperature=options.apply_temperature_correction,
branch_tolerance_mode=options.branch_impedance_tolerance_mode,
opf_results=None)
opf_results=None,
logger=logger)
results.bus_names = nc.bus_data.names

calculation_inputs = nc.split_into_islands(ignore_single_node_islands=options.ignore_single_node_islands)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def run(self):
n = len(self.grid.buses)
m = self.grid.get_branch_number()

numerical_circuit = compile_numerical_circuit_at(self.grid)
numerical_circuit = compile_numerical_circuit_at(self.grid, logger=self.logger)
self.results = StateEstimationResults(n=n,
m=m,
bus_names=numerical_circuit.bus_names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def run(self):

# compile
# print('Compiling...', end='')
nc = compile_numerical_circuit_at(self.grid, t_idx=None)
nc = compile_numerical_circuit_at(self.grid, t_idx=None, logger=self.logger)
calculation_inputs = nc.split_into_islands(ignore_single_node_islands=self.options.ignore_single_node_islands)

self.results = CascadingResults(self.cascade_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def run(self):
self.tic()

# compile the numerical circuit
numerical_circuit = compile_numerical_circuit_at(self.grid, t_idx=None)
numerical_circuit = compile_numerical_circuit_at(self.grid, t_idx=None, logger=self.logger)

evt = get_reliability_scenario(numerical_circuit,
horizon=1)
Expand Down
11 changes: 7 additions & 4 deletions src/GridCalEngine/Simulations/Stochastic/reliability_iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from GridCalEngine.Simulations.PowerFlow.power_flow_worker import PowerFlowOptions, multi_island_pf_nc, PowerFlowResults
from GridCalEngine.Devices.multi_circuit import MultiCircuit
from GridCalEngine.DataStructures.numerical_circuit import compile_numerical_circuit_at
from GridCalEngine.basic_structures import Vec, IntVec
from GridCalEngine.basic_structures import Vec, IntVec,Logger


def get_transition_probabilities(lbda: Vec, mu: Vec) -> Tuple[Vec, Vec]:
Expand All @@ -46,7 +46,8 @@ class ReliabilityIterable:

def __init__(self, grid: MultiCircuit,
forced_mttf: Union[None, float] = None,
forced_mttr: Union[None, float] = None):
forced_mttr: Union[None, float] = None,
logger: Logger = Logger()):
"""
:param grid: MultiCircuit
Expand All @@ -61,11 +62,13 @@ def __init__(self, grid: MultiCircuit,
# time index
self.t_idx = 0

self.logger = logger

# declare the power flow options
self.pf_options = PowerFlowOptions()

# compile the time step
nc = compile_numerical_circuit_at(self.grid, t_idx=None)
nc = compile_numerical_circuit_at(self.grid, t_idx=None, logger=logger)

# compute the transition probabilities
if forced_mttf is None:
Expand All @@ -90,7 +93,7 @@ def __next__(self) -> Tuple[IntVec, PowerFlowResults]:
raise StopIteration

# compile the time step
nc = compile_numerical_circuit_at(self.grid, t_idx=self.t_idx)
nc = compile_numerical_circuit_at(self.grid, t_idx=self.t_idx, logger=self.logger)

# determine the Markov states
p = np.random.random(nc.nbr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def run_single_thread_mc(self, use_lhs=False):
t_idx=None,
apply_temperature=False,
branch_tolerance_mode=BranchImpedanceMode.Specified,
opf_results=self.opf_time_series_results)
opf_results=self.opf_time_series_results,
logger=self.logger)

mc_results = StochasticPowerFlowResults(n=numerical_circuit.nbus,
m=numerical_circuit.nbr,
Expand Down
20 changes: 4 additions & 16 deletions src/GridCalEngine/Topology/topology_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,6 @@ def _add_candidate(self, new_candidate: Bus) -> int | None:
# the candidate was added already
return idx

def try_get_cn_candidate(self, cn: ConnectivityNode) -> Union[Bus, int]:
"""
:param cn:
:return:
"""
candidate = self._cn_to_candidate.get(cn, None)

if candidate is not None:
idx = self._candidate_to_int_dict[candidate]

return candidate, idx
else:
return None, None

def was_added(self, bus: Bus) -> bool:
"""
Check if a bus was added already
Expand All @@ -253,7 +238,7 @@ def _add_cn(self, cn: ConnectivityNode) -> int | None:
if cn.default_bus is None: # connectivity nodes can be linked to a previously existing Bus

# try to search if this CN has already been assigned a Bus
candidate_bus, idx = self.try_get_cn_candidate(cn=cn)
candidate_bus = self._cn_to_candidate.get(cn, None)

if candidate_bus is None:
# create a new candidate bus
Expand All @@ -266,6 +251,9 @@ def _add_cn(self, cn: ConnectivityNode) -> int | None:
# register
idx = self._add_candidate(candidate_bus)
self._cn_to_candidate[cn] = candidate_bus
else:
# there was a candidate already assigned to the CN, get its integer position
idx = self._candidate_to_int_dict[candidate_bus]

# cn.default_bus = candidate_bus # to avoid adding extra buses upon consecutive runs
self._add_new_candidate(candidate_bus)
Expand Down

0 comments on commit 909aa4e

Please sign in to comment.