Skip to content

Commit df24ad0

Browse files
PhilipFacklernkoukpaizanpelesh
authored
Add variable monitor for solver iteration output (#308)
* Add variable monitor for solver iteration output * Update enum values style; fix template parameter convention for VariableMonitor * Enable multiple styles specified through JSON input * Add specific guidelines for enums * Enable different variable types * Switched to private implementation using magic_enum --------- Co-authored-by: Nicholson Koukpaizan <koukpaizannk@ornl.gov> Co-authored-by: pelesh <peless@ornl.gov>
1 parent 2873ebb commit df24ad0

46 files changed

Lines changed: 1470 additions & 134 deletions

Some content is hidden

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

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- Added a header file defining constants to be used throughout the code.
4040
- Added `GridKitDocs` target for Doxygen documentation.
4141
- Added a header file defining common math functions (e.g., sigmoid) to be used throughout the code.
42+
- Added capability to print monitored variables in multiple formats, triggered from `Ida::runSimulation`.
4243

4344
## v0.1
4445

‎CONTRIBUTING.md‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,6 @@ For consistency, use the same name everywhere the same type is used.
249249
template <typename real_type>; // No, `_type` used in a template parameter name
250250
```
251251

252-
### Enums (enumerated types)
253-
254-
Always define `enum`s inside `GridKit` namespace. Type names should be
255-
capitalized and the constant names should be uppercase with underscores
256-
(but there is no underscore at the end!).
257-
258-
```c++
259-
enum ExampleEnum { CONST_ONE = 0,
260-
CONST_TWO = 8,
261-
YET_ANOTHER_CONST = 17 };
262-
```
263252

264253
### Constants
265254

@@ -274,6 +263,23 @@ name. Use all caps (screaming snake case).
274263
constexpr double EXP = 2.7183 // Yes
275264
```
276265

266+
### Enums (enumerated types)
267+
268+
Always define `enum`s inside `GridKit` namespace. The `enum` name should
269+
be upper camel case, same as class names. The `enum` element names should
270+
match symbol for physics quantity they represent or they should be uppercase
271+
(same as names for constants) if they do not represent a physics quantity.
272+
For example, enum element for real component of voltage $V_r$ should be `Vr`.
273+
A name for `enum` element for a "fast mode", for example, should be something
274+
like `FAST_MODE`, capitalized with underscores separating words (but no
275+
underscore at the end!).
276+
277+
```c++
278+
enum ExampleEnum { Vr, // Yes, it matches symbol for real voltage component
279+
VR, // No, the element name should match the physics symbol
280+
FAST_MODE}; // Yes, element name is all caps.
281+
```
282+
277283
### Pointers and references
278284
279285
The pointer `*` or reference `&` belong to the type and there should be no space between them and the type name.

‎GridKit/Model/CMakeLists.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ add_subdirectory(PhasorDynamics)
99
add_subdirectory(PowerFlow)
1010
add_subdirectory(PowerElectronics)
1111

12-
install(FILES Evaluator.hpp DESTINATION include/GridKit/Model)
12+
install(FILES
13+
Evaluator.hpp
14+
VariableMonitor.hpp
15+
VariableMonitorController.hpp
16+
DESTINATION include/GridKit/Model)

‎GridKit/Model/Evaluator.hpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <GridKit/CommonMath.hpp>
66
#include <GridKit/Constants.hpp>
77
#include <GridKit/LinearAlgebra/SparseMatrix/COO_Matrix.hpp>
8+
#include <GridKit/Model/VariableMonitor.hpp>
89
#include <GridKit/ScalarTraits.hpp>
910

1011
namespace GridKit
@@ -45,6 +46,43 @@ namespace GridKit
4546
virtual IdxT size() = 0;
4647
virtual IdxT nnz() = 0;
4748

49+
/**
50+
* @brief Is there something to monitor? Defaults to `false`
51+
*/
52+
virtual bool monitoring() const
53+
{
54+
return false;
55+
}
56+
57+
/**
58+
* @brief Print variables at current state
59+
*/
60+
virtual void printMonitoredVariables() const
61+
{
62+
}
63+
64+
/**
65+
* @brief Get non-owning reference to monitor
66+
*/
67+
virtual const VariableMonitorBase* getMonitor() const
68+
{
69+
return nullptr;
70+
}
71+
72+
/**
73+
* @brief Get monitor ready for output
74+
*/
75+
virtual void startMonitor()
76+
{
77+
}
78+
79+
/**
80+
* @brief Tell monitor to wrap up
81+
*/
82+
virtual void stopMonitor()
83+
{
84+
}
85+
4886
/**
4987
* @brief Is the Jacobian defined. Used in IDA to determine wether DQ is used or not
5088
*

‎GridKit/Model/PhasorDynamics/Branch/Branch.hpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99
#pragma once
1010

11+
#include <GridKit/Model/PhasorDynamics/Branch/BranchData.hpp>
1112
#include <GridKit/Model/PhasorDynamics/Component.hpp>
1213
#include <GridKit/Model/PhasorDynamics/ComponentSignals.hpp>
14+
#include <GridKit/Model/VariableMonitor.hpp>
1315

1416
// Forward declarations.
1517
namespace GridKit
@@ -51,11 +53,12 @@ namespace GridKit
5153
using Component<ScalarT, IdxT>::h_;
5254
using Component<ScalarT, IdxT>::J_;
5355

56+
public:
5457
using RealT = typename Component<ScalarT, IdxT>::RealT;
5558
using bus_type = BusBase<ScalarT, IdxT>;
5659
using model_data_type = BranchData<RealT, IdxT>;
60+
using MonitorT = Model::VariableMonitor<Branch, BranchData>;
5761

58-
public:
5962
Branch(bus_type* bus1, bus_type* bus2);
6063
Branch(bus_type* bus1, bus_type* bus2, RealT R, RealT X, RealT G, RealT B);
6164
Branch(bus_type* bus1, bus_type* bus2, const model_data_type& data);
@@ -77,7 +80,6 @@ namespace GridKit
7780
{
7881
}
7982

80-
public:
8183
void setR(RealT R)
8284
{
8385
R_ = R;
@@ -103,7 +105,11 @@ namespace GridKit
103105
setDerivedParams();
104106
}
105107

108+
const Model::VariableMonitorBase* getMonitor() const override;
109+
106110
private:
111+
void initializeParameters(const model_data_type& data);
112+
void initializeMonitor();
107113
void setDerivedParams();
108114

109115
ScalarT& Vr1()
@@ -162,9 +168,12 @@ namespace GridKit
162168
IdxT bus1_id_{0};
163169
IdxT bus2_id_{0};
164170

165-
/* Derivied parameters */
171+
/* Derived parameters */
166172
RealT b_;
167173
RealT g_;
174+
175+
/// Variable monitor
176+
std::unique_ptr<MonitorT> monitor_;
168177
};
169178

170179
} // namespace PhasorDynamics

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace GridKit
4040
ii2,
4141
im2,
4242
p2,
43-
q2,
43+
q2
4444
};
4545

4646
/**

‎GridKit/Model/PhasorDynamics/Branch/BranchImpl.hpp‎

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <GridKit/Model/PhasorDynamics/Branch/Branch.hpp>
1414
#include <GridKit/Model/PhasorDynamics/Branch/BranchData.hpp>
1515
#include <GridKit/Model/PhasorDynamics/Bus/Bus.hpp>
16+
#include <GridKit/Model/VariableMonitorImpl.hpp>
1617

1718
namespace GridKit
1819
{
@@ -74,37 +75,11 @@ namespace GridKit
7475
template <class ScalarT, typename IdxT>
7576
Branch<ScalarT, IdxT>::Branch(bus_type* bus1, bus_type* bus2, const model_data_type& data)
7677
: bus1_(bus1),
77-
bus2_(bus2)
78+
bus2_(bus2),
79+
monitor_(std::make_unique<MonitorT>(data))
7880
{
79-
if (data.parameters.contains(model_data_type::Parameters::R))
80-
{
81-
R_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::R));
82-
}
83-
84-
if (data.parameters.contains(model_data_type::Parameters::X))
85-
{
86-
X_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::X));
87-
}
88-
89-
if (data.parameters.contains(model_data_type::Parameters::G))
90-
{
91-
G_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::G));
92-
}
93-
94-
if (data.parameters.contains(model_data_type::Parameters::B))
95-
{
96-
B_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::B));
97-
}
98-
99-
if (data.ports.contains(model_data_type::Ports::bus1))
100-
{
101-
bus1_id_ = data.ports.at(model_data_type::Ports::bus1);
102-
}
103-
104-
if (data.ports.contains(model_data_type::Ports::bus2))
105-
{
106-
bus2_id_ = data.ports.at(model_data_type::Ports::bus2);
107-
}
81+
initializeParameters(data);
82+
initializeMonitor();
10883

10984
size_ = 0;
11085
setDerivedParams();
@@ -277,6 +252,66 @@ namespace GridKit
277252
return 0;
278253
}
279254

255+
template <class ScalarT, typename IdxT>
256+
void Branch<ScalarT, IdxT>::initializeParameters(const model_data_type& data)
257+
{
258+
if (data.parameters.contains(model_data_type::Parameters::R))
259+
{
260+
R_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::R));
261+
}
262+
263+
if (data.parameters.contains(model_data_type::Parameters::X))
264+
{
265+
X_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::X));
266+
}
267+
268+
if (data.parameters.contains(model_data_type::Parameters::G))
269+
{
270+
G_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::G));
271+
}
272+
273+
if (data.parameters.contains(model_data_type::Parameters::B))
274+
{
275+
B_ = std::get<RealT>(data.parameters.at(model_data_type::Parameters::B));
276+
}
277+
278+
if (data.ports.contains(model_data_type::Ports::bus1))
279+
{
280+
bus1_id_ = data.ports.at(model_data_type::Ports::bus1);
281+
}
282+
283+
if (data.ports.contains(model_data_type::Ports::bus2))
284+
{
285+
bus2_id_ = data.ports.at(model_data_type::Ports::bus2);
286+
}
287+
}
288+
289+
template <class ScalarT, typename IdxT>
290+
const Model::VariableMonitorBase* Branch<ScalarT, IdxT>::getMonitor() const
291+
{
292+
return monitor_.get();
293+
}
294+
295+
template <class ScalarT, typename IdxT>
296+
void Branch<ScalarT, IdxT>::initializeMonitor()
297+
{
298+
using Variable = typename model_data_type::MonitorableVariables;
299+
monitor_->set(Variable::ir1, [this]
300+
{ return Ir1(); });
301+
monitor_->set(Variable::ii1, [this]
302+
{ return Ii1(); });
303+
// monitor_->set(Variable::im1, [this] { return ?(); });
304+
// monitor_->set(Variable::p1, [this] { return ?(); });
305+
// monitor_->set(Variable::q1, [this] { return ?(); });
306+
monitor_->set(Variable::ir2, [this]
307+
{ return Ir2(); });
308+
monitor_->set(Variable::ii2, [this]
309+
{ return Ii2(); });
310+
// monitor_->set(Variable::im2, [this] { return ?(); });
311+
// monitor_->set(Variable::p2, [this] { return ?(); });
312+
// monitor_->set(Variable::q2, [this] { return ?(); });
313+
}
314+
280315
/**
281316
* @brief Derived parameters
282317
*

‎GridKit/Model/PhasorDynamics/Branch/CMakeLists.txt‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ if(GRIDKIT_ENABLE_ENZYME)
1515
BranchEnzyme.cpp
1616
HEADERS
1717
${_install_headers}
18+
INCLUDE_DIRECTORIES
19+
PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include
1820
LINK_LIBRARIES
1921
PRIVATE ClangEnzymeFlags
2022
COMPILE_OPTIONS
@@ -24,11 +26,14 @@ else()
2426
SOURCES
2527
Branch.cpp
2628
HEADERS
27-
${_install_headers})
29+
${_install_headers}
30+
INCLUDE_DIRECTORIES
31+
PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include)
2832
endif()
2933

3034
gridkit_add_library(phasor_dynamics_branch_dependency_tracking
31-
SOURCES BranchDependencyTracking.cpp)
35+
SOURCES BranchDependencyTracking.cpp
36+
INCLUDE_DIRECTORIES PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include)
3237

3338
# Link to interface target for all components
3439
target_link_libraries(phasor_dynamics_components

‎GridKit/Model/PhasorDynamics/Bus/BusData.hpp‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66
*/
77
#pragma once
88

9-
#include <bitset>
109
#include <optional>
10+
#include <set>
1111
#include <string>
1212
#include <type_traits>
1313

1414
namespace GridKit
1515
{
1616
namespace PhasorDynamics
1717
{
18+
/// Indices of the variables able to be monitored on this component
19+
enum class BusMonitorableVariables : size_t
20+
{
21+
Vr,
22+
Vi,
23+
Vm,
24+
Va
25+
};
26+
1827
/**
1928
* @brief Contains modeling data for a Bus
2029
*
@@ -49,20 +58,12 @@ namespace GridKit
4958
std::optional<RealT> freq_base; ///< Override for the system-wide base frequency
5059
std::optional<RealT> va_base; ///< Override for the system-wide power base
5160

52-
/// Indices of the variables able to be monitored on this component
53-
enum class MonitorableVariables : size_t
54-
{
55-
VR,
56-
VI,
57-
VM,
58-
VA,
59-
MAXIMUM,
60-
};
61+
/// Alias
62+
using MonitorableVariables = BusMonitorableVariables;
6163

6264
/// Set indicating the variables being monitored
63-
std::bitset<static_cast<
64-
std::underlying_type_t<MonitorableVariables>>(MonitorableVariables::MAXIMUM)>
65-
monitored_variables;
65+
std::set<MonitorableVariables> monitored_variables;
6666
};
67+
6768
} // namespace PhasorDynamics
6869
} // namespace GridKit

0 commit comments

Comments
 (0)