DPsim
Loading...
Searching...
No Matches
DP_Ph1_CurrentSource.cpp
Go to the documentation of this file.
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
10
11#include <limits>
12
13using namespace CPS;
14
16 Logger::Level logLevel)
17 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
18 mCurrentRef(mAttributes->createDynamic<Complex>("I_ref")) {
20 **mIntfVoltage = MatrixComp::Zero(1, 1);
21 **mIntfCurrent = MatrixComp::Zero(1, 1);
22}
23
29
33
35 **mCurrentRef = current;
36 mParametersSet = true;
37}
38
41 copy->setParameters(**mCurrentRef);
42 return copy;
43}
44
46
47 // If the source reference was not configured explicitly, derive it from the
48 // connected power-flow terminal. This mirrors the DP Ph3 current source and
49 // lets a current-source load start from PF without a Python-side setParameters
50 // call. Terminal 0 is the source's positive terminal (v0 - v1).
51 if (!mParametersSet) {
53 if (std::abs(voltage) > std::numeric_limits<Real>::epsilon()) {
54 const Complex power =
55 terminal(0)->singlePower() - terminal(1)->singlePower();
56 // Map the terminal's complex power directly to the current reference:
57 // S = V * conj(I). The source's terminal-current convention matches
58 // the power-flow current convention for this component.
59 **mCurrentRef = std::conj(power / voltage);
60 }
61 }
62
63 (**mIntfVoltage)(0, 0) = initialSingleVoltage(0) - initialSingleVoltage(1);
64 (**mIntfCurrent)(0, 0) = **mCurrentRef;
65
66 SPDLOG_LOGGER_INFO(mSLog,
67 "\n--- Initialization from powerflow ---"
68 "\nVoltage across: {:s}"
69 "\nCurrent: {:s}"
70 "\nTerminal 0 voltage: {:s}"
71 "\nTerminal 1 voltage: {:s}"
72 "\n--- Initialization from powerflow finished ---",
77}
78
80 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
82 (**mIntfCurrent)(0, 0) = **mCurrentRef;
83}
84
86 AttributeBase::List &prevStepDependencies,
87 AttributeBase::List &attributeDependencies,
88 AttributeBase::List &modifiedAttributes) {
89 attributeDependencies.push_back(mCurrentRef);
90 modifiedAttributes.push_back(mRightVector);
91 modifiedAttributes.push_back(mIntfCurrent);
92}
93
95 // Keep the public interface value current; the direct solver assembles the
96 // RHS from mCurrentRef via the compact callback and needs no dense staging.
97 (**mIntfCurrent)(0, 0) = **mCurrentRef;
98}
99
101 Matrix &rightVector) {
102 (**mIntfCurrent)(0, 0) = **mCurrentRef;
103
104 if (terminalNotGrounded(0))
106 -(**mIntfCurrent)(0, 0));
107 if (terminalNotGrounded(1))
109 (**mIntfCurrent)(0, 0));
110}
111
113 Matrix &rightVector) {
114 const Complex current = **mCurrentRef;
115 if (terminalNotGrounded(0))
116 Math::addToVectorElement(rightVector, matrixNodeIndex(0), -current);
117 if (terminalNotGrounded(1))
118 Math::addToVectorElement(rightVector, matrixNodeIndex(1), current);
119}
120
122 AttributeBase::List &prevStepDependencies,
123 AttributeBase::List &attributeDependencies,
124 AttributeBase::List &modifiedAttributes,
125 Attribute<Matrix>::Ptr &leftVector) {
126 attributeDependencies.push_back(leftVector);
127 modifiedAttributes.push_back(mIntfVoltage);
128}
129
131 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
132 mnaCompUpdateVoltage(**leftVector);
133}
134
136 (**mIntfVoltage)(0, 0) = 0;
137 if (terminalNotGrounded(0))
138 (**mIntfVoltage)(0, 0) =
140 if (terminalNotGrounded(1))
141 (**mIntfVoltage)(0, 0) =
142 (**mIntfVoltage)(0, 0) -
144}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void mnaCompPreStep(Real time, Int timeStepCount) override
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.
const Attribute< Complex >::Ptr mCurrentRef
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void setCurrentRef(Complex current)
Set the reference through the public attribute interface and mark it explicit.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void setParameters(Complex current)
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.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::Ptr terminal(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< CurrentSource > make(Args &&...args)
Definition PtrFactory.h:19
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61