DPsim
Loading...
Searching...
No Matches
DP_Ph3_Resistor.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-models/DP/DP_Ph3_Resistor.h>
10
11using namespace CPS;
12
13DP::Ph3::Resistor::Resistor(String uid, String name, Logger::Level logLevel)
14 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15 Base::Ph3::Resistor(mAttributes) {
16 mPhaseType = PhaseType::ABC;
17 setTerminalNumber(2);
18 **mIntfVoltage = MatrixComp::Zero(3, 1);
19 **mIntfCurrent = MatrixComp::Zero(3, 1);
20}
21
22SimPowerComp<Complex>::Ptr DP::Ph3::Resistor::clone(String name) {
23 auto copy = Resistor::make(name, mLogLevel);
24 copy->setParameters(**mResistance);
25 return copy;
26}
27
29
30 Real voltMag = Math::abs((**mIntfVoltage)(0, 0));
31 Real voltPhase = Math::phase((**mIntfVoltage)(0, 0));
32 (**mIntfVoltage)(1, 0) = Complex(voltMag * cos(voltPhase - 2. / 3. * M_PI),
33 voltMag * sin(voltPhase - 2. / 3. * M_PI));
34 (**mIntfVoltage)(2, 0) = Complex(voltMag * cos(voltPhase + 2. / 3. * M_PI),
35 voltMag * sin(voltPhase + 2. / 3. * M_PI));
36 **mIntfCurrent = (**mResistance).inverse() * **mIntfVoltage;
37
38 SPDLOG_LOGGER_INFO(mSLog,
39 "\n--- Initialization from powerflow ---"
40 "\nVoltage across amplitude and phase: \n{:s}"
41 "\nCurrent amplitude and phase: \n{:s}"
42 "\nTerminal 0 voltage amplitude and phase: \n{:s}"
43 "\nTerminal 1 voltage amplitude and phase: \n{:s}"
44 "\n--- Initialization from powerflow finished ---",
45 Logger::phasorMatrixToString(**mIntfVoltage),
46 Logger::phasorMatrixToString(**mIntfCurrent),
47 Logger::phasorMatrixToString(initialVoltage(0)),
48 Logger::phasorMatrixToString(initialVoltage(1)));
49}
50
51void DP::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep,
52 Attribute<Matrix>::Ptr leftVector) {
53 updateMatrixNodeIndices();
54 **mRightVector = Matrix::Zero(0, 0);
55}
56
58 SparseMatrixRow &systemMatrix) {
59 MatrixFixedSizeComp<3, 3> conductance = Matrix::Zero(3, 3);
60 conductance.real() = (**mResistance).inverse();
61
62 MNAStampUtils::stampAdmittanceMatrix(
63 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
64 terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
65}
66
68 AttributeBase::List &prevStepDependencies,
69 AttributeBase::List &attributeDependencies,
70 AttributeBase::List &modifiedAttributes,
71 Attribute<Matrix>::Ptr &leftVector) {
72 attributeDependencies.push_back(leftVector);
73 modifiedAttributes.push_back(mIntfVoltage);
74 modifiedAttributes.push_back(mIntfCurrent);
75}
76
77void DP::Ph3::Resistor::mnaCompPostStep(Real time, Int timeStepCount,
78 Attribute<Matrix>::Ptr &leftVector) {
79 mnaCompUpdateVoltage(**leftVector);
80 mnaCompUpdateCurrent(**leftVector);
81}
82
83void DP::Ph3::Resistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
84 // Voltage across component is defined as V1 - V0
85 **mIntfVoltage = MatrixComp::Zero(3, 1);
86 if (terminalNotGrounded(1)) {
87 (**mIntfVoltage)(0, 0) =
88 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 0));
89 (**mIntfVoltage)(1, 0) =
90 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 1));
91 (**mIntfVoltage)(2, 0) =
92 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 2));
93 }
94 if (terminalNotGrounded(0)) {
95 (**mIntfVoltage)(0, 0) =
96 (**mIntfVoltage)(0, 0) -
97 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 0));
98 (**mIntfVoltage)(1, 0) =
99 (**mIntfVoltage)(1, 0) -
100 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 1));
101 (**mIntfVoltage)(2, 0) =
102 (**mIntfVoltage)(2, 0) -
103 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 2));
104 }
105
106 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage A: {} < {}",
107 std::abs((**mIntfVoltage)(0, 0)),
108 std::arg((**mIntfVoltage)(0, 0)));
109}
110
111void DP::Ph3::Resistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
112 **mIntfCurrent = (**mResistance).inverse() * **mIntfVoltage;
113
114 SPDLOG_LOGGER_DEBUG(mSLog, "Current A: {} < {}",
115 std::abs((**mIntfCurrent)(0, 0)),
116 std::arg((**mIntfCurrent)(0, 0)));
117}
const CPS::Attribute< Matrix >::Ptr mResistance
Resistance [ohm].
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
Resistor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.