### Install Configuration Files Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt Installs the generated SuiteSparse configuration and version files to the installation directory. ```cmake install( FILES "${CMAKE_CURRENT_BINARY_DIR}/suitesparse/suitesparse-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/suitesparse/suitesparse-config-version.cmake" DESTINATION ${ConfigPackageLocation} ) ``` -------------------------------- ### Install pybind11 and lightsim2grid (for versions < 0.13) Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/install_from_source.md For lightsim2grid versions prior to 0.13, install pybind11 first, then compile and install the package. ```bash # install pybind11 pip install pybind11 # compile and install the python package pip install -U . ``` -------------------------------- ### Install SuiteSparse Libraries Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/README.md Installs the compiled SuiteSparse libraries and headers to the specified installation prefix. This makes them available for lightsim2grid. ```bash cmake --build . --config Release --target install ``` ```bash cmake --install . --config Release --prefix ..\built ``` -------------------------------- ### Install lightsim2grid from PyPI Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Use this command to install the latest version of lightsim2grid from the Python Package Index. This is the recommended and easiest installation method. ```bash python -m pip install lightsim2grid ``` -------------------------------- ### Install Grid2Op from Source Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Clone the grid2op repository and install it from source to ensure compatibility for running LightSim2grid tests. ```bash git clone https://github.com/Grid2Op/grid2op.git cd Grid2Op pip3 install -U -e . cd .. ``` -------------------------------- ### Install grid2op Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Install grid2op with a version greater than or equal to 1.6.4. This is a prerequisite for using LightSimBackend. ```commandline pip install grid2op>=1.6.4 ``` -------------------------------- ### Start Docker Container with Specific Path Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md An example of the 'docker run' command with a concrete local directory path specified for DIR_PATH. ```bash docker run -t -d -p 8888:8888 --name lightsim_container -v /home/MyName/L2RPNCompeitionCode:/L2RPNCompeitionCode -w /L2RPNCompeitionCode bdonnot/lightsim2grid ``` -------------------------------- ### Start LightSim2Grid Docker Container Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md Starts a Docker container, mapping local directories and setting the working directory. Replace DIR_PATH with your local directory path. ```bash docker run -t -d -p 8888:8888 --name lightsim_container -v DIR_PATH:/L2RPNCompeitionCode -w /L2RPNCompeitionCode bdonnot/lightsim2grid ``` -------------------------------- ### Example Docker Container Status Output Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md This is an example of the expected output after running 'docker ps' to confirm the container is active. ```text CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 89750964ca55 bdonnot/lightsim2grid "python3" 5 seconds ago Up 4 seconds 80/tcp lightsim_container ``` -------------------------------- ### Install lightsim2grid Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/README.md Installs the lightsim2grid package after SuiteSparse has been successfully built and installed. This command assumes you are in the root of the lightsim2grid source tree. ```bash py -m pip install . ``` -------------------------------- ### Install lightsim2grid Python Package Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/install_from_source.md Use this command to compile and install the lightsim2grid Python package from the source directory. ```bash # compile and install the python package pip install -U . ``` -------------------------------- ### Install Grid2Op and Dependencies Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md This command installs the grid2op package with optional dependencies and pybind11. This is equivalent to the Python packages included in the LightSim2Grid Docker image. ```bash pip install -U grid2op[optional] pybind11 ``` -------------------------------- ### Project and Include Directories Setup Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/SuiteSparse/KLU/CMakeLists.txt Defines the KLU project and specifies include paths for SuiteSparse configuration, KLU headers, and its dependencies (AMD, COLAMD, BTF). ```cmake project(KLU) include_directories("${suitesparseconfig_SOURCE_DIR}") include_directories("${KLU_SOURCE_DIR}/Include") include_directories("${AMD_SOURCE_DIR}/Include") include_directories("${COLAMD_SOURCE_DIR}/Include") include_directories("${BTF_SOURCE_DIR}/Include") ``` -------------------------------- ### Install Legacy LightSim2Grid C++ Source: https://github.com/grid2op/lightsim2grid/blob/master/CMakeLists.txt Configures the INSTALL_LEGACY_LIGHTSIM2GRID_CPP option. Defaults to ON for backward compatibility with 'from lightsim2grid_cpp import XXX' imports. ```cmake option(INSTALL_LEGACY_LIGHTSIM2GRID_CPP "Also install lightsim2grid_cpp at site-packages root for legacy 'from lightsim2grid_cpp import XXX' compatibility (deprecated, will default to OFF in a future release)" ON) ``` -------------------------------- ### Initialize and Get Power Flows with TimeSerie Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/time_series.md Demonstrates how to initialize the TimeSerie class and retrieve power, current, and voltage flows for a given scenario. Ensure grid2op and LightSimBackend are imported. ```python from lightsim2grid import TimeSerie import grid2op from lightsim2grid.lightSimBackend import LightSimBackend env_name = ... env = grid2op.make(env_name, backend=LightSimBackend()) time_series = TimeSerie(env) res_p, res_a, res_v = time_series.get_flows(scenario_id=..., seed=...) # we have: # res_p[row_id] will be the active power flows (origin side), on all powerlines corresponding to step "row_id" # res_a[row_id] will be the current flows, on all powerlines corresponding to step "row_id" # res_v[row_id] will be the complex voltage, on all bus of the grid at step "row_id" ``` -------------------------------- ### Basic Contingency Analysis Setup Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/security_analysis.md Initialize and use the ContingencyAnalysis class to simulate power flows after disconnections. Results include active power, current, and complex voltages for each contingency. ```python import grid2op from lightsim2grid import ContingencyAnalysis from lightsim2grid import LightSimBackend env_name = ... env = grid2op.make(env_name, backend=LightSimBackend()) security_analysis = ContingencyAnalysis(env) security_analysis.add_multiple_contingencies(...) # or security_analysis.add_single_contingency(...) res_p, res_a, res_v = security_analysis.get_flows() # in this results, then # res_p[row_id] will be the active power flows (origin side), on all powerlines corresponding to the `row_id` contingency. # res_a[row_id] will be the current flows, on all powerlines corresponding to step "row_id" # res_v[row_id] will be the complex voltage, on all bus of the grid corresponding to the `row_id` contingency. # you can retrieve which contingency is id'ed `row_id` with `security_analysis.contingency_order[row_id]` ``` -------------------------------- ### Use LightSimBackend with grid2op Runner Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/use_with_grid2op.md This example shows how to integrate LightSimBackend with the grid2op Runner for efficient environment execution. The environment is created with LightSimBackend before being passed to the Runner. ```python import grid2op from grid2op.Runner import Runner from grid2op.Agent import RandomAgent from lightsim2grid.LightSimBackend import LightSimBackend env_name = "rte_case14_realistic" # for example, other environments might be usable env = grid2op.make(env_name, backend=LightSimBackend() # this is the only change you have to make! ) NB_EPISODE = 10 # assess the performance for 10 episodes, for example NB_CORE = 2 # do it on 2 cores, for example PATH_SAVE = "agents_log" # and store the results in the "agents_log" folder runner = Runner(**env.get_params_for_runner(), agentClass=RandomAgent) runner.run(nb_episode=NB_EPISODE, nb_process=NB_CORE, path_save=PATH_SAVE) ``` -------------------------------- ### Test KLU Solver Integration Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/README.md Verifies the installation of lightsim2grid and the KLU solver by importing the KLUSolver class. No errors indicate successful integration. ```python from lightsim2grid.solver import KLUSolver ``` -------------------------------- ### Compile lightsim2grid using CMake (Cross-Platform) Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/install_from_source.md This method uses CMake and is recommended for Windows users, though it works on other platforms as well. It involves a multi-step process to generate C++ files, configure the build, and install the package. ```bash cd build_cmake python3 generate_c_files.py mkdir build && cd build cmake -DCMAKE_INSTALL_PREFIX=..built -DCMAKE_BUILD_TYPE=Release .. cmake --build . --config Release cmake --build . --config Release --target install ``` -------------------------------- ### Start Interactive Python REPL in Container Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md Opens an interactive Python read-eval-print loop (REPL) inside the running 'lightsim_container'. ```bash docker exec -it lightsim_container python ``` -------------------------------- ### Start LightSim2Grid Docker Container Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/quickstart.md Use this command to restart a previously stopped LightSim2Grid Docker container. This allows for further execution of commands without needing to recreate the container. ```bash docker container start lightsim_container ``` -------------------------------- ### Initialize LightSimBackend with Custom Parameters Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/lightsimbackend.md Demonstrates how to create a grid2op environment with a customized LightSimBackend. Specify parameters like max_iter, tol, and solver_type during backend initialization. ```python import grid2op import lightsim2grid from lightsim2grid import LightSimBackend env_name = ... env = grid2op.make(env_name, backend=LightSimBackend( max_iter=15, tol=1e-9, solver_type=lightsim2grid.solver.SolverType.KLUSolverSingleSlack, # etc. ) ) ``` -------------------------------- ### Initialize GridModel via LightSimBackend (Recommended) Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/gridmodel.md This is the recommended method for initializing a GridModel. It involves creating a lightsim2grid environment using the LightSimBackend and accessing the grid model from it. ```python from lightsim2grid.gridmodel import init # create a lightsim2grid "gridmodel" env_name = ... # eg. "l2rpn_case14_test" env = grid2op.make(env_name, backend=LightSimBackend()) grid_model = env.backend._grid ``` -------------------------------- ### Initialize Grid2Op Environment with LightSimBackend Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/lightsimbackend.md This snippet shows how to create a grid2op environment using the LightSimBackend. Simply pass `backend=LightSimBackend()` to the `grid2op.make` function. This backend will then be used for powerflow computations. ```python import grid2op from lightsim2grid.LightSimBackend import LightSimBackend from grid2op.Agent import RandomAgent # create an environment env_name = "rte_case14_realistic" # for example, other environments might be usable env = grid2op.make(env_name, backend=LightSimBackend() # this is the only change you have to make! ) # create an agent my_agent = RandomAgent(env.action_space) # proceed as you would any open ai gym loop nb_episode = 10 for _ in range(nb_episde): # you perform in this case 10 different episodes obs = env.reset() reward = env.reward_range[0] done = False while not done: # here you loop on the time steps: at each step your agent receive an observation # takes an action # and the environment computes the next observation that will be used at the next step. act = agent.act(obs, reward, done) obs, reward, done, info = env.step(act) # the `LightSimBackend` will be used to carry out the powerflow computation instead # of the default grid2op `PandaPowerBackend` ``` -------------------------------- ### Install C++ Extension Target Source: https://github.com/grid2op/lightsim2grid/blob/master/CMakeLists.txt Installs the compiled C++ extension target into the Python project's installation directory. This allows importing C++ components from Python. ```cmake install(TARGETS lightsim2grid_cpp DESTINATION ${SKBUILD_PROJECT_NAME}) ``` -------------------------------- ### Install SuiteSparse Project Macro Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt Macro to install a SuiteSparse project's targets and headers. It sets position-independent code for GCC/Clang and configures include directories for installation. ```cmake macro(install_suitesparse_project targetName headersList) ## set position independend code for GCC, Clang static (and shared?) libs if (NOT MINGW AND NOT MSVC) target_compile_options(${targetName} PRIVATE "-fPIC") endif() ## set include dir for install target target_include_directories(${targetName} PUBLIC $ $ ) set_target_properties(${targetName} PROPERTIES PUBLIC_HEADER "${headersList}") install(TARGETS ${targetName} EXPORT SuiteSparseTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION include/suitesparse ) endmacro() ``` -------------------------------- ### Dynamically Modify METIS CMakeLists.txt for Installation Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt When BUILD_METIS is enabled, this snippet reads the METIS CMakeLists.txt, checks if modifications have already been made, and appends necessary install commands for the 'metis' target if not. This is a workaround to ensure proper installation of METIS targets. ```cmake if(BUILD_METIS) ## prepare the installation : ## using metis target here is not possible because this target is added in another branch of the CMake structure ## TRICK: need to dynamically modify the metis CMakeLists.txt file before it going to parsed... ## (very ugly/poor for a metis project get from SCM (git/svn/cvs) but it's works ;) and it doesn't matter if metis was get from .zip) if(EXISTS "${METIS_DIR}/libmetis/CMakeLists.txt") file(READ "${METIS_DIR}/libmetis/CMakeLists.txt" contentFile) string(REGEX MATCH "EXPORT SuiteSparseTargets" alreadyModified ${contentFile}) ## use a string pattern to check if we have to do the modif if(NOT alreadyModified) file(APPEND "${METIS_DIR}/libmetis/CMakeLists.txt" " set_target_properties(metis PROPERTIES PUBLIC_HEADER \"../include/metis.h\") install(TARGETS metis ## this line is also the string pattern to check if the modification had already done EXPORT SuiteSparseTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION include ) " ) endif() endif() add_subdirectory(SuiteSparse/metis-5.1.0) ## important part for building metis from its src files endif(BUILD_METIS) ``` -------------------------------- ### Create grid2op Environment with LightSimBackend Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/use_with_grid2op.md This snippet demonstrates how to create a standard grid2op environment using the LightSimBackend. This is the primary change required for integration. ```python import grid2op from lightsim2grid.LightSimBackend import LightSimBackend from grid2op.Agent import RandomAgent # create an environment env_name = "rte_case14_realistic" # for example, other environments might be usable env = grid2op.make(env_name, backend=LightSimBackend() # this is the only change you have to make! ) # and use this as any grid2op environment, for example # create an agent my_agent = RandomAgent(env.action_space) # proceed as you would any open ai gym loop nb_episode = 10 for _ in range(nb_episde): # you perform in this case 10 different episodes obs = env.reset() reward = env.reward_range[0] done = False while not done: # here you loop on the time steps: at each step your agent receive an observation # takes an action # and the environment computes the next observation that will be used at the next step. act = agent.act(obs, reward, done) obs, reward, done, info = env.step(act) # the `LightSimBackend` will be used to carry out the powerflow computation instead # of the default grid2op `PandaPowerBackend` ``` -------------------------------- ### Install SuiteSparse Targets Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt Installs the exported SuiteSparse targets file, allowing CMake to find SuiteSparse when used by other projects. ```cmake install(EXPORT SuiteSparseTargets FILE SuiteSparse-targets.cmake NAMESPACE SuiteSparse:: DESTINATION ${ConfigPackageLocation} ) ``` -------------------------------- ### Conditional Install for Legacy C++ Extension Source: https://github.com/grid2op/lightsim2grid/blob/master/CMakeLists.txt Conditionally installs the C++ extension to the root directory if the INSTALL_LEGACY_LIGHTSIM2GRID_CPP variable is set. This is for backward compatibility. ```cmake if(INSTALL_LEGACY_LIGHTSIM2GRID_CPP) install(TARGETS lightsim2grid_cpp DESTINATION .) endif() ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/install_from_source.md Use 'uv' to create a virtual environment and activate it. This is recommended for managing dependencies. ```bash uv venv source .venv/bin/activate ``` -------------------------------- ### Set Library Install Directory Suffix Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt Determines and sets the LIB_POSTFIX based on the size of void pointers (32-bit or 64-bit) to ensure libraries are installed in the correct architecture-specific directory. ```cmake if(CMAKE_SIZEOF_VOID_P MATCHES "8") set(SUITESPARSE_LIB_POSTFIX "64") else() set(SUITESPARSE_LIB_POSTFIX "") endif() ## get POSTFIX for lib install dir set(LIB_POSTFIX "${SUITESPARSE_LIB_POSTFIX}" CACHE STRING "suffix for 32/64 inst dir placement") mark_as_advanced(LIB_POSTFIX) ``` -------------------------------- ### Set PATH_CKTSO Environment Variable Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Declare the PATH_CKTSO environment variable to point to a valid CKTSO package installation for compilation. ```bash export PATH_NICSLU=/home/user/Documents/cktso ``` -------------------------------- ### Set PATH_NICSLU Environment Variable Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Declare the PATH_NICSLU environment variable to point to a valid NICSLU package installation for compilation. ```bash export PATH_NICSLU=/home/user/Documents/nicslu/nicslu202103 ``` -------------------------------- ### Set CMAKE_INSTALL_PREFIX on Windows Source: https://github.com/grid2op/lightsim2grid/blob/master/build_cmake/CMakeLists.txt Overrides the CMAKE_INSTALL_PREFIX on Windows if it's not explicitly set, ensuring a consistent installation directory relative to the build directory. ```cmake if(WIN32 AND (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)) set(CMAKE_INSTALL_PREFIX "${${PROJECT_NAME}_BINARY_DIR}/install" CACHE PATH "Prefix prepended to install directories" FORCE) message(STATUS "Setting default CMAKE_INSTALL_PREFIX to: ${CMAKE_INSTALL_PREFIX}") else() # pass user defined install prefix to SuiteSparse message(STATUS "Using user defined CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") endif() ``` -------------------------------- ### Initialize GridModel from Pandapower (Not Recommended) Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/gridmodel.md This snippet shows how to initialize a GridModel from an existing pandapower network. It is not the recommended approach and may issue warnings. ```python from lightsim2grid.gridmodel import init pp_net = ... # any pandapower grid eg. pp_net = pn.case118() lightsim_grid_model = init(pp_net) # some warnings might be issued as well as some warnings ``` -------------------------------- ### Run Local Unit Tests Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Execute local unit tests to ensure the solver returns consistent results with pandapower. This requires installing grid2op from source. ```bash cd lightsim2grid/tests python -m unittest discover ``` -------------------------------- ### Using DC Solvers Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/use_solver.md Shows how to instantiate and use a DC solver for power grid analysis. This method is generally faster than AC powerflow. Ensure necessary Ybus, V0, Sbus, etc. are retrieved beforehand. ```python from lightsim2grid.solver import DCSolver # or any of the names above # retrieve some Ybus, V0, etc. as above dc_solver = DCSolver() converged = dc_solver.solve(Ybus, V0, Sbus, ref, slack_weights, pv, pq, max_it, tol) # process the results as above ``` -------------------------------- ### Initialize LightSimBackend with pypowsybl loader Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/lightsimbackend.md Use this snippet to initialize a grid2op environment with the LightSimBackend, specifying the 'pypowsybl' loader method to load a grid described in the iidm format. ```python import grid2op from lightsim2grid.LightSimBackend import LightSimBackend from grid2op.Agent import RandomAgent # create an environment env_with_iidm_as_the_grid_description = ... env = grid2op.make(env_name, backend=LightSimBackend(loader_method="pypowsybl") ) ``` -------------------------------- ### Clone lightsim2grid Repository and Initialize Submodules Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/install_from_source.md Use this command to download the lightsim2grid source code and its dependencies (SparseSuite and Eigen) using Git. ```bash git clone https://github.com/Grid2Op/lightsim2grid.git cd lightsim2grid # retrieve the code of SparseSuite and Eigen (dependencies, mandatory) git submodule init git submodule update ``` -------------------------------- ### Using AC Solvers with Newton Raphson Source: https://github.com/grid2op/lightsim2grid/blob/master/docs/use_solver.md Demonstrates how to instantiate and use an AC solver based on the Newton Raphson method. Ensure necessary Ybus, V0, Sbus, etc. are retrieved beforehand. ```python from lightsim2grid.solver import KLUSolver # or any of the names above # retrieve some Ybus, V0, etc. as above solver = KLUSolver() converged = solver.solve(Ybus, V0, Sbus, ref, slack_weights, pv, pq, max_it, tol) # process the results as above ``` -------------------------------- ### Profile Code with Perf Source: https://github.com/grid2op/lightsim2grid/blob/master/README.md Profile the code by navigating to the benchmarks directory, recording performance data, and then reporting it. ```command-line cd benchmarks perf record ./test_profile.py perf report ```