1 #include <dpsim-models/MNAStampUtils.h>
5 void MNAStampUtils::stampConductance(Real conductance, SparseMatrixRow &mat,
6 UInt node1Index, UInt node2Index,
7 Bool isTerminal1NotGrounded,
8 Bool isTerminal2NotGrounded,
9 const Logger::Log &mSLog) {
10 SPDLOG_LOGGER_DEBUG(mSLog,
"Start stamping conductance...");
12 stampValue(conductance, mat, node1Index, node2Index, isTerminal1NotGrounded,
13 isTerminal2NotGrounded, 1, 0, mSLog);
15 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
18 void MNAStampUtils::stampAdmittance(Complex admittance, SparseMatrixRow &mat,
19 UInt node1Index, UInt node2Index,
20 Bool isTerminal1NotGrounded,
21 Bool isTerminal2NotGrounded,
22 const Logger::Log &mSLog, Int maxFreq,
25 mSLog,
"Start stamping admittance for frequency index {:d}...", freqIdx);
27 stampValue(admittance, mat, node1Index, node2Index, isTerminal1NotGrounded,
28 isTerminal2NotGrounded, maxFreq, freqIdx, mSLog);
30 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
33 void MNAStampUtils::stampConductanceMatrix(
const Matrix &conductanceMat,
35 UInt node1Index, UInt node2Index,
36 Bool isTerminal1NotGrounded,
37 Bool isTerminal2NotGrounded,
38 const Logger::Log &mSLog) {
39 SPDLOG_LOGGER_DEBUG(mSLog,
"Start stamping conductance matrix...");
41 stampMatrix(conductanceMat, mat, node1Index, node2Index,
42 isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0, mSLog);
44 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
47 void MNAStampUtils::stamp3x3ConductanceMatrixBetween2Nodes(
48 const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index,
49 UInt node2Index,
const Logger::Log &mSLog) {
51 mSLog,
"Start stamping 3x3 conductance matrix between two nodes...");
53 stampMatrixBetween2Nodes(conductanceMat, 3, mat, node1Index, node2Index, 1, 0,
56 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
59 void MNAStampUtils::stamp3x3ConductanceMatrixNodeToGround(
60 const Matrix &conductanceMat, SparseMatrixRow &mat, UInt nodeIndex,
61 const Logger::Log &mSLog) {
63 mSLog,
"Start stamping 3x3 conductance matrix from node to ground...");
65 stampMatrixNodeToGround(conductanceMat, 3, mat, nodeIndex, 1, 0, mSLog);
67 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
70 void MNAStampUtils::stampAdmittanceMatrix(
71 const MatrixComp &admittanceMat, SparseMatrixRow &mat, UInt node1Index,
72 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
73 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
75 mSLog,
"Start stamping admittance matrix for frequency index {:d}...",
78 stampMatrix(admittanceMat, mat, node1Index, node2Index,
79 isTerminal1NotGrounded, isTerminal2NotGrounded, maxFreq, freqIdx,
82 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
86 Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
87 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
88 const Logger::Log &mSLog) {
89 SPDLOG_LOGGER_DEBUG(mSLog,
90 "Start stamping conductance as 3x3 scalar matrix...");
92 stampValueAsScalarMatrix(conductance, 3, mat, node1Index, node2Index,
93 isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0,
96 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
100 Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
101 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
102 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
103 SPDLOG_LOGGER_DEBUG(mSLog,
104 "Start stamping admittance as 3x3 scalar matrix for "
105 "frequency index {:d}...",
108 stampValueAsScalarMatrix(admittance, 3, mat, node1Index, node2Index,
109 isTerminal1NotGrounded, isTerminal2NotGrounded,
110 maxFreq, freqIdx, mSLog);
112 SPDLOG_LOGGER_DEBUG(mSLog,
"Stamping completed.");
115 template <
typename T>
116 void MNAStampUtils::stampValue(T value, SparseMatrixRow &mat, UInt node1Index,
117 UInt node2Index, Bool isTerminal1NotGrounded,
118 Bool isTerminal2NotGrounded, Int maxFreq,
119 Int freqIdx,
const Logger::Log &mSLog) {
120 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
121 stampToMatrix(value, mat, node1Index, node1Index, node2Index, node2Index,
122 maxFreq, freqIdx, mSLog);
123 }
else if (isTerminal1NotGrounded) {
124 addToMatrixElement(mat, node1Index, node1Index, value, maxFreq, freqIdx,
126 }
else if (isTerminal2NotGrounded) {
127 addToMatrixElement(mat, node2Index, node2Index, value, maxFreq, freqIdx,
132 template <
typename T>
133 void MNAStampUtils::stampMatrix(
const MatrixVar<T> &matrix,
134 SparseMatrixRow &mat, UInt node1Index,
135 UInt node2Index, Bool isTerminal1NotGrounded,
136 Bool isTerminal2NotGrounded, Int maxFreq,
137 Int freqIdx,
const Logger::Log &mSLog) {
138 Int numRows = matrix.rows();
139 Int numCols = matrix.cols();
140 if (numRows != numCols) {
144 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
145 stampMatrixBetween2Nodes(matrix, numRows, mat, node1Index, node2Index,
146 maxFreq, freqIdx, mSLog);
147 }
else if (isTerminal1NotGrounded) {
148 stampMatrixNodeToGround(matrix, numRows, mat, node1Index, maxFreq, freqIdx,
150 }
else if (isTerminal2NotGrounded) {
151 stampMatrixNodeToGround(matrix, numRows, mat, node2Index, maxFreq, freqIdx,
156 template <
typename T>
157 void MNAStampUtils::stampMatrixBetween2Nodes(
const MatrixVar<T> &matrix,
159 SparseMatrixRow &mat,
160 UInt node1Index, UInt node2Index,
161 Int maxFreq, Int freqIdx,
162 const Logger::Log &mSLog) {
163 for (UInt i = 0; i < sizeOfMatrix; i++) {
164 for (UInt j = 0; j < sizeOfMatrix; j++) {
165 stampToMatrix(matrix(i, j), mat, node1Index + i, node1Index + j,
166 node2Index + i, node2Index + j, maxFreq, freqIdx, mSLog);
171 template <
typename T>
172 void MNAStampUtils::stampMatrixNodeToGround(
173 const MatrixVar<T> &matrix, UInt sizeOfMatrix, SparseMatrixRow &mat,
174 UInt nodeIndex, Int maxFreq, Int freqIdx,
const Logger::Log &mSLog) {
175 for (UInt i = 0; i < sizeOfMatrix; i++) {
176 for (UInt j = 0; j < sizeOfMatrix; j++) {
177 addToMatrixElement(mat, nodeIndex + i, nodeIndex + j, matrix(i, j),
178 maxFreq, freqIdx, mSLog);
183 template <
typename T>
184 void MNAStampUtils::stampValueAsScalarMatrix(
185 T value, UInt sizeOfScalarMatrix, SparseMatrixRow &mat, UInt node1Index,
186 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
187 Int maxFreq, Int freqIdx,
const Logger::Log &mSLog) {
188 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
189 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
190 stampToMatrix(value, mat, node1Index + i, node1Index + i, node2Index + i,
191 node2Index + i, maxFreq, freqIdx, mSLog);
193 }
else if (isTerminal1NotGrounded) {
194 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
195 addToMatrixElement(mat, node1Index + i, node1Index + i, value, maxFreq,
198 }
else if (isTerminal2NotGrounded) {
199 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
200 addToMatrixElement(mat, node2Index + i, node2Index + i, value, maxFreq,
206 template <
typename T>
207 void MNAStampUtils::stampToMatrix(T value, SparseMatrixRow &mat, UInt row1,
208 UInt column1, UInt row2, UInt column2,
209 Int maxFreq, Int freqIdx,
210 const Logger::Log &mSLog) {
211 addToMatrixElement(mat, row1, column1, value, maxFreq, freqIdx, mSLog);
212 addToMatrixElement(mat, row1, column2, -value, maxFreq, freqIdx, mSLog);
213 addToMatrixElement(mat, row2, column1, -value, maxFreq, freqIdx, mSLog);
214 addToMatrixElement(mat, row2, column2, value, maxFreq, freqIdx, mSLog);
219 void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
220 Matrix::Index column, Real value,
221 Int maxFreq, Int freqIdx,
222 const Logger::Log &mSLog) {
223 SPDLOG_LOGGER_DEBUG(mSLog,
224 "- Adding {:s} to system matrix element ({:d},{:d})",
225 Logger::realToString(value), row, column);
227 Math::addToMatrixElement(mat, row, column, value);
230 void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
231 Matrix::Index column, Complex value,
232 Int maxFreq, Int freqIdx,
233 const Logger::Log &mSLog) {
234 SPDLOG_LOGGER_DEBUG(mSLog,
235 "- Adding {:s} to system matrix element ({:d},{:d})",
236 Logger::complexToString(value), row, column);
238 Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
static void stampConductanceAs3x3ScalarMatrix(Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog)
Stamps conductance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal t...
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)
Stamps admittance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal to...