Skip to content
Open
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
15 changes: 15 additions & 0 deletions GridKit/Model/PhasorDynamics/Stabilizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# [[
# Author(s):
# - Luke Lowery <lukel@tamu.edu>
#]]

gridkit_add_library(
phasor_dynamics_stabilizer
INTERFACE_TARGET
HEADERS StabilizerFactory.hpp
LINK_LIBRARIES INTERFACE GridKit::phasor_dynamics_core)

target_link_libraries(
phasor_dynamics_components
INTERFACE GridKit::phasor_dynamics_stabilizer)

add_subdirectory(IEEEST)
16 changes: 12 additions & 4 deletions GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/Ieeest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ namespace GridKit
*
* @return int - error code, 0 = success
*/
template <typename scalar_type, typename index_type>
int Ieeest<scalar_type, index_type>::evaluateJacobian()
template <typename scalar_type, typename index_type, size_t order>
int Ieeest<scalar_type, index_type, order>::evaluateJacobian()
{
Log::misc() << "Evaluate Jacobian for Ieeest..." << std::endl;
Log::misc() << "Jacobian evaluation not implemented!" << std::endl;
return 0;
}

// Available template instantiations
template class Ieeest<double, long int>;
template class Ieeest<double, size_t>;
template class Ieeest<double, long int, 0>;
template class Ieeest<double, long int, 1>;
template class Ieeest<double, long int, 2>;
template class Ieeest<double, long int, 3>;
template class Ieeest<double, long int, 4>;
template class Ieeest<double, size_t, 0>;
template class Ieeest<double, size_t, 1>;
template class Ieeest<double, size_t, 2>;
template class Ieeest<double, size_t, 3>;
template class Ieeest<double, size_t, 4>;
} // namespace Stabilizer
} // namespace PhasorDynamics
} // namespace GridKit
207 changes: 171 additions & 36 deletions GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/Ieeest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

#pragma once

#include <array>
#include <cstddef>
#include <memory>
#include <vector>
Comment on lines +10 to +12

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure vector should already be included. Could you comment on the need for cstddef and memory?


#include <GridKit/Definitions.hpp>
#include <GridKit/Model/PhasorDynamics/Component.hpp>
#include <GridKit/Model/PhasorDynamics/ComponentSignals.hpp>
#include <GridKit/Model/VariableMonitor.hpp>
Expand All @@ -32,34 +38,176 @@ namespace GridKit
{
namespace Stabilizer
{
/// Internal variables of a `Ieeest`
enum class IeeestInternalVariables : size_t
/**
* @brief Combined denominator coefficients of the IEEEST notch filter.
*
* The two second-order denominator factors expand into a single quartic
* with coefficients a1..a4.
*/
template <typename real_type>
inline std::array<real_type, 4> notchCoefficients(real_type A1,
real_type A2,
real_type A3,
real_type A4)
{
X1, ///< Notch filter state 1
X2, ///< Notch filter state 2
X3, ///< Notch filter state 3
X4, ///< Notch filter state 4
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
return {A1 + A3,
A2 + A4 + A1 * A3,
A1 * A4 + A2 * A3,
A2 * A4};
}

/**
* @brief Notch-filter order implied by the combined denominator
* coefficients.
*
* Structural coefficients are exact data-file literals, so the
* comparisons against zero are intentionally exact.
*/
template <typename real_type>
inline size_t notchOrder(real_type a1, real_type a2, real_type a3, real_type a4)
{
size_t order = 0;

if (a1 != ZERO<real_type>)
{
order = 1;
}
if (a2 != ZERO<real_type>)
{
order = 2;
}
if (a3 != ZERO<real_type>)
{
order = 3;
}
if (a4 != ZERO<real_type>)
{
order = 4;
}

return order;
}

/// Internal variable layout of a `Ieeest` by notch-filter order
template <size_t order>
struct IeeestVariables;

template <>
struct IeeestVariables<0>
{
/// Internal variables of a zeroth-order `Ieeest`
enum class InternalVariables : size_t
{
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
};
};

template <>
struct IeeestVariables<1>
{
/// Internal variables of a first-order `Ieeest`
enum class InternalVariables : size_t
{
X1, ///< Notch filter state 1
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
};
};

template <>
struct IeeestVariables<2>
{
/// Internal variables of a second-order `Ieeest`
enum class InternalVariables : size_t
{
X1, ///< Notch filter state 1
X2, ///< Notch filter state 2
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
};
};

template <>
struct IeeestVariables<3>
{
/// Internal variables of a third-order `Ieeest`
enum class InternalVariables : size_t
{
X1, ///< Notch filter state 1
X2, ///< Notch filter state 2
X3, ///< Notch filter state 3
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
};
};

template <>
struct IeeestVariables<4>
{
/// Internal variables of a fourth-order `Ieeest`
enum class InternalVariables : size_t
{
X1, ///< Notch filter state 1
X2, ///< Notch filter state 2
X3, ///< Notch filter state 3
X4, ///< Notch filter state 4
X5, ///< Lead-lag 1 state
X6, ///< Lead-lag 2 state
X7, ///< Washout state
V4, ///< Notch filter output
V5, ///< Lead-lag 1 output
V6, ///< Lead-lag 2 output
V7, ///< Unlimited stabilizer signal
VSS, ///< Limited stabilizer signal (model output)
MAXIMUM,
};
};

/// Internal variables of a `Ieeest` of the given notch-filter order
template <size_t order>
using IeeestInternalVariables = typename IeeestVariables<order>::InternalVariables;

/// External variables of a `Ieeest`
enum class IeeestExternalVariables : size_t
{
U, ///< Stabilizer input signal
MAXIMUM,
};

template <typename scalar_type, typename index_type>
template <typename scalar_type, typename index_type, size_t order>
class Ieeest : public Component<scalar_type, index_type>
{
static_assert(order <= 4, "Ieeest notch filter order must be in [0, 4]");

using Component<scalar_type, index_type>::gridkit_component_id_;
using Component<scalar_type, index_type>::alpha_;
using Component<scalar_type, index_type>::f_;
Expand All @@ -71,7 +219,6 @@ namespace GridKit
using Component<scalar_type, index_type>::y_;
using Component<scalar_type, index_type>::yp_;
using Component<scalar_type, index_type>::wb_;
using Component<scalar_type, index_type>::h_;
using Component<scalar_type, index_type>::J_rows_buffer_;
using Component<scalar_type, index_type>::J_cols_buffer_;
using Component<scalar_type, index_type>::J_vals_buffer_;
Expand Down Expand Up @@ -103,7 +250,7 @@ namespace GridKit
auto getSignals()
-> ComponentSignals<ScalarT,
IdxT,
IeeestInternalVariables,
IeeestInternalVariables<order>,
IeeestExternalVariables>&
{
return signals_;
Expand All @@ -119,6 +266,8 @@ namespace GridKit
ScalarT*);

private:
static constexpr RealT TIME_CONSTANT_MINIMUM = static_cast<RealT>(1.0e-3);

RealT A1_{0};
RealT A2_{0};
RealT A3_{0};
Expand All @@ -138,34 +287,20 @@ namespace GridKit
RealT Vcu_{0};
RealT Tdelay_{0};

RealT a0_{1};
RealT a1_{0};
RealT a2_{0};
RealT a3_{0};
RealT a4_{0};

// Precomputed masks and safe inverse coefficients for branch-free degenerate paths.
RealT use_notch_{0};
RealT bypass_notch_{1};
RealT use_4th_order_{0};
RealT use_3rd_order_{0};
RealT use_2nd_order_{0};
RealT safe_inv_a4_{0};
RealT safe_inv_a3_{0};
RealT safe_inv_a2_{0};
RealT use_T2_block_{1};
RealT bypass_T2_block_{0};
RealT use_T4_block_{1};
RealT bypass_T4_block_{0};
RealT use_T6_block_{1};
RealT bypass_T6_block_{0};

ComponentSignals<ScalarT, IdxT, IeeestInternalVariables, IeeestExternalVariables> signals_;
IdxT parameter_error_count_{0};

ComponentSignals<ScalarT, IdxT, IeeestInternalVariables<order>, IeeestExternalVariables> signals_;

std::unique_ptr<MonitorT> monitor_;

void initializeParameters(const ModelDataT& data);
void initializeMonitor();
void setDerivedParameters();

std::vector<ScalarT> ws_;
std::vector<IdxT> ws_indices_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ namespace GridKit
*
* @return int - error code, 0 = success
*/
template <typename scalar_type, typename index_type>
int Ieeest<scalar_type, index_type>::evaluateJacobian()
template <typename scalar_type, typename index_type, size_t order>
int Ieeest<scalar_type, index_type, order>::evaluateJacobian()
{
Log::misc() << "Evaluate Jacobian for Ieeest..." << std::endl;
Log::misc() << "Jacobian evaluation not implemented!" << std::endl;
return 0;
}

// Available template instantiations
template class Ieeest<DependencyTracking::Variable, long int>;
template class Ieeest<DependencyTracking::Variable, size_t>;
template class Ieeest<DependencyTracking::Variable, long int, 0>;
template class Ieeest<DependencyTracking::Variable, long int, 1>;
template class Ieeest<DependencyTracking::Variable, long int, 2>;
template class Ieeest<DependencyTracking::Variable, long int, 3>;
template class Ieeest<DependencyTracking::Variable, long int, 4>;
template class Ieeest<DependencyTracking::Variable, size_t, 0>;
template class Ieeest<DependencyTracking::Variable, size_t, 1>;
template class Ieeest<DependencyTracking::Variable, size_t, 2>;
template class Ieeest<DependencyTracking::Variable, size_t, 3>;
template class Ieeest<DependencyTracking::Variable, size_t, 4>;
} // namespace Stabilizer
} // namespace PhasorDynamics
} // namespace GridKit
Loading
Loading