From 0cd80785cdb579d00e4ecf6b48be7c710b8758df Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 18:21:25 -0400 Subject: [PATCH 1/9] Separate MatPower from other test utilities. --- .../MatPowerTesting/MatPowerTesting.hpp | 219 ++++++++++++++++++ .../MatPowerTesting/test_parse_branch_row.cpp | 2 +- .../MatPowerTesting/test_parse_bus_row.cpp | 2 +- .../MatPowerTesting/test_parse_gen_row.cpp | 2 +- .../test_parse_gencost_row.cpp | 2 +- .../MatPowerTesting/test_parse_matpower.cpp | 2 +- src/Utilities/Testing.hpp | 199 +--------------- 7 files changed, 228 insertions(+), 200 deletions(-) create mode 100644 examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp diff --git a/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp new file mode 100644 index 00000000..dbd9b908 --- /dev/null +++ b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp @@ -0,0 +1,219 @@ +/** + * @file Testing.hpp + * @author Slaven Peles + * + * Contains utilies for testing. + * + */ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +namespace +{ + + static constexpr double tol_ = 1e-8; + + inline std::ostream& errs() + { + std::cerr << "[examples/PowerFlow/MatPowerTesting.hpp]: "; + return std::cerr; + } + +} // namespace + +namespace GridKit +{ + namespace Testing + { + + template + inline bool isEqual(PowerSystemData::GenCostData a, + PowerSystemData::GenCostData b, + RealT tol = tol_) + { + (void) tol; // suppress warning + int fail = 0; + fail += a.kind != b.kind; + fail += a.startup != b.startup; + fail += a.shutdown != b.shutdown; + fail += a.n != b.n; + if (fail) + { + errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); + } + return fail == 0; + } + + template + inline bool isEqual(PowerSystemData::GenData a, + PowerSystemData::GenData b, + RealT tol = tol_) + { + int fail = 0; + + fail += a.bus != b.bus; + fail += !isEqual(a.Pg, b.Pg, tol); + fail += !isEqual(a.Qg, b.Qg, tol); + fail += !isEqual(a.Qmax, b.Qmax, tol); + fail += !isEqual(a.Qmin, b.Qmin, tol); + fail += !isEqual(a.Vg, b.Vg, tol); + fail += a.mBase != b.mBase; + fail += a.status != b.status; + fail += a.Pmax != b.Pmax; + fail += a.Pmin != b.Pmin; + fail += a.Pc1 != b.Pc1; + fail += a.Pc2 != b.Pc2; + fail += a.Qc1min != b.Qc1min; + fail += a.Qc1max != b.Qc1max; + fail += a.Qc2min != b.Qc2min; + fail += a.Qc2max != b.Qc2max; + fail += a.ramp_agc != b.ramp_agc; + fail += a.ramp_10 != b.ramp_10; + fail += a.ramp_30 != b.ramp_30; + fail += a.ramp_q != b.ramp_q; + fail += a.apf != b.apf; + + if (fail) + { + errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); + } + return fail == 0; + } + + template + inline bool isEqual(PowerSystemData::BusData a, + PowerSystemData::BusData b, + RealT tol = tol_) + { + int fail = 0; + + fail += a.bus_i != b.bus_i; + fail += a.type != b.type; + fail += a.Gs != b.Gs; + fail += a.Bs != b.Bs; + fail += a.area != b.area; + fail += !isEqual(a.Vm, b.Vm, tol); + fail += !isEqual(a.Va, b.Va, tol); + fail += a.baseKV != b.baseKV; + fail += a.zone != b.zone; + fail += !isEqual(a.Vmax, b.Vmax, tol); + fail += !isEqual(a.Vmin, b.Vmin, tol); + + if (fail) + { + errs() << "bus_i: a=" << a.bus_i << ", b=" << b.bus_i << "\n" + << "type: a=" << a.type << ", b=" << b.type << "\n" + << "Gs: a=" << a.Gs << ", b=" << b.Gs << "\n" + << "Bs: a=" << a.Bs << ", b=" << b.Bs << "\n" + << "area: a=" << a.area << ", b=" << b.area << "\n" + << "Vm: a=" << a.Vm << ", b=" << b.Vm << "\n" + << "Va: a=" << a.Va << ", b=" << b.Va << "\n" + << "baseKV: a=" << a.baseKV << ", b=" << b.baseKV << "\n" + << "zone: a=" << a.zone << ", b=" << b.zone << "\n" + << "Vmax: a=" << a.Vmax << ", b=" << b.Vmax << "\n" + << "Vmin: a=" << a.Vmin << ", b=" << b.Vmin << "\n"; + } + return fail == 0; + } + + template + inline bool isEqual(PowerSystemData::LoadData a, + PowerSystemData::LoadData b, + RealT tol = tol_) + { + int fail = 0; + + fail += a.bus_i != b.bus_i; + fail += !isEqual(a.Pd, b.Pd, tol); + fail += !isEqual(a.Qd, b.Qd, tol); + + if (fail) + { + errs() << "bus_i: a=" << a.bus_i << ", b=" << b.bus_i << "\n" + << "Pd: a=" << a.Pd << ", b=" << b.Pd << "\n" + << "Qd: a=" << a.Qd << ", b=" << b.Qd << "\n"; + } + return fail == 0; + } + + template + inline bool isEqual(PowerSystemData::BranchData a, + PowerSystemData::BranchData b, + RealT tol = tol_) + { + int fail = 0; + + fail += a.fbus != b.fbus; + fail += a.tbus != b.tbus; + fail += !isEqual(a.r, b.r, tol); + fail += !isEqual(a.x, b.x, tol); + fail += !isEqual(a.b, b.b, tol); + fail += a.rateA != b.rateA; + fail += a.rateB != b.rateB; + fail += a.rateC != b.rateC; + fail += a.ratio != b.ratio; + fail += a.angle != b.angle; + fail += a.status != b.status; + fail += a.angmin != b.angmin; + fail += a.angmax != b.angmax; + + if (fail) + { + errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); + } + return fail == 0; + } + + template + inline bool isEqual(std::vector a, std::vector b, double tol = tol_) + { + if (a.size() != b.size()) + throw std::runtime_error([&] + { + std::stringstream errs; + errs << "Containers do not have the same size!\n" + << "\tGot a.size() == " << a.size() << "\n" + << "\tGot b.size() == " << b.size() << "\n"; + return errs.str(); }()); + + int fail = 0; + for (std::size_t i = 0; i < a.size(); i++) + { + if (!isEqual(a[i], b[i], tol)) + { + fail++; + errs() << "[isEqual>]: Got failure with i=" << i << ".\n"; + } + } + + return fail == 0; + } + + template + inline bool isEqual(PowerSystemData::SystemModelData a, + PowerSystemData::SystemModelData b) + { + int fail = 0; + + fail += a.version != b.version; + fail += a.baseMVA != b.baseMVA; + fail += !isEqual(a.bus, b.bus); + fail += !isEqual(a.gen, b.gen); + fail += !isEqual(a.gencost, b.gencost); + fail += !isEqual(a.branch, b.branch); + fail += !isEqual(a.load, b.load); + + return fail == 0; + } + + } // namespace Testing + +} // namespace GridKit diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp index f6bb3c0d..300a989e 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include "MatPowerTesting.hpp" 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 92c1062c..7d223cb3 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include "MatPowerTesting.hpp" 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 63a1d550..e850a04c 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include "MatPowerTesting.hpp" 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 adddc1ed..d1c90c0b 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include "MatPowerTesting.hpp" 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 2994804b..5088245c 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include "MatPowerTesting.hpp" using namespace GridKit; using namespace GridKit::Testing; diff --git a/src/Utilities/Testing.hpp b/src/Utilities/Testing.hpp index 6f91ef1f..e0f38868 100644 --- a/src/Utilities/Testing.hpp +++ b/src/Utilities/Testing.hpp @@ -15,18 +15,6 @@ #include -namespace -{ - - static constexpr double tol_ = 1e-8; - - inline std::ostream& errs() - { - std::cerr << "[Utils/Testing.hpp]: "; - return std::cerr; - } - -} // namespace namespace GridKit { @@ -42,186 +30,6 @@ namespace GridKit return (error < tol); } - template - inline bool isEqual(PowerSystemData::GenCostData a, - PowerSystemData::GenCostData b, - RealT tol = tol_) - { - (void) tol; // suppress warning - int fail = 0; - fail += a.kind != b.kind; - fail += a.startup != b.startup; - fail += a.shutdown != b.shutdown; - fail += a.n != b.n; - if (fail) - { - errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); - } - return fail == 0; - } - - template - inline bool isEqual(PowerSystemData::GenData a, - PowerSystemData::GenData b, - RealT tol = tol_) - { - int fail = 0; - - fail += a.bus != b.bus; - fail += !isEqual(a.Pg, b.Pg, tol); - fail += !isEqual(a.Qg, b.Qg, tol); - fail += !isEqual(a.Qmax, b.Qmax, tol); - fail += !isEqual(a.Qmin, b.Qmin, tol); - fail += !isEqual(a.Vg, b.Vg, tol); - fail += a.mBase != b.mBase; - fail += a.status != b.status; - fail += a.Pmax != b.Pmax; - fail += a.Pmin != b.Pmin; - fail += a.Pc1 != b.Pc1; - fail += a.Pc2 != b.Pc2; - fail += a.Qc1min != b.Qc1min; - fail += a.Qc1max != b.Qc1max; - fail += a.Qc2min != b.Qc2min; - fail += a.Qc2max != b.Qc2max; - fail += a.ramp_agc != b.ramp_agc; - fail += a.ramp_10 != b.ramp_10; - fail += a.ramp_30 != b.ramp_30; - fail += a.ramp_q != b.ramp_q; - fail += a.apf != b.apf; - - if (fail) - { - errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); - } - return fail == 0; - } - - template - inline bool isEqual(PowerSystemData::BusData a, - PowerSystemData::BusData b, - RealT tol = tol_) - { - int fail = 0; - - fail += a.bus_i != b.bus_i; - fail += a.type != b.type; - fail += a.Gs != b.Gs; - fail += a.Bs != b.Bs; - fail += a.area != b.area; - fail += !isEqual(a.Vm, b.Vm, tol); - fail += !isEqual(a.Va, b.Va, tol); - fail += a.baseKV != b.baseKV; - fail += a.zone != b.zone; - fail += !isEqual(a.Vmax, b.Vmax, tol); - fail += !isEqual(a.Vmin, b.Vmin, tol); - - if (fail) - { - errs() << "bus_i: a=" << a.bus_i << ", b=" << b.bus_i << "\n" - << "type: a=" << a.type << ", b=" << b.type << "\n" - << "Gs: a=" << a.Gs << ", b=" << b.Gs << "\n" - << "Bs: a=" << a.Bs << ", b=" << b.Bs << "\n" - << "area: a=" << a.area << ", b=" << b.area << "\n" - << "Vm: a=" << a.Vm << ", b=" << b.Vm << "\n" - << "Va: a=" << a.Va << ", b=" << b.Va << "\n" - << "baseKV: a=" << a.baseKV << ", b=" << b.baseKV << "\n" - << "zone: a=" << a.zone << ", b=" << b.zone << "\n" - << "Vmax: a=" << a.Vmax << ", b=" << b.Vmax << "\n" - << "Vmin: a=" << a.Vmin << ", b=" << b.Vmin << "\n"; - } - return fail == 0; - } - - template - inline bool isEqual(PowerSystemData::LoadData a, - PowerSystemData::LoadData b, - RealT tol = tol_) - { - int fail = 0; - - fail += a.bus_i != b.bus_i; - fail += !isEqual(a.Pd, b.Pd, tol); - fail += !isEqual(a.Qd, b.Qd, tol); - - if (fail) - { - errs() << "bus_i: a=" << a.bus_i << ", b=" << b.bus_i << "\n" - << "Pd: a=" << a.Pd << ", b=" << b.Pd << "\n" - << "Qd: a=" << a.Qd << ", b=" << b.Qd << "\n"; - } - return fail == 0; - } - - template - inline bool isEqual(PowerSystemData::BranchData a, - PowerSystemData::BranchData b, - RealT tol = tol_) - { - int fail = 0; - - fail += a.fbus != b.fbus; - fail += a.tbus != b.tbus; - fail += !isEqual(a.r, b.r, tol); - fail += !isEqual(a.x, b.x, tol); - fail += !isEqual(a.b, b.b, tol); - fail += a.rateA != b.rateA; - fail += a.rateB != b.rateB; - fail += a.rateC != b.rateC; - fail += a.ratio != b.ratio; - fail += a.angle != b.angle; - fail += a.status != b.status; - fail += a.angmin != b.angmin; - fail += a.angmax != b.angmax; - - if (fail) - { - errs() << "Got failure!\na=" << a.str() << "\nb=" << b.str(); - } - return fail == 0; - } - - template - inline bool isEqual(std::vector a, std::vector b, double tol = tol_) - { - if (a.size() != b.size()) - throw std::runtime_error([&] - { - std::stringstream errs; - errs << "Containers do not have the same size!\n" - << "\tGot a.size() == " << a.size() << "\n" - << "\tGot b.size() == " << b.size() << "\n"; - return errs.str(); }()); - - int fail = 0; - for (std::size_t i = 0; i < a.size(); i++) - { - if (!isEqual(a[i], b[i], tol)) - { - fail++; - errs() << "[isEqual>]: Got failure with i=" << i << ".\n"; - } - } - - return fail == 0; - } - - template - inline bool isEqual(PowerSystemData::SystemModelData a, - PowerSystemData::SystemModelData b) - { - int fail = 0; - - fail += a.version != b.version; - fail += a.baseMVA != b.baseMVA; - fail += !isEqual(a.bus, b.bus); - fail += !isEqual(a.gen, b.gen); - fail += !isEqual(a.gencost, b.gencost); - fail += !isEqual(a.branch, b.branch); - fail += !isEqual(a.load, b.load); - - return fail == 0; - } - /** * @brief Equatlity comparison between maps with a tolerance for the scalar value * @@ -243,7 +51,7 @@ namespace GridKit if (a.size() != b.size()) { fail++; - errs() << "Containers do not have the same size! " + std::cerr << "Containers do not have the same size! " << "a.size() = " << a.size() << ", and " << "b.size() = " << b.size() << "\n"; } @@ -256,7 +64,7 @@ namespace GridKit if (!isEqual(pair_a.second, it->second, tol)) { fail++; - errs() << "Mismatching map values! " + std::cerr << "Mismatching map values! " << "a.first = " << pair_a.first << ", " << "a.second = " << pair_a.second << ", and " << "b.second = " << it->second << "\n"; @@ -265,7 +73,7 @@ namespace GridKit else { fail++; - errs() << "Entry not found in the second container! " + std::cerr << "Entry not found in the second container! " << "a.first = " << pair_a.first << "\n"; } } @@ -273,6 +81,7 @@ namespace GridKit return fail == 0; } + } // namespace Testing } // namespace GridKit From 1dfc79e542815465a19f12271f210bbf4ab48888 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 18:22:00 -0400 Subject: [PATCH 2/9] Update figure path in power flow example README file. --- examples/PowerFlow/Grid3Bus/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PowerFlow/Grid3Bus/README.md b/examples/PowerFlow/Grid3Bus/README.md index 31bd6f9b..edf4d82e 100644 --- a/examples/PowerFlow/Grid3Bus/README.md +++ b/examples/PowerFlow/Grid3Bus/README.md @@ -10,7 +10,7 @@ The mathematical model of the power flow problem is formulated as a set of nonli The model and its parameters are described in Figure 1:
- + Figure 1: A simple 3-bus grid example. From 584bfe33ccba37eb62b0819b7532a28d1d8f1062 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 18:22:32 -0400 Subject: [PATCH 3/9] Fix warning coming from Variable class. --- src/LinearAlgebra/SparsityPattern/Variable.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LinearAlgebra/SparsityPattern/Variable.hpp b/src/LinearAlgebra/SparsityPattern/Variable.hpp index 5b95eb61..a81e8d22 100644 --- a/src/LinearAlgebra/SparsityPattern/Variable.hpp +++ b/src/LinearAlgebra/SparsityPattern/Variable.hpp @@ -260,7 +260,7 @@ namespace GridKit bool is_fixed_; ///< Constant parameter flag. mutable DependencyMap* dependencies_; - static const size_t INVALID_VAR_NUMBER = static_cast(-1); + static const size_t INVALID_VAR_NUMBER = static_cast(-1); }; //------------------------------------ From 4ade8585a3867d017ff5425e22142c8602ee02e1 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 18:25:25 -0400 Subject: [PATCH 4/9] Fix warnings in Matpower tests. --- examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp | 2 +- examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp | 2 +- examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp | 2 +- examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp | 2 +- examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp index 300a989e..def74628 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp @@ -33,7 +33,7 @@ mpc.branch = [ } // namespace -int main(int argc, char** argv) +int main(int /* argc */, char** /* argv */) { int fail = 0; std::vector> branch_answer{ diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp index 7d223cb3..d38d48d5 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_bus_row.cpp @@ -28,7 +28,7 @@ mpc.bus = [ } // namespace -int main(int argc, char** argv) +int main(int /* argc */, char** /* argv */) { int fail = 0; std::vector> bus_answer{ diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp index e850a04c..ca35230f 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gen_row.cpp @@ -30,7 +30,7 @@ mpc.gen = [ } // namespace -int main(int argc, char** argv) +int main(int /* argc */, char** /* argv */) { int fail = 0; std::vector> gen_answer{ diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp index d1c90c0b..c258375e 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_gencost_row.cpp @@ -33,7 +33,7 @@ mpc.gencost = [ } // namespace -int main(int argc, char** argv) +int main(int /* argc */, char** /* argv */) { int fail = 0; std::vector> gencost_answer{ diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp index 5088245c..a7f71393 100644 --- a/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp +++ b/examples/PowerFlow/MatPowerTesting/test_parse_matpower.cpp @@ -83,7 +83,7 @@ mpc.gencost = [ } // namespace -int main(int argc, char** argv) +int main(int /* argc */, char** /* argv */) { int fail = 0; From 0adb6e249ad30e2d4568d2e8ade86897ea9f9875 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 18:32:56 -0400 Subject: [PATCH 5/9] Reduce output from phasor dynamics Example 1 --- examples/PhasorDynamics/Example1/example1.cpp | 40 +++++++++++++------ examples/PhasorDynamics/Example2/example2.cpp | 2 +- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/examples/PhasorDynamics/Example1/example1.cpp b/examples/PhasorDynamics/Example1/example1.cpp index 9c998044..b273e503 100644 --- a/examples/PhasorDynamics/Example1/example1.cpp +++ b/examples/PhasorDynamics/Example1/example1.cpp @@ -126,6 +126,7 @@ int main() double stop = static_cast(clock()); double error_V = 0.0; // error in |V| + double error_w = 0.0; // error in rotor speed // Read through the simulation data stored in the buffer. // Since we captured by reference, output should be available @@ -141,22 +142,37 @@ int main() if (err > error_V) error_V = err; - std::cout << "GridKit: t = " << data.ti - << ", |V| = " << std::sqrt(data.Vr * data.Vr + data.Vi * data.Vi) - << ", w = " << (1.0 + data.dw) << "\n"; - std::cout << "Ref : t = " << ref_sol[0] - << ", |V| = " << ref_sol[2] - << ", w = " << ref_sol[1] - << "\n"; - std::cout << "Error in |V| = " - << err - << "\n"; - std::cout << "\n"; + err = std::abs(1.0 + data.dw - ref_sol[1])/(1.0 + ref_sol[1]); + if (err > error_w) + error_w = err; + + // // Optional output + // std::cout << "GridKit: t = " << data.ti + // << ", |V| = " << std::sqrt(data.Vr * data.Vr + data.Vi * data.Vi) + // << ", w = " << (1.0 + data.dw) << "\n"; + // std::cout << "Ref : t = " << ref_sol[0] + // << ", |V| = " << ref_sol[2] + // << ", w = " << ref_sol[1] + // << "\n"; + // std::cout << "Error in |V| = " + // << err + // << "\n"; + // std::cout << "\n"; } + double error_V_allowed = 2e-4; + double error_w_allowed = 1e-4; + + // Tolerances based on Powerworld reference accuracy int status = 0; std::cout << "Max error in |V| = " << error_V << "\n"; - if (error_V > 2e-4) + if (error_V > error_V_allowed) + { + std::cout << "Test failed with error too large!\n"; + status = 1; + } + std::cout << "Max error in w = " << error_w << "\n"; + if (error_w > error_w_allowed) { std::cout << "Test failed with error too large!\n"; status = 1; diff --git a/examples/PhasorDynamics/Example2/example2.cpp b/examples/PhasorDynamics/Example2/example2.cpp index a04103ca..f661d96c 100644 --- a/examples/PhasorDynamics/Example2/example2.cpp +++ b/examples/PhasorDynamics/Example2/example2.cpp @@ -183,7 +183,7 @@ int main() } // fileout.close(); - std::cout << "Worst error " << worst_error + std::cout << "Max error " << worst_error << " at time t = " << worst_error_time << "\n"; std::cout << "\n\nComplete in " << (stop - start) / CLOCKS_PER_SEC << " seconds\n"; From 45383ef8aa8b88685c47d27f615837ba47eec12e Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 21 May 2025 19:21:10 -0400 Subject: [PATCH 6/9] Monor fixes to DG and microgrid examples. --- .../DistributedGeneratorTest/DGTest.cpp | 27 +++++--- .../PowerElectronics/Microgrid/Microgrid.cpp | 63 +++++++++++++------ 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp b/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp index 28d0801b..60ca64d0 100644 --- a/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp +++ b/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp @@ -16,7 +16,7 @@ * @param argv * @return int */ -int main(int argc, char const* argv[]) +int main(int /* argc */, char const** /* argv */) { GridKit::DistributedGeneratorParameters parms; @@ -49,12 +49,12 @@ int main(int argc, char const* argv[]) dg->evaluateResidual(); - std::cout << "Output: {"; - for (double i : dg->getResidual()) - { - printf("%e ,", i); - } - std::cout << "}\n"; + // std::cout << "Output: {"; + // for (double i : dg->getResidual()) + // { + // printf("%e ,", i); + // } + // std::cout << "}\n"; // Generated from matlab code with same parameters and inputs std::vector true_vec{3.141592277589793e+02, @@ -74,11 +74,20 @@ int main(int argc, char const* argv[]) 3.337988298081817e+03, 2.684419146397466e+03}; - std::cout << "Test the Relative Error\n"; + std::cout << "Testing the DistributedGenerator model ...\n"; + double error_allowed = 10*std::numeric_limits::epsilon(); for (size_t i = 0; i < true_vec.size(); i++) { - printf("%e ,\n", (true_vec[i] - dg->getResidual()[i]) / true_vec[i]); + double error = std::abs(true_vec[i] - dg->getResidual()[i]) / std::abs(1.0 + true_vec[i]); + if (error > error_allowed) + { + std::cout << "Model error for equation " << i << " is: " << error << "\n"; + std::cout << "Maximum allowed error is: " << error_allowed << "\n"; + std::cout << "Test FAILED!\n"; + return 1; + } } + std::cout << "Test successful!\n"; return 0; } diff --git a/examples/PowerElectronics/Microgrid/Microgrid.cpp b/examples/PowerElectronics/Microgrid/Microgrid.cpp index 3ea4c803..882475ca 100644 --- a/examples/PowerElectronics/Microgrid/Microgrid.cpp +++ b/examples/PowerElectronics/Microgrid/Microgrid.cpp @@ -15,16 +15,17 @@ #include #include -int main(int argc, char const* argv[]) +int main(int /* argc */, char const** /* argv */) { - ///@todo Needs to be modified. Some components are small relative to others thus there error is high (or could be matlab vector issue) + /// @todo Needs to be modified. Some components are small relative to others thus + /// there error is high (or could be matlab vector issue) double abs_tol = 1.0e-8; double rel_tol = 1.0e-8; - size_t max_step_amount = 3000; + size_t max_step_number = 3000; bool use_jac = true; // Create model - GridKit::PowerElectronicsModel* sysmodel = new GridKit::PowerElectronicsModel(rel_tol, abs_tol, use_jac, max_step_amount); + auto* sysmodel = new GridKit::PowerElectronicsModel(rel_tol, abs_tol, use_jac, max_step_number); // Modeled after the problem in the paper double RN = 1.0e4; @@ -315,20 +316,21 @@ int main(int argc, char const* argv[]) sysmodel->initialize(); sysmodel->evaluateResidual(); - std::vector& fres = sysmodel->getResidual(); - std::cout << "Verify Intial Resisdual is Zero: {\n"; - for (size_t i = 0; i < fres.size(); i++) - { - printf("%lu : %e \n", i, fres[i]); - } - std::cout << "}\n"; + // // Optional debugging output + // std::vector& fres = sysmodel->getResidual(); + // std::cout << "Verify Intial Resisdual is Zero: {\n"; + // for (size_t i = 0; i < fres.size(); i++) + // { + // printf("%lu : %e \n", i, fres[i]); + // } + // std::cout << "}\n"; sysmodel->updateTime(0.0, 1.0e-8); sysmodel->evaluateJacobian(); std::cout << "Intial Jacobian with alpha:\n"; // Create numerical integrator and configure it for the generator model - AnalysisManager::Sundials::Ida* idas = new AnalysisManager::Sundials::Ida(sysmodel); + auto* idas = new AnalysisManager::Sundials::Ida(sysmodel); double t_init = 0.0; double t_final = 1.0; @@ -342,13 +344,14 @@ int main(int argc, char const* argv[]) std::vector& yfinial = sysmodel->y(); - std::cout << "Final Vector y\n"; - for (size_t i = 0; i < yfinial.size(); i++) - { - std::cout << yfinial[i] << "\n"; - } + // // Optional debugging output + // std::cout << "Final Vector y\n"; + // for (size_t i = 0; i < yfinial.size(); i++) + // { + // std::cout << yfinial[i] << "\n"; + // } - // Generate from MATLAB code ODE form with tolerances of 1e-12 + // Generated from MATLAB code ODE form with tolerances of 1e-12 std::vector true_vec{ 2.297543153595780e+04, 1.275311524125022e+04, @@ -421,11 +424,31 @@ int main(int argc, char const* argv[]) 3.604108939430972e+02, -3.492842627398574e+01}; - std::cout << "Test the Relative Error\n"; + // std::cout << "Test the Relative Error\n"; + // for (size_t i = 0; i < true_vec.size(); i++) + // { + // printf("%lu : %e ,\n", i, abs(true_vec[i] - yfinial[i]) / abs(true_vec[i])); + // } + + std::cout << "Testing the DistributedGenerator model ...\n"; + double error_allowed = 1e-4; + double max_error = 0.0; for (size_t i = 0; i < true_vec.size(); i++) { - printf("%lu : %e ,\n", i, abs(true_vec[i] - yfinial[i]) / abs(true_vec[i])); + double error = std::abs(true_vec[i] - yfinial[i]) / std::abs(1.0 + true_vec[i]); + if (error > max_error) + max_error = error; + if (error > error_allowed) + { + std::cout << "Model error for equation " << i << " is: " << error << "\n"; + std::cout << "Maximum allowed error is: " << error_allowed << "\n"; + std::cout << "Test FAILED!\n"; + return 1; + } } + std::cout << "Max error = " << max_error << "\n"; + std::cout << "Allowed error = " << error_allowed << "\n"; + std::cout << "Test successful!\n"; delete idas; delete sysmodel; From 17945b029e21525567761f87e04c8e0dff583444 Mon Sep 17 00:00:00 2001 From: pelesh Date: Wed, 21 May 2025 23:37:42 +0000 Subject: [PATCH 7/9] Apply pre-commmit fixes --- examples/PhasorDynamics/Example1/example1.cpp | 2 +- .../DistributedGeneratorTest/DGTest.cpp | 2 +- examples/PowerElectronics/Microgrid/Microgrid.cpp | 4 ++-- .../MatPowerTesting/test_parse_branch_row.cpp | 2 +- .../MatPowerTesting/test_parse_bus_row.cpp | 2 +- .../MatPowerTesting/test_parse_gen_row.cpp | 2 +- .../MatPowerTesting/test_parse_gencost_row.cpp | 2 +- .../MatPowerTesting/test_parse_matpower.cpp | 2 +- src/Utilities/Testing.hpp | 14 ++++++-------- 9 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/PhasorDynamics/Example1/example1.cpp b/examples/PhasorDynamics/Example1/example1.cpp index b273e503..e4f1fef6 100644 --- a/examples/PhasorDynamics/Example1/example1.cpp +++ b/examples/PhasorDynamics/Example1/example1.cpp @@ -142,7 +142,7 @@ int main() if (err > error_V) error_V = err; - err = std::abs(1.0 + data.dw - ref_sol[1])/(1.0 + ref_sol[1]); + err = std::abs(1.0 + data.dw - ref_sol[1]) / (1.0 + ref_sol[1]); if (err > error_w) error_w = err; diff --git a/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp b/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp index 60ca64d0..483c3e39 100644 --- a/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp +++ b/examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp @@ -75,7 +75,7 @@ int main(int /* argc */, char const** /* argv */) 2.684419146397466e+03}; std::cout << "Testing the DistributedGenerator model ...\n"; - double error_allowed = 10*std::numeric_limits::epsilon(); + double error_allowed = 10 * std::numeric_limits::epsilon(); for (size_t i = 0; i < true_vec.size(); i++) { double error = std::abs(true_vec[i] - dg->getResidual()[i]) / std::abs(1.0 + true_vec[i]); diff --git a/examples/PowerElectronics/Microgrid/Microgrid.cpp b/examples/PowerElectronics/Microgrid/Microgrid.cpp index 882475ca..5ca5f35c 100644 --- a/examples/PowerElectronics/Microgrid/Microgrid.cpp +++ b/examples/PowerElectronics/Microgrid/Microgrid.cpp @@ -17,7 +17,7 @@ int main(int /* argc */, char const** /* argv */) { - /// @todo Needs to be modified. Some components are small relative to others thus + /// @todo Needs to be modified. Some components are small relative to others thus /// there error is high (or could be matlab vector issue) double abs_tol = 1.0e-8; double rel_tol = 1.0e-8; @@ -432,7 +432,7 @@ int main(int /* argc */, char const** /* argv */) std::cout << "Testing the DistributedGenerator model ...\n"; double error_allowed = 1e-4; - double max_error = 0.0; + double max_error = 0.0; for (size_t i = 0; i < true_vec.size(); i++) { double error = std::abs(true_vec[i] - yfinial[i]) / std::abs(1.0 + true_vec[i]); diff --git a/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp b/examples/PowerFlow/MatPowerTesting/test_parse_branch_row.cpp index def74628..e5ff203f 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 "MatPowerTesting.hpp" 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 d38d48d5..60a7c914 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 "MatPowerTesting.hpp" 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 ca35230f..45ce4a18 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 "MatPowerTesting.hpp" 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 c258375e..e1b9e061 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 "MatPowerTesting.hpp" 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 a7f71393..fb5bd196 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 "MatPowerTesting.hpp" using namespace GridKit; using namespace GridKit::Testing; diff --git a/src/Utilities/Testing.hpp b/src/Utilities/Testing.hpp index e0f38868..294416a6 100644 --- a/src/Utilities/Testing.hpp +++ b/src/Utilities/Testing.hpp @@ -15,7 +15,6 @@ #include - namespace GridKit { namespace Testing @@ -52,8 +51,8 @@ namespace GridKit { fail++; std::cerr << "Containers do not have the same size! " - << "a.size() = " << a.size() << ", and " - << "b.size() = " << b.size() << "\n"; + << "a.size() = " << a.size() << ", and " + << "b.size() = " << b.size() << "\n"; } for (const auto& pair_a : a) @@ -65,23 +64,22 @@ namespace GridKit { fail++; std::cerr << "Mismatching map values! " - << "a.first = " << pair_a.first << ", " - << "a.second = " << pair_a.second << ", and " - << "b.second = " << it->second << "\n"; + << "a.first = " << pair_a.first << ", " + << "a.second = " << pair_a.second << ", and " + << "b.second = " << it->second << "\n"; } } else { fail++; std::cerr << "Entry not found in the second container! " - << "a.first = " << pair_a.first << "\n"; + << "a.first = " << pair_a.first << "\n"; } } return fail == 0; } - } // namespace Testing } // namespace GridKit From 35daa10443545bb0529132e42692a4e92f926cdd Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 22 May 2025 13:36:08 -0400 Subject: [PATCH 8/9] Minor corrections --- examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp | 5 ++--- src/Utilities/Testing.hpp | 3 --- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp index dbd9b908..1da0e95c 100644 --- a/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp +++ b/examples/PowerFlow/MatPowerTesting/MatPowerTesting.hpp @@ -1,8 +1,8 @@ /** - * @file Testing.hpp + * @file MatPowerTesting.hpp * @author Slaven Peles * - * Contains utilies for testing. + * Contains utilities for testing Matpower parser for GridKit. * */ #pragma once @@ -10,7 +10,6 @@ #include #include #include -#include #include #include diff --git a/src/Utilities/Testing.hpp b/src/Utilities/Testing.hpp index 294416a6..188ee1c4 100644 --- a/src/Utilities/Testing.hpp +++ b/src/Utilities/Testing.hpp @@ -11,9 +11,6 @@ #include #include #include -#include - -#include namespace GridKit { From fb2c625d5280fc3e82b1c76200e5686fbee9f297 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Fri, 23 May 2025 12:14:12 -0400 Subject: [PATCH 9/9] Fix additional compiler warnings. --- examples/PowerElectronics/RLCircuit/RLCircuit.cpp | 2 +- examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp | 2 +- src/Model/PowerFlow/Branch/Branch.hpp | 2 +- src/Model/PowerFlow/Generator/GeneratorSlack.cpp | 2 +- src/Model/PowerFlow/MiniGrid/MiniGrid.hpp | 2 +- src/Model/PowerFlow/SystemModelPowerFlow.hpp | 2 +- src/PowerSystemData.hpp | 2 +- src/Utilities/FileIO.hpp | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/PowerElectronics/RLCircuit/RLCircuit.cpp b/examples/PowerElectronics/RLCircuit/RLCircuit.cpp index 841395c3..2847b505 100644 --- a/examples/PowerElectronics/RLCircuit/RLCircuit.cpp +++ b/examples/PowerElectronics/RLCircuit/RLCircuit.cpp @@ -14,7 +14,7 @@ #include #include -int main(int argc, char const* argv[]) +int main(int /* argc */, char const** /* argv */) { double abs_tol = 1.0e-8; double rel_tol = 1.0e-8; diff --git a/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp b/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp index 1568fda8..9864235a 100644 --- a/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp +++ b/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp @@ -31,7 +31,7 @@ static int test(index_type Nsize, real_type test_tolerance, bool error_tol = fal * @param argv unsued * @return int */ -int main(int argc, char const* argv[]) +int main(int /* argc */, char const** /* argv */) { int retval = 0; bool debug_out = false; diff --git a/src/Model/PowerFlow/Branch/Branch.hpp b/src/Model/PowerFlow/Branch/Branch.hpp index d6500bdd..c7eca804 100644 --- a/src/Model/PowerFlow/Branch/Branch.hpp +++ b/src/Model/PowerFlow/Branch/Branch.hpp @@ -63,7 +63,7 @@ namespace GridKit // int evaluateAdjointJacobian(); int evaluateAdjointIntegrand(); - void updateTime(real_type t, real_type a) + void updateTime(real_type /* t */, real_type /* a */) { } diff --git a/src/Model/PowerFlow/Generator/GeneratorSlack.cpp b/src/Model/PowerFlow/Generator/GeneratorSlack.cpp index ec1d588a..8e995da5 100644 --- a/src/Model/PowerFlow/Generator/GeneratorSlack.cpp +++ b/src/Model/PowerFlow/Generator/GeneratorSlack.cpp @@ -17,7 +17,7 @@ namespace GridKit */ template - GeneratorSlack::GeneratorSlack(bus_type* bus, GenData& data) + GeneratorSlack::GeneratorSlack(bus_type* bus, GenData& /* data */) : bus_(bus) { // std::cout << "Create a load model with " << size_ << " variables ...\n"; diff --git a/src/Model/PowerFlow/MiniGrid/MiniGrid.hpp b/src/Model/PowerFlow/MiniGrid/MiniGrid.hpp index eebb9f82..3b6d3930 100644 --- a/src/Model/PowerFlow/MiniGrid/MiniGrid.hpp +++ b/src/Model/PowerFlow/MiniGrid/MiniGrid.hpp @@ -59,7 +59,7 @@ namespace GridKit return -1; } - void updateTime(real_type t, real_type a) + void updateTime(real_type /* t */, real_type /* a */) { } diff --git a/src/Model/PowerFlow/SystemModelPowerFlow.hpp b/src/Model/PowerFlow/SystemModelPowerFlow.hpp index d47f0899..d43883f6 100644 --- a/src/Model/PowerFlow/SystemModelPowerFlow.hpp +++ b/src/Model/PowerFlow/SystemModelPowerFlow.hpp @@ -348,7 +348,7 @@ namespace GridKit return 0; } - void updateTime(real_type t, real_type a) + void updateTime(real_type /* t */, real_type /* a */) { } diff --git a/src/PowerSystemData.hpp b/src/PowerSystemData.hpp index a7b7dfb1..1a71be0a 100644 --- a/src/PowerSystemData.hpp +++ b/src/PowerSystemData.hpp @@ -197,7 +197,7 @@ namespace GridKit using LoadDataT = LoadData; std::string version; - IdxT baseMVA; + RealT baseMVA; std::vector bus; std::vector gen; std::vector branch; diff --git a/src/Utilities/FileIO.hpp b/src/Utilities/FileIO.hpp index 217c93fa..130b79c1 100644 --- a/src/Utilities/FileIO.hpp +++ b/src/Utilities/FileIO.hpp @@ -189,7 +189,7 @@ namespace if (std::regex_match(line, matches, pat)) { std::string s = matches[1]; - mp.baseMVA = std::atoi(s.c_str()); + mp.baseMVA = std::atof(s.c_str()); } else {