DPsim
Loading...
Searching...
No Matches
DecouplingIdealTransformer_EMT_Ph3.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
7
8using namespace CPS;
9using namespace CPS::EMT::Ph3;
10using namespace CPS::Signal;
11
13 String name, Logger::Level logLevel)
14 : SimSignalComp(name, name, logLevel),
15 mStates(mAttributes->create<Matrix>("states")),
17 mAttributes->create<Matrix>("v_intf", Matrix::Zero(3, 1))),
19 mAttributes->create<Matrix>("i_intf", Matrix::Zero(3, 1))),
20 mSrcVoltageRef(mAttributes->create<Matrix>("v_ref", Matrix::Zero(3, 1))),
21 mSrcCurrentRef(mAttributes->create<Matrix>("i_ref", Matrix::Zero(3, 1))) {
22
23 mRes1 = Resistor::make(name + "_r1", logLevel);
24 mRes2 = Resistor::make(name + "_r2", logLevel);
27
28 mSrcVoltage = mVoltageSrc->mVoltageRef;
29 mSrcCurrent = mCurrentSrc->mCurrentRef;
30}
31
33 SimNode<Real>::Ptr node1, SimNode<Real>::Ptr node2, Real delay,
34 Matrix voltageSrcIntfCurr, Matrix current1Extrap0, CouplingMethod method) {
35
36 mNode1 = node1;
37 mNode2 = node2;
39
40 mDelay = delay;
41 mCouplingMethod = method;
42
45 }
46
47 mRes1->setParameters(mInternalSeriesResistance);
48 mRes1->connect({node1, mVirtualNode});
49 mRes2->setParameters(mInternalParallelResistance);
50 mRes2->connect({node2, SimNode<Real>::GND});
51 mVoltageSrc->setParameters(Matrix::Zero(3, 1));
52 mVoltageSrcIntfCurr = voltageSrcIntfCurr;
53 mCurrent1Extrap0 = current1Extrap0;
55 mCurrentSrc->setParameters(Matrix::Zero(3, 1));
56 mCurrentSrc->connect({node2, SimNode<Real>::GND});
57}
58
60 if (mDelay <= 0) {
61 mDelay = 0;
62 mBufSize = 1;
63 mAlpha = 1;
64 } else {
65 mBufSize = static_cast<UInt>(ceil(mDelay / timeStep));
66 mAlpha = 1 - (mBufSize - mDelay / timeStep);
67 }
68 SPDLOG_LOGGER_INFO(mSLog, "bufsize {} alpha {}", mBufSize, mAlpha);
69
70 mVoltageSrc->setIntfCurrent(mVoltageSrcIntfCurr);
71 MatrixComp cur1 = mVoltageSrc->mIntfCurrent->get();
72 MatrixComp volt2 = mNode2->initialVoltage();
73
74 mVirtualNode->setInitialVoltage(mNode1->initialVoltage() -
76
77 SPDLOG_LOGGER_INFO(mSLog, "initial current: i_1 {}", cur1);
78 SPDLOG_LOGGER_INFO(mSLog, "initial voltage: v_2 {}", volt2);
79
80 **mSrcVoltageRef = volt2.real();
81 **mSrcCurrentRef = cur1.real();
82 mVoltageSrc->setParameters(**mSrcVoltageRef);
83 mCurrentSrc->setParameters(**mSrcCurrentRef);
84
85 Matrix mSourceCurrentIntfVoltage = volt2.real();
86 mCurrentSrc->setIntfVoltage(mSourceCurrentIntfVoltage);
87
88 mVoltageSrc->setIntfVoltage(mSourceCurrentIntfVoltage);
89 mVoltageSrc->setIntfCurrent(mVoltageSrcIntfCurr);
90
91 **mSourceVoltageIntfVoltage = volt2.real();
92 **mSourceVoltageIntfCurrent = mVoltageSrc->intfCurrent();
93
94 // Resize ring buffers and initialize
95 mCur1 = cur1.real().transpose().replicate(mBufSize, 1);
96 mVol2 = volt2.real().transpose().replicate(mBufSize, 1);
97
98 SPDLOG_LOGGER_INFO(mSLog, "Verify initial current: i_1 {}",
99 mCurrentSrc->intfCurrent()(0, 0));
100 SPDLOG_LOGGER_INFO(mSLog, "Verify initial voltage: v_2 {}",
101 mVoltageSrc->intfVoltage()(0, 0));
102
104 mCur1Extrap.row(0) = mCurrent1Extrap0.real().transpose();
105 if (mExtrapolationDegree > 0) {
106 mCur1Extrap.row(1) = mVoltageSrcIntfCurr.transpose();
107 }
108 mVol2Extrap = volt2.real().transpose().replicate(mExtrapolationDegree + 1, 1);
109}
110
112 Matrix c1 = data.row(mBufIdx);
113 Matrix c2 = mBufIdx == mBufSize - 1 ? data.row(0) : data.row(mBufIdx + 1);
114 return (mAlpha * c1 + (1 - mAlpha) * c2).transpose();
115}
116
119 Matrix c1 = data.row(mMacroBufIdx);
121 ? data.row(0)
122 : data.row(mMacroBufIdx + 1);
123 Real delayFraction =
124 (mDelay * (mBufIdx + 1)) / static_cast<float>(mBufSize);
125 Real tEval = mDelay + delayFraction;
126 return (((c2 - c1) / mDelay) * tEval + c1).transpose();
127 } else {
128 return (data.row(mMacroBufIdx)).transpose();
129 }
130}
131
133 Matrix volt1, cur2;
135 volt1 = interpolate(mVol2);
136 cur2 = interpolate(mCur1);
137 } else {
138 volt1 = extrapolate(mVol2Extrap);
139 cur2 = extrapolate(mCur1Extrap);
140 }
141
142 // Update voltage and current
143 **mSrcVoltageRef = volt1;
144 **mSrcCurrentRef = cur2;
145 **mSourceVoltageIntfVoltage = mVoltageSrc->intfVoltage();
146 **mSourceVoltageIntfCurrent = mVoltageSrc->intfCurrent();
147
150}
151
153 Int timeStepCount) {
154 mITM.step(time, timeStepCount);
155}
156
158 // Update ringbuffers with new values
159 mCur1.row(mBufIdx) = -mVoltageSrc->intfCurrent().transpose();
160 mVol2.row(mBufIdx) = mCurrentSrc->intfVoltage().transpose();
161
162 mBufIdx++;
163 if (mBufIdx == mBufSize) {
164 mCur1Extrap.row(mMacroBufIdx) = mCur1.row(mBufIdx - 1);
165 mVol2Extrap.row(mMacroBufIdx) = mVol2.row(mBufIdx - 1);
166 mMacroBufIdx++;
168 mMacroBufIdx = 0;
169 }
170 mBufIdx = 0;
171 }
172}
173
175 Int timeStepCount) {
176 mITM.postStep();
177}
178
180 return Task::List(
181 {std::make_shared<PreStep>(*this), std::make_shared<PostStep>(*this)});
182}
183
187
std::vector< Ptr > List
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
void execute(Real time, Int timeStepCount)
std::shared_ptr< EMT::Ph3::ControlledVoltageSource > mVoltageSrc
const Attribute< Matrix >::Ptr mStates
FIXME: workaround for dependency analysis as long as the states aren't attributes.
std::shared_ptr< EMT::Ph3::ControlledCurrentSource > mCurrentSrc
DecouplingIdealTransformer_EMT_Ph3(String name, Logger::Level logLevel=Logger::Level::info)
void setParameters(SimNode< Real >::Ptr node1, SimNode< Real >::Ptr node2, Real delay, Matrix voltageSrcIntfCurr, Matrix current1Extrap0, CouplingMethod method=CouplingMethod::DELAY)
std::shared_ptr< SimNode< VarType > > Ptr
Definition SimNode.h:32
static Ptr GND
Definition SimNode.h:35
SimSignalComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
std::vector< Ptr > List
Definition Task.h:28
std::shared_ptr< TopologicalNode > Ptr
Logger::Log mSLog
Component logger.
static std::shared_ptr< Resistor > make(Args &&...args)
Definition PtrFactory.h:19
struct dps_magma_data data
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
CouplingMethod
int Int
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
unsigned int UInt
Definition Definitions.h:60