DPsim
DP_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/DP/DP_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  setVirtualNodeNumber(0);
19  setTerminalNumber(1);
20 
21  SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
22  **mIntfVoltage = MatrixComp::Zero(1, 1);
23  **mIntfCurrent = MatrixComp::Zero(1, 1);
24 
25  // Create electrical sub components
26  mSubVoltageSource =
27  std::make_shared<DP::Ph1::VoltageSource>(**mName + "_vs", mLogLevel);
28  addMNASubComponent(mSubVoltageSource,
29  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
30  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
31 
32  SPDLOG_LOGGER_INFO(mSLog, "Electrical subcomponents: ");
33  for (auto subcomp : mSubComponents)
34  SPDLOG_LOGGER_INFO(mSLog, "- {}", subcomp->name());
35 
36  mSubVoltageSource->mVoltageRef->setReference(mVoltageRef);
37  mSubVoltageSource->mSrcFreq->setReference(mSrcFreq);
38 }
39 
41  auto copy = NetworkInjection::make(name, mLogLevel);
42  copy->setParameters(**mVoltageRef);
43  return copy;
44 }
45 
47  Real srcFreq) {
48  mParametersSet = true;
49 
50  mSubVoltageSource->setParameters(voltageRef, srcFreq);
51 
52  SPDLOG_LOGGER_INFO(mSLog,
53  "\nVoltage Ref={:s} [V]"
54  "\nFrequency={:s} [Hz]",
55  Logger::phasorToString(voltageRef),
56  Logger::realToString(srcFreq));
57 }
58 
59 void DP::Ph1::NetworkInjection::setParameters(Complex initialPhasor,
60  Real freqStart, Real rocof,
61  Real timeStart, Real duration,
62  bool smoothRamp) {
63  mParametersSet = true;
64 
65  mSubVoltageSource->setParameters(initialPhasor, freqStart, rocof, timeStart,
66  duration, smoothRamp);
67 
68  SPDLOG_LOGGER_INFO(
69  mSLog,
70  "\nVoltage Ref={:s} [V]"
71  "\nInitial frequency={:s} [Hz]"
72  "\nRamp ROCOF={:s} [Hz/s]"
73  "\nRamp duration={:s} [s]"
74  "\nRamp nadir={:s} [Hz]",
75  Logger::phasorToString(initialPhasor), Logger::realToString(freqStart),
76  Logger::realToString(rocof), Logger::realToString(duration),
77  Logger::realToString(freqStart + rocof * duration));
78 }
79 
80 void DP::Ph1::NetworkInjection::setParameters(Complex initialPhasor,
81  Real modulationFrequency,
82  Real modulationAmplitude,
83  Real baseFrequency /*= 0.0*/,
84  bool zigzag /*= false*/) {
85  mParametersSet = true;
86 
87  mSubVoltageSource->setParameters(initialPhasor, modulationFrequency,
88  modulationAmplitude, baseFrequency, zigzag);
89 
90  SPDLOG_LOGGER_INFO(mSLog,
91  "\nVoltage Ref={:s} [V]"
92  "\nFrequency={:s} [Hz]",
93  Logger::phasorToString(initialPhasor),
94  Logger::realToString(baseFrequency));
95 }
96 
98  Real frequency) {
99  // Connect electrical subcomponents
100  mSubVoltageSource->connect({SimNode::GND, node(0)});
101 
102  // Initialize electrical subcomponents
103  for (auto subcomp : mSubComponents) {
104  subcomp->initialize(mFrequencies);
105  subcomp->initializeFromNodesAndTerminals(frequency);
106  }
107 }
108 
109 // #### MNA functions ####
110 
112  Matrix &rightVector) {
113  SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
114  Logger::matrixToString(rightVector));
115 }
116 
118  AttributeBase::List &prevStepDependencies,
119  AttributeBase::List &attributeDependencies,
120  AttributeBase::List &modifiedAttributes) {
121  // add pre-step dependencies of component itself
122  prevStepDependencies.push_back(mIntfCurrent);
123  prevStepDependencies.push_back(mIntfVoltage);
124  modifiedAttributes.push_back(mRightVector);
125 }
126 
127 void DP::Ph1::NetworkInjection::mnaParentPreStep(Real time, Int timeStepCount) {
128  // pre-step of component itself
129  mnaCompApplyRightSideVectorStamp(**mRightVector);
130 }
131 
133  AttributeBase::List &prevStepDependencies,
134  AttributeBase::List &attributeDependencies,
135  AttributeBase::List &modifiedAttributes,
136  Attribute<Matrix>::Ptr &leftVector) {
137  // add post-step dependencies of component itself
138  attributeDependencies.push_back(leftVector);
139  modifiedAttributes.push_back(mIntfVoltage);
140  modifiedAttributes.push_back(mIntfCurrent);
141 }
142 
144  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
145  // post-step of component itself
146  mnaCompUpdateCurrent(**leftVector);
147  mnaCompUpdateVoltage(**leftVector);
148 }
149 
150 void DP::Ph1::NetworkInjection::mnaCompUpdateVoltage(const Matrix &leftVector) {
151  **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
152 }
153 
154 void DP::Ph1::NetworkInjection::mnaCompUpdateCurrent(const Matrix &leftVector) {
155  **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
156 }
157 
158 void DP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
159  const double dstate_dt[],
160  double resid[],
161  std::vector<int> &off) {
162  /* new state vector definintion:
163  state[0]=node0_voltage
164  state[1]=node1_voltage
165  ....
166  state[n]=noden_voltage
167  state[n+1]=component0_voltage
168  state[n+2]=component0_inductance (not yet implemented)
169  ...
170  state[m-1]=componentm_voltage
171  state[m]=componentm_inductance
172  */
173 
174  int Pos1 = matrixNodeIndex(0);
175  int Pos2 = matrixNodeIndex(1);
176  int c_offset = off[0] + off[1]; //current offset for component
177  int n_offset_1 =
178  c_offset + Pos1 + 1; // current offset for first nodal equation
179  int n_offset_2 =
180  c_offset + Pos2 + 1; // current offset for second nodal equation
181  resid[c_offset] = (state[Pos2] - state[Pos1]) -
182  state[c_offset]; // Voltage equation for Resistor
183  //resid[++c_offset] = ; //TODO : add inductance equation
184  resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
185  resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
186  off[1] += 1;
187 }
188 
190  (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
191  return (**mSubVoltageSource->mIntfVoltage)(0, 0);
192 }
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.
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.
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 mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const Attribute< String >::Ptr mName
Human readable name.
String type()
Get component type (cross-platform)
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.