DPsim
EMT_Ph3_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/EMT/EMT_Ph3_NetworkInjection.h>
10 
11 using namespace CPS;
12 
14  Logger::Level logLevel)
15  : CompositePowerComp<Real>(uid, name, true, true, logLevel),
16  mVoltageRef(mAttributes->createDynamic<MatrixComp>("V_ref")),
17  mSrcFreq(mAttributes->createDynamic<Real>("f_src")) {
18  mPhaseType = PhaseType::ABC;
19  setVirtualNodeNumber(0);
20  setTerminalNumber(1);
21  **mIntfVoltage = Matrix::Zero(3, 1);
22  **mIntfCurrent = Matrix::Zero(3, 1);
23 
24  SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
25 
26  // Create electrical sub components
27  mSubVoltageSource =
28  std::make_shared<EMT::Ph3::VoltageSource>(**mName + "_vs", mLogLevel);
29  addMNASubComponent(mSubVoltageSource,
30  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
31  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
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 
46 void EMT::Ph3::NetworkInjection::setParameters(MatrixComp voltageRef,
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::matrixCompToString(voltageRef),
56  Logger::realToString(srcFreq));
57 }
58 
59 void EMT::Ph3::NetworkInjection::setParameters(MatrixComp voltageRef,
60  Real freqStart, Real rocof,
61  Real timeStart, Real duration,
62  bool smoothRamp) {
63  mParametersSet = true;
64 
65  mSubVoltageSource->setParameters(voltageRef, 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::matrixCompToString(voltageRef), Logger::realToString(freqStart),
76  Logger::realToString(rocof), Logger::realToString(duration),
77  Logger::realToString(freqStart + rocof * duration));
78 }
79 
80 void EMT::Ph3::NetworkInjection::setParameters(MatrixComp voltageRef,
81  Real modulationFrequency,
82  Real modulationAmplitude,
83  Real baseFrequency /*= 0.0*/,
84  bool zigzag /*= false*/) {
85  mParametersSet = true;
86 
87  mSubVoltageSource->setParameters(voltageRef, modulationFrequency,
88  modulationAmplitude, baseFrequency, zigzag);
89 
90  SPDLOG_LOGGER_INFO(mSLog,
91  "\nVoltage Ref={:s} [V]"
92  "\nFrequency={:s} [Hz]",
93  Logger::matrixCompToString(voltageRef),
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 ####
111  Matrix &rightVector) {
112  SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
113  Logger::matrixToString(rightVector));
114 }
115 
117  AttributeBase::List &prevStepDependencies,
118  AttributeBase::List &attributeDependencies,
119  AttributeBase::List &modifiedAttributes) {
120  prevStepDependencies.push_back(mIntfCurrent);
121  prevStepDependencies.push_back(mIntfVoltage);
122  modifiedAttributes.push_back(mRightVector);
123 }
124 
126  Int timeStepCount) {
127  mnaCompApplyRightSideVectorStamp(**mRightVector);
128 }
129 
131  AttributeBase::List &prevStepDependencies,
132  AttributeBase::List &attributeDependencies,
133  AttributeBase::List &modifiedAttributes,
134  Attribute<Matrix>::Ptr &leftVector) {
135  attributeDependencies.push_back(leftVector);
136  modifiedAttributes.push_back(mIntfVoltage);
137  modifiedAttributes.push_back(mIntfCurrent);
138 }
139 
141  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
142  mnaCompUpdateCurrent(**leftVector);
143  mnaCompUpdateVoltage(**leftVector);
144 }
145 
147  const Matrix &leftVector) {
148  **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
149 }
150 
152  const Matrix &leftVector) {
153  **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
154 }
Base class for composite power components.
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Add a new subcomponent implementing MNA methods.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void setParameters(MatrixComp voltageRef, Real srcFreq=50.0)
Setter for reference voltage parameters.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates voltage across component.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Returns current through the component.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
NetworkInjection(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
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< Real > > > mSubComponents
Definition: SimPowerComp.h:33
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.