diff --git a/dpsim-models/include/dpsim-models/MNASimPowerComp.h b/dpsim-models/include/dpsim-models/MNASimPowerComp.h index 31d254b6d8..8190627e5a 100644 --- a/dpsim-models/include/dpsim-models/MNASimPowerComp.h +++ b/dpsim-models/include/dpsim-models/MNASimPowerComp.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include diff --git a/dpsim-models/include/dpsim-models/MNAStampUtils.h b/dpsim-models/include/dpsim-models/MNAStampUtils.h new file mode 100644 index 0000000000..fea77ed126 --- /dev/null +++ b/dpsim-models/include/dpsim-models/MNAStampUtils.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include + +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 + static void stampValue(T value, SparseMatrixRow &mat, UInt node1Index, + UInt node2Index, Bool isTerminal1NotGrounded, + Bool isTerminal2NotGrounded, Int maxFreq, Int freqIdx, + const Logger::Log &mSLog); + + template + static void stampMatrix(const MatrixVar &matrix, 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, + Int maxFreq, Int freqIdx, + const Logger::Log &mSLog); + + template + static void stampValueOnDiagonalNoConditions(T value, SparseMatrixRow &mat, + UInt nodeIndex, Int maxFreq, + Int freqIdx, + const Logger::Log &mSLog); + + template + 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 \ No newline at end of file diff --git a/dpsim-models/src/CMakeLists.txt b/dpsim-models/src/CMakeLists.txt index 230c6cccc3..50cafa8a3d 100644 --- a/dpsim-models/src/CMakeLists.txt +++ b/dpsim-models/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(dpsim-models STATIC Logger.cpp MathUtils.cpp + MNAStampUtils.cpp Attribute.cpp TopologicalNode.cpp TopologicalTerminal.cpp diff --git a/dpsim-models/src/DP/DP_Ph1_Capacitor.cpp b/dpsim-models/src/DP/DP_Ph1_Capacitor.cpp index c42e50457f..08f0195da4 100644 --- a/dpsim-models/src/DP/DP_Ph1_Capacitor.cpp +++ b/dpsim-models/src/DP/DP_Ph1_Capacitor.cpp @@ -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) { diff --git a/dpsim-models/src/DP/DP_Ph1_Inductor.cpp b/dpsim-models/src/DP/DP_Ph1_Inductor.cpp index aa53c01147..48fa904970 100644 --- a/dpsim-models/src/DP/DP_Ph1_Inductor.cpp +++ b/dpsim-models/src/DP/DP_Ph1_Inductor.cpp @@ -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) { diff --git a/dpsim-models/src/DP/DP_Ph1_Resistor.cpp b/dpsim-models/src/DP/DP_Ph1_Resistor.cpp index 6c20c74c08..3ef1f4fb28 100644 --- a/dpsim-models/src/DP/DP_Ph1_Resistor.cpp +++ b/dpsim-models/src/DP/DP_Ph1_Resistor.cpp @@ -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( diff --git a/dpsim-models/src/DP/DP_Ph3_Capacitor.cpp b/dpsim-models/src/DP/DP_Ph3_Capacitor.cpp index 53ad089247..5233267620 100644 --- a/dpsim-models/src/DP/DP_Ph3_Capacitor.cpp +++ b/dpsim-models/src/DP/DP_Ph3_Capacitor.cpp @@ -138,105 +138,9 @@ void DP::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep, void DP::Ph3::Capacitor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - - if (terminalNotGrounded(0)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mEquivCond(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mEquivCond(2, 2)); - } /* - mLog.debug() << "\n--- Apply system matrix stamp ---" << std::endl; - if (terminalNotGrounded(0)) { - mLog.debug() << "Add " << mEquivCond(0, 0) << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - mLog.debug() << "Add " << mEquivCond(1, 0) << " to " << matrixNodeIndex(0, 1) << "," << matrixNodeIndex(0, 1) << std::endl; - mLog.debug() << "Add " << mEquivCond(2, 0) << " to " << matrixNodeIndex(0, 2) << "," << matrixNodeIndex(0, 2) << std::endl; - } - if (terminalNotGrounded(1)) { - mLog.debug() << "Add " << mEquivCond(0, 0) << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(1, 0) << std::endl; - mLog.debug() << "Add " << mEquivCond(0, 1) << " to " << matrixNodeIndex(1, 1) << "," << matrixNodeIndex(1, 1) << std::endl; - mLog.debug() << "Add " << mEquivCond(0, 2) << " to " << matrixNodeIndex(1, 2) << "," << matrixNodeIndex(1, 2) << std::endl; - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - mLog.debug() << "Add " << -mEquivCond(0, 0) << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(1, 0) << std::endl - << "Add " << -mEquivCond(0, 0) << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - mLog.debug() << "Add " << -mEquivCond(1, 0) << " to " << matrixNodeIndex(0, 1) << "," << matrixNodeIndex(1, 1) << std::endl - << "Add " << -mEquivCond(1, 0) << " to " << matrixNodeIndex(1, 1) << "," << matrixNodeIndex(0, 1) << std::endl; - mLog.debug() << "Add " << -mEquivCond(2, 0) << " to " << matrixNodeIndex(0, 2) << "," << matrixNodeIndex(1, 2) << std::endl - << "Add " << -mEquivCond(2, 0) << " to " << matrixNodeIndex(1, 2) << "," << matrixNodeIndex(0, 2) << std::endl; - }*/ + MNAStampUtils::stampAdmittanceMatrix( + mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void DP::Ph3::Capacitor::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) { diff --git a/dpsim-models/src/DP/DP_Ph3_Inductor.cpp b/dpsim-models/src/DP/DP_Ph3_Inductor.cpp index 099093a476..be0ff3e8dc 100644 --- a/dpsim-models/src/DP/DP_Ph3_Inductor.cpp +++ b/dpsim-models/src/DP/DP_Ph3_Inductor.cpp @@ -107,94 +107,9 @@ void DP::Ph3::Inductor::mnaCompInitialize(Real omega, Real timeStep, void DP::Ph3::Inductor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - - if (terminalNotGrounded(0)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mEquivCond(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mEquivCond(2, 2)); - } - - // if (terminalNotGrounded(0)) - // mLog.debug() << "Add " << mEquivCond << " to system at " << matrixNodeIndex(0) << "," << matrixNodeIndex(0) << std::endl; - // if (terminalNotGrounded(1)) - // mLog.debug() << "Add " << mEquivCond << " to system at " << matrixNodeIndex(1) << "," << matrixNodeIndex(1) << std::endl; - // if (terminalNotGrounded(0) && terminalNotGrounded(1)) - // mLog.debug() << "Add " << -mEquivCond << " to system at " << matrixNodeIndex(0) << "," << matrixNodeIndex(1) << std::endl - // << "Add " << -mEquivCond << " to system at " << matrixNodeIndex(1) << "," << matrixNodeIndex(0) << std::endl; + MNAStampUtils::stampAdmittanceMatrix( + mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void DP::Ph3::Inductor::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) { diff --git a/dpsim-models/src/DP/DP_Ph3_Resistor.cpp b/dpsim-models/src/DP/DP_Ph3_Resistor.cpp index 90691b4610..7ca6a3a828 100644 --- a/dpsim-models/src/DP/DP_Ph3_Resistor.cpp +++ b/dpsim-models/src/DP/DP_Ph3_Resistor.cpp @@ -56,147 +56,12 @@ void DP::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep, void DP::Ph3::Resistor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { + MatrixFixedSizeComp<3, 3> conductance = Matrix::Zero(3, 3); + conductance.real() = (**mResistance).inverse(); - MatrixFixedSize<3, 3> conductance = (**mResistance).inverse(); - - //// Set diagonal entries - //if (terminalNotGrounded(0)) - // Math::addToMatrixElement(systemMatrix, matrixNodeIndices(0), matrixNodeIndices(0), conductance); - //if (terminalNotGrounded(1)) - // Math::addToMatrixElement(systemMatrix, matrixNodeIndices(1), matrixNodeIndices(1), conductance); - //// Set off diagonal entries - //if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - // Math::addToMatrixElement(systemMatrix, matrixNodeIndices(0), matrixNodeIndices(1), -conductance); - // Math::addToMatrixElement(systemMatrix, matrixNodeIndices(1), matrixNodeIndices(0), -conductance); - //} - // Set diagonal entries - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), - Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), - Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), - Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), - Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), - Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), - Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), - Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), - Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), - Complex(conductance(2, 2), 0)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), - Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), - Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), - Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), - Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), - Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), - Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), - Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), - Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), - Complex(conductance(2, 2), 0)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), - -Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), - -Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), - -Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), - -Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), - -Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), - -Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), - -Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), - -Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), - -Complex(conductance(2, 2), 0)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), - -Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), - -Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), - -Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), - -Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), - -Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), - -Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), - -Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), - -Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), - -Complex(conductance(2, 2), 0)); - } - - //if (terminalNotGrounded(0)) - // SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance, matrixNodeIndex(0,0), matrixNodeIndex(0,0)); - //if (terminalNotGrounded(1)) - // SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance, matrixNodeIndex(1,0), matrixNodeIndex(1,0)); - //if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - // SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance, matrixNodeIndex(0,0), matrixNodeIndex(1,0)); - // SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance, matrixNodeIndex(1,0), matrixNodeIndex(0,0)); - //} + MNAStampUtils::stampAdmittanceMatrix( + conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void DP::Ph3::Resistor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/EMT/EMT_Ph1_Capacitor.cpp b/dpsim-models/src/EMT/EMT_Ph1_Capacitor.cpp index d85454f067..5fcbba1961 100644 --- a/dpsim-models/src/EMT/EMT_Ph1_Capacitor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph1_Capacitor.cpp @@ -59,18 +59,9 @@ void EMT::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph1::Capacitor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(0), mEquivCond); - if (terminalNotGrounded(1)) - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(1), mEquivCond); - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(1), -mEquivCond); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(0), -mEquivCond); - } + MNAStampUtils::stampConductance(mEquivCond, systemMatrix, matrixNodeIndex(0), + matrixNodeIndex(1), terminalNotGrounded(0), + terminalNotGrounded(1), mSLog); } void EMT::Ph1::Capacitor::mnaCompApplyRightSideVectorStamp( diff --git a/dpsim-models/src/EMT/EMT_Ph1_Inductor.cpp b/dpsim-models/src/EMT/EMT_Ph1_Inductor.cpp index 97ab706d04..63e8a275da 100644 --- a/dpsim-models/src/EMT/EMT_Ph1_Inductor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph1_Inductor.cpp @@ -57,18 +57,9 @@ void EMT::Ph1::Inductor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph1::Inductor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(0), mEquivCond); - if (terminalNotGrounded(1)) - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(1), mEquivCond); - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(1), -mEquivCond); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(0), -mEquivCond); - } + MNAStampUtils::stampConductance(mEquivCond, systemMatrix, matrixNodeIndex(0), + matrixNodeIndex(1), terminalNotGrounded(0), + terminalNotGrounded(1), mSLog); } void EMT::Ph1::Inductor::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) { diff --git a/dpsim-models/src/EMT/EMT_Ph1_Resistor.cpp b/dpsim-models/src/EMT/EMT_Ph1_Resistor.cpp index 6ec21320ee..a96950151c 100644 --- a/dpsim-models/src/EMT/EMT_Ph1_Resistor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph1_Resistor.cpp @@ -52,34 +52,9 @@ void EMT::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph1::Resistor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { Real conductance = 1. / **mResistance; - // 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); - } - - if (terminalNotGrounded(0)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", conductance, - matrixNodeIndex(0), matrixNodeIndex(0)); - if (terminalNotGrounded(1)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", conductance, - matrixNodeIndex(1), matrixNodeIndex(1)); - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", -conductance, - matrixNodeIndex(0), matrixNodeIndex(1)); - SPDLOG_LOGGER_INFO(mSLog, "Add {:f} to system at ({:d},{:d})", -conductance, - matrixNodeIndex(1), matrixNodeIndex(0)); - } - mSLog->flush(); + MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0), + matrixNodeIndex(1), terminalNotGrounded(0), + terminalNotGrounded(1), mSLog); } void EMT::Ph1::Resistor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/EMT/EMT_Ph3_Capacitor.cpp b/dpsim-models/src/EMT/EMT_Ph3_Capacitor.cpp index 355fa26521..a1a9350193 100644 --- a/dpsim-models/src/EMT/EMT_Ph3_Capacitor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph3_Capacitor.cpp @@ -76,88 +76,9 @@ void EMT::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph3::Capacitor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mEquivCond(2, 2)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mEquivCond(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mEquivCond(2, 2)); - } + MNAStampUtils::stampConductanceMatrix( + mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); SPDLOG_LOGGER_INFO(mSLog, "\nEquivalent Conductance: {:s}", Logger::matrixToString(mEquivCond)); diff --git a/dpsim-models/src/EMT/EMT_Ph3_Inductor.cpp b/dpsim-models/src/EMT/EMT_Ph3_Inductor.cpp index d329a4de23..be7b68b3ec 100644 --- a/dpsim-models/src/EMT/EMT_Ph3_Inductor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph3_Inductor.cpp @@ -90,88 +90,9 @@ void EMT::Ph3::Inductor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph3::Inductor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mEquivCond(2, 2)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mEquivCond(2, 2)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mEquivCond(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mEquivCond(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mEquivCond(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mEquivCond(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mEquivCond(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mEquivCond(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mEquivCond(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mEquivCond(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mEquivCond(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mEquivCond(2, 2)); - } + MNAStampUtils::stampConductanceMatrix( + mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); SPDLOG_LOGGER_INFO(mSLog, "\nEquivalent Conductance: {:s}", Logger::matrixToString(mEquivCond)); diff --git a/dpsim-models/src/EMT/EMT_Ph3_Resistor.cpp b/dpsim-models/src/EMT/EMT_Ph3_Resistor.cpp index c4dcff8d8e..88ce5229de 100644 --- a/dpsim-models/src/EMT/EMT_Ph3_Resistor.cpp +++ b/dpsim-models/src/EMT/EMT_Ph3_Resistor.cpp @@ -69,93 +69,12 @@ void EMT::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep, void EMT::Ph3::Resistor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { + MatrixFixedSize<3, 3> conductance = Matrix::Zero(3, 3); + conductance = (**mResistance).inverse(); - Matrix conductance = Matrix::Zero(3, 3); - Math::invertMatrix(**mResistance, conductance); - - // Set diagonal entries - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), conductance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), conductance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), conductance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), conductance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), conductance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), conductance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), conductance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), conductance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), conductance(2, 2)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), conductance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), conductance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), conductance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), conductance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), conductance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), conductance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), conductance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), conductance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), conductance(2, 2)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -conductance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -conductance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -conductance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -conductance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -conductance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -conductance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -conductance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -conductance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -conductance(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -conductance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -conductance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -conductance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -conductance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -conductance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -conductance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -conductance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -conductance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -conductance(2, 2)); - } + MNAStampUtils::stampConductanceMatrix( + conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); SPDLOG_LOGGER_INFO(mSLog, "\nConductance matrix: {:s}", Logger::matrixToString(conductance)); diff --git a/dpsim-models/src/MNAStampUtils.cpp b/dpsim-models/src/MNAStampUtils.cpp new file mode 100644 index 0000000000..4bb7b6bb0f --- /dev/null +++ b/dpsim-models/src/MNAStampUtils.cpp @@ -0,0 +1,169 @@ +#include + +using namespace CPS; + +void MNAStampUtils::stampConductance(Real conductance, SparseMatrixRow &mat, + UInt node1Index, UInt node2Index, + Bool isTerminal1NotGrounded, + Bool isTerminal2NotGrounded, + const Logger::Log &mSLog) { + SPDLOG_LOGGER_DEBUG(mSLog, "Start stamping conductance..."); + + stampValue(conductance, mat, node1Index, node2Index, isTerminal1NotGrounded, + isTerminal2NotGrounded, 1, 0, mSLog); + + SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); +} + +void MNAStampUtils::stampAdmittance(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 for frequency index {:d}...", + freqIdx); + + stampValue(admittance, mat, node1Index, node2Index, isTerminal1NotGrounded, + isTerminal2NotGrounded, maxFreq, freqIdx, mSLog); + + SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); +} + +void MNAStampUtils::stampConductanceMatrix(const Matrix &conductanceMat, + SparseMatrixRow &mat, + UInt node1Index, UInt node2Index, + Bool isTerminal1NotGrounded, + Bool isTerminal2NotGrounded, + const Logger::Log &mSLog) { + SPDLOG_LOGGER_DEBUG(mSLog, "Start stamping conductance matrix..."); + + stampMatrix(conductanceMat, mat, node1Index, node2Index, + isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0, mSLog); + + SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed."); +} + +void MNAStampUtils::stampAdmittanceMatrix( + const MatrixComp &admittanceMat, 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 matrix for frequency index {:d}...", + freqIdx); + + stampMatrix(admittanceMat, 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, + Bool isTerminal2NotGrounded, Int maxFreq, + Int freqIdx, const Logger::Log &mSLog) { + if (isTerminal1NotGrounded && isTerminal2NotGrounded) { + stampValueNoConditions(value, mat, node1Index, node2Index, maxFreq, freqIdx, + mSLog); + } else if (isTerminal1NotGrounded) { + stampValueOnDiagonalNoConditions(value, mat, node1Index, maxFreq, freqIdx, + mSLog); + } else if (isTerminal2NotGrounded) { + stampValueOnDiagonalNoConditions(value, mat, node2Index, maxFreq, freqIdx, + mSLog); + } +} + +template +void MNAStampUtils::stampMatrix(const MatrixVar &matrix, + SparseMatrixRow &mat, UInt node1Index, + UInt node2Index, Bool isTerminal1NotGrounded, + Bool isTerminal2NotGrounded, Int maxFreq, + Int freqIdx, const Logger::Log &mSLog) { + Int numRows = matrix.rows(); + Int numCols = matrix.cols(); + if (numRows != numCols) { + throw InvalidArgumentException(); + } + + if (isTerminal1NotGrounded && isTerminal2NotGrounded) { + for (UInt i = 0; i < numRows; i++) { + for (UInt j = 0; j < numCols; j++) { + stampValueNoConditions(matrix(i, j), mat, node1Index + i, + node2Index + j, maxFreq, freqIdx, mSLog); + } + } + } else if (isTerminal1NotGrounded) { + for (UInt i = 0; i < numRows; i++) { + for (UInt j = 0; j < numCols; j++) { + stampValueOnDiagonalNoConditions(matrix(i, j), mat, node1Index + i, + maxFreq, freqIdx, mSLog); + } + } + } else if (isTerminal2NotGrounded) { + for (UInt i = 0; i < numRows; i++) { + for (UInt j = 0; j < numCols; j++) { + stampValueOnDiagonalNoConditions(matrix(i, j), mat, node2Index + j, + maxFreq, freqIdx, mSLog); + } + } + } +} + +template +void MNAStampUtils::stampValueNoConditions(T value, SparseMatrixRow &mat, + UInt node1Index, UInt node2Index, + Int maxFreq, Int freqIdx, + const Logger::Log &mSLog) { + stampValueOnDiagonalNoConditions(value, mat, node1Index, maxFreq, freqIdx, + mSLog); + stampValueOnDiagonalNoConditions(value, mat, node2Index, maxFreq, freqIdx, + mSLog); + stampValueOffDiagonalNoConditions(value, mat, node1Index, node2Index, maxFreq, + freqIdx, mSLog); +} + +template +void MNAStampUtils::stampValueOnDiagonalNoConditions(T value, + SparseMatrixRow &mat, + UInt nodeIndex, + Int maxFreq, Int freqIdx, + const Logger::Log &mSLog) { + addToMatrixElement(mat, nodeIndex, nodeIndex, value, maxFreq, freqIdx, mSLog); +} + +template +void MNAStampUtils::stampValueOffDiagonalNoConditions( + T value, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, + Int maxFreq, Int freqIdx, const Logger::Log &mSLog) { + addToMatrixElement(mat, node1Index, node2Index, -value, maxFreq, freqIdx, + mSLog); + addToMatrixElement(mat, node2Index, node1Index, -value, maxFreq, freqIdx, + mSLog); +} + +// These wrapper functions standardize the signatures of "Math::addToMatrixElement" for Real and Complex "value" parameters, +// facilitating the use of templates in the stamping logic. +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})", + Logger::realToString(value), row, column); + + Math::addToMatrixElement(mat, row, column, value); +} + +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})", + Logger::complexToString(value), row, column); + + Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx); +} diff --git a/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp b/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp index f6739394bd..3a07366a83 100644 --- a/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Capacitor.cpp @@ -69,39 +69,9 @@ void SP::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, void SP::Ph1::Capacitor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - - if (terminalNotGrounded(0)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(0), mSusceptance); - } - if (terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(1), mSusceptance); - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(1), -mSusceptance); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(0), -mSusceptance); - } - - SPDLOG_LOGGER_INFO(mSLog, "-- Matrix Stamp ---"); - if (terminalNotGrounded(0)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - mSusceptance.real(), mSusceptance.imag(), - matrixNodeIndex(0), matrixNodeIndex(0)); - if (terminalNotGrounded(1)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - mSusceptance.real(), mSusceptance.imag(), - matrixNodeIndex(1), matrixNodeIndex(1)); - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - -mSusceptance.real(), -mSusceptance.imag(), - matrixNodeIndex(0), matrixNodeIndex(1)); - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - -mSusceptance.real(), -mSusceptance.imag(), - matrixNodeIndex(1), matrixNodeIndex(0)); - } + MNAStampUtils::stampAdmittance(mSusceptance, systemMatrix, matrixNodeIndex(0), + matrixNodeIndex(1), terminalNotGrounded(0), + terminalNotGrounded(1), mSLog); } void SP::Ph1::Capacitor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/SP/SP_Ph1_Inductor.cpp b/dpsim-models/src/SP/SP_Ph1_Inductor.cpp index 60d997f360..c7167e01ad 100644 --- a/dpsim-models/src/SP/SP_Ph1_Inductor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Inductor.cpp @@ -66,38 +66,9 @@ void SP::Ph1::Inductor::mnaCompInitialize(Real omega, Real timeStep, void SP::Ph1::Inductor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(0), mSusceptance); - } - if (terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(1), mSusceptance); - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0), - matrixNodeIndex(1), -mSusceptance); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1), - matrixNodeIndex(0), -mSusceptance); - } - - SPDLOG_LOGGER_INFO(mSLog, "-- Matrix Stamp ---"); - if (terminalNotGrounded(0)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - mSusceptance.real(), mSusceptance.imag(), - matrixNodeIndex(0), matrixNodeIndex(0)); - if (terminalNotGrounded(1)) - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - mSusceptance.real(), mSusceptance.imag(), - matrixNodeIndex(1), matrixNodeIndex(1)); - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - -mSusceptance.real(), -mSusceptance.imag(), - matrixNodeIndex(0), matrixNodeIndex(1)); - SPDLOG_LOGGER_INFO(mSLog, "Add {:e}+j{:e} to system at ({:d},{:d})", - -mSusceptance.real(), -mSusceptance.imag(), - matrixNodeIndex(1), matrixNodeIndex(0)); - } + MNAStampUtils::stampAdmittance(mSusceptance, systemMatrix, matrixNodeIndex(0), + matrixNodeIndex(1), terminalNotGrounded(0), + terminalNotGrounded(1), mSLog); } void SP::Ph1::Inductor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/SP/SP_Ph1_Resistor.cpp b/dpsim-models/src/SP/SP_Ph1_Resistor.cpp index 14fe9ac4d0..62880d0474 100644 --- a/dpsim-models/src/SP/SP_Ph1_Resistor.cpp +++ b/dpsim-models/src/SP/SP_Ph1_Resistor.cpp @@ -107,44 +107,9 @@ void SP::Ph1::Resistor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { 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); } void SP::Ph1::Resistor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/SP/SP_Ph3_Capacitor.cpp b/dpsim-models/src/SP/SP_Ph3_Capacitor.cpp index 6b64b84666..ac38f68188 100644 --- a/dpsim-models/src/SP/SP_Ph3_Capacitor.cpp +++ b/dpsim-models/src/SP/SP_Ph3_Capacitor.cpp @@ -76,108 +76,9 @@ void SP::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep, void SP::Ph3::Capacitor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mSusceptance(2, 2)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mSusceptance(2, 2)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mSusceptance(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mSusceptance(2, 2)); - } - //TODO : ADD UPDATED LOGGER - /*mLog.debug() << "\n--- Apply system matrix stamp ---" << std::endl; - if (terminalNotGrounded(0)) { - mLog.debug() << "Add " << mEquivCond(0, 0) << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - mLog.debug() << "Add " << mEquivCond(1, 0) << " to " << matrixNodeIndex(0, 1) << "," << matrixNodeIndex(0, 1) << std::endl; - mLog.debug() << "Add " << mEquivCond(2, 0) << " to " << matrixNodeIndex(0, 2) << "," << matrixNodeIndex(0, 2) << std::endl; - } - if (terminalNotGrounded(1)) { - mLog.debug() << "Add " << mEquivCond(0, 0) << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(1, 0) << std::endl; - mLog.debug() << "Add " << mEquivCond(0, 1) << " to " << matrixNodeIndex(1, 1) << "," << matrixNodeIndex(1, 1) << std::endl; - mLog.debug() << "Add " << mEquivCond(0, 2) << " to " << matrixNodeIndex(1, 2) << "," << matrixNodeIndex(1, 2) << std::endl; - } - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - mLog.debug() << "Add " << -mEquivCond(0, 0) << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(1, 0) << std::endl - << "Add " << -mEquivCond(0, 0) << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - mLog.debug() << "Add " << -mEquivCond(1, 0) << " to " << matrixNodeIndex(0, 1) << "," << matrixNodeIndex(1, 1) << std::endl - << "Add " << -mEquivCond(1, 0) << " to " << matrixNodeIndex(1, 1) << "," << matrixNodeIndex(0, 1) << std::endl; - mLog.debug() << "Add " << -mEquivCond(2, 0) << " to " << matrixNodeIndex(0, 2) << "," << matrixNodeIndex(1, 2) << std::endl - << "Add " << -mEquivCond(2, 0) << " to " << matrixNodeIndex(1, 2) << "," << matrixNodeIndex(0, 2) << std::endl; - }*/ + MNAStampUtils::stampAdmittanceMatrix( + mSusceptance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void SP::Ph3::Capacitor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/SP/SP_Ph3_Inductor.cpp b/dpsim-models/src/SP/SP_Ph3_Inductor.cpp index e76e51aa81..e40f7007f3 100644 --- a/dpsim-models/src/SP/SP_Ph3_Inductor.cpp +++ b/dpsim-models/src/SP/SP_Ph3_Inductor.cpp @@ -78,97 +78,9 @@ void SP::Ph3::Inductor::mnaCompInitialize(Real omega, Real timeStep, void SP::Ph3::Inductor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), mSusceptance(2, 2)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), mSusceptance(2, 2)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), -mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), -mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), -mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), -mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), -mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), -mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), -mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), -mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), -mSusceptance(2, 2)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), -mSusceptance(0, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), -mSusceptance(0, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), -mSusceptance(0, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), -mSusceptance(1, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), -mSusceptance(1, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), -mSusceptance(1, 2)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), -mSusceptance(2, 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), -mSusceptance(2, 1)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), -mSusceptance(2, 2)); - } - - /* - if (terminalNotGrounded(0)) - mLog.debug() << "Add " << mEquivCond << " to system at " << matrixNodeIndex(0) << "," << matrixNodeIndex(0) << std::endl; - if (terminalNotGrounded(1)) - mLog.debug() << "Add " << mEquivCond << " to system at " << matrixNodeIndex(1) << "," << matrixNodeIndex(1) << std::endl; - if (terminalNotGrounded(0) && terminalNotGrounded(1)) - mLog.debug() << "Add " << -mEquivCond << " to system at " << matrixNodeIndex(0) << "," << matrixNodeIndex(1) << std::endl - << "Add " << -mEquivCond << " to system at " << matrixNodeIndex(1) << "," << matrixNodeIndex(0) << std::endl;*/ + MNAStampUtils::stampAdmittanceMatrix( + mSusceptance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void SP::Ph3::Inductor::mnaCompAddPostStepDependencies( diff --git a/dpsim-models/src/SP/SP_Ph3_Resistor.cpp b/dpsim-models/src/SP/SP_Ph3_Resistor.cpp index 3a33b2c647..8c6cebfc59 100644 --- a/dpsim-models/src/SP/SP_Ph3_Resistor.cpp +++ b/dpsim-models/src/SP/SP_Ph3_Resistor.cpp @@ -53,137 +53,12 @@ void SP::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep, void SP::Ph3::Resistor::mnaCompApplySystemMatrixStamp( SparseMatrixRow &systemMatrix) { + MatrixFixedSizeComp<3, 3> conductance = Matrix::Zero(3, 3); + conductance.real() = (**mResistance).inverse(); - Matrix conductance = (**mResistance).inverse(); - - if (terminalNotGrounded(0)) { - // set upper left block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 0), - Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 1), - Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(0, 2), - Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 0), - Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 1), - Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(0, 2), - Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 0), - Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 1), - Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(0, 2), - Complex(conductance(2, 2), 0)); - } - if (terminalNotGrounded(1)) { - // set buttom right block, 3x3 entries - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 0), - Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 1), - Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(1, 2), - Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 0), - Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 1), - Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(1, 2), - Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 0), - Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 1), - Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(1, 2), - Complex(conductance(2, 2), 0)); - } - // Set off diagonal blocks, 2x3x3 entries - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 0), - -Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 1), - -Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), - matrixNodeIndex(1, 2), - -Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 0), - -Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 1), - -Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), - matrixNodeIndex(1, 2), - -Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 0), - -Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 1), - -Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), - matrixNodeIndex(1, 2), - -Complex(conductance(2, 2), 0)); - - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 0), - -Complex(conductance(0, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 1), - -Complex(conductance(0, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), - matrixNodeIndex(0, 2), - -Complex(conductance(0, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 0), - -Complex(conductance(1, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 1), - -Complex(conductance(1, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), - matrixNodeIndex(0, 2), - -Complex(conductance(1, 2), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 0), - -Complex(conductance(2, 0), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 1), - -Complex(conductance(2, 1), 0)); - Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), - matrixNodeIndex(0, 2), - -Complex(conductance(2, 2), 0)); - } - - // TODO: add Log - /*if (terminalNotGrounded(0)) - mLog.debug() << "Add " << conductance << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - if (terminalNotGrounded(1)) - mLog.debug() << "Add " << conductance << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(1, 0) << std::endl; - if (terminalNotGrounded(0) && terminalNotGrounded(1)) { - mLog.debug() << "Add " << -conductance << " to " << matrixNodeIndex(0, 0) << "," << matrixNodeIndex(1, 0) << std::endl; - mLog.debug() << "Add " << -conductance << " to " << matrixNodeIndex(1, 0) << "," << matrixNodeIndex(0, 0) << std::endl; - }*/ + MNAStampUtils::stampAdmittanceMatrix( + conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1), + terminalNotGrounded(0), terminalNotGrounded(1), mSLog); } void SP::Ph3::Resistor::mnaCompAddPostStepDependencies(