|
| 1 | +/** |
| 2 | + * @file example3.cpp |
| 3 | + * @author Adam Birchfield (abirchfield@tamu.edu) |
| 4 | + * @author Slaven Peles (peless@ornl.gov) |
| 5 | + * @brief Example running a 10-bus system |
| 6 | + * |
| 7 | + * Simulates a 11-bus system with 10 Genrou 6th order generator models |
| 8 | + * split in two groups of five generators. The two groups are connected |
| 9 | + * by a high-impedance branch, which makes connection between them weak. |
| 10 | + * |
| 11 | + */ |
| 12 | +#include <cstdio> |
| 13 | +#include <ctime> |
| 14 | +#include <fstream> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +// #include "example3.hpp" |
| 18 | +#include <Model/PhasorDynamics/Branch/Branch.hpp> |
| 19 | +#include <Model/PhasorDynamics/Bus/Bus.hpp> |
| 20 | +#include <Model/PhasorDynamics/Bus/BusInfinite.hpp> |
| 21 | +#include <Model/PhasorDynamics/BusFault/BusFault.hpp> |
| 22 | +#include <Model/PhasorDynamics/Load/Load.hpp> |
| 23 | +#include <Model/PhasorDynamics/SynchronousMachine/GENROUwS/Genrou.hpp> |
| 24 | +#include <Model/PhasorDynamics/SystemModel.hpp> |
| 25 | +#include <Solver/Dynamic/Ida.hpp> |
| 26 | +#include <Utilities/Testing.hpp> |
| 27 | + |
| 28 | +using scalar_type = double; |
| 29 | +using real_type = double; |
| 30 | +using index_type = size_t; |
| 31 | + |
| 32 | +int main() |
| 33 | +{ |
| 34 | + using namespace GridKit::PhasorDynamics; |
| 35 | + using namespace AnalysisManager::Sundials; |
| 36 | + |
| 37 | + /* Create model parts */ |
| 38 | + BusInfinite<scalar_type, index_type> bus1(1, 0); |
| 39 | + |
| 40 | + Bus<scalar_type, index_type> bus2(0.999733719609643, 0.0230757421433613); |
| 41 | + Bus<scalar_type, index_type> bus3(0.999047460127767, 0.0436368240395443); |
| 42 | + Bus<scalar_type, index_type> bus4(0.998097277498088, 0.061658775943942); |
| 43 | + Bus<scalar_type, index_type> bus5(0.997021447662007, 0.0771246581966912); |
| 44 | + Bus<scalar_type, index_type> bus6(0.946436885707683, 0.322888837484268); |
| 45 | + Bus<scalar_type, index_type> bus7(0.943037519659334, 0.332686393642377); |
| 46 | + Bus<scalar_type, index_type> bus8(0.940418229359708, 0.340019961013984); |
| 47 | + Bus<scalar_type, index_type> bus9(0.938638861502395, 0.344901620288291); |
| 48 | + Bus<scalar_type, index_type> bus10(0.937739191669114, 0.347340277548916); |
| 49 | + |
| 50 | + Branch<scalar_type, index_type> branch12(&bus1, &bus2, 0.001, 0.005, 0, 0); |
| 51 | + Branch<scalar_type, index_type> branch23(&bus2, &bus3, 0.001, 0.005, 0, 0); |
| 52 | + Branch<scalar_type, index_type> branch34(&bus3, &bus4, 0.001, 0.005, 0, 0); |
| 53 | + Branch<scalar_type, index_type> branch45(&bus4, &bus5, 0.001, 0.005, 0, 0); |
| 54 | + Branch<scalar_type, index_type> branch56(&bus5, &bus6, 0.001, 0.1, 0, 0); |
| 55 | + Branch<scalar_type, index_type> branch67(&bus6, &bus7, 0.001, 0.005, 0, 0); |
| 56 | + Branch<scalar_type, index_type> branch78(&bus7, &bus8, 0.001, 0.005, 0, 0); |
| 57 | + Branch<scalar_type, index_type> branch89(&bus8, &bus9, 0.001, 0.005, 0, 0); |
| 58 | + Branch<scalar_type, index_type> branch910(&bus9, &bus10, 0.001, 0.005, 0, 0); |
| 59 | + |
| 60 | + Genrou<scalar_type, index_type> gen2(&bus2, 1, 0.5, -0.00442101, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 61 | + Genrou<scalar_type, index_type> gen3(&bus3, 1, 0.5, -0.02510812, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 62 | + Genrou<scalar_type, index_type> gen4(&bus4, 1, 0.5, -0.04339553, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 63 | + Genrou<scalar_type, index_type> gen5(&bus5, 1, 0.5, -0.2334993, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 64 | + Genrou<scalar_type, index_type> gen6(&bus6, 1, 0.5, 0.69907194, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 65 | + Genrou<scalar_type, index_type> gen7(&bus7, 1, 0.5, -0.08318208, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 66 | + Genrou<scalar_type, index_type> gen8(&bus8, 1, 0.5, -0.09123614, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 67 | + Genrou<scalar_type, index_type> gen9(&bus9, 1, 0.5, -0.09662372, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 68 | + Genrou<scalar_type, index_type> gen10(&bus10, 1, 0.5, -0.09932297, 3., 0., 0., 7., .04, .05, .75, 2.1, 0.2, 0.18, 0.5, 0.5, 0.18, 0.15, 0., 0.); |
| 69 | + |
| 70 | + BusFault<scalar_type, index_type> fault(&bus10, 0, 1e-5, 0); |
| 71 | + |
| 72 | + /* Connect everything together */ |
| 73 | + SystemModel<scalar_type, index_type> sys; |
| 74 | + |
| 75 | + sys.addBus(&bus1); |
| 76 | + sys.addBus(&bus2); |
| 77 | + sys.addBus(&bus3); |
| 78 | + sys.addBus(&bus4); |
| 79 | + sys.addBus(&bus5); |
| 80 | + sys.addBus(&bus6); |
| 81 | + sys.addBus(&bus7); |
| 82 | + sys.addBus(&bus8); |
| 83 | + sys.addBus(&bus9); |
| 84 | + sys.addBus(&bus10); |
| 85 | + sys.addComponent(&branch12); |
| 86 | + sys.addComponent(&branch23); |
| 87 | + sys.addComponent(&branch34); |
| 88 | + sys.addComponent(&branch45); |
| 89 | + sys.addComponent(&branch56); |
| 90 | + sys.addComponent(&branch67); |
| 91 | + sys.addComponent(&branch78); |
| 92 | + sys.addComponent(&branch89); |
| 93 | + sys.addComponent(&branch910); |
| 94 | + sys.addComponent(&gen2); |
| 95 | + sys.addComponent(&gen3); |
| 96 | + sys.addComponent(&gen4); |
| 97 | + sys.addComponent(&gen5); |
| 98 | + sys.addComponent(&gen6); |
| 99 | + sys.addComponent(&gen7); |
| 100 | + sys.addComponent(&gen8); |
| 101 | + sys.addComponent(&gen9); |
| 102 | + sys.addComponent(&gen10); |
| 103 | + sys.addComponent(&fault); |
| 104 | + sys.allocate(); |
| 105 | + |
| 106 | + real_type dt = 1.0 / 4.0 / 60.0; |
| 107 | + |
| 108 | + // Uncomment code below to print output to a file: |
| 109 | + std::ofstream fileout; |
| 110 | + fileout.open("example3_results.csv"); |
| 111 | + std::ostream& out = fileout; |
| 112 | + |
| 113 | + // Create header for the CSV output file |
| 114 | + out << "t,"; |
| 115 | + for (size_t i = 0; i < 9; ++i) |
| 116 | + { |
| 117 | + out << "v" << i + 2 << ","; |
| 118 | + } |
| 119 | + for (size_t i = 0; i < 9; ++i) |
| 120 | + { |
| 121 | + out << "omega" << i + 2 << ","; |
| 122 | + } |
| 123 | + out << "\n"; |
| 124 | + |
| 125 | + auto output_cb = [&](real_type t) |
| 126 | + { |
| 127 | + std::vector<double>& yval = sys.y(); |
| 128 | + |
| 129 | + // Output time |
| 130 | + out << t << ","; |
| 131 | + |
| 132 | + // Output voltage magnitudes on buses |
| 133 | + for (size_t i = 0; i < 9; ++i) |
| 134 | + { |
| 135 | + out << std::sqrt(yval[2 * i] * yval[2 * i] |
| 136 | + + yval[2 * i + 1] * yval[2 * i + 1]) |
| 137 | + << ","; |
| 138 | + } |
| 139 | + |
| 140 | + // Output generator frequencies |
| 141 | + for (size_t i = 0; i < 9; ++i) |
| 142 | + { |
| 143 | + // 18 is offset for variables of 9 buses. |
| 144 | + // Each generator has 21 equations. |
| 145 | + // We are outputting second equation of each generator. |
| 146 | + out << yval[18 + 21 * i + 1] << ","; |
| 147 | + } |
| 148 | + out << "\n"; |
| 149 | + }; |
| 150 | + |
| 151 | + // Set up simulation |
| 152 | + Ida<scalar_type, index_type> ida(&sys); |
| 153 | + ida.configureSimulation(); |
| 154 | + |
| 155 | + // Run simulation, output each `dt` interval |
| 156 | + scalar_type start = static_cast<scalar_type>(clock()); |
| 157 | + ida.initializeSimulation(0.0, false); |
| 158 | + |
| 159 | + // Run for 1s |
| 160 | + int nout = static_cast<int>(std::round((1.0 - 0.0) / dt)); |
| 161 | + ida.runSimulation(1.0, nout, output_cb); |
| 162 | + |
| 163 | + // Introduce fault to ground and run for 0.1s |
| 164 | + fault.setStatus(1); |
| 165 | + ida.initializeSimulation(1.0, false); |
| 166 | + nout = static_cast<int>(std::round((1.1 - 1.0) / dt)); |
| 167 | + ida.runSimulation(1.1, nout, output_cb); |
| 168 | + |
| 169 | + // Clear fault and run until t = 10s. |
| 170 | + fault.setStatus(0); |
| 171 | + ida.initializeSimulation(1.1, false); |
| 172 | + nout = static_cast<int>(std::round((10.0 - 1.1) / dt)); |
| 173 | + ida.runSimulation(10.0, nout, output_cb); |
| 174 | + double stop = static_cast<double>(clock()); |
| 175 | + |
| 176 | + fileout.close(); |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
0 commit comments