Skip to content

Commit 7336d1f

Browse files
committed
tempalte parameter for order implemetnations
1 parent 9fb15fa commit 7336d1f

11 files changed

Lines changed: 1093 additions & 432 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1+
# [[
2+
# Author(s):
3+
# - Luke Lowery <lukel@tamu.edu>
4+
#]]
5+
6+
gridkit_add_library(
7+
phasor_dynamics_stabilizer
8+
INTERFACE_TARGET
9+
HEADERS StabilizerFactory.hpp
10+
LINK_LIBRARIES INTERFACE GridKit::phasor_dynamics_core)
11+
12+
target_link_libraries(
13+
phasor_dynamics_components
14+
INTERFACE GridKit::phasor_dynamics_stabilizer)
15+
116
add_subdirectory(IEEEST)

GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/Ieeest.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ namespace GridKit
1717
*
1818
* @return int - error code, 0 = success
1919
*/
20-
template <typename scalar_type, typename index_type>
21-
int Ieeest<scalar_type, index_type>::evaluateJacobian()
20+
template <typename scalar_type, typename index_type, size_t order>
21+
int Ieeest<scalar_type, index_type, order>::evaluateJacobian()
2222
{
2323
Log::misc() << "Evaluate Jacobian for Ieeest..." << std::endl;
2424
Log::misc() << "Jacobian evaluation not implemented!" << std::endl;
2525
return 0;
2626
}
2727

2828
// Available template instantiations
29-
template class Ieeest<double, long int>;
30-
template class Ieeest<double, size_t>;
29+
template class Ieeest<double, long int, 0>;
30+
template class Ieeest<double, long int, 1>;
31+
template class Ieeest<double, long int, 2>;
32+
template class Ieeest<double, long int, 3>;
33+
template class Ieeest<double, long int, 4>;
34+
template class Ieeest<double, size_t, 0>;
35+
template class Ieeest<double, size_t, 1>;
36+
template class Ieeest<double, size_t, 2>;
37+
template class Ieeest<double, size_t, 3>;
38+
template class Ieeest<double, size_t, 4>;
3139
} // namespace Stabilizer
3240
} // namespace PhasorDynamics
3341
} // namespace GridKit

GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/Ieeest.hpp

Lines changed: 162 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
#pragma once
88

9+
#include <array>
910
#include <cstddef>
1011
#include <memory>
1112
#include <vector>
1213

14+
#include <GridKit/Definitions.hpp>
1315
#include <GridKit/Model/PhasorDynamics/Component.hpp>
1416
#include <GridKit/Model/PhasorDynamics/ComponentSignals.hpp>
1517
#include <GridKit/Model/VariableMonitor.hpp>
@@ -36,34 +38,176 @@ namespace GridKit
3638
{
3739
namespace Stabilizer
3840
{
39-
/// Internal variables of a `Ieeest`
40-
enum class IeeestInternalVariables : size_t
41+
/**
42+
* @brief Combined denominator coefficients of the IEEEST notch filter.
43+
*
44+
* The two second-order denominator factors expand into a single quartic
45+
* with coefficients a1..a4.
46+
*/
47+
template <typename real_type>
48+
inline std::array<real_type, 4> notchCoefficients(real_type A1,
49+
real_type A2,
50+
real_type A3,
51+
real_type A4)
4152
{
42-
X1, ///< Notch filter state 1
43-
X2, ///< Notch filter state 2
44-
X3, ///< Notch filter state 3
45-
X4, ///< Notch filter state 4
46-
X5, ///< Lead-lag 1 state
47-
X6, ///< Lead-lag 2 state
48-
X7, ///< Washout state
49-
V4, ///< Notch filter output
50-
V5, ///< Lead-lag 1 output
51-
V6, ///< Lead-lag 2 output
52-
V7, ///< Unlimited stabilizer signal
53-
VSS, ///< Limited stabilizer signal (model output)
54-
MAXIMUM,
53+
return {A1 + A3,
54+
A2 + A4 + A1 * A3,
55+
A1 * A4 + A2 * A3,
56+
A2 * A4};
57+
}
58+
59+
/**
60+
* @brief Notch-filter order implied by the combined denominator
61+
* coefficients.
62+
*
63+
* Structural coefficients are exact data-file literals, so the
64+
* comparisons against zero are intentionally exact.
65+
*/
66+
template <typename real_type>
67+
inline size_t notchOrder(real_type a1, real_type a2, real_type a3, real_type a4)
68+
{
69+
size_t order = 0;
70+
71+
if (a1 != ZERO<real_type>)
72+
{
73+
order = 1;
74+
}
75+
if (a2 != ZERO<real_type>)
76+
{
77+
order = 2;
78+
}
79+
if (a3 != ZERO<real_type>)
80+
{
81+
order = 3;
82+
}
83+
if (a4 != ZERO<real_type>)
84+
{
85+
order = 4;
86+
}
87+
88+
return order;
89+
}
90+
91+
/// Internal variable layout of a `Ieeest` by notch-filter order
92+
template <size_t order>
93+
struct IeeestVariables;
94+
95+
template <>
96+
struct IeeestVariables<0>
97+
{
98+
/// Internal variables of a zeroth-order `Ieeest`
99+
enum class InternalVariables : size_t
100+
{
101+
X5, ///< Lead-lag 1 state
102+
X6, ///< Lead-lag 2 state
103+
X7, ///< Washout state
104+
V4, ///< Notch filter output
105+
V5, ///< Lead-lag 1 output
106+
V6, ///< Lead-lag 2 output
107+
V7, ///< Unlimited stabilizer signal
108+
VSS, ///< Limited stabilizer signal (model output)
109+
MAXIMUM,
110+
};
111+
};
112+
113+
template <>
114+
struct IeeestVariables<1>
115+
{
116+
/// Internal variables of a first-order `Ieeest`
117+
enum class InternalVariables : size_t
118+
{
119+
X1, ///< Notch filter state 1
120+
X5, ///< Lead-lag 1 state
121+
X6, ///< Lead-lag 2 state
122+
X7, ///< Washout state
123+
V4, ///< Notch filter output
124+
V5, ///< Lead-lag 1 output
125+
V6, ///< Lead-lag 2 output
126+
V7, ///< Unlimited stabilizer signal
127+
VSS, ///< Limited stabilizer signal (model output)
128+
MAXIMUM,
129+
};
130+
};
131+
132+
template <>
133+
struct IeeestVariables<2>
134+
{
135+
/// Internal variables of a second-order `Ieeest`
136+
enum class InternalVariables : size_t
137+
{
138+
X1, ///< Notch filter state 1
139+
X2, ///< Notch filter state 2
140+
X5, ///< Lead-lag 1 state
141+
X6, ///< Lead-lag 2 state
142+
X7, ///< Washout state
143+
V4, ///< Notch filter output
144+
V5, ///< Lead-lag 1 output
145+
V6, ///< Lead-lag 2 output
146+
V7, ///< Unlimited stabilizer signal
147+
VSS, ///< Limited stabilizer signal (model output)
148+
MAXIMUM,
149+
};
150+
};
151+
152+
template <>
153+
struct IeeestVariables<3>
154+
{
155+
/// Internal variables of a third-order `Ieeest`
156+
enum class InternalVariables : size_t
157+
{
158+
X1, ///< Notch filter state 1
159+
X2, ///< Notch filter state 2
160+
X3, ///< Notch filter state 3
161+
X5, ///< Lead-lag 1 state
162+
X6, ///< Lead-lag 2 state
163+
X7, ///< Washout state
164+
V4, ///< Notch filter output
165+
V5, ///< Lead-lag 1 output
166+
V6, ///< Lead-lag 2 output
167+
V7, ///< Unlimited stabilizer signal
168+
VSS, ///< Limited stabilizer signal (model output)
169+
MAXIMUM,
170+
};
55171
};
56172

173+
template <>
174+
struct IeeestVariables<4>
175+
{
176+
/// Internal variables of a fourth-order `Ieeest`
177+
enum class InternalVariables : size_t
178+
{
179+
X1, ///< Notch filter state 1
180+
X2, ///< Notch filter state 2
181+
X3, ///< Notch filter state 3
182+
X4, ///< Notch filter state 4
183+
X5, ///< Lead-lag 1 state
184+
X6, ///< Lead-lag 2 state
185+
X7, ///< Washout state
186+
V4, ///< Notch filter output
187+
V5, ///< Lead-lag 1 output
188+
V6, ///< Lead-lag 2 output
189+
V7, ///< Unlimited stabilizer signal
190+
VSS, ///< Limited stabilizer signal (model output)
191+
MAXIMUM,
192+
};
193+
};
194+
195+
/// Internal variables of a `Ieeest` of the given notch-filter order
196+
template <size_t order>
197+
using IeeestInternalVariables = typename IeeestVariables<order>::InternalVariables;
198+
57199
/// External variables of a `Ieeest`
58200
enum class IeeestExternalVariables : size_t
59201
{
60202
U, ///< Stabilizer input signal
61203
MAXIMUM,
62204
};
63205

64-
template <typename scalar_type, typename index_type>
206+
template <typename scalar_type, typename index_type, size_t order>
65207
class Ieeest : public Component<scalar_type, index_type>
66208
{
209+
static_assert(order <= 4, "Ieeest notch filter order must be in [0, 4]");
210+
67211
using Component<scalar_type, index_type>::gridkit_component_id_;
68212
using Component<scalar_type, index_type>::alpha_;
69213
using Component<scalar_type, index_type>::f_;
@@ -75,7 +219,6 @@ namespace GridKit
75219
using Component<scalar_type, index_type>::y_;
76220
using Component<scalar_type, index_type>::yp_;
77221
using Component<scalar_type, index_type>::wb_;
78-
using Component<scalar_type, index_type>::h_;
79222
using Component<scalar_type, index_type>::J_rows_buffer_;
80223
using Component<scalar_type, index_type>::J_cols_buffer_;
81224
using Component<scalar_type, index_type>::J_vals_buffer_;
@@ -107,7 +250,7 @@ namespace GridKit
107250
auto getSignals()
108251
-> ComponentSignals<ScalarT,
109252
IdxT,
110-
IeeestInternalVariables,
253+
IeeestInternalVariables<order>,
111254
IeeestExternalVariables>&
112255
{
113256
return signals_;
@@ -149,20 +292,9 @@ namespace GridKit
149292
RealT a3_{0};
150293
RealT a4_{0};
151294

152-
IdxT order_{0};
153-
RealT s0_{1};
154-
RealT s1_{0};
155-
RealT s2_{0};
156-
RealT s3_{0};
157-
RealT s4_{0};
158-
RealT a1_inv_{0};
159-
RealT a2_inv_{0};
160-
RealT a3_inv_{0};
161-
RealT a4_inv_{0};
162-
163295
IdxT parameter_error_count_{0};
164296

165-
ComponentSignals<ScalarT, IdxT, IeeestInternalVariables, IeeestExternalVariables> signals_;
297+
ComponentSignals<ScalarT, IdxT, IeeestInternalVariables<order>, IeeestExternalVariables> signals_;
166298

167299
std::unique_ptr<MonitorT> monitor_;
168300

GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/IeeestDependencyTracking.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ namespace GridKit
1717
*
1818
* @return int - error code, 0 = success
1919
*/
20-
template <typename scalar_type, typename index_type>
21-
int Ieeest<scalar_type, index_type>::evaluateJacobian()
20+
template <typename scalar_type, typename index_type, size_t order>
21+
int Ieeest<scalar_type, index_type, order>::evaluateJacobian()
2222
{
2323
Log::misc() << "Evaluate Jacobian for Ieeest..." << std::endl;
2424
Log::misc() << "Jacobian evaluation not implemented!" << std::endl;
2525
return 0;
2626
}
2727

2828
// Available template instantiations
29-
template class Ieeest<DependencyTracking::Variable, long int>;
30-
template class Ieeest<DependencyTracking::Variable, size_t>;
29+
template class Ieeest<DependencyTracking::Variable, long int, 0>;
30+
template class Ieeest<DependencyTracking::Variable, long int, 1>;
31+
template class Ieeest<DependencyTracking::Variable, long int, 2>;
32+
template class Ieeest<DependencyTracking::Variable, long int, 3>;
33+
template class Ieeest<DependencyTracking::Variable, long int, 4>;
34+
template class Ieeest<DependencyTracking::Variable, size_t, 0>;
35+
template class Ieeest<DependencyTracking::Variable, size_t, 1>;
36+
template class Ieeest<DependencyTracking::Variable, size_t, 2>;
37+
template class Ieeest<DependencyTracking::Variable, size_t, 3>;
38+
template class Ieeest<DependencyTracking::Variable, size_t, 4>;
3139
} // namespace Stabilizer
3240
} // namespace PhasorDynamics
3341
} // namespace GridKit

GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/IeeestEnzyme.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ namespace GridKit
1919
*
2020
* @tparam ScalarT - Scalar data type
2121
* @tparam IdxT - Index data type
22+
* @tparam order - Notch filter order
2223
* @return int - error code, 0 = success
2324
*/
24-
template <typename scalar_type, typename index_type>
25-
int Ieeest<scalar_type, index_type>::evaluateJacobian()
25+
template <typename scalar_type, typename index_type, size_t order>
26+
int Ieeest<scalar_type, index_type, order>::evaluateJacobian()
2627
{
2728
Log::misc() << "Evaluate Jacobian for Ieeest..." << std::endl;
2829
Log::misc() << "Jacobian evaluation is experimental!" << std::endl;
@@ -42,7 +43,7 @@ namespace GridKit
4243

4344
nnz_ = 0;
4445

45-
GridKit::Enzyme::Sparse::DfDy<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
46+
GridKit::Enzyme::Sparse::DfDy<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT, order>,
4647
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
4748
f_.size(),
4849
y_.size(),
@@ -57,7 +58,7 @@ namespace GridKit
5758
J_vals_buffer_,
5859
nnz_);
5960

60-
GridKit::Enzyme::Sparse::DfDyp<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
61+
GridKit::Enzyme::Sparse::DfDyp<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT, order>,
6162
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
6263
f_.size(),
6364
y_.size(),
@@ -73,7 +74,7 @@ namespace GridKit
7374
J_vals_buffer_,
7475
nnz_);
7576

76-
GridKit::Enzyme::Sparse::DfDws<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
77+
GridKit::Enzyme::Sparse::DfDws<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT, order>,
7778
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
7879
f_.size(),
7980
ws_.size(),
@@ -94,8 +95,16 @@ namespace GridKit
9495
}
9596

9697
// Available template instantiations
97-
template class Ieeest<double, long int>;
98-
template class Ieeest<double, size_t>;
98+
template class Ieeest<double, long int, 0>;
99+
template class Ieeest<double, long int, 1>;
100+
template class Ieeest<double, long int, 2>;
101+
template class Ieeest<double, long int, 3>;
102+
template class Ieeest<double, long int, 4>;
103+
template class Ieeest<double, size_t, 0>;
104+
template class Ieeest<double, size_t, 1>;
105+
template class Ieeest<double, size_t, 2>;
106+
template class Ieeest<double, size_t, 3>;
107+
template class Ieeest<double, size_t, 4>;
99108

100109
} // namespace Stabilizer
101110
} // namespace PhasorDynamics

0 commit comments

Comments
 (0)