DPsim
Loading...
Searching...
No Matches
EMT_Ph3_ControlledCurrentSource.cpp
Go to the documentation of this file.
1/* Copyright 2017-2020 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
11#include <cmath>
12#include <stdexcept>
13
14using namespace CPS;
15
18 : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
19 mCurrentRef(mAttributes->create<Matrix>("I_ref")) {
23 **mIntfVoltage = Matrix::Zero(3, 1);
24 **mIntfCurrent = Matrix::Zero(3, 1);
25}
31
33 **mCurrentRef = currentRef;
34 SPDLOG_LOGGER_INFO(mSLog, "\nCurrent reference phasor [I]: {:s}",
35 Logger::matrixCompToString(currentRef));
36 mParametersSet = true;
37}
38
40 Real frequency) {
41 SPDLOG_LOGGER_INFO(
42 mSLog, "\n--- Initialization from node voltages and terminal ---");
43 if (!mParametersSet) {
45 const Complex power =
46 terminal(1)->singlePower() - terminal(0)->singlePower();
47 if (std::abs(voltage) > 0.0) {
48 const Complex current = std::conj(power / voltage / std::sqrt(3.0));
49 **mCurrentRef =
52 } else if (std::abs(power) > 0.0) {
53 throw std::invalid_argument(
54 "ControlledCurrentSource cannot infer a current from non-zero "
55 "power at zero terminal voltage.");
56 } else {
57 **mCurrentRef = Matrix::Zero(3, 1);
58 }
59 mParametersSet = true;
60
61 SPDLOG_LOGGER_INFO(mSLog,
62 "\nTerminal 0 voltage: {:s}"
63 "\nTerminal 1 voltage: {:s}"
64 "\nTerminal 0 power: {:s}"
65 "\nTerminal 1 power: {:s}",
68 Logger::complexToString(terminal(0)->singlePower()),
69 Logger::complexToString(terminal(1)->singlePower()));
70 } else {
71 SPDLOG_LOGGER_INFO(
72 mSLog,
73 "\nInitialization from node voltages and terminal omitted (parameter "
74 "already set)."
75 "\nReference voltage: {:s}",
77 }
78 SPDLOG_LOGGER_INFO(
79 mSLog, "\n--- Initialization from node voltages and terminal ---");
80 mSLog->flush();
81}
82
87
89 Matrix &rightVector) {
90 if (terminalNotGrounded(1)) {
91 Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
92 -(**mIntfCurrent)(0, 0));
93 Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
94 -(**mIntfCurrent)(1, 0));
95 Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
96 -(**mIntfCurrent)(2, 0));
97 }
98 if (terminalNotGrounded(0)) {
99 Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
100 (**mIntfCurrent)(0, 0));
101 Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
102 (**mIntfCurrent)(1, 0));
103 Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
104 (**mIntfCurrent)(2, 0));
105 }
106}
107
110
111 SPDLOG_LOGGER_DEBUG(mSLog, "\nUpdate current: {:s}",
113}
114
116 AttributeBase::List &prevStepDependencies,
117 AttributeBase::List &attributeDependencies,
118 AttributeBase::List &modifiedAttributes) {
119 attributeDependencies.push_back(mCurrentRef);
120 modifiedAttributes.push_back(mRightVector);
121 modifiedAttributes.push_back(mIntfVoltage);
122}
123
129
131 AttributeBase::List &prevStepDependencies,
132 AttributeBase::List &attributeDependencies,
133 AttributeBase::List &modifiedAttributes,
134 Attribute<Matrix>::Ptr &leftVector) {
135 attributeDependencies.push_back(leftVector);
136 modifiedAttributes.push_back(mIntfVoltage);
137};
138
140 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
141 mnaCompUpdateVoltage(**leftVector);
142}
143
145 const Matrix &leftVector) {
146 // v1 - v0
147 (**mIntfVoltage).setZero();
148 if (terminalNotGrounded(1)) {
149 (**mIntfVoltage)(0, 0) =
151 (**mIntfVoltage)(1, 0) =
153 (**mIntfVoltage)(2, 0) =
155 }
156 if (terminalNotGrounded(0)) {
157 (**mIntfVoltage)(0, 0) =
158 (**mIntfVoltage)(0, 0) -
160 (**mIntfVoltage)(1, 0) =
161 (**mIntfVoltage)(1, 0) -
163 (**mIntfVoltage)(2, 0) =
164 (**mIntfVoltage)(2, 0) -
166 }
167}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void setParameters(Matrix currentRef)
Setter for reference current.
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Returns voltage through the component.
ControlledCurrentSource(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
Attribute< T >::Ptr attributeTyped(const String &name) const
Return pointer to an attribute.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String complexToString(const Complex &num)
Definition Logger.cpp:63
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< ControlledCurrentSource > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS_TO_PEAK
Definition Definitions.h:48
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
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61