DPsim
Utils.cpp
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #include <dpsim/pybind/Utils.h>
10 
11 CPS::Matrix zeroMatrix(int dim) { return CPS::Matrix::Zero(dim, dim); }
12 
13 std::string getAttributeList(CPS::IdentifiedObject &obj) {
14  std::stringstream output;
15  output << std::setiosflags(std::ios::left);
16  output << "Attribute list for object with name " << obj.name() << ":"
17  << std::endl;
18  output << std::setw(30) << "name" << std::setw(20) << "type" << std::setw(15)
19  << "size" << std::setw(30) << "value" << std::endl;
20  output << std::setw(95) << std::setfill('-') << "" << std::setfill(' ')
21  << std::endl;
22  for (auto attr : obj.attributes()) {
23  std::string name = attr.first;
24  std::string type;
25  std::string value;
26  std::string size;
27  if (auto tryReal = std::dynamic_pointer_cast<CPS::Attribute<CPS::Real>>(
28  attr.second.getPtr())) {
29  type = "Real";
30  value = std::to_string(tryReal->get());
31  size = "";
32  } else if (auto tryComplex =
33  std::dynamic_pointer_cast<CPS::Attribute<CPS::Complex>>(
34  attr.second.getPtr())) {
35  type = "Complex";
36  value = std::to_string(abs(tryComplex->get())) + "<" +
37  std::to_string(arg(tryComplex->get()));
38  size = "";
39  } else if (auto tryMatrixReal =
40  std::dynamic_pointer_cast<CPS::Attribute<CPS::Matrix>>(
41  attr.second.getPtr())) {
42  type = "MatrixReal";
43  value = "[...]";
44  size = std::to_string(tryMatrixReal->get().rows()) + "x" +
45  std::to_string(tryMatrixReal->get().cols());
46  } else if (auto tryMatrixComp =
47  std::dynamic_pointer_cast<CPS::Attribute<CPS::MatrixComp>>(
48  attr.second.getPtr())) {
49  type = "MatrixComplex";
50  value = "[...]";
51  size = std::to_string(tryMatrixComp->get().rows()) + "x" +
52  std::to_string(tryMatrixComp->get().cols());
53  } else if (auto tryString =
54  std::dynamic_pointer_cast<CPS::Attribute<CPS::String>>(
55  attr.second.getPtr())) {
56  type = "String";
57  value = "\"" + tryString->get() + "\"";
58  size = "";
59  } else {
60  type = "Unknown";
61  value = "Unknown";
62  size = "Unknown";
63  }
64  output << std::setw(30) << name << std::setw(20) << type << std::setw(15)
65  << size << std::setw(30) << value << std::endl;
66  }
67  return output.str();
68 }
69 
70 void printAttributes(CPS::IdentifiedObject &obj) {
71  py::print(getAttributeList(obj));
72 }
73 
74 void printAttribute(CPS::IdentifiedObject &obj, std::string attrName) {
75  auto attr = obj.attribute(attrName);
76  if (auto tryReal =
77  std::dynamic_pointer_cast<CPS::Attribute<CPS::Real>>(attr.getPtr())) {
78  py::print("Attribute " + attrName + " on object " + obj.name() +
79  " has type Real and value " + std::to_string(tryReal->get()));
80  } else if (auto tryComplex =
81  std::dynamic_pointer_cast<CPS::Attribute<CPS::Complex>>(
82  attr.getPtr())) {
83  std::string value = std::to_string(abs(tryComplex->get())) + "<" +
84  std::to_string(arg(tryComplex->get()));
85  py::print("Attribute " + attrName + " on object " + obj.name() +
86  " has type Complex and value " + value);
87  } else if (auto tryMatrixReal =
88  std::dynamic_pointer_cast<CPS::Attribute<CPS::Matrix>>(
89  attr.getPtr())) {
90  std::string size = std::to_string(tryMatrixReal->get().rows()) + "x" +
91  std::to_string(tryMatrixReal->get().cols());
92  py::print("Attribute " + attrName + " on object " + obj.name() +
93  " has type MatrixReal (size " + size + ") and value:");
94  py::print(py::cast(tryMatrixReal->get()));
95  } else if (auto tryMatrixComp =
96  std::dynamic_pointer_cast<CPS::Attribute<CPS::MatrixComp>>(
97  attr.getPtr())) {
98  std::string size = std::to_string(tryMatrixComp->get().rows()) + "x" +
99  std::to_string(tryMatrixComp->get().cols());
100  py::print("Attribute " + attrName + " on object " + obj.name() +
101  " has type MatrixComplex (size " + size + ") and value:");
102  py::print(py::cast(tryMatrixComp->get()));
103  } else if (auto tryString =
104  std::dynamic_pointer_cast<CPS::Attribute<CPS::String>>(
105  attr.getPtr())) {
106  py::print("Attribute " + attrName + " on object " + obj.name() +
107  " has type String and value \"" + tryString->get() + "\"");
108  } else {
109  py::print("Could not determine type of attribute " + attrName);
110  }
111 }
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.