Skip to content

Commit

Permalink
added comp. ResIndSeries SP domain
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Moraga <[email protected]>
  • Loading branch information
martinmoraga committed Mar 6, 2023
1 parent 5c1ac32 commit 10a641d
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 29 deletions.
1 change: 1 addition & 0 deletions dpsim-models/include/dpsim-models/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dpsim-models/Config.h>

#include <dpsim-models/SP/SP_Ph1_AvVoltageSourceInverterDQ.h>
#include <dpsim-models/SP/SP_Ph1_ResIndSeries.h>
#include <dpsim-models/SP/SP_Ph1_RXLine.h>
#include <dpsim-models/SP/SP_Ph1_VoltageSourceInverter.h>
#include <dpsim-models/SP/SP_Ph1_PiLine.h>
Expand Down
2 changes: 1 addition & 1 deletion dpsim-models/include/dpsim-models/DP/DP_Ph1_ResIndSeries.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Ph1 {
/// Initializes state variables considering the number of frequencies
void initialize(Matrix frequencies);
/// Initializes states from power flow data
void initializeFromNodesAndTerminals(Real frequency);
void initializeFromNodesAndTerminals(Real frequency) override;
/// Initializes auxiliar variables
void initVars(Real timeStep);

Expand Down
61 changes: 61 additions & 0 deletions dpsim-models/include/dpsim-models/SP/SP_Ph1_ResIndSeries.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/

#pragma once

#include <dpsim-models/MNASimPowerComp.h>
#include <dpsim-models/Definitions.h>

namespace CPS {
namespace SP {
namespace Ph1 {
/// Static phasor ResIndSeries model (only implemented for dynamic simulations!)
class ResIndSeries :
public MNASimPowerComp<Complex>,
public SharedFactory<ResIndSeries> {
private:
/// Impedance
Complex mImpedance;
public:
/// Inductance [H]
const Attribute<Real>::Ptr mInductance;
///Resistance [ohm]
const Attribute<Real>::Ptr mResistance;
/// Defines UID, name and logging level
ResIndSeries(String uid, String name, Logger::Level logLevel = Logger::Level::off);
/// Defines name and logging level
ResIndSeries(String name, Logger::Level logLevel = Logger::Level::off)
: ResIndSeries(name, name, logLevel) { }

SimPowerComp<Complex>::Ptr clone(String name);

// #### General ####
/// Sets model specific parameters
void setParameters(Real resistance, Real inductance);
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency) override;


// #### MNA section ####
/// Initializes MNA specific variables
void mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) override;
/// Stamps system matrix
void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) override;
/// Update interface voltage from MNA system result
void mnaCompUpdateVoltage(const Matrix& leftVector) override;
/// Update interface current from MNA system result
void mnaCompUpdateCurrent(const Matrix& leftVector) override;
/// MNA pre and post step operations
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) override;
/// add MNA pre and post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

};
}
}
}
14 changes: 7 additions & 7 deletions dpsim-models/include/dpsim-models/SP/SP_Ph1_Resistor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Ph1 {

// #### General ####
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency);
void initializeFromNodesAndTerminals(Real frequency) override;

// #### Powerflow section ####
/// Set base voltage
Expand All @@ -67,17 +67,17 @@ namespace Ph1 {

// #### MNA section ####
///
void mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector);
void mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) override;
/// Stamps system matrix
void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix);
void mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) override;
/// Update interface voltage from MNA system result
void mnaCompUpdateVoltage(const Matrix& leftVector);
void mnaCompUpdateVoltage(const Matrix& leftVector) override;
/// Update interface current from MNA system result
void mnaCompUpdateCurrent(const Matrix& leftVector);
void mnaCompUpdateCurrent(const Matrix& leftVector) override;
/// MNA pre and post step operations
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector);
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) override;
/// add MNA pre and post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector);
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;

// #### MNA Tear Section ####
void mnaTearApplyMatrixStamp(Matrix& tearMatrix);
Expand Down
1 change: 1 addition & 0 deletions dpsim-models/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ list(APPEND MODELS_SOURCES
SP/SP_Ph1_Capacitor.cpp
SP/SP_Ph1_Inductor.cpp
SP/SP_Ph1_Resistor.cpp
SP/SP_Ph1_ResIndSeries.cpp
SP/SP_Ph1_AvVoltageSourceInverterDQ.cpp
SP/SP_Ph1_RXLine.cpp
SP/SP_Ph1_VoltageSourceInverter.cpp
Expand Down
2 changes: 1 addition & 1 deletion dpsim-models/src/DP/DP_Ph1_ResIndSeries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void DP::Ph1::ResIndSeries::initVars(Real timeStep) {
mPrevCurrFac(freq,0) = { preCurrFracReal, preCurrFracImag };

mEquivCurrent(freq,0) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mPrevCurrFac(freq,0) * (**mIntfCurrent)(0,freq);
(**mIntfCurrent)(0,freq) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mEquivCurrent(freq,0);
//(**mIntfCurrent)(0,freq) = mEquivCond(freq,0) * (**mIntfVoltage)(0,freq) + mEquivCurrent(freq,0);
}
}

Expand Down
4 changes: 3 additions & 1 deletion dpsim-models/src/SP/SP_Ph1_Capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void SP::Ph1::Capacitor::initializeFromNodesAndTerminals(Real frequency) {
Logger::phasorToString((**mIntfCurrent)(0, 0)),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
mSLog->flush();
}

// #### MNA section ####
Expand All @@ -62,6 +63,7 @@ void SP::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, Attribute<
"\n--- MNA initialization finished ---",
Logger::phasorToString((**mIntfVoltage)(0, 0)),
Logger::phasorToString((**mIntfCurrent)(0, 0)));
mSLog->flush();
}

void SP::Ph1::Capacitor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
Expand Down Expand Up @@ -105,7 +107,7 @@ void SP::Ph1::Capacitor::mnaCompPostStep(Real time, Int timeStepCount, Attribute

void SP::Ph1::Capacitor::mnaCompUpdateVoltage(const Matrix& leftVector) {
// v1 - v0
**mIntfVoltage = Matrix::Zero(3, 1);
**mIntfVoltage = Matrix::Zero(1, 1);
if (terminalNotGrounded(1)) {
(**mIntfVoltage)(0, 0) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
}
Expand Down
5 changes: 4 additions & 1 deletion dpsim-models/src/SP/SP_Ph1_Inductor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void SP::Ph1::Inductor::initializeFromNodesAndTerminals(Real frequency) {
Logger::phasorToString((**mIntfCurrent)(0, 0)),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
mSLog->flush();
}

// #### MNA section ####
Expand All @@ -59,6 +60,7 @@ void SP::Ph1::Inductor::mnaCompInitialize(Real omega, Real timeStep, Attribute<M
"\n--- MNA initialization finished ---",
Logger::phasorToString((**mIntfVoltage)(0, 0)),
Logger::phasorToString((**mIntfCurrent)(0, 0)));
mSLog->flush();
}

void SP::Ph1::Inductor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
Expand Down Expand Up @@ -87,6 +89,7 @@ void SP::Ph1::Inductor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
mSLog->info("Add {:e}+j{:e} to system at ({:d},{:d})",
-mSusceptance.real(), -mSusceptance.imag(), matrixNodeIndex(1), matrixNodeIndex(0));
}
mSLog->flush();
}

void SP::Ph1::Inductor::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) {
Expand All @@ -102,7 +105,7 @@ void SP::Ph1::Inductor::mnaCompPostStep(Real time, Int timeStepCount, Attribute<

void SP::Ph1::Inductor::mnaCompUpdateVoltage(const Matrix& leftVector) {
// v1 - v0
**mIntfVoltage = Matrix::Zero(3, 1);
**mIntfVoltage = Matrix::Zero(1, 1);
if (terminalNotGrounded(1)) {
(**mIntfVoltage)(0, 0) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
}
Expand Down
126 changes: 126 additions & 0 deletions dpsim-models/src/SP/SP_Ph1_ResIndSeries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/

#include <dpsim-models/SP/SP_Ph1_ResIndSeries.h>

using namespace CPS;


SP::Ph1::ResIndSeries::ResIndSeries(String uid, String name, Logger::Level logLevel)
: MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
mInductance(mAttributes->create<Real>("L")),
mResistance(mAttributes->create<Real>("R")) {
setTerminalNumber(2);
**mIntfVoltage = MatrixComp::Zero(1, 1);
**mIntfCurrent = MatrixComp::Zero(1, 1);
}

SimPowerComp<Complex>::Ptr SP::Ph1::ResIndSeries::clone(String name) {
auto copy = ResIndSeries::make(name, mLogLevel);
copy->setParameters(**mResistance, **mInductance);
return copy;
}

void SP::Ph1::ResIndSeries::setParameters(Real resistance, Real inductance) {
**mResistance = resistance;
**mInductance = inductance;
}

void SP::Ph1::ResIndSeries::initializeFromNodesAndTerminals(Real frequency) {

Real omega = 2. * PI * frequency;
mImpedance = { **mResistance, omega * **mInductance };
(**mIntfVoltage)(0,0) = initialSingleVoltage(1) - initialSingleVoltage(0);
(**mIntfCurrent)(0,0) = (**mIntfVoltage)(0,0) / mImpedance;

mSLog->info(
"\n--- Initialization from powerflow ---"
"\nVoltage across: {:s}"
"\nCurrent: {:s}"
"\nTerminal 0 voltage: {:s}"
"\nTerminal 1 voltage: {:s}"
"\n--- Initialization from powerflow finished ---",
Logger::phasorToString((**mIntfVoltage)(0,0)),
Logger::phasorToString((**mIntfCurrent)(0,0)),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
mSLog->flush();
}

// #### MNA section ####

void SP::Ph1::ResIndSeries::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
updateMatrixNodeIndices();
**mRightVector = Matrix::Zero(0, 0);

mSLog->info(
"\n--- MNA initialization ---"
"\nInitial voltage {:s}"
"\nInitial current {:s}"
"\n--- MNA initialization finished ---",
Logger::phasorToString((**mIntfVoltage)(0, 0)),
Logger::phasorToString((**mIntfCurrent)(0, 0)));
}

void SP::Ph1::ResIndSeries::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
Complex conductance = 1. / mImpedance;

for (UInt freq = 0; freq < mNumFreqs; freq++) {
// Set diagonal entries
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(0), conductance, mNumFreqs, freq);
if (terminalNotGrounded(1))
// Set off diagonal entries
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(1), conductance, mNumFreqs, freq);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), -conductance, mNumFreqs, freq);
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), matrixNodeIndex(0), -conductance, mNumFreqs, freq);
}

mSLog->info("-- Stamp frequency {:d} ---", freq);
if (terminalNotGrounded(0))
mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(conductance), matrixNodeIndex(0), matrixNodeIndex(0));
if (terminalNotGrounded(1))
mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(conductance), matrixNodeIndex(1), matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(0), matrixNodeIndex(1));
mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(1), matrixNodeIndex(0));
}
}
}

void SP::Ph1::ResIndSeries::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) {
attributeDependencies.push_back(leftVector);
modifiedAttributes.push_back(mIntfVoltage);
modifiedAttributes.push_back(mIntfCurrent);
}

void SP::Ph1::ResIndSeries::mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
this->mnaUpdateVoltage(**leftVector);
this->mnaUpdateCurrent(**leftVector);
}

void SP::Ph1::ResIndSeries::mnaCompUpdateVoltage(const Matrix& leftVector) {
// v1 - v0
for (UInt freq = 0; freq < mNumFreqs; freq++) {
(**mIntfVoltage)(0,freq) = 0;
if (terminalNotGrounded(1))
(**mIntfVoltage)(0,freq) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(1), mNumFreqs, freq);
if (terminalNotGrounded(0))
(**mIntfVoltage)(0,freq) = (**mIntfVoltage)(0,freq) - Math::complexFromVectorElement(leftVector, matrixNodeIndex(0), mNumFreqs, freq);

SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}", Logger::phasorToString((**mIntfVoltage)(0,freq)));
}
}

void SP::Ph1::ResIndSeries::mnaCompUpdateCurrent(const Matrix& leftVector) {
for (UInt freq = 0; freq < mNumFreqs; freq++) {
(**mIntfCurrent)(0,freq) = (**mIntfVoltage)(0,freq) / mImpedance;
SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}", Logger::phasorToString((**mIntfCurrent)(0,freq)));
}
}
3 changes: 3 additions & 0 deletions dpsim-models/src/SP/SP_Ph1_Resistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void SP::Ph1::Resistor::initializeFromNodesAndTerminals(Real frequency) {
Logger::phasorToString((**mIntfCurrent)(0, 0)),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
mSLog->flush();
}

// #### Powerflow section ####
Expand Down Expand Up @@ -95,6 +96,7 @@ void SP::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep, Attribute<M
"\n--- MNA initialization finished ---",
Logger::phasorToString((**mIntfVoltage)(0, 0)),
Logger::phasorToString((**mIntfCurrent)(0, 0)));
mSLog->flush();
}

void SP::Ph1::Resistor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
Expand Down Expand Up @@ -122,6 +124,7 @@ void SP::Ph1::Resistor::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
mSLog->info("Add {:s} to system at ({:d},{:d})", Logger::complexToString(-conductance), matrixNodeIndex(1), matrixNodeIndex(0));
}
}
mSLog->flush();
}

void SP::Ph1::Resistor::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) {
Expand Down
2 changes: 2 additions & 0 deletions dpsim-models/src/SP/SP_Ph1_VoltageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void SP::Ph1::VoltageSource::initializeFromNodesAndTerminals(Real frequency) {
Logger::phasorToString(mSrcSig->getSignal()),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
mSLog->flush();
}

// #### MNA functions ####
Expand Down Expand Up @@ -103,6 +104,7 @@ void SP::Ph1::VoltageSource::mnaCompInitialize(Real omega, Real timeStep, Attrib
"\n--- MNA initialization finished ---",
Logger::phasorToString((**mIntfVoltage)(0,0)),
Logger::phasorToString((**mIntfCurrent)(0,0)));
mSLog->flush();
}

void SP::Ph1::VoltageSource::mnaCompApplySystemMatrixStamp(Matrix& systemMatrix) {
Expand Down
6 changes: 6 additions & 0 deletions dpsim/src/pybind/SPComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ void addSPPh1Components(py::module_ mSPPh1) {
.def("connect", &CPS::SP::Ph1::Inductor::connect)
.def_property("L", createAttributeGetter<CPS::Real>("L"), createAttributeSetter<CPS::Real>("L"));

py::class_<CPS::SP::Ph1::ResIndSeries, std::shared_ptr<CPS::SP::Ph1::ResIndSeries>, CPS::SimPowerComp<CPS::Complex>>(mSPPh1, "ResInductor", py::multiple_inheritance())
.def(py::init<std::string>())
.def(py::init<std::string, CPS::Logger::Level>())
.def("set_parameters", &CPS::SP::Ph1::ResIndSeries::setParameters, "R"_a, "L"_a)
.def("connect", &CPS::SP::Ph1::ResIndSeries::connect);

py::class_<CPS::SP::Ph1::NetworkInjection, std::shared_ptr<CPS::SP::Ph1::NetworkInjection>, CPS::SimPowerComp<CPS::Complex>>(mSPPh1, "NetworkInjection", py::multiple_inheritance())
.def(py::init<std::string, CPS::Logger::Level>(), "name"_a, "loglevel"_a = CPS::Logger::Level::off)
.def("set_parameters", py::overload_cast<CPS::Real>(&CPS::SP::Ph1::NetworkInjection::setParameters), "voltage_set_point"_a)
Expand Down
Loading

0 comments on commit 10a641d

Please sign in to comment.