DPsim
Loading...
Searching...
No Matches
EMT_Ph1_Capacitor.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_Capacitor.h>
10
11using namespace CPS;
12
13EMT::Ph1::Capacitor::Capacitor(String uid, String name, Logger::Level logLevel)
14 : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
15 Base::Ph1::Capacitor(mAttributes) {
16 mEquivCurrent = 0;
17 **mIntfVoltage = Matrix::Zero(1, 1);
18 **mIntfCurrent = Matrix::Zero(1, 1);
19 setTerminalNumber(2);
20}
21
22SimPowerComp<Real>::Ptr EMT::Ph1::Capacitor::clone(String name) {
23 auto copy = Capacitor::make(name, mLogLevel);
24 copy->setParameters(**mCapacitance);
25 return copy;
26}
27
29
30 Real omega = 2 * PI * frequency;
31 Complex impedance = {0, -1. / (omega * **mCapacitance)};
32 Complex voltage =
33 RMS3PH_TO_PEAK1PH * (initialSingleVoltage(1) - initialSingleVoltage(0));
34 (**mIntfVoltage)(0, 0) = voltage.real();
35 (**mIntfCurrent)(0, 0) = (voltage / impedance).real();
36
37 SPDLOG_LOGGER_INFO(mSLog,
38 "\n--- Initialization from powerflow ---"
39 "\nVoltage across: {:f}"
40 "\nCurrent: {:f}"
41 "\nTerminal 0 voltage: {:f}"
42 "\nTerminal 1 voltage: {:f}"
43 "\n--- Initialization from powerflow finished ---",
44 (**mIntfVoltage)(0, 0), (**mIntfCurrent)(0, 0),
45 (RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)).real(),
46 (RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)).real());
47 mSLog->flush();
48}
49
50void EMT::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep,
51 Attribute<Matrix>::Ptr leftVector) {
53
54 mEquivCond = (2.0 * **mCapacitance) / timeStep;
55 // Update internal state
57 -(**mIntfCurrent)(0, 0) + -mEquivCond * (**mIntfVoltage)(0, 0);
58}
59
61 SparseMatrixRow &systemMatrix) {
62 MNAStampUtils::stampConductance(mEquivCond, systemMatrix, matrixNodeIndex(0),
63 matrixNodeIndex(1), terminalNotGrounded(0),
64 terminalNotGrounded(1), mSLog);
65}
66
68 Matrix &rightVector) {
70 -(**mIntfCurrent)(0, 0) + -mEquivCond * (**mIntfVoltage)(0, 0);
71 if (terminalNotGrounded(0))
72 Math::setVectorElement(rightVector, matrixNodeIndex(0), mEquivCurrent);
73 if (terminalNotGrounded(1))
74 Math::setVectorElement(rightVector, matrixNodeIndex(1), -mEquivCurrent);
75}
76
78 AttributeBase::List &prevStepDependencies,
79 AttributeBase::List &attributeDependencies,
80 AttributeBase::List &modifiedAttributes) {
81 // actually depends on C, but then we'd have to modify the system matrix anyway
82 modifiedAttributes.push_back(mRightVector);
83 prevStepDependencies.push_back(mIntfCurrent);
84 prevStepDependencies.push_back(mIntfVoltage);
85}
86
87void EMT::Ph1::Capacitor::mnaCompPreStep(Real time, Int timeStepCount) {
88 mnaCompApplyRightSideVectorStamp(**mRightVector);
89}
90
92 AttributeBase::List &prevStepDependencies,
93 AttributeBase::List &attributeDependencies,
94 AttributeBase::List &modifiedAttributes,
95 Attribute<Matrix>::Ptr &leftVector) {
96 attributeDependencies.push_back(leftVector);
97 modifiedAttributes.push_back(mIntfVoltage);
98 modifiedAttributes.push_back(mIntfCurrent);
99}
100
101void EMT::Ph1::Capacitor::mnaCompPostStep(Real time, Int timeStepCount,
102 Attribute<Matrix>::Ptr &leftVector) {
103 mnaCompUpdateVoltage(**leftVector);
104 mnaCompUpdateCurrent(**leftVector);
105}
106
107void EMT::Ph1::Capacitor::mnaCompUpdateVoltage(const Matrix &leftVector) {
108 // v1 - v0
109 (**mIntfVoltage)(0, 0) = 0;
110 if (terminalNotGrounded(1))
111 (**mIntfVoltage)(0, 0) =
112 Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
113 if (terminalNotGrounded(0))
114 (**mIntfVoltage)(0, 0) =
115 (**mIntfVoltage)(0, 0) -
116 Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
117}
118
119void EMT::Ph1::Capacitor::mnaCompUpdateCurrent(const Matrix &leftVector) {
120 (**mIntfCurrent)(0, 0) = mEquivCond * (**mIntfVoltage)(0, 0) + mEquivCurrent;
121}
const CPS::Attribute< Real >::Ptr mCapacitance
Capacitance [F].
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
Real mEquivCond
Equivalent conductance [S].
Capacitor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
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 mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
Real mEquivCurrent
DC equivalent current source [A].
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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< Real > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.