DPsim
Loading...
Searching...
No Matches
DecouplingIdealTransformer_EMT_Ph3.cpp
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
4#include "dpsim-models/Definitions.h"
5#include "dpsim-models/TopologicalNode.h"
6#include <dpsim-models/Signal/DecouplingIdealTransformer_EMT_Ph3.h>
7
8using namespace CPS;
9using namespace CPS::EMT::Ph3;
10using namespace CPS::Signal;
11
12DecouplingIdealTransformer_EMT_Ph3::DecouplingIdealTransformer_EMT_Ph3(
13 String name, Logger::Level logLevel)
14 : SimSignalComp(name, name, logLevel),
15 mStates(mAttributes->create<Matrix>("states")),
16 mSourceVoltageIntfVoltage(
17 mAttributes->create<Matrix>("v_intf", Matrix::Zero(3, 1))),
18 mSourceVoltageIntfCurrent(
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);
25 mVoltageSrc = ControlledVoltageSource::make(name + "_v", logLevel);
26 mCurrentSrc = ControlledCurrentSource::make(name + "_i", logLevel);
27
28 mSrcVoltage = mVoltageSrc->mVoltageRef;
29 mSrcCurrent = mCurrentSrc->mCurrentRef;
30}
31
32void DecouplingIdealTransformer_EMT_Ph3::setParameters(
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;
38 mVirtualNode = SimNode<Real>::make(name() + "_virtual", PhaseType::ABC);
39
40 mDelay = delay;
41 mCouplingMethod = method;
42
43 if (mCouplingMethod == CouplingMethod::EXTRAPOLATION_LINEAR) {
44 mExtrapolationDegree = 1;
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;
54 mVoltageSrc->connect({mVirtualNode, SimNode<Real>::GND});
55 mCurrentSrc->setParameters(Matrix::Zero(3, 1));
56 mCurrentSrc->connect({node2, SimNode<Real>::GND});
57}
58
59void DecouplingIdealTransformer_EMT_Ph3::initialize(Real omega, Real timeStep) {
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() -
75 mInternalSeriesResistance * cur1);
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
103 mCur1Extrap = Matrix(mExtrapolationDegree + 1, 3);
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
111Matrix DecouplingIdealTransformer_EMT_Ph3::interpolate(Matrix &data) {
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
117Matrix DecouplingIdealTransformer_EMT_Ph3::extrapolate(Matrix &data) {
118 if (mCouplingMethod == CouplingMethod::EXTRAPOLATION_LINEAR) {
119 Matrix c1 = data.row(mMacroBufIdx);
120 Matrix c2 = mMacroBufIdx == mExtrapolationDegree
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
132void DecouplingIdealTransformer_EMT_Ph3::step(Real time, Int timeStepCount) {
133 Matrix volt1, cur2;
134 if (mCouplingMethod == CouplingMethod::DELAY) {
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
148 mSrcVoltage->set(**mSrcVoltageRef);
149 mSrcCurrent->set(**mSrcCurrentRef);
150}
151
152void DecouplingIdealTransformer_EMT_Ph3::PreStep::execute(Real time,
153 Int timeStepCount) {
154 mITM.step(time, timeStepCount);
155}
156
157void DecouplingIdealTransformer_EMT_Ph3::postStep() {
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++;
167 if (mMacroBufIdx == mExtrapolationDegree + 1) {
168 mMacroBufIdx = 0;
169 }
170 mBufIdx = 0;
171 }
172}
173
174void DecouplingIdealTransformer_EMT_Ph3::PostStep::execute(Real time,
175 Int timeStepCount) {
176 mITM.postStep();
177}
178
179Task::List DecouplingIdealTransformer_EMT_Ph3::getTasks() {
180 return Task::List(
181 {std::make_shared<PreStep>(*this), std::make_shared<PostStep>(*this)});
182}
183
184IdentifiedObject::List DecouplingIdealTransformer_EMT_Ph3::getComponents() {
185 return IdentifiedObject::List({mRes1, mRes2, mVoltageSrc, mCurrentSrc});
186}
187
188TopologicalNode::Ptr DecouplingIdealTransformer_EMT_Ph3::getVirtualNode() {
189 return mVirtualNode;
190}
Logger::Log mSLog
Component logger.