4#include "dpsim-models/Definitions.h"
5#include "dpsim-models/TopologicalNode.h"
6#include <dpsim-models/Signal/DecouplingIdealTransformer_EMT_Ph3.h>
9using namespace CPS::EMT::Ph3;
10using namespace CPS::Signal;
12DecouplingIdealTransformer_EMT_Ph3::DecouplingIdealTransformer_EMT_Ph3(
13 String name, Logger::Level 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))) {
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);
28 mSrcVoltage = mVoltageSrc->mVoltageRef;
29 mSrcCurrent = mCurrentSrc->mCurrentRef;
32void DecouplingIdealTransformer_EMT_Ph3::setParameters(
33 SimNode<Real>::Ptr node1, SimNode<Real>::Ptr node2, Real delay,
34 Matrix voltageSrcIntfCurr, Matrix current1Extrap0, CouplingMethod method) {
38 mVirtualNode = SimNode<Real>::make(name() +
"_virtual", PhaseType::ABC);
41 mCouplingMethod = method;
43 if (mCouplingMethod == CouplingMethod::EXTRAPOLATION_LINEAR) {
44 mExtrapolationDegree = 1;
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});
59void DecouplingIdealTransformer_EMT_Ph3::initialize(Real omega, Real timeStep) {
65 mBufSize =
static_cast<UInt
>(ceil(mDelay / timeStep));
66 mAlpha = 1 - (mBufSize - mDelay / timeStep);
68 SPDLOG_LOGGER_INFO(
mSLog,
"bufsize {} alpha {}", mBufSize, mAlpha);
70 mVoltageSrc->setIntfCurrent(mVoltageSrcIntfCurr);
71 MatrixComp cur1 = mVoltageSrc->mIntfCurrent->get();
72 MatrixComp volt2 = mNode2->initialVoltage();
74 mVirtualNode->setInitialVoltage(mNode1->initialVoltage() -
75 mInternalSeriesResistance * cur1);
77 SPDLOG_LOGGER_INFO(
mSLog,
"initial current: i_1 {}", cur1);
78 SPDLOG_LOGGER_INFO(
mSLog,
"initial voltage: v_2 {}", volt2);
80 **mSrcVoltageRef = volt2.real();
81 **mSrcCurrentRef = cur1.real();
82 mVoltageSrc->setParameters(**mSrcVoltageRef);
83 mCurrentSrc->setParameters(**mSrcCurrentRef);
85 Matrix mSourceCurrentIntfVoltage = volt2.real();
86 mCurrentSrc->setIntfVoltage(mSourceCurrentIntfVoltage);
88 mVoltageSrc->setIntfVoltage(mSourceCurrentIntfVoltage);
89 mVoltageSrc->setIntfCurrent(mVoltageSrcIntfCurr);
91 **mSourceVoltageIntfVoltage = volt2.real();
92 **mSourceVoltageIntfCurrent = mVoltageSrc->intfCurrent();
95 mCur1 = cur1.real().transpose().replicate(mBufSize, 1);
96 mVol2 = volt2.real().transpose().replicate(mBufSize, 1);
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));
103 mCur1Extrap = Matrix(mExtrapolationDegree + 1, 3);
104 mCur1Extrap.row(0) = mCurrent1Extrap0.real().transpose();
105 if (mExtrapolationDegree > 0) {
106 mCur1Extrap.row(1) = mVoltageSrcIntfCurr.transpose();
108 mVol2Extrap = volt2.real().transpose().replicate(mExtrapolationDegree + 1, 1);
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();
117Matrix DecouplingIdealTransformer_EMT_Ph3::extrapolate(Matrix &data) {
118 if (mCouplingMethod == CouplingMethod::EXTRAPOLATION_LINEAR) {
119 Matrix c1 = data.row(mMacroBufIdx);
120 Matrix c2 = mMacroBufIdx == mExtrapolationDegree
122 : data.row(mMacroBufIdx + 1);
124 (mDelay * (mBufIdx + 1)) /
static_cast<float>(mBufSize);
125 Real tEval = mDelay + delayFraction;
126 return (((c2 - c1) / mDelay) * tEval + c1).transpose();
128 return (data.row(mMacroBufIdx)).transpose();
132void DecouplingIdealTransformer_EMT_Ph3::step(Real time, Int timeStepCount) {
134 if (mCouplingMethod == CouplingMethod::DELAY) {
135 volt1 = interpolate(mVol2);
136 cur2 = interpolate(mCur1);
138 volt1 = extrapolate(mVol2Extrap);
139 cur2 = extrapolate(mCur1Extrap);
143 **mSrcVoltageRef = volt1;
144 **mSrcCurrentRef = cur2;
145 **mSourceVoltageIntfVoltage = mVoltageSrc->intfVoltage();
146 **mSourceVoltageIntfCurrent = mVoltageSrc->intfCurrent();
148 mSrcVoltage->set(**mSrcVoltageRef);
149 mSrcCurrent->set(**mSrcCurrentRef);
152void DecouplingIdealTransformer_EMT_Ph3::PreStep::execute(Real time,
154 mITM.step(time, timeStepCount);
157void DecouplingIdealTransformer_EMT_Ph3::postStep() {
159 mCur1.row(mBufIdx) = -mVoltageSrc->intfCurrent().transpose();
160 mVol2.row(mBufIdx) = mCurrentSrc->intfVoltage().transpose();
163 if (mBufIdx == mBufSize) {
164 mCur1Extrap.row(mMacroBufIdx) = mCur1.row(mBufIdx - 1);
165 mVol2Extrap.row(mMacroBufIdx) = mVol2.row(mBufIdx - 1);
167 if (mMacroBufIdx == mExtrapolationDegree + 1) {
174void DecouplingIdealTransformer_EMT_Ph3::PostStep::execute(Real time,
179Task::List DecouplingIdealTransformer_EMT_Ph3::getTasks() {
181 {std::make_shared<PreStep>(*
this), std::make_shared<PostStep>(*
this)});
184IdentifiedObject::List DecouplingIdealTransformer_EMT_Ph3::getComponents() {
185 return IdentifiedObject::List({mRes1, mRes2, mVoltageSrc, mCurrentSrc});
188TopologicalNode::Ptr DecouplingIdealTransformer_EMT_Ph3::getVirtualNode() {
Logger::Log mSLog
Component logger.