DPsim
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 
11 using 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 
47 void 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 
56 void SP::Ph1::NetworkInjection::setParameters(Complex initialPhasor,
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 
77 void SP::Ph1::NetworkInjection::setParameters(Complex initialPhasor,
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 
95  mBaseVoltage = baseVoltage;
96 }
97 
99  Real baseApparentPower, Real baseOmega) {
100  SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
101  **mName);
102  SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V]", mBaseVoltage);
103 
104  **mVoltageSetPointPerUnit = **mVoltageSetPoint / mBaseVoltage;
105 
106  SPDLOG_LOGGER_INFO(mSLog, "Voltage Set-Point ={} [pu]",
107  **mVoltageSetPointPerUnit);
108  mSLog->flush();
109 }
110 
112  PowerflowBusType powerflowBusType) {
113  mPowerflowBusType = powerflowBusType;
114 }
115 
117  **mActivePowerInjection = powerInj.real();
118  **mReactivePowerInjection = powerInj.imag();
119 }
120 
121 // #### MNA Section ####
122 
124  Real srcFreq) {
125  mParametersSet = true;
126 
127  mSubVoltageSource->setParameters(voltageRef, srcFreq);
128 
129  SPDLOG_LOGGER_INFO(mSLog,
130  "\nVoltage Ref={:s} [V]"
131  "\nFrequency={:s} [Hz]",
132  Logger::phasorToString(voltageRef),
133  Logger::realToString(srcFreq));
134 }
135 
137  auto copy = NetworkInjection::make(name, mLogLevel);
138  copy->setParameters(**mVoltageRef);
139  return copy;
140 }
141 
143  Real frequency) {
144  // Connect electrical subcomponents
145  mSubVoltageSource->connect({SimNode::GND, node(0)});
146 
147  // Initialize electrical subcomponents
148  for (auto subcomp : mSubComponents) {
149  subcomp->initialize(mFrequencies);
150  subcomp->initializeFromNodesAndTerminals(frequency);
151  }
152 }
153 
154 // #### MNA functions ####
156  Matrix &rightVector) {
157  SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
158  Logger::matrixToString(rightVector));
159 }
160 
162  AttributeBase::List &prevStepDependencies,
163  AttributeBase::List &attributeDependencies,
164  AttributeBase::List &modifiedAttributes) {
165  prevStepDependencies.push_back(mIntfCurrent);
166  prevStepDependencies.push_back(mIntfVoltage);
167  modifiedAttributes.push_back(mRightVector);
168 }
169 
170 void SP::Ph1::NetworkInjection::mnaParentPreStep(Real time, Int timeStepCount) {
171  mnaCompApplyRightSideVectorStamp(**mRightVector);
172 }
173 
175  AttributeBase::List &prevStepDependencies,
176  AttributeBase::List &attributeDependencies,
177  AttributeBase::List &modifiedAttributes,
178  Attribute<Matrix>::Ptr &leftVector) {
179  attributeDependencies.push_back(leftVector);
180  modifiedAttributes.push_back(mIntfVoltage);
181  modifiedAttributes.push_back(mIntfCurrent);
182 }
183 
185  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
186  mnaCompUpdateCurrent(**leftVector);
187  mnaCompUpdateVoltage(**leftVector);
188 }
189 
190 void SP::Ph1::NetworkInjection::mnaCompUpdateVoltage(const Matrix &leftVector) {
191  **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
192 }
193 
194 void SP::Ph1::NetworkInjection::mnaCompUpdateCurrent(const Matrix &leftVector) {
195  **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
196 }
197 
198 void SP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
199  const double dstate_dt[],
200  double resid[],
201  std::vector<int> &off) {
202  /* new state vector definintion:
203  state[0]=node0_voltage
204  state[1]=node1_voltage
205  ....
206  state[n]=noden_voltage
207  state[n+1]=component0_voltage
208  state[n+2]=component0_inductance (not yet implemented)
209  ...
210  state[m-1]=componentm_voltage
211  state[m]=componentm_inductance
212  */
213 
214  int Pos1 = matrixNodeIndex(0);
215  int Pos2 = matrixNodeIndex(1);
216  int c_offset = off[0] + off[1]; //current offset for component
217  int n_offset_1 =
218  c_offset + Pos1 + 1; // current offset for first nodal equation
219  int n_offset_2 =
220  c_offset + Pos2 + 1; // current offset for second nodal equation
221  resid[c_offset] = (state[Pos2] - state[Pos1]) -
222  state[c_offset]; // Voltage equation for Resistor
223  //resid[++c_offset] = ; //TODO : add inductance equation
224  resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
225  resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
226  off[1] += 1;
227 }
228 
230  (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
231  return (**mSubVoltageSource->mIntfVoltage)(0, 0);
232 }
Base class for composite power components.
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Add a new subcomponent implementing MNA methods.
const Attribute< String >::Ptr mName
Human readable name.
String type()
Get component type (cross-platform)
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.
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.
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.
Base class for all components that are transmitting power.
Definition: SimPowerComp.h:17
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
Definition: SimPowerComp.h:33
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.