From 416a2a3b6523a601410d7f2ffa545b008abeba5d Mon Sep 17 00:00:00 2001 From: superwhiskers Date: Thu, 24 Jul 2025 10:28:26 -0400 Subject: [PATCH 1/2] different implementation of signal assignment --- src/Model/PhasorDynamics/Branch/Branch.cpp | 23 +++++++++++-- src/Model/PhasorDynamics/Branch/Branch.hpp | 3 ++ src/Model/PhasorDynamics/Component.hpp | 38 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index e38753a73..1321c3e6d 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -7,11 +7,11 @@ * */ -#include "Branch.hpp" - +#include #include #include +#include "Branch.hpp" #include #include @@ -68,6 +68,8 @@ namespace GridKit bus1_id_(0), bus2_id_(0) { + // we have four external variables (V_r1, V_i1, V_r2, V_i2) + external_variable_signals_.resize(4); } template @@ -184,6 +186,23 @@ namespace GridKit return 0; } + /// Attaches a signal node to an external variable on this component + /// + /// See the documentation on `Component::attachSignalNode` for more details. The signals + /// available to be set on this component are: + /// - 0: Vr1 + /// - 1: Vi1 + /// - 2: Vr2 + /// - 3: Vi2 + template + void Branch::attachSignalNode(size_t variable, const SignalNode* signal) + { + // we have four external variables + assert(variable <= 3); + + external_variable_signals_[variable] = signal; + } + // Available template instantiations template class Branch; template class Branch; diff --git a/src/Model/PhasorDynamics/Branch/Branch.hpp b/src/Model/PhasorDynamics/Branch/Branch.hpp index 5be4657c6..fb36216c2 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.hpp +++ b/src/Model/PhasorDynamics/Branch/Branch.hpp @@ -45,6 +45,7 @@ namespace GridKit using Component::yp_; using Component::tag_; using Component::f_; + using Component::external_variable_signals_; using real_type = typename Component::real_type; using bus_type = BusBase; @@ -62,6 +63,8 @@ namespace GridKit virtual int evaluateResidual() override; virtual int evaluateJacobian() override; + virtual void attachSignalNode(size_t, const SignalNode*) override; + virtual void updateTime(real_type /* t */, real_type /* a */) override { } diff --git a/src/Model/PhasorDynamics/Component.hpp b/src/Model/PhasorDynamics/Component.hpp index 616989fdd..5764cedb0 100644 --- a/src/Model/PhasorDynamics/Component.hpp +++ b/src/Model/PhasorDynamics/Component.hpp @@ -1,10 +1,11 @@ #pragma once -#include #include +#include #include #include +#include namespace GridKit { @@ -40,6 +41,38 @@ namespace GridKit return false; } + /// Attaches a signal node to an external variable on this component + /// + /// @pre The object is initialized + /// @post The signal node index corresponding to the variable specified is set to the + /// provided signal + virtual void attachSignalNode([[maybe_unused]] size_t variable, + [[maybe_unused]] const SignalNode* signal) + { + /// TODO: replace this interface with something significantly better. this interface has + /// many, many issues, among them being that there is no type safety in the way + /// the variable is specified; the callee must cast the variable enumeration to + /// the variable itself (which requires more work) or specify the integer directly + /// (also bad, as it makes the code harder to read and doesn't automatically update + /// the index when changes are made to the enumeration. additionally, since errors + /// resulting from this will only be caught at runtime, it is harder to debug). + /// + /// another problem with this interface is that like many other methods in gridkit, + /// we assume that it semantically makes sense for all components to provide them + /// and narrow them down with runtime errors. + /// + /// finally, this implementation also suffers from the problem that it requires the + /// non-stub implementations to write lots of boilerplate code to actually implement + /// it. + /// + /// a prototype of a better implementation for this can be found here: + /// https://github.com/ORNL/GridKit/pull/193/commits/d9158691c8e4de3d5bd0269c415a76f3b7ca76c0 + /// + /// this implementation suffers from none of the issues described above. + + throw "No signals exist for this component"; + } + // virtual void updateTime(real_type t, real_type a) // { // time_ = t; @@ -143,6 +176,9 @@ namespace GridKit std::vector param_up_{}; std::vector param_lo_{}; + /// Vector containing signals attached to external variables + std::vector*>> external_variable_signals_; + // // Public adjoint sensitivity methods (not yet implemented in components) // From 97d4ee6bcefd82a1034516d9ff784f4c9778d389 Mon Sep 17 00:00:00 2001 From: superwhiskers Date: Thu, 24 Jul 2025 14:31:53 +0000 Subject: [PATCH 2/2] Apply pre-commmit fixes --- src/Model/PhasorDynamics/Branch/Branch.cpp | 3 ++- src/Model/PhasorDynamics/Component.hpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Model/PhasorDynamics/Branch/Branch.cpp b/src/Model/PhasorDynamics/Branch/Branch.cpp index 1321c3e6d..4ccf21d83 100644 --- a/src/Model/PhasorDynamics/Branch/Branch.cpp +++ b/src/Model/PhasorDynamics/Branch/Branch.cpp @@ -7,11 +7,12 @@ * */ +#include "Branch.hpp" + #include #include #include -#include "Branch.hpp" #include #include diff --git a/src/Model/PhasorDynamics/Component.hpp b/src/Model/PhasorDynamics/Component.hpp index 5764cedb0..dfb63d87d 100644 --- a/src/Model/PhasorDynamics/Component.hpp +++ b/src/Model/PhasorDynamics/Component.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include #include