DPsim
Loading...
Searching...
No Matches
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
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 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
40SimPowerComp<Complex>::Ptr DP::Ph1::NetworkInjection::clone(String name) {
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
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
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
103// #### MNA functions ####
104
106 Matrix &rightVector) {
107 SPDLOG_LOGGER_DEBUG(mSLog, "Right Side Vector: {:s}",
108 Logger::matrixToString(rightVector));
109}
110
112 AttributeBase::List &prevStepDependencies,
113 AttributeBase::List &attributeDependencies,
114 AttributeBase::List &modifiedAttributes) {
115 // add pre-step dependencies of component itself
116 prevStepDependencies.push_back(mIntfCurrent);
117 prevStepDependencies.push_back(mIntfVoltage);
118 modifiedAttributes.push_back(mRightVector);
119}
120
121void DP::Ph1::NetworkInjection::mnaParentPreStep(Real time, Int timeStepCount) {
122 // pre-step of component itself
124}
125
127 AttributeBase::List &prevStepDependencies,
128 AttributeBase::List &attributeDependencies,
129 AttributeBase::List &modifiedAttributes,
130 Attribute<Matrix>::Ptr &leftVector) {
131 // add post-step dependencies of component itself
132 attributeDependencies.push_back(leftVector);
133 modifiedAttributes.push_back(mIntfVoltage);
134 modifiedAttributes.push_back(mIntfCurrent);
135}
136
138 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
139 // post-step of component itself
140 mnaCompUpdateCurrent(**leftVector);
141 mnaCompUpdateVoltage(**leftVector);
142}
143
145 **mIntfVoltage = **mSubVoltageSource->mIntfVoltage;
146}
147
149 **mIntfCurrent = **mSubVoltageSource->mIntfCurrent;
150}
151
152void DP::Ph1::NetworkInjection::daeResidual(double ttime, const double state[],
153 const double dstate_dt[],
154 double resid[],
155 std::vector<int> &off) {
156 /* New state vector definintion:
157 * state[0]=node0_voltage
158 * state[1]=node1_voltage
159 * ....
160 * state[n]=noden_voltage
161 * state[n+1]=component0_voltage
162 * state[n+2]=component0_inductance (not yet implemented)
163 * ...
164 * state[m-1]=componentm_voltage
165 * state[m]=componentm_inductance
166 */
167
168 int Pos1 = matrixNodeIndex(0);
169 int Pos2 = matrixNodeIndex(1);
170 int c_offset = off[0] + off[1]; //current offset for component
171 int n_offset_1 =
172 c_offset + Pos1 + 1; // current offset for first nodal equation
173 int n_offset_2 =
174 c_offset + Pos2 + 1; // current offset for second nodal equation
175 resid[c_offset] = (state[Pos2] - state[Pos1]) -
176 state[c_offset]; // Voltage equation for Resistor
177 //resid[++c_offset] = ; //TODO : add inductance equation
178 resid[n_offset_1] += (**mIntfCurrent)(0, 0).real();
179 resid[n_offset_2] += (**mIntfCurrent)(0, 0).real();
180 off[1] += 1;
181}
182
184 (**mIntfVoltage)(0, 0) = (**mSubVoltageSource->mIntfVoltage)(0, 0);
185 return (**mSubVoltageSource->mIntfVoltage)(0, 0);
186}
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)
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 mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
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
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.