Skip to content

Commit

Permalink
now it compiles, need to check tests, and implement logic solver side…
Browse files Browse the repository at this point in the history
… [skip ci]
  • Loading branch information
BDonnot committed Dec 8, 2023
1 parent eba1aab commit c5c85bb
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 42 deletions.
6 changes: 5 additions & 1 deletion src/BaseSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include "BaseSolver.h"
#include "GridModel.h" // needs to be included here because of the forward declaration

void BaseSolver::reset(const SolverControl & solver_control){

void BaseSolver::reset(){
// reset timers
reset_timer();

Expand All @@ -20,8 +21,11 @@ void BaseSolver::reset(const SolverControl & solver_control){
V_ = RealVect(); // voltage angle // TODO solver control: see if I could reuse some of these
nr_iter_ = 0; // number of iteration performs by the algorithm
err_ = ErrorType::NotInitError; //error message:

_solver_control = SolverControl();
}


RealVect BaseSolver::_evaluate_Fx(const Eigen::SparseMatrix<cplx_type> & Ybus,
const CplxVect & V,
const CplxVect & Sbus,
Expand Down
14 changes: 6 additions & 8 deletions src/BaseSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class BaseSolver : public BaseConstants
real_type tol
) = 0 ;

<<<<<<< HEAD
virtual
void reset(const SolverControl & solver_control);
=======
void tell_solver_control(const SolverControl & solver_control){
_solver_control = solver_control;
}
virtual void reset();
virtual RealMat get_ptdf(const Eigen::SparseMatrix<cplx_type> & dcYbus){
throw std::runtime_error("Impossible to get the PTDF matrix with this solver type.");
}
Expand All @@ -111,9 +111,6 @@ class BaseSolver : public BaseConstants
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();
>>>>>>> bd-dev

protected:
virtual void reset_timer(){
Expand Down Expand Up @@ -232,7 +229,8 @@ class BaseSolver : public BaseConstants
double timer_total_nr_;

const GridModel * _gridmodel; // does not have ownership so that's fine (pointer to the base gridmodel, can be used for some powerflow)

SolverControl _solver_control;

private:
// no copy allowed
BaseSolver( const BaseSolver & ) ;
Expand Down
9 changes: 8 additions & 1 deletion src/ChooseSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ class ChooseSolver
#endif

// now switch the union (see https://en.cppreference.com/w/cpp/language/union)
// reset the old solver
reset();

// and assign the right solver
_solver_type = type;
// and now reset the new one
reset();
}

void reset()
Expand Down Expand Up @@ -275,6 +277,11 @@ class ChooseSolver
return res;
}

void tell_solver_control(const SolverControl & solver_control){
auto p_solver = get_prt_solver("get_ptdf", true);
p_solver -> tell_solver_control(solver_control);
}

/** 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: 3 additions & 3 deletions src/DataGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,16 @@ void DataGen::update_slack_weights(Eigen::Ref<Eigen::Array<bool, Eigen::Dynamic,
if(p_mw_(gen_id) > 0.){
// gen is properly connected
if(!gen_slackbus_[gen_id]) solver_control.tell_slack_participate_changed(); // it was not in the slack before, so I need to reset the solver
add_slackbus(gen_id, p_mw_(gen_id));
add_slackbus(gen_id, p_mw_(gen_id), solver_control);

}else{
// gen is now "turned off"
if(gen_slackbus_[gen_id]) solver_control.tell_slack_participate_changed(); // it was in the slack before, so I need to reset the solver
remove_slackbus(gen_id);
remove_slackbus(gen_id, solver_control);
}
}else{
if(gen_slackbus_[gen_id]) solver_control.tell_slack_participate_changed(); // it was in the slack before, I need to reset the solver
remove_slackbus(gen_id);
remove_slackbus(gen_id, solver_control);
}
}
}
Expand Down
31 changes: 21 additions & 10 deletions src/DataGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,29 @@ class DataGen: public DataGeneric
we suppose that the data are correct (ie gen_id in the proper range, and weight > 0.)
This is checked in GridModel, and not at this stage
**/
void add_slackbus(int gen_id, real_type weight){
gen_slackbus_[gen_id] = true;
gen_slack_weight_[gen_id] = weight;
void add_slackbus(int gen_id, real_type weight, SolverControl & solver_control){
// TODO DEBUG MODE
if(weight <= 0.) throw std::runtime_error("DataGen::add_slackbus Cannot assign a negative weight to the slack bus.");
if(!gen_slackbus_[gen_id]) solver_control.tell_slack_participate_changed();
gen_slackbus_[gen_id] = true;
if(gen_slack_weight_[gen_id] != weight) solver_control.tell_slack_weight_changed();
gen_slack_weight_[gen_id] = weight;
}
void remove_slackbus(int gen_id){
void remove_slackbus(int gen_id, SolverControl & solver_control){
if(!gen_slackbus_[gen_id]) solver_control.tell_slack_participate_changed();
gen_slackbus_[gen_id] = false;
gen_slack_weight_[gen_id] = 0.;
}
void remove_all_slackbus(){
const int nb_gen = nb();
SolverControl unused_solver_control;
for(int gen_id = 0; gen_id < nb_gen; ++gen_id)
{
remove_slackbus(gen_id);
remove_slackbus(gen_id, unused_solver_control);
}
}
// returns only the gen_id with the highest p that is connected to this bus !
int assign_slack_bus(int slack_bus_id, const std::vector<real_type> & gen_p_per_bus){
int assign_slack_bus(int slack_bus_id, const std::vector<real_type> & gen_p_per_bus, SolverControl & solver_control){
const int nb_gen = nb();
int res_gen_id = -1;
real_type max_p = -1.;
Expand All @@ -201,7 +205,7 @@ class DataGen: public DataGeneric
if(!status_[gen_id]) continue;
if(bus_id_(gen_id) != slack_bus_id) continue;
const real_type p_mw = p_mw_(gen_id);
add_slackbus(gen_id, p_mw / gen_p_per_bus[slack_bus_id]);
add_slackbus(gen_id, p_mw / gen_p_per_bus[slack_bus_id], solver_control);
if((p_mw > max_p) || (res_gen_id == -1) ){
res_gen_id = gen_id;
max_p = p_mw;
Expand All @@ -224,14 +228,18 @@ class DataGen: public DataGeneric
void turnedoff_no_pv(){turnedoff_gen_pv_=false;} // turned off generators are not pv
void turnedoff_pv(){turnedoff_gen_pv_=true;} // turned off generators are pv
bool get_turnedoff_gen_pv() const {return turnedoff_gen_pv_;}
void update_slack_weights(Eigen::Ref<Eigen::Array<bool, Eigen::Dynamic, Eigen::RowMajor> > could_be_slack, SolverControl & solver_control);
void update_slack_weights(Eigen::Ref<Eigen::Array<bool, Eigen::Dynamic, Eigen::RowMajor> > could_be_slack,
SolverControl & solver_control);

void deactivate(int gen_id, SolverControl & solver_control) {
if (status_[gen_id]){
solver_control.tell_recompute_sbus();
if(voltage_regulator_on_[gen_id]) solver_control.tell_v_changed();
if(!turnedoff_gen_pv_) solver_control.tell_pv_changed();
if(gen_slack_weight_[gen_id]) solver_control.tell_slack_participate_changed();
if(gen_slack_weight_[gen_id]){
solver_control.tell_slack_participate_changed();
solver_control.tell_slack_weight_changed();
}
}
_deactivate(gen_id, status_);
}
Expand All @@ -240,7 +248,10 @@ class DataGen: public DataGeneric
solver_control.tell_recompute_sbus();
if(voltage_regulator_on_[gen_id]) solver_control.tell_v_changed();
if(!turnedoff_gen_pv_) solver_control.tell_pv_changed();
if(gen_slack_weight_[gen_id]) solver_control.tell_slack_participate_changed();
if(gen_slack_weight_[gen_id]){
solver_control.tell_slack_participate_changed();
solver_control.tell_slack_weight_changed();
}
}
_reactivate(gen_id, status_);
}
Expand Down
29 changes: 15 additions & 14 deletions src/GridModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ void GridModel::reset(bool reset_solver, bool reset_ac, bool reset_dc)
_solver.set_gridmodel(this);
_dc_solver.set_gridmodel(this);
}
// std::cout << "GridModel::reset called" << std::endl;
}

CplxVect GridModel::ac_pf(const CplxVect & Vinit,
Expand Down Expand Up @@ -415,15 +414,16 @@ CplxVect GridModel::pre_process_solver(const CplxVect & Vinit,
const SolverControl & solver_control)
{
// TODO get rid of the "is_ac" argument: this info is available in the _solver already
if(is_ac) _solver.reset(solver_control);
else _dc_solver.reset(solver_control);
slack_bus_id_ = generators_.get_slack_bus_id();
if(is_ac) _solver.tell_solver_control(solver_control);
else _dc_solver.tell_solver_control(solver_control);

if (solver_control.has_slack_participate_changed()) slack_bus_id_ = generators_.get_slack_bus_id();
if (solver_control.ybus_change_sparsity_pattern() || solver_control.has_dimension_changed()) init_Ybus(Ybus, id_me_to_solver, id_solver_to_me);
if (solver_control.ybus_change_sparsity_pattern() || solver_control.has_dimension_changed() || solver_control.need_recompute_ybus()) fillYbus(Ybus, is_ac, id_me_to_solver);
if (solver_control.has_dimension_changed()) init_Sbus(Sbus_, id_me_to_solver, id_solver_to_me, slack_bus_id_solver);
fillpv_pq(id_me_to_solver, id_solver_to_me, slack_bus_id_solver, solver_control);
if (solver_control.has_slack_participate_changed() || solver_control.has_pv_changed() || solver_control.has_pq_changed()) fillpv_pq(id_me_to_solver, id_solver_to_me, slack_bus_id_solver, solver_control);

if (solver_control.need_recompute_sbus()){
if (solver_control.has_dimension_changed() || solver_control.need_recompute_sbus() && is_ac){
int nb_bus_total = static_cast<int>(bus_vn_kv_.size());
total_q_min_per_bus_ = RealVect::Constant(nb_bus_total, 0.);
total_q_max_per_bus_ = RealVect::Constant(nb_bus_total, 0.);
Expand Down Expand Up @@ -477,7 +477,7 @@ void GridModel::process_results(bool conv,
// compute the results of the flows, P,Q,V of loads etc.
compute_results(ac);
}
need_reset_ = false;
solver_control_.tell_none_changed();
const CplxVect & res_tmp = ac ? _solver.get_V(): _dc_solver.get_V() ;

// convert back the results to "big" vector
Expand All @@ -487,7 +487,7 @@ void GridModel::process_results(bool conv,
} else {
//powerflow diverge
reset_results();
need_reset_ = true; // in this case, the powerflow diverge, so i need to recompute Ybus next time
// TODO solver control ??? something to do here ?
}
}

Expand Down Expand Up @@ -698,13 +698,14 @@ CplxVect GridModel::dc_pf(const CplxVect & Vinit,

// pre process the data to define a proper jacobian matrix, the proper voltage vector etc.
bool is_ac = false;
bool reset_solver = topo_changed_; // I reset the solver only if the topology change
CplxVect V = pre_process_solver(Vinit, Ybus_dc_,
id_me_to_dc_solver_, id_dc_solver_to_me_, slack_bus_id_dc_solver_,
is_ac, reset_solver);
is_ac, solver_control_);

// start the solver
slack_weights_ = generators_.get_slack_weights(Ybus_dc_.rows(), id_me_to_dc_solver_);
if(solver_control_.has_slack_participate_changed() ||
solver_control_.has_pv_changed() ||
solver_control_.has_slack_weight_changed()) slack_weights_ = generators_.get_slack_weights(Ybus_dc_.rows(), id_me_to_dc_solver_);
conv = _dc_solver.compute_pf(Ybus_dc_, V, dcSbus_, slack_bus_id_dc_solver_, slack_weights_, bus_pv_, bus_pq_, max_iter, tol);

// store results (fase -> because I am in dc mode)
Expand Down Expand Up @@ -761,7 +762,7 @@ void GridModel::add_gen_slackbus(int gen_id, real_type weight){
exc_ << "GridModel::add_gen_slackbus: please enter a valid weight for the slack bus (> 0.)";
throw std::runtime_error(exc_.str());
}
generators_.add_slackbus(gen_id, weight);
generators_.add_slackbus(gen_id, weight, solver_control_);
}

void GridModel::remove_gen_slackbus(int gen_id){
Expand All @@ -781,7 +782,7 @@ void GridModel::remove_gen_slackbus(int gen_id){
exc_ << "Generator with id " << gen_id << " does not exist and can't be the slack bus";
throw std::runtime_error(exc_.str());
}
generators_.remove_slackbus(gen_id);
generators_.remove_slackbus(gen_id, solver_control_);
}

/** GRID2OP SPECIFIC REPRESENTATION **/
Expand Down Expand Up @@ -999,7 +1000,7 @@ std::tuple<int, int> GridModel::assign_slack_to_most_connected(){

// and reset the slack bus
generators_.remove_all_slackbus();
res_gen_id = generators_.assign_slack_bus(res_bus_id, gen_p_per_bus);
res_gen_id = generators_.assign_slack_bus(res_bus_id, gen_p_per_bus, solver_control_);
std::get<1>(res) = res_gen_id;
slack_bus_id_ = std::vector<int>();
slack_weights_ = RealVect();
Expand Down
6 changes: 4 additions & 2 deletions src/GridModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ class GridModel : public DataGeneric
{
(this->*fun_react)(el_id); // eg reactivate_load(load_id);
(this->*fun_change)(el_id, new_bus_backend); // eg change_bus_load(load_id, new_bus_backend);
topo_changed_ = true;
// topo_changed_ = true;
solver_control_.tell_dimension_changed();
}
} else{
if(has_changed(el_pos))
Expand All @@ -714,7 +715,8 @@ class GridModel : public DataGeneric
// bus_status_ is set to "false" in GridModel.update_topo
// and a bus is activated if (and only if) one element is connected to it.
// I must not set `bus_status_[new_bus_backend] = false;` in this case !
topo_changed_ = true;
// topo_changed_ = true;
solver_control_.tell_dimension_changed();
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SolverControl
need_recompute_sbus_(true),
need_recompute_ybus_(true),
v_changed_(true),
slack_weight_changed_(true),
ybus_change_sparsity_pattern_(true)
{};

Expand All @@ -76,6 +77,7 @@ class SolverControl
need_recompute_sbus_ = true;
need_recompute_ybus_ = true;
v_changed_ = true;
slack_weight_changed_ = true;
ybus_change_sparsity_pattern_ = true;
}

Expand All @@ -88,6 +90,7 @@ class SolverControl
need_recompute_sbus_ = false;
need_recompute_ybus_ = false;
v_changed_ = false;
slack_weight_changed_ = false;
ybus_change_sparsity_pattern_ = false;
}

Expand All @@ -109,16 +112,19 @@ class SolverControl
void tell_ybus_change_sparsity_pattern(){ybus_change_sparsity_pattern_ = true;} //should be used after the powerflow as run, so some vectors will not be recomputed if not needed.
// tell at least one generator changed its v setpoint
void tell_v_changed(){v_changed_ = true;}
// at least one generator has changed its slack participation
void tell_slack_weight_changed(){slack_weight_changed_ = true;}

bool has_dimension_changed() const {return change_dimension_;}
bool has_pv_changed() const {return pv_changed_;}
bool has_pq_changed() const {return pq_changed_;}
bool has_tell_slack_participate_changed() const {return slack_participate_changed_;}
bool has_slack_participate_changed() const {return slack_participate_changed_;}
bool need_reset_solver() const {return need_reset_solver_;}
bool need_recompute_sbus() const {return need_recompute_sbus_;}
bool need_recompute_ybus() const {return need_recompute_ybus_;}
bool ybus_change_sparsity_pattern() const {return ybus_change_sparsity_pattern_;}
bool v_changed() const {return v_changed_;}
bool has_slack_weight_changed() const {return slack_weight_changed_;}
bool has_v_changed() const {return v_changed_;}

protected:
bool change_dimension_;
Expand All @@ -129,6 +135,7 @@ class SolverControl
bool need_recompute_sbus_; // some coeff of sbus changed, need to recompute it
bool need_recompute_ybus_; // some coeff of ybus changed, but not its sparsity pattern
bool v_changed_;
bool slack_weight_changed_;
bool ybus_change_sparsity_pattern_; // sparsity pattern of ybus changed (and so are its coeff), or ybus change of dimension
};

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ PYBIND11_MODULE(lightsim2grid_cpp, m)
.def("has_dimension_changed", &SolverControl::has_dimension_changed, "TODO")
.def("has_pv_changed", &SolverControl::has_pv_changed, "TODO")
.def("has_pq_changed", &SolverControl::has_pq_changed, "TODO")
.def("has_tell_slack_participate_changed", &SolverControl::has_tell_slack_participate_changed, "TODO")
.def("has_slack_participate_changed", &SolverControl::has_slack_participate_changed, "TODO")
.def("need_reset_solver", &SolverControl::need_reset_solver, "TODO")
.def("need_recompute_sbus", &SolverControl::need_recompute_sbus, "TODO")
.def("need_recompute_ybus", &SolverControl::need_recompute_ybus, "TODO")
Expand Down

0 comments on commit c5c85bb

Please sign in to comment.