DPsim
Loading...
Searching...
No Matches
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
11using namespace CPS;
12
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
22DP::Ph1::CurrentSource::CurrentSource(String name, Complex current,
23 Logger::Level logLevel)
24 : CurrentSource(name, logLevel) {
25 setParameters(current);
26}
27
28void DP::Ph1::CurrentSource::setParameters(Complex current) {
29 **mCurrentRef = current;
30 mParametersSet = true;
31}
32
33SimPowerComp<Complex>::Ptr DP::Ph1::CurrentSource::clone(String name) {
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
57void 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
72void 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
97void DP::Ph1::CurrentSource::mnaCompPostStep(
98 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
99 mnaCompUpdateVoltage(**leftVector);
100}
101
102void 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}
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.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
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.