Skip to content

Commit

Permalink
adding names in the gridmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Nov 9, 2023
1 parent ef6b806 commit 6c73b5c
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 69 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log
--------
- [refacto] have a structure in cpp for the buses
- [refacto] have the id_grid_to_solver and id_solver_to_grid etc. directly in the solver and NOT in the gridmodel.
- [refacto] put some method in the DataGeneric as well as some attribute (_status for example)
- support 3w trafo (as modeled in pandapower)
- improve speed by not performing internal checks
(keep check for boundaries and all for python API instead) [see `TODO DEBUG MODE` in c++ code]
Expand All @@ -13,8 +14,6 @@ Change Log
- a mode to do both `Computer` and `SecurityAnalysisCPP`
- use the "multi slack hack" (see issue #50) for SecurityAnalysis or Computer for example
- code `helm` powerflow method
- possibility to read CGMES files
- possibility to read XIIDM files
- interface with gridpack (to enforce q limits for example)
- maybe have a look at suitesparse "sliplu" tools ?
- easier building (get rid of the "make" part)
Expand All @@ -30,8 +29,9 @@ Change Log
these cases.
- [ADDED] sets of methods to extract the main component of a grid and perform powerflow only on this
one.
- [ADDED] possibility to set / retrieve the names of each elements of the grid.
- [IMPROVED] now performing the new grid2op `create_test_suite`
- [IMPROVED] now lightsim2grid properly throw BackendError
- [IMPROVED] now lightsim2grid properly throw `BackendError`

[0.7.5] 2023-10-05
--------------------
Expand Down
20 changes: 15 additions & 5 deletions lightsim2grid/gridmodel/from_pypowsybl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def _aux_get_bus(bus_df, df, conn_key="connected", bus_key="bus_id"):
tmp_bus_id[mask_disco] = df.iloc[first_el_co][bus_key] # assign a "random" bus to disco element
bus_id = bus_df.loc[tmp_bus_id.values]["bus_id"].values
# deactivate the element not on the main component
wrong_component = bus_df.loc[tmp_bus_id.values]["connected_component"].values != 0
mask_disco[wrong_component] = True
# wrong_component = bus_df.loc[tmp_bus_id.values]["connected_component"].values != 0
# mask_disco[wrong_component] = True
# assign bus -1 to disconnected elements
bus_id[mask_disco] = -1
return bus_id , mask_disco.values
Expand All @@ -38,8 +38,11 @@ def init(net : pypo.network,
slack_bus_id: int = None,
sn_mva = 100.,
sort_index=True,
f_hz = 50.):
f_hz = 50.,
only_main_component=True):
model = GridModel()
# model.set_f_hz(f_hz)

# for substation
# network.get_voltage_levels()["substation_id"]
# network.get_substations()
Expand Down Expand Up @@ -84,6 +87,7 @@ def init(net : pypo.network,
for gen_id, is_disco in enumerate(gen_disco):
if is_disco:
model.deactivate_gen(gen_id)
model.set_gen_names(df_gen.index)

# for loads
if sort_index:
Expand All @@ -98,6 +102,7 @@ def init(net : pypo.network,
for load_id, is_disco in enumerate(load_disco):
if is_disco:
model.deactivate_load(load_id)
model.set_load_names(df_load.index)

# for lines
if sort_index:
Expand Down Expand Up @@ -141,6 +146,7 @@ def init(net : pypo.network,
for line_id, (is_or_disc, is_ex_disc) in enumerate(zip(lor_disco, lex_disco)):
if is_or_disc or is_ex_disc:
model.deactivate_powerline(line_id)
model.set_line_names(df_line.index)

# for trafo
if sort_index:
Expand Down Expand Up @@ -176,6 +182,7 @@ def init(net : pypo.network,
for t_id, (is_or_disc, is_ex_disc) in enumerate(zip(tor_disco, tex_disco)):
if is_or_disc or is_ex_disc:
model.deactivate_trafo(t_id)
model.set_trafo_names(df_trafo.index)

# for shunt
if sort_index:
Expand All @@ -192,6 +199,7 @@ def init(net : pypo.network,
for shunt_id, disco in enumerate(sh_disco):
if disco:
model.deactivate_shunt(shunt_id)
model.set_shunt_names(df_trafo.index)

# for hvdc (TODO not tested yet)
df_dc = net.get_hvdc_lines().sort_index()
Expand All @@ -218,6 +226,7 @@ def init(net : pypo.network,
for hvdc_id, (is_or_disc, is_ex_disc) in enumerate(zip(hvdc_from_disco, hvdc_to_disco)):
if is_or_disc or is_ex_disc:
model.deactivate_hvdc(hvdc_id)
model.set_dcline_names(df_sations.index)

# storage units (TODO not tested yet)
if sort_index:
Expand All @@ -232,6 +241,7 @@ def init(net : pypo.network,
for batt_id, disco in enumerate(batt_disco):
if disco:
model.deactivate_storage(batt_id)
model.set_storage_names(df_batt.index)

# TODO dist slack
if gen_slack_id is None and slack_bus_id is None:
Expand Down Expand Up @@ -262,6 +272,6 @@ def init(net : pypo.network,
# raise RuntimeError("Impossible currently to init a grid with tap changers at the moment.")

# and now deactivate all elements and nodes not in the main component
# TODO DC LINE: one side might be in the connected comp and not the other !
model.consider_only_main_component()
if only_main_component:
model.consider_only_main_component()
return model
14 changes: 8 additions & 6 deletions src/DataDCLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ DataDCLine::StateRes DataDCLine::get_state() const
std::vector<real_type> loss_percent(loss_percent_.begin(), loss_percent_.end());
std::vector<real_type> loss_mw(loss_mw_.begin(), loss_mw_.end());
std::vector<bool> status = status_;
DataDCLine::StateRes res(from_gen_.get_state(),
DataDCLine::StateRes res(names_,
from_gen_.get_state(),
to_gen_.get_state(),
loss_percent,
loss_mw,
Expand All @@ -25,11 +26,12 @@ DataDCLine::StateRes DataDCLine::get_state() const

void DataDCLine::set_state(DataDCLine::StateRes & my_state){
reset_results();
from_gen_.set_state(std::get<0>(my_state));
to_gen_.set_state(std::get<1>(my_state));
std::vector<real_type> & loss_percent = std::get<2>(my_state);
std::vector<real_type> & loss_mw = std::get<3>(my_state);
std::vector<bool> & status = std::get<4>(my_state);
names_ = std::get<0>(my_state);
from_gen_.set_state(std::get<1>(my_state));
to_gen_.set_state(std::get<2>(my_state));
std::vector<real_type> & loss_percent = std::get<3>(my_state);
std::vector<real_type> & loss_mw = std::get<4>(my_state);
std::vector<bool> & status = std::get<5>(my_state);
status_ = status;
loss_percent_ = RealVect::Map(&loss_percent[0], loss_percent.size());
loss_mw_ = RealVect::Map(&loss_mw[0], loss_percent.size());
Expand Down
6 changes: 6 additions & 0 deletions src/DataDCLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DataDCLine : public DataGeneric
public:
// members
int id; // id of the dcline
std::string name;
bool connected;
int bus_or_id;
int bus_ex_id;
Expand All @@ -53,6 +54,7 @@ class DataDCLine : public DataGeneric

DCLineInfo(const DataDCLine & r_data_dcline, int my_id):
id(-1),
name(""),
connected(false),
bus_or_id(-1),
bus_ex_id(-1),
Expand All @@ -76,6 +78,9 @@ class DataDCLine : public DataGeneric
if((my_id >= 0) & (my_id < r_data_dcline.nb()))
{
id = my_id;
if(r_data_dcline.names_.size()){
name = r_data_dcline.names_[my_id];
}
loss_pct = r_data_dcline.loss_percent_(my_id);
loss_mw = r_data_dcline.loss_mw_(my_id);

Expand Down Expand Up @@ -108,6 +113,7 @@ class DataDCLine : public DataGeneric

public:
typedef std::tuple<
std::vector<std::string>,
DataGen::StateRes,
DataGen::StateRes,
std::vector<double>, // loss_percent
Expand Down
24 changes: 12 additions & 12 deletions src/DataGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ DataGen::StateRes DataGen::get_state() const
std::vector<bool> status = status_;
std::vector<bool> slack_bus = gen_slackbus_;
std::vector<real_type> slack_weight = gen_slack_weight_;
DataGen::StateRes res(turnedoff_gen_pv_, p_mw, vm_pu, min_q, max_q, bus_id, status, slack_bus, slack_weight);
DataGen::StateRes res(names_, turnedoff_gen_pv_, p_mw, vm_pu, min_q, max_q, bus_id, status, slack_bus, slack_weight);
return res;
}

void DataGen::set_state(DataGen::StateRes & my_state )
void DataGen::set_state(DataGen::StateRes & my_state)
{
reset_results();

turnedoff_gen_pv_ = std::get<0>(my_state);
names_ = std::get<0>(my_state);
turnedoff_gen_pv_ = std::get<1>(my_state);

// the generators themelves
std::vector<real_type> & p_mw = std::get<1>(my_state);
std::vector<real_type> & vm_pu = std::get<2>(my_state);
std::vector<real_type> & min_q = std::get<3>(my_state);
std::vector<real_type> & max_q = std::get<4>(my_state);
std::vector<int> & bus_id = std::get<5>(my_state);
std::vector<bool> & status = std::get<6>(my_state);
std::vector<bool> & slack_bus = std::get<7>(my_state);
std::vector<real_type> & slack_weight = std::get<8>(my_state);
std::vector<real_type> & p_mw = std::get<2>(my_state);
std::vector<real_type> & vm_pu = std::get<3>(my_state);
std::vector<real_type> & min_q = std::get<4>(my_state);
std::vector<real_type> & max_q = std::get<5>(my_state);
std::vector<int> & bus_id = std::get<6>(my_state);
std::vector<bool> & status = std::get<7>(my_state);
std::vector<bool> & slack_bus = std::get<8>(my_state);
std::vector<real_type> & slack_weight = std::get<9>(my_state);
// TODO check sizes

// input data
Expand Down
6 changes: 6 additions & 0 deletions src/DataGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DataGen: public DataGeneric
// members
// TODO add some const here (value should not be changed !) !!!
int id; // id of the generator
std::string name;
bool connected;
int bus_id;
bool is_slack;
Expand All @@ -56,6 +57,7 @@ class DataGen: public DataGeneric

GenInfo(const DataGen & r_data_gen, int my_id):
id(-1),
name(""),
connected(false),
bus_id(-1),
is_slack(false),
Expand All @@ -73,6 +75,9 @@ class DataGen: public DataGeneric
if((my_id >= 0) & (my_id < r_data_gen.nb()))
{
id = my_id;
if(r_data_gen.names_.size()){
name = r_data_gen.names_[my_id];
}
connected = r_data_gen.status_[my_id];
bus_id = r_data_gen.bus_id_[my_id];
is_slack = r_data_gen.gen_slackbus_[my_id];
Expand Down Expand Up @@ -101,6 +106,7 @@ class DataGen: public DataGeneric

public:
typedef std::tuple<
std::vector<std::string>,
bool,
std::vector<real_type>, // p_mw
std::vector<real_type>, // vm_pu_
Expand Down
8 changes: 7 additions & 1 deletion src/DataGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ class DataGeneric : public BaseConstants
virtual void get_graph(std::vector<Eigen::Triplet<real_type> > & res) const {};
virtual void disconnect_if_not_in_main_component(std::vector<bool> & busbar_in_main_component) {};

void set_names(const std::vector<std::string> & names){
names_ = names;
}

/**"define" the destructor for compliance with clang (otherwise lots of warnings)**/
virtual ~DataGeneric() {};

protected:
std::vector<std::string> names_;

protected:
/**
activation / deactivation of elements
Expand Down
18 changes: 9 additions & 9 deletions src/DataLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ DataLine::StateRes DataLine::get_state() const
std::vector<int > branch_from_id(bus_or_id_.begin(), bus_or_id_.end());
std::vector<int > branch_to_id(bus_ex_id_.begin(), bus_ex_id_.end());
std::vector<bool> status = status_;
DataLine::StateRes res(branch_r, branch_x, branch_hor, branch_hex, branch_from_id, branch_to_id, status);
DataLine::StateRes res(names_, branch_r, branch_x, branch_hor, branch_hex, branch_from_id, branch_to_id, status);
return res;
}
void DataLine::set_state(DataLine::StateRes & my_state)
{
reset_results();

std::vector<real_type> & branch_r = std::get<0>(my_state);
std::vector<real_type> & branch_x = std::get<1>(my_state);
std::vector<cplx_type > & branch_h_or = std::get<2>(my_state);
std::vector<cplx_type > & branch_h_ex = std::get<3>(my_state);
std::vector<int> & branch_from_id = std::get<4>(my_state);
std::vector<int> & branch_to_id = std::get<5>(my_state);
std::vector<bool> & status = std::get<6>(my_state);
names_ = std::get<0>(my_state);
std::vector<real_type> & branch_r = std::get<1>(my_state);
std::vector<real_type> & branch_x = std::get<2>(my_state);
std::vector<cplx_type > & branch_h_or = std::get<3>(my_state);
std::vector<cplx_type > & branch_h_ex = std::get<4>(my_state);
std::vector<int> & branch_from_id = std::get<5>(my_state);
std::vector<int> & branch_to_id = std::get<6>(my_state);
std::vector<bool> & status = std::get<7>(my_state);
// TODO check sizes

// now assign the values
Expand Down
6 changes: 6 additions & 0 deletions src/DataLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DataLine : public DataGeneric
public:
// members
int id; // id of the line
std::string name;
bool connected;
int bus_or_id;
int bus_ex_id;
Expand All @@ -61,6 +62,7 @@ class DataLine : public DataGeneric

LineInfo(const DataLine & r_data_line, int my_id):
id(my_id),
name(""),
connected(false),
bus_or_id(-1),
bus_ex_id(-1),
Expand All @@ -84,6 +86,9 @@ class DataLine : public DataGeneric
if((my_id >= 0) & (my_id < r_data_line.nb()))
{
id = my_id;
if(r_data_line.names_.size()){
name = r_data_line.names_[my_id];
}
connected = r_data_line.status_[my_id];
bus_or_id = r_data_line.bus_or_id_.coeff(my_id);
bus_ex_id = r_data_line.bus_ex_id_.coeff(my_id);
Expand Down Expand Up @@ -117,6 +122,7 @@ class DataLine : public DataGeneric

public:
typedef std::tuple<
std::vector<std::string>,
std::vector<real_type>, // branch_r
std::vector<real_type>, // branch_x
std::vector<cplx_type>, // branch_h
Expand Down
12 changes: 6 additions & 6 deletions src/DataLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ DataLoad::StateRes DataLoad::get_state() const
std::vector<real_type> q_mvar(q_mvar_.begin(), q_mvar_.end());
std::vector<int> bus_id(bus_id_.begin(), bus_id_.end());
std::vector<bool> status = status_;
DataLoad::StateRes res(p_mw, q_mvar, bus_id, status);
DataLoad::StateRes res(names_, p_mw, q_mvar, bus_id, status);
return res;
}
void DataLoad::set_state(DataLoad::StateRes & my_state )
{
reset_results();

std::vector<real_type> & p_mw = std::get<0>(my_state);
std::vector<real_type> & q_mvar = std::get<1>(my_state);
std::vector<int> & bus_id = std::get<2>(my_state);
std::vector<bool> & status = std::get<3>(my_state);
names_ = std::get<0>(my_state);
std::vector<real_type> & p_mw = std::get<1>(my_state);
std::vector<real_type> & q_mvar = std::get<2>(my_state);
std::vector<int> & bus_id = std::get<3>(my_state);
std::vector<bool> & status = std::get<4>(my_state);
// TODO check sizes

// input data
Expand Down
6 changes: 6 additions & 0 deletions src/DataLoad.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DataLoad : public DataGeneric
// members
// TODO add some const here (value should not be changed !) !!!
int id; // id of the generator
std::string name;
bool connected;
int bus_id;

Expand All @@ -57,6 +58,7 @@ class DataLoad : public DataGeneric

LoadInfo(const DataLoad & r_data_load, int my_id):
id(-1),
name(""),
connected(false),
bus_id(-1),
target_p_mw(0.),
Expand All @@ -70,6 +72,9 @@ class DataLoad : public DataGeneric
if((my_id >= 0) & (my_id < r_data_load.nb()))
{
id = my_id;
if(r_data_load.names_.size()){
name = r_data_load.names_[my_id];
}
connected = r_data_load.status_[my_id];
bus_id = r_data_load.bus_id_[my_id];

Expand Down Expand Up @@ -112,6 +117,7 @@ class DataLoad : public DataGeneric
// regular implementation
public:
typedef std::tuple<
std::vector<std::string>,
std::vector<real_type>, // p_mw
std::vector<real_type>, // q_mvar
std::vector<int>, // bus_id
Expand Down
Loading

0 comments on commit 6c73b5c

Please sign in to comment.