Check if some given (complex) Voltage meets the KCL
This module allows to check if a given vector checks the KCL (Kirchhoff Current Law) or not.
It is a wrapper around the lightsim2grid.gridmodel.GridModel.check_solution()
function.
TODO DOC in progress !
Warning
The grid2op environment is read from a grid.json` file. Make sure to use an environment that can be loaded by lightsim2grid (default environment uses pandapower and are fully compatible)
Examples
See the section Use with grid2op for more information and more examples.
For standard grid2op environment, you can use it like:
import grid2op
import numpy as np
from lightsim2grid import PhysicalLawChecker
# create a grid2op environment
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name, ...)
# create the checker
checker = PhysicalLawChecker(env)
# get an observation
obs = env.reset()
# retrieve somehow a complex voltage
v = np.zeros(2*env.n_sub, dtype=complex)
v[...] = ... # put here the value of the complex voltage you want to get
# check if it meets the KCL (Kirchhoff's Current Law)
mismatch = checker.check_solution(v, obs)
# mistmatch has same size as v and contains the (complex) current mismatch at each bus of the grid.
Detailed usage
Classes:

Utility tools to check that a given vector meets the KCL (Kirchhoff's Current Law) for a given grid (given as a grid2op observation). 
 class lightsim2grid.physical_law_checker.PhysicalLawChecker(grid2op_env)[source]
Utility tools to check that a given vector meets the KCL (Kirchhoff’s Current Law) for a given grid (given as a grid2op observation).
Notes
Due to floating point precision between grid2op (float32) and lightsim2grid (float64) there might be a slight difference in the “mismatch” in the order of 1e5 / 1e6. If you want to check the kirchoffs law, then you won’t be able to check with a tolerance less than 1e5.
Warning
The grid2op environment is read from a grid.json file. Make sure to use an environment that can be loaded by lightsim2grid !
Examples
It can be used as:
import grid2op import numpy as np from lightsim2grid import PhysicalLawChecker # create a grid2op environment env_name = "l2rpn_case14_sandbox" env = grid2op.make(env_name, ...) # create the checker checker = PhysicalLawChecker(env) # get an observation obs = env.reset() # retrieve somehow a complex voltage v = np.zeros(2*env.n_sub, dtype=complex) v[...] = ... # put here the value of the complex voltage you want to get # check if it meets the KCL (Kirchhoff's Current Law) mismatch = checker.check_solution(v, obs) # mistmatch has same size as v and contains the (complex) current mismatch at each bus of the grid.
Methods:
check_solution
(vcomplex, grid2op_obs[, ...])This function checks that the complete vcomplex vector meets the kirchhoff current laws for a given grid.
 check_solution(vcomplex, grid2op_obs, with_qlim=False)[source]
This function checks that the complete vcomplex vector meets the kirchhoff current laws for a given grid. This vector is expected to be a complex vector representing the complex voltage at each bus of a given powergrid.
The given grid is given in the form of a grid2op observation that should come from the same environment as the environment used to initialize the PhysicalLawChecker.
 Parameters:
vcomplex – A numpy complex vector representing the complex voltage at each bus (should have the same size as the total number of buses in the grid2op environment)
grid2op_obs – A grid2op observation representing the state of the grid
with_qlim – Whether to take into account the reactive limits for the generators
 Returns:
A numpy array (complex) that has the same shape as vcomplex and that computes the power mismatch at each bus of the grid.
 Return type:
res
Notes
The vector vcomplex needs to have as many components as the possible number of buses (typically 2 * env.n_sub) for regular grid2op environments.
If some buses are “empty” / “deactivated” (ie no elements are connected to it) then the given components of vcomplex is ignored: you can put whatever you want there it will have no impact on the final result.
The order of the buses are expected to be given in the following order: First n_sub components of vcomplex (and res) represents bus 1 of the substation 1, 2, …, n_sub and last n_sub components of vcomplex (and res) represents bus 2 of the same substation.