9 #include <dpsim-models/EMT/EMT_Ph3_CurrentSource.h>
14 Logger::Level logLevel)
16 mCurrentRef(mAttributes->create<MatrixComp>(
"I_ref")),
17 mSrcFreq(mAttributes->createDynamic<Real>(
"f_src")),
18 mSigOut(mAttributes->createDynamic<Complex>(
"sigOut")) {
19 mPhaseType = PhaseType::ABC;
20 setVirtualNodeNumber(0);
26 auto copy = CurrentSource::make(name, mLogLevel);
27 copy->setParameters(attributeTyped<MatrixComp>(
"I_ref")->get(),
28 attributeTyped<Real>(
"f_src")->get());
34 auto srcSigSine = Signal::SineWaveGenerator::make(**mName +
"_sw");
36 srcSigSine->setParameters(Complex(1, 0), srcFreq);
39 **mCurrentRef = currentRef;
40 mSrcFreq->setReference(mSrcSig->mFreq);
42 mSLog->info(
"\nCurrent reference phasor [I]: {:s}"
43 "\nFrequency [Hz]: {:s}",
44 Logger::matrixCompToString(currentRef),
45 Logger::realToString(srcFreq));
47 mParametersSet =
true;
52 mSLog,
"\n--- Initialization from node voltages and terminal ---");
53 if (!mParametersSet) {
55 Signal::SineWaveGenerator::make(**mName +
"_sw", Logger::Level::off);
57 srcSigSine->setParameters(Complex(1, 0), frequency);
60 Complex v_ref = initialSingleVoltage(1) - initialSingleVoltage(0);
61 Complex s_ref = terminal(1)->singlePower() - terminal(0)->singlePower();
64 Complex i_ref = std::conj(s_ref / v_ref / sqrt(3.));
67 mSrcFreq->setReference(mSrcSig->attributeTyped<Real>(
"freq"));
69 SPDLOG_LOGGER_INFO(mSLog,
70 "\nReference current: {:s}"
71 "\nReference voltage: {:s}"
72 "\nReference power: {:s}"
73 "\nTerminal 0 voltage: {:s}"
74 "\nTerminal 1 voltage: {:s}"
75 "\nTerminal 0 power: {:s}"
76 "\nTerminal 1 power: {:s}",
77 Logger::phasorToString(i_ref),
78 Logger::phasorToString(v_ref),
79 Logger::complexToString(s_ref),
80 Logger::phasorToString(initialSingleVoltage(0)),
81 Logger::phasorToString(initialSingleVoltage(1)),
82 Logger::complexToString(terminal(0)->singlePower()),
83 Logger::complexToString(terminal(1)->singlePower()));
87 "\nInitialization from node voltages and terminal omitted (parameter "
89 "\nReference voltage: {:s}",
90 Logger::matrixCompToString(attributeTyped<MatrixComp>(
"I_ref")->get()));
93 mSLog,
"\n--- Initialization from node voltages and terminal ---");
99 updateMatrixNodeIndices();
103 Matrix &rightVector) {
104 if (terminalNotGrounded(1)) {
105 Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
106 -(**mIntfCurrent)(0, 0));
107 Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
108 -(**mIntfCurrent)(1, 0));
109 Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
110 -(**mIntfCurrent)(2, 0));
112 if (terminalNotGrounded(0)) {
113 Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
114 (**mIntfCurrent)(0, 0));
115 Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
116 (**mIntfCurrent)(1, 0));
117 Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
118 (**mIntfCurrent)(2, 0));
122 void EMT::Ph3::CurrentSource::updateCurrent(Real time) {
123 if (mSrcSig !=
nullptr) {
125 for (
int i = 0; i < 3; i++) {
126 (**mIntfCurrent)(i, 0) = RMS_TO_PEAK * Math::abs((**mCurrentRef)(i, 0)) *
127 cos(Math::phase(mSrcSig->getSignal()) +
128 Math::phase((**mCurrentRef)(i, 0)));
131 **mIntfCurrent = RMS_TO_PEAK * (**mCurrentRef).real();
133 SPDLOG_LOGGER_DEBUG(mSLog,
"\nUpdate current: {:s}",
134 Logger::matrixToString(**mIntfCurrent));
138 AttributeBase::List &prevStepDependencies,
139 AttributeBase::List &attributeDependencies,
140 AttributeBase::List &modifiedAttributes) {
141 attributeDependencies.push_back(mCurrentRef);
142 modifiedAttributes.push_back(mRightVector);
143 modifiedAttributes.push_back(mIntfVoltage);
148 mnaCompApplyRightSideVectorStamp(**mRightVector);
152 AttributeBase::List &prevStepDependencies,
153 AttributeBase::List &attributeDependencies,
154 AttributeBase::List &modifiedAttributes,
156 attributeDependencies.push_back(leftVector);
157 modifiedAttributes.push_back(mIntfVoltage);
162 mnaCompUpdateVoltage(**leftVector);
167 **mIntfVoltage = Matrix::Zero(3, 1);
168 if (terminalNotGrounded(1)) {
169 (**mIntfVoltage)(0, 0) =
170 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
171 (**mIntfVoltage)(1, 0) =
172 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
173 (**mIntfVoltage)(2, 0) =
174 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
176 if (terminalNotGrounded(0)) {
177 (**mIntfVoltage)(0, 0) =
178 (**mIntfVoltage)(0, 0) -
179 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
180 (**mIntfVoltage)(1, 0) =
181 (**mIntfVoltage)(1, 0) -
182 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
183 (**mIntfVoltage)(2, 0) =
184 (**mIntfVoltage)(2, 0) -
185 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Returns voltage through the component.
CurrentSource(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void setParameters(MatrixComp currentRef, Real srcFreq=50.0)
Setter for reference current.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
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 mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
Base class for all MNA components that are transmitting power.
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
Base class for all components that are transmitting power.
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
Current through component.
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Voltage between terminals.