|BDonnot c78e5ec40f||4 weeks ago|
|.circleci||2 months ago|
|.github||1 month ago|
|SuiteSparse@1869379f46||4 weeks ago|
|benchmarks||4 weeks ago|
|build_cmake||1 month ago|
|docs||4 weeks ago|
|eigen@1f4c0311cd||2 months ago|
|lightsim2grid||4 weeks ago|
|old||1 year ago|
|src||4 weeks ago|
|utils||5 months ago|
|wheelhouse||5 months ago|
|.gitignore||4 weeks ago|
|.gitmodules||1 year ago|
|.readthedocs.yml||12 months ago|
|AUTHORS.txt||1 year ago|
|CHANGELOG.rst||4 weeks ago|
|DISCLAIMER.md||12 months ago|
|LICENSE||1 year ago|
|LICENSE.md||1 year ago|
|Makefile||12 months ago|
|README.md||4 weeks ago|
|compare_pandapower.py||1 year ago|
|pyproject.toml||1 month ago|
|requirements.txt||1 month ago|
|setup.py||4 weeks ago|
Provide a fast backend for grid2op using c++ KLU and Eigen librairies. Its primary goal is to serve as a fast backend for the grid2op platform, used primarily as a testbed platform for sequential decision making in the world of power system.
See the Disclaimer to have a more detailed view on what is and what is not this package. For example this package should not be used for detailed power system computations or simulations.
Once installed (don't forget, if you used the optional virtual env
above you need to load it with
source venv/bin/activate) you can
use it as any python package.
This functionality requires you to have grid2op installed, with at least version 0.7.0. You can install it with
pip install grid2op>=0.7.0
Then you can use a LightSimBackend instead of the default PandapowerBackend this way:
import grid2op from lightsim2grid import LightSimBackend backend = LightSimBackend() env = grid2op.make(backend=backend) # do regular computation as you would with grid2op
And you are good to go.
It is also possible to use directly the "solver" part of lightsim2grid.
Suppose you somehow get:
Ybusthe admittance matrix of your powersystem given by pandapower
V0the (complex) voltage vector at each bus given by pandapower
Sbusthe (complex) power absorb at each bus as given by pandapower
ppcia ppc internal pandapower test case
pvlist of PV buses
pqlist of PQ buses
optionslist of pandapower "options"
You can define replace the
newtonpf function of
pandapower.pandapower.newtonpf function with the following
piece of code:
from lightsim2grid.newtonpf import newtonpf V, converged, iterations, J = newtonpf(Ybus, V, Sbus, pv, pq, ppci, options)
This function uses the KLU algorithm and a c++ implementation of a Newton solver for speed.
Since version 0.5.3, lightsim2grid is can be installed like most python packages, with a call to:
python -m pip install lightsim2grid
It includes faster grid2op backend and the
KLU solver, even on windows. This is definitely the
easiest method to install lightsim2grid on your system and have it running without too much issues.
Note though that these packages have been compiled on a different platform that the one you are using. You might still get some benefit (in terms of performances) to install it from your on your machine.
You need to:
This package relies on the excellent
pybind11 package to integrate c++ code into python easily.
So to install lightsim2grid you need
pybind11 and its requirement, which include a working compiler: for example
(as of writing)
gcc (default on ubuntu, version >= 4.8), clang (default on MacOS, version >= 5.0.0) or
Microsoft visual studio (Microsoft Visual Studio 2015 Update 3 or newer).
This readme does not cover the install of such compilers. Please refer to the documentation of pybind11 for more information. Do not hesitate to write github issues if you encounter a problem in installing such compiler (nb on windows you have to install visual studio, on linux of MacOs you might already have a working compiler installed).
First, you can download it with git with:
git clone https://github.com/BDonnot/lightsim2grid.git cd lightsim2grid # it is recommended to do a python virtual environment python -m virtualenv venv # optional source venv/bin/activate # optional # retrieve the code of SparseSuite and Eigen (dependencies, mandatory) git submodule init git submodule update
SuiteSparse comes with the faster KLU linear solver.
Since version 0.3.0 this requirement has been removed. This entails that on linux / macos you can still benefit from the faster KLU solver. You can still benefit from the speed up of lightsim (versus the default PandaPowerBackend) but this speed up will be less than if you manage to compile SuiteSparse (see the subsection Benchmark for more information).
NB in both cases the algorithm to compute the powerflow is exactly the same. It is a Newton-Raphson based method. But to carry out this algorithm, one need to solver some linear equations. The only difference in the two version (with KLU and without) is that the linear equation solver is different. Up to the double float precision, both results (with and without KLU) should match.
We only detail the compilation on a system using "make" (so most likely GNU-Linux and MacOS). If you don't feel comfortable with this, either you can ignore it, or you have also the possibility to use the provided a docker version. See the next section Installation Using Docker for more information.
This is the easiest method to compile SuiteSparse on your system but unfortunately it only works on OS where "make" is available (eg Linux or MacOS) but this will not work on Windows... The compilation on windows is covered in the next paragraph (optional) option B. Compilation of SuiteSparse using "cmake"
Anyway, in this case, it's super easy. Just do:
# compile static libraries of SparseSuite make
And you are good to go. Nothing more.
This works on most platform including MacOS, Linux and Windows.
It requires to install the free
cmake program and to do a bit more works than for other system. This is why we
only recommend to use it on Windows.
The main steps (for windows, somme commands needs to be adapted on linux / macos) are:
mkdir buildand cd there:
cmake -DCMAKE_INSTALL_PREFIX=..\built -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
cmake --build . --config Release --target install
For more information, feel free to read the dedicated README.
Another linear solver that can be used with lighsim2grid is the "NICSLU" linear solver that might, in some cases, be even faster than the KLU linear solver. This can lead to more speed up if using lighsim2grid.
To use it, you need to:
NICSLU_GIT(eg NICSLU_GIT="/home/user/Documents/nicslu/"). Also note that you need to check that your usage is compliant with their license !
export PATH_NICSLU=NICSLU_GIT/nicsluDATE(for example
export PATH_NICSLU=/home/user/Documents/nicslu/nicslu202103if you cloned the repository as the example of
step 1)and use the version of nicslu compiled by the author on March 2021 [version distributed at time of writing the readme] )
And this is it. Lightsim will be able to use this linear solver.
Be carefull though, you require a license file in order to use it. As of now, the best way is to copy paste the license file at the same location that the one you execute python from (ie you need to copy paste it each time). We will try to find another solution.
Now you simply need to install the lightsim2grid package this way, like any python package:
# install the dependency pip install -U pybind11 # compile and install the python package pip install -U .
And you are done :-)
In this section we will expose some brief benchmarks about the use of lightsim2grid in the grid2op settings. The code to run these benchmarks are given with this package int the benchmark folder.
All of them has been run on a computer with the following configuration: Configuration:
The code to reproduce the benchmark on your machine are given, once
cd into the
cd benchmarks # cd in the lightsim2grid benchmarks directory if not already python3 benchmark_solvers.py --name l2rpn_case14_sandbox --no_test --number 1000 python3 benchmark_solvers.py --name l2rpn_neurips_2020_track2_small --no_test --number 1000
(results may vary depending on the hard drive, the ram etc. )
(to run these benchmarks, some data will automatically be downloaded, this requires an internet access)
(we remind that these simulations correspond to simulation on one core of the CPU. Of course it is possible to make use of all the available cores, which would increase the number of steps that can be performed per second)
We compare 6 different solvers:
SuiteSparsec package implemented in KLUSolver.
First on an environment based on the IEEE case14 grid:
|case14_sandbox||grid2op speed (it/s)||grid2op 'backend.runpf' time (ms)||solver powerflow time (ms)|
From a grid2op perspective, lightsim2grid allows to compute up to ~1000 steps each second on the case 14 and "only" 61 for the default PandaPower Backend, leading to a speed up of ~16 in this case (lightsim2grid is ~16 times faster than Pandapower). For such a small environment, there is no sensible difference in using KLU linear solver compared to using the SparseLU solver of Eigen (1030 vs 992 iterations on the reported runs, might slightly vary across runs). KLU and NICSLU achieve almost identical performances.
Then on an environment based on the IEEE case 118:
|neurips_2020_track2||grid2op speed (it/s)||grid2op 'backend.runpf' time (ms)||solver powerflow time (ms)|
For an environment based on the IEEE 118, the speed up in using lightsim + KLU (LS+KLU) is ~18 time faster than
using the default PandaPower backend. The speed up of lightsim + SparseLU is a bit lower, but it is still ~10
times faster than using the default backend [the
LS+KLU solver is ~2-3 times faster than the
0.33 ms per powerflow for
L2+KLU compared to
0.79 ms for
LS+SLU), but it only translates to
providing ~30-40% more
iterations per second in the total program (
533) mainly because grid2op itself takes some times to modify the
grid and performs all the check it does.] For this testcase once again there is no noticeable difference between
If we look now only at the time to compute one powerflow (and don't take into account the time to load the data, to initialize the solver, to modify the grid, read back the results, to perform the other update in the grid2op environment etc.) we can notice that it takes on average (over 1000 different states) approximately 0.33ms to compute a powerflow with the LightSimBackend (if using the KLU linear solver) compared to the 6.6 ms when using the PandaPowerBackend (speed up of ~18 times)
NB pandapower performances heavily depends on the pandas version used, we used here a version of pandas which we found gave the best performances on our machine.
Lightsim2grid aims at providing a somewhat efficient (in terms of computation speed) backend targeting the grid2op platform.
It provides a c++ api, compatible with grid2op that is able to compute flows (and voltages and reactive power) from a given grid. This grid can be modified according to grid2op mechanism (see more information in the official grid2Op documentation ).
This code do not aim at providing state of the art solver in term of performances nor in terms of realism in the modeling of power system elements (eg loads, generators, powerlines, transformers, etc.).
Lightsim2grid codebase is "organized" in 4 different parts:
Ybus(sparse) complex admitance matrix and
Sbuscomplex injection vector from the state of the powergrid (eg physical properties of each elements, which elements are in service, which power is produce at each generators and consumed at each loads, what is the grid topology etc.)
V(and part of the
Sbusvector) the equation
V.(Ybus.V)* = Sbuswith the "standard" "powerflow constraints" (eg the voltage magnitude of
Vis set at given components, and on other it's the imaginary part of
Sbuscomplex vectors computed at step 3).
For now some basic "solver" (eg the program that performs points
3. above) are available, based on the
Gauss Seidel or the Newton-Raphson methods to perform "powerflows".
Nothing prevents any other "solver" to be used with lightsim2grid and thus with grid2op. For this, you simply need to implement, in c++ a "lightsim2grid solver" which mainly consists in defining a function:
bool compute_pf(const Eigen::SparseMatrix<cplx_type> & Ybus, // the admittance matrix CplxVect & V, // store the results of the powerflow and the Vinit ! const CplxVect & Sbus, // the injection vector const Eigen::VectorXi & pv, // (might be ignored) index of the components of Sbus should be computed const Eigen::VectorXi & pq, // (might be ignored) index of the components of |V| should be computed int max_iter, // maximum number of iteration (might be ignored) real_type tol // solver tolerance );
The types used are:
real_type: double => type representing the real number
cplx_type: std::complex<real_type> => type representing the complex number
CplxVect: Eigen::Matrix<cplx_type, Eigen::Dynamic, 1> => type representing a vector of complex elements
Eigen::VectorXi=> represents a vector of integer
Eigen::SparseMatrix<cplx_type>=> represents a sparse matrix
See for example BaseNRSolver for the implementation of a Newton Raphson solver (and its derived classes KLUSolver and SparseLUSolver that uses different routine to implement this algorithm) for examples on how to implement a solver.
Any contribution in this area is more than welcome.
NB It is not mandatory to "embed" all the code of the solver in lightsim2grid. Thanks to different customization, it is perfectly possible to install a given "lightsim solver" only if certain conditions are met. For example, on windows based machine, the SuiteSparse library cannot be easily compiled, and the KLUSolver is then not available.
It would be totally fine if some "lightsim2grid" solvers are available only if some packages are installed on the machine for example.
In this section we cover the use of docker with grid2op.
First, you need to install docker. You can consult the docker on windows if you use a windows like operating system, if you are using MacOs you can consult docker on Mac. The installation of docker on linux depends on your linux distribution, we will not list them all here.
Once done, you can simply "install" the lightsim2grid image with:
docker pull bdonnot/lightsim2grid:latest
This step should be done only once (unless you delete the image) it will download approximately 4 or 5GB from the internet. The lightsim2grid image contains lightsim and grid2op python packages (as well as their dependencies), equivalent of what would be installed if you typed:
pip install -U grid2op[optional] pybind11 # and do steps detailed in section "Installation (from source)" # that we will not repeat
You can skip this section if you know how to use docker. We will present here "the simplest way" to use. This is NOT a tutorial on docker, and you can find better use of this technology on the docker website.
For this tutorial, we suppose you have a script named
my_script.py located in the directory (complete path)
DIR_PATH (e.g. on windows you can have
DIR_PATH looking like "c:\User\MyName\L2RPNCompeitionCode" or
DIR_PATH will look like "/home/MyName/L2RPNCompeitionCode", this path is your choice, you can name it
the way you like)
You first need to start a docker container and tell docker that the container can access your local files with the following command:
docker run -t -d -p 8888:8888 --name lightsim_container -v DIR_PATH:/L2RPNCompeitionCode -w /L2RPNCompeitionCode bdonnot/lightsim2grid
More information on this command in the official docker documentation
After this call you can check everything went smoothly with by invoking:
And the results should look like:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 89750964ca55 bdonnot/lightsim2grid "python3" 5 seconds ago Up 4 seconds 80/tcp lightsim_container
NB I insist,
DIR_PATH should be replaced by the path on which you are working, see again the introduction of this
section for more information, in the example above this can look like:
docker run -t -d -p 8888:8888 --name lightsim_container -v /home/MyName/L2RPNCompeitionCode:/L2RPNCompeitionCode -w /L2RPNCompeitionCode bdonnot/lightsim2grid
Once everything is set-up you can execute anything you want on this container. Note that doing so, the execution
of the code will be totally independant of your system. Only the things located in
DIR_PATH will be visible
by your script, only the python package installed in the container will be usable, only the python interpreter
of the containter (python 3.6 at time of writing) will be usable etc.
docker exec lightsim_container python my_script.py
Of course, the "my_script.py" should save its output somewhere on the hard drive.
If you rather want to execute a python REPL (read-eval-print loop), corresponding to the "interactive python interpreter", you can run this command:
docker exec -it lightsim_container python
We also added the possibility to run jupyter notebook from this container. To do so, you can run the command:
docker exec -it lightsim_container jupyter notebook --port=8888 --no-browser --ip='*' --allow-root
More information is provided in the official documentation of docker exec.
Usually, docker run as root on your machine, be careful, you can do irreversible things with it. "A great power comes with a great responsibility".
Also, we recall that we presented a really short introduction to docker and its possibility. We have not implied that this was enough, nor explain (on purpose, to make this short) any of the commands. We strongly encourage you to have a look for yourself.
We want to recall the paragraph
7. Limitation of Liability under which lightsim2grid, and this "tutorial"
Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages.
Once you are done with your experiments, you can stop the docker container:
docker container stop lightsim_container
This will free all the CPU / GPU resources that this container will use. If you want to start it again, for another experiment for example, just use the command:
docker container start lightsim_container
This will allow you to run another batch of
dcoker exec (see
3.2) Execute your code on this container)
without having to re run the container.
If you want to go a step further, you can also delete the container with the command:
docker container rm lightsim_container
This will remove the container, and all your code executed there, the history of commands etc. If you want to use
lightsim2grid with docker again you will have to go through section
3. Run a code on this container all over
And if you also want to remove the image, you can do:
docker rmi bdonnot/lightsim2grid
NB this last command will completely erase the lightsim2grid image from your machine. This means that
if you want to use it again, you will have to download it again (see section
2. Get the lightsim2grid image)
Finally, you can see the official documentation in case you need to uninstall docker completely from your system.
And some official tests, to make sure the solver returns the same results as pandapower are performed in "lightsim2grid/tests"
cd lightsim2grid/tests python -m unittest discover
This tests ensure that the results given by this simulator are consistent with the one given by pandapower when using the Newton-Raphson algorithm, with a single slack bus, without enforcing q limits on the generators etc.
NB to run these tests you need to install grid2op from source otherwise all the test of the LightSim2gridBackend will fail. In order to do so you can do:
git clone https://github.com/rte-france/Grid2Op.git cd Grid2Op pip3 install -U -e . cd ..
Some tests are performed automatically on standard platform each time modifications are made in the lightsim2grid code.
These tests include, for now, compilation on gcc (version 8, 9, 10 and 11) and clang (version 10, 11 and 12).
NB Older version of clang are not tested regularly, but lightsim2grid used to work on these versions.
There are discrepency in the handling of storage units, when the are not asked to produce / consume anything (setpoint is 0.) between pandapower and lightsim2grid only in the case where the storage unit is alone on its bus.
Pandapower does not detect it and the episode can continue. On the other side, lightsim2grid detects it and raise an error because in that case the grid is not connex anymore (which is the desired behaviour).
On the clang compiler (default one on MacOS computer) it is sometime require to downgrade the pybind11 version to 2.6.2 to install the package.
You can downgrade pybind11 with:
python -m pip install -U pybind11==2.6.2