DPsim
Loading...
Searching...
No Matches
DataLoggerInterface.h
1/* An interface for data loggers for simulation data
2 * logging.
3 *
4 * Author: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
5 * SPDX-FileCopyrightText: 2024 Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
6 * SPDX-License-Identifier: Apache-2.0
7 */
8#pragma once
9
10#include <dpsim-models/Attribute.h>
11#include <dpsim-models/Filesystem.h>
12#include <dpsim-models/PtrFactory.h>
13#include <dpsim-models/SimNode.h>
14#include <dpsim-models/Task.h>
15#include <dpsim/Definitions.h>
16#include <dpsim/Scheduler.h>
17#include <iomanip>
18
19namespace DPsim {
20
21class DataLoggerInterface {
22protected:
23 std::map<String, CPS::AttributeBase::Ptr> mAttributes;
24
25public:
26 typedef std::shared_ptr<DataLoggerInterface> Ptr;
27 typedef std::vector<DataLoggerInterface::Ptr> List;
28
29 DataLoggerInterface() : mAttributes(){};
30
31 virtual ~DataLoggerInterface() = default;
32
33 // Start the logger. After starting the number of columns should not be changed.
34 virtual void start() = 0;
35 // Stops the logger. Afterwards it should not be used anymore.
36 virtual void stop() = 0;
37
38 virtual void logAttribute(const String &name, CPS::AttributeBase::Ptr attr,
39 UInt rowsMax = 0, UInt colsMax = 0) {
40 if (auto attrReal =
41 std::dynamic_pointer_cast<CPS::Attribute<Real>>(attr.getPtr())) {
42 mAttributes[name] = attrReal;
43 } else if (auto attrComp =
44 std::dynamic_pointer_cast<CPS::Attribute<Complex>>(
45 attr.getPtr())) {
46 mAttributes[name + ".re"] = attrComp->deriveReal();
47 mAttributes[name + ".im"] = attrComp->deriveImag();
48 } else if (auto attrInt = std::dynamic_pointer_cast<CPS::Attribute<Int>>(
49 attr.getPtr())) {
50 mAttributes[name] = attrInt;
51 } else if (auto attrMatrix =
52 std::dynamic_pointer_cast<CPS::Attribute<Matrix>>(
53 attr.getPtr())) {
54 UInt rows = static_cast<UInt>((**attrMatrix).rows());
55 UInt cols = static_cast<UInt>((**attrMatrix).cols());
56 if (rowsMax == 0 || rowsMax > rows)
57 rowsMax = rows;
58 if (colsMax == 0 || colsMax > cols)
59 colsMax = cols;
60 if (rows == 1 && cols == 1) {
61 mAttributes[name] = attrMatrix->deriveCoeff<Real>(0, 0);
62 } else if (cols == 1) {
63 for (UInt k = 0; k < rowsMax; ++k) {
64 mAttributes[name + "_" + std::to_string(k)] =
65 attrMatrix->deriveCoeff<Real>(k, 0);
66 }
67 } else {
68 for (UInt k = 0; k < rowsMax; ++k) {
69 for (UInt l = 0; l < colsMax; ++l) {
70 mAttributes[name + "_" + std::to_string(k) + "_" +
71 std::to_string(l)] =
72 attrMatrix->deriveCoeff<Real>(k, l);
73 }
74 }
75 }
76 } else if (auto attrMatrix =
77 std::dynamic_pointer_cast<CPS::Attribute<MatrixComp>>(
78 attr.getPtr())) {
79 UInt rows = static_cast<UInt>((**attrMatrix).rows());
80 UInt cols = static_cast<UInt>((**attrMatrix).cols());
81 if (rowsMax == 0 || rowsMax > rows)
82 rowsMax = rows;
83 if (colsMax == 0 || colsMax > cols)
84 colsMax = cols;
85 if (rows == 1 && cols == 1) {
86 mAttributes[name + ".re"] =
87 attrMatrix->deriveCoeff<Complex>(0, 0)->deriveReal();
88 mAttributes[name + ".im"] =
89 attrMatrix->deriveCoeff<Complex>(0, 0)->deriveImag();
90 } else if (cols == 1) {
91 for (UInt k = 0; k < rowsMax; ++k) {
92 mAttributes[name + "_" + std::to_string(k) + ".re"] =
93 attrMatrix->deriveCoeff<Complex>(k, 0)->deriveReal();
94 mAttributes[name + "_" + std::to_string(k) + ".im"] =
95 attrMatrix->deriveCoeff<Complex>(k, 0)->deriveImag();
96 }
97 } else {
98 for (UInt k = 0; k < rowsMax; ++k) {
99 for (UInt l = 0; l < colsMax; ++l) {
100 mAttributes[name + "_" + std::to_string(k) + "_" +
101 std::to_string(l) + ".re"] =
102 attrMatrix->deriveCoeff<Complex>(k, l)->deriveReal();
103 mAttributes[name + "_" + std::to_string(k) + "_" +
104 std::to_string(l) + ".im"] =
105 attrMatrix->deriveCoeff<Complex>(k, l)->deriveImag();
106 }
107 }
108 }
109 } else {
110 throw std::runtime_error(
111 "DataLoggerInterface: Unknown attribute type for attribute " + name);
112 }
113 }
114
117 void logAttribute(const std::vector<String> &name,
118 CPS::AttributeBase::Ptr attr) {
119 if (auto attrMatrix =
120 std::dynamic_pointer_cast<CPS::Attribute<Matrix>>(attr.getPtr())) {
121 if ((**attrMatrix).rows() == 1 && (**attrMatrix).cols() == 1) {
122 logAttribute(name[0], attrMatrix->deriveCoeff<CPS::Real>(0, 0));
123 } else if ((**attrMatrix).cols() == 1) {
124 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
125 logAttribute(name[k], attrMatrix->deriveCoeff<CPS::Real>(k, 0));
126 }
127 } else {
128 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
129 for (UInt l = 0; l < (**attrMatrix).cols(); ++l) {
130 logAttribute(name[k * (**attrMatrix).cols() + l],
131 attrMatrix->deriveCoeff<CPS::Real>(k, l));
132 }
133 }
134 }
135 } else if (auto attrMatrix =
136 std::dynamic_pointer_cast<CPS::Attribute<MatrixComp>>(
137 attr.getPtr())) {
138 if ((**attrMatrix).rows() == 1 && (**attrMatrix).cols() == 1) {
139 logAttribute(name[0], attrMatrix->deriveCoeff<CPS::Complex>(0, 0));
140 } else if ((**attrMatrix).cols() == 1) {
141 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
142 logAttribute(name[k], attrMatrix->deriveCoeff<CPS::Complex>(k, 0));
143 }
144 } else {
145 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
146 for (UInt l = 0; l < (**attrMatrix).cols(); ++l) {
147 logAttribute(name[k * (**attrMatrix).cols() + l],
148 attrMatrix->deriveCoeff<CPS::Complex>(k, l));
149 }
150 }
151 }
152 }
153 }
154
155 virtual void log(Real time, Int timeStepCount) = 0;
156
157 virtual CPS::Task::Ptr getTask() = 0;
158};
159} // namespace DPsim
void logAttribute(const std::vector< String > &name, CPS::AttributeBase::Ptr attr)