DPsim
Loading...
Searching...
No Matches
SP_Ph1_NetworkInjection.cpp
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
9#include <dpsim-models/SP/SP_Ph1_NetworkInjection.h>
10
11using namespace CPS;
12
14 Logger::Level logLevel)
15 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
16 mVoltageRef(mAttributes->createDynamic<Complex>("V_ref")),
17 mSrcFreq(mAttributes->createDynamic<Real>("f_src")),
18 mVoltageSetPoint(mAttributes->create<Real>("V_set")),
19 mVoltageSetPointPerUnit(mAttributes->create<Real>("V_set_pu", 1.0)),
20 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
21 mReactivePowerInjection(mAttributes->create<Real>("q_inj")) {
22
23 SPDLOG_LOGGER_INFO(mSLog, "Create {} of type {}", **mName, this->type());
24 mSLog->flush();
25 **mIntfVoltage = MatrixComp::Zero(1, 1);
26 **mIntfCurrent = MatrixComp::Zero(1, 1);
27 setVirtualNodeNumber(0);
28 setTerminalNumber(1);
29
30 // Create electrical sub components
31 mSubVoltageSource =
32 std::make_shared<SP::Ph1::VoltageSource>(**mName + "_vs", mLogLevel);
33 addMNASubComponent(mSubVoltageSource,
34 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
35 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
36 SPDLOG_LOGGER_INFO(mSLog, "Electrical subcomponents: ");
37 for (auto subcomp : mSubComponents)
38 SPDLOG_LOGGER_INFO(mSLog, "- {}", subcomp->name());
39
40 // MNA attributes
41 mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
42 mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
43}
44
45// #### Powerflow section ####
46
47void SP::Ph1::NetworkInjection::setParameters(Real voltageSetPoint) {
48 **mVoltageSetPoint = voltageSetPoint;
49
50 SPDLOG_LOGGER_INFO(mSLog, "Voltage Set-Point ={} [V]", **mVoltageSetPoint);
51 mSLog->flush();
52
53 mParametersSet = true;
54}
55
57 Real freqStart, Real rocof,
58 Real timeStart, Real duration,
59 bool smoothRamp) {
60 mParametersSet = true;
61
62 mSubVoltageSource->setParameters(initialPhasor, freqStart, rocof, timeStart,
63 duration, smoothRamp);
64
65 SPDLOG_LOGGER_INFO(
66 mSLog,
67 "\nVoltage Ref={:s} [V]"
68 "\nInitial frequency={:s} [Hz]"
69 "\nRamp ROCOF={:s} [Hz/s]"
70 "\nRamp duration={:s} [s]"
71 "\nRamp nadir={:s} [Hz]",
72 Logger::phasorToString(initialPhasor), Logger::realToString(freqStart),
73 Logger::realToString(rocof), Logger::realToString(duration),
74 Logger::realToString(freqStart + rocof * duration));
75}
76
78 Real modulationFrequency,
79 Real modulationAmplitude,
80 Real baseFrequency /*= 0.0*/,
81 bool zigzag /*= false*/) {
82 mParametersSet = true;
83
84 mSubVoltageSource->setParameters(initialPhasor, modulationFrequency,
85 modulationAmplitude, baseFrequency, zigzag);
86
87 SPDLOG_LOGGER_INFO(mSLog,
88 "\nVoltage Ref={:s} [V]"
89 "\nFrequency={:s} [Hz]",
90 Logger::phasorToString(initialPhasor),
91 Logger::realToString(baseFrequency));
92}
93
94Real SP::Ph1::NetworkInjection::getBaseVoltage() const { return mBaseVoltage; }
95
97 mBaseVoltage = baseVoltage;
98}
99
101 Real baseApparentPower, Real baseOmega) {
102 SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
103 **mName);
104 SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V]", mBaseVoltage);
105
106 **mVoltageSetPointPerUnit = **mVoltageSetPoint / mBaseVoltage;
107
108 SPDLOG_LOGGER_INFO(mSLog, "Voltage Set-Point ={} [pu]",
110 mSLog->flush();
111}
112
114 PowerflowBusType powerflowBusType) {
115 mPowerflowBusType = powerflowBusType;
116}
117
119 **mActivePowerInjection = powerInj.real();
120 **mReactivePowerInjection = powerInj.imag();
121}
122
123// #### MNA Section ####
124
126 Real srcFreq) {
127 mParametersSet = true;
128
129 mSubVoltageSource->setParameters(voltageRef, srcFreq);
130
131 SPDLOG_LOGGER_INFO(mSLog,
132 "\nVoltage Ref={:s} [V]"
133 "\nFrequency={:s} [Hz]",
134 Logger::phasorToString(voltageRef),
135 Logger::realToString(srcFreq));
136}
137
138SimPowerComp<Complex>::Ptr SP::Ph1::NetworkInjection::clone(String name) {
139 auto copy = NetworkInjection::make(name, mLogLevel);
140 copy->setParameters(**mVoltageRef);
141 return copy;
142}
143
145 Real frequency) {
146 // Connect electrical subcomponents
147 mSubVoltageSource->connect({SimNode::GND, node(0)});
148}
149
150// #### MNA functions ####
152 Matrix &rightVector) {
153 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
154 Logger::matrixToString(rightVector));
155}
156
158 AttributeBase::List &prevStepDependencies,
159 AttributeBase::List &attributeDependencies,
160 AttributeBase::List &modifiedAttributes) {
161 prevStepDependencies.push_back(mIntfCurrent);
162 prevStepDependencies.push_back(mIntfVoltage);
163 modifiedAttributes.push_back(mRightVector);
164}
165
169
171 AttributeBase::List &prevStepDependencies,
172 AttributeBase::List &attributeDependencies,
173 AttributeBase::List &modifiedAttributes,
174 Attribute<Matrix>::Ptr &leftVector) {
175 attributeDependencies.push_back(leftVector);
176 modifiedAttributes.push_back(mIntfVoltage);
177 modifiedAttributes.push_back(mIntfCurrent);
178}
179
181 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
182 mnaCompUpdateCurrent(**leftVector);
183 mnaCompUpdateVoltage(**leftVector);
184}
185
187 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
188}
189
191 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
192}
193
194void SP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
195 const double dstate_dt[],
196 double resid[],
197 std::vector<int> &off) {
198 /* New state vector definintion:
199 * state[0]=node0_voltage
200 * state[1]=node1_voltage
201 * ....
202 * state[n]=noden_voltage
203 * state[n+1]=component0_voltage
204 * state[n+2]=component0_inductance (not yet implemented)
205 * ...
206 * state[m-1]=componentm_voltage
207 * state[m]=componentm_inductance
208 */
209
210 int Pos1 = matrixNodeIndex(0);
211 int Pos2 = matrixNodeIndex(1);
212 int c_offset = off[0] + off[1]; //current offset for component
213 int n_offset_1 =
214 c_offset + Pos1 + 1; // current offset for first nodal equation
215 int n_offset_2 =
216 c_offset + Pos2 + 1; // current offset for second nodal equation
217 resid[c_offset] = (state[Pos2] - state[Pos1]) -
218 state[c_offset]; // Voltage equation for Resistor
219 //resid[++c_offset] = ; //TODO : add inductance equation
220 resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
221 resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
222 off[1] += 1;
223}
224
226 (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
227 return (**mSubVoltageSource->mIntfVoltage)(0, 0);
228}
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)
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.
Attribute< Matrix >::Ptr mRightVector
PowerflowBusType mPowerflowBusType
Define the type of bus the component is modelled by.
const Attribute< Real >::Ptr mActivePowerInjection
Active Power Injection [W].
void setParameters(Real vSetPointPerUnit)
Set parameters relevant for PF solver.
Complex daeInitialize() override
Voltage Getter.
void daeResidual(double ttime, const double state[], const double dstate_dt[], double resid[], std::vector< int > &off) override
Residual function for DAE Solver.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void calculatePerUnitParameters(Real baseApparentPower, Real baseOmega)
Calculates component's parameters in specified per-unit system.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
const Attribute< Real >::Ptr mVoltageSetPoint
Voltage set point [V].
NetworkInjection(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void setBaseVoltage(Real baseVoltage)
Set base voltage.
const Attribute< Real >::Ptr mVoltageSetPointPerUnit
Voltage set point [pu].
void modifyPowerFlowBusType(PowerflowBusType powerflowBusType) override
Modify powerflow bus type.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Returns current through the component.
void updatePowerInjection(Complex powerInj)
Update power injection.
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
const Attribute< Real >::Ptr mReactivePowerInjection
Reactive Power Injection [Var].
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimNode< Complex >::Ptr node(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
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.