Skip to content

Commit af5a66c

Browse files
Refactor Microgrid Examples to Facilitate Code Reuse (#551)
* Refactor common microgrid code and update examples * Fixed include path for microgrid examples * Minor update to MicrogridNetwork.hpp implementation and microgrid examples * Update comments and grid network builder * Update cmake files and examples * Separate Network construction and system assembler * Update microgrid examples * [skip ci] Update examples/PowerElectronics/ExamplesHelper/SystemAssembler.hpp --------- Co-authored-by: abdourahmanbarry <abdourahmanbarry@users.noreply.github.com> Co-authored-by: Nicholson Koukpaizan <72402802+nkoukpaizan@users.noreply.github.com>
1 parent f1da4df commit af5a66c

9 files changed

Lines changed: 367 additions & 475 deletions

File tree

‎examples/PowerElectronics/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# - Slaven Peles <peless@ornl.gov>
44
#]]
55

6+
add_subdirectory(ExamplesHelper)
67
add_subdirectory(DistributedGeneratorTest)
78

89
if(TARGET SUNDIALS::idas)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_library(power_elec_microgrid_network INTERFACE)
2+
3+
add_library(GridKit::power_elec_microgrid_network ALIAS power_elec_microgrid_network)
4+
5+
target_link_libraries(
6+
power_elec_microgrid_network
7+
INTERFACE GridKit::power_elec_disgen
8+
GridKit::power_elec_microbusdq
9+
GridKit::power_elec_microline
10+
GridKit::power_elec_microload)
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// MicrogridNetwork.hpp
2+
3+
#pragma once
4+
5+
#include <cassert>
6+
#include <cmath>
7+
#include <cstddef>
8+
#include <vector>
9+
10+
#include <GridKit/Model/PowerElectronics/Bus/MicrogridBus.hpp>
11+
#include <GridKit/Model/PowerElectronics/Bus/SignalNode.hpp>
12+
#include <GridKit/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp>
13+
#include <GridKit/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp>
14+
#include <GridKit/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp>
15+
#include <GridKit/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp>
16+
#include <GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp>
17+
18+
/*
19+
* Contains components and nodes that make up the scaled microgrid network.
20+
*
21+
* The network contains 2 * N_size IBRs and stores the physical components
22+
* that make up the scale microgrid.
23+
*/
24+
template <class ScalarT, typename IdxT>
25+
struct ScaleMicrogridNetwork
26+
{
27+
28+
using SignalNode = GridKit::PowerElectronics::SignalNode<ScalarT, IdxT>;
29+
using Bus = GridKit::PowerElectronics::MicrogridBus<ScalarT, IdxT>;
30+
using BusDQ = GridKit::MicrogridBusDQ<ScalarT, IdxT>;
31+
using DGGenerator = GridKit::DistributedGenerator<ScalarT, IdxT>;
32+
using Line = GridKit::MicrogridLine<ScalarT, IdxT>;
33+
using Load = GridKit::MicrogridLoad<ScalarT, IdxT>;
34+
using GenParams = GridKit::DistributedGeneratorParameters<ScalarT, IdxT>;
35+
36+
size_t model_id_next;
37+
size_t N_size;
38+
SignalNode dg_signal;
39+
std::vector<Bus> buses;
40+
std::vector<BusDQ*> busesDQ;
41+
std::vector<DGGenerator*> generators;
42+
std::vector<Line*> lines;
43+
std::vector<Load*> loads;
44+
std::vector<GenParams> DGParam_list;
45+
46+
ScaleMicrogridNetwork(size_t n_size)
47+
: model_id_next(0),
48+
N_size(n_size),
49+
buses(2 * n_size),
50+
busesDQ(2 * n_size, nullptr),
51+
generators(2 * n_size, nullptr),
52+
lines(2 * n_size - 1, nullptr),
53+
loads(2 * n_size, nullptr),
54+
DGParam_list(2 * n_size)
55+
{
56+
buildScaleMicrogridNetwork();
57+
}
58+
59+
/**
60+
* @brief Construct all components of the scaled microgrid network.
61+
*
62+
* Builds a microgrid containing @c 2*N_size generators and buses connected
63+
* in a chain by transmission lines. Loads are connected to every other bus,
64+
* and a virtual DQ bus is associated with each physical bus.
65+
*
66+
* The first two generators use the reference generator parameter set, while
67+
* all remaining generators use the second parameter set. Line parameters
68+
* alternate along the network, and the first load uses a different parameter
69+
* set from the remaining loads.
70+
*
71+
* The created components are stored in this network and assigned unique model
72+
* identifiers using @c model_id_next.
73+
*
74+
* @pre @c N_size is greater than zero.
75+
*
76+
* @post The network contains @c 2*N_size generators and virtual DQ buses.
77+
* @post The network contains @c 2*N_size-1 transmission lines connecting
78+
* consecutive buses.
79+
* @post The network contains @c N_size loads connected to every other bus.
80+
* @post @c DGParam_list contains the parameters associated with each generator.
81+
* @post @c model_id_next is advanced for every component created.
82+
*
83+
* @note Components are dynamically allocated and their pointers are stored
84+
* in the corresponding component vectors.
85+
*/
86+
void buildScaleMicrogridNetwork()
87+
{
88+
assert(N_size > 0);
89+
// Every Bus has the same virtual resistance. This is due to numerical stability as mentioned in the paper.
90+
ScalarT RN = 1.0e4;
91+
92+
// TODO: add this as parameters
93+
// DG Params Vector
94+
// All DGs have the same set of parameters except for the first two.
95+
GenParams DG_parms1;
96+
DG_parms1.wb_ = 2.0 * M_PI * 50.0;
97+
DG_parms1.wc_ = 31.41;
98+
DG_parms1.mp_ = 9.4e-5;
99+
DG_parms1.Vn_ = 380.0;
100+
DG_parms1.nq_ = 1.3e-3;
101+
DG_parms1.F_ = 0.75;
102+
DG_parms1.Kiv_ = 420.0;
103+
DG_parms1.Kpv_ = 0.1;
104+
DG_parms1.Kic_ = 2.0e4;
105+
DG_parms1.Kpc_ = 15.0;
106+
DG_parms1.Cf_ = 5.0e-5;
107+
DG_parms1.rLf_ = 0.1;
108+
DG_parms1.Lf_ = 1.35e-3;
109+
DG_parms1.rLc_ = 0.03;
110+
DG_parms1.Lc_ = 0.35e-3;
111+
112+
GenParams DG_parms2;
113+
DG_parms2.wb_ = 2.0 * M_PI * 50.0;
114+
DG_parms2.wc_ = 31.41;
115+
DG_parms2.mp_ = 12.5e-5;
116+
DG_parms2.Vn_ = 380.0;
117+
DG_parms2.nq_ = 1.5e-3;
118+
DG_parms2.F_ = 0.75;
119+
DG_parms2.Kiv_ = 390.0;
120+
DG_parms2.Kpv_ = 0.05;
121+
DG_parms2.Kic_ = 16.0e3;
122+
DG_parms2.Kpc_ = 10.5;
123+
DG_parms2.Cf_ = 50.0e-6;
124+
DG_parms2.rLf_ = 0.1;
125+
DG_parms2.Lf_ = 1.35e-3;
126+
DG_parms2.rLc_ = 0.03;
127+
DG_parms2.Lc_ = 0.35e-3;
128+
129+
DGParam_list.assign(2 * N_size, DG_parms2);
130+
131+
// First two generators use parameters 1
132+
if (DGParam_list.size() >= 1)
133+
{
134+
DGParam_list[0] = DG_parms1;
135+
}
136+
if (DGParam_list.size() >= 2)
137+
{
138+
DGParam_list[1] = DG_parms1;
139+
}
140+
141+
// line vector params
142+
// Every odd line has the same parameters and every even line has the same parameters
143+
ScalarT rline1 = 0.23;
144+
ScalarT Lline1 = 0.1 / (2.0 * M_PI * 50.0);
145+
ScalarT rline2 = 0.35;
146+
ScalarT Lline2 = 0.58 / (2.0 * M_PI * 50.0);
147+
std::vector<ScalarT> rline_list(2 * N_size - 1, 0.0);
148+
std::vector<ScalarT> Lline_list(2 * N_size - 1, 0.0);
149+
for (IdxT i = 0; i < rline_list.size(); i++)
150+
{
151+
rline_list[i] = (i % 2) ? rline2 : rline1;
152+
Lline_list[i] = (i % 2) ? Lline2 : Lline1;
153+
}
154+
155+
// load parms
156+
// Only the first load has the same paramaters.
157+
ScalarT rload1 = 3.0;
158+
ScalarT Lload1 = 2.0 / (2.0 * M_PI * 50.0);
159+
ScalarT rload2 = 2.0;
160+
ScalarT Lload2 = 1.0 / (2.0 * M_PI * 50.0);
161+
162+
std::vector<ScalarT> rload_list(N_size, rload2);
163+
std::vector<ScalarT> Lload_list(N_size, Lload2);
164+
if (rload_list.size() >= 1)
165+
{
166+
rload_list[0] = rload1;
167+
Lload_list[0] = Lload1;
168+
}
169+
170+
// Create the reference generator
171+
auto* dg_ref = new DGGenerator(model_id_next++,
172+
DGParam_list[0],
173+
true,
174+
&dg_signal,
175+
&buses[0]);
176+
177+
generators[0] = dg_ref;
178+
179+
// Create the remaining generators.
180+
for (IdxT i = 1; i < 2 * N_size; i++)
181+
{
182+
auto* dg = new DGGenerator(model_id_next++,
183+
DGParam_list[i],
184+
false,
185+
&dg_signal,
186+
&buses[i]);
187+
188+
generators[i] = dg;
189+
}
190+
191+
// Create transmission lines between consecutive buses.
192+
for (IdxT i = 0; i < 2 * N_size - 1; i++)
193+
{
194+
auto* line_model = new Line(model_id_next++,
195+
rline_list[i],
196+
Lline_list[i],
197+
&dg_signal,
198+
&buses[i],
199+
&buses[i + 1]);
200+
201+
lines[i] = line_model;
202+
}
203+
204+
// Create loads on every other bus.
205+
for (IdxT i = 0; i < N_size; i++)
206+
{
207+
auto* load_model = new Load(model_id_next++,
208+
rload_list[i],
209+
Lload_list[i],
210+
&dg_signal,
211+
&buses[2 * i]);
212+
213+
loads[2 * i] = load_model;
214+
}
215+
216+
// Create and Add all the microgrid Virtual DQ Buses
217+
for (IdxT i = 0; i < 2 * N_size; i++)
218+
{
219+
auto* virDQbus_model = new BusDQ(model_id_next++,
220+
RN,
221+
&buses[i]);
222+
223+
busesDQ[i] = virDQbus_model;
224+
}
225+
}
226+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SystemAssembler.hpp
2+
3+
#pragma once
4+
5+
#include <cassert>
6+
#include <cmath>
7+
8+
#include <examples/PowerElectronics/ExamplesHelper/MicrogridNetwork.hpp>
9+
10+
/**
11+
* @brief Assemble a scaled microgrid network into a power electronics model.
12+
*
13+
* Adds the signal node, physical buses, generators, transmission lines,
14+
* loads, and virtual DQ buses stored in @p network to @p sys_model.
15+
*
16+
* This function does not construct or allocate any network components.
17+
*
18+
* @param[in] network Constructed scaled microgrid network whose components
19+
* are added to the system model.
20+
* @param[in,out] sys_model Power electronics model to which the network
21+
* components and nodes are added.
22+
*
23+
* @pre @c network.N_size is greater than zero.
24+
* @pre All component and node pointers referenced by @p network are valid.
25+
*
26+
* @post The signal node and all physical buses in @p network have been added
27+
* to @p sys_model.
28+
* @post All generators, transmission lines, loads, and virtual DQ buses in
29+
* @p network have been added to @p sys_model.
30+
*
31+
* @note This function only assembles the network into the system model. It
32+
* does not call PowerElectronicsModel::allocate().
33+
*/
34+
template <class ScalarT, typename IdxT>
35+
void assembleSystem(ScaleMicrogridNetwork<ScalarT, IdxT>& network, GridKit::PowerElectronicsModel<ScalarT, IdxT>& sys_model)
36+
{
37+
size_t N_size = network.N_size;
38+
39+
// Ensure minimum size requirement
40+
assert(N_size > 0);
41+
42+
// Add all bus nodes
43+
sys_model.addNode(&network.dg_signal);
44+
45+
for (size_t i = 0; i < 2 * N_size; i++)
46+
{
47+
sys_model.addNode(&network.buses[i]);
48+
}
49+
50+
// Add all generators
51+
for (IdxT i = 0; i < 2 * N_size; i++)
52+
{
53+
sys_model.addComponent(network.generators[i]);
54+
}
55+
56+
// Load all the Line components
57+
for (IdxT i = 0; i < 2 * N_size - 1; i++)
58+
{
59+
sys_model.addComponent(network.lines[i]);
60+
}
61+
62+
// Load all the Load components
63+
for (IdxT i = 0; i < 2 * N_size; i++)
64+
{
65+
if (network.loads[i] != nullptr)
66+
{
67+
sys_model.addComponent(network.loads[i]);
68+
}
69+
}
70+
71+
// Add all the microgrid Virtual DQ Buses
72+
for (IdxT i = 0; i < 2 * N_size; i++)
73+
{
74+
sys_model.addComponent(network.busesDQ[i]);
75+
}
76+
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
add_executable(microgrid Microgrid.cpp)
2+
23
target_link_libraries(
3-
microgrid
4-
GridKit::power_elec_disgen
5-
GridKit::power_elec_microline
6-
GridKit::power_elec_microload
7-
GridKit::solvers_dyn
8-
GridKit::power_elec_microbusdq)
4+
microgrid GridKit::solvers_dyn power_elec_microgrid_network)
95

106
add_test(NAME Microgrid COMMAND $<TARGET_FILE:microgrid>)
117
install(TARGETS microgrid RUNTIME DESTINATION bin)

0 commit comments

Comments
 (0)