diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c880b94..025c310 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -39,6 +39,9 @@ Change Log - [ADDED] computation of PTPF (Power Transfer Distribution Factor) is now possible - [IMPROVED] now performing the new grid2op `create_test_suite` - [IMPROVED] now lightsim2grid properly throw `BackendError` +- [IMPROVED] clean ce cpp side by refactoring: making clearer the difference (linear) solver + vs powerflow algorithm and move same type of files in the same directory. This change + does not really affect python side at the moment (but will in future versions) [0.7.5] 2023-10-05 -------------------- diff --git a/docs/benchmarks.rst b/docs/benchmarks.rst index a49c428..4ad4aa2 100644 --- a/docs/benchmarks.rst +++ b/docs/benchmarks.rst @@ -39,7 +39,8 @@ To run the benchmark `cd` in the [benchmark](./benchmarks) folder and type: (we remind that these simulations correspond to simulation on one core of the CPU. Of course it is possible to make use of all the available cores, which would increase the number of steps that can be performed) -We compare up to 19 different solvers: +We compare up to 19 different "solvers" (combination of "linear solver used" (*eg* Eigen, KLU, CKTSO, NICSLU) +and powerflow algorithm (*eg* "Newton Raphson", or "Fast Decoupled")): - **PP**: PandaPowerBackend (default grid2op backend) which is the reference in our benchmarks (uses the numba acceleration). It is our reference solver. @@ -102,16 +103,24 @@ Computation time In this first subsection we compare the computation times: - **grid2op speed** from a grid2op point of view - (this include the time to compute the powerflow, plus the time to modify the powergrid plus the - time to read back the data once the powerflow has run plus the time to update the environment and - the observations etc.). It is reported in "iteration per second" (`it/s`) and represents the number of grid2op "step" + (this include the time to compute the powerflow, plus the time to modify + the powergrid plus the + time to read back the data once the powerflow has run plus the time to update + the environment and + the observations etc.). It is reported in "iteration per second" (`it/s`) and + represents the number of grid2op "step" that can be computed per second. -- **grid2op 'backend.runpf' time** corresponds to the time the solver take to perform a powerflow - as seen from grid2op (counting the resolution time and some time to check the validity of the results but - not the time to update the grid nor the grid2op environment), for lightsim2grid it includes the time to read back the data +- **grid2op 'backend.runpf' time** corresponds to the time the solver take + to perform a powerflow + as seen from grid2op (counting the resolution time and some time to check + the validity of the results but + not the time to update the grid nor the grid2op environment), for lightsim2grid + it includes the time to read back the data from c++ to python. It is reported in milli seconds (ms). -- **solver powerflow time** corresponds only to the time spent in the solver itself. It does not take into - account any of the checking, nor the transfer of the data python side etc. It is reported in milli seconds (ms) as well. +- **solver powerflow time** corresponds only to the time spent in the solver + itself. It does not take into + account any of the checking, nor the transfer of the data python side etc. + It is reported in milli seconds (ms) as well. There are two major differences between **grid2op 'backend.runpf' time** and **solver powerflow time**. In **grid2op 'backend.runpf' time** the time to initialize the solver (usually with the DC approximation) is counted (it is not in **solver powerflow time**). Secondly, diff --git a/setup.py b/setup.py index 9a0f4f0..8ddbbf5 100644 --- a/setup.py +++ b/setup.py @@ -95,7 +95,7 @@ "be available, which is maybe ~30% slower than \"KLU\". If you are using grid2op there " "will still be a huge benefit.") -INCLUDE = INCLUDE_suitesparse +INCLUDE = ["src"] + INCLUDE_suitesparse # now add the Eigen library (header only) eigen_path = os.path.abspath(".") @@ -137,31 +137,31 @@ f"-DVERSION_MEDIUM={VERSION_MEDIUM}", f"-DVERSION_MINOR={VERSION_MINOR}"] src_files = ['src/main.cpp', + "src/powerflow_algorithm/GaussSeidelAlgo.cpp", + "src/powerflow_algorithm/GaussSeidelSynchAlgo.cpp", + "src/powerflow_algorithm/BaseAlgo.cpp", + "src/linear_solvers/SparseLUSolver.cpp", "src/help_fun_msg.cpp", - "src/SparseLUSolver.cpp", "src/BaseConstants.cpp", "src/GridModel.cpp", - "src/DataConverter.cpp", - "src/DataLine.cpp", - "src/DataGeneric.cpp", - "src/DataShunt.cpp", - "src/DataTrafo.cpp", - "src/DataLoad.cpp", - "src/DataGen.cpp", - "src/DataSGen.cpp", - "src/DataDCLine.cpp", "src/ChooseSolver.cpp", - "src/GaussSeidelSolver.cpp", - "src/GaussSeidelSynchSolver.cpp", - "src/BaseSolver.cpp", "src/BaseMultiplePowerflow.cpp", "src/Computers.cpp", "src/SecurityAnalysis.cpp", "src/Solvers.cpp", - "src/Utils.cpp"] + "src/Utils.cpp", + "src/DataConverter.cpp", + "src/element_container/LineContainer.cpp", + "src/element_container/GenericContainer.cpp", + "src/element_container/ShuntContainer.cpp", + "src/element_container/TrafoContainer.cpp", + "src/element_container/LoadContainer.cpp", + "src/element_container/GeneratorContainer.cpp", + "src/element_container/SGenContainer.cpp", + "src/element_container/DCLineContainer.cpp"] if KLU_SOLVER_AVAILABLE: - src_files.append("src/KLUSolver.cpp") + src_files.append("src/linear_solvers/KLUSolver.cpp") extra_compile_args_tmp.append("-DKLU_SOLVER_AVAILABLE") print("INFO: Using KLU package") @@ -208,7 +208,7 @@ if include_nicslu and libnicslu_path is not None: LIBS.append(os.path.join(path_nicslu, libnicslu_path)) include_dirs.append(os.path.join(path_nicslu, "include")) - src_files.append("src/NICSLUSolver.cpp") + src_files.append("src/linear_solvers/NICSLUSolver.cpp") extra_compile_args.append("-DNICSLU_SOLVER_AVAILABLE") print("INFO: Using NICSLU package") @@ -255,7 +255,7 @@ if include_cktso and libcktso_path is not None: LIBS.append(os.path.join(path_cktso, libcktso_path)) include_dirs.append(os.path.join(path_cktso, "include")) - src_files.append("src/CKTSOSolver.cpp") + src_files.append("src/linear_solvers/CKTSOSolver.cpp") extra_compile_args.append("-DCKTSO_SOLVER_AVAILABLE") print("INFO: Using CKTSO package") diff --git a/src/BaseMultiplePowerflow.cpp b/src/BaseMultiplePowerflow.cpp index 6793869..48aac80 100644 --- a/src/BaseMultiplePowerflow.cpp +++ b/src/BaseMultiplePowerflow.cpp @@ -33,8 +33,8 @@ bool BaseMultiplePowerflow::compute_one_powerflow(const Eigen::SparseMatrix reset(); } - // benefit from dynamic stuff and inheritance by having a method that returns a BaseSolver * + // benefit from dynamic stuff and inheritance by having a method that returns a BaseAlgo * bool compute_pf(const Eigen::SparseMatrix & Ybus, // size (nb_bus, nb_bus) CplxVect & V, // size nb_bus const CplxVect & Sbus, // size nb_bus @@ -388,9 +385,9 @@ class ChooseSolver /** returns a pointer to the current solver used **/ - const BaseSolver * get_prt_solver(const std::string & error_msg, bool check_right_solver_=true) const { + const BaseAlgo * get_prt_solver(const std::string & error_msg, bool check_right_solver_=true) const { if (check_right_solver_) check_right_solver(error_msg); - const BaseSolver * res; + const BaseAlgo * res; if(_solver_type == SolverType::SparseLU){res = &_solver_lu;} else if(_solver_type == SolverType::SparseLUSingleSlack){res = &_solver_lu_single;} else if(_solver_type == SolverType::DC){res = &_solver_dc;} @@ -422,9 +419,9 @@ class ChooseSolver else throw std::runtime_error("Unknown solver type encountered (ChooseSolver get_prt_solver const)"); return res; } - BaseSolver * get_prt_solver(const std::string & error_msg, bool check_right_solver_=true) { + BaseAlgo * get_prt_solver(const std::string & error_msg, bool check_right_solver_=true) { if (check_right_solver_) check_right_solver(error_msg); - BaseSolver * res; + BaseAlgo * res; if(_solver_type == SolverType::SparseLU){res = &_solver_lu;} else if(_solver_type == SolverType::SparseLUSingleSlack){res = &_solver_lu_single;} else if(_solver_type == SolverType::DC){res = &_solver_dc;} @@ -464,8 +461,8 @@ class ChooseSolver // TODO have a way to use Union here https://en.cppreference.com/w/cpp/language/union SparseLUSolver _solver_lu; SparseLUSolverSingleSlack _solver_lu_single; - GaussSeidelSolver _solver_gaussseidel; - GaussSeidelSynchSolver _solver_gaussseidelsynch; + GaussSeidelAlgo _solver_gaussseidel; + GaussSeidelSynchAlgo _solver_gaussseidelsynch; DCSolver _solver_dc; FDPF_XB_SparseLUSolver _solver_fdpf_xb_lu; FDPF_BX_SparseLUSolver _solver_fdpf_bx_lu; diff --git a/src/GridModel.cpp b/src/GridModel.cpp index 85307d3..5f4579a 100644 --- a/src/GridModel.cpp +++ b/src/GridModel.cpp @@ -146,21 +146,21 @@ void GridModel::set_state(GridModel::StateRes & my_state) std::vector & bus_status = std::get<7>(my_state); // powerlines - DataLine::StateRes & state_lines = std::get<8>(my_state); + LineContainer::StateRes & state_lines = std::get<8>(my_state); // shunts - DataShunt::StateRes & state_shunts = std::get<9>(my_state); + ShuntContainer::StateRes & state_shunts = std::get<9>(my_state); // trafos - DataTrafo::StateRes & state_trafos = std::get<10>(my_state); + TrafoContainer::StateRes & state_trafos = std::get<10>(my_state); // generators - DataGen::StateRes & state_gens = std::get<11>(my_state); + GeneratorContainer::StateRes & state_gens = std::get<11>(my_state); // loads - DataLoad::StateRes & state_loads = std::get<12>(my_state); + LoadContainer::StateRes & state_loads = std::get<12>(my_state); // static gen - DataSGen::StateRes & state_sgens= std::get<13>(my_state); + SGenContainer::StateRes & state_sgens= std::get<13>(my_state); // storage units - DataLoad::StateRes & state_storages = std::get<14>(my_state); + LoadContainer::StateRes & state_storages = std::get<14>(my_state); // dc lines - DataDCLine::StateRes & state_dc_lines = std::get<15>(my_state); + DCLineContainer::StateRes & state_dc_lines = std::get<15>(my_state); // assign it to this instance @@ -338,7 +338,7 @@ CplxVect GridModel::ac_pf(const CplxVect & Vinit, }; void GridModel::check_solution_q_values_onegen(CplxVect & res, - const DataGen::GenInfo& gen, + const GeneratorContainer::GenInfo& gen, bool check_q_limits) const{ if(check_q_limits) { @@ -626,8 +626,6 @@ void GridModel::init_slack_bus(const CplxVect & Sbus, const std::vector& id_solver_to_me, const Eigen::VectorXi & slack_bus_id_me, Eigen::VectorXi & slack_bus_id_solver){ - - const int nb_bus = static_cast(id_solver_to_me.size()); // slack_bus_id_solver = Eigen::VectorXi::Zero(slack_bus_id_.size()); slack_bus_id_solver = Eigen::VectorXi::Constant(slack_bus_id_me.size(), _deactivated_bus_id); diff --git a/src/GridModel.h b/src/GridModel.h index 19717ba..eee6cc7 100644 --- a/src/GridModel.h +++ b/src/GridModel.h @@ -27,14 +27,14 @@ #include "Eigen/SparseLU" // import data classes -#include "DataGeneric.h" -#include "DataLine.h" -#include "DataShunt.h" -#include "DataTrafo.h" -#include "DataLoad.h" -#include "DataGen.h" -#include "DataSGen.h" -#include "DataDCLine.h" +#include "element_container/GenericContainer.h" +#include "element_container/LineContainer.h" +#include "element_container/ShuntContainer.h" +#include "element_container/TrafoContainer.h" +#include "element_container/LoadContainer.h" +#include "element_container/GeneratorContainer.h" +#include "element_container/SGenContainer.h" +#include "element_container/DCLineContainer.h" // import newton raphson solvers using different linear algebra solvers #include "ChooseSolver.h" @@ -42,7 +42,7 @@ // enum class SolverType; //TODO implement a BFS check to make sure the Ymatrix is "connected" [one single component] -class GridModel : public DataGeneric +class GridModel : public GenericContainer { public: typedef std::tuple< @@ -55,21 +55,21 @@ class GridModel : public DataGeneric std::vector, // bus_vn_kv std::vector, // bus_status // powerlines - DataLine::StateRes , + LineContainer::StateRes , // shunts - DataShunt::StateRes, + ShuntContainer::StateRes, // trafos - DataTrafo::StateRes, + TrafoContainer::StateRes, // gens - DataGen::StateRes, + GeneratorContainer::StateRes, // loads - DataLoad::StateRes, + LoadContainer::StateRes, // static generators - DataSGen::StateRes, + SGenContainer::StateRes, // storage units - DataLoad::StateRes, + LoadContainer::StateRes, //dc lines - DataDCLine::StateRes + DCLineContainer::StateRes > StateRes; GridModel(): @@ -101,7 +101,7 @@ class GridModel : public DataGeneric const std::vector & id_dc_solver_to_me() const {return id_dc_solver_to_me_;} // retrieve the underlying data (raw class) - const DataGen & get_generators_as_data() const {return generators_;} + const GeneratorContainer & get_generators_as_data() const {return generators_;} void turnedoff_no_pv(){generators_.turnedoff_no_pv();} // turned off generators are not pv void turnedoff_pv(){generators_.turnedoff_pv();} // turned off generators are pv bool get_turnedoff_gen_pv() {return generators_.get_turnedoff_gen_pv();} @@ -109,11 +109,11 @@ class GridModel : public DataGeneric generators_.update_slack_weights(could_be_slack, solver_control_); } - const DataSGen & get_static_generators_as_data() const {return sgens_;} - const DataLoad & get_loads_as_data() const {return loads_;} - const DataLine & get_powerlines_as_data() const {return powerlines_;} - const DataTrafo & get_trafos_as_data() const {return trafos_;} - const DataDCLine & get_dclines_as_data() const {return dc_lines_;} + const SGenContainer & get_static_generators_as_data() const {return sgens_;} + const LoadContainer & get_loads_as_data() const {return loads_;} + const LineContainer & get_powerlines_as_data() const {return powerlines_;} + const TrafoContainer & get_trafos_as_data() const {return trafos_;} + const DCLineContainer & get_dclines_as_data() const {return dc_lines_;} Eigen::Ref get_bus_vn_kv() const {return bus_vn_kv_;} std::tuple assign_slack_to_most_connected(); void consider_only_main_component(); @@ -271,7 +271,7 @@ class GridModel : public DataGeneric void tell_recompute_sbus(){solver_control_.tell_recompute_sbus();} //should be used after the powerflow as run, so some vectors will not be recomputed if not needed. void tell_solver_need_reset(){solver_control_.tell_solver_need_reset();} //should be used after the powerflow as run, so some vectors will not be recomputed if not needed. void tell_ybus_change_sparsity_pattern(){solver_control_.tell_ybus_change_sparsity_pattern();} //should be used after the powerflow as run, so some vectors will not be recomputed if not needed. - const SolverControl & get_solver_control() const { return solver_control_;} + const SolverControl & get_solver_control() const {return solver_control_;} // dc powerflow CplxVect dc_pf(const CplxVect & Vinit, @@ -317,14 +317,14 @@ class GridModel : public DataGeneric Eigen::Index nb_trafo() const {return trafos_.nb();} // read only data accessor - const DataLine & get_lines() const {return powerlines_;} - const DataDCLine & get_dclines() const {return dc_lines_;} - const DataTrafo & get_trafos() const {return trafos_;} - const DataGen & get_generators() const {return generators_;} - const DataLoad & get_loads() const {return loads_;} - const DataLoad & get_storages() const {return storages_;} - const DataSGen & get_static_generators() const {return sgens_;} - const DataShunt & get_shunts() const {return shunts_;} + const LineContainer & get_lines() const {return powerlines_;} + const DCLineContainer & get_dclines() const {return dc_lines_;} + const TrafoContainer & get_trafos() const {return trafos_;} + const GeneratorContainer & get_generators() const {return generators_;} + const LoadContainer & get_loads() const {return loads_;} + const LoadContainer & get_storages() const {return storages_;} + const SGenContainer & get_static_generators() const {return sgens_;} + const ShuntContainer & get_shunts() const {return shunts_;} const std::vector & get_bus_status() const {return bus_status_;} void set_line_names(const std::vector & names){ @@ -740,7 +740,7 @@ class GridModel : public DataGeneric int size); void check_solution_q_values( CplxVect & res, bool check_q_limits) const; - void check_solution_q_values_onegen(CplxVect & res, const DataGen::GenInfo& gen, bool check_q_limits) const; + void check_solution_q_values_onegen(CplxVect & res, const GeneratorContainer::GenInfo& gen, bool check_q_limits) const; protected: // memory for the import @@ -776,35 +776,35 @@ class GridModel : public DataGeneric std::vector id_dc_solver_to_me_; // 2. powerline - DataLine powerlines_; + LineContainer powerlines_; // 3. shunt - DataShunt shunts_; + ShuntContainer shunts_; // 4. transformers // have the r, x, h and ratio // ratio is computed from the tap, so maybe store tap num and tap_step_pct - DataTrafo trafos_; + TrafoContainer trafos_; // 5. generators RealVect total_q_min_per_bus_; RealVect total_q_max_per_bus_; Eigen::VectorXi total_gen_per_bus_; - DataGen generators_; + GeneratorContainer generators_; // 6. loads - DataLoad loads_; + LoadContainer loads_; - // 6. static generators (P,Q generators) - DataSGen sgens_; + // 7. static generators (P,Q generators) + SGenContainer sgens_; - // 7. storage units - DataLoad storages_; + // 8. storage units + LoadContainer storages_; - // hvdc - DataDCLine dc_lines_; + // 9. hvdc + DCLineContainer dc_lines_; - // 8. slack bus + // 10. slack bus // std::vector slack_bus_id_; Eigen::VectorXi slack_bus_id_ac_me_; // slack bus id, gridmodel number Eigen::VectorXi slack_bus_id_ac_solver_; // slack bus id, solver number diff --git a/src/SecurityAnalysis.cpp b/src/SecurityAnalysis.cpp index f4d5c9a..ccab406 100644 --- a/src/SecurityAnalysis.cpp +++ b/src/SecurityAnalysis.cpp @@ -7,6 +7,7 @@ // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. #include "SecurityAnalysis.h" + #include #include /* isfinite */ @@ -89,7 +90,7 @@ void SecurityAnalysis::init_li_coeffs(bool ac_solver_used){ } } - if(bus_1_id != DataGeneric::_deactivated_bus_id && bus_2_id != DataGeneric::_deactivated_bus_id) + if(bus_1_id != GenericContainer::_deactivated_bus_id && bus_2_id != GenericContainer::_deactivated_bus_id) { // element is connected this_cont_coeffs.push_back({bus_1_id, bus_1_id, y_ff}); diff --git a/src/Solvers.cpp b/src/Solvers.cpp index eed71c9..9cd8060 100644 --- a/src/Solvers.cpp +++ b/src/Solvers.cpp @@ -13,7 +13,7 @@ // this is why i need to define them here for every specialization. template -void BaseFDPFSolver::fillBp_Bpp(Eigen::SparseMatrix & Bp, Eigen::SparseMatrix & Bpp) const +void BaseFDPFAlgo::fillBp_Bpp(Eigen::SparseMatrix & Bp, Eigen::SparseMatrix & Bpp) const { _gridmodel->fillBp_Bpp(Bp, Bpp, XB_BX); } diff --git a/src/Solvers.h b/src/Solvers.h index 4adaac8..705be7e 100644 --- a/src/Solvers.h +++ b/src/Solvers.h @@ -6,36 +6,39 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "BaseNRSolver.h" -#include "BaseNRSolverSingleSlack.h" -#include "DCSolver.h" -#include "BaseFDPFSolver.h" -#include "SparseLUSolver.h" -#include "KLUSolver.h" -#include "NICSLUSolver.h" -#include "CKTSOSolver.h" +#include "powerflow_algorithm/BaseDCAlgo.h" +#include "powerflow_algorithm/BaseNRAlgo.h" +#include "powerflow_algorithm/BaseNRSingleSlackAlgo.h" +#include "powerflow_algorithm/BaseFDPFAlgo.h" +#include "powerflow_algorithm/GaussSeidelSynchAlgo.h" +#include "powerflow_algorithm/GaussSeidelAlgo.h" + +#include "linear_solvers/SparseLUSolver.h" +#include "linear_solvers/KLUSolver.h" +#include "linear_solvers/NICSLUSolver.h" +#include "linear_solvers/CKTSOSolver.h" /** Solver based on Newton Raphson, using the SparseLU decomposition of Eigen**/ -typedef BaseNRSolver SparseLUSolver; +typedef BaseNRAlgo SparseLUSolver; /** Solver based on Newton Raphson, using the SparseLU decomposition of Eigen, do not consider multiple slack bus**/ -typedef BaseNRSolverSingleSlack SparseLUSolverSingleSlack; +typedef BaseNRSingleSlackAlgo SparseLUSolverSingleSlack; /** Solver based on Newton Raphson, using the SparseLU decomposition of Eigen, only suitable for the DC approximation**/ -typedef BaseDCSolver DCSolver; +typedef BaseDCAlgo DCSolver; /** Solver based on Fast Decoupled, using the SparseLU decomposition of Eigen**/ -typedef BaseFDPFSolver FDPF_XB_SparseLUSolver; -typedef BaseFDPFSolver FDPF_BX_SparseLUSolver; +typedef BaseFDPFAlgo FDPF_XB_SparseLUSolver; +typedef BaseFDPFAlgo FDPF_BX_SparseLUSolver; #ifdef KLU_SOLVER_AVAILABLE /** Solver based on Newton Raphson, using the KLU linear solver**/ - typedef BaseNRSolver KLUSolver; + typedef BaseNRAlgo KLUSolver; /** Solver based on Newton Raphson, using the KLU linear solver, do not consider multiple slack bus**/ - typedef BaseNRSolverSingleSlack KLUSolverSingleSlack; + typedef BaseNRSingleSlackAlgo KLUSolverSingleSlack; /** Solver based on Newton Raphson, using the KLU linear solver, only suitable for the DC approximation**/ - typedef BaseDCSolver KLUDCSolver; + typedef BaseDCAlgo KLUDCSolver; /** Solver based on Fast Decoupled, using the KLU linear solver**/ - typedef BaseFDPFSolver FDPF_XB_KLUSolver; - typedef BaseFDPFSolver FDPF_BX_KLUSolver; + typedef BaseFDPFAlgo FDPF_XB_KLUSolver; + typedef BaseFDPFAlgo FDPF_BX_KLUSolver; #elif defined(_READ_THE_DOCS) // hack to display accurately the doc in read the doc even if the models are not compiled /** Solver based on Newton Raphson, using the KLU linear solver**/ @@ -51,14 +54,14 @@ typedef BaseFDPFSolver FDPF_BX_SparseLUSol #ifdef NICSLU_SOLVER_AVAILABLE /** Solver based on Newton Raphson, using the NICSLU linear solver (needs a specific license)**/ - typedef BaseNRSolver NICSLUSolver; + typedef BaseNRAlgo NICSLUSolver; /** Solver based on Newton Raphson, using the NICSLU linear solver (needs a specific license), do not consider multiple slack bus**/ - typedef BaseNRSolverSingleSlack NICSLUSolverSingleSlack; + typedef BaseNRSingleSlackAlgo NICSLUSolverSingleSlack; /** Solver based on Newton Raphson, using the NICSLU linear solver (needs a specific license), only suitable for the DC approximation**/ - typedef BaseDCSolver NICSLUDCSolver; + typedef BaseDCAlgo NICSLUDCSolver; /** Solver based on Fast Decoupled, using the NICSLU linear solver (needs a specific license)**/ - typedef BaseFDPFSolver FDPF_XB_NICSLUSolver; - typedef BaseFDPFSolver FDPF_BX_NICSLUSolver; + typedef BaseFDPFAlgo FDPF_XB_NICSLUSolver; + typedef BaseFDPFAlgo FDPF_BX_NICSLUSolver; #elif defined(_READ_THE_DOCS) // hack to display accurately the doc in read the doc even if the models are not compiled /** Solver based on Newton Raphson, using the NICSLU linear solver (needs a specific license)**/ @@ -74,14 +77,14 @@ typedef BaseFDPFSolver FDPF_BX_SparseLUSol #ifdef CKTSO_SOLVER_AVAILABLE /** Solver based on Newton Raphson, using the CKTSO linear solver (needs a specific license)**/ - typedef BaseNRSolver CKTSOSolver; + typedef BaseNRAlgo CKTSOSolver; /** Solver based on Newton Raphson, using the CKTSO linear solver (needs a specific license), do not consider multiple slack bus**/ - typedef BaseNRSolverSingleSlack CKTSOSolverSingleSlack; + typedef BaseNRSingleSlackAlgo CKTSOSolverSingleSlack; /** Solver based on Newton Raphson, using the CKTSO linear solver (needs a specific license), only suitable for the DC approximation**/ - typedef BaseDCSolver CKTSODCSolver; + typedef BaseDCAlgo CKTSODCSolver; /** Solver based on Fast Decoupled, using the CKTSO linear solver (needs a specific license)**/ - typedef BaseFDPFSolver FDPF_XB_CKTSOSolver; - typedef BaseFDPFSolver FDPF_BX_CKTSOSolver; + typedef BaseFDPFAlgo FDPF_XB_CKTSOSolver; + typedef BaseFDPFAlgo FDPF_BX_CKTSOSolver; #elif defined(_READ_THE_DOCS) // hack to display accurately the doc in read the doc even if the models are not compiled /** Solver based on Newton Raphson, using the CKTSO linear solver (needs a specific license)**/ diff --git a/src/DataDCLine.cpp b/src/element_container/DCLineContainer.cpp similarity index 88% rename from src/DataDCLine.cpp rename to src/element_container/DCLineContainer.cpp index c74301b..716229b 100644 --- a/src/DataDCLine.cpp +++ b/src/element_container/DCLineContainer.cpp @@ -6,16 +6,17 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataDCLine.h" +#include "DCLineContainer.h" + #include #include -DataDCLine::StateRes DataDCLine::get_state() const +DCLineContainer::StateRes DCLineContainer::get_state() const { std::vector loss_percent(loss_percent_.begin(), loss_percent_.end()); std::vector loss_mw(loss_mw_.begin(), loss_mw_.end()); std::vector status = status_; - DataDCLine::StateRes res(names_, + DCLineContainer::StateRes res(names_, from_gen_.get_state(), to_gen_.get_state(), loss_percent, @@ -24,7 +25,7 @@ DataDCLine::StateRes DataDCLine::get_state() const return res; } -void DataDCLine::set_state(DataDCLine::StateRes & my_state){ +void DCLineContainer::set_state(DCLineContainer::StateRes & my_state){ reset_results(); names_ = std::get<0>(my_state); from_gen_.set_state(std::get<1>(my_state)); @@ -37,7 +38,7 @@ void DataDCLine::set_state(DataDCLine::StateRes & my_state){ loss_mw_ = RealVect::Map(&loss_mw[0], loss_percent.size()); } -void DataDCLine::init(const Eigen::VectorXi & branch_from_id, +void DCLineContainer::init(const Eigen::VectorXi & branch_from_id, const Eigen::VectorXi & branch_to_id, const RealVect & p_mw, const RealVect & loss_percent, @@ -61,7 +62,7 @@ void DataDCLine::init(const Eigen::VectorXi & branch_from_id, to_gen_.init(p_ex, vm_ex_pu, min_q_ex, max_q_ex, branch_to_id); } -void DataDCLine::nb_line_end(std::vector & res) const +void DCLineContainer::nb_line_end(std::vector & res) const { const Eigen::Index nb = from_gen_.nb(); const auto & bus_or_id = get_bus_id_or(); @@ -76,7 +77,7 @@ void DataDCLine::nb_line_end(std::vector & res) const } // TODO DC LINE: one side might be in the connected comp and not the other ! -void DataDCLine::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) +void DCLineContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) { const Eigen::Index nb = from_gen_.nb(); const auto & bus_or_id = get_bus_id_or(); diff --git a/src/DataDCLine.h b/src/element_container/DCLineContainer.h similarity index 92% rename from src/DataDCLine.h rename to src/element_container/DCLineContainer.h index b9363d2..14abf21 100644 --- a/src/DataDCLine.h +++ b/src/element_container/DCLineContainer.h @@ -6,23 +6,22 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATADCLINE_H -#define DATADCLINE_H +#ifndef DCLINECONTAINER_H +#define DCLINECONTAINER_H #include -#include "Utils.h" - #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" -#include "DataGeneric.h" -#include "DataGen.h" +#include "Utils.h" +#include "GenericContainer.h" +#include "GeneratorContainer.h" -class DataDCLine : public DataGeneric +class DCLineContainer : public GenericContainer { public: class DCLineInfo @@ -39,8 +38,8 @@ class DataDCLine : public DataGeneric real_type target_vm_ex_pu; real_type loss_pct; real_type loss_mw; - DataGen::GenInfo gen_or; - DataGen::GenInfo gen_ex; + GeneratorContainer::GenInfo gen_or; + GeneratorContainer::GenInfo gen_ex; bool has_res; real_type res_p_or_mw; @@ -52,7 +51,7 @@ class DataDCLine : public DataGeneric real_type res_v_ex_kv; real_type res_theta_ex_deg; - DCLineInfo(const DataDCLine & r_data_dcline, int my_id): + DCLineInfo(const DCLineContainer & r_data_dcline, int my_id): id(-1), name(""), connected(false), @@ -109,13 +108,13 @@ class DataDCLine : public DataGeneric typedef DCLineInfo DataInfo; private: - typedef DataConstIterator DataDCLineConstIterator; + typedef GenericContainerConstIterator DCLineConstIterator; public: typedef std::tuple< std::vector, - DataGen::StateRes, - DataGen::StateRes, + GeneratorContainer::StateRes, + GeneratorContainer::StateRes, std::vector, // loss_percent std::vector, // vm_to_pu std::vector // loss_mw @@ -124,9 +123,9 @@ class DataDCLine : public DataGeneric int nb() const { return static_cast(from_gen_.nb()); } // iterator - typedef DataDCLineConstIterator const_iterator_type; - const_iterator_type begin() const {return DataDCLineConstIterator(this, 0); } - const_iterator_type end() const {return DataDCLineConstIterator(this, nb()); } + typedef DCLineConstIterator const_iterator_type; + const_iterator_type begin() const {return DCLineConstIterator(this, 0); } + const_iterator_type end() const {return DCLineConstIterator(this, nb()); } DCLineInfo operator[](int id) const { if(id < 0) @@ -141,11 +140,11 @@ class DataDCLine : public DataGeneric } // underlying generators are not pv when powerline is off - DataDCLine(): from_gen_(false), to_gen_(false) {}; + DCLineContainer(): from_gen_(false), to_gen_(false) {}; // pickle - DataDCLine::StateRes get_state() const; - void set_state(DataDCLine::StateRes & my_state); + DCLineContainer::StateRes get_state() const; + void set_state(DCLineContainer::StateRes & my_state); // TODO min_p, max_p void init(const Eigen::VectorXi & branch_from_id, @@ -297,12 +296,12 @@ class DataDCLine : public DataGeneric protected: // it is modeled as 2 generators that are "linked" together // see https://pandapower.readthedocs.io/en/v2.0.1/elements/dcline.html#electric-model - DataGen from_gen_; - DataGen to_gen_; + GeneratorContainer from_gen_; + GeneratorContainer to_gen_; RealVect loss_percent_; RealVect loss_mw_; std::vector status_; }; -#endif //DATADCLINE_H \ No newline at end of file +#endif //DCLINECONTAINER_H \ No newline at end of file diff --git a/src/DataGen.cpp b/src/element_container/GeneratorContainer.cpp similarity index 84% rename from src/DataGen.cpp rename to src/element_container/GeneratorContainer.cpp index 973f92e..254813f 100644 --- a/src/DataGen.cpp +++ b/src/element_container/GeneratorContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,11 +6,11 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataGen.h" +#include "GeneratorContainer.h" #include #include -void DataGen::init(const RealVect & generators_p, +void GeneratorContainer::init(const RealVect & generators_p, const RealVect & generators_v, const RealVect & generators_min_q, const RealVect & generators_max_q, @@ -24,7 +24,7 @@ void DataGen::init(const RealVect & generators_p, if(min_q_.size() != max_q_.size()) { std::ostringstream exc_; - exc_ << "DataGen::init: Impossible to initialize generator with generators_min_q of size "; + exc_ << "GeneratorContainer::init: Impossible to initialize generator with generators_min_q of size "; exc_ << min_q_.size(); exc_ << " and generators_max_q of size "; exc_ << max_q_.size(); @@ -36,7 +36,7 @@ void DataGen::init(const RealVect & generators_p, if (min_q_(gen_id) > max_q_(gen_id)) { std::ostringstream exc_; - exc_ << "DataGen::init: Impossible to initialize generator min_q being above max_q for generator "; + exc_ << "GeneratorContainer::init: Impossible to initialize generator min_q being above max_q for generator "; exc_ << gen_id; throw std::runtime_error(exc_.str()); } @@ -49,7 +49,7 @@ void DataGen::init(const RealVect & generators_p, q_mvar_ = RealVect::Zero(generators_p.size()); } -void DataGen::init_full(const RealVect & generators_p, +void GeneratorContainer::init_full(const RealVect & generators_p, const RealVect & generators_v, const RealVect & generators_q, const std::vector & voltage_regulator_on, @@ -64,7 +64,7 @@ void DataGen::init_full(const RealVect & generators_p, } -DataGen::StateRes DataGen::get_state() const +GeneratorContainer::StateRes GeneratorContainer::get_state() const { std::vector p_mw(p_mw_.begin(), p_mw_.end()); std::vector vm_pu(vm_pu_.begin(), vm_pu_.end()); @@ -76,13 +76,13 @@ DataGen::StateRes DataGen::get_state() const std::vector slack_bus = gen_slackbus_; std::vector voltage_regulator_on = voltage_regulator_on_; std::vector slack_weight = gen_slack_weight_; - DataGen::StateRes res(names_, turnedoff_gen_pv_, voltage_regulator_on, + GeneratorContainer::StateRes res(names_, turnedoff_gen_pv_, voltage_regulator_on, p_mw, vm_pu, q_mvar, min_q, max_q, bus_id, status, slack_bus, slack_weight); return res; } -void DataGen::set_state(DataGen::StateRes & my_state) +void GeneratorContainer::set_state(GeneratorContainer::StateRes & my_state) { reset_results(); names_ = std::get<0>(my_state); @@ -114,7 +114,7 @@ void DataGen::set_state(DataGen::StateRes & my_state) gen_slack_weight_ = slack_weight; } -RealVect DataGen::get_slack_weights(Eigen::Index nb_bus_solver, const std::vector & id_grid_to_solver){ +RealVect GeneratorContainer::get_slack_weights(Eigen::Index nb_bus_solver, const std::vector & id_grid_to_solver){ const int nb_gen = nb(); int bus_id_me, bus_id_solver; RealVect res = RealVect::Zero(nb_bus_solver); @@ -126,7 +126,7 @@ RealVect DataGen::get_slack_weights(Eigen::Index nb_bus_solver, const std::vecto if(bus_id_solver == _deactivated_bus_id){ // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGen::get_slack_weights: Generator with id "; + exc_ << "GeneratorContainer::get_slack_weights: Generator with id "; exc_ << gen_id; exc_ << " is connected to a disconnected bus while being connected to the grid."; throw std::runtime_error(exc_.str()); @@ -139,7 +139,7 @@ RealVect DataGen::get_slack_weights(Eigen::Index nb_bus_solver, const std::vecto return res; } -void DataGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { +void GeneratorContainer::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { const int nb_gen = nb(); int bus_id_me, bus_id_solver; cplx_type tmp; @@ -152,7 +152,7 @@ void DataGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solv if(bus_id_solver == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::fillSbus: Generator with id "; + exc_ << "GeneratorContainer::fillSbus: Generator with id "; exc_ << gen_id; exc_ << " is connected to a disconnected bus while being connected to the grid."; throw std::runtime_error(exc_.str()); @@ -166,7 +166,7 @@ void DataGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solv } } -void DataGen::fillpv(std::vector & bus_pv, +void GeneratorContainer::fillpv(std::vector & bus_pv, std::vector & has_bus_been_added, const Eigen::VectorXi & slack_bus_id_solver, const std::vector & id_grid_to_solver) const @@ -184,7 +184,7 @@ void DataGen::fillpv(std::vector & bus_pv, if(bus_id_solver == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::fillpv: Generator with id "; + exc_ << "GeneratorContainer::fillpv: Generator with id "; exc_ << gen_id; exc_ << " is connected to a disconnected bus while being connected to the grid."; throw std::runtime_error(exc_.str()); @@ -197,7 +197,7 @@ void DataGen::fillpv(std::vector & bus_pv, } } -void DataGen::compute_results(const Eigen::Ref & Va, +void GeneratorContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -211,7 +211,7 @@ void DataGen::compute_results(const Eigen::Ref & Va, res_p_ = p_mw_; } -void DataGen::reset_results(){ +void GeneratorContainer::reset_results(){ res_p_ = RealVect(); // in MW res_q_ = RealVect(); // in MVar res_v_ = RealVect(); // in kV @@ -219,7 +219,7 @@ void DataGen::reset_results(){ // bus_slack_weight_ = RealVect(); } -void DataGen::get_vm_for_dc(RealVect & Vm){ +void GeneratorContainer::get_vm_for_dc(RealVect & Vm){ const int nb_gen = nb(); int bus_id_me; for(int gen_id = 0; gen_id < nb_gen; ++gen_id){ @@ -235,14 +235,14 @@ void DataGen::get_vm_for_dc(RealVect & Vm){ } } -void DataGen::change_p(int gen_id, real_type new_p, SolverControl & solver_control) +void GeneratorContainer::change_p(int gen_id, real_type new_p, SolverControl & solver_control) { bool my_status = status_.at(gen_id); // and this check that load_id is not out of bound if(!my_status) { // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::change_p: Impossible to change the active value of a disconnected generator (check gen. id "; + exc_ << "GeneratorContainer::change_p: Impossible to change the active value of a disconnected generator (check gen. id "; exc_ << gen_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -261,14 +261,14 @@ void DataGen::change_p(int gen_id, real_type new_p, SolverControl & solver_contr p_mw_(gen_id) = new_p; } -void DataGen::change_q(int gen_id, real_type new_q, SolverControl & solver_control) +void GeneratorContainer::change_q(int gen_id, real_type new_q, SolverControl & solver_control) { bool my_status = status_.at(gen_id); // and this check that load_id is not out of bound if(!my_status) { // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::change_q: Impossible to change the reactive value of a disconnected generator (check gen. id "; + exc_ << "GeneratorContainer::change_q: Impossible to change the reactive value of a disconnected generator (check gen. id "; exc_ << gen_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -279,14 +279,14 @@ void DataGen::change_q(int gen_id, real_type new_q, SolverControl & solver_contr q_mvar_(gen_id) = new_q; } -void DataGen::change_v(int gen_id, real_type new_v_pu, SolverControl & solver_control) +void GeneratorContainer::change_v(int gen_id, real_type new_v_pu, SolverControl & solver_control) { bool my_status = status_.at(gen_id); // and this check that load_id is not out of bound if(!my_status) { // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::change_p: Impossible to change the voltage setpoint of a disconnected generator (check gen. id "; + exc_ << "GeneratorContainer::change_p: Impossible to change the voltage setpoint of a disconnected generator (check gen. id "; exc_ << gen_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -295,7 +295,7 @@ void DataGen::change_v(int gen_id, real_type new_v_pu, SolverControl & solver_co vm_pu_(gen_id) = new_v_pu; } -void DataGen::set_vm(CplxVect & V, const std::vector & id_grid_to_solver) const +void GeneratorContainer::set_vm(CplxVect & V, const std::vector & id_grid_to_solver) const { const int nb_gen = nb(); int bus_id_me, bus_id_solver; @@ -311,7 +311,7 @@ void DataGen::set_vm(CplxVect & V, const std::vector & id_grid_to_solver) c if(bus_id_solver == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::set_vm: Generator with id "; + exc_ << "GeneratorContainer::set_vm: Generator with id "; exc_ << gen_id; exc_ << " is connected to a disconnected bus while being connected to the grid."; throw std::runtime_error(exc_.str()); @@ -331,7 +331,7 @@ void DataGen::set_vm(CplxVect & V, const std::vector & id_grid_to_solver) c } } -Eigen::VectorXi DataGen::get_slack_bus_id() const{ +Eigen::VectorXi GeneratorContainer::get_slack_bus_id() const{ std::vector tmp; tmp.reserve(gen_slackbus_.size()); Eigen::VectorXi res; @@ -343,17 +343,17 @@ Eigen::VectorXi DataGen::get_slack_bus_id() const{ if(!is_in_vect(my_bus, tmp)) tmp.push_back(my_bus); } } - if(tmp.empty()) throw std::runtime_error("DataGen::get_slack_bus_id: no generator are tagged slack bus for this grid."); + if(tmp.empty()) throw std::runtime_error("GeneratorContainer::get_slack_bus_id: no generator are tagged slack bus for this grid."); res = Eigen::VectorXi::Map(tmp.data(), tmp.size()); // force the copy of the data apparently return res; } -void DataGen::set_p_slack(const RealVect& node_mismatch, +void GeneratorContainer::set_p_slack(const RealVect& node_mismatch, const std::vector & id_grid_to_solver) { if(bus_slack_weight_.size() == 0){ // TODO DEBUG MODE: perform this check only in debug mode - throw std::runtime_error("DataGen::set_p_slack: Impossible to set the active value of generators for the slack bus: no known slack (you should haved called DataGen::get_slack_weights first)"); + throw std::runtime_error("Generator::set_p_slack: Impossible to set the active value of generators for the slack bus: no known slack (you should haved called Generator::get_slack_weights first)"); } const auto nb_gen = nb(); for(int gen_id = 0; gen_id < nb_gen; ++gen_id){ @@ -372,7 +372,7 @@ void DataGen::set_p_slack(const RealVect& node_mismatch, } } -void DataGen::init_q_vector(int nb_bus, +void GeneratorContainer::init_q_vector(int nb_bus, Eigen::VectorXi & total_gen_per_bus, RealVect & total_q_min_per_bus, RealVect & total_q_max_per_bus) const // delta_q_per_gen_) // total number of bus on the grid @@ -392,7 +392,7 @@ void DataGen::init_q_vector(int nb_bus, } } -void DataGen::set_q(const RealVect & reactive_mismatch, +void GeneratorContainer::set_q(const RealVect & reactive_mismatch, const std::vector & id_grid_to_solver, bool ac, const Eigen::VectorXi & total_gen_per_bus, @@ -431,7 +431,7 @@ void DataGen::set_q(const RealVect & reactive_mismatch, } -void DataGen::update_slack_weights(Eigen::Ref > could_be_slack, +void GeneratorContainer::update_slack_weights(Eigen::Ref > could_be_slack, SolverControl & solver_control) { const int nb_gen = nb(); @@ -456,7 +456,7 @@ void DataGen::update_slack_weights(Eigen::Ref & bus_status) const { +void GeneratorContainer::reconnect_connected_buses(std::vector & bus_status) const { const int nb_gen = nb(); for(int gen_id = 0; gen_id < nb_gen; ++gen_id) { @@ -465,7 +465,7 @@ void DataGen::reconnect_connected_buses(std::vector & bus_status) const { if(my_bus == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataGen::reconnect_connected_buses: Generator with id "; + exc_ << "Generator::reconnect_connected_buses: Generator with id "; exc_ << gen_id; exc_ << " is connected to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_gen(...)` ?."; throw std::runtime_error(exc_.str()); @@ -474,7 +474,7 @@ void DataGen::reconnect_connected_buses(std::vector & bus_status) const { } } -void DataGen::gen_p_per_bus(std::vector & res) const +void GeneratorContainer::gen_p_per_bus(std::vector & res) const { const int nb_gen = nb(); for(int gen_id = 0; gen_id < nb_gen; ++gen_id) @@ -485,7 +485,7 @@ void DataGen::gen_p_per_bus(std::vector & res) const } } -void DataGen::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ +void GeneratorContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ const int nb_gen = nb(); SolverControl unused_solver_control; for(int gen_id = 0; gen_id < nb_gen; ++gen_id) diff --git a/src/DataGen.h b/src/element_container/GeneratorContainer.h similarity index 92% rename from src/DataGen.h rename to src/element_container/GeneratorContainer.h index 0f531cb..d9b854d 100644 --- a/src/DataGen.h +++ b/src/element_container/GeneratorContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,20 +6,19 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATAGEN_H -#define DATAGEN_H +#ifndef GENERATORCONTAINER_H +#define GENERATORCONTAINER_H #include #include -#include "Utils.h" - #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class represents the list of all generators. @@ -30,7 +29,7 @@ The convention used for the generator is the same as in pandapower: and for modeling of the Ybus matrix: https://pandapower.readthedocs.io/en/latest/elements/gen.html#electric-model **/ -class DataGen: public DataGeneric +class GeneratorContainer: public GenericContainer { public: class GenInfo @@ -57,7 +56,7 @@ class DataGen: public DataGeneric real_type res_v_kv; real_type res_theta_deg; - GenInfo(const DataGen & r_data_gen, int my_id): + GenInfo(const GeneratorContainer & r_data_gen, int my_id): id(-1), name(""), connected(false), @@ -108,7 +107,7 @@ class DataGen: public DataGeneric typedef GenInfo DataInfo; private: - typedef DataConstIterator DataGenConstIterator; + typedef GenericContainerConstIterator GeneratorConstIterator; public: typedef std::tuple< @@ -126,8 +125,8 @@ class DataGen: public DataGeneric std::vector // gen_slack_weight_ > StateRes; - DataGen():turnedoff_gen_pv_(true){}; - DataGen(bool turnedoff_gen_pv):turnedoff_gen_pv_(turnedoff_gen_pv) {}; + GeneratorContainer():turnedoff_gen_pv_(true){}; + GeneratorContainer(bool turnedoff_gen_pv):turnedoff_gen_pv_(turnedoff_gen_pv) {}; // TODO add pmin and pmax here ! void init(const RealVect & generators_p, @@ -149,9 +148,9 @@ class DataGen: public DataGeneric int nb() const { return static_cast(p_mw_.size()); } // iterator - typedef DataGenConstIterator const_iterator_type; - const_iterator_type begin() const {return DataGenConstIterator(this, 0); } - const_iterator_type end() const {return DataGenConstIterator(this, nb()); } + typedef GeneratorConstIterator const_iterator_type; + const_iterator_type begin() const {return GeneratorConstIterator(this, 0); } + const_iterator_type end() const {return GeneratorConstIterator(this, nb()); } GenInfo operator[](int id) const { if(id < 0) @@ -166,8 +165,8 @@ class DataGen: public DataGeneric } // pickle - DataGen::StateRes get_state() const; - void set_state(DataGen::StateRes & my_state ); + GeneratorContainer::StateRes get_state() const; + void set_state(GeneratorContainer::StateRes & my_state ); // slack handling /** @@ -176,7 +175,7 @@ class DataGen: public DataGeneric **/ 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(weight <= 0.) throw std::runtime_error("GeneratorContainer::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(); @@ -212,7 +211,7 @@ class DataGen: public DataGeneric } } // TODO DEBUG MODE - if(res_gen_id == -1) throw std::runtime_error("DataGen::assign_slack_bus No generator connected to the desired buses"); + if(res_gen_id == -1) throw std::runtime_error("GeneratorContainer::assign_slack_bus No generator connected to the desired buses"); return res_gen_id; } @@ -348,4 +347,4 @@ class DataGen: public DataGeneric bool turnedoff_gen_pv_; // are turned off generators (including one with p=0) pv ? }; -#endif //DATAGEN_H +#endif //GENERATORCONTAINER_H diff --git a/src/DataGeneric.cpp b/src/element_container/GenericContainer.cpp similarity index 80% rename from src/DataGeneric.cpp rename to src/element_container/GenericContainer.cpp index 3b7c330..6daa4fe 100644 --- a/src/DataGeneric.cpp +++ b/src/element_container/GenericContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,14 +6,15 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataGeneric.h" +#include "GenericContainer.h" + #include #include -const int DataGeneric::_deactivated_bus_id = -1; +const int GenericContainer::_deactivated_bus_id = -1; // TODO all functions bellow are generic ! Make a base class for that -void DataGeneric::_get_amps(RealVect & a, const RealVect & p, const RealVect & q, const RealVect & v){ +void GenericContainer::_get_amps(RealVect & a, const RealVect & p, const RealVect & q, const RealVect & v){ const real_type _1_sqrt_3 = 1.0 / std::sqrt(3.); RealVect p2q2 = p.array() * p.array() + q.array() * q.array(); p2q2 = p2q2.array().cwiseSqrt(); @@ -26,22 +27,22 @@ void DataGeneric::_get_amps(RealVect & a, const RealVect & p, const RealVect & q } a = p2q2.array() * _1_sqrt_3 / v_tmp.array(); } -void DataGeneric::_reactivate(int el_id, std::vector & status){ +void GenericContainer::_reactivate(int el_id, std::vector & status){ bool val = status.at(el_id); status.at(el_id) = true; //TODO why it's needed to do that again } -void DataGeneric::_deactivate(int el_id, std::vector & status){ +void GenericContainer::_deactivate(int el_id, std::vector & status){ bool val = status.at(el_id); status.at(el_id) = false; //TODO why it's needed to do that again } -void DataGeneric::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el_bus_ids, SolverControl & solver_control, int nb_bus){ +void GenericContainer::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el_bus_ids, SolverControl & solver_control, int nb_bus){ // bus id here "me_id" and NOT "solver_id" // throw error: object id does not exist if(el_id >= el_bus_ids.size()) { // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::_change_bus: Cannot change the bus of element with id "; + exc_ << "GenericContainer::_change_bus: Cannot change the bus of element with id "; exc_ << el_id; exc_ << " while the grid counts "; exc_ << el_bus_ids.size(); @@ -52,7 +53,7 @@ void DataGeneric::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el { // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::_change_bus: Cannot change the bus of element with id "; + exc_ << "GenericContainer::_change_bus: Cannot change the bus of element with id "; exc_ << el_id; exc_ << " (id should be >= 0)"; throw std::out_of_range(exc_.str()); @@ -63,7 +64,7 @@ void DataGeneric::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el { // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::_change_bus: Cannot change an element to bus "; + exc_ << "GenericContainer::_change_bus: Cannot change an element to bus "; exc_ << new_bus_me_id; exc_ << " There are only "; exc_ << nb_bus; @@ -74,7 +75,7 @@ void DataGeneric::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el { // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::_change_bus: new bus id should be >=0 and not "; + exc_ << "GenericContainer::_change_bus: new bus id should be >=0 and not "; exc_ << new_bus_me_id; throw std::out_of_range(exc_.str()); } @@ -92,7 +93,7 @@ void DataGeneric::_change_bus(int el_id, int new_bus_me_id, Eigen::VectorXi & el bus_me_id = new_bus_me_id; } -int DataGeneric::_get_bus(int el_id, const std::vector & status_, const Eigen::VectorXi & bus_id_) +int GenericContainer::_get_bus(int el_id, const std::vector & status_, const Eigen::VectorXi & bus_id_) { int res; bool val = status_.at(el_id); // also check if the el_id is out of bound @@ -103,7 +104,7 @@ int DataGeneric::_get_bus(int el_id, const std::vector & status_, const Ei return res; } -void DataGeneric::v_kv_from_vpu(const Eigen::Ref & Va, +void GenericContainer::v_kv_from_vpu(const Eigen::Ref & Va, const Eigen::Ref & Vm, const std::vector & status, int nb_element, @@ -120,7 +121,7 @@ void DataGeneric::v_kv_from_vpu(const Eigen::Ref & Va, if(bus_solver_id == _deactivated_bus_id){ // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::v_kv_from_vpu: The element of id "; + exc_ << "GenericContainer::v_kv_from_vpu: The element of id "; exc_ << bus_solver_id; exc_ << " is connected to a disconnected bus"; throw std::runtime_error(exc_.str()); @@ -131,7 +132,7 @@ void DataGeneric::v_kv_from_vpu(const Eigen::Ref & Va, } -void DataGeneric::v_deg_from_va(const Eigen::Ref & Va, +void GenericContainer::v_deg_from_va(const Eigen::Ref & Va, const Eigen::Ref & Vm, const std::vector & status, int nb_element, @@ -148,7 +149,7 @@ void DataGeneric::v_deg_from_va(const Eigen::Ref & Va, if(bus_solver_id == _deactivated_bus_id){ // TODO DEBUG MODE: only check in debug mode std::ostringstream exc_; - exc_ << "DataGeneric::v_deg_from_va: The element of id "; + exc_ << "GenericContainer::v_deg_from_va: The element of id "; exc_ << bus_solver_id; exc_ << " is connected to a disconnected bus"; throw std::runtime_error(exc_.str()); diff --git a/src/DataGeneric.h b/src/element_container/GenericContainer.h similarity index 89% rename from src/DataGeneric.h rename to src/element_container/GenericContainer.h index 768cc7d..c0e6da3 100644 --- a/src/DataGeneric.h +++ b/src/element_container/GenericContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,23 +6,22 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATAGENERIC_H -#define DATAGENERIC_H +#ifndef GENERIC_CONTAINER_H +#define GENERIC_CONTAINER_H #include // for std::find -#include "Utils.h" - #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" +#include "Utils.h" #include "BaseConstants.h" // iterator type template -class DataConstIterator +class GenericContainerConstIterator { protected: typedef typename DataType::DataInfo DataInfo; @@ -34,22 +33,22 @@ class DataConstIterator DataInfo my_info; // functions - DataConstIterator(const DataType * const data_, int id): + GenericContainerConstIterator(const DataType * const data_, int id): _p_data_(data_), my_id(id), my_info(*data_, id) {}; const DataInfo& operator*() const { return my_info; } - bool operator==(const DataConstIterator & other) const { return (my_id == other.my_id) && (_p_data_ == other._p_data_); } - bool operator!=(const DataConstIterator & other) const { return !(*this == other); } - DataConstIterator & operator++() + bool operator==(const GenericContainerConstIterator & other) const { return (my_id == other.my_id) && (_p_data_ == other._p_data_); } + bool operator!=(const GenericContainerConstIterator & other) const { return !(*this == other); } + GenericContainerConstIterator & operator++() { ++my_id; my_info = DataInfo(*_p_data_, my_id); return *this; } - DataConstIterator & operator--() + GenericContainerConstIterator & operator--() { --my_id; my_info = DataInfo(*_p_data_, my_id); @@ -63,7 +62,7 @@ class DataConstIterator /** Base class for every object that can be manipulated **/ -class DataGeneric : public BaseConstants +class GenericContainer : public BaseConstants { public: @@ -109,7 +108,7 @@ class DataGeneric : public BaseConstants } /**"define" the destructor for compliance with clang (otherwise lots of warnings)**/ - virtual ~DataGeneric() {}; + virtual ~GenericContainer() {}; protected: std::vector names_; @@ -168,5 +167,5 @@ class DataGeneric : public BaseConstants bool is_in_vect(int val, const T & cont) const {return std::find(cont.begin(), cont.end(), val) != cont.end();} }; -#endif // DATAGENERIC_H +#endif // GENERIC_CONTAINER_H diff --git a/src/DataLine.cpp b/src/element_container/LineContainer.cpp similarity index 90% rename from src/DataLine.cpp rename to src/element_container/LineContainer.cpp index 984f5b2..610aa45 100644 --- a/src/DataLine.cpp +++ b/src/element_container/LineContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,10 +6,11 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataLine.h" +#include "LineContainer.h" + #include -void DataLine::init(const RealVect & branch_r, +void LineContainer::init(const RealVect & branch_r, const RealVect & branch_x, const CplxVect & branch_h, const Eigen::VectorXi & branch_from_id, @@ -38,7 +39,7 @@ void DataLine::init(const RealVect & branch_r, _update_model_coeffs(); } -void DataLine::init(const RealVect & branch_r, +void LineContainer::init(const RealVect & branch_r, const RealVect & branch_x, const CplxVect & branch_h_or, const CplxVect & branch_h_ex, @@ -68,7 +69,7 @@ void DataLine::init(const RealVect & branch_r, _update_model_coeffs(); } -DataLine::StateRes DataLine::get_state() const +LineContainer::StateRes LineContainer::get_state() const { std::vector branch_r(powerlines_r_.begin(), powerlines_r_.end()); std::vector branch_x(powerlines_x_.begin(), powerlines_x_.end()); @@ -77,10 +78,10 @@ DataLine::StateRes DataLine::get_state() const std::vector branch_from_id(bus_or_id_.begin(), bus_or_id_.end()); std::vector branch_to_id(bus_ex_id_.begin(), bus_ex_id_.end()); std::vector status = status_; - DataLine::StateRes res(names_, branch_r, branch_x, branch_hor, branch_hex, branch_from_id, branch_to_id, status); + LineContainer::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) +void LineContainer::set_state(LineContainer::StateRes & my_state) { reset_results(); names_ = std::get<0>(my_state); @@ -107,7 +108,7 @@ void DataLine::set_state(DataLine::StateRes & my_state) _update_model_coeffs(); } -void DataLine::_update_model_coeffs() +void LineContainer::_update_model_coeffs() { const auto my_size = powerlines_r_.size(); @@ -143,12 +144,12 @@ void DataLine::_update_model_coeffs() } } -void DataLine::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver) +void LineContainer::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver) { throw std::runtime_error("You should not use that!"); } -void DataLine::fillYbus(std::vector > & res, +void LineContainer::fillYbus(std::vector > & res, bool ac, const std::vector & id_grid_to_solver, real_type sn_mva) const @@ -169,7 +170,7 @@ void DataLine::fillYbus(std::vector > & res, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillYbusBranch: the line with id "; + exc_ << "Line::fillYbusBranch: the line with id "; exc_ << line_id; exc_ << " is connected (or side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -178,7 +179,7 @@ void DataLine::fillYbus(std::vector > & res, int bus_ex_solver_id = id_grid_to_solver[bus_ex_id_me]; if(bus_ex_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillYbusBranch: the line with id "; + exc_ << "Line::fillYbusBranch: the line with id "; exc_ << line_id; exc_ << " is connected (ex side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -204,7 +205,7 @@ void DataLine::fillYbus(std::vector > & res, } } -void DataLine::fillBp_Bpp(std::vector > & Bp, +void LineContainer::fillBp_Bpp(std::vector > & Bp, std::vector > & Bpp, const std::vector & id_grid_to_solver, real_type sn_mva, @@ -234,7 +235,7 @@ void DataLine::fillBp_Bpp(std::vector > & Bp, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillBp_Bpp: the line with id "; + exc_ << "LineContainer::fillBp_Bpp: the line with id "; exc_ << line_id; exc_ << " is connected (or side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -243,7 +244,7 @@ void DataLine::fillBp_Bpp(std::vector > & Bp, int bus_ex_solver_id = id_grid_to_solver[bus_ex_id_me]; if(bus_ex_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillBp_Bpp: the line with id "; + exc_ << "LineContainer::fillBp_Bpp: the line with id "; exc_ << line_id; exc_ << " is connected (ex side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -259,7 +260,7 @@ void DataLine::fillBp_Bpp(std::vector > & Bp, ys_bpp = 1. / (0. + my_i * powerlines_x_(line_id)); }else{ std::ostringstream exc_; - exc_ << "DataLine::fillBp_Bpp: unknown method for the FDPF powerflow for line id "; + exc_ << "LineContainer::fillBp_Bpp: unknown method for the FDPF powerflow for line id "; exc_ << line_id; throw std::runtime_error(exc_.str()); } @@ -289,7 +290,7 @@ void DataLine::fillBp_Bpp(std::vector > & Bp, } -void DataLine::fillBf_for_PTDF(std::vector > & Bf, +void LineContainer::fillBf_for_PTDF(std::vector > & Bf, const std::vector & id_grid_to_solver, real_type sn_mva, int nb_powerline, @@ -306,7 +307,7 @@ void DataLine::fillBf_for_PTDF(std::vector > & Bf, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillBf_for_PTDF: the line with id "; + exc_ << "LineContainer::fillBf_for_PTDF: the line with id "; exc_ << line_id; exc_ << " is connected (or side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -315,7 +316,7 @@ void DataLine::fillBf_for_PTDF(std::vector > & Bf, int bus_ex_solver_id = id_grid_to_solver[bus_ex_id_me]; if(bus_ex_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::fillBf_for_PTDF: the line with id "; + exc_ << "LineContainer::fillBf_for_PTDF: the line with id "; exc_ << line_id; exc_ << " is connected (ex side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -337,7 +338,7 @@ void DataLine::fillBf_for_PTDF(std::vector > & Bf, } -void DataLine::reset_results() +void LineContainer::reset_results() { res_powerline_por_ = RealVect(); // in MW res_powerline_qor_ = RealVect(); // in MVar @@ -349,7 +350,7 @@ void DataLine::reset_results() res_powerline_aex_ = RealVect(); // in kA } -void DataLine::compute_results(const Eigen::Ref & Va, +void LineContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -378,7 +379,7 @@ void DataLine::compute_results(const Eigen::Ref & Va, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::compute_results: the line with id "; + exc_ << "LineContainer::compute_results: the line with id "; exc_ << line_id; exc_ << " is connected (or side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -387,7 +388,7 @@ void DataLine::compute_results(const Eigen::Ref & Va, int bus_ex_solver_id = id_grid_to_solver[bus_ex_id_me]; if(bus_ex_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLine::compute_results: the line with id "; + exc_ << "LineContainer::compute_results: the line with id "; exc_ << line_id; exc_ << " is connected (ex side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -440,7 +441,7 @@ void DataLine::compute_results(const Eigen::Ref & Va, _get_amps(res_powerline_aex_, res_powerline_pex_, res_powerline_qex_, res_powerline_vex_); } -void DataLine::reconnect_connected_buses(std::vector & bus_status) const{ +void LineContainer::reconnect_connected_buses(std::vector & bus_status) const{ const auto my_size = powerlines_r_.size(); for(Eigen::Index line_id = 0; line_id < my_size; ++line_id){ @@ -451,7 +452,7 @@ void DataLine::reconnect_connected_buses(std::vector & bus_status) const{ if(bus_or_id_me == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataLine::reconnect_connected_buses: Line with id "; + exc_ << "LineContainer::reconnect_connected_buses: Line with id "; exc_ << line_id; exc_ << " is connected (origin) to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_powerline(...)` ?."; throw std::runtime_error(exc_.str()); @@ -462,7 +463,7 @@ void DataLine::reconnect_connected_buses(std::vector & bus_status) const{ if(bus_ex_id_me == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataLine::reconnect_connected_buses: Line with id "; + exc_ << "LineContainer::reconnect_connected_buses: Line with id "; exc_ << line_id; exc_ << " is connected (ext) to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_powerline(...)` ?."; throw std::runtime_error(exc_.str()); @@ -471,7 +472,7 @@ void DataLine::reconnect_connected_buses(std::vector & bus_status) const{ } } -void DataLine::nb_line_end(std::vector & res) const{ +void LineContainer::nb_line_end(std::vector & res) const{ const auto my_size = powerlines_r_.size(); for(Eigen::Index line_id = 0; line_id < my_size; ++line_id){ // don't do anything if the element is disconnected @@ -483,7 +484,7 @@ void DataLine::nb_line_end(std::vector & res) const{ } } -void DataLine::get_graph(std::vector > & res) const +void LineContainer::get_graph(std::vector > & res) const { const auto my_size = powerlines_r_.size(); for(Eigen::Index line_id = 0; line_id < my_size; ++line_id){ @@ -496,7 +497,7 @@ void DataLine::get_graph(std::vector > & res) const } } -void DataLine::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) +void LineContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) { const Eigen::Index nb_line = nb(); SolverControl unused_solver_control; diff --git a/src/DataLine.h b/src/element_container/LineContainer.h similarity index 91% rename from src/DataLine.h rename to src/element_container/LineContainer.h index 49f07db..fb78817 100644 --- a/src/DataLine.h +++ b/src/element_container/LineContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,31 +6,24 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATALINE_H -#define DATALINE_H +#ifndef LINE_CONTAINER_H +#define LINE_CONTAINER_H #include -#include "Utils.h" - #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class is a container for all the powerlines on the grid. -The convention used for the generator is the same as in pandapower: -https://pandapower.readthedocs.io/en/latest/elements/line.html - -and for modeling of the Ybus matrix: -https://pandapower.readthedocs.io/en/latest/elements/line.html#electric-model - **/ -class DataLine : public DataGeneric +class LineContainer : public GenericContainer { public: class LineInfo @@ -60,7 +53,7 @@ class DataLine : public DataGeneric real_type res_a_ex_ka; real_type res_theta_ex_deg; - LineInfo(const DataLine & r_data_line, int my_id): + LineInfo(const LineContainer & r_data_line, int my_id): id(my_id), name(""), connected(false), @@ -118,7 +111,7 @@ class DataLine : public DataGeneric typedef LineInfo DataInfo; private: - typedef DataConstIterator DataLineConstIterator; + typedef GenericContainerConstIterator LineConstIterator; public: typedef std::tuple< @@ -132,7 +125,7 @@ class DataLine : public DataGeneric std::vector // status_ > StateRes; - DataLine() {}; + LineContainer() {}; void init(const RealVect & branch_r, const RealVect & branch_x, @@ -150,8 +143,8 @@ class DataLine : public DataGeneric ); // pickle - DataLine::StateRes get_state() const; - void set_state(DataLine::StateRes & my_state ); + LineContainer::StateRes get_state() const; + void set_state(LineContainer::StateRes & my_state ); template void check_size(const T& my_state) { @@ -159,18 +152,18 @@ class DataLine : public DataGeneric unsigned int size_th = 6; if (my_state.size() != size_th) { - std::cout << "LightSim::DataLine state size " << my_state.size() << " instead of "<< size_th << std::endl; + std::cout << "LightSim::LineContainer state size " << my_state.size() << " instead of "<< size_th << std::endl; // TODO more explicit error message - throw std::runtime_error("Invalid state when loading LightSim::DataLine"); + throw std::runtime_error("Invalid state when loading LightSim::LineContainer"); } } int nb() const { return static_cast(powerlines_r_.size()); } // make it iterable - typedef DataLineConstIterator const_iterator_type; - const_iterator_type begin() const {return DataLineConstIterator(this, 0); } - const_iterator_type end() const {return DataLineConstIterator(this, nb()); } + typedef LineConstIterator const_iterator_type; + const_iterator_type begin() const {return LineConstIterator(this, 0); } + const_iterator_type end() const {return LineConstIterator(this, nb()); } LineInfo operator[](int id) const { if(id < 0) @@ -302,4 +295,4 @@ class DataLine : public DataGeneric CplxVect ydc_tt_; }; -#endif //DATALINE_H +#endif //LINE_CONTAINER_H diff --git a/src/DataLoad.cpp b/src/element_container/LoadContainer.cpp similarity index 77% rename from src/DataLoad.cpp rename to src/element_container/LoadContainer.cpp index 4354f76..4a4de71 100644 --- a/src/DataLoad.cpp +++ b/src/element_container/LoadContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,10 +6,10 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataLoad.h" +#include "LoadContainer.h" #include -void DataLoad::init(const RealVect & loads_p, +void LoadContainer::init(const RealVect & loads_p, const RealVect & loads_q, const Eigen::VectorXi & loads_bus_id) { @@ -20,16 +20,16 @@ void DataLoad::init(const RealVect & loads_p, } -DataLoad::StateRes DataLoad::get_state() const +LoadContainer::StateRes LoadContainer::get_state() const { std::vector p_mw(p_mw_.begin(), p_mw_.end()); std::vector q_mvar(q_mvar_.begin(), q_mvar_.end()); std::vector bus_id(bus_id_.begin(), bus_id_.end()); std::vector status = status_; - DataLoad::StateRes res(names_, p_mw, q_mvar, bus_id, status); + LoadContainer::StateRes res(names_, p_mw, q_mvar, bus_id, status); return res; } -void DataLoad::set_state(DataLoad::StateRes & my_state ) +void LoadContainer::set_state(LoadContainer::StateRes & my_state ) { reset_results(); names_ = std::get<0>(my_state); @@ -47,7 +47,7 @@ void DataLoad::set_state(DataLoad::StateRes & my_state ) } -void DataLoad::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { +void LoadContainer::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { int nb_load = nb(); int bus_id_me, bus_id_solver; cplx_type tmp; @@ -59,7 +59,7 @@ void DataLoad::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_sol bus_id_solver = id_grid_to_solver[bus_id_me]; if(bus_id_solver == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataLoad::fillSbus: the load with id "; + exc_ << "LoadContainer::fillSbus: the load with id "; exc_ << load_id; exc_ << " is connected to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -70,7 +70,7 @@ void DataLoad::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_sol } } -void DataLoad::compute_results(const Eigen::Ref & Va, +void LoadContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -85,19 +85,19 @@ void DataLoad::compute_results(const Eigen::Ref & Va, res_q_ = q_mvar_; } -void DataLoad::reset_results(){ +void LoadContainer::reset_results(){ res_p_ = RealVect(); // in MW res_q_ = RealVect(); // in MVar res_v_ = RealVect(); // in kV } -void DataLoad::change_p(int load_id, real_type new_p, SolverControl & solver_control) +void LoadContainer::change_p(int load_id, real_type new_p, SolverControl & solver_control) { bool my_status = status_.at(load_id); // and this check that load_id is not out of bound if(!my_status) { std::ostringstream exc_; - exc_ << "DataLoad::change_p: Impossible to change the active value of a disconnected load (check load id "; + exc_ << "LoadContainer::change_p: Impossible to change the active value of a disconnected load (check load id "; exc_ << load_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -106,13 +106,13 @@ void DataLoad::change_p(int load_id, real_type new_p, SolverControl & solver_con p_mw_(load_id) = new_p; } -void DataLoad::change_q(int load_id, real_type new_q, SolverControl & solver_control) +void LoadContainer::change_q(int load_id, real_type new_q, SolverControl & solver_control) { bool my_status = status_.at(load_id); // and this check that load_id is not out of bound if(!my_status) { std::ostringstream exc_; - exc_ << "DataLoad::change_q: Impossible to change the reactive value of a disconnected load (check load id "; + exc_ << "LoadContainer::change_q: Impossible to change the reactive value of a disconnected load (check load id "; exc_ << load_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -121,7 +121,7 @@ void DataLoad::change_q(int load_id, real_type new_q, SolverControl & solver_con q_mvar_(load_id) = new_q; } -void DataLoad::reconnect_connected_buses(std::vector & bus_status) const { +void LoadContainer::reconnect_connected_buses(std::vector & bus_status) const { const int nb_load = nb(); for(int load_id = 0; load_id < nb_load; ++load_id) { @@ -130,7 +130,7 @@ void DataLoad::reconnect_connected_buses(std::vector & bus_status) const { if(my_bus == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataLoad::reconnect_connected_buses: Load with id "; + exc_ << "LoadContainer::reconnect_connected_buses: Load with id "; exc_ << load_id; exc_ << " is connected to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_load(...)` ?."; throw std::runtime_error(exc_.str()); @@ -139,7 +139,7 @@ void DataLoad::reconnect_connected_buses(std::vector & bus_status) const { } } -void DataLoad::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ +void LoadContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ const int nb_el = nb(); SolverControl unused_solver_control; for(int el_id = 0; el_id < nb_el; ++el_id) diff --git a/src/DataLoad.h b/src/element_container/LoadContainer.h similarity index 89% rename from src/DataLoad.h rename to src/element_container/LoadContainer.h index 5f08d4b..3786d52 100644 --- a/src/DataLoad.h +++ b/src/element_container/LoadContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,17 +6,17 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATALOAD_H -#define DATALOAD_H +#ifndef LOAD_CONTAINER_H +#define LOAD_CONTAINER_H -#include "Utils.h" #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class is a container for all loads on the grid. @@ -31,7 +31,7 @@ NOTE: this class is also used for the storage units! So storage units are modele which entails that negative storage: the unit is discharging, power is injected in the grid, positive storage: the unit is charging, power is taken from the grid. **/ -class DataLoad : public DataGeneric +class LoadContainer : public GenericContainer { // TODO make a single class for load and shunt and just specialize the part where the // TODO powerflow equations are located (when i update the Y matrix) @@ -56,7 +56,7 @@ class DataLoad : public DataGeneric real_type res_v_kv; real_type res_theta_deg; - LoadInfo(const DataLoad & r_data_load, int my_id): + LoadInfo(const LoadContainer & r_data_load, int my_id): id(-1), name(""), connected(false), @@ -95,12 +95,12 @@ class DataLoad : public DataGeneric typedef LoadInfo DataInfo; private: - typedef DataConstIterator DataLoadConstIterator; + typedef GenericContainerConstIterator LoadContainerConstIterator; public: - typedef DataLoadConstIterator const_iterator_type; - const_iterator_type begin() const {return DataLoadConstIterator(this, 0); } - const_iterator_type end() const {return DataLoadConstIterator(this, nb()); } + typedef LoadContainerConstIterator const_iterator_type; + const_iterator_type begin() const {return LoadContainerConstIterator(this, 0); } + const_iterator_type end() const {return LoadContainerConstIterator(this, nb()); } LoadInfo operator[](int id) const { if(id < 0) @@ -124,11 +124,11 @@ class DataLoad : public DataGeneric std::vector // status > StateRes; - DataLoad() {}; + LoadContainer() {}; // pickle (python) - DataLoad::StateRes get_state() const; - void set_state(DataLoad::StateRes & my_state ); + LoadContainer::StateRes get_state() const; + void set_state(LoadContainer::StateRes & my_state ); void init(const RealVect & loads_p, @@ -189,4 +189,4 @@ class DataLoad : public DataGeneric RealVect res_theta_; // in degree }; -#endif //DATALOAD_H +#endif //LOAD_CONTAINER_H diff --git a/src/DataSGen.cpp b/src/element_container/SGenContainer.cpp similarity index 71% rename from src/DataSGen.cpp rename to src/element_container/SGenContainer.cpp index 08d9100..48c1baa 100644 --- a/src/DataSGen.cpp +++ b/src/element_container/SGenContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,8 +6,9 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataSGen.h" -void DataSGen::init(const RealVect & sgen_p, +#include "SGenContainer.h" + +void SGenContainer::init(const RealVect & sgen_p, const RealVect & sgen_q, const RealVect & sgen_pmin, const RealVect & sgen_pmax, @@ -16,13 +17,13 @@ void DataSGen::init(const RealVect & sgen_p, const Eigen::VectorXi & sgen_bus_id) { int size = static_cast(sgen_p.size()); - DataGeneric::check_size(sgen_p, size, "sgen_p"); - DataGeneric::check_size(sgen_q, size, "sgen_q"); - DataGeneric::check_size(sgen_pmin, size, "sgen_pmin"); - DataGeneric::check_size(sgen_pmax, size, "sgen_pmax"); - DataGeneric::check_size(sgen_qmin, size, "sgen_qmin"); - DataGeneric::check_size(sgen_qmax, size, "sgen_qmax"); - DataGeneric::check_size(sgen_bus_id, size, "sgen_bus_id"); + GenericContainer::check_size(sgen_p, size, "sgen_p"); + GenericContainer::check_size(sgen_q, size, "sgen_q"); + GenericContainer::check_size(sgen_pmin, size, "sgen_pmin"); + GenericContainer::check_size(sgen_pmax, size, "sgen_pmax"); + GenericContainer::check_size(sgen_qmin, size, "sgen_qmin"); + GenericContainer::check_size(sgen_qmax, size, "sgen_qmax"); + GenericContainer::check_size(sgen_bus_id, size, "sgen_bus_id"); p_mw_ = sgen_p; q_mvar_ = sgen_q; @@ -34,7 +35,7 @@ void DataSGen::init(const RealVect & sgen_p, status_ = std::vector(sgen_p.size(), true); } -DataSGen::StateRes DataSGen::get_state() const +SGenContainer::StateRes SGenContainer::get_state() const { std::vector p_mw(p_mw_.begin(), p_mw_.end()); std::vector q_mvar(q_mvar_.begin(), q_mvar_.end()); @@ -44,11 +45,11 @@ DataSGen::StateRes DataSGen::get_state() const std::vector q_max(q_max_mvar_.begin(), q_max_mvar_.end()); std::vector bus_id(bus_id_.begin(), bus_id_.end()); std::vector status = status_; - DataSGen::StateRes res(names_, p_mw, q_mvar, p_min, p_max, q_min, q_max, bus_id, status); + SGenContainer::StateRes res(names_, p_mw, q_mvar, p_min, p_max, q_min, q_max, bus_id, status); return res; } -void DataSGen::set_state(DataSGen::StateRes & my_state ) +void SGenContainer::set_state(SGenContainer::StateRes & my_state ) { reset_results(); @@ -62,14 +63,14 @@ void DataSGen::set_state(DataSGen::StateRes & my_state ) std::vector & bus_id = std::get<7>(my_state); std::vector & status = std::get<8>(my_state); auto size = p_mw.size(); - DataGeneric::check_size(p_mw, size, "p_mw"); - DataGeneric::check_size(q_mvar, size, "q_mvar"); - DataGeneric::check_size(p_min, size, "p_min"); - DataGeneric::check_size(p_max, size, "p_max"); - DataGeneric::check_size(q_min, size, "q_min"); - DataGeneric::check_size(q_max, size, "q_max"); - DataGeneric::check_size(bus_id, size, "bus_id"); - DataGeneric::check_size(status, size, "status"); + GenericContainer::check_size(p_mw, size, "p_mw"); + GenericContainer::check_size(q_mvar, size, "q_mvar"); + GenericContainer::check_size(p_min, size, "p_min"); + GenericContainer::check_size(p_max, size, "p_max"); + GenericContainer::check_size(q_min, size, "q_min"); + GenericContainer::check_size(q_max, size, "q_max"); + GenericContainer::check_size(bus_id, size, "bus_id"); + GenericContainer::check_size(status, size, "status"); p_mw_ = RealVect::Map(&p_mw[0], size); q_mvar_ = RealVect::Map(&q_mvar[0], size); @@ -82,7 +83,7 @@ void DataSGen::set_state(DataSGen::StateRes & my_state ) status_ = status; } -void DataSGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { +void SGenContainer::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const { const int nb_sgen = nb(); int bus_id_me, bus_id_solver; cplx_type tmp; @@ -94,7 +95,7 @@ void DataSGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_sol bus_id_solver = id_grid_to_solver[bus_id_me]; if(bus_id_solver == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataSGen::fillSbus: Static Generator with id "; + exc_ << "SGenContainer::fillSbus: Static Generator with id "; exc_ << sgen_id; exc_ << " is connected to a disconnected bus while being connected to the grid."; throw std::runtime_error(exc_.str()); @@ -105,7 +106,7 @@ void DataSGen::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_sol } } -void DataSGen::compute_results(const Eigen::Ref & Va, +void SGenContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -121,19 +122,19 @@ void DataSGen::compute_results(const Eigen::Ref & Va, else res_q_ = RealVect::Zero(nb_sgen); } -void DataSGen::reset_results(){ +void SGenContainer::reset_results(){ res_p_ = RealVect(); // in MW res_q_ = RealVect(); // in MVar res_v_ = RealVect(); // in kV } -void DataSGen::change_p(int sgen_id, real_type new_p, SolverControl & solver_control) +void SGenContainer::change_p(int sgen_id, real_type new_p, SolverControl & solver_control) { bool my_status = status_.at(sgen_id); // and this check that load_id is not out of bound if(!my_status) { std::ostringstream exc_; - exc_ << "DataSGen::change_p: Impossible to change the active value of a disconnected static generator (check sgen id "; + exc_ << "SGenContainer::change_p: Impossible to change the active value of a disconnected static generator (check sgen id "; exc_ << sgen_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -142,13 +143,13 @@ void DataSGen::change_p(int sgen_id, real_type new_p, SolverControl & solver_con p_mw_(sgen_id) = new_p; } -void DataSGen::change_q(int sgen_id, real_type new_q, SolverControl & solver_control) +void SGenContainer::change_q(int sgen_id, real_type new_q, SolverControl & solver_control) { bool my_status = status_.at(sgen_id); // and this check that load_id is not out of bound if(!my_status) { std::ostringstream exc_; - exc_ << "DataSGen::change_q: Impossible to change the reactive value of a disconnected static generator (check sgen id "; + exc_ << "SGenContainer::change_q: Impossible to change the reactive value of a disconnected static generator (check sgen id "; exc_ << sgen_id; exc_ << ")"; throw std::runtime_error(exc_.str()); @@ -157,7 +158,7 @@ void DataSGen::change_q(int sgen_id, real_type new_q, SolverControl & solver_con q_mvar_(sgen_id) = new_q; } -void DataSGen::reconnect_connected_buses(std::vector & bus_status) const { +void SGenContainer::reconnect_connected_buses(std::vector & bus_status) const { const int nb_sgen = nb(); for(int sgen_id = 0; sgen_id < nb_sgen; ++sgen_id) { @@ -166,7 +167,7 @@ void DataSGen::reconnect_connected_buses(std::vector & bus_status) const { if(my_bus == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataSGen::reconnect_connected_buses: Static Generator with id "; + exc_ << "SGenContainer::reconnect_connected_buses: Static Generator with id "; exc_ << sgen_id; exc_ << " is connected to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_sgen(...)` ?."; throw std::runtime_error(exc_.str()); @@ -175,7 +176,7 @@ void DataSGen::reconnect_connected_buses(std::vector & bus_status) const { } } -void DataSGen::gen_p_per_bus(std::vector & res) const +void SGenContainer::gen_p_per_bus(std::vector & res) const { const int nb_gen = nb(); for(int sgen_id = 0; sgen_id < nb_gen; ++sgen_id) @@ -186,7 +187,7 @@ void DataSGen::gen_p_per_bus(std::vector & res) const } } -void DataSGen::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ +void SGenContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ const int nb_el = nb(); SolverControl unused_solver_control; for(int el_id = 0; el_id < nb_el; ++el_id) diff --git a/src/DataSGen.h b/src/element_container/SGenContainer.h similarity index 90% rename from src/DataSGen.h rename to src/element_container/SGenContainer.h index d9c1b30..ae05cdb 100644 --- a/src/DataSGen.h +++ b/src/element_container/SGenContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,17 +6,17 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATASGEN_H -#define DATASGEN_H +#ifndef SGEN_CONTAINER_H +#define SGEN_CONTAINER_H -#include "Utils.h" #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class is a container for all static generator (PQ generators) on the grid. @@ -28,7 +28,7 @@ The convention used for the static is the same as in pandapower: and for modeling of the Ybus matrix: https://pandapower.readthedocs.io/en/latest/elements/sgen.html#electric-model **/ -class DataSGen: public DataGeneric +class SGenContainer: public GenericContainer { // TODO make a single class for load and shunt and just specialize the part where the // TODO powerflow equations are located (when i update the Y matrix) @@ -60,7 +60,7 @@ class DataSGen: public DataGeneric real_type res_v_kv; real_type res_theta_deg; - SGenInfo(const DataSGen & r_data_sgen, int my_id): + SGenInfo(const SGenContainer & r_data_sgen, int my_id): id(-1), name(""), connected(false), @@ -108,12 +108,12 @@ class DataSGen: public DataGeneric typedef SGenInfo DataInfo; private: - typedef DataConstIterator DataSGenConstIterator; + typedef GenericContainerConstIterator SGenContainerConstIterator; public: - typedef DataSGenConstIterator const_iterator_type; - const_iterator_type begin() const {return DataSGenConstIterator(this, 0); } - const_iterator_type end() const {return DataSGenConstIterator(this, nb()); } + typedef SGenContainerConstIterator const_iterator_type; + const_iterator_type begin() const {return SGenContainerConstIterator(this, 0); } + const_iterator_type end() const {return SGenContainerConstIterator(this, nb()); } SGenInfo operator[](int id) const { if(id < 0) @@ -140,11 +140,11 @@ class DataSGen: public DataGeneric std::vector // status > StateRes; - DataSGen() {}; + SGenContainer() {}; // pickle (python) - DataSGen::StateRes get_state() const; - void set_state(DataSGen::StateRes & my_state ); + SGenContainer::StateRes get_state() const; + void set_state(SGenContainer::StateRes & my_state ); void init(const RealVect & sgen_p, @@ -214,4 +214,4 @@ class DataSGen: public DataGeneric RealVect res_theta_; // in degree }; -#endif //DATASGEN_H +#endif //SGEN_CONTAINER_H diff --git a/src/DataShunt.cpp b/src/element_container/ShuntContainer.cpp similarity index 81% rename from src/DataShunt.cpp rename to src/element_container/ShuntContainer.cpp index ba52bbd..5817834 100644 --- a/src/DataShunt.cpp +++ b/src/element_container/ShuntContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,10 +6,11 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataShunt.h" +#include "ShuntContainer.h" + #include -void DataShunt::init(const RealVect & shunt_p_mw, +void ShuntContainer::init(const RealVect & shunt_p_mw, const RealVect & shunt_q_mvar, const Eigen::VectorXi & shunt_bus_id) { @@ -19,17 +20,17 @@ void DataShunt::init(const RealVect & shunt_p_mw, status_ = std::vector(p_mw_.size(), true); // by default everything is connected } -DataShunt::StateRes DataShunt::get_state() const +ShuntContainer::StateRes ShuntContainer::get_state() const { std::vector p_mw(p_mw_.begin(), p_mw_.end()); std::vector q_mvar(q_mvar_.begin(), q_mvar_.end()); std::vector bus_id(bus_id_.begin(), bus_id_.end()); std::vector status = status_; - DataShunt::StateRes res(names_, p_mw, q_mvar, bus_id, status); + ShuntContainer::StateRes res(names_, p_mw, q_mvar, bus_id, status); return res; } -void DataShunt::set_state(DataShunt::StateRes & my_state ) +void ShuntContainer::set_state(ShuntContainer::StateRes & my_state ) { reset_results(); names_ = std::get<0>(my_state); @@ -46,7 +47,7 @@ void DataShunt::set_state(DataShunt::StateRes & my_state ) status_ = status; } -void DataShunt::fillYbus(std::vector > & res, +void ShuntContainer::fillYbus(std::vector > & res, bool ac, const std::vector & id_grid_to_solver, real_type sn_mva) const @@ -67,7 +68,7 @@ void DataShunt::fillYbus(std::vector > & res, bus_id_solver = id_grid_to_solver[bus_id_me]; if(bus_id_solver == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataShunt::fillYbus: the shunt with id "; + exc_ << "ShuntContainer::fillYbus: the shunt with id "; exc_ << shunt_id; exc_ << " is connected to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -77,7 +78,7 @@ void DataShunt::fillYbus(std::vector > & res, } } -void DataShunt::fillBp_Bpp(std::vector > & Bp, +void ShuntContainer::fillBp_Bpp(std::vector > & Bp, std::vector > & Bpp, const std::vector & id_grid_to_solver, real_type sn_mva, @@ -94,7 +95,7 @@ void DataShunt::fillBp_Bpp(std::vector > & Bp, bus_id_solver = id_grid_to_solver[bus_id_me]; if(bus_id_solver == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataShunt::fillBp_Bpp: the shunt with id "; + exc_ << "ShuntContainer::fillBp_Bpp: the shunt with id "; exc_ << shunt_id; exc_ << " is connected to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -106,7 +107,7 @@ void DataShunt::fillBp_Bpp(std::vector > & Bp, } } -void DataShunt::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const // in DC i need that +void ShuntContainer::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_solver, bool ac) const // in DC i need that { if(ac) return; // in AC I do not do that // std::cout << " ok i use this function" << std::endl; @@ -126,11 +127,11 @@ void DataShunt::fillSbus(CplxVect & Sbus, const std::vector & id_grid_to_so } } -void DataShunt::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver){ - throw std::runtime_error("DataShunt::fillYbus_spmat: should not be used anymore !"); +void ShuntContainer::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver){ + throw std::runtime_error("ShuntContainer::fillYbus_spmat: should not be used anymore !"); } -void DataShunt::compute_results(const Eigen::Ref & Va, +void ShuntContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -148,7 +149,7 @@ void DataShunt::compute_results(const Eigen::Ref & Va, int bus_id_me = bus_id_(shunt_id); int bus_solver_id = id_grid_to_solver[bus_id_me]; if(bus_solver_id == _deactivated_bus_id){ - throw std::runtime_error("DataShunt::compute_results: A shunt is connected to a disconnected bus."); + throw std::runtime_error("ShuntContainer::compute_results: A shunt is connected to a disconnected bus."); } cplx_type E = V(bus_solver_id); cplx_type y = -my_one_ * (p_mw_(shunt_id) + my_i * q_mvar_(shunt_id)) / sn_mva; @@ -160,13 +161,13 @@ void DataShunt::compute_results(const Eigen::Ref & Va, } } -void DataShunt::reset_results(){ +void ShuntContainer::reset_results(){ res_p_ = RealVect(); // in MW res_q_ = RealVect(); // in MVar res_v_ = RealVect(); // in kV } -void DataShunt::change_p(int shunt_id, real_type new_p, SolverControl & solver_control) +void ShuntContainer::change_p(int shunt_id, real_type new_p, SolverControl & solver_control) { bool my_status = status_.at(shunt_id); // and this check that load_id is not out of bound if(!my_status) throw std::runtime_error("Impossible to change the active value of a disconnected shunt"); @@ -178,7 +179,7 @@ void DataShunt::change_p(int shunt_id, real_type new_p, SolverControl & solver_c } -void DataShunt::change_q(int shunt_id, real_type new_q, SolverControl & solver_control) +void ShuntContainer::change_q(int shunt_id, real_type new_q, SolverControl & solver_control) { bool my_status = status_.at(shunt_id); // and this check that load_id is not out of bound if(!my_status) throw std::runtime_error("Impossible to change the reactive value of a disconnected shunt"); @@ -188,7 +189,7 @@ void DataShunt::change_q(int shunt_id, real_type new_q, SolverControl & solver_c q_mvar_(shunt_id) = new_q; } -void DataShunt::reconnect_connected_buses(std::vector & bus_status) const { +void ShuntContainer::reconnect_connected_buses(std::vector & bus_status) const { const int nb_shunt = nb(); for(int shunt_id = 0; shunt_id < nb_shunt; ++shunt_id) { @@ -197,7 +198,7 @@ void DataShunt::reconnect_connected_buses(std::vector & bus_status) const if(my_bus == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataShunt::reconnect_connected_buses: Shunt with id "; + exc_ << "ShuntContainer::reconnect_connected_buses: Shunt with id "; exc_ << shunt_id; exc_ << " is connected to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_shunt(...)` ?."; throw std::runtime_error(exc_.str()); @@ -206,7 +207,7 @@ void DataShunt::reconnect_connected_buses(std::vector & bus_status) const } } -void DataShunt::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ +void ShuntContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component){ const int nb_el = nb(); SolverControl unused_solver_control; for(int el_id = 0; el_id < nb_el; ++el_id) diff --git a/src/DataShunt.h b/src/element_container/ShuntContainer.h similarity index 89% rename from src/DataShunt.h rename to src/element_container/ShuntContainer.h index 53e5c33..2cbed98 100644 --- a/src/DataShunt.h +++ b/src/element_container/ShuntContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,18 +6,16 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATASHUNT_H -#define DATASHUNT_H - -#include "Utils.h" +#ifndef SHUNT_CONTAINER_H +#define SHUNT_CONTAINER_H #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" - -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class is a container for all shunts on the grid. @@ -28,7 +26,7 @@ The convention used for the shunt is the same as in pandapower: and for modeling of the Ybus matrix: https://pandapower.readthedocs.io/en/latest/elements/shunt.html#electric-model **/ -class DataShunt : public DataGeneric +class ShuntContainer : public GenericContainer { // iterators part public: @@ -50,7 +48,7 @@ class DataShunt : public DataGeneric real_type res_v_kv; real_type res_theta_deg; - ShuntInfo(const DataShunt & r_data_shunt, int my_id): + ShuntInfo(const ShuntContainer & r_data_shunt, int my_id): id(-1), name(""), connected(false), @@ -89,12 +87,12 @@ class DataShunt : public DataGeneric typedef ShuntInfo DataInfo; private: - typedef DataConstIterator DataShuntConstIterator; + typedef GenericContainerConstIterator ShuntContainerConstIterator; public: - typedef DataShuntConstIterator const_iterator_type; - const_iterator_type begin() const {return DataShuntConstIterator(this, 0); } - const_iterator_type end() const {return DataShuntConstIterator(this, nb()); } + typedef ShuntContainerConstIterator const_iterator_type; + const_iterator_type begin() const {return ShuntContainerConstIterator(this, 0); } + const_iterator_type end() const {return ShuntContainerConstIterator(this, nb()); } ShuntInfo operator[](int id) const { if(id < 0) @@ -117,7 +115,7 @@ class DataShunt : public DataGeneric std::vector // status > StateRes; - DataShunt() {}; + ShuntContainer() {}; void init(const RealVect & shunt_p_mw, const RealVect & shunt_q_mvar, @@ -125,8 +123,8 @@ class DataShunt : public DataGeneric ); // pickle (python) - DataShunt::StateRes get_state() const; - void set_state(DataShunt::StateRes & my_state ); + ShuntContainer::StateRes get_state() const; + void set_state(ShuntContainer::StateRes & my_state ); int nb() const { return static_cast(p_mw_.size()); } @@ -191,4 +189,4 @@ class DataShunt : public DataGeneric RealVect res_theta_; // in kV }; -#endif //DATASHUNT_H +#endif //SHUNT_CONTAINER_H diff --git a/src/DataTrafo.cpp b/src/element_container/TrafoContainer.cpp similarity index 86% rename from src/DataTrafo.cpp rename to src/element_container/TrafoContainer.cpp index 15afa2a..ac4c346 100644 --- a/src/DataTrafo.cpp +++ b/src/element_container/TrafoContainer.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,11 +6,12 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "DataTrafo.h" +#include "TrafoContainer.h" + #include #include -void DataTrafo::init(const RealVect & trafo_r, +void TrafoContainer::init(const RealVect & trafo_r, const RealVect & trafo_x, const CplxVect & trafo_b, const RealVect & trafo_tap_step_pct, @@ -27,15 +28,15 @@ void DataTrafo::init(const RealVect & trafo_r, DOES NOT WORK WITH POWERLINES **/ const int size = static_cast(trafo_r.size()); - DataGeneric::check_size(trafo_r, size, "trafo_r"); - DataGeneric::check_size(trafo_x, size, "trafo_x"); - DataGeneric::check_size(trafo_b, size, "trafo_b"); - DataGeneric::check_size(trafo_tap_step_pct, size, "trafo_tap_step_pct"); - DataGeneric::check_size(trafo_tap_pos, size, "trafo_tap_pos"); - DataGeneric::check_size(trafo_shift_degree, size, "trafo_shift_degree"); - DataGeneric::check_size(trafo_tap_hv, static_cast::size_type>(size), "trafo_tap_hv"); - DataGeneric::check_size(trafo_hv_id, size, "trafo_hv_id"); - DataGeneric::check_size(trafo_lv_id, size, "trafo_lv_id"); + GenericContainer::check_size(trafo_r, size, "trafo_r"); + GenericContainer::check_size(trafo_x, size, "trafo_x"); + GenericContainer::check_size(trafo_b, size, "trafo_b"); + GenericContainer::check_size(trafo_tap_step_pct, size, "trafo_tap_step_pct"); + GenericContainer::check_size(trafo_tap_pos, size, "trafo_tap_pos"); + GenericContainer::check_size(trafo_shift_degree, size, "trafo_shift_degree"); + GenericContainer::check_size(trafo_tap_hv, static_cast::size_type>(size), "trafo_tap_hv"); + GenericContainer::check_size(trafo_hv_id, size, "trafo_hv_id"); + GenericContainer::check_size(trafo_lv_id, size, "trafo_lv_id"); //TODO "parrallel" in the pandapower dataframe, like for lines, are not handled. Handle it python side! @@ -55,7 +56,7 @@ void DataTrafo::init(const RealVect & trafo_r, } -DataTrafo::StateRes DataTrafo::get_state() const +TrafoContainer::StateRes TrafoContainer::get_state() const { std::vector branch_r(r_.begin(), r_.end()); std::vector branch_x(x_.begin(), x_.end()); @@ -66,12 +67,12 @@ DataTrafo::StateRes DataTrafo::get_state() const std::vector ratio(ratio_.begin(), ratio_.end()); std::vector shift(shift_.begin(), shift_.end()); std::vector is_tap_hv_side = is_tap_hv_side_; - DataTrafo::StateRes res(names_, branch_r, branch_x, branch_h, bus_hv_id, bus_lv_id, status, ratio, is_tap_hv_side, shift); + TrafoContainer::StateRes res(names_, branch_r, branch_x, branch_h, bus_hv_id, bus_lv_id, status, ratio, is_tap_hv_side, shift); return res; } -void DataTrafo::set_state(DataTrafo::StateRes & my_state) +void TrafoContainer::set_state(TrafoContainer::StateRes & my_state) { reset_results(); @@ -87,15 +88,15 @@ void DataTrafo::set_state(DataTrafo::StateRes & my_state) std::vector & shift = std::get<9>(my_state); auto size = branch_r.size(); - DataGeneric::check_size(branch_r, size, "branch_r"); - DataGeneric::check_size(branch_x, size, "branch_x"); - DataGeneric::check_size(branch_h, size, "branch_h"); - DataGeneric::check_size(bus_hv_id, size, "bus_hv_id"); - DataGeneric::check_size(bus_lv_id, size, "bus_lv_id"); - DataGeneric::check_size(status, size, "status"); - DataGeneric::check_size(ratio, size, "ratio"); - DataGeneric::check_size(is_tap_hv_side, size, "is_tap_hv_side"); - DataGeneric::check_size(shift, size, "shift"); + GenericContainer::check_size(branch_r, size, "branch_r"); + GenericContainer::check_size(branch_x, size, "branch_x"); + GenericContainer::check_size(branch_h, size, "branch_h"); + GenericContainer::check_size(bus_hv_id, size, "bus_hv_id"); + GenericContainer::check_size(bus_lv_id, size, "bus_lv_id"); + GenericContainer::check_size(status, size, "status"); + GenericContainer::check_size(ratio, size, "ratio"); + GenericContainer::check_size(is_tap_hv_side, size, "is_tap_hv_side"); + GenericContainer::check_size(shift, size, "shift"); // now assign the values r_ = RealVect::Map(&branch_r[0], size); @@ -113,7 +114,7 @@ void DataTrafo::set_state(DataTrafo::StateRes & my_state) } -void DataTrafo::_update_model_coeffs() +void TrafoContainer::_update_model_coeffs() { const Eigen::Index my_size = r_.size(); @@ -163,12 +164,12 @@ void DataTrafo::_update_model_coeffs() } } -void DataTrafo::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver) +void TrafoContainer::fillYbus_spmat(Eigen::SparseMatrix & res, bool ac, const std::vector & id_grid_to_solver) { throw std::runtime_error("You should not use that!"); } -void DataTrafo::fillYbus(std::vector > & res, +void TrafoContainer::fillYbus(std::vector > & res, bool ac, const std::vector & id_grid_to_solver, real_type sn_mva) const @@ -186,7 +187,7 @@ void DataTrafo::fillYbus(std::vector > & res, int bus_hv_solver_id = id_grid_to_solver[bus_hv_id_me]; if(bus_hv_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::fillYbus: the trafo with id "; + exc_ << "TrafoContainer::fillYbus: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (hv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -195,7 +196,7 @@ void DataTrafo::fillYbus(std::vector > & res, int bus_lv_solver_id = id_grid_to_solver[bus_lv_id_me]; if(bus_lv_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::fillYbus: the trafo with id "; + exc_ << "TrafoContainer::fillYbus: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (lv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -221,7 +222,7 @@ void DataTrafo::fillYbus(std::vector > & res, } } -void DataTrafo::hack_Sbus_for_dc_phase_shifter(CplxVect & Sbus, bool ac, const std::vector & id_grid_to_solver){ +void TrafoContainer::hack_Sbus_for_dc_phase_shifter(CplxVect & Sbus, bool ac, const std::vector & id_grid_to_solver){ if(ac) return; // return; const int nb_trafo = nb(); @@ -235,7 +236,7 @@ void DataTrafo::hack_Sbus_for_dc_phase_shifter(CplxVect & Sbus, bool ac, const s bus_id_solver_lv = id_grid_to_solver[bus_id_me]; if(bus_id_solver_lv == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::hack_Sbus_for_dc_phase_shifter: the trafo with id "; + exc_ << "TrafoContainer::hack_Sbus_for_dc_phase_shifter: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (lv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -244,7 +245,7 @@ void DataTrafo::hack_Sbus_for_dc_phase_shifter(CplxVect & Sbus, bool ac, const s bus_id_solver_hv = id_grid_to_solver[bus_id_me]; if(bus_id_solver_hv == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::hack_Sbus_for_dc_phase_shifter: the trafo with id "; + exc_ << "TrafoContainer::hack_Sbus_for_dc_phase_shifter: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (hv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -254,7 +255,7 @@ void DataTrafo::hack_Sbus_for_dc_phase_shifter(CplxVect & Sbus, bool ac, const s } } -void DataTrafo::compute_results(const Eigen::Ref & Va, +void TrafoContainer::compute_results(const Eigen::Ref & Va, const Eigen::Ref & Vm, const Eigen::Ref & V, const std::vector & id_grid_to_solver, @@ -284,7 +285,7 @@ void DataTrafo::compute_results(const Eigen::Ref & Va, int bus_hv_solver_id = id_grid_to_solver[bus_hv_id_me]; if(bus_hv_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::compute_results: the trafo with id "; + exc_ << "TrafoContainer::compute_results: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (hv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -293,7 +294,7 @@ void DataTrafo::compute_results(const Eigen::Ref & Va, int bus_lv_solver_id = id_grid_to_solver[bus_lv_id_me]; if(bus_lv_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::compute_results: the trafo with id "; + exc_ << "TrafoContainer::compute_results: the trafo with id "; exc_ << trafo_id; exc_ << " is connected (lv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -346,7 +347,7 @@ void DataTrafo::compute_results(const Eigen::Ref & Va, _get_amps(res_a_lv_, res_p_lv_, res_q_lv_, res_v_lv_); } -void DataTrafo::reset_results(){ +void TrafoContainer::reset_results(){ res_p_hv_ = RealVect(); // in MW res_q_hv_ = RealVect(); // in MVar res_v_hv_ = RealVect(); // in kV @@ -358,7 +359,7 @@ void DataTrafo::reset_results(){ } -void DataTrafo::fillBp_Bpp(std::vector > & Bp, +void TrafoContainer::fillBp_Bpp(std::vector > & Bp, std::vector > & Bpp, const std::vector & id_grid_to_solver, real_type sn_mva, @@ -389,7 +390,7 @@ void DataTrafo::fillBp_Bpp(std::vector > & Bp, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::fillBp_Bpp: the trafo with id "; + exc_ << "TrafoContainer::fillBp_Bpp: the trafo with id "; exc_ << tr_id; exc_ << " is connected (hv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -398,7 +399,7 @@ void DataTrafo::fillBp_Bpp(std::vector > & Bp, int bus_ex_solver_id = id_grid_to_solver[bus_ex_id_me]; if(bus_ex_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::fillBp_Bpp: the trafo with id "; + exc_ << "TrafoContainer::fillBp_Bpp: the trafo with id "; exc_ << tr_id; exc_ << " is connected (lv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -428,7 +429,7 @@ void DataTrafo::fillBp_Bpp(std::vector > & Bp, ys_bpp = 1. / (0. + my_i * x_(tr_id)); }else{ std::ostringstream exc_; - exc_ << "DataTrafo::fillBp_Bpp: unknown method for the FDPF powerflow for line id "; + exc_ << "TrafoContainer::fillBp_Bpp: unknown method for the FDPF powerflow for line id "; exc_ << tr_id; throw std::runtime_error(exc_.str()); } @@ -459,7 +460,7 @@ void DataTrafo::fillBp_Bpp(std::vector > & Bp, } -void DataTrafo::fillBf_for_PTDF(std::vector > & Bf, +void TrafoContainer::fillBf_for_PTDF(std::vector > & Bf, const std::vector & id_grid_to_solver, real_type sn_mva, int nb_powerline, @@ -476,7 +477,7 @@ void DataTrafo::fillBf_for_PTDF(std::vector > & Bf, int bus_or_solver_id = id_grid_to_solver[bus_or_id_me]; if(bus_or_solver_id == _deactivated_bus_id){ std::ostringstream exc_; - exc_ << "DataTrafo::fillBf_for_PTDF: the line with id "; + exc_ << "TrafoContainer::fillBf_for_PTDF: the line with id "; exc_ << tr_id; exc_ << " is connected (hv side) to a disconnected bus while being connected"; throw std::runtime_error(exc_.str()); @@ -508,7 +509,7 @@ void DataTrafo::fillBf_for_PTDF(std::vector > & Bf, } -void DataTrafo::reconnect_connected_buses(std::vector & bus_status) const{ +void TrafoContainer::reconnect_connected_buses(std::vector & bus_status) const{ const Eigen::Index nb_trafo = nb(); for(Eigen::Index trafo_id = 0; trafo_id < nb_trafo; ++trafo_id){ @@ -519,7 +520,7 @@ void DataTrafo::reconnect_connected_buses(std::vector & bus_status) const{ if(bus_or_id_me == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataTrafo::reconnect_connected_buses: Trafo with id "; + exc_ << "TrafoContainer::reconnect_connected_buses: Trafo with id "; exc_ << trafo_id; exc_ << " is connected (hv) to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_trafo(...)` ?."; throw std::runtime_error(exc_.str()); @@ -530,7 +531,7 @@ void DataTrafo::reconnect_connected_buses(std::vector & bus_status) const{ if(bus_ex_id_me == _deactivated_bus_id){ // TODO DEBUG MODE only this in debug mode std::ostringstream exc_; - exc_ << "DataTrafo::reconnect_connected_buses: Trafo with id "; + exc_ << "TrafoContainer::reconnect_connected_buses: Trafo with id "; exc_ << trafo_id; exc_ << " is connected (lv) to bus '-1' (meaning disconnected) while you said it was disconnected. Have you called `gridmodel.deactivate_trafo(...)` ?."; throw std::runtime_error(exc_.str()); @@ -539,7 +540,7 @@ void DataTrafo::reconnect_connected_buses(std::vector & bus_status) const{ } } -void DataTrafo::nb_line_end(std::vector & res) const{ +void TrafoContainer::nb_line_end(std::vector & res) const{ const Eigen::Index nb_trafo = nb(); for(Eigen::Index trafo_id = 0; trafo_id < nb_trafo; ++trafo_id){ @@ -552,7 +553,7 @@ void DataTrafo::nb_line_end(std::vector & res) const{ } } -void DataTrafo::get_graph(std::vector > & res) const +void TrafoContainer::get_graph(std::vector > & res) const { const auto my_size = nb(); for(Eigen::Index line_id = 0; line_id < my_size; ++line_id){ @@ -565,7 +566,7 @@ void DataTrafo::get_graph(std::vector > & res) const } } -void DataTrafo::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) +void TrafoContainer::disconnect_if_not_in_main_component(std::vector & busbar_in_main_component) { const Eigen::Index nb_line = nb(); SolverControl unused_solver_control; diff --git a/src/DataTrafo.h b/src/element_container/TrafoContainer.h similarity index 93% rename from src/DataTrafo.h rename to src/element_container/TrafoContainer.h index 033a58a..6e15025 100644 --- a/src/DataTrafo.h +++ b/src/element_container/TrafoContainer.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020, RTE (https://www.rte-france.com) +// Copyright (c) 2020-2023, RTE (https://www.rte-france.com) // See AUTHORS.txt // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, @@ -6,18 +6,17 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DATATRAFO_H -#define DATATRAFO_H +#ifndef TRAFO_CONTAINER_H +#define TRAFO_CONTAINER_H -#include "Utils.h" #include "Eigen/Core" #include "Eigen/Dense" #include "Eigen/SparseCore" #include "Eigen/SparseLU" - -#include "DataGeneric.h" +#include "Utils.h" +#include "GenericContainer.h" /** This class is a container for all transformers on the grid. @@ -30,7 +29,7 @@ The convention used for the transformer is the same as in pandapower: and for modeling of the Ybus matrix: https://pandapower.readthedocs.io/en/latest/elements/trafo.html#electric-model **/ -class DataTrafo : public DataGeneric +class TrafoContainer : public GenericContainer { public: class TrafoInfo @@ -61,7 +60,7 @@ class DataTrafo : public DataGeneric real_type res_a_lv_ka; real_type res_theta_lv_deg; - TrafoInfo(const DataTrafo & r_data_trafo, int my_id): + TrafoInfo(const TrafoContainer & r_data_trafo, int my_id): id(-1), name(""), connected(false), @@ -121,7 +120,7 @@ class DataTrafo : public DataGeneric typedef TrafoInfo DataInfo; private: - typedef DataConstIterator DataTrafoConstIterator; + typedef GenericContainerConstIterator TrafoContainerConstIterator; public: typedef std::tuple< @@ -137,7 +136,7 @@ class DataTrafo : public DataGeneric std::vector // shift_ > StateRes; - DataTrafo() {}; + TrafoContainer() {}; void init(const RealVect & trafo_r, const RealVect & trafo_x, @@ -151,15 +150,15 @@ class DataTrafo : public DataGeneric const Eigen::VectorXi & trafo_lv_id ); //pickle - DataTrafo::StateRes get_state() const; - void set_state(DataTrafo::StateRes & my_state ); + TrafoContainer::StateRes get_state() const; + void set_state(TrafoContainer::StateRes & my_state ); int nb() const { return static_cast(r_.size()); } // make it iterable - typedef DataTrafoConstIterator const_iterator_type; - const_iterator_type begin() const {return DataTrafoConstIterator(this, 0); } - const_iterator_type end() const {return DataTrafoConstIterator(this, nb()); } + typedef TrafoContainerConstIterator const_iterator_type; + const_iterator_type begin() const {return TrafoContainerConstIterator(this, 0); } + const_iterator_type end() const {return TrafoContainerConstIterator(this, nb()); } TrafoInfo operator[](int id) const { if(id < 0) @@ -288,4 +287,4 @@ class DataTrafo : public DataGeneric RealVect dc_x_tau_shift_; }; -#endif //DATATRAFO_H +#endif //TRAFO_CONTAINER_H diff --git a/src/help_fun_msg.cpp b/src/help_fun_msg.cpp index a33c1b6..48f58a9 100644 --- a/src/help_fun_msg.cpp +++ b/src/help_fun_msg.cpp @@ -808,12 +808,12 @@ const std::string DocIterator::has_res = R"mydelimiter( )mydelimiter"; -const std::string DocIterator::DataGen = R"mydelimiter( +const std::string DocIterator::GeneratorContainer = R"mydelimiter( This class allows to iterate through the generators of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. In lightsim2grid they are modeled as "pv" meanings you give the active production setpoint and voltage magnitude setpoint - (see :attr:`lightsim2grid.elements.DataSGen` for more exotic PQ generators). + (see :attr:`lightsim2grid.elements.SGenContainer` for more exotic PQ generators). The active production value setpoint are modified only for the generators participating to the slack buses (see :attr:`lightsim2grid.elements.GenInfo.is_slack` and :attr:`lightsim2grid.elements.GenInfo.slack_weight`). @@ -847,7 +847,8 @@ const std::string DocIterator::DataGen = R"mydelimiter( )mydelimiter"; const std::string DocIterator::GenInfo = R"mydelimiter( - This class represents what you get from retrieving some elements from :class:`lightsim2grid.elements.DataGen` + This class represents what you get from retrieving some elements from + :class:`lightsim2grid.elements.GeneratorContainer` It allows to read information from each generator of the powergrid. @@ -932,11 +933,12 @@ const std::string DocIterator::max_p_mw = R"mydelimiter( )mydelimiter"; -const std::string DocIterator::DataSGen = R"mydelimiter( +const std::string DocIterator::SGenContainer = R"mydelimiter( This class allows to iterate through the static generators of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. - In lightsim2grid they are two types of generators the more standard PV generators (see :attr:`lightsim2grid.elements.DataGen`). These + In lightsim2grid they are two types of generators the more standard PV generators (see + :attr:`lightsim2grid.elements.GeneratorContainer`). These are more exotic generators known as PQ, where you give the active production value and reactive production value. It's basically like loads, but using the generator convention (if the value is positive, it means power is taken from the grid to the element) @@ -972,7 +974,8 @@ const std::string DocIterator::DataSGen = R"mydelimiter( )mydelimiter"; const std::string DocIterator::SGenInfo = R"mydelimiter( - This class represents what you get from retrieving some elements from :class:`lightsim2grid.elements.DataSGen` + This class represents what you get from retrieving some elements from + :class:`lightsim2grid.elements.SGenContainer` It allows to read information from each static generator of the powergrid. @@ -1001,7 +1004,7 @@ const std::string DocIterator::SGenInfo = R"mydelimiter( )mydelimiter"; -const std::string DocIterator::DataLoad = R"mydelimiter( +const std::string DocIterator::LoadContainer = R"mydelimiter( This class allows to iterate through the loads **and storage units** of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. @@ -1049,7 +1052,8 @@ const std::string DocIterator::DataLoad = R"mydelimiter( )mydelimiter"; const std::string DocIterator::LoadInfo = R"mydelimiter( - This class represents what you get from retrieving some elements from :class:`lightsim2grid.elements.DataLoad`. + This class represents what you get from retrieving some elements from + :class:`lightsim2grid.elements.LoadContainer`. We remind the reader that storage units are also modeled as load in lightsim2grid. It allows to read information from each load / storage unit of the powergrid. @@ -1088,7 +1092,7 @@ const std::string DocIterator::LoadInfo = R"mydelimiter( )mydelimiter"; -const std::string DocIterator::DataShunt = R"mydelimiter( +const std::string DocIterator::ShuntContainer = R"mydelimiter( This class allows to iterate through the load of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. @@ -1122,7 +1126,8 @@ const std::string DocIterator::DataShunt = R"mydelimiter( )mydelimiter"; const std::string DocIterator::ShuntInfo = R"mydelimiter( - This class represents what you get from retrieving the shunts from :class:`lightsim2grid.elements.DataShunt`. + This class represents what you get from retrieving the shunts from + :class:`lightsim2grid.elements.ShuntContainer`. It allows to read information from each shunt of the powergrid. @@ -1150,7 +1155,7 @@ const std::string DocIterator::ShuntInfo = R"mydelimiter( )mydelimiter"; -const std::string DocIterator::DataTrafo = R"mydelimiter( +const std::string DocIterator::TrafoContainer = R"mydelimiter( This class allows to iterate through the transformers of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. @@ -1184,7 +1189,8 @@ const std::string DocIterator::DataTrafo = R"mydelimiter( )mydelimiter"; const std::string DocIterator::TrafoInfo = R"mydelimiter( - This class represents what you get from retrieving the transformers from :class:`lightsim2grid.elements.DataTrafo`. + This class represents what you get from retrieving the transformers from + :class:`lightsim2grid.elements.TrafoContainer`. It allows to read information from each transformer of the powergrid. @@ -1321,7 +1327,7 @@ const std::string DocIterator::res_a_hv_ka = R"mydelimiter( )mydelimiter" + DocIterator::only_avail_res; -const std::string DocIterator::DataLine = R"mydelimiter( +const std::string DocIterator::LineContainer = R"mydelimiter( This class allows to iterate through the powerlines of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. @@ -1355,7 +1361,8 @@ const std::string DocIterator::DataLine = R"mydelimiter( )mydelimiter"; const std::string DocIterator::LineInfo = R"mydelimiter( - This class represents what you get from retrieving the powerlines from :class:`lightsim2grid.elements.DataLine`. + This class represents what you get from retrieving the powerlines from + :class:`lightsim2grid.elements.LineContainer`. It allows to read information from each powerline of the powergrid. @@ -1475,7 +1482,7 @@ const std::string DocIterator::res_a_ex_ka = R"mydelimiter( )mydelimiter" + DocIterator::only_avail_res; -const std::string DocIterator::DataDCLine = R"mydelimiter( +const std::string DocIterator::DCLineContainer = R"mydelimiter( This class allows to iterate through the dc lines of the :class:`lightsim2grid.gridmodel.GridModel` easily, as if they were in a python list. @@ -1523,7 +1530,8 @@ const std::string DocIterator::DataDCLine = R"mydelimiter( const std::string DocIterator::DCLineInfo = R"mydelimiter( - This class represents what you get from retrieving the dc powerlines from :class:`lightsim2grid.elements.DataDCLine`. + This class represents what you get from retrieving the dc powerlines from + :class:`lightsim2grid.elements.DCLineContainer`. It allows to read information from each dc powerline of the powergrid. @@ -1789,7 +1797,8 @@ const std::string DocGridModel::get_dc_solver = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_lines = R"mydelimiter( - This function allows to retrieve the powerlines (as a :class:`lightsim2grid.elements.DataLine` object, + This function allows to retrieve the powerlines (as a + :class:`lightsim2grid.elements.LineContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1807,7 +1816,8 @@ const std::string DocGridModel::get_lines = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_trafos = R"mydelimiter( - This function allows to retrieve the transformers (as a :class:`lightsim2grid.elements.DataLine` object, + This function allows to retrieve the transformers (as a + :class:`lightsim2grid.elements.LineContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1825,7 +1835,8 @@ const std::string DocGridModel::get_trafos = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_generators = R"mydelimiter( - This function allows to retrieve the (standard) generators (as a :class:`lightsim2grid.elements.DataGen` object, + This function allows to retrieve the (standard) generators (as a + :class:`lightsim2grid.elements.GeneratorContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1843,7 +1854,8 @@ const std::string DocGridModel::get_generators = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_static_generators = R"mydelimiter( - This function allows to retrieve the (more exotic) static generators (as a :class:`lightsim2grid.elements.DataSGen` object, + This function allows to retrieve the (more exotic) static generators (as a + :class:`lightsim2grid.elements.SGenContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1861,7 +1873,8 @@ const std::string DocGridModel::get_static_generators = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_shunts = R"mydelimiter( - This function allows to retrieve the shunts (as a :class:`lightsim2grid.elements.DataShunt` object, + This function allows to retrieve the shunts (as a + :class:`lightsim2grid.elements.ShuntContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1879,12 +1892,13 @@ const std::string DocGridModel::get_shunts = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_storages = R"mydelimiter( - This function allows to retrieve the storage units (as a :class:`lightsim2grid.elements.DataLoad` object, + This function allows to retrieve the storage units (as a + :class:`lightsim2grid.elements.LoadContainer` object, see :ref:`elements-modeled` for more information) .. note:: We want to emphize that, as far as lightsim2grid is concerned, the storage units are modeled as loads. This is why - this function will return a :class:`lightsim2grid.elements.DataLoad`. + this function will return a :class:`lightsim2grid.elements.LoadContainer`. Examples --------- @@ -1901,7 +1915,7 @@ const std::string DocGridModel::get_storages = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_loads = R"mydelimiter( - This function allows to retrieve the loads (as a :class:`lightsim2grid.elements.DataLoad` object, + This function allows to retrieve the loads (as a :class:`lightsim2grid.elements.LoadContainer` object, see :ref:`elements-modeled` for more information) Examples @@ -1920,7 +1934,8 @@ const std::string DocGridModel::get_loads = R"mydelimiter( )mydelimiter"; const std::string DocGridModel::get_dclines = R"mydelimiter( - This function allows to retrieve the dc powerlines (as a :class:`lightsim2grid.elements.DataDCLine` object, + This function allows to retrieve the dc powerlines (as a + :class:`lightsim2grid.elements.DCLineContainer` object, see :ref:`elements-modeled` for more information) Examples diff --git a/src/help_fun_msg.h b/src/help_fun_msg.h index 3f503b1..cf98db4 100644 --- a/src/help_fun_msg.h +++ b/src/help_fun_msg.h @@ -90,25 +90,25 @@ struct DocIterator static const std::string h_pu; // specific to generators - static const std::string DataGen; + static const std::string GeneratorContainer; static const std::string GenInfo; static const std::string is_slack; static const std::string slack_weight; // specific to sgens - static const std::string DataSGen; + static const std::string SGenContainer; static const std::string SGenInfo; // specific to loads (and storage units) - static const std::string DataLoad; + static const std::string LoadContainer; static const std::string LoadInfo; // specific to shunts - static const std::string DataShunt; + static const std::string ShuntContainer; static const std::string ShuntInfo; // specific to transformers - static const std::string DataTrafo; + static const std::string TrafoContainer; static const std::string TrafoInfo; static const std::string bus_hv_id; static const std::string bus_lv_id; @@ -127,7 +127,7 @@ struct DocIterator static const std::string res_theta_lv_deg; // specific to powerlines - static const std::string DataLine; + static const std::string LineContainer; static const std::string LineInfo; static const std::string bus_or_id; static const std::string bus_ex_id; @@ -144,7 +144,7 @@ struct DocIterator // specific to dc lines static const std::string dc_line_formula; - static const std::string DataDCLine; + static const std::string DCLineContainer; static const std::string DCLineInfo; static const std::string target_p_or_mw; static const std::string target_vm_or_pu; diff --git a/src/CKTSOSolver.cpp b/src/linear_solvers/CKTSOSolver.cpp similarity index 100% rename from src/CKTSOSolver.cpp rename to src/linear_solvers/CKTSOSolver.cpp diff --git a/src/CKTSOSolver.h b/src/linear_solvers/CKTSOSolver.h similarity index 100% rename from src/CKTSOSolver.h rename to src/linear_solvers/CKTSOSolver.h diff --git a/src/KLUSolver.cpp b/src/linear_solvers/KLUSolver.cpp similarity index 100% rename from src/KLUSolver.cpp rename to src/linear_solvers/KLUSolver.cpp diff --git a/src/KLUSolver.h b/src/linear_solvers/KLUSolver.h similarity index 100% rename from src/KLUSolver.h rename to src/linear_solvers/KLUSolver.h diff --git a/src/NICSLUSolver.cpp b/src/linear_solvers/NICSLUSolver.cpp similarity index 100% rename from src/NICSLUSolver.cpp rename to src/linear_solvers/NICSLUSolver.cpp diff --git a/src/NICSLUSolver.h b/src/linear_solvers/NICSLUSolver.h similarity index 100% rename from src/NICSLUSolver.h rename to src/linear_solvers/NICSLUSolver.h diff --git a/src/SparseLUSolver.cpp b/src/linear_solvers/SparseLUSolver.cpp similarity index 100% rename from src/SparseLUSolver.cpp rename to src/linear_solvers/SparseLUSolver.cpp diff --git a/src/SparseLUSolver.h b/src/linear_solvers/SparseLUSolver.h similarity index 100% rename from src/SparseLUSolver.h rename to src/linear_solvers/SparseLUSolver.h diff --git a/src/main.cpp b/src/main.cpp index a743bba..453e3d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -349,31 +349,31 @@ PYBIND11_MODULE(lightsim2grid_cpp, m) #endif // CKTSO_SOLVER_AVAILABLE (or _READ_THE_DOCS) - py::class_(m, "GaussSeidelSolver", DocSolver::GaussSeidelSolver.c_str()) + py::class_(m, "GaussSeidelSolver", DocSolver::GaussSeidelSolver.c_str()) .def(py::init<>()) - .def("get_Va", &GaussSeidelSolver::get_Va, DocSolver::get_Va.c_str()) // get the voltage angle vector (vector of double) - .def("get_Vm", &GaussSeidelSolver::get_Vm, DocSolver::get_Vm.c_str()) // get the voltage magnitude vector (vector of double) - .def("get_V", &GaussSeidelSolver::get_V, DocSolver::get_V.c_str()) - .def("get_error", &GaussSeidelSolver::get_error, DocSolver::get_error.c_str()) // get the error message, see the definition of "err_" for more information - .def("get_nb_iter", &GaussSeidelSolver::get_nb_iter, DocSolver::get_nb_iter.c_str()) // return the number of iteration performed at the last optimization - .def("reset", &GaussSeidelSolver::reset, DocSolver::reset.c_str()) // reset the solver to its original state - .def("converged", &GaussSeidelSolver::converged, DocSolver::converged.c_str()) // whether the solver has converged - .def("compute_pf", &GaussSeidelSolver::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()) // compute the powerflow - .def("get_timers", &GaussSeidelSolver::get_timers, DocSolver::get_timers.c_str()) // returns the timers corresponding to times the solver spent in different part - .def("solve", &GaussSeidelSolver::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()); // perform the newton raphson optimization - - py::class_(m, "GaussSeidelSynchSolver", DocSolver::GaussSeidelSynchSolver.c_str()) + .def("get_Va", &GaussSeidelAlgo::get_Va, DocSolver::get_Va.c_str()) // get the voltage angle vector (vector of double) + .def("get_Vm", &GaussSeidelAlgo::get_Vm, DocSolver::get_Vm.c_str()) // get the voltage magnitude vector (vector of double) + .def("get_V", &GaussSeidelAlgo::get_V, DocSolver::get_V.c_str()) + .def("get_error", &GaussSeidelAlgo::get_error, DocSolver::get_error.c_str()) // get the error message, see the definition of "err_" for more information + .def("get_nb_iter", &GaussSeidelAlgo::get_nb_iter, DocSolver::get_nb_iter.c_str()) // return the number of iteration performed at the last optimization + .def("reset", &GaussSeidelAlgo::reset, DocSolver::reset.c_str()) // reset the solver to its original state + .def("converged", &GaussSeidelAlgo::converged, DocSolver::converged.c_str()) // whether the solver has converged + .def("compute_pf", &GaussSeidelAlgo::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()) // compute the powerflow + .def("get_timers", &GaussSeidelAlgo::get_timers, DocSolver::get_timers.c_str()) // returns the timers corresponding to times the solver spent in different part + .def("solve", &GaussSeidelAlgo::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()); // perform the newton raphson optimization + + py::class_(m, "GaussSeidelSynchSolver", DocSolver::GaussSeidelSynchSolver.c_str()) .def(py::init<>()) - .def("get_Va", &GaussSeidelSynchSolver::get_Va, DocSolver::get_Va.c_str()) // get the voltage angle vector (vector of double) - .def("get_Vm", &GaussSeidelSynchSolver::get_Vm, DocSolver::get_Vm.c_str()) // get the voltage magnitude vector (vector of double) - .def("get_V", &GaussSeidelSynchSolver::get_V, DocSolver::get_V.c_str()) - .def("get_error", &GaussSeidelSynchSolver::get_error, DocSolver::get_error.c_str()) // get the error message, see the definition of "err_" for more information - .def("get_nb_iter", &GaussSeidelSynchSolver::get_nb_iter, DocSolver::get_nb_iter.c_str()) // return the number of iteration performed at the last optimization - .def("reset", &GaussSeidelSynchSolver::reset, DocSolver::reset.c_str()) // reset the solver to its original state - .def("converged", &GaussSeidelSynchSolver::converged, DocSolver::converged.c_str()) // whether the solver has converged - .def("compute_pf", &GaussSeidelSynchSolver::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()) // compute the powerflow - .def("get_timers", &GaussSeidelSynchSolver::get_timers, DocSolver::get_timers.c_str()) // returns the timers corresponding to times the solver spent in different part - .def("solve", &GaussSeidelSynchSolver::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()); // perform the newton raphson optimization + .def("get_Va", &GaussSeidelSynchAlgo::get_Va, DocSolver::get_Va.c_str()) // get the voltage angle vector (vector of double) + .def("get_Vm", &GaussSeidelSynchAlgo::get_Vm, DocSolver::get_Vm.c_str()) // get the voltage magnitude vector (vector of double) + .def("get_V", &GaussSeidelSynchAlgo::get_V, DocSolver::get_V.c_str()) + .def("get_error", &GaussSeidelSynchAlgo::get_error, DocSolver::get_error.c_str()) // get the error message, see the definition of "err_" for more information + .def("get_nb_iter", &GaussSeidelSynchAlgo::get_nb_iter, DocSolver::get_nb_iter.c_str()) // return the number of iteration performed at the last optimization + .def("reset", &GaussSeidelSynchAlgo::reset, DocSolver::reset.c_str()) // reset the solver to its original state + .def("converged", &GaussSeidelSynchAlgo::converged, DocSolver::converged.c_str()) // whether the solver has converged + .def("compute_pf", &GaussSeidelSynchAlgo::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()) // compute the powerflow + .def("get_timers", &GaussSeidelSynchAlgo::get_timers, DocSolver::get_timers.c_str()) // returns the timers corresponding to times the solver spent in different part + .def("solve", &GaussSeidelSynchAlgo::compute_pf, py::call_guard(), DocSolver::compute_pf.c_str()); // perform the newton raphson optimization // Only "const" method are exported // it is so that i cannot modify the internal solver of a gridmodel python side @@ -396,192 +396,192 @@ PYBIND11_MODULE(lightsim2grid_cpp, m) .def("get_fdpf_bx_lu", &ChooseSolver::get_fdpf_bx_lu, py::return_value_policy::reference, DocGridModel::_internal_do_not_use.c_str()); // iterator for generators - py::class_(m, "DataGen", DocIterator::DataGen.c_str()) - .def("__len__", [](const DataGen & data) { return data.nb(); }) - .def("__getitem__", [](const DataGen & data, int k){return data[k]; } ) - .def("__iter__", [](const DataGen & data) { + py::class_(m, "GeneratorContainer", DocIterator::GeneratorContainer.c_str()) + .def("__len__", [](const GeneratorContainer & data) { return data.nb(); }) + .def("__getitem__", [](const GeneratorContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const GeneratorContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "GenInfo", DocIterator::GenInfo.c_str()) - .def_readonly("id", &DataGen::GenInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataGen::GenInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataGen::GenInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_id", &DataGen::GenInfo::bus_id, DocIterator::bus_id.c_str()) - .def_readonly("is_slack", &DataGen::GenInfo::is_slack, DocIterator::is_slack.c_str()) - .def_readonly("slack_weight", &DataGen::GenInfo::slack_weight, DocIterator::slack_weight.c_str()) - .def_readonly("voltage_regulator_on", &DataGen::GenInfo::voltage_regulator_on, "TODO") - .def_readonly("target_p_mw", &DataGen::GenInfo::target_p_mw, DocIterator::target_p_mw.c_str()) - .def_readonly("target_vm_pu", &DataGen::GenInfo::target_vm_pu, DocIterator::target_vm_pu.c_str()) - .def_readonly("target_q_mvar", &DataGen::GenInfo::target_q_mvar, "TODO") - .def_readonly("min_q_mvar", &DataGen::GenInfo::min_q_mvar, DocIterator::min_q_mvar.c_str()) - .def_readonly("max_q_mvar", &DataGen::GenInfo::max_q_mvar, DocIterator::max_q_mvar.c_str()) - .def_readonly("has_res", &DataGen::GenInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_mw", &DataGen::GenInfo::res_p_mw, DocIterator::res_p_mw.c_str()) - .def_readonly("res_q_mvar", &DataGen::GenInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) - .def_readonly("res_theta_deg", &DataGen::GenInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) - .def_readonly("res_v_kv", &DataGen::GenInfo::res_v_kv, DocIterator::res_v_kv.c_str()); + py::class_(m, "GenInfo", DocIterator::GenInfo.c_str()) + .def_readonly("id", &GeneratorContainer::GenInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &GeneratorContainer::GenInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &GeneratorContainer::GenInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_id", &GeneratorContainer::GenInfo::bus_id, DocIterator::bus_id.c_str()) + .def_readonly("is_slack", &GeneratorContainer::GenInfo::is_slack, DocIterator::is_slack.c_str()) + .def_readonly("slack_weight", &GeneratorContainer::GenInfo::slack_weight, DocIterator::slack_weight.c_str()) + .def_readonly("voltage_regulator_on", &GeneratorContainer::GenInfo::voltage_regulator_on, "TODO") + .def_readonly("target_p_mw", &GeneratorContainer::GenInfo::target_p_mw, DocIterator::target_p_mw.c_str()) + .def_readonly("target_vm_pu", &GeneratorContainer::GenInfo::target_vm_pu, DocIterator::target_vm_pu.c_str()) + .def_readonly("target_q_mvar", &GeneratorContainer::GenInfo::target_q_mvar, "TODO") + .def_readonly("min_q_mvar", &GeneratorContainer::GenInfo::min_q_mvar, DocIterator::min_q_mvar.c_str()) + .def_readonly("max_q_mvar", &GeneratorContainer::GenInfo::max_q_mvar, DocIterator::max_q_mvar.c_str()) + .def_readonly("has_res", &GeneratorContainer::GenInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_mw", &GeneratorContainer::GenInfo::res_p_mw, DocIterator::res_p_mw.c_str()) + .def_readonly("res_q_mvar", &GeneratorContainer::GenInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) + .def_readonly("res_theta_deg", &GeneratorContainer::GenInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) + .def_readonly("res_v_kv", &GeneratorContainer::GenInfo::res_v_kv, DocIterator::res_v_kv.c_str()); // iterator for sgens - py::class_(m, "DataSGen", DocIterator::DataSGen.c_str()) - .def("__len__", [](const DataSGen & data) { return data.nb(); }) - .def("__getitem__", [](const DataSGen & data, int k){return data[k]; } ) - .def("__iter__", [](const DataSGen & data) { + py::class_(m, "SGenContainer", DocIterator::SGenContainer.c_str()) + .def("__len__", [](const SGenContainer & data) { return data.nb(); }) + .def("__getitem__", [](const SGenContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const SGenContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "SGenInfo", DocIterator::SGenInfo.c_str()) - .def_readonly("id", &DataSGen::SGenInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataSGen::SGenInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataSGen::SGenInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_id", &DataSGen::SGenInfo::bus_id, DocIterator::bus_id.c_str()) - .def_readonly("min_q_mvar", &DataSGen::SGenInfo::min_q_mvar, DocIterator::min_q_mvar.c_str()) - .def_readonly("max_q_mvar", &DataSGen::SGenInfo::max_q_mvar, DocIterator::max_q_mvar.c_str()) - .def_readonly("min_p_mw", &DataSGen::SGenInfo::min_p_mw, DocIterator::min_p_mw.c_str()) - .def_readonly("max_p_mw", &DataSGen::SGenInfo::max_p_mw, DocIterator::max_p_mw.c_str()) - .def_readonly("target_p_mw", &DataSGen::SGenInfo::target_p_mw, DocIterator::target_p_mw.c_str()) - .def_readonly("target_q_mvar", &DataSGen::SGenInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) - .def_readonly("has_res", &DataSGen::SGenInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_mw", &DataSGen::SGenInfo::res_p_mw, DocIterator::res_p_mw.c_str()) - .def_readonly("res_q_mvar", &DataSGen::SGenInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) - .def_readonly("res_theta_deg", &DataSGen::SGenInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) - .def_readonly("res_v_kv", &DataSGen::SGenInfo::res_v_kv, DocIterator::res_v_kv.c_str()); + py::class_(m, "SGenInfo", DocIterator::SGenInfo.c_str()) + .def_readonly("id", &SGenContainer::SGenInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &SGenContainer::SGenInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &SGenContainer::SGenInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_id", &SGenContainer::SGenInfo::bus_id, DocIterator::bus_id.c_str()) + .def_readonly("min_q_mvar", &SGenContainer::SGenInfo::min_q_mvar, DocIterator::min_q_mvar.c_str()) + .def_readonly("max_q_mvar", &SGenContainer::SGenInfo::max_q_mvar, DocIterator::max_q_mvar.c_str()) + .def_readonly("min_p_mw", &SGenContainer::SGenInfo::min_p_mw, DocIterator::min_p_mw.c_str()) + .def_readonly("max_p_mw", &SGenContainer::SGenInfo::max_p_mw, DocIterator::max_p_mw.c_str()) + .def_readonly("target_p_mw", &SGenContainer::SGenInfo::target_p_mw, DocIterator::target_p_mw.c_str()) + .def_readonly("target_q_mvar", &SGenContainer::SGenInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) + .def_readonly("has_res", &SGenContainer::SGenInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_mw", &SGenContainer::SGenInfo::res_p_mw, DocIterator::res_p_mw.c_str()) + .def_readonly("res_q_mvar", &SGenContainer::SGenInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) + .def_readonly("res_theta_deg", &SGenContainer::SGenInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) + .def_readonly("res_v_kv", &SGenContainer::SGenInfo::res_v_kv, DocIterator::res_v_kv.c_str()); // iterator for loads (and storage units) - py::class_(m, "DataLoad", DocIterator::DataLoad.c_str()) - .def("__len__", [](const DataLoad & data) { return data.nb(); }) - .def("__getitem__", [](const DataLoad & data, int k){return data[k]; } ) - .def("__iter__", [](const DataLoad & data) { + py::class_(m, "LoadContainer", DocIterator::LoadContainer.c_str()) + .def("__len__", [](const LoadContainer & data) { return data.nb(); }) + .def("__getitem__", [](const LoadContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const LoadContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "LoadInfo", DocIterator::LoadInfo.c_str()) - .def_readonly("id", &DataLoad::LoadInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataLoad::LoadInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataLoad::LoadInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_id", &DataLoad::LoadInfo::bus_id, DocIterator::bus_id.c_str()) - .def_readonly("target_p_mw", &DataLoad::LoadInfo::target_p_mw, DocIterator::target_p_mw.c_str()) - .def_readonly("target_q_mvar", &DataLoad::LoadInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) - .def_readonly("has_res", &DataLoad::LoadInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_mw", &DataLoad::LoadInfo::res_p_mw, DocIterator::res_p_mw.c_str()) - .def_readonly("res_q_mvar", &DataLoad::LoadInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) - .def_readonly("res_theta_deg", &DataLoad::LoadInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) - .def_readonly("res_v_kv", &DataLoad::LoadInfo::res_v_kv, DocIterator::res_v_kv.c_str()); + py::class_(m, "LoadInfo", DocIterator::LoadInfo.c_str()) + .def_readonly("id", &LoadContainer::LoadInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &LoadContainer::LoadInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &LoadContainer::LoadInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_id", &LoadContainer::LoadInfo::bus_id, DocIterator::bus_id.c_str()) + .def_readonly("target_p_mw", &LoadContainer::LoadInfo::target_p_mw, DocIterator::target_p_mw.c_str()) + .def_readonly("target_q_mvar", &LoadContainer::LoadInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) + .def_readonly("has_res", &LoadContainer::LoadInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_mw", &LoadContainer::LoadInfo::res_p_mw, DocIterator::res_p_mw.c_str()) + .def_readonly("res_q_mvar", &LoadContainer::LoadInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) + .def_readonly("res_theta_deg", &LoadContainer::LoadInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) + .def_readonly("res_v_kv", &LoadContainer::LoadInfo::res_v_kv, DocIterator::res_v_kv.c_str()); // iterator for shunts - py::class_(m, "DataShunt", DocIterator::DataShunt.c_str()) - .def("__len__", [](const DataShunt & data) { return data.nb(); }) - .def("__getitem__", [](const DataShunt & data, int k){return data[k]; } ) - .def("__iter__", [](const DataShunt & data) { + py::class_(m, "ShuntContainer", DocIterator::ShuntContainer.c_str()) + .def("__len__", [](const ShuntContainer & data) { return data.nb(); }) + .def("__getitem__", [](const ShuntContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const ShuntContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "ShuntInfo", DocIterator::ShuntInfo.c_str()) - .def_readonly("id", &DataShunt::ShuntInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataShunt::ShuntInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataShunt::ShuntInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_id", &DataShunt::ShuntInfo::bus_id, DocIterator::bus_id.c_str()) - .def_readonly("target_p_mw", &DataShunt::ShuntInfo::target_p_mw, DocIterator::target_p_mw.c_str()) - .def_readonly("target_q_mvar", &DataShunt::ShuntInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) - .def_readonly("has_res", &DataShunt::ShuntInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_mw", &DataShunt::ShuntInfo::res_p_mw, DocIterator::res_p_mw.c_str()) - .def_readonly("res_q_mvar", &DataShunt::ShuntInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) - .def_readonly("res_theta_deg", &DataShunt::ShuntInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) - .def_readonly("res_v_kv", &DataShunt::ShuntInfo::res_v_kv, DocIterator::res_v_kv.c_str()); + py::class_(m, "ShuntInfo", DocIterator::ShuntInfo.c_str()) + .def_readonly("id", &ShuntContainer::ShuntInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &ShuntContainer::ShuntInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &ShuntContainer::ShuntInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_id", &ShuntContainer::ShuntInfo::bus_id, DocIterator::bus_id.c_str()) + .def_readonly("target_p_mw", &ShuntContainer::ShuntInfo::target_p_mw, DocIterator::target_p_mw.c_str()) + .def_readonly("target_q_mvar", &ShuntContainer::ShuntInfo::target_q_mvar, DocIterator::target_q_mvar.c_str()) + .def_readonly("has_res", &ShuntContainer::ShuntInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_mw", &ShuntContainer::ShuntInfo::res_p_mw, DocIterator::res_p_mw.c_str()) + .def_readonly("res_q_mvar", &ShuntContainer::ShuntInfo::res_q_mvar, DocIterator::res_q_mvar.c_str()) + .def_readonly("res_theta_deg", &ShuntContainer::ShuntInfo::res_theta_deg, DocIterator::res_theta_deg.c_str()) + .def_readonly("res_v_kv", &ShuntContainer::ShuntInfo::res_v_kv, DocIterator::res_v_kv.c_str()); // iterator for trafos - py::class_(m, "DataTrafo", DocIterator::DataTrafo.c_str()) - .def("__len__", [](const DataTrafo & data) { return data.nb(); }) - .def("__getitem__", [](const DataTrafo & data, int k){return data[k]; } ) - .def("__iter__", [](const DataTrafo & data) { + py::class_(m, "TrafoContainer", DocIterator::TrafoContainer.c_str()) + .def("__len__", [](const TrafoContainer & data) { return data.nb(); }) + .def("__getitem__", [](const TrafoContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const TrafoContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "TrafoInfo", DocIterator::TrafoInfo.c_str()) - .def_readonly("id", &DataTrafo::TrafoInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataTrafo::TrafoInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataTrafo::TrafoInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_hv_id", &DataTrafo::TrafoInfo::bus_hv_id, DocIterator::bus_hv_id.c_str()) - .def_readonly("bus_lv_id", &DataTrafo::TrafoInfo::bus_lv_id, DocIterator::bus_lv_id.c_str()) - .def_readonly("r_pu", &DataTrafo::TrafoInfo::r_pu, DocIterator::r_pu.c_str()) - .def_readonly("x_pu", &DataTrafo::TrafoInfo::x_pu, DocIterator::x_pu.c_str()) - .def_readonly("h_pu", &DataTrafo::TrafoInfo::h_pu, DocIterator::h_pu.c_str()) - .def_readonly("is_tap_hv_side", &DataTrafo::TrafoInfo::is_tap_hv_side, DocIterator::is_tap_hv_side.c_str()) - .def_readonly("ratio", &DataTrafo::TrafoInfo::ratio, DocIterator::ratio.c_str()) - .def_readonly("shift_rad", &DataTrafo::TrafoInfo::shift_rad, DocIterator::shift_rad.c_str()) - .def_readonly("has_res", &DataTrafo::TrafoInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_hv_mw", &DataTrafo::TrafoInfo::res_p_hv_mw, DocIterator::res_p_hv_mw.c_str()) - .def_readonly("res_q_hv_mvar", &DataTrafo::TrafoInfo::res_q_hv_mvar, DocIterator::res_q_hv_mvar.c_str()) - .def_readonly("res_v_hv_kv", &DataTrafo::TrafoInfo::res_v_hv_kv, DocIterator::res_v_hv_kv.c_str()) - .def_readonly("res_a_hv_ka", &DataTrafo::TrafoInfo::res_a_hv_ka, DocIterator::res_a_hv_ka.c_str()) - .def_readonly("res_p_lv_mw", &DataTrafo::TrafoInfo::res_p_lv_mw, DocIterator::res_p_lv_mw.c_str()) - .def_readonly("res_q_lv_mvar", &DataTrafo::TrafoInfo::res_q_lv_mvar, DocIterator::res_q_lv_mvar.c_str()) - .def_readonly("res_v_lv_kv", &DataTrafo::TrafoInfo::res_v_lv_kv, DocIterator::res_v_lv_kv.c_str()) - .def_readonly("res_a_lv_ka", &DataTrafo::TrafoInfo::res_a_lv_ka, DocIterator::res_a_lv_ka.c_str()) - .def_readonly("res_theta_hv_deg", &DataTrafo::TrafoInfo::res_theta_hv_deg, DocIterator::res_theta_hv_deg.c_str()) - .def_readonly("res_theta_lv_deg", &DataTrafo::TrafoInfo::res_theta_lv_deg, DocIterator::res_theta_lv_deg.c_str()); + py::class_(m, "TrafoInfo", DocIterator::TrafoInfo.c_str()) + .def_readonly("id", &TrafoContainer::TrafoInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &TrafoContainer::TrafoInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &TrafoContainer::TrafoInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_hv_id", &TrafoContainer::TrafoInfo::bus_hv_id, DocIterator::bus_hv_id.c_str()) + .def_readonly("bus_lv_id", &TrafoContainer::TrafoInfo::bus_lv_id, DocIterator::bus_lv_id.c_str()) + .def_readonly("r_pu", &TrafoContainer::TrafoInfo::r_pu, DocIterator::r_pu.c_str()) + .def_readonly("x_pu", &TrafoContainer::TrafoInfo::x_pu, DocIterator::x_pu.c_str()) + .def_readonly("h_pu", &TrafoContainer::TrafoInfo::h_pu, DocIterator::h_pu.c_str()) + .def_readonly("is_tap_hv_side", &TrafoContainer::TrafoInfo::is_tap_hv_side, DocIterator::is_tap_hv_side.c_str()) + .def_readonly("ratio", &TrafoContainer::TrafoInfo::ratio, DocIterator::ratio.c_str()) + .def_readonly("shift_rad", &TrafoContainer::TrafoInfo::shift_rad, DocIterator::shift_rad.c_str()) + .def_readonly("has_res", &TrafoContainer::TrafoInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_hv_mw", &TrafoContainer::TrafoInfo::res_p_hv_mw, DocIterator::res_p_hv_mw.c_str()) + .def_readonly("res_q_hv_mvar", &TrafoContainer::TrafoInfo::res_q_hv_mvar, DocIterator::res_q_hv_mvar.c_str()) + .def_readonly("res_v_hv_kv", &TrafoContainer::TrafoInfo::res_v_hv_kv, DocIterator::res_v_hv_kv.c_str()) + .def_readonly("res_a_hv_ka", &TrafoContainer::TrafoInfo::res_a_hv_ka, DocIterator::res_a_hv_ka.c_str()) + .def_readonly("res_p_lv_mw", &TrafoContainer::TrafoInfo::res_p_lv_mw, DocIterator::res_p_lv_mw.c_str()) + .def_readonly("res_q_lv_mvar", &TrafoContainer::TrafoInfo::res_q_lv_mvar, DocIterator::res_q_lv_mvar.c_str()) + .def_readonly("res_v_lv_kv", &TrafoContainer::TrafoInfo::res_v_lv_kv, DocIterator::res_v_lv_kv.c_str()) + .def_readonly("res_a_lv_ka", &TrafoContainer::TrafoInfo::res_a_lv_ka, DocIterator::res_a_lv_ka.c_str()) + .def_readonly("res_theta_hv_deg", &TrafoContainer::TrafoInfo::res_theta_hv_deg, DocIterator::res_theta_hv_deg.c_str()) + .def_readonly("res_theta_lv_deg", &TrafoContainer::TrafoInfo::res_theta_lv_deg, DocIterator::res_theta_lv_deg.c_str()); // iterator for trafos - py::class_(m, "DataLine", DocIterator::DataLine.c_str()) - .def("__len__", [](const DataLine & data) { return data.nb(); }) - .def("__getitem__", [](const DataLine & data, int k){return data[k]; } ) - .def("__iter__", [](const DataLine & data) { + py::class_(m, "LineContainer", DocIterator::LineContainer.c_str()) + .def("__len__", [](const LineContainer & data) { return data.nb(); }) + .def("__getitem__", [](const LineContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const LineContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "LineInfo", DocIterator::LineInfo.c_str()) - .def_readonly("id", &DataLine::LineInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataLine::LineInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataLine::LineInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_or_id", &DataLine::LineInfo::bus_or_id, DocIterator::bus_or_id.c_str()) - .def_readonly("bus_ex_id", &DataLine::LineInfo::bus_ex_id, DocIterator::bus_ex_id.c_str()) - .def_readonly("r_pu", &DataLine::LineInfo::r_pu, DocIterator::r_pu.c_str()) - .def_readonly("x_pu", &DataLine::LineInfo::x_pu, DocIterator::x_pu.c_str()) - .def_readonly("h_pu", &DataLine::LineInfo::h_pu, DocIterator::x_pu.c_str()) - .def_readonly("h_or_pu", &DataLine::LineInfo::h_or_pu, DocIterator::h_pu.c_str()) - .def_readonly("h_ex_pu", &DataLine::LineInfo::h_ex_pu, DocIterator::h_pu.c_str()) - .def_readonly("has_res", &DataLine::LineInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_or_mw", &DataLine::LineInfo::res_p_or_mw, DocIterator::res_p_or_mw.c_str()) - .def_readonly("res_q_or_mvar", &DataLine::LineInfo::res_q_or_mvar, DocIterator::res_q_or_mvar.c_str()) - .def_readonly("res_v_or_kv", &DataLine::LineInfo::res_v_or_kv, DocIterator::res_v_or_kv.c_str()) - .def_readonly("res_a_or_ka", &DataLine::LineInfo::res_a_or_ka, DocIterator::res_a_or_ka.c_str()) - .def_readonly("res_p_ex_mw", &DataLine::LineInfo::res_p_ex_mw, DocIterator::res_p_ex_mw.c_str()) - .def_readonly("res_q_ex_mvar", &DataLine::LineInfo::res_q_ex_mvar, DocIterator::res_q_ex_mvar.c_str()) - .def_readonly("res_v_ex_kv", &DataLine::LineInfo::res_v_ex_kv, DocIterator::res_v_ex_kv.c_str()) - .def_readonly("res_a_ex_ka", &DataLine::LineInfo::res_a_ex_ka, DocIterator::res_a_ex_ka.c_str()) - .def_readonly("res_theta_or_deg", &DataLine::LineInfo::res_theta_or_deg, DocIterator::res_theta_or_deg.c_str()) - .def_readonly("res_theta_ex_deg", &DataLine::LineInfo::res_theta_ex_deg, DocIterator::res_theta_ex_deg.c_str()); + py::class_(m, "LineInfo", DocIterator::LineInfo.c_str()) + .def_readonly("id", &LineContainer::LineInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &LineContainer::LineInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &LineContainer::LineInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_or_id", &LineContainer::LineInfo::bus_or_id, DocIterator::bus_or_id.c_str()) + .def_readonly("bus_ex_id", &LineContainer::LineInfo::bus_ex_id, DocIterator::bus_ex_id.c_str()) + .def_readonly("r_pu", &LineContainer::LineInfo::r_pu, DocIterator::r_pu.c_str()) + .def_readonly("x_pu", &LineContainer::LineInfo::x_pu, DocIterator::x_pu.c_str()) + .def_readonly("h_pu", &LineContainer::LineInfo::h_pu, DocIterator::x_pu.c_str()) + .def_readonly("h_or_pu", &LineContainer::LineInfo::h_or_pu, DocIterator::h_pu.c_str()) + .def_readonly("h_ex_pu", &LineContainer::LineInfo::h_ex_pu, DocIterator::h_pu.c_str()) + .def_readonly("has_res", &LineContainer::LineInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_or_mw", &LineContainer::LineInfo::res_p_or_mw, DocIterator::res_p_or_mw.c_str()) + .def_readonly("res_q_or_mvar", &LineContainer::LineInfo::res_q_or_mvar, DocIterator::res_q_or_mvar.c_str()) + .def_readonly("res_v_or_kv", &LineContainer::LineInfo::res_v_or_kv, DocIterator::res_v_or_kv.c_str()) + .def_readonly("res_a_or_ka", &LineContainer::LineInfo::res_a_or_ka, DocIterator::res_a_or_ka.c_str()) + .def_readonly("res_p_ex_mw", &LineContainer::LineInfo::res_p_ex_mw, DocIterator::res_p_ex_mw.c_str()) + .def_readonly("res_q_ex_mvar", &LineContainer::LineInfo::res_q_ex_mvar, DocIterator::res_q_ex_mvar.c_str()) + .def_readonly("res_v_ex_kv", &LineContainer::LineInfo::res_v_ex_kv, DocIterator::res_v_ex_kv.c_str()) + .def_readonly("res_a_ex_ka", &LineContainer::LineInfo::res_a_ex_ka, DocIterator::res_a_ex_ka.c_str()) + .def_readonly("res_theta_or_deg", &LineContainer::LineInfo::res_theta_or_deg, DocIterator::res_theta_or_deg.c_str()) + .def_readonly("res_theta_ex_deg", &LineContainer::LineInfo::res_theta_ex_deg, DocIterator::res_theta_ex_deg.c_str()); // iterator for dc lines - py::class_(m, "DataDCLine", DocIterator::DataDCLine.c_str()) - .def("__len__", [](const DataDCLine & data) { return data.nb(); }) - .def("__getitem__", [](const DataDCLine & data, int k){return data[k]; } ) - .def("__iter__", [](const DataDCLine & data) { + py::class_(m, "DCLineContainer", DocIterator::DCLineContainer.c_str()) + .def("__len__", [](const DCLineContainer & data) { return data.nb(); }) + .def("__getitem__", [](const DCLineContainer & data, int k){return data[k]; } ) + .def("__iter__", [](const DCLineContainer & data) { return py::make_iterator(data.begin(), data.end()); }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */ - py::class_(m, "DCLineInfo", DocIterator::DCLineInfo.c_str()) - .def_readonly("id", &DataDCLine::DCLineInfo::id, DocIterator::id.c_str()) - .def_readonly("name", &DataDCLine::DCLineInfo::name, DocIterator::name.c_str()) - .def_readonly("connected", &DataDCLine::DCLineInfo::connected, DocIterator::connected.c_str()) - .def_readonly("bus_or_id", &DataDCLine::DCLineInfo::bus_or_id, DocIterator::bus_or_id.c_str()) - .def_readonly("bus_ex_id", &DataDCLine::DCLineInfo::bus_ex_id, DocIterator::bus_ex_id.c_str()) - .def_readonly("target_p_or_mw", &DataDCLine::DCLineInfo::target_p_or_mw, DocIterator::target_p_or_mw.c_str()) - .def_readonly("target_vm_or_pu", &DataDCLine::DCLineInfo::target_vm_or_pu, DocIterator::target_vm_or_pu.c_str()) - .def_readonly("target_vm_ex_pu", &DataDCLine::DCLineInfo::target_vm_ex_pu, DocIterator::target_vm_ex_pu.c_str()) - .def_readonly("loss_pct", &DataDCLine::DCLineInfo::loss_pct, DocIterator::loss_pct.c_str()) - .def_readonly("loss_mw", &DataDCLine::DCLineInfo::loss_mw, DocIterator::loss_mw.c_str()) - .def_readonly("gen_or", &DataDCLine::DCLineInfo::gen_or, DocIterator::gen_or.c_str()) - .def_readonly("gen_ex", &DataDCLine::DCLineInfo::gen_ex, DocIterator::gen_ex.c_str()) - .def_readonly("has_res", &DataDCLine::DCLineInfo::has_res, DocIterator::has_res.c_str()) - .def_readonly("res_p_or_mw", &DataDCLine::DCLineInfo::res_p_or_mw, DocIterator::res_p_or_mw_dcline.c_str()) - .def_readonly("res_p_ex_mw", &DataDCLine::DCLineInfo::res_p_ex_mw, DocIterator::res_p_ex_mw_dcline.c_str()) - .def_readonly("res_q_or_mvar", &DataDCLine::DCLineInfo::res_q_or_mvar, DocIterator::res_q_or_mvar_dcline.c_str()) - .def_readonly("res_q_ex_mvar", &DataDCLine::DCLineInfo::res_q_ex_mvar, DocIterator::res_q_ex_mvar_dcline.c_str()) - .def_readonly("res_v_or_kv", &DataDCLine::DCLineInfo::res_v_or_kv, DocIterator::res_v_or_kv_dcline.c_str()) - .def_readonly("res_v_ex_kv", &DataDCLine::DCLineInfo::res_v_ex_kv, DocIterator::res_v_ex_kv_dcline.c_str()) - .def_readonly("res_theta_or_deg", &DataDCLine::DCLineInfo::res_theta_or_deg, DocIterator::res_theta_or_deg_dcline.c_str()) - .def_readonly("res_theta_ex_deg", &DataDCLine::DCLineInfo::res_theta_ex_deg, DocIterator::res_theta_ex_deg_dcline.c_str()) + py::class_(m, "DCLineInfo", DocIterator::DCLineInfo.c_str()) + .def_readonly("id", &DCLineContainer::DCLineInfo::id, DocIterator::id.c_str()) + .def_readonly("name", &DCLineContainer::DCLineInfo::name, DocIterator::name.c_str()) + .def_readonly("connected", &DCLineContainer::DCLineInfo::connected, DocIterator::connected.c_str()) + .def_readonly("bus_or_id", &DCLineContainer::DCLineInfo::bus_or_id, DocIterator::bus_or_id.c_str()) + .def_readonly("bus_ex_id", &DCLineContainer::DCLineInfo::bus_ex_id, DocIterator::bus_ex_id.c_str()) + .def_readonly("target_p_or_mw", &DCLineContainer::DCLineInfo::target_p_or_mw, DocIterator::target_p_or_mw.c_str()) + .def_readonly("target_vm_or_pu", &DCLineContainer::DCLineInfo::target_vm_or_pu, DocIterator::target_vm_or_pu.c_str()) + .def_readonly("target_vm_ex_pu", &DCLineContainer::DCLineInfo::target_vm_ex_pu, DocIterator::target_vm_ex_pu.c_str()) + .def_readonly("loss_pct", &DCLineContainer::DCLineInfo::loss_pct, DocIterator::loss_pct.c_str()) + .def_readonly("loss_mw", &DCLineContainer::DCLineInfo::loss_mw, DocIterator::loss_mw.c_str()) + .def_readonly("gen_or", &DCLineContainer::DCLineInfo::gen_or, DocIterator::gen_or.c_str()) + .def_readonly("gen_ex", &DCLineContainer::DCLineInfo::gen_ex, DocIterator::gen_ex.c_str()) + .def_readonly("has_res", &DCLineContainer::DCLineInfo::has_res, DocIterator::has_res.c_str()) + .def_readonly("res_p_or_mw", &DCLineContainer::DCLineInfo::res_p_or_mw, DocIterator::res_p_or_mw_dcline.c_str()) + .def_readonly("res_p_ex_mw", &DCLineContainer::DCLineInfo::res_p_ex_mw, DocIterator::res_p_ex_mw_dcline.c_str()) + .def_readonly("res_q_or_mvar", &DCLineContainer::DCLineInfo::res_q_or_mvar, DocIterator::res_q_or_mvar_dcline.c_str()) + .def_readonly("res_q_ex_mvar", &DCLineContainer::DCLineInfo::res_q_ex_mvar, DocIterator::res_q_ex_mvar_dcline.c_str()) + .def_readonly("res_v_or_kv", &DCLineContainer::DCLineInfo::res_v_or_kv, DocIterator::res_v_or_kv_dcline.c_str()) + .def_readonly("res_v_ex_kv", &DCLineContainer::DCLineInfo::res_v_ex_kv, DocIterator::res_v_ex_kv_dcline.c_str()) + .def_readonly("res_theta_or_deg", &DCLineContainer::DCLineInfo::res_theta_or_deg, DocIterator::res_theta_or_deg_dcline.c_str()) + .def_readonly("res_theta_ex_deg", &DCLineContainer::DCLineInfo::res_theta_ex_deg, DocIterator::res_theta_ex_deg_dcline.c_str()) ; // converters @@ -603,6 +603,9 @@ PYBIND11_MODULE(lightsim2grid_cpp, m) .def("need_recompute_sbus", &SolverControl::need_recompute_sbus, "TODO") .def("need_recompute_ybus", &SolverControl::need_recompute_ybus, "TODO") .def("ybus_change_sparsity_pattern", &SolverControl::ybus_change_sparsity_pattern, "TODO") + .def("has_slack_weight_changed", &SolverControl::has_slack_weight_changed, "TODO") + .def("has_v_changed", &SolverControl::has_v_changed, "TODO") + .def("has_ybus_some_coeffs_zero", &SolverControl::has_ybus_some_coeffs_zero, "TODO") ; py::class_(m, "GridModel", DocGridModel::GridModel.c_str()) diff --git a/src/BaseSolver.cpp b/src/powerflow_algorithm/BaseAlgo.cpp similarity index 87% rename from src/BaseSolver.cpp rename to src/powerflow_algorithm/BaseAlgo.cpp index 843d1a4..1849562 100644 --- a/src/BaseSolver.cpp +++ b/src/powerflow_algorithm/BaseAlgo.cpp @@ -6,11 +6,11 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "BaseSolver.h" +#include "BaseAlgo.h" #include "GridModel.h" // needs to be included here because of the forward declaration -void BaseSolver::reset(){ +void BaseAlgo::reset(){ // reset timers reset_timer(); @@ -27,7 +27,7 @@ void BaseSolver::reset(){ } -RealVect BaseSolver::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, +RealVect BaseAlgo::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, const CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & pv, @@ -53,7 +53,7 @@ RealVect BaseSolver::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, return res; } -RealVect BaseSolver::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, +RealVect BaseAlgo::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, const CplxVect & V, const CplxVect & Sbus, Eigen::Index slack_id, // id of the ref slack bus @@ -117,7 +117,7 @@ RealVect BaseSolver::_evaluate_Fx(const Eigen::SparseMatrix & Ybus, } -bool BaseSolver::_check_for_convergence(const RealVect & F, +bool BaseAlgo::_check_for_convergence(const RealVect & F, real_type tol) { auto timer = CustTimer(); @@ -128,7 +128,7 @@ bool BaseSolver::_check_for_convergence(const RealVect & F, return res; } -bool BaseSolver::_check_for_convergence(const RealVect & p, +bool BaseAlgo::_check_for_convergence(const RealVect & p, const RealVect & q, real_type tol) { @@ -140,7 +140,7 @@ bool BaseSolver::_check_for_convergence(const RealVect & p, return res; } -Eigen::VectorXi BaseSolver::extract_slack_bus_id(const Eigen::VectorXi & pv, +Eigen::VectorXi BaseAlgo::extract_slack_bus_id(const Eigen::VectorXi & pv, const Eigen::VectorXi & pq, unsigned int nb_bus) { @@ -151,7 +151,7 @@ Eigen::VectorXi BaseSolver::extract_slack_bus_id(const Eigen::VectorXi & pv, int nb_slacks = nb_bus - pv.size() - pq.size(); if(nb_slacks == 0){ // TODO DEBUG MODE - throw std::runtime_error("BaseSolver::extract_slack_bus_id: All buses are tagged as PV or PQ, there can be no slack."); + throw std::runtime_error("BaseAlgo::extract_slack_bus_id: All buses are tagged as PV or PQ, there can be no slack."); } Eigen::VectorXi res(nb_slacks); Eigen::Index i_res = 0; @@ -168,7 +168,7 @@ Eigen::VectorXi BaseSolver::extract_slack_bus_id(const Eigen::VectorXi & pv, { if((i_res >= nb_slacks)){ // TODO DEBUG MODE - throw std::runtime_error("BaseSolver::extract_slack_bus_id: too many slack found. Maybe a bus is both PV and PQ ?"); + throw std::runtime_error("BaseAlgo::extract_slack_bus_id: too many slack found. Maybe a bus is both PV and PQ ?"); } res[i_res] = k; ++i_res; @@ -176,18 +176,18 @@ Eigen::VectorXi BaseSolver::extract_slack_bus_id(const Eigen::VectorXi & pv, } if(res.size() != i_res){ // TODO DEBUG MODE - throw std::runtime_error("BaseSolver::extract_slack_bus_id: Some slacks are not found in your grid."); + throw std::runtime_error("BaseAlgo::extract_slack_bus_id: Some slacks are not found in your grid."); } return res; } -void BaseSolver::get_Bf(Eigen::SparseMatrix & Bf) const { +void BaseAlgo::get_Bf(Eigen::SparseMatrix & Bf) const { if(IS_AC) throw std::runtime_error("get_Bf: impossible to use this in AC mode for now"); _gridmodel->fillBf_for_PTDF(Bf); } -void BaseSolver::get_Bf_transpose(Eigen::SparseMatrix & Bf_T) const { +void BaseAlgo::get_Bf_transpose(Eigen::SparseMatrix & Bf_T) const { if(IS_AC) throw std::runtime_error("get_Bf: impossible to use this in AC mode for now"); _gridmodel->fillBf_for_PTDF(Bf_T, true); } diff --git a/src/BaseSolver.h b/src/powerflow_algorithm/BaseAlgo.h similarity index 96% rename from src/BaseSolver.h rename to src/powerflow_algorithm/BaseAlgo.h index deeaaa1..8556609 100644 --- a/src/BaseSolver.h +++ b/src/powerflow_algorithm/BaseAlgo.h @@ -6,8 +6,8 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef BASESOLVER_H -#define BASESOLVER_H +#ifndef BASEALGO_H +#define BASEALGO_H #include #include @@ -32,17 +32,17 @@ class GridModel; /** -This class represents a solver to compute powerflow. +This class represents a algorithm to compute powerflow. It can be derived for different usecase, for example for DC powerflow, AC powerflow using Newton Raphson method etc. **/ -class BaseSolver : public BaseConstants +class BaseAlgo : public BaseConstants { public: const bool IS_AC; // should be static ideally... public: - BaseSolver(bool is_ac=true): + BaseAlgo(bool is_ac=true): BaseConstants(), IS_AC(is_ac), n_(-1), @@ -52,7 +52,7 @@ class BaseSolver : public BaseConstants timer_check_(0.), timer_total_nr_(0.){}; - virtual ~BaseSolver(){} + virtual ~BaseAlgo(){} void set_gridmodel(const GridModel * gridmodel){ _gridmodel = gridmodel; @@ -233,9 +233,9 @@ class BaseSolver : public BaseConstants private: // no copy allowed - BaseSolver( const BaseSolver & ) ; - BaseSolver & operator=( const BaseSolver & ) ; + BaseAlgo( const BaseAlgo & ) ; + BaseAlgo & operator=( const BaseAlgo & ) ; }; -#endif // BASESOLVER_H +#endif // BASEALGO_H diff --git a/src/DCSolver.h b/src/powerflow_algorithm/BaseDCAlgo.h similarity index 88% rename from src/DCSolver.h rename to src/powerflow_algorithm/BaseDCAlgo.h index 5e0e035..06e6935 100644 --- a/src/DCSolver.h +++ b/src/powerflow_algorithm/BaseDCAlgo.h @@ -6,23 +6,23 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef DCSOLVER_H -#define DCSOLVER_H +#ifndef BASE_DC_ALGO_H +#define BASE_DC_ALGO_H -#include "BaseSolver.h" +#include "BaseAlgo.h" template -class BaseDCSolver: public BaseSolver +class BaseDCAlgo: public BaseAlgo { public: - BaseDCSolver(): - BaseSolver(false), + BaseDCAlgo(): + BaseAlgo(false), _linear_solver(), need_factorize_(true), sizeYbus_with_slack_(0), sizeYbus_without_slack_(0){}; - ~BaseDCSolver(){} + ~BaseDCAlgo(){} virtual void reset(); @@ -45,8 +45,8 @@ class BaseDCSolver: public BaseSolver private: // no copy allowed - BaseDCSolver( const BaseSolver & ) =delete ; - BaseDCSolver & operator=( const BaseSolver & ) =delete; + BaseDCAlgo( const BaseDCAlgo & ) =delete ; + BaseDCAlgo & operator=( const BaseDCAlgo & ) =delete; protected: void fill_mat_bus_id(int nb_bus_solver); @@ -72,6 +72,6 @@ class BaseDCSolver: public BaseSolver }; -#include "DCSolver.tpp" +#include "BaseDCAlgo.tpp" -#endif // DCSOLVER_H +#endif // BASE_DC_ALGO_H diff --git a/src/DCSolver.tpp b/src/powerflow_algorithm/BaseDCAlgo.tpp similarity index 92% rename from src/DCSolver.tpp rename to src/powerflow_algorithm/BaseDCAlgo.tpp index 30b903b..ed7e8ab 100644 --- a/src/DCSolver.tpp +++ b/src/powerflow_algorithm/BaseDCAlgo.tpp @@ -10,7 +10,7 @@ // TODO SLACK !!! template -bool BaseDCSolver::compute_pf(const Eigen::SparseMatrix & Ybus, +bool BaseDCAlgo::compute_pf(const Eigen::SparseMatrix & Ybus, CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & slack_ids, @@ -37,7 +37,7 @@ bool BaseDCSolver::compute_pf(const Eigen::SparseMatrix } auto timer = CustTimer(); - BaseSolver::reset_timer(); + BaseAlgo::reset_timer(); sizeYbus_with_slack_ = static_cast(Ybus.rows()); #ifdef __COUT_TIMES @@ -131,7 +131,7 @@ bool BaseDCSolver::compute_pf(const Eigen::SparseMatrix // retrieve back the results in the proper shape (add back the slack bus) // TODO have a better way for this, for example using `.segment(0,npv)` - // see the BaseSolver.cpp: _evaluate_Fx + // see the BaseAlgo.cpp: _evaluate_Fx RealVect Va_dc = RealVect::Constant(sizeYbus_with_slack_, my_zero_); // fill Va from dc approx for (int ybus_id=0; ybus_id < sizeYbus_with_slack_; ++ybus_id){ @@ -164,7 +164,7 @@ bool BaseDCSolver::compute_pf(const Eigen::SparseMatrix } template -void BaseDCSolver::fill_mat_bus_id(int nb_bus_solver){ +void BaseDCAlgo::fill_mat_bus_id(int nb_bus_solver){ mat_bus_id_ = Eigen::VectorXi::Constant(nb_bus_solver, -1); // Eigen::VectorXi me_to_ybus = Eigen::VectorXi::Constant(nb_bus_solver - slack_bus_ids_solver.size(), -1); int solver_id = 0; @@ -177,14 +177,14 @@ void BaseDCSolver::fill_mat_bus_id(int nb_bus_solver){ } template -void BaseDCSolver::fill_dcYbus_noslack(int nb_bus_solver, const Eigen::SparseMatrix & ref_mat){ +void BaseDCAlgo::fill_dcYbus_noslack(int nb_bus_solver, const Eigen::SparseMatrix & ref_mat){ // TODO see if "prune" might work here https://eigen.tuxfamily.org/dox/classEigen_1_1SparseMatrix.html#title29 remove_slack_buses(nb_bus_solver, ref_mat, dcYbus_noslack_); } template template // ref_mat_type should be `real_type` or `cplx_type` -void BaseDCSolver::remove_slack_buses(int nb_bus_solver, const Eigen::SparseMatrix & ref_mat, Eigen::SparseMatrix & res_mat){ +void BaseDCAlgo::remove_slack_buses(int nb_bus_solver, const Eigen::SparseMatrix & ref_mat, Eigen::SparseMatrix & res_mat){ res_mat = Eigen::SparseMatrix(sizeYbus_without_slack_, sizeYbus_without_slack_); // TODO dist slack: -1 or -mat_bus_id_.size() here ???? std::vector > tripletList; tripletList.reserve(ref_mat.nonZeros()); @@ -205,8 +205,8 @@ void BaseDCSolver::remove_slack_buses(int nb_bus_solver, const Eig } template -void BaseDCSolver::reset(){ - BaseSolver::reset(); +void BaseDCAlgo::reset(){ + BaseAlgo::reset(); _linear_solver.reset(); need_factorize_ = true; sizeYbus_with_slack_ = 0; @@ -219,7 +219,7 @@ void BaseDCSolver::reset(){ } template -RealMat BaseDCSolver::get_ptdf(const Eigen::SparseMatrix & dcYbus){ +RealMat BaseDCAlgo::get_ptdf(const Eigen::SparseMatrix & dcYbus){ Eigen::SparseMatrix Bf_T_with_slack; RealMat PTDF; RealVect rhs = RealVect::Zero(sizeYbus_without_slack_); // TODO dist slack: -1 or -mat_bus_id_.size() here ???? @@ -229,7 +229,7 @@ RealMat BaseDCSolver::get_ptdf(const Eigen::SparseMatrix::get_ptdf(const Eigen::SparseMatrix -Eigen::SparseMatrix BaseDCSolver::get_lodf(){ +Eigen::SparseMatrix BaseDCAlgo::get_lodf(){ // TODO return dcYbus_noslack_; } template -Eigen::SparseMatrix BaseDCSolver::get_bsdf(){ +Eigen::SparseMatrix BaseDCAlgo::get_bsdf(){ // TODO return dcYbus_noslack_; diff --git a/src/BaseFDPFSolver.h b/src/powerflow_algorithm/BaseFDPFAlgo.h similarity index 95% rename from src/BaseFDPFSolver.h rename to src/powerflow_algorithm/BaseFDPFAlgo.h index f670eea..67b9874 100644 --- a/src/BaseFDPFSolver.h +++ b/src/powerflow_algorithm/BaseFDPFAlgo.h @@ -6,19 +6,19 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef BASEFDPFSOLVER_H -#define BASEFDPFSOLVER_H +#ifndef BASEFDPFALGO_H +#define BASEFDPFALGO_H -#include "BaseSolver.h" +#include "BaseAlgo.h" /** Base class for Fast Decoupled Powerflow based solver **/ template -class BaseFDPFSolver : public BaseSolver +class BaseFDPFAlgo: public BaseAlgo { public: - BaseFDPFSolver():BaseSolver(true), need_factorize_(true) {} + BaseFDPFAlgo():BaseAlgo(true), need_factorize_(true) {} virtual bool compute_pf(const Eigen::SparseMatrix & Ybus, @@ -47,7 +47,7 @@ class BaseFDPFSolver : public BaseSolver virtual void reset() { - BaseSolver::reset(); + BaseAlgo::reset(); // solution of the problem Bp_ = Eigen::SparseMatrix (); // the B prime matrix (size n_pvpq) Bpp_ = Eigen::SparseMatrix(); // the B double prime matrix (size n_pq) @@ -67,7 +67,7 @@ class BaseFDPFSolver : public BaseSolver protected: virtual void reset_timer(){ - BaseSolver::reset_timer(); + BaseAlgo::reset_timer(); } CplxVect evaluate_mismatch(const Eigen::SparseMatrix & Ybus, @@ -213,10 +213,10 @@ class BaseFDPFSolver : public BaseSolver private: // no copy allowed - BaseFDPFSolver( const BaseFDPFSolver & ) =delete ; - BaseFDPFSolver & operator=( const BaseFDPFSolver & ) =delete ; + BaseFDPFAlgo( const BaseFDPFAlgo & ) =delete ; + BaseFDPFAlgo & operator=( const BaseFDPFAlgo & ) =delete ; }; -#include "BaseFDPFSolver.tpp" +#include "BaseFDPFAlgo.tpp" -#endif // BASEFDPFSOLVER_H +#endif // BASEFDPFALGO_H diff --git a/src/BaseFDPFSolver.tpp b/src/powerflow_algorithm/BaseFDPFAlgo.tpp similarity index 93% rename from src/BaseFDPFSolver.tpp rename to src/powerflow_algorithm/BaseFDPFAlgo.tpp index 080d191..fe13caf 100644 --- a/src/BaseFDPFSolver.tpp +++ b/src/powerflow_algorithm/BaseFDPFAlgo.tpp @@ -9,7 +9,7 @@ // inspired from pypower https://github.com/rwl/PYPOWER/blob/master/pypower/fdpf.py template -bool BaseFDPFSolver::compute_pf(const Eigen::SparseMatrix & Ybus, +bool BaseFDPFAlgo::compute_pf(const Eigen::SparseMatrix & Ybus, CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & slack_ids, @@ -32,14 +32,14 @@ bool BaseFDPFSolver::compute_pf(const Eigen::SparseMatrix::compute_pf(const Eigen::SparseMatrix -void BaseFDPFSolver::fill_sparse_matrices(const Eigen::SparseMatrix & grid_Bp, +void BaseFDPFAlgo::fill_sparse_matrices(const Eigen::SparseMatrix & grid_Bp, const Eigen::SparseMatrix & grid_Bpp, const std::vector & pvpq_inv, const std::vector & pq_inv, @@ -166,7 +166,7 @@ void BaseFDPFSolver::fill_sparse_matrices(const Eigen::Spar } template -void BaseFDPFSolver::aux_fill_sparse_matrices(const Eigen::SparseMatrix & grid_Bp_Bpp, +void BaseFDPFAlgo::aux_fill_sparse_matrices(const Eigen::SparseMatrix & grid_Bp_Bpp, const std::vector & ind_inv, Eigen::Index mat_dim, Eigen::SparseMatrix & res) diff --git a/src/BaseNRSolver.h b/src/powerflow_algorithm/BaseNRAlgo.h similarity index 95% rename from src/BaseNRSolver.h rename to src/powerflow_algorithm/BaseNRAlgo.h index bde0d6a..2b68155 100644 --- a/src/BaseNRSolver.h +++ b/src/powerflow_algorithm/BaseNRAlgo.h @@ -6,19 +6,19 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef BASENRSOLVER_H -#define BASENRSOLVER_H +#ifndef BASE_NR_ALGO_H +#define BASE_NR_ALGO_H -#include "BaseSolver.h" +#include "BaseAlgo.h" /** Base class for Newton Raphson based solver **/ template -class BaseNRSolver : public BaseSolver +class BaseNRAlgo : public BaseAlgo { public: - BaseNRSolver():BaseSolver(true), need_factorize_(true), timer_initialize_(0.), timer_dSbus_(0.), timer_fillJ_(0.) {} + BaseNRAlgo():BaseAlgo(true), need_factorize_(true), timer_initialize_(0.), timer_dSbus_(0.), timer_fillJ_(0.) {} virtual Eigen::Ref > get_J() const { @@ -56,7 +56,7 @@ class BaseNRSolver : public BaseSolver protected: virtual void reset_timer(){ - BaseSolver::reset_timer(); + BaseAlgo::reset_timer(); timer_dSbus_ = 0.; timer_fillJ_ = 0.; timer_initialize_ = 0.; @@ -198,8 +198,8 @@ class BaseNRSolver : public BaseSolver private: // no copy allowed - BaseNRSolver( const BaseNRSolver & ) =delete ; - BaseNRSolver & operator=( const BaseNRSolver & ) =delete ; + BaseNRAlgo( const BaseNRAlgo & ) =delete ; + BaseNRAlgo & operator=( const BaseNRAlgo & ) =delete ; /** helper function to print the max_col left most columns of the J matrix **/ void print_J(int min_col=-1, int max_col=-1) const{ @@ -235,6 +235,6 @@ class BaseNRSolver : public BaseSolver } }; -#include "BaseNRSolver.tpp" +#include "BaseNRAlgo.tpp" -#endif // BASENRSOLVER_H +#endif // BASE_NR_ALGO_H diff --git a/src/BaseNRSolver.tpp b/src/powerflow_algorithm/BaseNRAlgo.tpp similarity index 94% rename from src/BaseNRSolver.tpp rename to src/powerflow_algorithm/BaseNRAlgo.tpp index 4dbbfb8..0977d7d 100644 --- a/src/BaseNRSolver.tpp +++ b/src/powerflow_algorithm/BaseNRAlgo.tpp @@ -6,13 +6,13 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -// #include "BaseNRSolver.h" // now a template class, so this file will be included instead ! +// #include "BaseNRAlgo.h" // now a template class, so this file will be included instead ! // TODO get rid of the pvpq, pv, pq etc and put the jacobian "in the right order" // to ease and make way faster the filling of the sparse matrix J template -bool BaseNRSolver::compute_pf(const Eigen::SparseMatrix & Ybus, +bool BaseNRAlgo::compute_pf(const Eigen::SparseMatrix & Ybus, CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & slack_ids, @@ -35,14 +35,14 @@ bool BaseNRSolver::compute_pf(const Eigen::SparseMatrix if(Sbus.size() != Ybus.rows() || Sbus.size() != Ybus.cols() ){ // TODO DEBUG MODE std::ostringstream exc_; - exc_ << "BaseNRSolver::compute_pf: Size of the Sbus should be the same as the size of Ybus. Currently: "; + exc_ << "BaseNRAlgo::compute_pf: Size of the Sbus should be the same as the size of Ybus. Currently: "; exc_ << "Sbus (" << Sbus.size() << ") and Ybus (" << Ybus.rows() << ", " << Ybus.cols() << ")."; throw std::runtime_error(exc_.str()); } if(V.size() != Ybus.rows() || V.size() != Ybus.cols() ){ // TODO DEBUG MODE std::ostringstream exc_; - exc_ << "BaseNRSolver::compute_pf: Size of V (init voltages) should be the same as the size of Ybus. Currently: "; + exc_ << "BaseNRAlgo::compute_pf: Size of V (init voltages) should be the same as the size of Ybus. Currently: "; exc_ << "V (" << V.size() << ") and Ybus (" << Ybus.rows()<< ", " << Ybus.cols() << ")."; throw std::runtime_error(exc_.str()); } @@ -89,12 +89,12 @@ bool BaseNRSolver::compute_pf(const Eigen::SparseMatrix // std::cout << "iter " << nr_iter_ << " dx(0): " << -F(0) << " dx(1): " << -F(1) << std::endl; // std::cout << "slack_absorbed " << slack_absorbed << std::endl; value_map_.clear(); // TODO smarter solver: only needed if ybus has changed - // BaseNRSolver::col_map_.clear(); // TODO smarter solver: only needed if ybus has changed - // BaseNRSolver::row_map_.clear(); // TODO smarter solver: only needed if ybus has changed + // BaseNRAlgo::col_map_.clear(); // TODO smarter solver: only needed if ybus has changed + // BaseNRAlgo::row_map_.clear(); // TODO smarter solver: only needed if ybus has changed dS_dVm_.resize(0,0); // TODO smarter solver: only needed if ybus has changed dS_dVa_.resize(0,0); // TODO smarter solver: only needed if ybus has changed - // BaseNRSolver::dS_dVm_.setZero(); // TODO smarter solver: only needed if ybus has changed - // BaseNRSolver::dS_dVa_.setZero(); // TODO smarter solver: only needed if ybus has changed + // BaseNRAlgo::dS_dVm_.setZero(); // TODO smarter solver: only needed if ybus has changed + // BaseNRAlgo::dS_dVa_.setZero(); // TODO smarter solver: only needed if ybus has changed while ((!converged) & (nr_iter_ < max_iter)){ nr_iter_++; fill_jacobian_matrix(Ybus, V_, slack_bus_id, slack_weights, pq, pvpq, pq_inv, pvpq_inv); @@ -161,8 +161,8 @@ bool BaseNRSolver::compute_pf(const Eigen::SparseMatrix } template -void BaseNRSolver::reset(){ - BaseSolver::reset(); +void BaseNRAlgo::reset(){ + BaseAlgo::reset(); // reset specific attributes J_ = Eigen::SparseMatrix(); // the jacobian matrix dS_dVm_ = Eigen::SparseMatrix(); @@ -175,7 +175,7 @@ void BaseNRSolver::reset(){ } template -void BaseNRSolver::_dSbus_dV(const Eigen::Ref > & Ybus, +void BaseNRAlgo::_dSbus_dV(const Eigen::Ref > & Ybus, const Eigen::Ref & V){ // std::cout << "Ybus.nonZeros(): " << Ybus.nonZeros() << std::endl; auto timer = CustTimer(); @@ -236,7 +236,7 @@ void BaseNRSolver::_dSbus_dV(const Eigen::Ref -void BaseNRSolver::_get_values_J(int & nb_obj_this_col, +void BaseNRAlgo::_get_values_J(int & nb_obj_this_col, std::vector & inner_index, std::vector & values, const Eigen::Ref > & mat, // ex. dS_dVa_r @@ -263,7 +263,7 @@ void BaseNRSolver::_get_values_J(int & nb_obj_this_col, } template -void BaseNRSolver::_get_values_J(int & nb_obj_this_col, +void BaseNRAlgo::_get_values_J(int & nb_obj_this_col, std::vector & inner_index, std::vector & values, const Eigen::Ref > & mat, // ex. dS_dVa_r @@ -300,7 +300,7 @@ void BaseNRSolver::_get_values_J(int & nb_obj_this_col, } template -void BaseNRSolver::fill_jacobian_matrix(const Eigen::SparseMatrix & Ybus, +void BaseNRAlgo::fill_jacobian_matrix(const Eigen::SparseMatrix & Ybus, const CplxVect & V, Eigen::Index slack_bus_id, const RealVect & slack_weights, @@ -359,7 +359,7 @@ void BaseNRSolver::fill_jacobian_matrix(const Eigen::SparseMatrix< #ifdef __COUT_TIMES auto timer3 = CustTimer(); #endif // - if (BaseNRSolver::value_map_.size() == 0) fill_value_map(slack_bus_id, pq, pvpq, true); + if (BaseNRAlgo::value_map_.size() == 0) fill_value_map(slack_bus_id, pq, pvpq, true); fill_jacobian_matrix_kown_sparsity_pattern(slack_bus_id, pq, pvpq ); @@ -371,7 +371,7 @@ void BaseNRSolver::fill_jacobian_matrix(const Eigen::SparseMatrix< } template -void BaseNRSolver::fill_jacobian_matrix_unkown_sparsity_pattern( +void BaseNRAlgo::fill_jacobian_matrix_unkown_sparsity_pattern( const Eigen::SparseMatrix & Ybus, const CplxVect & V, Eigen::Index slack_bus_id, @@ -541,7 +541,7 @@ dS_dVa_ and dS_dVm_ to be used to fill J_ it requires that J_ is initialized, in compressed mode. **/ template -void BaseNRSolver::fill_value_map( +void BaseNRAlgo::fill_value_map( Eigen::Index slack_bus_id, const Eigen::VectorXi & pq, const Eigen::VectorXi & pvpq, @@ -550,7 +550,7 @@ void BaseNRSolver::fill_value_map( { const int n_pvpq = static_cast(pvpq.size()); value_map_ = std::vector (); - value_map_.reserve(BaseNRSolver::J_.nonZeros()); + value_map_.reserve(BaseNRAlgo::J_.nonZeros()); // col_map_ = std::vector (J_.nonZeros()); // row_map_ = std::vector (J_.nonZeros()); @@ -620,7 +620,7 @@ void BaseNRSolver::fill_value_map( } template -void BaseNRSolver::fill_jacobian_matrix_kown_sparsity_pattern( +void BaseNRAlgo::fill_jacobian_matrix_kown_sparsity_pattern( Eigen::Index slack_bus_id, const Eigen::VectorXi & pq, const Eigen::VectorXi & pvpq diff --git a/src/BaseNRSolverSingleSlack.h b/src/powerflow_algorithm/BaseNRSingleSlackAlgo.h similarity index 86% rename from src/BaseNRSolverSingleSlack.h rename to src/powerflow_algorithm/BaseNRSingleSlackAlgo.h index 09f8042..7ed9ccf 100644 --- a/src/BaseNRSolverSingleSlack.h +++ b/src/powerflow_algorithm/BaseNRSingleSlackAlgo.h @@ -6,21 +6,21 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef BASENRSOLVERSINGLESLACK_H -#define BASENRSOLVERSINGLESLACK_H +#ifndef BASE_NR_SINGLESLACK_ALGO_H +#define BASE_NR_SINGLESLACK_ALGO_H -#include "BaseNRSolver.h" +#include "BaseNRAlgo.h" /** Base class for Newton Raphson based solver (only interesting for single slack) **/ template -class BaseNRSolverSingleSlack : public BaseNRSolver +class BaseNRSingleSlackAlgo : public BaseNRAlgo { public: - BaseNRSolverSingleSlack():BaseNRSolver(){} + BaseNRSingleSlackAlgo():BaseNRAlgo(){} - ~BaseNRSolverSingleSlack(){} + ~BaseNRSingleSlackAlgo(){} virtual bool compute_pf(const Eigen::SparseMatrix & Ybus, @@ -63,6 +63,6 @@ class BaseNRSolverSingleSlack : public BaseNRSolver }; -#include "BaseNRSolverSingleSlack.tpp" +#include "BaseNRSingleSlackAlgo.tpp" -#endif // BASENRSOLVERSINGLESLACK_H +#endif // BASE_NR_SINGLESLACK_ALGO_H diff --git a/src/BaseNRSolverSingleSlack.tpp b/src/powerflow_algorithm/BaseNRSingleSlackAlgo.tpp similarity index 65% rename from src/BaseNRSolverSingleSlack.tpp rename to src/powerflow_algorithm/BaseNRSingleSlackAlgo.tpp index 7e39ba2..8c0a429 100644 --- a/src/BaseNRSolverSingleSlack.tpp +++ b/src/powerflow_algorithm/BaseNRSingleSlackAlgo.tpp @@ -7,10 +7,10 @@ // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. // #include "BaseNRSolverSingleSlack.h" -// #include "BaseNRSolver.h" +// #include "BaseNRAlgo.h" template -bool BaseNRSolverSingleSlack::compute_pf(const Eigen::SparseMatrix & Ybus, +bool BaseNRSingleSlackAlgo::compute_pf(const Eigen::SparseMatrix & Ybus, CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & slack_ids, @@ -30,28 +30,28 @@ bool BaseNRSolverSingleSlack::compute_pf(const Eigen::SparseMatrix // TODO Ybus (nrow or ncol), pv and pq have value that are between 0 and nrow etc. if(Sbus.size() != Ybus.rows() || Sbus.size() != Ybus.cols() ){ std::ostringstream exc_; - exc_ << "BaseNRSolverSingleSlack::compute_pf: Size of the Sbus should be the same as the size of Ybus. Currently: "; + exc_ << "BaseNRSingleSlackAlgo::compute_pf: Size of the Sbus should be the same as the size of Ybus. Currently: "; exc_ << "Sbus (" << Sbus.size() << ") and Ybus (" << Ybus.rows() << ", " << Ybus.cols() << ")."; throw std::runtime_error(exc_.str()); } if(V.size() != Ybus.rows() || V.size() != Ybus.cols() ){ std::ostringstream exc_; - exc_ << "BaseNRSolverSingleSlack::compute_pf: Size of V (init voltages) should be the same as the size of Ybus. Currently: "; + exc_ << "BaseNRSingleSlackAlgo::compute_pf: Size of V (init voltages) should be the same as the size of Ybus. Currently: "; exc_ << "V (" << V.size() << ") and Ybus (" << Ybus.rows()<<", "<::is_linear_solver_valid()){ + if(!BaseNRAlgo::is_linear_solver_valid()){ return false; } - BaseNRSolver::reset_timer(); - BaseNRSolver::reset_if_needed(); - BaseNRSolver::err_ = ErrorType::NoError; // reset the error if previous error happened + BaseNRAlgo::reset_timer(); + BaseNRAlgo::reset_if_needed(); + BaseNRAlgo::err_ = ErrorType::NoError; // reset the error if previous error happened auto timer = CustTimer(); // initialize once and for all the "inverse" of these vectors - // Eigen::VectorXi my_pv = BaseNRSolver::retrieve_pv_with_slack(slack_ids, pv); + // Eigen::VectorXi my_pv = BaseNRAlgo::retrieve_pv_with_slack(slack_ids, pv); Eigen::VectorXi my_pv = pv; - // Eigen::VectorXi my_pv = pv; // BaseNRSolver::retrieve_pv_with_slack(slack_ids, pv); + // Eigen::VectorXi my_pv = pv; // BaseNRAlgo::retrieve_pv_with_slack(slack_ids, pv); const int n_pv = static_cast(my_pv.size()); const int n_pq = static_cast(pq.size()); @@ -63,33 +63,33 @@ bool BaseNRSolverSingleSlack::compute_pf(const Eigen::SparseMatrix std::vector pq_inv(V.size(), -1); for(int inv_id=0; inv_id < n_pq; ++inv_id) pq_inv[pq(inv_id)] = inv_id; - BaseNRSolver::V_ = V; - BaseNRSolver::Vm_ = BaseNRSolver::V_.array().abs(); // update Vm and Va again in case - BaseNRSolver::Va_ = BaseNRSolver::V_.array().arg(); // we wrapped around with a negative Vm + BaseNRAlgo::V_ = V; + BaseNRAlgo::Vm_ = BaseNRAlgo::V_.array().abs(); // update Vm and Va again in case + BaseNRAlgo::Va_ = BaseNRAlgo::V_.array().arg(); // we wrapped around with a negative Vm // first check, if the problem is already solved, i stop there - RealVect F = BaseNRSolver::_evaluate_Fx(Ybus, V, Sbus, my_pv, pq); - bool converged = BaseNRSolver::_check_for_convergence(F, tol); - BaseNRSolver::nr_iter_ = 0; //current step + RealVect F = BaseNRAlgo::_evaluate_Fx(Ybus, V, Sbus, my_pv, pq); + bool converged = BaseNRAlgo::_check_for_convergence(F, tol); + BaseNRAlgo::nr_iter_ = 0; //current step bool res = true; // have i converged or not bool has_just_been_initialized = false; // to avoid a call to klu_refactor follow a call to klu_factor in the same loop - const cplx_type m_i = BaseNRSolver::my_i; // otherwise it does not compile - BaseNRSolver::value_map_.clear(); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed - BaseNRSolver::dS_dVm_.resize(0,0); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed - BaseNRSolver::dS_dVa_.resize(0,0); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed - // BaseNRSolver::J_.setZero(); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed or ybus_some_coeffs_zero_ - // BaseNRSolver::dS_dVm_.setZero(); // TODO smarter solver: only needed if ybus has changed - // BaseNRSolver::dS_dVa_.setZero(); // TODO smarter solver: only needed if ybus has changed - while ((!converged) & (BaseNRSolver::nr_iter_ < max_iter)){ - BaseNRSolver::nr_iter_++; - // std::cout << "\tnr_iter_ " << BaseNRSolver::nr_iter_ << std::endl; - fill_jacobian_matrix(Ybus, BaseNRSolver::V_, pq, pvpq, pq_inv, pvpq_inv); - if(BaseNRSolver::need_factorize_){ - BaseNRSolver::initialize(); - if(BaseNRSolver::err_ != ErrorType::NoError){ + const cplx_type m_i = BaseNRAlgo::my_i; // otherwise it does not compile + BaseNRAlgo::value_map_.clear(); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed + BaseNRAlgo::dS_dVm_.resize(0,0); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed + BaseNRAlgo::dS_dVa_.resize(0,0); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed + // BaseNRAlgo::J_.setZero(); // TODO smarter solver: only needed if ybus has changed or pq changed or pv changed or ybus_some_coeffs_zero_ + // BaseNRAlgo::dS_dVm_.setZero(); // TODO smarter solver: only needed if ybus has changed + // BaseNRAlgo::dS_dVa_.setZero(); // TODO smarter solver: only needed if ybus has changed + while ((!converged) & (BaseNRAlgo::nr_iter_ < max_iter)){ + BaseNRAlgo::nr_iter_++; + // std::cout << "\tnr_iter_ " << BaseNRAlgo::nr_iter_ << std::endl; + fill_jacobian_matrix(Ybus, BaseNRAlgo::V_, pq, pvpq, pq_inv, pvpq_inv); + if(BaseNRAlgo::need_factorize_){ + BaseNRAlgo::initialize(); + if(BaseNRAlgo::err_ != ErrorType::NoError){ // I got an error during the initialization of the linear system, i need to stop here - // std::cout << BaseNRSolver::err_ << std::endl; + // std::cout << BaseNRAlgo::err_ << std::endl; res = false; break; } @@ -99,62 +99,62 @@ bool BaseNRSolverSingleSlack::compute_pf(const Eigen::SparseMatrix // std::cout << "no need to factorize" << std::endl; } - BaseNRSolver::solve(F, has_just_been_initialized); + BaseNRAlgo::solve(F, has_just_been_initialized); has_just_been_initialized = false; - if(BaseNRSolver::err_ != ErrorType::NoError){ + if(BaseNRAlgo::err_ != ErrorType::NoError){ // I got an error during the solving of the linear system, i need to stop here - // std::cout << BaseNRSolver::err_ << std::endl; + // std::cout << BaseNRAlgo::err_ << std::endl; res = false; break; } // auto dx = -F; - BaseNRSolver::Vm_ = BaseNRSolver::V_.array().abs(); // update Vm and Va again in case - BaseNRSolver::Va_ = BaseNRSolver::V_.array().arg(); // we wrapped around with a negative Vm + BaseNRAlgo::Vm_ = BaseNRAlgo::V_.array().abs(); // update Vm and Va again in case + BaseNRAlgo::Va_ = BaseNRAlgo::V_.array().arg(); // we wrapped around with a negative Vm // update voltage (this should be done consistently with "klu_solver._evaluate_Fx") - if (n_pv > 0) BaseNRSolver::Va_(my_pv) -= F.segment(0, n_pv); + if (n_pv > 0) BaseNRAlgo::Va_(my_pv) -= F.segment(0, n_pv); if (n_pq > 0){ - BaseNRSolver::Va_(pq) -= F.segment(n_pv,n_pq); - BaseNRSolver::Vm_(pq) -= F.segment(n_pv+n_pq, n_pq); + BaseNRAlgo::Va_(pq) -= F.segment(n_pv,n_pq); + BaseNRAlgo::Vm_(pq) -= F.segment(n_pv+n_pq, n_pq); } // TODO change here for not having to cast all the time ... maybe - const RealVect & Vm = BaseNRSolver::Vm_; // I am forced to redefine the type for it to compile properly - const RealVect & Va = BaseNRSolver::Va_; - BaseNRSolver::V_ = Vm.array() * (Va.array().cos().cast() + m_i * Va.array().sin().cast() ); + const RealVect & Vm = BaseNRAlgo::Vm_; // I am forced to redefine the type for it to compile properly + const RealVect & Va = BaseNRAlgo::Va_; + BaseNRAlgo::V_ = Vm.array() * (Va.array().cos().cast() + m_i * Va.array().sin().cast() ); - F = BaseNRSolver::_evaluate_Fx(Ybus, BaseNRSolver::V_, Sbus, my_pv, pq); + F = BaseNRAlgo::_evaluate_Fx(Ybus, BaseNRAlgo::V_, Sbus, my_pv, pq); bool tmp = F.allFinite(); if(!tmp){ - BaseNRSolver::err_ = ErrorType::InifiniteValue; - // std::cout << BaseNRSolver::err_ << std::endl; + BaseNRAlgo::err_ = ErrorType::InifiniteValue; + // std::cout << BaseNRAlgo::err_ << std::endl; break; // divergence due to Nans } - converged = BaseNRSolver::_check_for_convergence(F, tol); + converged = BaseNRAlgo::_check_for_convergence(F, tol); } if(!converged){ - if (BaseNRSolver::err_ == ErrorType::NoError) BaseNRSolver::err_ = ErrorType::TooManyIterations; + if (BaseNRAlgo::err_ == ErrorType::NoError) BaseNRAlgo::err_ = ErrorType::TooManyIterations; res = false; } - BaseNRSolver::timer_total_nr_ += timer.duration(); + BaseNRAlgo::timer_total_nr_ += timer.duration(); #ifdef __COUT_TIMES - std::cout << "Computation time: " << "\n\t timer_initialize_: " << BaseNRSolver::timer_initialize_ - << "\n\t timer_dSbus_ (called in _fillJ_): " << BaseNRSolver::timer_dSbus_ - << "\n\t timer_fillJ_: " << BaseNRSolver::timer_fillJ_ - << "\n\t timer_Fx_: " << BaseNRSolver::timer_Fx_ - << "\n\t timer_check_: " << BaseNRSolver::timer_check_ - << "\n\t timer_solve_: " << BaseNRSolver::timer_solve_ - << "\n\t timer_total_nr_: " << BaseNRSolver::timer_total_nr_ + std::cout << "Computation time: " << "\n\t timer_initialize_: " << BaseNRAlgo::timer_initialize_ + << "\n\t timer_dSbus_ (called in _fillJ_): " << BaseNRAlgo::timer_dSbus_ + << "\n\t timer_fillJ_: " << BaseNRAlgo::timer_fillJ_ + << "\n\t timer_Fx_: " << BaseNRAlgo::timer_Fx_ + << "\n\t timer_check_: " << BaseNRAlgo::timer_check_ + << "\n\t timer_solve_: " << BaseNRAlgo::timer_solve_ + << "\n\t timer_total_nr_: " << BaseNRAlgo::timer_total_nr_ << "\n\n"; #endif // __COUT_TIMES - BaseNRSolver::_solver_control.tell_none_changed(); + BaseNRAlgo::_solver_control.tell_none_changed(); return res; } template -void BaseNRSolverSingleSlack::fill_jacobian_matrix(const Eigen::SparseMatrix & Ybus, +void BaseNRSingleSlackAlgo::fill_jacobian_matrix(const Eigen::SparseMatrix & Ybus, const CplxVect & V, const Eigen::VectorXi & pq, const Eigen::VectorXi & pvpq, @@ -175,7 +175,7 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix(const Eigen::Sp **/ auto timer = CustTimer(); - BaseNRSolver::_dSbus_dV(Ybus, V); + BaseNRAlgo::_dSbus_dV(Ybus, V); const int n_pvpq = static_cast(pvpq.size()); const int n_pq = static_cast(pq.size()); @@ -183,7 +183,7 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix(const Eigen::Sp // TODO to gain a bit more time below, try to compute directly, in _dSbus_dV(Ybus, V); // TODO the `dS_dVa_[pvpq, pvpq]` // TODO so that it's easier to retrieve in the next few lines ! - if(BaseNRSolver::J_.cols() != size_j) + if(BaseNRAlgo::J_.cols() != size_j) // if(true) { #ifdef __COUT_TIMES @@ -202,7 +202,7 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix(const Eigen::Sp #ifdef __COUT_TIMES auto timer3 = CustTimer(); #endif // __COUT_TIMES - if (BaseNRSolver::value_map_.size() == 0){ + if (BaseNRAlgo::value_map_.size() == 0){ // std::cout << "\t\tfill_value_map called" << std::endl; fill_value_map(pq, pvpq, true); } @@ -212,11 +212,11 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix(const Eigen::Sp std::cout << "\t\t fill_jacobian_matrix_kown_sparsity_pattern : " << timer3.duration() << std::endl; #endif // __COUT_TIMES } - BaseNRSolver::timer_fillJ_ += timer.duration(); + BaseNRAlgo::timer_fillJ_ += timer.duration(); } template -void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity_pattern( +void BaseNRSingleSlackAlgo::fill_jacobian_matrix_unkown_sparsity_pattern( const Eigen::SparseMatrix & Ybus, const CplxVect & V, const Eigen::VectorXi & pq, @@ -246,22 +246,22 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity const int n_pq = static_cast(pq.size()); const int size_j = n_pvpq + n_pq; - const Eigen::SparseMatrix dS_dVa_r = BaseNRSolver::dS_dVa_.real(); - const Eigen::SparseMatrix dS_dVa_i = BaseNRSolver::dS_dVa_.imag(); - const Eigen::SparseMatrix dS_dVm_r = BaseNRSolver::dS_dVm_.real(); - const Eigen::SparseMatrix dS_dVm_i = BaseNRSolver::dS_dVm_.imag(); + const Eigen::SparseMatrix dS_dVa_r = BaseNRAlgo::dS_dVa_.real(); + const Eigen::SparseMatrix dS_dVa_i = BaseNRAlgo::dS_dVa_.imag(); + const Eigen::SparseMatrix dS_dVm_r = BaseNRAlgo::dS_dVm_.real(); + const Eigen::SparseMatrix dS_dVm_i = BaseNRAlgo::dS_dVm_.imag(); // Method (1) seems to be faster than the others // optim : if the matrix was already computed, i don't initialize it, i instead reuse as much as i can // i can do that because the matrix will ALWAYS have the same non zero coefficients. // in this if, i allocate it in a "large enough" place to avoid copy when first filling it - if(BaseNRSolver::J_.cols() != size_j) + if(BaseNRAlgo::J_.cols() != size_j) { need_insert = true; - BaseNRSolver::J_ = Eigen::SparseMatrix(size_j, size_j); + BaseNRAlgo::J_ = Eigen::SparseMatrix(size_j, size_j); // pre allocate a large enough matrix - BaseNRSolver::J_.reserve(2*(BaseNRSolver::dS_dVa_.nonZeros() + BaseNRSolver::dS_dVm_.nonZeros())); + BaseNRAlgo::J_.reserve(2*(BaseNRAlgo::dS_dVa_.nonZeros() + BaseNRAlgo::dS_dVm_.nonZeros())); // from an experiment, outerIndexPtr is initialized, with the number of columns // innerIndexPtr and valuePtr are not. } @@ -284,14 +284,14 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity // fill with the first column with the column of dS_dVa[:,pvpq[col_id]] // and check the row order ! - BaseNRSolver::_get_values_J(nb_obj_this_col, inner_index, values, + BaseNRAlgo::_get_values_J(nb_obj_this_col, inner_index, values, dS_dVa_r, pvpq_inv, pvpq, col_id, 0, 0); // fill the rest of the rows with the first column of dS_dVa_imag[:,pq[col_id]] - BaseNRSolver::_get_values_J(nb_obj_this_col, inner_index, values, + BaseNRAlgo::_get_values_J(nb_obj_this_col, inner_index, values, dS_dVa_i, pq_inv, pvpq, col_id, @@ -301,8 +301,8 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity // "efficient" insert of the element in the matrix for(int in_ind=0; in_ind < nb_obj_this_col; ++in_ind){ int row_id = inner_index[in_ind]; - if(need_insert) BaseNRSolver::J_.insert(row_id, col_id) = values[in_ind]; // HERE FOR PERF OPTIM (1) - else BaseNRSolver::J_.coeffRef(row_id, col_id) = values[in_ind]; // HERE FOR PERF OPTIM (1) + if(need_insert) BaseNRAlgo::J_.insert(row_id, col_id) = values[in_ind]; // HERE FOR PERF OPTIM (1) + else BaseNRAlgo::J_.coeffRef(row_id, col_id) = values[in_ind]; // HERE FOR PERF OPTIM (1) // J_.insert(row_id, col_id) = values[in_ind]; // HERE FOR PERF OPTIM (2) // coeffs.push_back(Eigen::Triplet(row_id, col_id, values[in_ind])); // HERE FOR PERF OPTIM (3) } @@ -318,7 +318,7 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity // fill with the first column with the column of dS_dVa[:,pvpq[col_id]] // and check the row order ! - BaseNRSolver::_get_values_J(nb_obj_this_col, inner_index, values, + BaseNRAlgo::_get_values_J(nb_obj_this_col, inner_index, values, dS_dVm_r, pvpq_inv, pq, col_id, @@ -326,7 +326,7 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity 0); // fill the rest of the rows with the first column of dS_dVa_imag[:,pq[col_id]] - BaseNRSolver::_get_values_J(nb_obj_this_col, inner_index, values, + BaseNRAlgo::_get_values_J(nb_obj_this_col, inner_index, values, dS_dVm_i, pq_inv, pq, col_id, @@ -336,14 +336,14 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_unkown_sparsity // "efficient" insert of the element in the matrix for(int in_ind=0; in_ind < nb_obj_this_col; ++in_ind){ int row_id = inner_index[in_ind]; - if(need_insert) BaseNRSolver::J_.insert(row_id, col_id + n_pvpq) = values[in_ind]; // HERE FOR PERF OPTIM (1) - else BaseNRSolver::J_.coeffRef(row_id, col_id + n_pvpq) = values[in_ind]; // HERE FOR PERF OPTIM (1) + if(need_insert) BaseNRAlgo::J_.insert(row_id, col_id + n_pvpq) = values[in_ind]; // HERE FOR PERF OPTIM (1) + else BaseNRAlgo::J_.coeffRef(row_id, col_id + n_pvpq) = values[in_ind]; // HERE FOR PERF OPTIM (1) // J_.insert(row_id, col_id + n_pvpq) = values[in_ind]; // HERE FOR PERF OPTIM (2) // coeffs.push_back(Eigen::Triplet(row_id, col_id + n_pvpq, values[in_ind])); // HERE FOR PERF OPTIM (3) } } // J_.setFromTriplets(coeffs.begin(), coeffs.end()); // HERE FOR PERF OPTIM (3) - BaseNRSolver::J_.makeCompressed(); + BaseNRAlgo::J_.makeCompressed(); } /** @@ -352,21 +352,21 @@ dS_dVa_ and dS_dVm_ to be used to fill J_ it requires that J_ is initialized, in compressed mode. **/ template -void BaseNRSolverSingleSlack::fill_value_map( +void BaseNRSingleSlackAlgo::fill_value_map( const Eigen::VectorXi & pq, const Eigen::VectorXi & pvpq, bool reset_J ) { const int n_pvpq = static_cast(pvpq.size()); - BaseNRSolver::value_map_.clear(); - // std::cout << "BaseNRSolver::J_.nonZeros(): " << BaseNRSolver::J_.nonZeros() << std::endl; - BaseNRSolver::value_map_.reserve(BaseNRSolver::J_.nonZeros()); + BaseNRAlgo::value_map_.clear(); + // std::cout << "BaseNRAlgo::J_.nonZeros(): " << BaseNRAlgo::J_.nonZeros() << std::endl; + BaseNRAlgo::value_map_.reserve(BaseNRAlgo::J_.nonZeros()); - const int n_col = static_cast(BaseNRSolver::J_.cols()); + const int n_col = static_cast(BaseNRAlgo::J_.cols()); unsigned int pos_el = 0; for (int col_=0; col_ < n_col; ++col_){ - for (Eigen::SparseMatrix::InnerIterator it(BaseNRSolver::J_, col_); it; ++it) + for (Eigen::SparseMatrix::InnerIterator it(BaseNRAlgo::J_, col_); it; ++it) { const int row_id = static_cast(it.row()); const int col_id = static_cast(it.col()); // it's equal to "col_" @@ -377,7 +377,7 @@ void BaseNRSolverSingleSlack::fill_value_map( const int row_id_dS_dVa_r = pvpq[row_id]; const int col_id_dS_dVa_r = pvpq[col_id]; // this_el = dS_dVa_r.coeff(row_id_dS_dVa_r, col_id_dS_dVa_r); - BaseNRSolver::value_map_.push_back(&BaseNRSolver::dS_dVa_.coeffRef(row_id_dS_dVa_r, col_id_dS_dVa_r)); + BaseNRAlgo::value_map_.push_back(&BaseNRAlgo::dS_dVa_.coeffRef(row_id_dS_dVa_r, col_id_dS_dVa_r)); // I don't need to perform these checks: if they failed, the element would not be in J_ in the first place // const int is_row_non_null = pq_inv[row_id_dS_dVa_r]; @@ -392,31 +392,31 @@ void BaseNRSolverSingleSlack::fill_value_map( const int row_id_dS_dVa_i = pq[row_id - n_pvpq]; const int col_id_dS_dVa_i = pvpq[col_id]; // this_el = dS_dVa_i.coeff(row_id_dS_dVa_i, col_id_dS_dVa_i); - BaseNRSolver::value_map_.push_back(&BaseNRSolver::dS_dVa_.coeffRef(row_id_dS_dVa_i, col_id_dS_dVa_i)); + BaseNRAlgo::value_map_.push_back(&BaseNRAlgo::dS_dVa_.coeffRef(row_id_dS_dVa_i, col_id_dS_dVa_i)); }else if((col_id >= n_pvpq) && (row_id < n_pvpq)){ // this is the J12 part (dS_dVm_r) const int row_id_dS_dVm_r = pvpq[row_id]; const int col_id_dS_dVm_r = pq[col_id - n_pvpq]; // this_el = dS_dVm_r.coeff(row_id_dS_dVm_r, col_id_dS_dVm_r); - BaseNRSolver::value_map_.push_back(&BaseNRSolver::dS_dVm_.coeffRef(row_id_dS_dVm_r, col_id_dS_dVm_r)); + BaseNRAlgo::value_map_.push_back(&BaseNRAlgo::dS_dVm_.coeffRef(row_id_dS_dVm_r, col_id_dS_dVm_r)); }else if((col_id >= n_pvpq) && (row_id >= n_pvpq)){ // this is the J22 part (dS_dVm_i) const int row_id_dS_dVm_i = pq[row_id - n_pvpq]; const int col_id_dS_dVm_i = pq[col_id - n_pvpq]; // this_el = dS_dVm_i.coeff(row_id_dS_dVm_i, col_id_dS_dVm_i); - BaseNRSolver::value_map_.push_back(&BaseNRSolver::dS_dVm_.coeffRef(row_id_dS_dVm_i, col_id_dS_dVm_i)); + BaseNRAlgo::value_map_.push_back(&BaseNRAlgo::dS_dVm_.coeffRef(row_id_dS_dVm_i, col_id_dS_dVm_i)); } // go to the next element ++pos_el; } } - // BaseNRSolver::dS_dVa_.makeCompressed(); - // BaseNRSolver::dS_dVm_.makeCompressed(); + // BaseNRAlgo::dS_dVa_.makeCompressed(); + // BaseNRAlgo::dS_dVm_.makeCompressed(); } template -void BaseNRSolverSingleSlack::fill_jacobian_matrix_kown_sparsity_pattern( +void BaseNRSingleSlackAlgo::fill_jacobian_matrix_kown_sparsity_pattern( const Eigen::VectorXi & pq, const Eigen::VectorXi & pvpq ) @@ -443,15 +443,15 @@ void BaseNRSolverSingleSlack::fill_jacobian_matrix_kown_sparsity_p const int n_pvpq = static_cast(pvpq.size()); // real_type * J_x_ptr = J_.valuePtr(); - const int n_cols = static_cast(BaseNRSolver::J_.cols()); // equal to nrow + const int n_cols = static_cast(BaseNRAlgo::J_.cols()); // equal to nrow unsigned int pos_el = 0; for (int col_id=0; col_id < n_cols; ++col_id){ - for (Eigen::SparseMatrix::InnerIterator it(BaseNRSolver::J_, col_id); it; ++it) + for (Eigen::SparseMatrix::InnerIterator it(BaseNRAlgo::J_, col_id); it; ++it) { const auto row_id = it.row(); // only one if is necessary (magic !) // top rows are "real" part and bottom rows are imaginary part (you can check) - it.valueRef() = row_id < n_pvpq ? std::real(*BaseNRSolver::value_map_[pos_el]) : std::imag(*BaseNRSolver::value_map_[pos_el]); + it.valueRef() = row_id < n_pvpq ? std::real(*BaseNRAlgo::value_map_[pos_el]) : std::imag(*BaseNRAlgo::value_map_[pos_el]); // go to the next element ++pos_el; } diff --git a/src/GaussSeidelSolver.cpp b/src/powerflow_algorithm/GaussSeidelAlgo.cpp similarity index 96% rename from src/GaussSeidelSolver.cpp rename to src/powerflow_algorithm/GaussSeidelAlgo.cpp index 365d02a..448dc9f 100644 --- a/src/GaussSeidelSolver.cpp +++ b/src/powerflow_algorithm/GaussSeidelAlgo.cpp @@ -6,9 +6,9 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "GaussSeidelSolver.h" +#include "GaussSeidelAlgo.h" -bool GaussSeidelSolver::compute_pf(const Eigen::SparseMatrix & Ybus, +bool GaussSeidelAlgo::compute_pf(const Eigen::SparseMatrix & Ybus, CplxVect & V, const CplxVect & Sbus, const Eigen::VectorXi & slack_ids, @@ -78,7 +78,7 @@ bool GaussSeidelSolver::compute_pf(const Eigen::SparseMatrix & Ybus, return res; } -void GaussSeidelSolver::one_iter(CplxVect & tmp_Sbus, +void GaussSeidelAlgo::one_iter(CplxVect & tmp_Sbus, const Eigen::SparseMatrix & Ybus, const Eigen::VectorXi & pv, const Eigen::VectorXi & pq) diff --git a/src/GaussSeidelSolver.h b/src/powerflow_algorithm/GaussSeidelAlgo.h similarity index 81% rename from src/GaussSeidelSolver.h rename to src/powerflow_algorithm/GaussSeidelAlgo.h index 26e0a46..2939966 100644 --- a/src/GaussSeidelSolver.h +++ b/src/powerflow_algorithm/GaussSeidelAlgo.h @@ -6,17 +6,17 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef GAUSSSEIDELSOLVER_H -#define GAUSSSEIDELSOLVER_H +#ifndef GAUSSSEIDEL_ALGO_H +#define GAUSSSEIDEL_ALGO_H -#include "BaseSolver.h" +#include "BaseAlgo.h" -class GaussSeidelSolver : public BaseSolver +class GaussSeidelAlgo : public BaseAlgo { public: - GaussSeidelSolver():BaseSolver() {}; + GaussSeidelAlgo():BaseAlgo(true) {}; - ~GaussSeidelSolver(){} + ~GaussSeidelAlgo(){} // todo can be factorized Eigen::SparseMatrix get_J(){ @@ -47,9 +47,9 @@ class GaussSeidelSolver : public BaseSolver private: // no copy allowed - GaussSeidelSolver( const GaussSeidelSolver & ) ; - GaussSeidelSolver & operator=( const GaussSeidelSolver & ) ; + GaussSeidelAlgo( const GaussSeidelAlgo & ) =delete; + GaussSeidelAlgo & operator=( const GaussSeidelAlgo & ) =delete; }; -#endif // GAUSSSEIDELSOLVER_H +#endif // GAUSSSEIDEL_ALGO_H diff --git a/src/GaussSeidelSynchSolver.cpp b/src/powerflow_algorithm/GaussSeidelSynchAlgo.cpp similarity index 95% rename from src/GaussSeidelSynchSolver.cpp rename to src/powerflow_algorithm/GaussSeidelSynchAlgo.cpp index 9225709..1a31bdb 100644 --- a/src/GaussSeidelSynchSolver.cpp +++ b/src/powerflow_algorithm/GaussSeidelSynchAlgo.cpp @@ -6,9 +6,9 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#include "GaussSeidelSynchSolver.h" +#include "GaussSeidelSynchAlgo.h" -void GaussSeidelSynchSolver::one_iter(CplxVect & tmp_Sbus, +void GaussSeidelSynchAlgo::one_iter(CplxVect & tmp_Sbus, const Eigen::SparseMatrix & Ybus, const Eigen::VectorXi & pv, const Eigen::VectorXi & pq) diff --git a/src/GaussSeidelSynchSolver.h b/src/powerflow_algorithm/GaussSeidelSynchAlgo.h similarity index 68% rename from src/GaussSeidelSynchSolver.h rename to src/powerflow_algorithm/GaussSeidelSynchAlgo.h index 57607ea..7749f08 100644 --- a/src/GaussSeidelSynchSolver.h +++ b/src/powerflow_algorithm/GaussSeidelSynchAlgo.h @@ -6,21 +6,21 @@ // SPDX-License-Identifier: MPL-2.0 // This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform. -#ifndef GAUSSSEIDELSYNCHSOLVER_H -#define GAUSSSEIDELSYNCHSOLVER_H +#ifndef GAUSSSEIDELSYNCH_ALGO_H +#define GAUSSSEIDELSYNCH_ALGO_H -#include "GaussSeidelSolver.h" +#include "GaussSeidelAlgo.h" /** The gauss seidel method, where all the updates are happening in a synchronous way, instead of in a asynchronous way (like for standard gauss seidel) **/ -class GaussSeidelSynchSolver : public GaussSeidelSolver +class GaussSeidelSynchAlgo: public GaussSeidelAlgo { public: - GaussSeidelSynchSolver():GaussSeidelSolver() {}; + GaussSeidelSynchAlgo():GaussSeidelAlgo() {}; - ~GaussSeidelSynchSolver(){} + ~GaussSeidelSynchAlgo(){} protected: void one_iter(CplxVect & tmp_Sbus, @@ -31,9 +31,9 @@ class GaussSeidelSynchSolver : public GaussSeidelSolver private: // no copy allowed - GaussSeidelSynchSolver( const GaussSeidelSynchSolver & ) ; - GaussSeidelSynchSolver & operator=( const GaussSeidelSynchSolver & ) ; + GaussSeidelSynchAlgo( const GaussSeidelSynchAlgo & ) =delete; + GaussSeidelSynchAlgo & operator=( const GaussSeidelSynchAlgo & )=delete ; }; -#endif // GAUSSSEIDELSYNCHSOLVER_H +#endif // GAUSSSEIDELSYNCH_ALGO_H