DPsim
Loading...
Searching...
No Matches
DataLoggerInterface.h
Go to the documentation of this file.
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
14#include <dpsim-models/Task.h>
15#include <dpsim/Definitions.h>
16#include <dpsim/Scheduler.h>
17#include <iomanip>
18
19namespace DPsim {
20
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
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 attrBool = std::dynamic_pointer_cast<CPS::Attribute<Bool>>(
52 attr.getPtr())) {
53 mAttributes[name] = attrBool;
54 } else if (auto attrMatrix =
55 std::dynamic_pointer_cast<CPS::Attribute<Matrix>>(
56 attr.getPtr())) {
57 UInt rows = static_cast<UInt>((**attrMatrix).rows());
58 UInt cols = static_cast<UInt>((**attrMatrix).cols());
59 if (rowsMax == 0 || rowsMax > rows)
60 rowsMax = rows;
61 if (colsMax == 0 || colsMax > cols)
62 colsMax = cols;
63 if (rows == 1 && cols == 1) {
64 mAttributes[name] = attrMatrix->deriveCoeff<Real>(0, 0);
65 } else if (cols == 1) {
66 for (UInt k = 0; k < rowsMax; ++k) {
67 mAttributes[name + "_" + std::to_string(k)] =
68 attrMatrix->deriveCoeff<Real>(k, 0);
69 }
70 } else {
71 for (UInt k = 0; k < rowsMax; ++k) {
72 for (UInt l = 0; l < colsMax; ++l) {
73 mAttributes[name + "_" + std::to_string(k) + "_" +
74 std::to_string(l)] =
75 attrMatrix->deriveCoeff<Real>(k, l);
76 }
77 }
78 }
79 } else if (auto attrMatrix =
80 std::dynamic_pointer_cast<CPS::Attribute<MatrixComp>>(
81 attr.getPtr())) {
82 UInt rows = static_cast<UInt>((**attrMatrix).rows());
83 UInt cols = static_cast<UInt>((**attrMatrix).cols());
84 if (rowsMax == 0 || rowsMax > rows)
85 rowsMax = rows;
86 if (colsMax == 0 || colsMax > cols)
87 colsMax = cols;
88 if (rows == 1 && cols == 1) {
89 mAttributes[name + ".re"] =
90 attrMatrix->deriveCoeff<Complex>(0, 0)->deriveReal();
91 mAttributes[name + ".im"] =
92 attrMatrix->deriveCoeff<Complex>(0, 0)->deriveImag();
93 } else if (cols == 1) {
94 for (UInt k = 0; k < rowsMax; ++k) {
95 mAttributes[name + "_" + std::to_string(k) + ".re"] =
96 attrMatrix->deriveCoeff<Complex>(k, 0)->deriveReal();
97 mAttributes[name + "_" + std::to_string(k) + ".im"] =
98 attrMatrix->deriveCoeff<Complex>(k, 0)->deriveImag();
99 }
100 } else {
101 for (UInt k = 0; k < rowsMax; ++k) {
102 for (UInt l = 0; l < colsMax; ++l) {
103 mAttributes[name + "_" + std::to_string(k) + "_" +
104 std::to_string(l) + ".re"] =
105 attrMatrix->deriveCoeff<Complex>(k, l)->deriveReal();
106 mAttributes[name + "_" + std::to_string(k) + "_" +
107 std::to_string(l) + ".im"] =
108 attrMatrix->deriveCoeff<Complex>(k, l)->deriveImag();
109 }
110 }
111 }
112 } else {
113 throw std::runtime_error(
114 "DataLoggerInterface: Unknown attribute type for attribute " + name);
115 }
116 }
117
120 void logAttribute(const std::vector<String> &name,
122 if (auto attrMatrix =
123 std::dynamic_pointer_cast<CPS::Attribute<Matrix>>(attr.getPtr())) {
124 if ((**attrMatrix).rows() == 1 && (**attrMatrix).cols() == 1) {
125 logAttribute(name[0], attrMatrix->deriveCoeff<CPS::Real>(0, 0));
126 } else if ((**attrMatrix).cols() == 1) {
127 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
128 logAttribute(name[k], attrMatrix->deriveCoeff<CPS::Real>(k, 0));
129 }
130 } else {
131 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
132 for (UInt l = 0; l < (**attrMatrix).cols(); ++l) {
133 logAttribute(name[k * (**attrMatrix).cols() + l],
134 attrMatrix->deriveCoeff<CPS::Real>(k, l));
135 }
136 }
137 }
138 } else if (auto attrMatrix =
139 std::dynamic_pointer_cast<CPS::Attribute<MatrixComp>>(
140 attr.getPtr())) {
141 if ((**attrMatrix).rows() == 1 && (**attrMatrix).cols() == 1) {
142 logAttribute(name[0], attrMatrix->deriveCoeff<CPS::Complex>(0, 0));
143 } else if ((**attrMatrix).cols() == 1) {
144 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
145 logAttribute(name[k], attrMatrix->deriveCoeff<CPS::Complex>(k, 0));
146 }
147 } else {
148 for (UInt k = 0; k < (**attrMatrix).rows(); ++k) {
149 for (UInt l = 0; l < (**attrMatrix).cols(); ++l) {
150 logAttribute(name[k * (**attrMatrix).cols() + l],
151 attrMatrix->deriveCoeff<CPS::Complex>(k, l));
152 }
153 }
154 }
155 }
156 }
157
158 virtual void log(Real time, Int timeStepCount) = 0;
159
160 virtual CPS::Task::Ptr getTask() = 0;
161};
162} // namespace DPsim
AttributePointer< AttributeBase > Ptr
Definition Attribute.h:122
std::shared_ptr< T > getPtr() const
Definition Attribute.h:89
std::shared_ptr< Task > Ptr
Definition Task.h:27
std::vector< DataLoggerInterface::Ptr > List
virtual void logAttribute(const String &name, CPS::AttributeBase::Ptr attr, UInt rowsMax=0, UInt colsMax=0)
virtual ~DataLoggerInterface()=default
virtual CPS::Task::Ptr getTask()=0
std::shared_ptr< DataLoggerInterface > Ptr
std::map< String, CPS::AttributeBase::Ptr > mAttributes
void logAttribute(const std::vector< String > &name, CPS::AttributeBase::Ptr attr)
virtual void start()=0
virtual void log(Real time, Int timeStepCount)=0
double Real
Definition Definitions.h:62
std::complex< Real > Complex
Definition Definitions.h:63
CPS::Complex Complex
Definition Definitions.h:19
CPS::Real Real
Definition Definitions.h:18
CPS::String String
Definition Definitions.h:20
CPS::Int Int
Definition Definitions.h:22
CPS::UInt UInt
Definition Definitions.h:23