Repository navigation
integrate co-simulation into phasor dynamics applications #638
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #pragma once | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a header for the new files, with your name as the author and a short description of what the file does. |
||
|
|
||
| #include "SimulationOptions.hpp" | ||
| #include "SimulationRunner.hpp" | ||
|
|
||
| #ifdef GRIDKIT_ENABLE_ZMQ | ||
|
|
||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
|
|
||
| #include <GridKit/CoSim/CoSim.hpp> | ||
|
|
||
| #include "CoSimulationTypes.hpp" | ||
| #include "CoupledClient.hpp" | ||
| #include "CoupledServer.hpp" | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace PhasorDynamics | ||
| { | ||
| inline CoSim::RunResult runCoupledClientStudies( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some documentation for these functions would be helpful: what they do and how one will later use them. |
||
| const SimulationOptions& options, | ||
| NextCoupledStudy next, | ||
| CompleteCoupledStudy complete, | ||
| bool shutdown_peer, | ||
| bool print_results = false) | ||
| { | ||
| CoupledClient client( | ||
| options, | ||
| std::move(next), | ||
| std::move(complete), | ||
| shutdown_peer, | ||
| print_results); | ||
|
|
||
| CoSim::CoSim<double, CoSim::Disposition::CLIENT> adapter( | ||
| options.endpoint, | ||
| CoSim::MessageStepFunction<double>( | ||
| [&]( | ||
| const CoSimulationMessage& reply, | ||
| const CoSim::MessageExchangeFunction<double>& exchange) | ||
| { return client.handleReply(reply, exchange); }), | ||
| CoSim::MessageExtractFunction<double>([&](const auto&) | ||
| { return client.extract(); }), | ||
| options.timeout_ms); | ||
|
|
||
| const auto result = adapter.run(); | ||
|
|
||
| if (result == CoSim::RunResult::COMPLETED && !client.finished()) | ||
| { | ||
| throw std::runtime_error( | ||
| "co-simulation peer ended before the study completed"); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| inline Testing::TestStatus runCoupledServer( | ||
| const StudyData& base, const SimulationOptions& options) | ||
| { | ||
| CoupledServer server(base, options); | ||
|
|
||
| CoSim::CoSim<double, CoSim::Disposition::SERVER> adapter( | ||
| options.endpoint, | ||
| CoSim::MessageStepFunction<double>( | ||
| [&](const CoSimulationMessage& message, const auto&) | ||
| { return server.handleRequest(message); }), | ||
| CoSim::MessageExtractFunction<double>([&](const auto&) | ||
| { return server.extract(); }), | ||
| options.timeout_ms); | ||
|
|
||
| if (adapter.run() == CoSim::RunResult::STOPPED) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!server.finished()) | ||
| { | ||
| throw std::runtime_error( | ||
| "co-simulation peer closed before the study completed"); | ||
| } | ||
|
|
||
| return server.status(); | ||
| } | ||
|
|
||
| inline Testing::TestStatus runCoupledStudy( | ||
| const StudyData& data, const SimulationOptions& options) | ||
| { | ||
| if (options.role == "server") | ||
| { | ||
| return runCoupledServer(data, options); | ||
| } | ||
|
|
||
| bool first = true; | ||
| Testing::TestStatus status = true; | ||
| const auto next = [&]() -> std::optional<StudyData> | ||
| { | ||
| if (!first) | ||
| { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| first = false; | ||
| return data; | ||
| }; | ||
|
|
||
| if (runCoupledClientStudies( | ||
| options, | ||
| next, | ||
| [&](Testing::TestStatus result) | ||
| { status *= result; }, | ||
| false, | ||
| true) | ||
| == CoSim::RunResult::STOPPED) | ||
| { | ||
| status = false; | ||
| } | ||
|
|
||
| return status; | ||
| } | ||
| } // namespace PhasorDynamics | ||
| } // namespace GridKit | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| #include <GridKit/Definitions.hpp> | ||
|
|
||
| #ifdef GRIDKIT_ENABLE_ZMQ | ||
|
|
||
| #include <functional> | ||
| #include <optional> | ||
|
|
||
| #include <GridKit/CoSim/Message.hpp> | ||
|
|
||
| #include "AnalysisUtilities.hpp" | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace PhasorDynamics | ||
| { | ||
| /// Alias for the co-simulation message type used in the applications. | ||
| using CoSimulationMessage = CoSim::MessageData<double>; | ||
|
|
||
| using NextCoupledStudy = std::function<std::optional<StudyData>()>; | ||
| using CompleteCoupledStudy = std::function<void(Testing::TestStatus)>; | ||
|
Comment on lines
+21
to
+22
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These alias names for |
||
| } // namespace PhasorDynamics | ||
| } // namespace GridKit | ||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a separate cosimulation application here? Conceptually, I don't think
DynamicSimulationandContingencyAnalysisthemselves should linkGridKit::co_simandGridKit::solvers_steady.