DPsim
DP_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/DP/DP_Ph1_CurrentSource.h>
10 
11 using namespace CPS;
12 
13 DP::Ph1::CurrentSource::CurrentSource(String uid, String name,
14  Logger::Level logLevel)
15  : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
16  mCurrentRef(mAttributes->createDynamic<Complex>("I_ref")) {
17  setTerminalNumber(2);
18  **mIntfVoltage = MatrixComp::Zero(1, 1);
19  **mIntfCurrent = MatrixComp::Zero(1, 1);
20 }
21 
22 DP::Ph1::CurrentSource::CurrentSource(String name, Complex current,
23  Logger::Level logLevel)
24  : CurrentSource(name, logLevel) {
25  setParameters(current);
26 }
27 
28 void DP::Ph1::CurrentSource::setParameters(Complex current) {
29  **mCurrentRef = current;
30  mParametersSet = true;
31 }
32 
34  auto copy = CurrentSource::make(name, mLogLevel);
35  copy->setParameters(**mCurrentRef);
36  return copy;
37 }
38 
40 
41  (**mIntfVoltage)(0, 0) = initialSingleVoltage(0) - initialSingleVoltage(1);
42  (**mIntfCurrent)(0, 0) = **mCurrentRef;
43 
44  SPDLOG_LOGGER_INFO(mSLog,
45  "\n--- Initialization from powerflow ---"
46  "\nVoltage across: {:s}"
47  "\nCurrent: {:s}"
48  "\nTerminal 0 voltage: {:s}"
49  "\nTerminal 1 voltage: {:s}"
50  "\n--- Initialization from powerflow finished ---",
51  Logger::phasorToString((**mIntfVoltage)(0, 0)),
52  Logger::phasorToString((**mIntfCurrent)(0, 0)),
53  Logger::phasorToString(initialSingleVoltage(0)),
54  Logger::phasorToString(initialSingleVoltage(1)));
55 }
56 
57 void DP::Ph1::CurrentSource::mnaCompInitialize(
58  Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
59  updateMatrixNodeIndices();
60  (**mIntfCurrent)(0, 0) = **mCurrentRef;
61 }
62 
64  AttributeBase::List &prevStepDependencies,
65  AttributeBase::List &attributeDependencies,
66  AttributeBase::List &modifiedAttributes) {
67  attributeDependencies.push_back(mCurrentRef);
68  modifiedAttributes.push_back(mRightVector);
69  modifiedAttributes.push_back(mIntfCurrent);
70 }
71 
72 void DP::Ph1::CurrentSource::mnaCompPreStep(Real time, Int timeStepCount) {
73  mnaCompApplyRightSideVectorStamp(**mRightVector);
74 }
75 
77  Matrix &rightVector) {
78  (**mIntfCurrent)(0, 0) = **mCurrentRef;
79 
80  if (terminalNotGrounded(0))
81  Math::setVectorElement(rightVector, matrixNodeIndex(0),
82  -(**mIntfCurrent)(0, 0));
83  if (terminalNotGrounded(1))
84  Math::setVectorElement(rightVector, matrixNodeIndex(1),
85  (**mIntfCurrent)(0, 0));
86 }
87 
89  AttributeBase::List &prevStepDependencies,
90  AttributeBase::List &attributeDependencies,
91  AttributeBase::List &modifiedAttributes,
92  Attribute<Matrix>::Ptr &leftVector) {
93  attributeDependencies.push_back(leftVector);
94  modifiedAttributes.push_back(mIntfVoltage);
95 }
96 
97 void DP::Ph1::CurrentSource::mnaCompPostStep(
98  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
99  mnaCompUpdateVoltage(**leftVector);
100 }
101 
102 void DP::Ph1::CurrentSource::mnaCompUpdateVoltage(const Matrix &leftVector) {
103  (**mIntfVoltage)(0, 0) = 0;
104  if (terminalNotGrounded(0))
105  (**mIntfVoltage)(0, 0) =
106  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
107  if (terminalNotGrounded(1))
108  (**mIntfVoltage)(0, 0) =
109  (**mIntfVoltage)(0, 0) -
110  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
111 }
Dynamic phasor ideal current source.
CurrentSource(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name and logging level.
SimPowerComp< Complex >::Ptr clone(String copySuffix) 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 mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
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< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45