DPsim
Loading...
Searching...
No Matches
DecouplingLineEMT.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
11using namespace CPS;
12using namespace CPS::EMT::Ph1;
13using namespace CPS::Signal;
14
16 : SimSignalComp(name, name, logLevel),
17 mStates(mAttributes->create<Matrix>("states")),
18 mSrcCur1Ref(mAttributes->create<Real>("i_src1")),
19 mSrcCur2Ref(mAttributes->create<Real>("i_src2")) {
20
21 mRes1 = Resistor::make(name + "_r1", logLevel);
22 mRes2 = Resistor::make(name + "_r2", logLevel);
23 mSrc1 = CurrentSource::make(name + "_i1", logLevel);
24 mSrc2 = CurrentSource::make(name + "_i2", logLevel);
25
26 mSrcCur1 = mSrc1->mCurrentRef;
27 mSrcCur2 = mSrc2->mCurrentRef;
28}
29
31 SimNode<Real>::Ptr node2, Real resistance,
32 Real inductance, Real capacitance) {
33
34 mResistance = resistance;
35 mInductance = inductance;
36 mCapacitance = capacitance;
37 mNode1 = node1;
38 mNode2 = node2;
39
40 mSurgeImpedance = sqrt(inductance / capacitance);
41 mDelay = sqrt(inductance * capacitance);
42 SPDLOG_LOGGER_INFO(mSLog, "surge impedance: {}", mSurgeImpedance);
43 SPDLOG_LOGGER_INFO(mSLog, "delay: {}", mDelay);
44
45 mRes1->setParameters(mSurgeImpedance + mResistance / 4);
46 mRes1->connect({node1, SimNode<Real>::GND});
47 mRes2->setParameters(mSurgeImpedance + mResistance / 4);
48 mRes2->connect({node2, SimNode<Real>::GND});
49 mSrc1->setParameters(0);
50 mSrc1->connect({node1, SimNode<Real>::GND});
51 mSrc2->setParameters(0);
52 mSrc2->connect({node2, SimNode<Real>::GND});
53}
54
56 if (mDelay < timeStep)
57 throw SystemError("Timestep too large for decoupling");
58
59 mBufSize = static_cast<UInt>(ceil(mDelay / timeStep));
60 mAlpha = 1 - (mBufSize - mDelay / timeStep);
61 SPDLOG_LOGGER_INFO(mSLog, "bufsize {} alpha {}", mBufSize, mAlpha);
62
63 const Real resistanceQuarter = mResistance / 4.;
64 const Real sourceImpedance = mSurgeImpedance + resistanceQuarter;
65 const Real denominator = sourceImpedance * sourceImpedance;
67 mInitialSourceImpedanceInverse = 1. / sourceImpedance;
69 mResistanceCurrentCoefficient = -resistanceQuarter / denominator;
70
71 // Initialization based on static PI-line model
72 Complex volt1 = mNode1->initialSingleVoltage();
73 Complex volt2 = mNode2->initialSingleVoltage();
74 Complex initAdmittance = 1. / Complex(mResistance, omega * mInductance) +
75 Complex(0, omega * mCapacitance / 2);
76 Complex cur1 = volt1 * initAdmittance -
77 volt2 / Complex(mResistance, omega * mInductance);
78 Complex cur2 = volt2 * initAdmittance -
79 volt1 / Complex(mResistance, omega * mInductance);
80 SPDLOG_LOGGER_INFO(mSLog, "initial voltages: v_k {} v_m {}", volt1, volt2);
81 SPDLOG_LOGGER_INFO(mSLog, "initial currents: i_km {} i_mk {}", cur1, cur2);
82
83 // Resize ring buffers and initialize
84 mVolt1.resize(mBufSize, volt1.real());
85 mVolt2.resize(mBufSize, volt2.real());
86 mCur1.resize(mBufSize, cur1.real());
87 mCur2.resize(mBufSize, cur2.real());
88}
89
91 // linear interpolation of the nearest values
92 Real c1 = data[mBufIdx];
93 Real c2 = mBufIdx == mBufSize - 1 ? data[0] : data[mBufIdx + 1];
94 return mAlpha * c1 + (1 - mAlpha) * c2;
95}
96
97void DecouplingLineEMT::step(Real time, Int timeStepCount) {
98 Real volt1 = interpolate(mVolt1);
99 Real volt2 = interpolate(mVolt2);
100 Real cur1 = interpolate(mCur1);
101 Real cur2 = interpolate(mCur2);
102 if (timeStepCount == 0) {
103 // initialization
106 } else {
107 // Update currents
109 (volt2 + mSurgeMinusResistanceQuarter * cur2) +
111 (volt1 + mSurgeMinusResistanceQuarter * cur1);
113 (volt1 + mSurgeMinusResistanceQuarter * cur1) +
115 (volt2 + mSurgeMinusResistanceQuarter * cur2);
116 }
117 mSrcCur1->set(**mSrcCur1Ref);
118 mSrcCur2->set(**mSrcCur2Ref);
119}
120
122 mLine.step(time, timeStepCount);
123}
124
126 // Update ringbuffers with new values
127 mVolt1[mBufIdx] = -mRes1->intfVoltage()(0, 0);
128 mVolt2[mBufIdx] = -mRes2->intfVoltage()(0, 0);
129 mCur1[mBufIdx] = -mRes1->intfCurrent()(0, 0) + mSrcCur1->get().real();
130 mCur2[mBufIdx] = -mRes2->intfCurrent()(0, 0) + mSrcCur2->get().real();
131
132 mBufIdx++;
133 if (mBufIdx == mBufSize)
134 mBufIdx = 0;
135}
136
138 mLine.postStep();
139}
140
142 return Task::List(
143 {std::make_shared<PreStep>(*this), std::make_shared<PostStep>(*this)});
144}
145
std::vector< Ptr > List
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
void initialize(Real omega, Real timeStep)
DecouplingLineEMT(String name, Logger::Level logLevel=Logger::Level::info)
Real interpolate(std::vector< Real > &data)
void setParameters(SimNode< Real >::Ptr node1, SimNode< Real >::Ptr node2, Real resistance, Real inductance, Real capacitance)
std::shared_ptr< EMT::SimNode > mNode2
Attribute< Complex >::Ptr mSrcCur2
std::shared_ptr< EMT::Ph1::Resistor > mRes2
IdentifiedObject::List getLineComponents()
std::shared_ptr< EMT::Ph1::Resistor > mRes1
const Attribute< Real >::Ptr mSrcCur1Ref
std::shared_ptr< EMT::Ph1::CurrentSource > mSrc2
Attribute< Complex >::Ptr mSrcCur1
void step(Real time, Int timeStepCount)
const Attribute< Real >::Ptr mSrcCur2Ref
std::shared_ptr< EMT::Ph1::CurrentSource > mSrc1
std::shared_ptr< EMT::SimNode > mNode1
const Attribute< Matrix >::Ptr mStates
FIXME: workaround for dependency analysis as long as the states aren't attributes.
std::shared_ptr< SimNode< VarType > > Ptr
Definition SimNode.h:34
static Ptr GND
Definition SimNode.h:37
SimSignalComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
std::vector< Ptr > List
Definition Task.h:28
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: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
unsigned int UInt
Definition Definitions.h:58