Skip to content

Commit

Permalink
Reuse code for MNA matrix stamp operations (#297)
Browse files Browse the repository at this point in the history
### Summary
This PR introduces the utility class `MNAStampUtils` for MNA matrix
stamping operations. It standardizes and reuses the stamping logic for
1-phase and 3-phase EMT, DP and SP R, L, C components. The motivation is
to make the codebase more maintainable and reduce redundancy.

### Additional Context
- **Issue Reference**: These changes were discussed in issue
[#288](#288) - Reuse
conductance stamp code.
- **Future Plans**: There are plans for future PRs to extend this
approach to other components deriving from the `MNASimPowerComp` class.
This might involve adding more variations of stamping functions to
`MNAStampUtils` to accommodate different parameter lists or optimized
logic for specific cases, such as components with a single terminal or
symmetric phase impedances.
  • Loading branch information
m-mirz authored Jun 8, 2024
2 parents da76744 + 6d2b3c6 commit 37442dc
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 1,261 deletions.
1 change: 1 addition & 0 deletions dpsim-models/include/dpsim-models/MNASimPowerComp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <dpsim-models/MNAStampUtils.h>
#include <dpsim-models/SimPowerComp.h>
#include <dpsim-models/Solver/MNAInterface.h>

Expand Down
76 changes: 76 additions & 0 deletions dpsim-models/include/dpsim-models/MNAStampUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <dpsim-models/Logger.h>
#include <dpsim-models/MathUtils.h>

namespace CPS {
class MNAStampUtils {
public:
static void stampConductance(Real conductance, SparseMatrixRow &mat,
UInt node1Index, UInt node2Index,
Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded,
const Logger::Log &mSLog);

static void stampAdmittance(Complex admittance, SparseMatrixRow &mat,
UInt node1Index, UInt node2Index,
Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded,
const Logger::Log &mSLog, Int maxFreq = 1,
Int freqIdx = 0);

static void stampConductanceMatrix(const Matrix &conductanceMat,
SparseMatrixRow &mat, UInt node1Index,
UInt node2Index,
Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded,
const Logger::Log &mSLog);

static void stampAdmittanceMatrix(
const MatrixComp &admittanceMat, SparseMatrixRow &mat, UInt node1Index,
UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
const Logger::Log &mSLog, Int maxFreq = 1, Int freqIdx = 0);

private:
template <typename T>
static void stampValue(T value, SparseMatrixRow &mat, UInt node1Index,
UInt node2Index, Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded, Int maxFreq, Int freqIdx,
const Logger::Log &mSLog);

template <typename T>
static void stampMatrix(const MatrixVar<T> &matrix, SparseMatrixRow &mat,
UInt node1Index, UInt node2Index,
Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded, Int maxFreq, Int freqIdx,
const Logger::Log &mSLog);

template <typename T>
static void stampValueNoConditions(T value, SparseMatrixRow &mat,
UInt node1Index, UInt node2Index,
Int maxFreq, Int freqIdx,
const Logger::Log &mSLog);

template <typename T>
static void stampValueOnDiagonalNoConditions(T value, SparseMatrixRow &mat,
UInt nodeIndex, Int maxFreq,
Int freqIdx,
const Logger::Log &mSLog);

template <typename T>
static void stampValueOffDiagonalNoConditions(T value, SparseMatrixRow &mat,
UInt node1Index,
UInt node2Index, Int maxFreq,
Int freqIdx,
const Logger::Log &mSLog);

static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
Matrix::Index column, Real value, Int maxFreq,
Int freqIdx, const Logger::Log &mSLog);

static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
Matrix::Index column, Complex value,
Int maxFreq, Int freqIdx,
const Logger::Log &mSLog);
};
} // namespace CPS
1 change: 1 addition & 0 deletions dpsim-models/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(dpsim-models STATIC
Logger.cpp
MathUtils.cpp
MNAStampUtils.cpp
Attribute.cpp
TopologicalNode.cpp
TopologicalTerminal.cpp
Expand Down
78 changes: 8 additions & 70 deletions dpsim-models/src/DP/DP_Ph1_Capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,81 +123,19 @@ void DP::Ph1::Capacitor::mnaCompInitializeHarm(
void DP::Ph1::Capacitor::mnaCompApplySystemMatrixStamp(
SparseMatrixRow &systemMatrix) {
for (UInt freq = 0; freq < mNumFreqs; freq++) {
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(0), mEquivCond(freq, 0),
mNumFreqs, freq);
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(1), mEquivCond(freq, 0),
mNumFreqs, freq);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), -mEquivCond(freq, 0),
mNumFreqs, freq);
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(0), -mEquivCond(freq, 0),
mNumFreqs, freq);
}

SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freq);
if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
mEquivCond(freq, 0).real(), mEquivCond(freq, 0).imag(),
matrixNodeIndex(0), matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
mEquivCond(freq, 0).real(), mEquivCond(freq, 0).imag(),
matrixNodeIndex(1), matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
-mEquivCond(freq, 0).real(),
-mEquivCond(freq, 0).imag(), matrixNodeIndex(0),
matrixNodeIndex(1));
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
-mEquivCond(freq, 0).real(),
-mEquivCond(freq, 0).imag(), matrixNodeIndex(1),
matrixNodeIndex(0));
}
MNAStampUtils::stampAdmittance(
mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0), terminalNotGrounded(1),
mSLog, mNumFreqs, freq);
}
}

void DP::Ph1::Capacitor::mnaCompApplySystemMatrixStampHarm(
SparseMatrixRow &systemMatrix, Int freqIdx) {
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(0), mEquivCond(freqIdx, 0));
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(1), mEquivCond(freqIdx, 0));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), -mEquivCond(freqIdx, 0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(0), -mEquivCond(freqIdx, 0));
}

SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freqIdx);
if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
mEquivCond(freqIdx, 0).real(),
mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
mEquivCond(freqIdx, 0).real(),
mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
-mEquivCond(freqIdx, 0).real(),
-mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
matrixNodeIndex(1));
SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})",
-mEquivCond(freqIdx, 0).real(),
-mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
matrixNodeIndex(0));
}
MNAStampUtils::stampAdmittance(mEquivCond(freqIdx, 0), systemMatrix,
matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1),
mSLog);
}

void DP::Ph1::Capacitor::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) {
Expand Down
76 changes: 8 additions & 68 deletions dpsim-models/src/DP/DP_Ph1_Inductor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,79 +112,19 @@ void DP::Ph1::Inductor::mnaCompInitializeHarm(
void DP::Ph1::Inductor::mnaCompApplySystemMatrixStamp(
SparseMatrixRow &systemMatrix) {
for (UInt freq = 0; freq < mNumFreqs; freq++) {
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(0), mEquivCond(freq, 0),
mNumFreqs, freq);
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(1), mEquivCond(freq, 0),
mNumFreqs, freq);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), -mEquivCond(freq, 0),
mNumFreqs, freq);
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(0), -mEquivCond(freq, 0),
mNumFreqs, freq);
}

SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freq);
if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(mEquivCond(freq, 0)),
matrixNodeIndex(0), matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(mEquivCond(freq, 0)),
matrixNodeIndex(1), matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-mEquivCond(freq, 0)),
matrixNodeIndex(0), matrixNodeIndex(1));
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-mEquivCond(freq, 0)),
matrixNodeIndex(1), matrixNodeIndex(0));
}
MNAStampUtils::stampAdmittance(
mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0), terminalNotGrounded(1),
mSLog, mNumFreqs, freq);
}
}

void DP::Ph1::Inductor::mnaCompApplySystemMatrixStampHarm(
SparseMatrixRow &systemMatrix, Int freqIdx) {
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(0), mEquivCond(freqIdx, 0));
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(1), mEquivCond(freqIdx, 0));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), -mEquivCond(freqIdx, 0));
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(0), -mEquivCond(freqIdx, 0));
}

SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freqIdx);
if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
mEquivCond(freqIdx, 0).real(),
mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
mEquivCond(freqIdx, 0).real(),
mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
-mEquivCond(freqIdx, 0).real(),
-mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
matrixNodeIndex(1));
SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
-mEquivCond(freqIdx, 0).real(),
-mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
matrixNodeIndex(0));
}
MNAStampUtils::stampAdmittance(mEquivCond(freqIdx, 0), systemMatrix,
matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1),
mSLog);
}

void DP::Ph1::Inductor::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) {
Expand Down
74 changes: 6 additions & 68 deletions dpsim-models/src/DP/DP_Ph1_Resistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,81 +77,19 @@ void DP::Ph1::Resistor::mnaCompApplySystemMatrixStamp(
Complex conductance = Complex(1. / **mResistance, 0);

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);
}

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

void DP::Ph1::Resistor::mnaCompApplySystemMatrixStampHarm(
SparseMatrixRow &systemMatrix, Int freqIdx) {
Complex conductance = Complex(1. / **mResistance, 0);
// Set diagonal entries
if (terminalNotGrounded(0))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(0), conductance);
if (terminalNotGrounded(1))
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(1), conductance);
// Set off diagonal entries
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), -conductance);
Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
matrixNodeIndex(0), -conductance);
}

SPDLOG_LOGGER_INFO(mSLog, "-- Stamp for frequency {:f} ---",
mFrequencies(freqIdx, 0));
if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance), matrixNodeIndex(0),
matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance), matrixNodeIndex(1),
matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-conductance),
matrixNodeIndex(0), matrixNodeIndex(1));
SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-conductance),
matrixNodeIndex(1), matrixNodeIndex(0));
}
MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0),
terminalNotGrounded(1), mSLog);
}

void DP::Ph1::Resistor::mnaCompAddPostStepDependencies(
Expand Down
Loading

0 comments on commit 37442dc

Please sign in to comment.