Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Model/PhasorDynamics/Branch/Branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Branch.hpp"

#include <cassert>
#include <cmath>
#include <iostream>

Expand Down Expand Up @@ -68,6 +69,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 <class ScalarT, typename IdxT>
Expand Down Expand Up @@ -184,6 +187,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 <class ScalarT, typename IdxT>
void Branch<ScalarT, IdxT>::attachSignalNode(size_t variable, const SignalNode<ScalarT, IdxT>* signal)
{
// we have four external variables
assert(variable <= 3);

external_variable_signals_[variable] = signal;
}

// Available template instantiations
template class Branch<double, long int>;
template class Branch<double, size_t>;
Expand Down
3 changes: 3 additions & 0 deletions src/Model/PhasorDynamics/Branch/Branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace GridKit
using Component<ScalarT, IdxT>::yp_;
using Component<ScalarT, IdxT>::tag_;
using Component<ScalarT, IdxT>::f_;
using Component<ScalarT, IdxT>::external_variable_signals_;

using real_type = typename Component<ScalarT, IdxT>::real_type;
using bus_type = BusBase<ScalarT, IdxT>;
Expand All @@ -62,6 +63,8 @@ namespace GridKit
virtual int evaluateResidual() override;
virtual int evaluateJacobian() override;

virtual void attachSignalNode(size_t, const SignalNode<ScalarT, IdxT>*) override;

virtual void updateTime(real_type /* t */, real_type /* a */) override
{
}
Expand Down
38 changes: 37 additions & 1 deletion src/Model/PhasorDynamics/Component.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <exception>
#include <optional>
#include <vector>

#include <AutomaticDifferentiation/DependencyTracking/Variable.hpp>
#include <Model/Evaluator.hpp>
#include <Model/PhasorDynamics/SignalNode/SignalNode.hpp>

namespace GridKit
{
Expand Down Expand Up @@ -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<ScalarT, IdxT>* 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;
Expand Down Expand Up @@ -143,6 +176,9 @@ namespace GridKit
std::vector<ScalarT> param_up_{};
std::vector<ScalarT> param_lo_{};

/// Vector containing signals attached to external variables
std::vector<std::optional<const SignalNode<ScalarT, IdxT>*>> external_variable_signals_;

//
// Public adjoint sensitivity methods (not yet implemented in components)
//
Expand Down