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 // Initialize electrical subcomponents
150 for (auto subcomp : mSubComponents) {
151 subcomp->initialize(mFrequencies);
152 subcomp->initializeFromNodesAndTerminals(frequency);
153 }
154}
155
156// #### MNA functions ####
158 Matrix &rightVector) {
159 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
160 Logger::matrixToString(rightVector));
161}
162
164 AttributeBase::List &prevStepDependencies,
165 AttributeBase::List &attributeDependencies,
166 AttributeBase::List &modifiedAttributes) {
167 prevStepDependencies.push_back(mIntfCurrent);
168 prevStepDependencies.push_back(mIntfVoltage);
169 modifiedAttributes.push_back(mRightVector);
170}
171
175
177 AttributeBase::List &prevStepDependencies,
178 AttributeBase::List &attributeDependencies,
179 AttributeBase::List &modifiedAttributes,
180 Attribute<Matrix>::Ptr &leftVector) {
181 attributeDependencies.push_back(leftVector);
182 modifiedAttributes.push_back(mIntfVoltage);
183 modifiedAttributes.push_back(mIntfCurrent);
184}
185
187 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
188 mnaCompUpdateCurrent(**leftVector);
189 mnaCompUpdateVoltage(**leftVector);
190}
191
193 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
194}
195
197 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
198}
199
200void SP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
201 const double dstate_dt[],
202 double resid[],
203 std::vector<int> &off) {
204 /* New state vector definintion:
205 * state[0]=node0_voltage
206 * state[1]=node1_voltage
207 * ....
208 * state[n]=noden_voltage
209 * state[n+1]=component0_voltage
210 * state[n+2]=component0_inductance (not yet implemented)
211 * ...
212 * state[m-1]=componentm_voltage
213 * state[m]=componentm_inductance
214 */
215
216 int Pos1 = matrixNodeIndex(0);
217 int Pos2 = matrixNodeIndex(1);
218 int c_offset = off[0] + off[1]; //current offset for component
219 int n_offset_1 =
220 c_offset + Pos1 + 1; // current offset for first nodal equation
221 int n_offset_2 =
222 c_offset + Pos2 + 1; // current offset for second nodal equation
223 resid[c_offset] = (state[Pos2] - state[Pos1]) -
224 state[c_offset]; // Voltage equation for Resistor
225 //resid[++c_offset] = ; //TODO : add inductance equation
226 resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
227 resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
228 off[1] += 1;
229}
230
232 (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
233 return (**mSubVoltageSource->mIntfVoltage)(0, 0);
234}
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 initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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].
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.