Repository navigation
Refactor Microgrid Examples to Facilitate Code Reuse #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d54e118
Refactor common microgrid code and update examples
abdourahmanbarry 5d24419
Fixed include path for microgrid examples
abdourahmanbarry a9f1cea
Minor update to MicrogridNetwork.hpp implementation and microgrid exa…
abdourahmanbarry cb49c4a
Apply pre-commit fixes
abdourahmanbarry 164854f
Update comments and grid network builder
abdourahmanbarry 4147312
Apply pre-commit fixes
abdourahmanbarry b7f13f9
Update cmake files and examples
abdourahmanbarry c8e99a3
Apply pre-commit fixes
abdourahmanbarry d7fc3fb
Separate Network construction and system assembler
abdourahmanbarry eb3443b
Update microgrid examples
abdourahmanbarry 0c0ccf2
Apply pre-commit fixes
abdourahmanbarry b443292
[skip ci] Update examples/PowerElectronics/ExamplesHelper/SystemAssem…
nkoukpaizan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| add_library(power_elec_microgrid_network INTERFACE) | ||
|
|
||
| add_library(GridKit::power_elec_microgrid_network ALIAS power_elec_microgrid_network) | ||
|
|
||
| target_link_libraries( | ||
| power_elec_microgrid_network | ||
| INTERFACE GridKit::power_elec_disgen | ||
| GridKit::power_elec_microbusdq | ||
| GridKit::power_elec_microline | ||
| GridKit::power_elec_microload) | ||
226 changes: 226 additions & 0 deletions
226
examples/PowerElectronics/ExamplesHelper/MicrogridNetwork.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| // MicrogridNetwork.hpp | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cassert> | ||
| #include <cmath> | ||
| #include <cstddef> | ||
| #include <vector> | ||
|
|
||
| #include <GridKit/Model/PowerElectronics/Bus/MicrogridBus.hpp> | ||
| #include <GridKit/Model/PowerElectronics/Bus/SignalNode.hpp> | ||
| #include <GridKit/Model/PowerElectronics/DistributedGenerator/DistributedGenerator.hpp> | ||
| #include <GridKit/Model/PowerElectronics/MicrogridBusDQ/MicrogridBusDQ.hpp> | ||
| #include <GridKit/Model/PowerElectronics/MicrogridLine/MicrogridLine.hpp> | ||
| #include <GridKit/Model/PowerElectronics/MicrogridLoad/MicrogridLoad.hpp> | ||
| #include <GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp> | ||
|
|
||
| /* | ||
| * Contains components and nodes that make up the scaled microgrid network. | ||
| * | ||
| * The network contains 2 * N_size IBRs and stores the physical components | ||
| * that make up the scale microgrid. | ||
| */ | ||
| template <class ScalarT, typename IdxT> | ||
| struct ScaleMicrogridNetwork | ||
| { | ||
|
|
||
| using SignalNode = GridKit::PowerElectronics::SignalNode<ScalarT, IdxT>; | ||
| using Bus = GridKit::PowerElectronics::MicrogridBus<ScalarT, IdxT>; | ||
| using BusDQ = GridKit::MicrogridBusDQ<ScalarT, IdxT>; | ||
| using DGGenerator = GridKit::DistributedGenerator<ScalarT, IdxT>; | ||
| using Line = GridKit::MicrogridLine<ScalarT, IdxT>; | ||
| using Load = GridKit::MicrogridLoad<ScalarT, IdxT>; | ||
| using GenParams = GridKit::DistributedGeneratorParameters<ScalarT, IdxT>; | ||
|
|
||
| size_t model_id_next; | ||
| size_t N_size; | ||
| SignalNode dg_signal; | ||
| std::vector<Bus> buses; | ||
| std::vector<BusDQ*> busesDQ; | ||
| std::vector<DGGenerator*> generators; | ||
| std::vector<Line*> lines; | ||
| std::vector<Load*> loads; | ||
| std::vector<GenParams> DGParam_list; | ||
|
|
||
| ScaleMicrogridNetwork(size_t n_size) | ||
| : model_id_next(0), | ||
| N_size(n_size), | ||
| buses(2 * n_size), | ||
| busesDQ(2 * n_size, nullptr), | ||
| generators(2 * n_size, nullptr), | ||
| lines(2 * n_size - 1, nullptr), | ||
| loads(2 * n_size, nullptr), | ||
| DGParam_list(2 * n_size) | ||
| { | ||
| buildScaleMicrogridNetwork(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Construct all components of the scaled microgrid network. | ||
| * | ||
| * Builds a microgrid containing @c 2*N_size generators and buses connected | ||
| * in a chain by transmission lines. Loads are connected to every other bus, | ||
| * and a virtual DQ bus is associated with each physical bus. | ||
| * | ||
| * The first two generators use the reference generator parameter set, while | ||
| * all remaining generators use the second parameter set. Line parameters | ||
| * alternate along the network, and the first load uses a different parameter | ||
| * set from the remaining loads. | ||
| * | ||
| * The created components are stored in this network and assigned unique model | ||
| * identifiers using @c model_id_next. | ||
| * | ||
| * @pre @c N_size is greater than zero. | ||
| * | ||
| * @post The network contains @c 2*N_size generators and virtual DQ buses. | ||
| * @post The network contains @c 2*N_size-1 transmission lines connecting | ||
| * consecutive buses. | ||
| * @post The network contains @c N_size loads connected to every other bus. | ||
| * @post @c DGParam_list contains the parameters associated with each generator. | ||
| * @post @c model_id_next is advanced for every component created. | ||
| * | ||
| * @note Components are dynamically allocated and their pointers are stored | ||
| * in the corresponding component vectors. | ||
| */ | ||
| void buildScaleMicrogridNetwork() | ||
| { | ||
| assert(N_size > 0); | ||
| // Every Bus has the same virtual resistance. This is due to numerical stability as mentioned in the paper. | ||
| ScalarT RN = 1.0e4; | ||
|
|
||
| // TODO: add this as parameters | ||
| // DG Params Vector | ||
| // All DGs have the same set of parameters except for the first two. | ||
| GenParams DG_parms1; | ||
| DG_parms1.wb_ = 2.0 * M_PI * 50.0; | ||
| DG_parms1.wc_ = 31.41; | ||
| DG_parms1.mp_ = 9.4e-5; | ||
| DG_parms1.Vn_ = 380.0; | ||
| DG_parms1.nq_ = 1.3e-3; | ||
| DG_parms1.F_ = 0.75; | ||
| DG_parms1.Kiv_ = 420.0; | ||
| DG_parms1.Kpv_ = 0.1; | ||
| DG_parms1.Kic_ = 2.0e4; | ||
| DG_parms1.Kpc_ = 15.0; | ||
| DG_parms1.Cf_ = 5.0e-5; | ||
| DG_parms1.rLf_ = 0.1; | ||
| DG_parms1.Lf_ = 1.35e-3; | ||
| DG_parms1.rLc_ = 0.03; | ||
| DG_parms1.Lc_ = 0.35e-3; | ||
|
nkoukpaizan marked this conversation as resolved.
Resolved
|
||
|
|
||
| GenParams DG_parms2; | ||
| DG_parms2.wb_ = 2.0 * M_PI * 50.0; | ||
| DG_parms2.wc_ = 31.41; | ||
| DG_parms2.mp_ = 12.5e-5; | ||
| DG_parms2.Vn_ = 380.0; | ||
| DG_parms2.nq_ = 1.5e-3; | ||
| DG_parms2.F_ = 0.75; | ||
| DG_parms2.Kiv_ = 390.0; | ||
| DG_parms2.Kpv_ = 0.05; | ||
| DG_parms2.Kic_ = 16.0e3; | ||
| DG_parms2.Kpc_ = 10.5; | ||
| DG_parms2.Cf_ = 50.0e-6; | ||
| DG_parms2.rLf_ = 0.1; | ||
| DG_parms2.Lf_ = 1.35e-3; | ||
| DG_parms2.rLc_ = 0.03; | ||
| DG_parms2.Lc_ = 0.35e-3; | ||
|
|
||
| DGParam_list.assign(2 * N_size, DG_parms2); | ||
|
|
||
| // First two generators use parameters 1 | ||
| if (DGParam_list.size() >= 1) | ||
| { | ||
| DGParam_list[0] = DG_parms1; | ||
| } | ||
| if (DGParam_list.size() >= 2) | ||
| { | ||
| DGParam_list[1] = DG_parms1; | ||
| } | ||
|
|
||
| // line vector params | ||
| // Every odd line has the same parameters and every even line has the same parameters | ||
| ScalarT rline1 = 0.23; | ||
| ScalarT Lline1 = 0.1 / (2.0 * M_PI * 50.0); | ||
| ScalarT rline2 = 0.35; | ||
| ScalarT Lline2 = 0.58 / (2.0 * M_PI * 50.0); | ||
| std::vector<ScalarT> rline_list(2 * N_size - 1, 0.0); | ||
| std::vector<ScalarT> Lline_list(2 * N_size - 1, 0.0); | ||
| for (IdxT i = 0; i < rline_list.size(); i++) | ||
| { | ||
| rline_list[i] = (i % 2) ? rline2 : rline1; | ||
| Lline_list[i] = (i % 2) ? Lline2 : Lline1; | ||
| } | ||
|
|
||
| // load parms | ||
| // Only the first load has the same paramaters. | ||
| ScalarT rload1 = 3.0; | ||
| ScalarT Lload1 = 2.0 / (2.0 * M_PI * 50.0); | ||
| ScalarT rload2 = 2.0; | ||
| ScalarT Lload2 = 1.0 / (2.0 * M_PI * 50.0); | ||
|
|
||
| std::vector<ScalarT> rload_list(N_size, rload2); | ||
| std::vector<ScalarT> Lload_list(N_size, Lload2); | ||
| if (rload_list.size() >= 1) | ||
| { | ||
| rload_list[0] = rload1; | ||
| Lload_list[0] = Lload1; | ||
| } | ||
|
|
||
| // Create the reference generator | ||
| auto* dg_ref = new DGGenerator(model_id_next++, | ||
| DGParam_list[0], | ||
| true, | ||
| &dg_signal, | ||
| &buses[0]); | ||
|
|
||
| generators[0] = dg_ref; | ||
|
|
||
| // Create the remaining generators. | ||
| for (IdxT i = 1; i < 2 * N_size; i++) | ||
| { | ||
| auto* dg = new DGGenerator(model_id_next++, | ||
| DGParam_list[i], | ||
| false, | ||
| &dg_signal, | ||
| &buses[i]); | ||
|
|
||
| generators[i] = dg; | ||
| } | ||
|
|
||
| // Create transmission lines between consecutive buses. | ||
| for (IdxT i = 0; i < 2 * N_size - 1; i++) | ||
| { | ||
| auto* line_model = new Line(model_id_next++, | ||
| rline_list[i], | ||
| Lline_list[i], | ||
| &dg_signal, | ||
| &buses[i], | ||
| &buses[i + 1]); | ||
|
|
||
| lines[i] = line_model; | ||
| } | ||
|
|
||
| // Create loads on every other bus. | ||
| for (IdxT i = 0; i < N_size; i++) | ||
| { | ||
| auto* load_model = new Load(model_id_next++, | ||
| rload_list[i], | ||
| Lload_list[i], | ||
| &dg_signal, | ||
| &buses[2 * i]); | ||
|
|
||
| loads[2 * i] = load_model; | ||
| } | ||
|
|
||
| // Create and Add all the microgrid Virtual DQ Buses | ||
| for (IdxT i = 0; i < 2 * N_size; i++) | ||
| { | ||
| auto* virDQbus_model = new BusDQ(model_id_next++, | ||
| RN, | ||
| &buses[i]); | ||
|
|
||
| busesDQ[i] = virDQbus_model; | ||
| } | ||
| } | ||
| }; | ||
76 changes: 76 additions & 0 deletions
76
examples/PowerElectronics/ExamplesHelper/SystemAssembler.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SystemAssembler.hpp | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cassert> | ||
| #include <cmath> | ||
|
|
||
| #include <examples/PowerElectronics/ExamplesHelper/MicrogridNetwork.hpp> | ||
|
|
||
| /** | ||
| * @brief Assemble a scaled microgrid network into a power electronics model. | ||
| * | ||
| * Adds the signal node, physical buses, generators, transmission lines, | ||
| * loads, and virtual DQ buses stored in @p network to @p sys_model. | ||
| * | ||
| * This function does not construct or allocate any network components. | ||
| * | ||
| * @param[in] network Constructed scaled microgrid network whose components | ||
| * are added to the system model. | ||
| * @param[in,out] sys_model Power electronics model to which the network | ||
| * components and nodes are added. | ||
| * | ||
| * @pre @c network.N_size is greater than zero. | ||
| * @pre All component and node pointers referenced by @p network are valid. | ||
| * | ||
| * @post The signal node and all physical buses in @p network have been added | ||
| * to @p sys_model. | ||
| * @post All generators, transmission lines, loads, and virtual DQ buses in | ||
| * @p network have been added to @p sys_model. | ||
| * | ||
| * @note This function only assembles the network into the system model. It | ||
| * does not call PowerElectronicsModel::allocate(). | ||
| */ | ||
| template <class ScalarT, typename IdxT> | ||
| void assembleSystem(ScaleMicrogridNetwork<ScalarT, IdxT>& network, GridKit::PowerElectronicsModel<ScalarT, IdxT>& sys_model) | ||
| { | ||
| size_t N_size = network.N_size; | ||
|
|
||
| // Ensure minimum size requirement | ||
| assert(N_size > 0); | ||
|
|
||
| // Add all bus nodes | ||
| sys_model.addNode(&network.dg_signal); | ||
|
|
||
| for (size_t i = 0; i < 2 * N_size; i++) | ||
| { | ||
| sys_model.addNode(&network.buses[i]); | ||
| } | ||
|
|
||
| // Add all generators | ||
| for (IdxT i = 0; i < 2 * N_size; i++) | ||
| { | ||
| sys_model.addComponent(network.generators[i]); | ||
| } | ||
|
|
||
| // Load all the Line components | ||
| for (IdxT i = 0; i < 2 * N_size - 1; i++) | ||
| { | ||
| sys_model.addComponent(network.lines[i]); | ||
| } | ||
|
|
||
| // Load all the Load components | ||
| for (IdxT i = 0; i < 2 * N_size; i++) | ||
| { | ||
| if (network.loads[i] != nullptr) | ||
| { | ||
| sys_model.addComponent(network.loads[i]); | ||
| } | ||
| } | ||
|
|
||
| // Add all the microgrid Virtual DQ Buses | ||
| for (IdxT i = 0; i < 2 * N_size; i++) | ||
| { | ||
| sys_model.addComponent(network.busesDQ[i]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,7 @@ | ||
| add_executable(microgrid Microgrid.cpp) | ||
|
|
||
| target_link_libraries( | ||
| microgrid | ||
| GridKit::power_elec_disgen | ||
| GridKit::power_elec_microline | ||
| GridKit::power_elec_microload | ||
| GridKit::solvers_dyn | ||
| GridKit::power_elec_microbusdq) | ||
| microgrid GridKit::solvers_dyn power_elec_microgrid_network) | ||
|
|
||
| add_test(NAME Microgrid COMMAND $<TARGET_FILE:microgrid>) | ||
| install(TARGETS microgrid RUNTIME DESTINATION bin) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.