Skip to content

Commit 8e91d10

Browse files
committed
Adopt the standard model interface and start from flat
1 parent 680fdde commit 8e91d10

48 files changed

Lines changed: 1575 additions & 1033 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎GridKit/AutomaticDifferentiation/Enzyme/D2LDx2.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace GridKit
2626
* of \f$L\f$. Only structural entries are stored, in global indices.
2727
*
2828
* @tparam ModelT - model type with internal and external local variables
29-
* and equality constraints, and internal inequality constraints
29+
* and equality constraints, and inequality constraints
3030
*/
3131
template <typename ModelT>
3232
struct D2LDx2

‎GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#pragma once
88

99
#include <cstddef>
10-
#include <numeric>
1110
#include <vector>
1211

12+
#include <GridKit/Utilities/Enum.hpp>
13+
1314
namespace GridKit
1415
{
1516
namespace Enzyme
@@ -29,9 +30,9 @@ namespace GridKit
2930
BusResidual12, //< Special case for branches that are connected to two buses
3031
BusResidual21, //< Special case for branches that are connected to two buses
3132
BusResidual22, //< Special case for branches that are connected to two buses
32-
Objective, //< Optimization objective, `objective(x)`
33+
Objective, //< Optimization objective
3334
Equalities, //< Optimization internal and external equality contributions
34-
Inequalities //< Optimization internal inequality constraints
35+
Inequalities //< Optimization inequality rows
3536
};
3637

3738
/**
@@ -234,14 +235,16 @@ namespace GridKit
234235
/**
235236
* @brief Wrapper partial template specialization for an optimization objective
236237
*
238+
* Local variables are the internal variables, then the external ones.
237239
*/
238240
template <typename ModelT>
239241
struct ModelWrapper<ModelT, MemberFunctions::Objective>
240242
{
241-
/// Number of local variables: internal, then those each port reads
242-
static constexpr auto NUM_INPUTS =
243-
ModelT::NUM_INTERNAL_VARIABLES
244-
+ std::accumulate(ModelT::NUM_PORT_VARIABLES.begin(), ModelT::NUM_PORT_VARIABLES.end(), std::size_t{0});
243+
/// Number of internal variables
244+
static constexpr std::size_t NUM_INTERNAL_INPUTS = Utilities::enum_size<typename ModelT::InternalVariablesT>();
245+
246+
/// Number of local variables
247+
static constexpr std::size_t NUM_INPUTS = NUM_INTERNAL_INPUTS + Utilities::enum_size<typename ModelT::ExternalVariablesT>();
245248

246249
/**
247250
* @param[in] model - Pointer to the model to be differentiated
@@ -251,54 +254,61 @@ namespace GridKit
251254
template <typename ScalarT>
252255
[[gnu::always_inline]] inline static ScalarT eval(const ModelT* model, const ScalarT* x)
253256
{
254-
return model->objective(x);
257+
return model->objective(x, x + NUM_INTERNAL_INPUTS);
255258
}
256259
};
257260

258261
/**
259-
* @brief Wrapper partial template specialization for optimization equality constraints
262+
* @brief Wrapper partial template specialization for optimization equality contributions
260263
*
264+
* Local contributions are those to the internal rows, then those at the ports.
261265
*/
262266
template <typename ModelT>
263267
struct ModelWrapper<ModelT, MemberFunctions::Equalities>
264268
{
265-
/// Number of local equality contributions: internal, then those at each port
266-
static constexpr auto NUM_OUTPUTS =
267-
ModelT::NUM_INTERNAL_EQUALITIES
268-
+ std::accumulate(ModelT::NUM_PORT_EQUALITIES.begin(), ModelT::NUM_PORT_EQUALITIES.end(), std::size_t{0});
269+
/// Number of internal variables
270+
static constexpr std::size_t NUM_INTERNAL_INPUTS = Utilities::enum_size<typename ModelT::InternalVariablesT>();
271+
272+
/// Number of contributions to the internal rows
273+
static constexpr std::size_t NUM_INTERNAL_OUTPUTS = Utilities::enum_size<typename ModelT::InternalEqualitiesT>();
274+
275+
/// Number of local equality contributions
276+
static constexpr std::size_t NUM_OUTPUTS = NUM_INTERNAL_OUTPUTS + Utilities::enum_size<typename ModelT::ExternalEqualitiesT>();
269277

270278
/**
271279
* @param[in] model - Pointer to the model to be differentiated
272280
* @param[in] x - Local variables
273-
* @param[out] c - Local equality constraint functions
281+
* @param[out] c - Local equality contributions
274282
*/
275283
template <typename ScalarT>
276284
[[gnu::always_inline]] inline static void eval(const ModelT* model, const ScalarT* x, ScalarT* c)
277285
{
278-
model->internalEqualities(x, c);
279-
model->externalEqualities(x, c + ModelT::NUM_INTERNAL_EQUALITIES);
286+
model->internalEqualities(x, x + NUM_INTERNAL_INPUTS, c);
287+
model->externalEqualities(x, x + NUM_INTERNAL_INPUTS, c + NUM_INTERNAL_OUTPUTS);
280288
}
281289
};
282290

283291
/**
284-
* @brief Wrapper partial template specialization for optimization inequality constraints
285-
*
292+
* @brief Wrapper partial template specialization for optimization inequality rows
286293
*/
287294
template <typename ModelT>
288295
struct ModelWrapper<ModelT, MemberFunctions::Inequalities>
289296
{
290-
/// Number of inequality rows, all internal
291-
static constexpr auto NUM_OUTPUTS = ModelT::NUM_INTERNAL_INEQUALITIES;
297+
/// Number of internal variables
298+
static constexpr std::size_t NUM_INTERNAL_INPUTS = Utilities::enum_size<typename ModelT::InternalVariablesT>();
299+
300+
/// Number of inequality rows
301+
static constexpr std::size_t NUM_OUTPUTS = Utilities::enum_size<typename ModelT::InequalitiesT>();
292302

293303
/**
294304
* @param[in] model - Pointer to the model to be differentiated
295305
* @param[in] x - Local variables
296-
* @param[out] d - Local inequality constraint functions
306+
* @param[out] d - Inequality rows
297307
*/
298308
template <typename ScalarT>
299309
[[gnu::always_inline]] inline static void eval(const ModelT* model, const ScalarT* x, ScalarT* d)
300310
{
301-
model->internalInequalities(x, d);
311+
model->inequalities(x, x + NUM_INTERNAL_INPUTS, d);
302312
}
303313
};
304314
} // namespace Sparse

‎GridKit/Model/OptimalPowerFlow/Branch/Branch.hpp‎

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <array>
9+
#include <cstddef>
910

1011
#include <GridKit/Model/OptimalPowerFlow/Branch/BranchData.hpp>
1112
#include <GridKit/Model/OptimalPowerFlow/Bus/Bus.hpp>
@@ -15,6 +16,31 @@ namespace GridKit
1516
{
1617
namespace OptimalPowerFlow
1718
{
19+
/// External variables of a `Branch`, the voltages at its ports
20+
enum class BranchExternalVariables : size_t
21+
{
22+
VR1, ///< \f$V_{\mathrm{r}1}\f$ Bus-1 real voltage [p.u.]
23+
VI1, ///< \f$V_{\mathrm{i}1}\f$ Bus-1 imaginary voltage [p.u.]
24+
VR2, ///< \f$V_{\mathrm{r}2}\f$ Bus-2 real voltage [p.u.]
25+
VI2, ///< \f$V_{\mathrm{i}2}\f$ Bus-2 imaginary voltage [p.u.]
26+
};
27+
28+
/// External equality contributions of a `Branch`, the power into the buses at its ports
29+
enum class BranchExternalEqualities : size_t
30+
{
31+
DP1, ///< \f$\Delta P_1^\mathrm{bus}\f$ Active power into bus 1 [p.u.]
32+
DQ1, ///< \f$\Delta Q_1^\mathrm{bus}\f$ Reactive power into bus 1 [p.u.]
33+
DP2, ///< \f$\Delta P_2^\mathrm{bus}\f$ Active power into bus 2 [p.u.]
34+
DQ2, ///< \f$\Delta Q_2^\mathrm{bus}\f$ Reactive power into bus 2 [p.u.]
35+
};
36+
37+
/// Inequality rows of a `Branch`
38+
enum class BranchInequalities : size_t
39+
{
40+
S1, ///< \f$P_1^2 + Q_1^2\f$ Squared apparent power at bus 1 [p.u.]
41+
S2, ///< \f$P_2^2 + Q_2^2\f$ Squared apparent power at bus 2 [p.u.]
42+
};
43+
1844
/**
1945
* @brief Line or off-nominal transformer between two buses
2046
*
@@ -24,65 +50,71 @@ namespace GridKit
2450
class Branch final : public Component<scalar_type, index_type>
2551
{
2652
using ComponentT = Component<scalar_type, index_type>;
27-
using BusT = Bus<scalar_type, index_type>;
2853
using ScalarT = scalar_type;
2954
using IdxT = index_type;
3055
using RealT = typename ComponentT::RealT;
3156
using ModelDataT = BranchData<RealT, IdxT>;
57+
using BusT = Bus<scalar_type, index_type>;
3258

3359
using ComponentT::d_upper_;
3460

3561
public:
36-
static constexpr IdxT NUM_PORTS = 2;
37-
static constexpr IdxT NUM_INTERNAL_VARIABLES = 0;
38-
static constexpr IdxT NUM_INTERNAL_EQUALITIES = 0;
39-
static constexpr IdxT NUM_INTERNAL_INEQUALITIES = 2;
62+
using InternalVariablesT = Utilities::EmptyEnum;
63+
using ExternalVariablesT = BranchExternalVariables;
64+
using InternalEqualitiesT = Utilities::EmptyEnum;
65+
using ExternalEqualitiesT = BranchExternalEqualities;
66+
using InequalitiesT = BranchInequalities;
4067

41-
static constexpr std::array<IdxT, NUM_PORTS> NUM_PORT_VARIABLES{BusT::NUM_INTERNAL_VARIABLES,
42-
BusT::NUM_INTERNAL_VARIABLES};
43-
static constexpr std::array<IdxT, NUM_PORTS> NUM_PORT_EQUALITIES{BusT::NUM_INTERNAL_EQUALITIES,
44-
BusT::NUM_INTERNAL_EQUALITIES};
68+
static constexpr std::array<IdxT, Utilities::enum_size<BranchPorts>()> NUM_PORT_VARIABLES{
69+
Utilities::enum_size<typename BusT::InternalVariablesT>(),
70+
Utilities::enum_size<typename BusT::InternalVariablesT>()};
71+
static constexpr std::array<IdxT, Utilities::enum_size<BranchPorts>()> NUM_PORT_EQUALITIES{
72+
Utilities::enum_size<typename BusT::InternalEqualitiesT>(),
73+
Utilities::enum_size<typename BusT::InternalEqualitiesT>()};
4574

4675
explicit Branch(const ModelDataT& data);
4776

4877
Model::ConfigurationChecks verify() const override;
49-
int allocate() override;
78+
int initialize() override;
5079

80+
int evaluateGradient() override;
5181
int evaluateEqualityJacobian() override;
5282
int evaluateInequalityJacobian() override;
5383
int evaluateHessian(RealT sigma, const RealT* lambda_c, const RealT* lambda_d) override;
5484

55-
[[gnu::always_inline]] void externalEqualities(const ScalarT* x, ScalarT* c) const override;
56-
[[gnu::always_inline]] void internalInequalities(const ScalarT* x, ScalarT* d) const override;
85+
[[gnu::always_inline]] void externalEqualities(const ScalarT* x, const ScalarT* external, ScalarT* c) const override;
86+
[[gnu::always_inline]] void inequalities(const ScalarT* x, const ScalarT* external, ScalarT* d) const override;
5787

5888
bool inService() const
5989
{
60-
return in_service_ > ZERO<RealT>;
90+
return in_service_;
6191
}
6292

6393
private:
64-
[[gnu::always_inline]] void power(const ScalarT* x, ScalarT* s) const;
65-
void setDerivedParams();
66-
67-
RealT in_service_{ONE<RealT>};
68-
RealT R_{0.0};
69-
RealT X_{0.0};
70-
RealT G_{0.0};
71-
RealT B_{0.0};
72-
RealT Gmag_{0.0};
73-
RealT Bmag_{0.0};
74-
RealT tap_{1.0};
75-
RealT phase_{0.0};
76-
RealT Smax_{0.0};
77-
78-
RealT g11_{0.0};
79-
RealT b11_{0.0};
80-
RealT g12_{0.0};
81-
RealT b12_{0.0};
82-
RealT g21_{0.0};
83-
RealT b21_{0.0};
84-
RealT g22_{0.0};
85-
RealT b22_{0.0};
94+
void initializeParameters(const ModelDataT& data);
95+
void setDerivedParameters();
96+
97+
[[gnu::always_inline]] void power(const ScalarT* external, ScalarT* s) const;
98+
99+
RealT R_{ZERO<RealT>};
100+
RealT X_{ZERO<RealT>};
101+
RealT G_{ZERO<RealT>};
102+
RealT B_{ZERO<RealT>};
103+
RealT Gmag_{ZERO<RealT>};
104+
RealT Bmag_{ZERO<RealT>};
105+
RealT tap_{ONE<RealT>};
106+
RealT phase_{ZERO<RealT>};
107+
RealT Smax_{UNBOUNDED<RealT>};
108+
bool in_service_{true};
109+
110+
RealT g11_{ZERO<RealT>};
111+
RealT b11_{ZERO<RealT>};
112+
RealT g12_{ZERO<RealT>};
113+
RealT b12_{ZERO<RealT>};
114+
RealT g21_{ZERO<RealT>};
115+
RealT b21_{ZERO<RealT>};
116+
RealT g22_{ZERO<RealT>};
117+
RealT b22_{ZERO<RealT>};
86118
};
87119
} // namespace OptimalPowerFlow
88120
} // namespace GridKit

‎GridKit/Model/OptimalPowerFlow/Branch/BranchData.hpp‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ namespace GridKit
1616
/// Parameters for a branch
1717
enum class BranchParameters : size_t
1818
{
19-
R, ///< \f$R\f$ Series resistance [p.u.]
20-
X, ///< \f$X\f$ Series reactance [p.u.]
21-
G, ///< \f$G\f$ Total line shunt conductance, split between the ports [p.u.]
22-
B, ///< \f$B\f$ Total line shunt susceptance, split between the ports [p.u.]
23-
Gmag, ///< \f$G_\mathrm{mag}\f$ Magnetizing shunt conductance at bus 1 [p.u.]
24-
Bmag, ///< \f$B_\mathrm{mag}\f$ Magnetizing shunt susceptance at bus 1 [p.u.]
25-
tap, ///< \f$\tau\f$ Off-nominal tap magnitude on the bus-1 side [p.u.]
26-
phase, ///< \f$\theta\f$ Phase-shift angle [rad]
27-
Smax, ///< \f$S^{\max}\f$ Apparent power limit at each port [p.u.]
19+
R, ///< \f$R\f$ Series resistance [p.u.]
20+
X, ///< \f$X\f$ Series reactance [p.u.]
21+
G, ///< \f$G\f$ Total line shunt conductance, split between the ports [p.u.]
22+
B, ///< \f$B\f$ Total line shunt susceptance, split between the ports [p.u.]
23+
Gmag, ///< \f$G_\mathrm{mag}\f$ Magnetizing shunt conductance at bus 1 [p.u.]
24+
Bmag, ///< \f$B_\mathrm{mag}\f$ Magnetizing shunt susceptance at bus 1 [p.u.]
25+
tap, ///< \f$\tau\f$ Off-nominal tap magnitude on the bus-1 side [p.u.]
26+
phase, ///< \f$\theta\f$ Phase-shift angle [rad]
27+
Smax, ///< \f$S^{\max}\f$ Apparent power limit at each port [p.u.]
28+
in_service, ///< \f$u\f$ Service status
2829
};
2930

3031
/// Ports of a branch

‎GridKit/Model/OptimalPowerFlow/Branch/BranchDependencyTracking.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ namespace GridKit
1212
{
1313
namespace OptimalPowerFlow
1414
{
15+
template <typename scalar_type, typename index_type>
16+
int Branch<scalar_type, index_type>::evaluateGradient()
17+
{
18+
return 0;
19+
}
20+
1521
template <typename scalar_type, typename index_type>
1622
int Branch<scalar_type, index_type>::evaluateEqualityJacobian()
1723
{

‎GridKit/Model/OptimalPowerFlow/Branch/BranchEnzyme.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace GridKit
1111
{
1212
namespace OptimalPowerFlow
1313
{
14+
template <typename scalar_type, typename index_type>
15+
int Branch<scalar_type, index_type>::evaluateGradient()
16+
{
17+
return ComponentT::evaluateGradient(*this);
18+
}
19+
1420
template <typename scalar_type, typename index_type>
1521
int Branch<scalar_type, index_type>::evaluateEqualityJacobian()
1622
{

0 commit comments

Comments
 (0)