DPsim
Loading...
Searching...
No Matches
MNAStampUtils.cpp
Go to the documentation of this file.
2
3using namespace CPS;
4
6 UInt node1Index, UInt node2Index,
7 Bool isTerminal1NotGrounded,
8 Bool isTerminal2NotGrounded,
9 const Logger::Log &mSLog) {
10 SPDLOG_LOGGER_DEBUG(mSLog, "Start stamping conductance...");
11
12 stampValue(conductance, mat, node1Index, node2Index, isTerminal1NotGrounded,
13 isTerminal2NotGrounded, 1, 0, mSLog);
14
15 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
16}
17
19 UInt node1Index, UInt node2Index,
20 Bool isTerminal1NotGrounded,
21 Bool isTerminal2NotGrounded,
22 const Logger::Log &mSLog, Int maxFreq,
23 Int freqIdx) {
24 SPDLOG_LOGGER_DEBUG(
25 mSLog, "Start stamping admittance for frequency index {:d}...", freqIdx);
26
27 stampValue(admittance, mat, node1Index, node2Index, isTerminal1NotGrounded,
28 isTerminal2NotGrounded, maxFreq, freqIdx, mSLog);
29
30 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
31}
32
34 const Matrix &admittance, SparseMatrixRow &mat, UInt node1Index,
35 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
36 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
37 if (admittance.rows() != 2 || admittance.cols() != 2)
39
40 SPDLOG_LOGGER_DEBUG(
41 mSLog,
42 "Start stamping packed real admittance for frequency index {:d}...",
43 freqIdx);
44
45 stampValue(admittance, mat, node1Index, node2Index, isTerminal1NotGrounded,
46 isTerminal2NotGrounded, maxFreq, freqIdx, mSLog);
47
48 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
49}
50
52 SparseMatrixRow &mat,
53 UInt node1Index, UInt node2Index,
54 Bool isTerminal1NotGrounded,
55 Bool isTerminal2NotGrounded,
56 const Logger::Log &mSLog) {
57 SPDLOG_LOGGER_DEBUG(mSLog, "Start stamping conductance matrix...");
58
59 stampMatrix(conductanceMat, mat, node1Index, node2Index,
60 isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0, mSLog);
61
62 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
63}
64
66 const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index,
67 UInt node2Index, const Logger::Log &mSLog) {
68 SPDLOG_LOGGER_DEBUG(
69 mSLog, "Start stamping 3x3 conductance matrix between two nodes...");
70
71 stampMatrixBetween2Nodes(conductanceMat, 3, mat, node1Index, node2Index, 1, 0,
72 mSLog);
73
74 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
75}
76
78 const Matrix &conductanceMat, SparseMatrixRow &mat, UInt nodeIndex,
79 const Logger::Log &mSLog) {
80 SPDLOG_LOGGER_DEBUG(
81 mSLog, "Start stamping 3x3 conductance matrix from node to ground...");
82
83 stampMatrixNodeToGround(conductanceMat, 3, mat, nodeIndex, 1, 0, mSLog);
84
85 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
86}
87
89 const MatrixComp &admittanceMat, SparseMatrixRow &mat, UInt node1Index,
90 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
91 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
92 SPDLOG_LOGGER_DEBUG(
93 mSLog, "Start stamping admittance matrix for frequency index {:d}...",
94 freqIdx);
95
96 stampMatrix(admittanceMat, mat, node1Index, node2Index,
97 isTerminal1NotGrounded, isTerminal2NotGrounded, maxFreq, freqIdx,
98 mSLog);
99
100 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
101}
102
104 UInt phaseCount, SparseMatrixRow &mat,
105 UInt node1Index, UInt node2Index,
106 Bool isTerminal1NotGrounded,
107 Bool isTerminal2NotGrounded,
108 const Logger::Log &mSLog, Int maxFreq,
109 Int freqIdx) {
110 if (admittanceMat.rows() != 2 * phaseCount ||
111 admittanceMat.cols() != 2 * phaseCount)
113
114 SPDLOG_LOGGER_DEBUG(mSLog,
115 "Start stamping packed real admittance matrix for "
116 "frequency index {:d}...",
117 freqIdx);
118
119 for (UInt row = 0; row < phaseCount; ++row) {
120 for (UInt col = 0; col < phaseCount; ++col) {
121 const Matrix block = admittanceMat.block(2 * row, 2 * col, 2, 2);
122
123 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
124 stampToMatrix(block, mat, node1Index + row, node1Index + col,
125 node2Index + row, node2Index + col, maxFreq, freqIdx,
126 mSLog);
127 } else if (isTerminal1NotGrounded) {
128 addToMatrixElement(mat, node1Index + row, node1Index + col, block,
129 maxFreq, freqIdx, mSLog);
130 } else if (isTerminal2NotGrounded) {
131 addToMatrixElement(mat, node2Index + row, node2Index + col, block,
132 maxFreq, freqIdx, mSLog);
133 }
134 }
135 }
136
137 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
138}
139
141 Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
142 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
143 const Logger::Log &mSLog) {
144 SPDLOG_LOGGER_DEBUG(mSLog,
145 "Start stamping conductance as 3x3 scalar matrix...");
146
147 stampValueAsScalarMatrix(conductance, 3, mat, node1Index, node2Index,
148 isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0,
149 mSLog);
150
151 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
152}
153
155 Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
156 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
157 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
158 SPDLOG_LOGGER_DEBUG(mSLog,
159 "Start stamping admittance as 3x3 scalar matrix for "
160 "frequency index {:d}...",
161 freqIdx);
162
163 stampValueAsScalarMatrix(admittance, 3, mat, node1Index, node2Index,
164 isTerminal1NotGrounded, isTerminal2NotGrounded,
165 maxFreq, freqIdx, mSLog);
166
167 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
168}
169
170template <typename T>
171void MNAStampUtils::stampValue(T value, SparseMatrixRow &mat, UInt node1Index,
172 UInt node2Index, Bool isTerminal1NotGrounded,
173 Bool isTerminal2NotGrounded, Int maxFreq,
174 Int freqIdx, const Logger::Log &mSLog) {
175 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
176 stampToMatrix(value, mat, node1Index, node1Index, node2Index, node2Index,
177 maxFreq, freqIdx, mSLog);
178 } else if (isTerminal1NotGrounded) {
179 addToMatrixElement(mat, node1Index, node1Index, value, maxFreq, freqIdx,
180 mSLog);
181 } else if (isTerminal2NotGrounded) {
182 addToMatrixElement(mat, node2Index, node2Index, value, maxFreq, freqIdx,
183 mSLog);
184 }
185}
186
187template <typename T>
188void MNAStampUtils::stampMatrix(const MatrixVar<T> &matrix,
189 SparseMatrixRow &mat, UInt node1Index,
190 UInt node2Index, Bool isTerminal1NotGrounded,
191 Bool isTerminal2NotGrounded, Int maxFreq,
192 Int freqIdx, const Logger::Log &mSLog) {
193 Int numRows = matrix.rows();
194 Int numCols = matrix.cols();
195 if (numRows != numCols) {
196 throw InvalidArgumentException();
197 }
198
199 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
200 stampMatrixBetween2Nodes(matrix, numRows, mat, node1Index, node2Index,
201 maxFreq, freqIdx, mSLog);
202 } else if (isTerminal1NotGrounded) {
203 stampMatrixNodeToGround(matrix, numRows, mat, node1Index, maxFreq, freqIdx,
204 mSLog);
205 } else if (isTerminal2NotGrounded) {
206 stampMatrixNodeToGround(matrix, numRows, mat, node2Index, maxFreq, freqIdx,
207 mSLog);
208 }
209}
210
211template <typename T>
212void MNAStampUtils::stampMatrixBetween2Nodes(const MatrixVar<T> &matrix,
213 UInt sizeOfMatrix,
214 SparseMatrixRow &mat,
215 UInt node1Index, UInt node2Index,
216 Int maxFreq, Int freqIdx,
217 const Logger::Log &mSLog) {
218 for (UInt i = 0; i < sizeOfMatrix; i++) {
219 for (UInt j = 0; j < sizeOfMatrix; j++) {
220 stampToMatrix(matrix(i, j), mat, node1Index + i, node1Index + j,
221 node2Index + i, node2Index + j, maxFreq, freqIdx, mSLog);
222 }
223 }
224}
225
226template <typename T>
227void MNAStampUtils::stampMatrixNodeToGround(
228 const MatrixVar<T> &matrix, UInt sizeOfMatrix, SparseMatrixRow &mat,
229 UInt nodeIndex, Int maxFreq, Int freqIdx, const Logger::Log &mSLog) {
230 for (UInt i = 0; i < sizeOfMatrix; i++) {
231 for (UInt j = 0; j < sizeOfMatrix; j++) {
232 addToMatrixElement(mat, nodeIndex + i, nodeIndex + j, matrix(i, j),
233 maxFreq, freqIdx, mSLog);
234 }
235 }
236}
237
238template <typename T>
239void MNAStampUtils::stampValueAsScalarMatrix(
240 T value, UInt sizeOfScalarMatrix, SparseMatrixRow &mat, UInt node1Index,
241 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
242 Int maxFreq, Int freqIdx, const Logger::Log &mSLog) {
243 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
244 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
245 stampToMatrix(value, mat, node1Index + i, node1Index + i, node2Index + i,
246 node2Index + i, maxFreq, freqIdx, mSLog);
247 }
248 } else if (isTerminal1NotGrounded) {
249 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
250 addToMatrixElement(mat, node1Index + i, node1Index + i, value, maxFreq,
251 freqIdx, mSLog);
252 }
253 } else if (isTerminal2NotGrounded) {
254 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
255 addToMatrixElement(mat, node2Index + i, node2Index + i, value, maxFreq,
256 freqIdx, mSLog);
257 }
258 }
259}
260
261template <typename T>
262void MNAStampUtils::stampToMatrix(T value, SparseMatrixRow &mat, UInt row1,
263 UInt column1, UInt row2, UInt column2,
264 Int maxFreq, Int freqIdx,
265 const Logger::Log &mSLog) {
266 addToMatrixElement(mat, row1, column1, value, maxFreq, freqIdx, mSLog);
267 addToMatrixElement(mat, row1, column2, -value, maxFreq, freqIdx, mSLog);
268 addToMatrixElement(mat, row2, column1, -value, maxFreq, freqIdx, mSLog);
269 addToMatrixElement(mat, row2, column2, value, maxFreq, freqIdx, mSLog);
270}
271
272// These wrapper functions standardize the signatures of "Math::addToMatrixElement" for Real and Complex "value" parameters,
273// facilitating the use of templates in the stamping logic.
274void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
275 Matrix::Index column, Real value,
276 Int maxFreq, Int freqIdx,
277 const Logger::Log &mSLog) {
278 SPDLOG_LOGGER_DEBUG(mSLog,
279 "- Adding {:s} to system matrix element ({:d},{:d})",
280 Logger::realToString(value), row, column);
281
282 Math::addToMatrixElement(mat, row, column, value);
283}
284
285void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
286 Matrix::Index column, Complex value,
287 Int maxFreq, Int freqIdx,
288 const Logger::Log &mSLog) {
289 SPDLOG_LOGGER_DEBUG(mSLog,
290 "- Adding {:s} to system matrix element ({:d},{:d})",
291 Logger::complexToString(value), row, column);
292
293 Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
294}
295
296void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
297 Matrix::Index column,
298 const Matrix &value, Int maxFreq,
299 Int freqIdx, const Logger::Log &mSLog) {
300 SPDLOG_LOGGER_DEBUG(
301 mSLog,
302 "- Adding packed real 2x2 block to system matrix element ({:d},{:d})",
303 row, column);
304
305 Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
306}
std::shared_ptr< spdlog::logger > Log
Definition Logger.h:34
static String complexToString(const Complex &num)
Definition Logger.cpp:63
static String realToString(const Real &num)
Definition Logger.cpp:69
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 stampConductance(Real conductance, 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)
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...
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 stamp3x3ConductanceMatrixBetween2Nodes(const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, const Logger::Log &mSLog)
static void stampConductanceMatrix(const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog)
static void stamp3x3ConductanceMatrixNodeToGround(const Matrix &conductanceMat, SparseMatrixRow &mat, UInt nodeIndex, const Logger::Log &mSLog)
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
Eigen::Matrix< VarType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixVar
Definition Definitions.h:97
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
bool Bool
Definition Definitions.h:64
unsigned int UInt
Definition Definitions.h:60
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74