diff --git a/lightsim2grid/gridmodel/from_pypowsybl.py b/lightsim2grid/gridmodel/from_pypowsybl.py index 375fa9d..b82a269 100644 --- a/lightsim2grid/gridmodel/from_pypowsybl.py +++ b/lightsim2grid/gridmodel/from_pypowsybl.py @@ -30,7 +30,7 @@ def _aux_get_bus(bus_df, df, conn_key="connected", bus_key="bus_id"): # mask_disco[wrong_component] = True # assign bus -1 to disconnected elements bus_id[mask_disco] = -1 - return bus_id , mask_disco.values + return bus_id, mask_disco.values def init(net : pypo.network, diff --git a/lightsim2grid/securityAnalysis.py b/lightsim2grid/securityAnalysis.py index aa555ab..a0cc724 100644 --- a/lightsim2grid/securityAnalysis.py +++ b/lightsim2grid/securityAnalysis.py @@ -8,8 +8,6 @@ __all__ = ["SecurityAnalysisCPP", "SecurityAnalysis"] -import os -import warnings import copy import numpy as np from collections.abc import Iterable diff --git a/src/BaseSolver.h b/src/BaseSolver.h index 54d8710..8c1eaba 100644 --- a/src/BaseSolver.h +++ b/src/BaseSolver.h @@ -98,6 +98,16 @@ class BaseSolver : public BaseConstants real_type tol ) = 0 ; + virtual Eigen::SparseMatrix get_ptdf(){ + throw std::runtime_error("Impossible to get the PTDF matrix with this solver type."); + } + virtual Eigen::SparseMatrix get_lodf(){ // TODO interface is likely to change + throw std::runtime_error("Impossible to get the LODF matrix with this solver type."); + } + virtual Eigen::SparseMatrix get_bsdf(){ // TODO interface is likely to change + throw std::runtime_error("Impossible to get the BSDF matrix with this solver type."); + } + virtual void reset(); diff --git a/src/ChooseSolver.h b/src/ChooseSolver.h index 57d5e69..ce75188 100644 --- a/src/ChooseSolver.h +++ b/src/ChooseSolver.h @@ -218,6 +218,7 @@ class ChooseSolver auto p_solver = get_prt_solver("get_Vm", true); return p_solver -> get_Vm(); } + Eigen::Ref > get_J() const { check_right_solver("get_J"); @@ -262,6 +263,18 @@ class ChooseSolver else throw std::runtime_error("Unknown solver type encountered (get_J)"); } + Eigen::SparseMatrix get_ptdf(){ + if(_solver_type != SolverType::DC && + _solver_type != SolverType::KLUDC && + _solver_type != SolverType::NICSLUDC && + _solver_type != SolverType::CKTSODC){ + throw std::runtime_error("ChooseSolver::get_ptdf: cannot get ptdf for a solver that is not DC."); + } + auto p_solver = get_prt_solver("get_ptdf", true); + const auto & res = p_solver -> get_ptdf(); + return res; + } + /** apparently i cannot pass a const ref for a sparse matrix in python**/ Eigen::SparseMatrix get_J_python() const{ Eigen::SparseMatrix res = get_J(); diff --git a/src/DCSolver.h b/src/DCSolver.h index 54e1657..e8ee780 100644 --- a/src/DCSolver.h +++ b/src/DCSolver.h @@ -34,6 +34,10 @@ class BaseDCSolver: public BaseSolver real_type tol ); + virtual Eigen::SparseMatrix get_ptdf(); // TODO + virtual Eigen::SparseMatrix get_lodf(); // TODO + virtual Eigen::SparseMatrix get_bsdf(); // TODO + private: // no copy allowed BaseDCSolver( const BaseSolver & ) =delete ; @@ -42,6 +46,8 @@ class BaseDCSolver: public BaseSolver protected: LinearSolver _linear_solver; bool need_factorize_; + RealVect Sbus_noslack_; + Eigen::SparseMatrix Ybus_noslack_; }; diff --git a/src/DCSolver.tpp b/src/DCSolver.tpp index 36611ee..114f76c 100644 --- a/src/DCSolver.tpp +++ b/src/DCSolver.tpp @@ -169,3 +169,25 @@ void BaseDCSolver::reset(){ _linear_solver.reset(); need_factorize_ = true; } + +template +Eigen::SparseMatrix BaseDCSolver::get_ptdf(){ + // TODO + // Bf (nb_branch, nb_bus) : en dc un truc du genre 1 / x / tap for (1..nb_branch, from_bus) + // and -1. / x / tap for (1..nb_branch, to_bus) + return Ybus_noslack_; +} + +template +Eigen::SparseMatrix BaseDCSolver::get_lodf(){ + // TODO + return Ybus_noslack_; + +} + +template +Eigen::SparseMatrix BaseDCSolver::get_bsdf(){ + // TODO + return Ybus_noslack_; + +} diff --git a/src/GridModel.cpp b/src/GridModel.cpp index e8fbc75..0c13b70 100644 --- a/src/GridModel.cpp +++ b/src/GridModel.cpp @@ -720,6 +720,13 @@ CplxVect GridModel::dc_pf(const CplxVect & Vinit, return res; } +Eigen::SparseMatrix GridModel::get_ptdf(){ + if(Ybus_dc_.size() == 0){ + throw std::runtime_error("Cannot get the ptdf without having first computed a dc powerflow."); + } + return _dc_solver.get_ptdf(); +} + /** Retrieve the number of connected buses **/ diff --git a/src/GridModel.h b/src/GridModel.h index a4d31b8..4df9ee1 100644 --- a/src/GridModel.h +++ b/src/GridModel.h @@ -267,6 +267,7 @@ class GridModel : public DataGeneric int max_iter, // not used for DC real_type tol // not used for DC ); + Eigen::SparseMatrix get_ptdf(); // ac powerflow CplxVect ac_pf(const CplxVect & Vinit,