Skip to content

Commit

Permalink
preparing for ptdf
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Dec 1, 2023
1 parent 800573b commit 582b90c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lightsim2grid/gridmodel/from_pypowsybl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions lightsim2grid/securityAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

__all__ = ["SecurityAnalysisCPP", "SecurityAnalysis"]

import os
import warnings
import copy
import numpy as np
from collections.abc import Iterable
Expand Down
10 changes: 10 additions & 0 deletions src/BaseSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class BaseSolver : public BaseConstants
real_type tol
) = 0 ;

virtual Eigen::SparseMatrix<real_type> get_ptdf(){
throw std::runtime_error("Impossible to get the PTDF matrix with this solver type.");
}
virtual Eigen::SparseMatrix<real_type> 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<real_type> 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();

Expand Down
13 changes: 13 additions & 0 deletions src/ChooseSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class ChooseSolver
auto p_solver = get_prt_solver("get_Vm", true);
return p_solver -> get_Vm();
}

Eigen::Ref<const Eigen::SparseMatrix<real_type> > get_J() const
{
check_right_solver("get_J");
Expand Down Expand Up @@ -262,6 +263,18 @@ class ChooseSolver
else throw std::runtime_error("Unknown solver type encountered (get_J)");
}

Eigen::SparseMatrix<real_type> 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<real_type> get_J_python() const{
Eigen::SparseMatrix<real_type> res = get_J();
Expand Down
6 changes: 6 additions & 0 deletions src/DCSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class BaseDCSolver: public BaseSolver
real_type tol
);

virtual Eigen::SparseMatrix<real_type> get_ptdf(); // TODO
virtual Eigen::SparseMatrix<real_type> get_lodf(); // TODO
virtual Eigen::SparseMatrix<real_type> get_bsdf(); // TODO

private:
// no copy allowed
BaseDCSolver( const BaseSolver & ) =delete ;
Expand All @@ -42,6 +46,8 @@ class BaseDCSolver: public BaseSolver
protected:
LinearSolver _linear_solver;
bool need_factorize_;
RealVect Sbus_noslack_;
Eigen::SparseMatrix<real_type> Ybus_noslack_;

};

Expand Down
22 changes: 22 additions & 0 deletions src/DCSolver.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,25 @@ void BaseDCSolver<LinearSolver>::reset(){
_linear_solver.reset();
need_factorize_ = true;
}

template<class LinearSolver>
Eigen::SparseMatrix<real_type> BaseDCSolver<LinearSolver>::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<class LinearSolver>
Eigen::SparseMatrix<real_type> BaseDCSolver<LinearSolver>::get_lodf(){
// TODO
return Ybus_noslack_;

}

template<class LinearSolver>
Eigen::SparseMatrix<real_type> BaseDCSolver<LinearSolver>::get_bsdf(){
// TODO
return Ybus_noslack_;

}
7 changes: 7 additions & 0 deletions src/GridModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,13 @@ CplxVect GridModel::dc_pf(const CplxVect & Vinit,
return res;
}

Eigen::SparseMatrix<real_type> 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
**/
Expand Down
1 change: 1 addition & 0 deletions src/GridModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class GridModel : public DataGeneric
int max_iter, // not used for DC
real_type tol // not used for DC
);
Eigen::SparseMatrix<real_type> get_ptdf();

// ac powerflow
CplxVect ac_pf(const CplxVect & Vinit,
Expand Down

0 comments on commit 582b90c

Please sign in to comment.