DPsim
Loading...
Searching...
No Matches
SimPowerComp.cpp
Go to the documentation of this file.
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
10
11using namespace CPS;
12
13template <typename VarType>
15 Logger::Level logLevel)
16 : TopologicalPowerComp(uid, name, logLevel),
17 mIntfVoltage(mAttributes->create<MatrixVar<VarType>>("v_intf")),
18 mIntfCurrent(mAttributes->create<MatrixVar<VarType>>("i_intf")),
21 mTerminals.clear();
22}
23
24template <typename VarType>
28
29template <typename VarType> UInt SimPowerComp<VarType>::terminalNumber() {
30 return mNumTerminals;
31}
32
33template <typename VarType>
37
38template <typename VarType>
40 if (index >= mTerminals.size()) {
41 throw SystemError("Node not available for " + **mUID);
42 }
43 return mTerminals[index]->node();
44};
45
46template <typename VarType>
48 return mMatrixNodeIndices[nodeIndex * 3];
49}
50
51template <typename VarType>
53 return mMatrixNodeIndices[nodeIndex * 3 + phaseIndex];
54}
55
56template <typename VarType>
58 return node(index)->matrixNodeIndices();
59}
60
61template <typename VarType> UInt SimPowerComp<VarType>::virtualNodesNumber() {
62 return mNumVirtualNodes;
63}
64
65template <typename VarType> Bool SimPowerComp<VarType>::hasVirtualNodes() {
66 return mNumVirtualNodes > 0;
68
69template <typename VarType> Bool SimPowerComp<VarType>::hasSubComponents() {
70 return mSubComponents.size() > 0;
72
73template <typename VarType>
76}
78template <typename VarType>
80 return mVirtualNodes;
82
83template <typename VarType>
85 return virtualNode(index)->matrixNodeIndices();
86}
87
88template <typename VarType>
90 return virtualMatrixNodeIndices(nodeIndex)[phaseIndex];
91}
92
93template <typename VarType>
97
98template <typename VarType>
100 return **mIntfVoltage;
101}
103template <typename VarType>
105 return mTerminals[index]->initialVoltage();
107
108template <typename VarType>
110 return mTerminals[index]->initialSingleVoltage();
111}
113template <typename VarType>
115 return !mMatrixNodeIndexIsGround[index];
117
118template <typename VarType>
120 **mIntfCurrent = current;
121}
123template <typename VarType>
127
128// #### Terminals ####
129template <typename VarType>
132 std::count(mTerminals.begin(), mTerminals.end(), nullptr));
134
135template <typename VarType>
137 return (std::count(mTerminals.begin(), mTerminals.end(), nullptr) > 0);
138}
139
140template <typename VarType>
143 throw SystemError("Found unconnected Terminals for " + **mUID);
145}
147template <typename VarType>
149 if (index >= mTerminals.size()) {
150 throw SystemError("Terminal not available for " + **mUID);
151 }
152 return mTerminals[index];
154
155template <typename VarType>
163
164template <typename VarType>
166 mNumTerminals = num;
167 mTerminals.resize(mNumTerminals, nullptr);
168 mMatrixNodeIndices.resize(3 * num);
169 mMatrixNodeIndexIsGround.resize(num);
170}
171
172template <typename VarType>
175 if (mNumTerminals < terminals.size()) {
176 SPDLOG_LOGGER_ERROR(
177 mSLog, "Number of Terminals is too large for Component {} - Ignoring",
178 **mName);
179 return;
180 }
182}
183
184template <typename VarType>
186 typename SimTerminal<VarType>::Ptr terminal, UInt terminalPosition) {
187 if (mNumTerminals <= terminalPosition) {
188 SPDLOG_LOGGER_ERROR(
189 mSLog, "Terminal position number too large for Component {} - Ignoring",
190 **mName);
191 return;
192 }
193 mTerminals[terminalPosition] = terminal;
194 SPDLOG_LOGGER_INFO(
195 mSLog, "Set Terminal at position {} to Node {}, simulation node {}",
196 terminalPosition, mTerminals[terminalPosition]->node()->name(),
197 mTerminals[terminalPosition]->matrixNodeIndex());
198}
199
200template <typename VarType>
202 for (UInt nodeIdx = 0; nodeIdx < mNumTerminals; nodeIdx++) {
203 mMatrixNodeIndices[3 * nodeIdx] =
204 node(nodeIdx)->matrixNodeIndex(PhaseType::A);
205 mMatrixNodeIndices[3 * nodeIdx + 1] =
206 node(nodeIdx)->matrixNodeIndex(PhaseType::B);
207 mMatrixNodeIndices[3 * nodeIdx + 2] =
208 node(nodeIdx)->matrixNodeIndex(PhaseType::C);
209 mMatrixNodeIndexIsGround[nodeIdx] = node(nodeIdx)->isGround();
210 }
211}
212
213// #### Nodes ####
214template <typename VarType> UInt SimPowerComp<VarType>::nodeNumber() {
215 return static_cast<UInt>(
216 std::count(mTerminals.begin(), mTerminals.end(), nullptr));
217}
218
219template <typename VarType>
222 for (typename SimTerminal<VarType>::Ptr term : mTerminals) {
223 // mTerminals is preallocated with nullptr and only filled by connect(),
224 // so an unconnected component must not crash the lookup (see #626)
225 if (!term)
226 continue;
227 nodes.push_back(term->node());
228 }
229 return nodes;
230}
231
232// #### Virtual Nodes ####
233template <typename VarType>
235 mNumVirtualNodes = num;
236 mVirtualNodes.resize(mNumVirtualNodes, nullptr);
237
238 for (UInt idx = 0; idx < mNumVirtualNodes; idx++) {
239 String nodeName = **mName + "_vnode_" + std::to_string(idx);
240 setVirtualNodeAt(std::make_shared<SimNode<VarType>>(nodeName, mPhaseType),
241 idx);
242 }
243}
244
245template <typename VarType>
247 typename SimNode<VarType>::Ptr virtualNode, UInt nodeNum) {
248 if (mNumVirtualNodes <= nodeNum) {
249 SPDLOG_LOGGER_ERROR(
250 mSLog,
251 "Virtual Node position number too large for Component {} - Ignoring",
252 **mName);
253 }
254 mVirtualNodes[nodeNum] = virtualNode;
255 SPDLOG_LOGGER_INFO(
256 mSLog, "Set virtual Node at position {} to Node {}, simulation node {}",
257 nodeNum, mVirtualNodes[nodeNum]->name(),
258 mVirtualNodes[nodeNum]->matrixNodeIndex());
259}
260
261template <typename VarType>
263 if (index >= mVirtualNodes.size()) {
264 throw SystemError("Node not available for " + **mUID);
265 }
266 return mVirtualNodes[index];
267}
268
269// #### Other functions ####
270template <typename VarType>
272 if (mNumTerminals < nodes.size()) {
273 SPDLOG_LOGGER_ERROR(
274 mSLog, "Number of Nodes is too large for Component {} - Ignoring",
275 **mName);
276 return;
277 }
278 for (UInt i = 0; i < nodes.size(); i++) {
279 if (nodes[i] == nullptr) {
280 throw SystemError(
281 fmt::format("Node is nullptr for Component {}", **mName));
282 }
283 String name = **mName + "_T" + std::to_string(i);
286 terminal->setNode(nodes[i]);
288 }
289}
290
291template <typename VarType>
293 mFrequencies = frequencies;
294 mNumFreqs = static_cast<UInt>(mFrequencies.size());
295
296 if (mPhaseType != PhaseType::ABC) {
299 } else {
302 }
303
304 for (auto node : mVirtualNodes)
305 node->initialize(frequencies);
306}
307
308// Declare specializations to move definitions to .cpp
309template class CPS::SimPowerComp<Real>;
310template class CPS::SimPowerComp<Complex>;
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
const Attribute< String >::Ptr mUID
Unique identifier.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
std::shared_ptr< SimNode< VarType > > Ptr
Definition SimNode.h:34
std::vector< Ptr > List
Definition SimNode.h:35
Base class for all components that are transmitting power.
UInt mNumFreqs
Number of network frequencies.
UInt matrixNodeIndex(UInt nodeIndex)
void setVirtualNodeAt(typename SimNode< VarType >::Ptr virtualNode, UInt nodeNum)
Sets the virtual node at index nodeNum.
const Attribute< MatrixVar< VarType > >::Ptr mIntfCurrent
Current through component.
SimTerminal< VarType >::List mTerminals
List of Terminals.
const MatrixVar< VarType > & intfVoltage()
Bool hasVirtualNodes()
Returns true if virtual node number is greater than zero.
std::vector< bool > mMatrixNodeIndexIsGround
UInt terminalNumber()
Returns nominal number of Terminals for this component type.
UInt virtualSimNode(UInt nodeIndex, UInt phaseIndex=0)
Get simulation node number from virtual node.
SimPowerComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Basic constructor that takes UID, name and log level.
PhaseType mPhaseType
UInt nodeNumber()
Returns the actual number of Nodes / Terminals that are already set to valid Nodes.
void checkForUnconnectedTerminals()
SimNode< VarType >::Ptr node(UInt index)
Get pointer to node.
MatrixVar< VarType > * mIntfVoltageValue
std::vector< UInt > mMatrixNodeIndices
"Cached" list of simulation nodes (to avoid shared_ptr accesses during simulation)
Matrix mFrequencies
List of considered network frequencies.
virtual void initialize(Matrix frequencies)
Initialize components with correct network frequencies.
SimTerminal< VarType >::Ptr terminal(UInt index)
Get pointer to Terminal.
const MatrixVar< VarType > & intfCurrent()
SimTerminal< VarType >::List terminals()
Return list of Terminal pointers.
std::vector< UInt > virtualMatrixNodeIndices(UInt index)
Get vector of simulation node numbers from virtual Node.
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< VarType > >::Ptr mIntfVoltage
Voltage between terminals.
void connect(typename SimNode< VarType >::List nodes)
Sets all nodes and checks for nominal number of Nodes for this Component.
SimPowerComp< VarType >::List subComponents()
Get list of subcomponents.
UInt virtualNodesNumber()
Returns nominal number of virtual nodes for this component type.
void setTerminalAt(typename SimTerminal< VarType >::Ptr terminal, UInt terminalPosition)
Sets Terminal at index terminalPosition.
std::vector< Ptr > List
void setIntfVoltage(MatrixVar< VarType > voltage)
MatrixComp initialVoltage(UInt index)
SimNode< Complex >::Ptr virtualNode(UInt index)
MatrixVar< VarType > * mIntfCurrentValue
void setIntfCurrent(MatrixVar< VarType > current)
TopologicalNode::List topologicalNodes()
Get nodes as base type TopologicalNode.
UInt terminalNumberConnected()
Returns the number of connected Terminals.
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Complex >::List mVirtualNodes
std::vector< std::shared_ptr< SimPowerComp< VarType > > > mSubComponents
void setTerminals(typename SimTerminal< VarType >::List terminals)
SimNode< VarType >::List & virtualNodes()
Bool terminalNotGrounded(UInt index)
Bool hasSubComponents()
Returns true if subcomponents included in this component.
Complex initialSingleVoltage(UInt index)
void updateMatrixNodeIndices()
Update the "cached" mMatrixNodeIndices and mMatrixNodeIndexIsGround members.
void setTerminalNumber(UInt num)
virtual SimPowerComp< VarType >::Ptr clone(String name)
Returns a modified copy of the component with the given suffix added to the name and without.
TopologicalTerminal::List topologicalTerminals()
Returns the list of terminals as TopologicalTerminal pointers.
std::vector< UInt > matrixNodeIndices(UInt index)
TODO replace with access to mMatrixNodeIndices.
std::shared_ptr< SimTerminal< VarType > > Ptr
Definition SimTerminal.h:24
std::vector< Ptr > List
Definition SimTerminal.h:25
std::vector< Ptr > List
UInt mNumTerminals
Determines the number of Terminals which can be connected to network Nodes.
UInt mNumVirtualNodes
Determines the number of virtual or internal Nodes.
Logger::Log mSLog
Component logger.
TopologicalPowerComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Basic constructor that takes UID, name and log level.
static std::shared_ptr< SimTerminal< VarType > > make(Args &&...args)
Definition PtrFactory.h:19
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
Eigen::Matrix< VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixVar
Definition Definitions.h:95
std::complex< Real > Complex
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58