DPsim
Loading...
Searching...
No Matches
DP_Ph3_NetworkInjection.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
5
6using namespace CPS;
7
11
13 Bool useSubcomponents,
14 Logger::Level logLevel)
15 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
16 mUseSubcomponents(useSubcomponents),
17 mVoltageRef(mAttributes->createDynamic<MatrixComp>("V_ref")),
18 mSrcFreq(mAttributes->createDynamic<Real>("f_src")) {
20 setVirtualNodeNumber(mUseSubcomponents ? 0 : 1);
22 **mIntfVoltage = MatrixComp::Zero(3, 1);
23 **mIntfCurrent = MatrixComp::Zero(3, 1);
24
25 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
26
27 // Create electrical sub components
28 if (mUseSubcomponents) {
29 mSubVoltageSource =
30 std::make_shared<DP::Ph3::VoltageSource>(**mName + "_vs", mLogLevel);
31 addMNASubComponent(mSubVoltageSource,
34 mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
35 mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
36 }
37}
38
40 auto copy = NetworkInjection::make(name, mUseSubcomponents, mLogLevel);
41 copy->setParameters(**mVoltageRef);
42 return copy;
43}
44
46 Real srcFreq) {
47 mParametersSet = true;
48
49 if (mUseSubcomponents)
50 mSubVoltageSource->setParameters(voltageRef, srcFreq);
51 else {
52 **mVoltageRef = voltageRef;
53 **mSrcFreq = srcFreq;
54 auto signal = Signal::SineWaveGenerator::make(**mName + "_sw");
55 signal->setParameters(Complex(1., 0.), srcFreq);
56 mDirectSignal = signal;
57 }
58
59 SPDLOG_LOGGER_INFO(mSLog,
60 "\nVoltage Ref={:s} [V]"
61 "\nFrequency={:s} [Hz]",
63 Logger::realToString(srcFreq));
64}
65
67 Real frequency) {
68 // Connect electrical subcomponents
69 if (mUseSubcomponents) {
70 mSubVoltageSource->connect({SimNode::GND, node(0)});
71 return;
72 }
73 if ((**mVoltageRef).isZero())
76 if (!mDirectSignal) {
77 auto signal = Signal::SineWaveGenerator::make(**mName + "_sw");
78 signal->setParameters(Complex(1., 0.), **mSrcFreq);
79 mDirectSignal = signal;
80 }
81}
82
84 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
85 if (!mUseSubcomponents) {
87 }
88}
89
91 SparseMatrixRow &systemMatrix) {
92 if (mUseSubcomponents)
93 return;
95 for (UInt phase = 0; phase < 3; ++phase) {
96 const UInt sourceCurrent = virtualNode(0)->matrixNodeIndex(phases[phase]);
97 if (terminalNotGrounded(0)) {
98 Math::setMatrixElement(systemMatrix, matrixNodeIndex(0, phase),
99 sourceCurrent, Complex(1., 0.));
100 Math::setMatrixElement(systemMatrix, sourceCurrent,
101 matrixNodeIndex(0, phase), Complex(1., 0.));
102 }
103 }
104}
105
106// #### MNA functions ####
108 Matrix &rightVector) {
109 if (!mUseSubcomponents) {
110 const PhaseType phases[] = {PhaseType::A, PhaseType::B, PhaseType::C};
111 for (UInt phase = 0; phase < 3; ++phase)
112 Math::setVectorElement(rightVector,
113 virtualNode(0)->matrixNodeIndex(phases[phase]),
114 (**mIntfVoltage)(phase, 0));
115 }
116 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
117 Logger::matrixToString(rightVector));
118}
119
121 AttributeBase::List &prevStepDependencies,
122 AttributeBase::List &attributeDependencies,
123 AttributeBase::List &modifiedAttributes) {
124 prevStepDependencies.push_back(mIntfCurrent);
125 prevStepDependencies.push_back(mIntfVoltage);
126 modifiedAttributes.push_back(mRightVector);
127}
128
130 if (!mUseSubcomponents) {
131 mDirectSignal->step(time);
132 **mIntfVoltage = **mVoltageRef * mDirectSignal->getSignal();
133 }
135}
136
138 AttributeBase::List &prevStepDependencies,
139 AttributeBase::List &attributeDependencies,
140 AttributeBase::List &modifiedAttributes,
141 Attribute<Matrix>::Ptr &leftVector) {
142 attributeDependencies.push_back(leftVector);
143 modifiedAttributes.push_back(mIntfVoltage);
144 modifiedAttributes.push_back(mIntfCurrent);
145}
146
148 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
149 mnaCompUpdateCurrent(**leftVector);
150 mnaCompUpdateVoltage(**leftVector);
151}
152
154 if (mUseSubcomponents)
155 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
156}
157
159 if (mUseSubcomponents)
160 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
161 else
162 for (UInt phase = 0; phase < 3; ++phase) {
163 const PhaseType phases[] = {PhaseType::A, PhaseType::B, PhaseType::C};
164 (**mIntfCurrent)(phase, 0) = Math::complexFromVectorElement(
165 leftVector, virtualNode(0)->matrixNodeIndex(phases[phase]));
166 }
167}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
NetworkInjection(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
const CPS::Attribute< MatrixComp >::Ptr mVoltageRef
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Returns current through the component.
void setParameters(MatrixComp voltageRef, Real srcFreq=0.0)
Setter for reference voltage parameters.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const CPS::Attribute< Real >::Ptr mSrcFreq
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String realToString(const Real &num)
Definition Logger.cpp:69
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 setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static MatrixComp singlePhaseVariableToThreePhase(Complex var_1ph)
To convert single phase complex variables (voltages, currents) to symmetrical three phase ones.
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimNode< Complex >::Ptr node(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
SimNode< Complex >::Ptr virtualNode(UInt index)
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< NetworkInjection > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
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
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72