From 15602b7c9370445006d23a67614c30991011f43c Mon Sep 17 00:00:00 2001 From: pelesh Date: Tue, 27 May 2025 12:58:12 -0400 Subject: [PATCH 1/5] Move `PowerSystemsData.hpp` and `CircuitGraph.hpp` to correct subdirectories (#114) * Mowe PowerSystemsData.hpp to src/Models/PowerFlow. * Move CircuitGraph.hpp to Model/PowerElectronics * Move Matpower parser to src/Model/PowerFlow --------- Co-authored-by: pelesh --- examples/PowerFlow/Grid3Bus/Grid3BusSys.cpp | 4 +- .../MatPowerTesting/MatPowerTesting.hpp | 2 +- .../MatPowerTesting/test_parse_branch_row.cpp | 4 +- .../MatPowerTesting/test_parse_bus_row.cpp | 4 +- .../MatPowerTesting/test_parse_gen_row.cpp | 4 +- .../test_parse_gencost_row.cpp | 4 +- .../MatPowerTesting/test_parse_matpower.cpp | 4 +- src/Model/PhasorDynamics/Branch/Branch.cpp | 2 +- src/Model/PhasorDynamics/Bus/Bus.cpp | 2 +- src/Model/PhasorDynamics/Bus/BusInfinite.cpp | 2 +- .../PhasorDynamics/BusFault/BusFault.cpp | 2 +- src/Model/PhasorDynamics/Load/Load.cpp | 1 - .../PowerElectronics/Capacitor/Capacitor.hpp | 1 - .../PowerElectronics/CircuitComponent.hpp | 1 - .../PowerElectronics}/CircuitGraph.hpp | 0 .../DistributedGenerator.hpp | 1 - .../InductionMotor/InductionMotor.hpp | 1 - .../PowerElectronics/Inductor/Inductor.hpp | 1 - .../LinearTransformer/LinearTransformer.hpp | 1 - .../MicrogridBusDQ/MicrogridBusDQ.hpp | 1 - .../MicrogridLine/MicrogridLine.hpp | 1 - .../MicrogridLoad/MicrogridLoad.hpp | 1 - .../PowerElectronics/Resistor/Resistor.hpp | 1 - .../SynchronousMachine/SynchronousMachine.hpp | 1 - .../SystemModelPowerElectronics.hpp | 2 +- .../TransmissionLine/TransmissionLine.hpp | 1 - .../VoltageSource/VoltageSource.hpp | 1 - src/Model/PowerFlow/Branch/Branch.cpp | 2 +- src/Model/PowerFlow/Bus/BusFactory.hpp | 2 +- src/Model/PowerFlow/Bus/BusPQ.hpp | 2 +- src/Model/PowerFlow/Bus/BusPV.hpp | 2 +- src/Model/PowerFlow/Bus/BusSlack.hpp | 2 +- .../PowerFlow/Generator/GeneratorFactory.hpp | 2 +- src/Model/PowerFlow/Generator/GeneratorPQ.hpp | 2 +- src/Model/PowerFlow/Generator/GeneratorPV.hpp | 2 +- .../PowerFlow/Generator/GeneratorSlack.hpp | 3 +- src/Model/PowerFlow/Load/Load.hpp | 2 +- src/Model/PowerFlow/MatpowerParser.hpp | 299 ++++++++++++++++++ src/{ => Model/PowerFlow}/PowerSystemData.hpp | 0 src/Utilities/FileIO.hpp | 287 ----------------- 40 files changed, 327 insertions(+), 330 deletions(-) rename src/{ => Model/PowerElectronics}/CircuitGraph.hpp (100%) create mode 100644 src/Model/PowerFlow/MatpowerParser.hpp rename src/{ => Model/PowerFlow}/PowerSystemData.hpp (100%) diff --git a/examples/PowerFlow/Grid3Bus/Grid3BusSys.cpp b/examples/PowerFlow/Grid3Bus/Grid3BusSys.cpp index 28ba4430..b5d83076 100644 --- a/examples/PowerFlow/Grid3Bus/Grid3BusSys.cpp +++ b/examples/PowerFlow/Grid3Bus/Grid3BusSys.cpp @@ -20,11 +20,11 @@ #include #include #include +#include #include +#include #include -#include #include -#include #include static const std::string BUS3_DATA_STRING = R"( diff --git a/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp index 1da0e95c..648296a4 100644 --- a/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp +++ b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp @@ -12,7 +12,7 @@ #include #include -#include +#include #include namespace diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp index e5ff203f..442dda53 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp @@ -1,8 +1,8 @@ #include #include "MatPowerTesting.hpp" -#include -#include +#include +#include using namespace GridKit; using namespace GridKit::Testing; diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp index 60a7c914..ff874dcf 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp @@ -1,8 +1,8 @@ #include #include "MatPowerTesting.hpp" -#include -#include +#include +#include using namespace GridKit; using namespace GridKit::Testing; diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp index 45ce4a18..437f0af7 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp @@ -1,8 +1,8 @@ #include #include "MatPowerTesting.hpp" -#include -#include +#include +#include using namespace GridKit; using namespace GridKit::Testing; diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp index e1b9e061..68dad345 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp @@ -1,8 +1,8 @@ #include #include "MatPowerTesting.hpp" -#include -#include +#include +#include using namespace GridKit; using namespace GridKit::Testing; diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp index fb5bd196..c384743f 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp @@ -1,8 +1,8 @@ #include #include "MatPowerTesting.hpp" -#include -#include +#include +#include using namespace GridKit; using namespace GridKit::Testing; diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index 0ccbe1a1..c65b3367 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PhasorDynamics/Bus/Bus.cpp b/src/Model/PhasorDynamics/Bus/Bus.cpp index 7253ee66..3dfaad6e 100644 --- a/src/Model/PhasorDynamics/Bus/Bus.cpp +++ b/src/Model/PhasorDynamics/Bus/Bus.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PhasorDynamics/Bus/BusInfinite.cpp b/src/Model/PhasorDynamics/Bus/BusInfinite.cpp index f4f18b83..61fa5d20 100644 --- a/src/Model/PhasorDynamics/Bus/BusInfinite.cpp +++ b/src/Model/PhasorDynamics/Bus/BusInfinite.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PhasorDynamics/BusFault/BusFault.cpp b/src/Model/PhasorDynamics/BusFault/BusFault.cpp index d2f519ee..7b12af30 100644 --- a/src/Model/PhasorDynamics/BusFault/BusFault.cpp +++ b/src/Model/PhasorDynamics/BusFault/BusFault.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PhasorDynamics/Load/Load.cpp b/src/Model/PhasorDynamics/Load/Load.cpp index b7c51505..e1ccb26c 100644 --- a/src/Model/PhasorDynamics/Load/Load.cpp +++ b/src/Model/PhasorDynamics/Load/Load.cpp @@ -5,7 +5,6 @@ #include #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/Capacitor/Capacitor.hpp b/src/Model/PowerElectronics/Capacitor/Capacitor.hpp index b86ed913..1357150e 100644 --- a/src/Model/PowerElectronics/Capacitor/Capacitor.hpp +++ b/src/Model/PowerElectronics/Capacitor/Capacitor.hpp @@ -4,7 +4,6 @@ #define _CAP_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/CircuitComponent.hpp b/src/Model/PowerElectronics/CircuitComponent.hpp index 56e0d4c5..f3efe363 100644 --- a/src/Model/PowerElectronics/CircuitComponent.hpp +++ b/src/Model/PowerElectronics/CircuitComponent.hpp @@ -6,7 +6,6 @@ #include #include -#include namespace GridKit { diff --git a/src/CircuitGraph.hpp b/src/Model/PowerElectronics/CircuitGraph.hpp similarity index 100% rename from src/CircuitGraph.hpp rename to src/Model/PowerElectronics/CircuitGraph.hpp diff --git a/src/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp b/src/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp index 96654cf6..a7540646 100644 --- a/src/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp +++ b/src/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp @@ -4,7 +4,6 @@ #define _CAP_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/InductionMotor/InductionMotor.hpp b/src/Model/PowerElectronics/InductionMotor/InductionMotor.hpp index 007e7fd5..1e02ef0c 100644 --- a/src/Model/PowerElectronics/InductionMotor/InductionMotor.hpp +++ b/src/Model/PowerElectronics/InductionMotor/InductionMotor.hpp @@ -4,7 +4,6 @@ #define _IMOTOR_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/Inductor/Inductor.hpp b/src/Model/PowerElectronics/Inductor/Inductor.hpp index c40920b0..685e69bb 100644 --- a/src/Model/PowerElectronics/Inductor/Inductor.hpp +++ b/src/Model/PowerElectronics/Inductor/Inductor.hpp @@ -4,7 +4,6 @@ #define _IND_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/LinearTransformer/LinearTransformer.hpp b/src/Model/PowerElectronics/LinearTransformer/LinearTransformer.hpp index b1e9c57c..7be8bd59 100644 --- a/src/Model/PowerElectronics/LinearTransformer/LinearTransformer.hpp +++ b/src/Model/PowerElectronics/LinearTransformer/LinearTransformer.hpp @@ -4,7 +4,6 @@ #define _LTRANS_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp b/src/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp index 46173c5a..150618c2 100644 --- a/src/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp +++ b/src/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp @@ -4,7 +4,6 @@ #define _VIRBUSDQ_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp b/src/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp index 14d29b85..66304c59 100644 --- a/src/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp +++ b/src/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp @@ -4,7 +4,6 @@ #define _TRANLINE_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp b/src/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp index 685a0504..e05764d5 100644 --- a/src/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp +++ b/src/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp @@ -4,7 +4,6 @@ #define _TRANLOAD_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/Resistor/Resistor.hpp b/src/Model/PowerElectronics/Resistor/Resistor.hpp index 0821b992..06186351 100644 --- a/src/Model/PowerElectronics/Resistor/Resistor.hpp +++ b/src/Model/PowerElectronics/Resistor/Resistor.hpp @@ -4,7 +4,6 @@ #define _RES_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/SynchronousMachine/SynchronousMachine.hpp b/src/Model/PowerElectronics/SynchronousMachine/SynchronousMachine.hpp index 74766d15..2c98384b 100644 --- a/src/Model/PowerElectronics/SynchronousMachine/SynchronousMachine.hpp +++ b/src/Model/PowerElectronics/SynchronousMachine/SynchronousMachine.hpp @@ -6,7 +6,6 @@ #include #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/SystemModelPowerElectronics.hpp b/src/Model/PowerElectronics/SystemModelPowerElectronics.hpp index 10548309..7c82ec70 100644 --- a/src/Model/PowerElectronics/SystemModelPowerElectronics.hpp +++ b/src/Model/PowerElectronics/SystemModelPowerElectronics.hpp @@ -6,8 +6,8 @@ #include #include -#include #include +#include #include namespace GridKit diff --git a/src/Model/PowerElectronics/TransmissionLine/TransmissionLine.hpp b/src/Model/PowerElectronics/TransmissionLine/TransmissionLine.hpp index badd0cf9..18610692 100644 --- a/src/Model/PowerElectronics/TransmissionLine/TransmissionLine.hpp +++ b/src/Model/PowerElectronics/TransmissionLine/TransmissionLine.hpp @@ -4,7 +4,6 @@ #define _TRANLINE_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerElectronics/VoltageSource/VoltageSource.hpp b/src/Model/PowerElectronics/VoltageSource/VoltageSource.hpp index d3b630a0..03d7a1e0 100644 --- a/src/Model/PowerElectronics/VoltageSource/VoltageSource.hpp +++ b/src/Model/PowerElectronics/VoltageSource/VoltageSource.hpp @@ -4,7 +4,6 @@ #define _VOSO_HPP_ #include -#include namespace GridKit { diff --git a/src/Model/PowerFlow/Branch/Branch.cpp b/src/Model/PowerFlow/Branch/Branch.cpp index 08b94326..ecbc1b6d 100644 --- a/src/Model/PowerFlow/Branch/Branch.cpp +++ b/src/Model/PowerFlow/Branch/Branch.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Bus/BusFactory.hpp b/src/Model/PowerFlow/Bus/BusFactory.hpp index 5a1c6e95..dc91c38b 100644 --- a/src/Model/PowerFlow/Bus/BusFactory.hpp +++ b/src/Model/PowerFlow/Bus/BusFactory.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Bus/BusPQ.hpp b/src/Model/PowerFlow/Bus/BusPQ.hpp index fd96ca39..e0b76b16 100644 --- a/src/Model/PowerFlow/Bus/BusPQ.hpp +++ b/src/Model/PowerFlow/Bus/BusPQ.hpp @@ -3,7 +3,7 @@ #define _BUS_PQ_HPP_ #include "BaseBus.hpp" -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Bus/BusPV.hpp b/src/Model/PowerFlow/Bus/BusPV.hpp index ac180e92..b4ea8fd8 100644 --- a/src/Model/PowerFlow/Bus/BusPV.hpp +++ b/src/Model/PowerFlow/Bus/BusPV.hpp @@ -5,7 +5,7 @@ #include #include "BaseBus.hpp" -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Bus/BusSlack.hpp b/src/Model/PowerFlow/Bus/BusSlack.hpp index 96d2ba66..4a693e43 100644 --- a/src/Model/PowerFlow/Bus/BusSlack.hpp +++ b/src/Model/PowerFlow/Bus/BusSlack.hpp @@ -3,7 +3,7 @@ #define _BUS_SLACK_HPP_ #include "BaseBus.hpp" -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Generator/GeneratorFactory.hpp b/src/Model/PowerFlow/Generator/GeneratorFactory.hpp index 07d1ac4c..daedd23b 100644 --- a/src/Model/PowerFlow/Generator/GeneratorFactory.hpp +++ b/src/Model/PowerFlow/Generator/GeneratorFactory.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Generator/GeneratorPQ.hpp b/src/Model/PowerFlow/Generator/GeneratorPQ.hpp index 1561c61b..0628257e 100644 --- a/src/Model/PowerFlow/Generator/GeneratorPQ.hpp +++ b/src/Model/PowerFlow/Generator/GeneratorPQ.hpp @@ -5,7 +5,7 @@ #include "GeneratorBase.hpp" #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Generator/GeneratorPV.hpp b/src/Model/PowerFlow/Generator/GeneratorPV.hpp index 7882114e..efe629f1 100644 --- a/src/Model/PowerFlow/Generator/GeneratorPV.hpp +++ b/src/Model/PowerFlow/Generator/GeneratorPV.hpp @@ -5,7 +5,7 @@ #include "GeneratorBase.hpp" #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Generator/GeneratorSlack.hpp b/src/Model/PowerFlow/Generator/GeneratorSlack.hpp index fa5b1bef..43a7c321 100644 --- a/src/Model/PowerFlow/Generator/GeneratorSlack.hpp +++ b/src/Model/PowerFlow/Generator/GeneratorSlack.hpp @@ -1,11 +1,10 @@ #pragma once -// #include #include #include "GeneratorBase.hpp" -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/Load/Load.hpp b/src/Model/PowerFlow/Load/Load.hpp index a9c17eb0..5091761c 100644 --- a/src/Model/PowerFlow/Load/Load.hpp +++ b/src/Model/PowerFlow/Load/Load.hpp @@ -3,7 +3,7 @@ #define _LOAD_HPP_ #include -#include +#include namespace GridKit { diff --git a/src/Model/PowerFlow/MatpowerParser.hpp b/src/Model/PowerFlow/MatpowerParser.hpp new file mode 100644 index 00000000..dcf926ef --- /dev/null +++ b/src/Model/PowerFlow/MatpowerParser.hpp @@ -0,0 +1,299 @@ + +/** + * @file FileIO.hpp + * @author Slaven Peles + * + * Contains definition of a utility for reading lookup tables. + * + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace GridKit +{ + using namespace GridKit::PowerSystemData; + + static const std::string matlab_syntax_error{ + "Only a subset of Matlab syntax is supported." + "\n\t'=' for assignment must be on the same line as the field, eg " + "`mpc.version = '2'`." + "\n\tOpen brace ('[') must be on the same line as the field for matrix " + "initialization." + "\n\tEach row of a matrix must be terminated by a semicolon."}; + + std::ostream& logs() + { +#ifndef NDEBUG + std::cerr << "[FileIO.hpp]: "; + return std::cerr; +#else + static std::ofstream ofs; + ofs.setstate(std::ios_base::badbit); + return ofs; +#endif + } + + void ltrim(std::string& s) + { + const std::string nothing = ""; + s = std::regex_replace(s, std::regex("^\\s+"), nothing); + } + + void rtrim(std::string& s) + { + const std::string nothing = ""; + s = std::regex_replace(s, std::regex("\\s+$"), nothing); + } + + void trim_matlab_comments(std::string& s) + { + const std::string nothing = ""; + s = std::regex_replace(s, std::regex("%.+"), nothing); + } + + // Retrive MATPOWER component from assignment line. + // + // For example, the string " mpc.bus = [ ... ] % Some comment" will + // return the value "bus". + std::string getMatPowerComponent(const std::string& line) + { + logs() << "Getting matpower component from line\n"; + std::regex pat("mpc.([a-zA-Z]+)\\s*=.+"); + std::smatch matches; + std::string component; + if (std::regex_match(line, matches, pat)) + { + component = matches[1].str(); + } + else + { + throw std::runtime_error(matlab_syntax_error + "\nGot line " + line); + } + ltrim(component); + rtrim(component); + return component; + } + + // Ensure that all of the given line has been consumed, and that the only + // remaining non-whitespace character left in the line is a semicolon. + void checkEndOfMatrixRow(std::istream& is) + { + std::string rest; + is >> rest; + ltrim(rest); + rtrim(rest); + if (rest != ";") + throw std::runtime_error(matlab_syntax_error); + } + + template + void readMatPowerBusRow(const std::string& row, BusData& br, LoadData& lr) + { + logs() << "Parsing MATPOWER bus row\n"; + std::stringstream is(row); + is >> br.bus_i // Bus ID + >> br.type // Bus type: 1 = PQ, 2 = PV, 3 = ref, 4 = isolated + >> lr.Pd // Active power demand [MW] + >> lr.Qd // Reactive power demand [MVAr] + >> br.Gs // Shunt conductance (MW demanded at V = 1.0 p.u.) + >> br.Bs // Shunt susceptance (MVAr injected at V = 1.0 p.u.) + >> br.area // Area number (>0) + >> br.Vm // Voltage magnitude (p.u.) + >> br.Va // Voltage phase (deg) + >> br.baseKV // Base voltage [kV] + >> br.zone // Loss zone number (>0) + >> br.Vmax // Maximum voltage magnitude (p.u.) + >> br.Vmin; // Minimum voltage magnitude (p.u.) + + lr.bus_i = br.bus_i; + + // std::cout << br.str(); + // logs() << "Read BusData with the following values:\n" << br.str(); + // return br; + } + + template + void readMatPowerGenRow(GenData& gr, std::string& row) + { + logs() << "Parsing MATPOWER gen row\n"; + std::stringstream is(row); + is >> gr.bus >> gr.Pg >> gr.Qg >> gr.Qmax >> gr.Qmin >> gr.Vg >> gr.mBase + >> gr.status >> gr.Pmax >> gr.Pmin >> gr.Pc1 >> gr.Pc2 >> gr.Qc1min + >> gr.Qc1max >> gr.Qc2min >> gr.Qc2max >> gr.ramp_agc >> gr.ramp_10 + >> gr.ramp_30 >> gr.ramp_q >> gr.apf; + checkEndOfMatrixRow(is); + } + + template + void readMatPowerBranchRow(BranchData& br, std::string& row) + { + logs() << "Parsing MATPOWER branch row\n"; + std::stringstream is(row); + is >> br.fbus >> br.tbus >> br.r >> br.x >> br.b >> br.rateA >> br.rateB + >> br.rateC >> br.ratio >> br.angle >> br.status >> br.angmin + >> br.angmax; + checkEndOfMatrixRow(is); + } + + template + void readMatPowerGenCostRow(GenCostData& gcr, std::string& row) + { + logs() << "Parsing MATPOWER gen cost row\n"; + // Ensure last character is semicolon. + rtrim(row); + if (row[row.size() - 1] != ';') + throw std::runtime_error(matlab_syntax_error + "\nGot line " + row); + + std::stringstream is(row); + is >> gcr.kind >> gcr.startup >> gcr.shutdown >> gcr.n; + + for (RealT r; is >> r;) + { + gcr.rest.push_back(r); + } + } + + template + void readMatPowerVersion(SystemModelData& mp, std::string& line) + { + logs() << "Parsing matpower version\n"; + std::regex pat("mpc\\.version\\s*=\\s*'([0-9])';"); + std::smatch matches; + if (std::regex_match(line, matches, pat)) + { + mp.version = matches[1].str(); + } + else + { + throw std::runtime_error(matlab_syntax_error + "\nGot line '" + line + "'"); + } + } + + template + void readMatPowerBaseMVA(SystemModelData& mp, std::string& line) + { + std::regex pat("mpc\\.baseMVA\\s*=\\s*([0-9]+);"); + std::smatch matches; + if (std::regex_match(line, matches, pat)) + { + std::string s = matches[1]; + mp.baseMVA = std::atof(s.c_str()); + } + else + { + throw std::runtime_error(matlab_syntax_error + "\nGot line '" + line + "'"); + } + } + + template + void readMatPowerFile(SystemModelData& mp, std::string& filename) + { + std::ifstream ifs{filename}; + readMatPower(mp, ifs); + } + + template + void readMatPower(SystemModelData& mp, std::istream& is) + { + using BusDataT = BusData; + using GenDataT = GenData; + using BranchDataT = BranchData; + using GenCostDataT = GenCostData; + using LoadDataT = LoadData; + + for (std::string line; std::getline(is, line);) + { + // Trim whitespace and remove comments + ltrim(line); + rtrim(line); + logs() << line << "\n"; + trim_matlab_comments(line); + + // Skip empty lines and comment-only lines + if (line.size() == 0) + continue; + + // Skip the matlab function declaration + if (line.find("function") != std::string::npos) + continue; + + // Check for MATPOWER component definitions + if (line.find("mpc") != std::string::npos) + { + const std::string component = getMatPowerComponent(line); + logs() << "Got component: '" << component << "'\n"; + // First, parse matrix components + if (component == "bus") + { + while (std::getline(is, line)) + { + if (line.find("];") != std::string::npos) + break; + BusDataT br; + LoadDataT lr; + readMatPowerBusRow(line, br, lr); + mp.bus.push_back(std::move(br)); + mp.load.push_back(std::move(lr)); + } + } + else if (component == "gen") + { + while (std::getline(is, line)) + { + if (line.find("];") != std::string::npos) + break; + GenDataT gr; + readMatPowerGenRow(gr, line); + mp.gen.push_back(gr); + } + } + else if (component == "branch") + { + while (std::getline(is, line)) + { + if (line.find("];") != std::string::npos) + break; + BranchDataT br; + readMatPowerBranchRow(br, line); + mp.branch.push_back(br); + } + } + else if (component == "gencost") + { + while (std::getline(is, line)) + { + if (line.find("];") != std::string::npos) + break; + GenCostDataT gcr; + readMatPowerGenCostRow(gcr, line); + mp.gencost.push_back(gcr); + } + } + + // Next, parse scalar components + else if (component == "version") + { + readMatPowerVersion(mp, line); + } + else if (component == "baseMVA") + { + readMatPowerBaseMVA(mp, line); + } + } + } + } + + // } // namespace PowerSystemsData +} // namespace GridKit diff --git a/src/PowerSystemData.hpp b/src/Model/PowerFlow/PowerSystemData.hpp similarity index 100% rename from src/PowerSystemData.hpp rename to src/Model/PowerFlow/PowerSystemData.hpp diff --git a/src/Utilities/FileIO.hpp b/src/Utilities/FileIO.hpp index 130b79c1..1e4c2b3d 100644 --- a/src/Utilities/FileIO.hpp +++ b/src/Utilities/FileIO.hpp @@ -8,200 +8,14 @@ */ #pragma once -#include #include #include -#include #include -#include #include #include -#include - -namespace -{ - - using namespace GridKit; - using namespace GridKit::PowerSystemData; - - static const std::string matlab_syntax_error{ - "Only a subset of Matlab syntax is supported." - "\n\t'=' for assignment must be on the same line as the field, eg " - "`mpc.version = '2'`." - "\n\tOpen brace ('[') must be on the same line as the field for matrix " - "initialization." - "\n\tEach row of a matrix must be terminated by a semicolon."}; - - std::ostream& logs() - { -#ifndef NDEBUG - std::cerr << "[FileIO.hpp]: "; - return std::cerr; -#else - static std::ofstream ofs; - ofs.setstate(std::ios_base::badbit); - return ofs; -#endif - } - - void ltrim(std::string& s) - { - const std::string nothing = ""; - s = std::regex_replace(s, std::regex("^\\s+"), nothing); - } - - void rtrim(std::string& s) - { - const std::string nothing = ""; - s = std::regex_replace(s, std::regex("\\s+$"), nothing); - } - - void trim_matlab_comments(std::string& s) - { - const std::string nothing = ""; - s = std::regex_replace(s, std::regex("%.+"), nothing); - } - - // Retrive MATPOWER component from assignment line. - // - // For example, the string " mpc.bus = [ ... ] % Some comment" will - // return the value "bus". - std::string getMatPowerComponent(const std::string& line) - { - logs() << "Getting matpower component from line\n"; - std::regex pat("mpc.([a-zA-Z]+)\\s*=.+"); - std::smatch matches; - std::string component; - if (std::regex_match(line, matches, pat)) - { - component = matches[1].str(); - } - else - { - throw std::runtime_error(matlab_syntax_error + "\nGot line " + line); - } - ltrim(component); - rtrim(component); - return component; - } - - // Ensure that all of the given line has been consumed, and that the only - // remaining non-whitespace character left in the line is a semicolon. - void checkEndOfMatrixRow(std::istream& is) - { - std::string rest; - is >> rest; - ltrim(rest); - rtrim(rest); - if (rest != ";") - throw std::runtime_error(matlab_syntax_error); - } - - template - void readMatPowerBusRow(const std::string& row, BusData& br, LoadData& lr) - { - logs() << "Parsing MATPOWER bus row\n"; - std::stringstream is(row); - is >> br.bus_i // Bus ID - >> br.type // Bus type: 1 = PQ, 2 = PV, 3 = ref, 4 = isolated - >> lr.Pd // Active power demand [MW] - >> lr.Qd // Reactive power demand [MVAr] - >> br.Gs // Shunt conductance (MW demanded at V = 1.0 p.u.) - >> br.Bs // Shunt susceptance (MVAr injected at V = 1.0 p.u.) - >> br.area // Area number (>0) - >> br.Vm // Voltage magnitude (p.u.) - >> br.Va // Voltage phase (deg) - >> br.baseKV // Base voltage [kV] - >> br.zone // Loss zone number (>0) - >> br.Vmax // Maximum voltage magnitude (p.u.) - >> br.Vmin; // Minimum voltage magnitude (p.u.) - - lr.bus_i = br.bus_i; - - // std::cout << br.str(); - // logs() << "Read BusData with the following values:\n" << br.str(); - // return br; - } - - template - void readMatPowerGenRow(GenData& gr, std::string& row) - { - logs() << "Parsing MATPOWER gen row\n"; - std::stringstream is(row); - is >> gr.bus >> gr.Pg >> gr.Qg >> gr.Qmax >> gr.Qmin >> gr.Vg >> gr.mBase - >> gr.status >> gr.Pmax >> gr.Pmin >> gr.Pc1 >> gr.Pc2 >> gr.Qc1min - >> gr.Qc1max >> gr.Qc2min >> gr.Qc2max >> gr.ramp_agc >> gr.ramp_10 - >> gr.ramp_30 >> gr.ramp_q >> gr.apf; - checkEndOfMatrixRow(is); - } - - template - void readMatPowerBranchRow(BranchData& br, std::string& row) - { - logs() << "Parsing MATPOWER branch row\n"; - std::stringstream is(row); - is >> br.fbus >> br.tbus >> br.r >> br.x >> br.b >> br.rateA >> br.rateB - >> br.rateC >> br.ratio >> br.angle >> br.status >> br.angmin - >> br.angmax; - checkEndOfMatrixRow(is); - } - - template - void readMatPowerGenCostRow(GenCostData& gcr, std::string& row) - { - logs() << "Parsing MATPOWER gen cost row\n"; - // Ensure last character is semicolon. - rtrim(row); - if (row[row.size() - 1] != ';') - throw std::runtime_error(matlab_syntax_error + "\nGot line " + row); - - std::stringstream is(row); - is >> gcr.kind >> gcr.startup >> gcr.shutdown >> gcr.n; - - for (RealT r; is >> r;) - { - gcr.rest.push_back(r); - } - } - - template - void readMatPowerVersion(SystemModelData& mp, std::string& line) - { - logs() << "Parsing matpower version\n"; - std::regex pat("mpc\\.version\\s*=\\s*'([0-9])';"); - std::smatch matches; - if (std::regex_match(line, matches, pat)) - { - mp.version = matches[1].str(); - } - else - { - throw std::runtime_error(matlab_syntax_error + "\nGot line '" + line + "'"); - } - } - - template - void readMatPowerBaseMVA(SystemModelData& mp, std::string& line) - { - std::regex pat("mpc\\.baseMVA\\s*=\\s*([0-9]+);"); - std::smatch matches; - if (std::regex_match(line, matches, pat)) - { - std::string s = matches[1]; - mp.baseMVA = std::atof(s.c_str()); - } - else - { - throw std::runtime_error(matlab_syntax_error + "\nGot line '" + line + "'"); - } - } - -} // namespace - namespace GridKit { - /** * @brief Reads in an input stream of tabulated data * @@ -281,105 +95,4 @@ namespace GridKit std::cout << "\n"; } } - - template - void readMatPowerFile(SystemModelData& mp, std::string& filename) - { - std::ifstream ifs{filename}; - readMatPower(mp, ifs); - } - - template - void readMatPower(SystemModelData& mp, std::istream& is) - { - using BusDataT = BusData; - using GenDataT = GenData; - using BranchDataT = BranchData; - using GenCostDataT = GenCostData; - using LoadDataT = LoadData; - - for (std::string line; std::getline(is, line);) - { - // Trim whitespace and remove comments - ltrim(line); - rtrim(line); - logs() << line << "\n"; - trim_matlab_comments(line); - - // Skip empty lines and comment-only lines - if (line.size() == 0) - continue; - - // Skip the matlab function declaration - if (line.find("function") != std::string::npos) - continue; - - // Check for MATPOWER component definitions - if (line.find("mpc") != std::string::npos) - { - const std::string component = getMatPowerComponent(line); - logs() << "Got component: '" << component << "'\n"; - // First, parse matrix components - if (component == "bus") - { - while (std::getline(is, line)) - { - if (line.find("];") != std::string::npos) - break; - BusDataT br; - LoadDataT lr; - readMatPowerBusRow(line, br, lr); - mp.bus.push_back(std::move(br)); - mp.load.push_back(std::move(lr)); - } - } - else if (component == "gen") - { - while (std::getline(is, line)) - { - if (line.find("];") != std::string::npos) - break; - GenDataT gr; - readMatPowerGenRow(gr, line); - mp.gen.push_back(gr); - } - } - else if (component == "branch") - { - while (std::getline(is, line)) - { - if (line.find("];") != std::string::npos) - break; - BranchDataT br; - readMatPowerBranchRow(br, line); - mp.branch.push_back(br); - } - } - else if (component == "gencost") - { - while (std::getline(is, line)) - { - if (line.find("];") != std::string::npos) - break; - GenCostDataT gcr; - readMatPowerGenCostRow(gcr, line); - mp.gencost.push_back(gcr); - } - } - - // Next, parse scalar components - else if (component == "version") - { - readMatPowerVersion(mp, line); - } - else if (component == "baseMVA") - { - readMatPowerBaseMVA(mp, line); - } - } - } - } - } // namespace GridKit From 1a92315c259c41db419824b88e1a866944724e41 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Mon, 26 May 2025 23:44:48 -0400 Subject: [PATCH 2/5] Create phasor dynamics bus and branch data structures. --- src/Model/PhasorDynamics/Branch/Branch.cpp | 12 ++--- src/Model/PhasorDynamics/Branch/Branch.hpp | 22 ++++----- .../PhasorDynamics/Branch/BranchData.hpp | 36 +++++++++++++++ src/Model/PhasorDynamics/Bus/Bus.cpp | 10 ++--- src/Model/PhasorDynamics/Bus/Bus.hpp | 45 ++----------------- src/Model/PhasorDynamics/Bus/BusData.hpp | 33 ++++++++++++++ 6 files changed, 94 insertions(+), 64 deletions(-) create mode 100644 src/Model/PhasorDynamics/Branch/BranchData.hpp create mode 100644 src/Model/PhasorDynamics/Bus/BusData.hpp diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index c65b3367..26ac9902 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include namespace GridKit { @@ -73,13 +73,13 @@ namespace GridKit } template - Branch::Branch(bus_type* bus1, bus_type* bus2, BranchData& data) + Branch::Branch(bus_type* bus1, bus_type* bus2, model_data_type& data) : bus1_(bus1), bus2_(bus2), - R_(data.r), - X_(data.x), - G_(0.0), - B_(data.b), + R_(data.R), + X_(data.X), + G_(data.G), + B_(data.B), bus1ID_(data.fbus), bus2ID_(data.tbus) { diff --git a/src/Model/PhasorDynamics/Branch/Branch.hpp b/src/Model/PhasorDynamics/Branch/Branch.hpp index 9d6ac38d..5ca14744 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.hpp +++ b/src/Model/PhasorDynamics/Branch/Branch.hpp @@ -13,7 +13,7 @@ // Forward declarations. namespace GridKit { - namespace PowerSystemData + namespace PhasorDynamics { template struct BranchData; @@ -51,21 +51,21 @@ namespace GridKit using Component::yp_; using Component::tag_; using Component::f_; - using Component::g_; - using Component::yB_; - using Component::ypB_; - using Component::fB_; - using Component::gB_; - using Component::param_; + // using Component::g_; + // using Component::yB_; + // using Component::ypB_; + // using Component::fB_; + // using Component::gB_; + // using Component::param_; - using bus_type = BusBase; - using real_type = typename Component::real_type; - using BranchData = GridKit::PowerSystemData::BranchData; + using real_type = typename Component::real_type; + using bus_type = BusBase; + using model_data_type = BranchData; public: Branch(bus_type* bus1, bus_type* bus2); Branch(bus_type* bus1, bus_type* bus2, real_type R, real_type X, real_type G, real_type B); - Branch(bus_type* bus1, bus_type* bus2, BranchData& data); + Branch(bus_type* bus1, bus_type* bus2, model_data_type& data); virtual ~Branch(); virtual int allocate() override; diff --git a/src/Model/PhasorDynamics/Branch/BranchData.hpp b/src/Model/PhasorDynamics/Branch/BranchData.hpp new file mode 100644 index 00000000..fdfc0f40 --- /dev/null +++ b/src/Model/PhasorDynamics/Branch/BranchData.hpp @@ -0,0 +1,36 @@ +/** + * @file BranchData.hpp + * @author Slaven Peles (peless@ornl.gov) + * @brief Modeling data for branches (transmission lines) + * + */ +#pragma once + + +namespace GridKit +{ + namespace PhasorDynamics + { + /** + * @brief Contains modeling data for a Branch + * + * @tparam RealT Real parameter data type + * @tparam IdxT Integer parameter data type + * + * Integer parameters are of the same type as matrix and vector indices. + * + * @todo Decide on naming scheme for model parameters. + */ + template + struct BranchData + { + RealT R{0.0}; ///< line series resistance + RealT X{0.0}; ///< line series reactance + RealT G{0.0}; ///< line shunt conductance + RealT B{0.0}; ///< line shunt charging + + IdxT fbus{0}; ///< Unique ID of bus 1 + IdxT tbus{0}; ///< Unique ID of bus 2 + }; + } +} \ No newline at end of file diff --git a/src/Model/PhasorDynamics/Bus/Bus.cpp b/src/Model/PhasorDynamics/Bus/Bus.cpp index 3dfaad6e..4aa83f9c 100644 --- a/src/Model/PhasorDynamics/Bus/Bus.cpp +++ b/src/Model/PhasorDynamics/Bus/Bus.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace GridKit { @@ -61,10 +61,10 @@ namespace GridKit * @param[in] data - structure with bus data */ template - Bus::Bus(BusData& data) - : BusBase(data.bus_i), - Vr0_(data.Vm * cos(data.Va)), - Vi0_(data.Vm * sin(data.Va)) + Bus::Bus(DataT& data) + : BusBase(data.bus_id), + Vr0_(data.Vr0), + Vi0_(data.Vi0) { // std::cout << "Create Bus..." << std::endl; // std::cout << "Number of equations is " << size_ << std::endl; diff --git a/src/Model/PhasorDynamics/Bus/Bus.hpp b/src/Model/PhasorDynamics/Bus/Bus.hpp index aedaa2a8..aef4f593 100644 --- a/src/Model/PhasorDynamics/Bus/Bus.hpp +++ b/src/Model/PhasorDynamics/Bus/Bus.hpp @@ -6,7 +6,7 @@ // Forward declaration of BusData structure namespace GridKit { - namespace PowerSystemData + namespace PhasorDynamics { template struct BusData; @@ -39,11 +39,11 @@ namespace GridKit public: using real_type = typename BusBase::real_type; - using BusData = GridKit::PowerSystemData::BusData; + using DataT = BusData; Bus(); Bus(ScalarT Vr, ScalarT Vi); - Bus(BusData& data); + Bus(DataT& data); virtual ~Bus(); virtual int allocate() override; @@ -101,45 +101,6 @@ namespace GridKit return f_[1]; } - // virtual ScalarT& VrB() override - // { - // return yB_[0]; - // } - - // virtual const ScalarT& VrB() const override - // { - // return yB_[0]; - // } - - // virtual ScalarT& ViB() override - // { - // return yB_[1]; - // } - - // virtual const ScalarT& ViB() const override - // { - // return yB_[1]; - // } - - // virtual ScalarT& IrB() override - // { - // return fB_[0]; - // } - - // virtual const ScalarT& IrB() const override - // { - // return fB_[0]; - // } - - // virtual ScalarT& IiB() override - // { - // return fB_[1]; - // } - - // virtual const ScalarT& IiB() const override - // { - // return fB_[1]; - // } private: ScalarT Vr0_{0.0}; diff --git a/src/Model/PhasorDynamics/Bus/BusData.hpp b/src/Model/PhasorDynamics/Bus/BusData.hpp new file mode 100644 index 00000000..35956d98 --- /dev/null +++ b/src/Model/PhasorDynamics/Bus/BusData.hpp @@ -0,0 +1,33 @@ +/** + * @file BusData.hpp + * @author Slaven Peles (peless@ornl.gov) + * @brief Modeling data for branches (transmission lines) + * + */ +#pragma once + + +namespace GridKit +{ + namespace PhasorDynamics + { + /** + * @brief Contains modeling data for a Bus + * + * @tparam RealT Real parameter data type + * @tparam IdxT Integer parameter data type + * + * Integer parameters are of the same type as matrix and vector indices. + * + * @todo Decide on naming scheme for model parameters. + */ + template + struct BusData + { + RealT Vr0{0.0}; ///< Initial value for real bus voltage + RealT Vi0{0.0}; ///< Initial value for imaginary bus voltage + + IdxT bus_id{0}; ///< Unique ID of bus 1 + }; + } +} \ No newline at end of file From 109b8c52fc8854a4eb0440e04edb0e9048209ed3 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Tue, 27 May 2025 00:15:21 -0400 Subject: [PATCH 3/5] Add load data structure. --- src/Model/PhasorDynamics/Branch/Branch.cpp | 12 +++---- src/Model/PhasorDynamics/Branch/Branch.hpp | 16 +++------ .../PhasorDynamics/Branch/BranchData.hpp | 4 +-- src/Model/PhasorDynamics/Bus/BusData.hpp | 2 +- src/Model/PhasorDynamics/Load/Load.cpp | 3 +- src/Model/PhasorDynamics/Load/Load.hpp | 22 ++++++++----- src/Model/PhasorDynamics/Load/LoadData.hpp | 33 +++++++++++++++++++ 7 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 src/Model/PhasorDynamics/Load/LoadData.hpp diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index 26ac9902..a22ca09f 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -36,8 +36,8 @@ namespace GridKit X_(0.01), G_(0.0), B_(0.0), - bus1ID_(0), - bus2ID_(0) + bus1_id_(0), + bus2_id_(0) { size_ = 0; } @@ -67,8 +67,8 @@ namespace GridKit X_(X), G_(G), B_(B), - bus1ID_(0), - bus2ID_(0) + bus1_id_(0), + bus2_id_(0) { } @@ -80,8 +80,8 @@ namespace GridKit X_(data.X), G_(data.G), B_(data.B), - bus1ID_(data.fbus), - bus2ID_(data.tbus) + bus1_id_(data.bus1_id), + bus2_id_(data.bus2_id) { size_ = 0; } diff --git a/src/Model/PhasorDynamics/Branch/Branch.hpp b/src/Model/PhasorDynamics/Branch/Branch.hpp index 5ca14744..2f41e688 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.hpp +++ b/src/Model/PhasorDynamics/Branch/Branch.hpp @@ -11,21 +11,15 @@ #include // Forward declarations. -namespace GridKit -{ - namespace PhasorDynamics - { - template - struct BranchData; - } -} // namespace GridKit - namespace GridKit { namespace PhasorDynamics { template class BusBase; + + template + struct BranchData; } } // namespace GridKit @@ -154,8 +148,8 @@ namespace GridKit real_type X_; real_type G_; real_type B_; - const IdxT bus1ID_; - const IdxT bus2ID_; + const IdxT bus1_id_; + const IdxT bus2_id_; }; } // namespace PhasorDynamics diff --git a/src/Model/PhasorDynamics/Branch/BranchData.hpp b/src/Model/PhasorDynamics/Branch/BranchData.hpp index fdfc0f40..4c531bae 100644 --- a/src/Model/PhasorDynamics/Branch/BranchData.hpp +++ b/src/Model/PhasorDynamics/Branch/BranchData.hpp @@ -29,8 +29,8 @@ namespace GridKit RealT G{0.0}; ///< line shunt conductance RealT B{0.0}; ///< line shunt charging - IdxT fbus{0}; ///< Unique ID of bus 1 - IdxT tbus{0}; ///< Unique ID of bus 2 + IdxT bus1_id{0}; ///< Unique ID of bus 1 + IdxT bus2_id{0}; ///< Unique ID of bus 2 }; } } \ No newline at end of file diff --git a/src/Model/PhasorDynamics/Bus/BusData.hpp b/src/Model/PhasorDynamics/Bus/BusData.hpp index 35956d98..664dc653 100644 --- a/src/Model/PhasorDynamics/Bus/BusData.hpp +++ b/src/Model/PhasorDynamics/Bus/BusData.hpp @@ -1,7 +1,7 @@ /** * @file BusData.hpp * @author Slaven Peles (peless@ornl.gov) - * @brief Modeling data for branches (transmission lines) + * @brief Modeling data for buses (nodes) * */ #pragma once diff --git a/src/Model/PhasorDynamics/Load/Load.cpp b/src/Model/PhasorDynamics/Load/Load.cpp index e1ccb26c..4dcb2372 100644 --- a/src/Model/PhasorDynamics/Load/Load.cpp +++ b/src/Model/PhasorDynamics/Load/Load.cpp @@ -5,6 +5,7 @@ #include #include +#include namespace GridKit { @@ -34,14 +35,12 @@ namespace GridKit : bus_(bus), R_(R), X_(X) - { } template Load::Load(bus_type* bus, IdxT component_id) : bus_(bus) - { size_ = 0; component_id_ = component_id; diff --git a/src/Model/PhasorDynamics/Load/Load.hpp b/src/Model/PhasorDynamics/Load/Load.hpp index d8535509..30a653f5 100644 --- a/src/Model/PhasorDynamics/Load/Load.hpp +++ b/src/Model/PhasorDynamics/Load/Load.hpp @@ -11,7 +11,10 @@ namespace GridKit { template class BusBase; - } + + template + struct LoadData; + } // namespace PhasorDynamics } // namespace GridKit namespace GridKit @@ -33,16 +36,17 @@ namespace GridKit using Component::yp_; using Component::tag_; using Component::f_; - using Component::g_; - using Component::yB_; - using Component::ypB_; - using Component::fB_; - using Component::gB_; - using Component::param_; + // using Component::g_; + // using Component::yB_; + // using Component::ypB_; + // using Component::fB_; + // using Component::gB_; + // using Component::param_; using Component::component_id_; - using bus_type = BusBase; - using real_type = typename Component::real_type; + using real_type = typename Component::real_type; + using bus_type = BusBase; + using model_data_type = LoadData; public: Load(bus_type* bus); diff --git a/src/Model/PhasorDynamics/Load/LoadData.hpp b/src/Model/PhasorDynamics/Load/LoadData.hpp new file mode 100644 index 00000000..cde0d153 --- /dev/null +++ b/src/Model/PhasorDynamics/Load/LoadData.hpp @@ -0,0 +1,33 @@ +/** + * @file LoadData.hpp + * @author Slaven Peles (peless@ornl.gov) + * @brief Modeling data for loads + * + */ +#pragma once + + +namespace GridKit +{ + namespace PhasorDynamics + { + /** + * @brief Contains modeling data for a Load + * + * @tparam RealT Real parameter data type + * @tparam IdxT Integer parameter data type + * + * Integer parameters are of the same type as matrix and vector indices. + * + * @todo Decide on naming scheme for model parameters. + */ + template + struct LoadData + { + RealT R{0.0}; ///< load resistance + RealT X{0.0}; ///< load reactance + + IdxT bus_id{0}; ///< Unique ID of bus 1 + }; + } +} \ No newline at end of file From 205f729443cf479c12ee875919a84a388b6cda81 Mon Sep 17 00:00:00 2001 From: pelesh Date: Tue, 27 May 2025 04:18:58 +0000 Subject: [PATCH 4/5] Apply pre-commmit fixes --- src/Model/PhasorDynamics/Branch/Branch.cpp | 2 +- src/Model/PhasorDynamics/Branch/Branch.hpp | 2 +- src/Model/PhasorDynamics/Branch/BranchData.hpp | 15 +++++++-------- src/Model/PhasorDynamics/Bus/Bus.hpp | 1 - src/Model/PhasorDynamics/Bus/BusData.hpp | 13 ++++++------- src/Model/PhasorDynamics/Load/LoadData.hpp | 13 ++++++------- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index a22ca09f..a2277dfc 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -12,8 +12,8 @@ #include #include -#include #include +#include namespace GridKit { diff --git a/src/Model/PhasorDynamics/Branch/Branch.hpp b/src/Model/PhasorDynamics/Branch/Branch.hpp index 2f41e688..88ac13aa 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.hpp +++ b/src/Model/PhasorDynamics/Branch/Branch.hpp @@ -20,7 +20,7 @@ namespace GridKit template struct BranchData; - } + } // namespace PhasorDynamics } // namespace GridKit namespace GridKit diff --git a/src/Model/PhasorDynamics/Branch/BranchData.hpp b/src/Model/PhasorDynamics/Branch/BranchData.hpp index 4c531bae..9f66fd28 100644 --- a/src/Model/PhasorDynamics/Branch/BranchData.hpp +++ b/src/Model/PhasorDynamics/Branch/BranchData.hpp @@ -2,23 +2,22 @@ * @file BranchData.hpp * @author Slaven Peles (peless@ornl.gov) * @brief Modeling data for branches (transmission lines) - * + * */ #pragma once - namespace GridKit { namespace PhasorDynamics { /** * @brief Contains modeling data for a Branch - * + * * @tparam RealT Real parameter data type * @tparam IdxT Integer parameter data type - * + * * Integer parameters are of the same type as matrix and vector indices. - * + * * @todo Decide on naming scheme for model parameters. */ template @@ -27,10 +26,10 @@ namespace GridKit RealT R{0.0}; ///< line series resistance RealT X{0.0}; ///< line series reactance RealT G{0.0}; ///< line shunt conductance - RealT B{0.0}; ///< line shunt charging + RealT B{0.0}; ///< line shunt charging IdxT bus1_id{0}; ///< Unique ID of bus 1 IdxT bus2_id{0}; ///< Unique ID of bus 2 }; - } -} \ No newline at end of file + } // namespace PhasorDynamics +} // namespace GridKit diff --git a/src/Model/PhasorDynamics/Bus/Bus.hpp b/src/Model/PhasorDynamics/Bus/Bus.hpp index aef4f593..57828ef0 100644 --- a/src/Model/PhasorDynamics/Bus/Bus.hpp +++ b/src/Model/PhasorDynamics/Bus/Bus.hpp @@ -101,7 +101,6 @@ namespace GridKit return f_[1]; } - private: ScalarT Vr0_{0.0}; ScalarT Vi0_{0.0}; diff --git a/src/Model/PhasorDynamics/Bus/BusData.hpp b/src/Model/PhasorDynamics/Bus/BusData.hpp index 664dc653..abd8ab59 100644 --- a/src/Model/PhasorDynamics/Bus/BusData.hpp +++ b/src/Model/PhasorDynamics/Bus/BusData.hpp @@ -2,23 +2,22 @@ * @file BusData.hpp * @author Slaven Peles (peless@ornl.gov) * @brief Modeling data for buses (nodes) - * + * */ #pragma once - namespace GridKit { namespace PhasorDynamics { /** * @brief Contains modeling data for a Bus - * + * * @tparam RealT Real parameter data type * @tparam IdxT Integer parameter data type - * + * * Integer parameters are of the same type as matrix and vector indices. - * + * * @todo Decide on naming scheme for model parameters. */ template @@ -29,5 +28,5 @@ namespace GridKit IdxT bus_id{0}; ///< Unique ID of bus 1 }; - } -} \ No newline at end of file + } // namespace PhasorDynamics +} // namespace GridKit diff --git a/src/Model/PhasorDynamics/Load/LoadData.hpp b/src/Model/PhasorDynamics/Load/LoadData.hpp index cde0d153..f2892927 100644 --- a/src/Model/PhasorDynamics/Load/LoadData.hpp +++ b/src/Model/PhasorDynamics/Load/LoadData.hpp @@ -2,23 +2,22 @@ * @file LoadData.hpp * @author Slaven Peles (peless@ornl.gov) * @brief Modeling data for loads - * + * */ #pragma once - namespace GridKit { namespace PhasorDynamics { /** * @brief Contains modeling data for a Load - * + * * @tparam RealT Real parameter data type * @tparam IdxT Integer parameter data type - * + * * Integer parameters are of the same type as matrix and vector indices. - * + * * @todo Decide on naming scheme for model parameters. */ template @@ -29,5 +28,5 @@ namespace GridKit IdxT bus_id{0}; ///< Unique ID of bus 1 }; - } -} \ No newline at end of file + } // namespace PhasorDynamics +} // namespace GridKit From 8a10d7123cbec824f65159adbcad10459a3599e2 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Tue, 27 May 2025 00:31:26 -0400 Subject: [PATCH 5/5] Use BusData with BusInfinite. --- src/Model/PhasorDynamics/Bus/BusInfinite.cpp | 10 ++-- src/Model/PhasorDynamics/Bus/BusInfinite.hpp | 46 ++----------------- .../PhasorDynamics/BusFault/BusFault.cpp | 1 - 3 files changed, 8 insertions(+), 49 deletions(-) diff --git a/src/Model/PhasorDynamics/Bus/BusInfinite.cpp b/src/Model/PhasorDynamics/Bus/BusInfinite.cpp index 61fa5d20..e8a231d1 100644 --- a/src/Model/PhasorDynamics/Bus/BusInfinite.cpp +++ b/src/Model/PhasorDynamics/Bus/BusInfinite.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include namespace GridKit { @@ -66,10 +66,10 @@ namespace GridKit * @param[in] data - structure with bus data */ template - BusInfinite::BusInfinite(BusData& data) - : BusBase(data.bus_i), - Vr_(data.Vm * cos(data.Va)), - Vi_(data.Vm * sin(data.Va)) + BusInfinite::BusInfinite(DataT& data) + : BusBase(data.bus_id), + Vr_(data.Vr0), + Vi_(data.Vi0) { size_ = 0; } diff --git a/src/Model/PhasorDynamics/Bus/BusInfinite.hpp b/src/Model/PhasorDynamics/Bus/BusInfinite.hpp index 6acdd7c6..0657ec37 100644 --- a/src/Model/PhasorDynamics/Bus/BusInfinite.hpp +++ b/src/Model/PhasorDynamics/Bus/BusInfinite.hpp @@ -6,7 +6,7 @@ // Forward declaration of BusData structure namespace GridKit { - namespace PowerSystemData + namespace PhasorDynamics { template struct BusData; @@ -37,11 +37,11 @@ namespace GridKit public: using real_type = typename BusBase::real_type; - using BusData = GridKit::PowerSystemData::BusData; + using DataT = BusData; BusInfinite(); BusInfinite(ScalarT Vr, ScalarT Vi); - BusInfinite(BusData& data); + BusInfinite(DataT& data); virtual ~BusInfinite(); virtual int allocate() override; @@ -99,46 +99,6 @@ namespace GridKit return Ii_; } - // virtual ScalarT& VrB() override - // { - // return VrB_; - // } - - // virtual const ScalarT& VrB() const override - // { - // return VrB_; - // } - - // virtual ScalarT& ViB() override - // { - // return ViB_; - // } - - // virtual const ScalarT& ViB() const override - // { - // return ViB_; - // } - - // virtual ScalarT& IrB() override - // { - // return IrB_; - // } - - // virtual const ScalarT& IrB() const override - // { - // return IrB_; - // } - - // virtual ScalarT& IiB() override - // { - // return IiB_; - // } - - // virtual const ScalarT& IiB() const override - // { - // return IiB_; - // } - private: ScalarT Vr_{0.0}; ScalarT Vi_{0.0}; diff --git a/src/Model/PhasorDynamics/BusFault/BusFault.cpp b/src/Model/PhasorDynamics/BusFault/BusFault.cpp index 7b12af30..d3832407 100644 --- a/src/Model/PhasorDynamics/BusFault/BusFault.cpp +++ b/src/Model/PhasorDynamics/BusFault/BusFault.cpp @@ -13,7 +13,6 @@ #include #include -#include namespace GridKit {