From 12a4c6c5fd1b2672a4af5e50dbf30a22f50fe716 Mon Sep 17 00:00:00 2001 From: Georgii Tishenin Date: Wed, 12 Jun 2024 12:38:07 +0200 Subject: [PATCH] MNAStampUtils: Add functions for stamping value as a scalar matrix Signed-off-by: Georgii Tishenin --- .../include/dpsim-models/MNAStampUtils.h | 20 ++++++ dpsim-models/src/MNAStampUtils.cpp | 65 +++++++++++++++++-- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/dpsim-models/include/dpsim-models/MNAStampUtils.h b/dpsim-models/include/dpsim-models/MNAStampUtils.h index fea77ed126..1dca3b4130 100644 --- a/dpsim-models/include/dpsim-models/MNAStampUtils.h +++ b/dpsim-models/include/dpsim-models/MNAStampUtils.h @@ -31,6 +31,18 @@ class MNAStampUtils { UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq = 1, Int freqIdx = 0); + /// Stamps conductance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal to conductance). + static void stampConductanceAs3x3ScalarMatrix( + Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, + Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, + const Logger::Log &mSLog); + + /// Stamps admittance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal to admittance). + static void stampAdmittanceAs3x3ScalarMatrix( + Complex admittance, SparseMatrixRow &mat, UInt node1Index, + UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, + const Logger::Log &mSLog, Int maxFreq = 1, Int freqIdx = 0); + private: template static void stampValue(T value, SparseMatrixRow &mat, UInt node1Index, @@ -45,6 +57,14 @@ class MNAStampUtils { Bool isTerminal2NotGrounded, Int maxFreq, Int freqIdx, const Logger::Log &mSLog); + template + static void stampValueAsScalarMatrix(T value, UInt sizeOfScalarMatrix, + SparseMatrixRow &mat, UInt node1Index, + UInt node2Index, + Bool isTerminal1NotGrounded, + Bool isTerminal2NotGrounded, Int maxFreq, + Int freqIdx, const Logger::Log &mSLog); + template static void stampValueNoConditions(T value, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, diff --git a/dpsim-models/src/MNAStampUtils.cpp b/dpsim-models/src/MNAStampUtils.cpp index 4bb7b6bb0f..de3159f9ad 100644 --- a/dpsim-models/src/MNAStampUtils.cpp +++ b/dpsim-models/src/MNAStampUtils.cpp @@ -22,8 +22,7 @@ void MNAStampUtils::stampAdmittance(Complex admittance, SparseMatrixRow &mat, const Logger::Log &mSLog, Int maxFreq, Int freqIdx) { SPDLOG_LOGGER_DEBUG( - mSLog, "Start stamping admittance for frequency index {:d}...", - freqIdx); + mSLog, "Start stamping admittance for frequency index {:d}...", freqIdx); stampValue(admittance, mat, node1Index, node2Index, isTerminal1NotGrounded, isTerminal2NotGrounded, maxFreq, freqIdx, mSLog); @@ -50,8 +49,7 @@ void MNAStampUtils::stampAdmittanceMatrix( UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq, Int freqIdx) { SPDLOG_LOGGER_DEBUG( - mSLog, - "Start stamping admittance matrix for frequency index {:d}...", + mSLog, "Start stamping admittance matrix for frequency index {:d}...", freqIdx); stampMatrix(admittanceMat, mat, node1Index, node2Index, @@ -61,6 +59,36 @@ void MNAStampUtils::stampAdmittanceMatrix( SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); } +void MNAStampUtils::stampConductanceAs3x3ScalarMatrix( + Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, + Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, + const Logger::Log &mSLog) { + SPDLOG_LOGGER_DEBUG(mSLog, + "Start stamping conductance as 3x3 scalar matrix..."); + + stampValueAsScalarMatrix(conductance, 3, mat, node1Index, node2Index, + isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0, + mSLog); + + SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); +} + +void MNAStampUtils::stampAdmittanceAs3x3ScalarMatrix( + Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, + Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, + const Logger::Log &mSLog, Int maxFreq, Int freqIdx) { + SPDLOG_LOGGER_DEBUG(mSLog, + "Start stamping admittance as 3x3 scalar matrix for " + "frequency index {:d}...", + freqIdx); + + stampValueAsScalarMatrix(admittance, 3, mat, node1Index, node2Index, + isTerminal1NotGrounded, isTerminal2NotGrounded, + maxFreq, freqIdx, mSLog); + + SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); +} + template void MNAStampUtils::stampValue(T value, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, @@ -114,6 +142,29 @@ void MNAStampUtils::stampMatrix(const MatrixVar &matrix, } } +template +void MNAStampUtils::stampValueAsScalarMatrix( + T value, UInt sizeOfScalarMatrix, SparseMatrixRow &mat, UInt node1Index, + UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, + Int maxFreq, Int freqIdx, const Logger::Log &mSLog) { + if (isTerminal1NotGrounded && isTerminal2NotGrounded) { + for (UInt i = 0; i < sizeOfScalarMatrix; i++) { + stampValueNoConditions(value, mat, node1Index + i, node2Index + i, + maxFreq, freqIdx, mSLog); + } + } else if (isTerminal1NotGrounded) { + for (UInt i = 0; i < sizeOfScalarMatrix; i++) { + stampValueOnDiagonalNoConditions(value, mat, node1Index + i, maxFreq, + freqIdx, mSLog); + } + } else if (isTerminal2NotGrounded) { + for (UInt i = 0; i < sizeOfScalarMatrix; i++) { + stampValueOnDiagonalNoConditions(value, mat, node2Index + i, maxFreq, + freqIdx, mSLog); + } + } +} + template void MNAStampUtils::stampValueNoConditions(T value, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, @@ -152,7 +203,8 @@ void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Real value, Int maxFreq, Int freqIdx, const Logger::Log &mSLog) { - SPDLOG_LOGGER_DEBUG(mSLog, "- Adding {:s} to system matrix element ({:d},{:d})", + SPDLOG_LOGGER_DEBUG(mSLog, + "- Adding {:s} to system matrix element ({:d},{:d})", Logger::realToString(value), row, column); Math::addToMatrixElement(mat, row, column, value); @@ -162,7 +214,8 @@ void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq, Int freqIdx, const Logger::Log &mSLog) { - SPDLOG_LOGGER_DEBUG(mSLog, "- Adding {:s} to system matrix element ({:d},{:d})", + SPDLOG_LOGGER_DEBUG(mSLog, + "- Adding {:s} to system matrix element ({:d},{:d})", Logger::complexToString(value), row, column); Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);