Skip to content

Commit 757dcfe

Browse files
committed
Support Layout types WIDE and LONG
1 parent a6f047a commit 757dcfe

18 files changed

Lines changed: 377 additions & 306 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## v.03
44

5+
- Added long monitor output layout and an ACTIVSg2000 PostgreSQL example.
6+
- Removed unused `VariableMonitorController::addVariable`.
57
- Added `BusSignalVoltageOut` bus model with voltage signal outlets and current signal inlets.
68
- Added `BusSignalVoltageIn` bus model with voltage signal inlets and current signal outlets.
79
- Removed `BusToSignalAdapter`. Prefer `BusSignalVoltageOut` or `BusSignalVoltageIn`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace GridKit
6666
{
6767
bus_id_ = data.bus_id;
6868
size_ = 2;
69-
monitor_ = std::make_unique<MonitorT>("Bus_" + data.name, data.monitored_variables);
69+
monitor_ = std::make_unique<MonitorT>("Bus", data.name, data.monitored_variables);
7070
using Variable = typename ModelDataT::MonitorableVariables;
7171
monitor_->set(Variable::Vr, [this]
7272
{ return Vr(); });

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace GridKit
6565
{
6666
bus_id_ = data.bus_id;
6767
size_ = 0;
68-
monitor_ = std::make_unique<MonitorT>("Bus_" + data.name, data.monitored_variables);
68+
monitor_ = std::make_unique<MonitorT>("Bus", data.name, data.monitored_variables);
6969
using Variable = typename ModelDataT::MonitorableVariables;
7070
monitor_->set(Variable::Vr, [this]
7171
{ return Vr(); });

‎GridKit/Model/PhasorDynamics/Bus/BusSignalVoltageIn/BusSignalVoltageInImpl.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace GridKit
5252
{
5353
bus_id_ = data.bus_id;
5454
size_ = 0;
55-
monitor_ = std::make_unique<MonitorT>("Bus_" + data.name, data.monitored_variables);
55+
monitor_ = std::make_unique<MonitorT>("Bus", data.name, data.monitored_variables);
5656
using Variable = typename ModelDataT::MonitorableVariables;
5757
monitor_->set(Variable::Vr, [this]
5858
{ return Vr(); });

‎GridKit/Model/PhasorDynamics/Bus/BusSignalVoltageOut/BusSignalVoltageOutImpl.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace GridKit
5151
{
5252
bus_id_ = data.bus_id;
5353
size_ = 2;
54-
monitor_ = std::make_unique<MonitorT>("Bus_" + data.name, data.monitored_variables);
54+
monitor_ = std::make_unique<MonitorT>("Bus", data.name, data.monitored_variables);
5555
using Variable = typename ModelDataT::MonitorableVariables;
5656
monitor_->set(Variable::Vr, [this]
5757
{ return Vr(); });

‎GridKit/Model/PhasorDynamics/INPUT_FORMAT.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ an output for monitored variables (those listed in the `mon` field of a
7272
`file_name` | Optional string indicating output file name. If omitted, `stdout` is used.
7373
`format` | One of { "CSV", "JSON", "YAML" } (case-insensitive)
7474
`delim` | Optional string specifying delimiter to use for CSV output (default is `","`).
75+
`layout` | Optional, one of { "WIDE", "LONG" } (default is "WIDE"). "LONG" writes one row per variable and sample.
7576

7677
__NOTE__: If `monitors` entry is omitted entirely, you will get the default CSV
7778
output to `stdout`. If you wish to change the console output format, use an

‎GridKit/Model/PhasorDynamics/SystemModelDataJSONParser.hpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace GridKit
1818
using json = nlohmann::json;
1919
using Log = ::GridKit::Utilities::Logger;
2020
using MonitorFormat = ::GridKit::Model::VariableMonitorFormat;
21+
using MonitorLayout = ::GridKit::Model::VariableMonitorLayout;
2122

2223
/// JSON parser function implementation for the `SystemModelData` type
2324
///
@@ -64,13 +65,14 @@ namespace GridKit
6465
auto fmt_str = raw_mon.at("format").get<std::string>();
6566
auto format = enum_parse(MonitorFormat{}, fmt_str);
6667
auto delim = raw_mon.value("delim", std::string(","));
67-
if (format.has_value())
68+
auto layout = enum_parse(MonitorLayout{}, raw_mon.value("layout", std::string("wide")));
69+
if (format.has_value() && layout.has_value())
6870
{
69-
sm.monitor_sink.emplace_back(format.value(), file_name, delim);
71+
sm.monitor_sink.emplace_back(format.value(), file_name, delim, layout.value());
7072
}
7173
else
7274
{
73-
Log::error() << "\n\tInvalid monitor format: \"" << fmt_str << "\"."
75+
Log::error() << "\n\tInvalid monitor format or layout: " << raw_mon.dump()
7476
<< "\n\tSee the \"monitors\" list in your JSON file."
7577
<< std::endl;
7678
}

‎GridKit/Model/VariableMonitor.hpp‎

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <set>
1313
#include <sstream>
1414
#include <string>
15+
#include <string_view>
1516
#include <system_error>
1617
#include <type_traits>
1718
#include <variant>
@@ -29,7 +30,7 @@ namespace GridKit
2930
namespace VariableMonitorDetail
3031
{
3132
template <typename RealT>
32-
std::string formatReal(RealT value)
33+
void formatReal(std::string& out, RealT value)
3334
{
3435
std::array<char, 128> buffer{};
3536
constexpr auto precision = std::numeric_limits<RealT>::digits10 + 1;
@@ -42,13 +43,14 @@ namespace GridKit
4243

4344
if (ec == std::errc{})
4445
{
45-
return std::string(buffer.data(), ptr);
46+
out.append(buffer.data(), ptr);
47+
return;
4648
}
4749

4850
std::ostringstream os;
4951
os.precision(precision);
5052
os << std::scientific << value;
51-
return os.str();
53+
out += os.str();
5254
}
5355
} // namespace VariableMonitorDetail
5456

@@ -63,6 +65,16 @@ namespace GridKit
6365
YAML ///< YAML format
6466
};
6567

68+
/**
69+
* @enum VariableMonitorLayout
70+
* Available table layouts for monitor output
71+
*/
72+
enum class VariableMonitorLayout
73+
{
74+
WIDE, ///< One row per sample with one column per variable
75+
LONG ///< One record per sample and variable
76+
};
77+
6678
/**
6779
* @brief Abstract class for managing output of monitored variables
6880
*
@@ -94,8 +106,20 @@ namespace GridKit
94106
{
95107
};
96108

109+
/// Type used for dispatch
110+
struct Wide
111+
{
112+
};
113+
114+
/// Type used for dispatch
115+
struct Long
116+
{
117+
};
118+
97119
/// Short alias for local use
98120
using Format = VariableMonitorFormat;
121+
/// Short alias for local use
122+
using Layout = VariableMonitorLayout;
99123

100124
/**
101125
* @brief Defines information necessary to create a monitor sink
@@ -108,6 +132,8 @@ namespace GridKit
108132
std::string file_name{};
109133
/// Delimiter (used only with CSV format currently)
110134
std::string delim{","};
135+
/// Table layout
136+
Layout layout{Layout::WIDE};
111137
};
112138

113139
virtual ~VariableMonitorBase()
@@ -119,50 +145,27 @@ namespace GridKit
119145
*/
120146
virtual bool empty() const = 0;
121147

122-
protected:
123-
///@{
124-
/**
125-
* @brief Print items relevant to the start of a file
126-
*/
127-
virtual void appendHeader(std::string&, Csv) const = 0;
148+
/// Appends the formatted current value of a monitored variable
149+
using ValueFormatter = std::function<void(std::string&)>;
128150

129-
virtual void appendHeader(std::string&, Json) const
130-
{
131-
}
132-
133-
virtual void appendHeader(std::string&, Yaml) const
134-
{
135-
}
136-
137-
///@}
138-
139-
///@{
140-
/**
141-
* @brief Print monitored variables at current state
142-
*/
143-
virtual void append(std::string&, Csv) const = 0;
144-
virtual void append(std::string&, Json) const = 0;
145-
virtual void append(std::string&, Yaml) const = 0;
146-
147-
///@}
148-
149-
///@{
150151
/**
151-
* @brief Print items relevant to the end of a file
152+
* @brief Monitored variable and the formatter of its current value
152153
*/
153-
virtual void appendFooter(std::string&, Csv) const
154+
struct Entry
154155
{
155-
}
156-
157-
virtual void appendFooter(std::string&, Json) const
158-
{
159-
}
160-
161-
virtual void appendFooter(std::string&, Yaml) const
162-
{
163-
}
156+
/// Device class, such as `Genrou`
157+
std::string_view device_class;
158+
/// Device identifier within its class
159+
std::string_view id;
160+
/// Variable name
161+
std::string_view variable;
162+
/// Formats the current value
163+
const ValueFormatter* value;
164+
};
164165

165-
///@}
166+
protected:
167+
/// Append one entry per monitored variable
168+
virtual void appendEntries(std::vector<Entry>&) const = 0;
166169
};
167170

168171
template <typename eval_type, template <typename, typename> typename model_data_type>

0 commit comments

Comments
 (0)