DPsim
Loading...
Searching...
No Matches
DP_Ph1_NetworkInjection.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;
12
16
18 Bool useSubcomponents,
19 Logger::Level logLevel)
20 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
21 mUseSubcomponents(useSubcomponents),
22 mVoltageRef(mAttributes->createDynamic<Complex>("V_ref")),
23 mSrcFreq(mAttributes->createDynamic<Real>("f_src")) {
24 setVirtualNodeNumber(mUseSubcomponents ? 0 : 1);
26
27 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
28 **mIntfVoltage = MatrixComp::Zero(1, 1);
29 **mIntfCurrent = MatrixComp::Zero(1, 1);
30
31 // Create electrical sub components
32 if (mUseSubcomponents) {
33 mSubVoltageSource =
34 std::make_shared<DP::Ph1::VoltageSource>(**mName + "_vs", mLogLevel);
35 addMNASubComponent(mSubVoltageSource,
38 mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
39 mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
40 }
41
42 SPDLOG_LOGGER_INFO(mSLog, "Electrical subcomponents: ");
43 for (auto subcomp : mSubComponents)
44 SPDLOG_LOGGER_INFO(mSLog, "- {}", subcomp->name());
45}
46
52
54 Real srcFreq) {
55 mParametersSet = true;
56 if (mUseSubcomponents)
57 mSubVoltageSource->setParameters(voltageRef, srcFreq);
58 else {
59 mVoltageRef->set(voltageRef);
60 mSrcFreq->set(srcFreq);
61 auto signal = Signal::SineWaveGenerator::make(**mName + "_sw");
62 signal->mVoltageRef->setReference(mVoltageRef);
63 signal->mFreq->setReference(mSrcFreq);
64 signal->setParameters(voltageRef, srcFreq);
65 mDirectSignal = signal;
66 }
67
68 SPDLOG_LOGGER_INFO(mSLog,
69 "\nVoltage Ref={:s} [V]"
70 "\nFrequency={:s} [Hz]",
71 Logger::phasorToString(voltageRef),
72 Logger::realToString(srcFreq));
73}
74
76 Real freqStart, Real rocof,
77 Real timeStart, Real duration,
78 bool smoothRamp) {
79 mParametersSet = true;
80 if (mUseSubcomponents)
81 mSubVoltageSource->setParameters(initialPhasor, freqStart, rocof, timeStart,
82 duration, smoothRamp);
83 else {
84 mVoltageRef->set(initialPhasor);
85 mSrcFreq->set(freqStart);
86 auto signal = Signal::FrequencyRampGenerator::make(**mName + "_fr");
87 signal->mFreq->setReference(mSrcFreq);
88 signal->setParameters(initialPhasor, freqStart, rocof, timeStart, duration,
89 smoothRamp);
90 mDirectSignal = signal;
91 }
92
93 SPDLOG_LOGGER_INFO(
94 mSLog,
95 "\nVoltage Ref={:s} [V]"
96 "\nInitial frequency={:s} [Hz]"
97 "\nRamp ROCOF={:s} [Hz/s]"
98 "\nRamp duration={:s} [s]"
99 "\nRamp nadir={:s} [Hz]",
100 Logger::phasorToString(initialPhasor), Logger::realToString(freqStart),
102 Logger::realToString(freqStart + rocof * duration));
103}
104
106 Real modulationFrequency,
107 Real modulationAmplitude,
108 Real baseFrequency /*= 0.0*/,
109 bool zigzag /*= false*/) {
110 mParametersSet = true;
111 if (mUseSubcomponents)
112 mSubVoltageSource->setParameters(initialPhasor, modulationFrequency,
113 modulationAmplitude, baseFrequency,
114 zigzag);
115 else {
116 mVoltageRef->set(initialPhasor);
117 mSrcFreq->set(baseFrequency);
118 auto signal = Signal::CosineFMGenerator::make(**mName + "_fm");
119 signal->mFreq->setReference(mSrcFreq);
120 signal->setParameters(initialPhasor, modulationFrequency,
121 modulationAmplitude, baseFrequency, zigzag);
122 mDirectSignal = signal;
123 }
124
125 SPDLOG_LOGGER_INFO(mSLog,
126 "\nVoltage Ref={:s} [V]"
127 "\nFrequency={:s} [Hz]",
128 Logger::phasorToString(initialPhasor),
129 Logger::realToString(baseFrequency));
130}
131
133 Real frequency) {
134 // Connect electrical subcomponents
135 if (mUseSubcomponents) {
136 mSubVoltageSource->connect({SimNode::GND, node(0)});
137 return;
138 }
139 if (**mVoltageRef == Complex(0., 0.))
141 if (mDirectSignal == nullptr) {
142 auto signal = Signal::SineWaveGenerator::make(**mName);
143 signal->mVoltageRef->setReference(mVoltageRef);
144 signal->mFreq->setReference(mSrcFreq);
145 signal->setParameters(**mVoltageRef);
146 mDirectSignal = signal;
147 }
148 // The legacy voltage-source child initializes its public interface voltage
149 // to zero and publishes the generated waveform on the first pre-step.
150 mDirectVoltage = 0.;
151 (**mIntfVoltage)(0, 0) = mDirectVoltage;
152}
153
154// #### MNA functions ####
155
157 SparseMatrixRow &systemMatrix) {
158 if (mUseSubcomponents)
159 return;
160 const UInt sourceCurrent = virtualNode(0)->matrixNodeIndex();
161 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
162 Math::setMatrixElement(systemMatrix, sourceCurrent, matrixNodeIndex(0),
163 Complex(1., 0.), mNumFreqs, freq);
164 Math::setMatrixElement(systemMatrix, matrixNodeIndex(0), sourceCurrent,
165 Complex(1., 0.), mNumFreqs, freq);
166 }
167}
168
170 Matrix &rightVector) {
171 if (!mUseSubcomponents)
173 mDirectVoltage, mNumFreqs);
174 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
175 Logger::matrixToString(rightVector));
176}
177
179 AttributeBase::List &prevStepDependencies,
180 AttributeBase::List &attributeDependencies,
181 AttributeBase::List &modifiedAttributes) {
182 // add pre-step dependencies of component itself
183 if (!mUseSubcomponents) {
184 attributeDependencies.push_back(mVoltageRef);
185 attributeDependencies.push_back(mSrcFreq);
186 }
187 prevStepDependencies.push_back(mIntfCurrent);
188 prevStepDependencies.push_back(mIntfVoltage);
189 modifiedAttributes.push_back(mRightVector);
190}
191
193 if (!mUseSubcomponents) {
194 mDirectSignal->step(time);
195 mDirectVoltage = mDirectSignal->getSignal();
196 (**mIntfVoltage)(0, 0) = mDirectVoltage;
197 }
198 // pre-step of component itself
200}
201
203 AttributeBase::List &prevStepDependencies,
204 AttributeBase::List &attributeDependencies,
205 AttributeBase::List &modifiedAttributes,
206 Attribute<Matrix>::Ptr &leftVector) {
207 // add post-step dependencies of component itself
208 attributeDependencies.push_back(leftVector);
209 modifiedAttributes.push_back(mIntfVoltage);
210 modifiedAttributes.push_back(mIntfCurrent);
211}
212
214 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
215 // post-step of component itself
216 mnaCompUpdateCurrent(**leftVector);
217 mnaCompUpdateVoltage(**leftVector);
218}
219
221 if (mUseSubcomponents)
222 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
223}
224
226 if (mUseSubcomponents) {
227 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
228 return;
229 }
230 const UInt sourceCurrent = virtualNode(0)->matrixNodeIndex();
231 for (UInt freq = 0; freq < mNumFreqs; ++freq)
233 leftVector, sourceCurrent, mNumFreqs, freq);
234}
235
236void DP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
237 const double dstate_dt[],
238 double resid[],
239 std::vector<int> &off) {
240 /* New state vector definintion:
241 * state[0]=node0_voltage
242 * state[1]=node1_voltage
243 * ....
244 * state[n]=noden_voltage
245 * state[n+1]=component0_voltage
246 * state[n+2]=component0_inductance (not yet implemented)
247 * ...
248 * state[m-1]=componentm_voltage
249 * state[m]=componentm_inductance
250 */
251
252 int Pos1 = matrixNodeIndex(0);
253 int Pos2 = matrixNodeIndex(1);
254 int c_offset = off[0] + off[1]; //current offset for component
255 int n_offset_1 =
256 c_offset + Pos1 + 1; // current offset for first nodal equation
257 int n_offset_2 =
258 c_offset + Pos2 + 1; // current offset for second nodal equation
259 resid[c_offset] = (state[Pos2] - state[Pos1]) -
260 state[c_offset]; // Voltage equation for Resistor
261 //resid[++c_offset] = ; //TODO : add inductance equation
262 resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
263 resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
264 off[1] += 1;
265}
266
268 if (mUseSubcomponents)
269 (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
270 return (**mIntfVoltage)(0, 0);
271}
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)
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 daeResidual(double ttime, const double state[], const double dstate_dt[], double resid[], std::vector< int > &off) override
Residual function for DAE Solver.
Complex daeInitialize() override
Voltage Getter.
NetworkInjection(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Returns current through the component.
void setParameters(Complex voltageRef, Real srcFreq=0.0)
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
More General setter for voltage source parameters.
const Attribute< Complex >::Ptr mVoltageRef
const Attribute< Real >::Ptr mSrcFreq
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
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 matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String realToString(const Real &num)
Definition Logger.cpp:69
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
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)
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
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
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
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
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