Grid2op / lightsim2grid

LightSim2grid implements a c++ backend targeting the Grid2Op (https://github.com/rte-france/grid2op) platform.
https://lightsim2grid.readthedocs.io/en/latest/
Mozilla Public License 2.0
51 stars 10 forks source link

A potential bug when using `PhysicalLawChecker` class #51

Closed Mleyliabadi closed 1 year ago

Mleyliabadi commented 2 years ago

Environment

Bug description

The following reported bug concerns the (PhysicalLawChecker) class, and more specifically when running the check_solution function to verify the Kirchhoff's law. It works correctly on real observations computed using physical solvers when there is no topology change (bus bar change) on the grid. However, when introducing some bus topology changes, the corresponding output of check_solution function does not respect the Kirchhoff's law, and there are some mismatches.

How to reproduce

To reproduce the bug, I have provided a Python code snippet (see below)

Code snippet

import grid2op
from lightsim2grid import LightSimBackend
from lightsim2grid import PhysicalLawChecker
from cmath import exp, pi

def compute_complex_v(env, obs):
    """
    Function to compute the complex voltage
    """
    Ybus=env.backend._grid.get_Ybus()
    theta_or=obs.theta_or
    theta_ex=obs.theta_ex

    lines_or_pu_to_kv=env.backend.lines_or_pu_to_kv
    lines_ex_pu_to_kv=env.backend.lines_ex_pu_to_kv
    v_or=obs.v_or/lines_or_pu_to_kv
    v_ex=obs.v_ex/lines_ex_pu_to_kv

    bus_theta = np.zeros(2*env.n_sub)
    bus_v = np.zeros(2*env.n_sub)

    lor_bus, lor_conn = obs._get_bus_id(
        obs.line_or_pos_topo_vect, obs.line_or_to_subid
    )
    lex_bus, lex_conn = obs._get_bus_id(
        obs.line_ex_pos_topo_vect, obs.line_ex_to_subid
    )

    bus_theta[lor_bus] = theta_or
    bus_theta[lex_bus] = theta_ex

    bus_v[lor_bus]=v_or
    bus_v[lex_bus]=v_ex
    bus_v_complex=np.array([bus_v[k]*exp(1j*(bus_theta[k]*2*pi)/360) for k in range(len(bus_v))])
    return bus_v_complex

# the normal case, without any topology change
env = grid2op.make("l2rpn_case14_sandbox", backend=LightSimBackend())
obs = env.reset()
v_complex = compute_complex_v(env, obs)
checker = PhysicalLawChecker(env)
mismatch = checker.check_solution(v_complex, obs)
tol = 1e-4
print(np.any(np.abs(mismatch) > tol)) # the output is `False`

# the case with topology change at substation 4
action = env.action_space({'set_bus':{'substations_id':[(4,(2,1,2,1,2))]}})
obs, reward, done, info = env.step(action)
v_complex = compute_complex_v(env, obs)
checker = PhysicalLawChecker(env)
mismatch = checker.check_solution(v_complex, obs)
tol=1e-2
print(np.any(mismatch > tol)) # the output is `True`

Current output

False
True

Expected output

The expected output without and with topology changes should be False which means that the Kirchhoff's law is respected for observations issued from Physical solver.

False
False
BDonnot commented 1 year ago

Hello,

With the example provided, by checking "mismatch" vector i got:

(Pdb) np.abs(mismatch)
array([0.00000000e+00, 1.18335433e-05, 9.50266363e-06, 1.75569287e-04,
       4.26133770e-05, 7.15545898e-06, 4.58905795e-05, 5.55111512e-15,
       2.68788108e-06, 1.73460272e-05, 3.77548176e-05, 2.36069288e-05,
       3.07799537e-05, 3.93859092e-06, 0.00000000e+00, 0.00000000e+00,
       0.00000000e+00, 0.00000000e+00, 8.75801899e-05, 0.00000000e+00,
       0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
       0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00])

Highest value is 1.75569287e-04 which seems to be totally negligible: input data are rounded at 1e-2 so anything bellow that is pure numeric noise.

And honestly a difference of 170W (1.7 e-4 MW) on a transmission grid cannot be seen at all.

Try update grid2op and lightsim2grid to latest version.