DPsim
EMT_Ph1_CurrentSource.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/EMT/EMT_Ph1_CurrentSource.h>
10 
11 using namespace CPS;
12 
14  Logger::Level logLevel)
15  : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
16  mCurrentRef(mAttributes->create<Complex>("I_ref")),
17  mSrcFreq(mAttributes->create<Real>("f_src")) {
18  setTerminalNumber(2);
19  **mIntfVoltage = Matrix::Zero(1, 1);
20  **mIntfCurrent = Matrix::Zero(1, 1);
21 }
22 
24  auto copy = CurrentSource::make(name, mLogLevel);
25  copy->setParameters(**mCurrentRef, **mSrcFreq);
26  return copy;
27 }
28 
29 void EMT::Ph1::CurrentSource::setParameters(Complex currentRef, Real srcFreq) {
30  **mCurrentRef = currentRef;
31  **mSrcFreq = srcFreq;
32 
33  mParametersSet = true;
34 }
35 
37  Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
38  updateMatrixNodeIndices();
39  (**mIntfCurrent)(0, 0) =
40  Math::abs(**mCurrentRef) * cos(Math::phase(**mCurrentRef));
41 }
42 
44  Matrix &rightVector) {
45  if (terminalNotGrounded(0))
46  Math::setVectorElement(rightVector, matrixNodeIndex(0),
47  -(**mIntfCurrent)(0, 0));
48 
49  if (terminalNotGrounded(1))
50  Math::setVectorElement(rightVector, matrixNodeIndex(1),
51  (**mIntfCurrent)(0, 0));
52 }
53 
54 void EMT::Ph1::CurrentSource::updateState(Real time) {
55  Complex currentRef = mCurrentRef->get();
56  Real srcFreq = mSrcFreq->get();
57  if (srcFreq > 0)
58  (**mIntfCurrent)(0, 0) =
59  Math::abs(currentRef) *
60  cos(time * 2. * PI * srcFreq + Math::phase(currentRef));
61  else
62  (**mIntfCurrent)(0, 0) = currentRef.real();
63 }
64 
66  AttributeBase::List &prevStepDependencies,
67  AttributeBase::List &attributeDependencies,
68  AttributeBase::List &modifiedAttributes) {
69  attributeDependencies.push_back(mCurrentRef);
70  modifiedAttributes.push_back(mRightVector);
71  modifiedAttributes.push_back(mIntfCurrent);
72 }
73 
74 void EMT::Ph1::CurrentSource::mnaCompPreStep(Real time, Int timeStepCount) {
75  updateState(time);
76  mnaCompApplyRightSideVectorStamp(**mRightVector);
77 }
78 
80  AttributeBase::List &prevStepDependencies,
81  AttributeBase::List &attributeDependencies,
82  AttributeBase::List &modifiedAttributes,
83  Attribute<Matrix>::Ptr &leftVector) {
84  attributeDependencies.push_back(leftVector);
85  modifiedAttributes.push_back(mIntfVoltage);
86 }
87 
88 void EMT::Ph1::CurrentSource::mnaCompPostStep(
89  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
90  mnaCompUpdateVoltage(**leftVector);
91 }
92 
93 void EMT::Ph1::CurrentSource::mnaCompUpdateVoltage(const Matrix &leftVector) {
94  (**mIntfVoltage)(0, 0) = 0;
95  if (terminalNotGrounded(0))
96  (**mIntfVoltage)(0, 0) =
97  Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
98  if (terminalNotGrounded(1))
99  (**mIntfVoltage)(0, 0) =
100  (**mIntfVoltage)(0, 0) -
101  Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
102 }
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
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.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
CurrentSource(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
Base class for all MNA components that are transmitting power.
Base class for all components that are transmitting power.
Definition: SimPowerComp.h:17
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45