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 Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
105 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
106 const Logger::Log &mSLog) {
107 SPDLOG_LOGGER_DEBUG(mSLog,
108 "Start stamping conductance as 3x3 scalar matrix...");
109
110 stampValueAsScalarMatrix(conductance, 3, mat, node1Index, node2Index,
111 isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0,
112 mSLog);
113
114 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
115}
116
118 Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
119 Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
120 const Logger::Log &mSLog, Int maxFreq, Int freqIdx) {
121 SPDLOG_LOGGER_DEBUG(mSLog,
122 "Start stamping admittance as 3x3 scalar matrix for "
123 "frequency index {:d}...",
124 freqIdx);
125
126 stampValueAsScalarMatrix(admittance, 3, mat, node1Index, node2Index,
127 isTerminal1NotGrounded, isTerminal2NotGrounded,
128 maxFreq, freqIdx, mSLog);
129
130 SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
131}
132
133template <typename T>
134void MNAStampUtils::stampValue(T value, SparseMatrixRow &mat, UInt node1Index,
135 UInt node2Index, Bool isTerminal1NotGrounded,
136 Bool isTerminal2NotGrounded, Int maxFreq,
137 Int freqIdx, const Logger::Log &mSLog) {
138 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
139 stampToMatrix(value, mat, node1Index, node1Index, node2Index, node2Index,
140 maxFreq, freqIdx, mSLog);
141 } else if (isTerminal1NotGrounded) {
142 addToMatrixElement(mat, node1Index, node1Index, value, maxFreq, freqIdx,
143 mSLog);
144 } else if (isTerminal2NotGrounded) {
145 addToMatrixElement(mat, node2Index, node2Index, value, maxFreq, freqIdx,
146 mSLog);
147 }
148}
149
150template <typename T>
151void MNAStampUtils::stampMatrix(const MatrixVar<T> &matrix,
152 SparseMatrixRow &mat, UInt node1Index,
153 UInt node2Index, Bool isTerminal1NotGrounded,
154 Bool isTerminal2NotGrounded, Int maxFreq,
155 Int freqIdx, const Logger::Log &mSLog) {
156 Int numRows = matrix.rows();
157 Int numCols = matrix.cols();
158 if (numRows != numCols) {
159 throw InvalidArgumentException();
160 }
161
162 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
163 stampMatrixBetween2Nodes(matrix, numRows, mat, node1Index, node2Index,
164 maxFreq, freqIdx, mSLog);
165 } else if (isTerminal1NotGrounded) {
166 stampMatrixNodeToGround(matrix, numRows, mat, node1Index, maxFreq, freqIdx,
167 mSLog);
168 } else if (isTerminal2NotGrounded) {
169 stampMatrixNodeToGround(matrix, numRows, mat, node2Index, maxFreq, freqIdx,
170 mSLog);
171 }
172}
173
174template <typename T>
175void MNAStampUtils::stampMatrixBetween2Nodes(const MatrixVar<T> &matrix,
176 UInt sizeOfMatrix,
177 SparseMatrixRow &mat,
178 UInt node1Index, UInt node2Index,
179 Int maxFreq, Int freqIdx,
180 const Logger::Log &mSLog) {
181 for (UInt i = 0; i < sizeOfMatrix; i++) {
182 for (UInt j = 0; j < sizeOfMatrix; j++) {
183 stampToMatrix(matrix(i, j), mat, node1Index + i, node1Index + j,
184 node2Index + i, node2Index + j, maxFreq, freqIdx, mSLog);
185 }
186 }
187}
188
189template <typename T>
190void MNAStampUtils::stampMatrixNodeToGround(
191 const MatrixVar<T> &matrix, UInt sizeOfMatrix, SparseMatrixRow &mat,
192 UInt nodeIndex, Int maxFreq, Int freqIdx, const Logger::Log &mSLog) {
193 for (UInt i = 0; i < sizeOfMatrix; i++) {
194 for (UInt j = 0; j < sizeOfMatrix; j++) {
195 addToMatrixElement(mat, nodeIndex + i, nodeIndex + j, matrix(i, j),
196 maxFreq, freqIdx, mSLog);
197 }
198 }
199}
200
201template <typename T>
202void MNAStampUtils::stampValueAsScalarMatrix(
203 T value, UInt sizeOfScalarMatrix, SparseMatrixRow &mat, UInt node1Index,
204 UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
205 Int maxFreq, Int freqIdx, const Logger::Log &mSLog) {
206 if (isTerminal1NotGrounded && isTerminal2NotGrounded) {
207 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
208 stampToMatrix(value, mat, node1Index + i, node1Index + i, node2Index + i,
209 node2Index + i, maxFreq, freqIdx, mSLog);
210 }
211 } else if (isTerminal1NotGrounded) {
212 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
213 addToMatrixElement(mat, node1Index + i, node1Index + i, value, maxFreq,
214 freqIdx, mSLog);
215 }
216 } else if (isTerminal2NotGrounded) {
217 for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
218 addToMatrixElement(mat, node2Index + i, node2Index + i, value, maxFreq,
219 freqIdx, mSLog);
220 }
221 }
222}
223
224template <typename T>
225void MNAStampUtils::stampToMatrix(T value, SparseMatrixRow &mat, UInt row1,
226 UInt column1, UInt row2, UInt column2,
227 Int maxFreq, Int freqIdx,
228 const Logger::Log &mSLog) {
229 addToMatrixElement(mat, row1, column1, value, maxFreq, freqIdx, mSLog);
230 addToMatrixElement(mat, row1, column2, -value, maxFreq, freqIdx, mSLog);
231 addToMatrixElement(mat, row2, column1, -value, maxFreq, freqIdx, mSLog);
232 addToMatrixElement(mat, row2, column2, value, maxFreq, freqIdx, mSLog);
233}
234
235// These wrapper functions standardize the signatures of "Math::addToMatrixElement" for Real and Complex "value" parameters,
236// facilitating the use of templates in the stamping logic.
237void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
238 Matrix::Index column, Real value,
239 Int maxFreq, Int freqIdx,
240 const Logger::Log &mSLog) {
241 SPDLOG_LOGGER_DEBUG(mSLog,
242 "- Adding {:s} to system matrix element ({:d},{:d})",
243 Logger::realToString(value), row, column);
244
245 Math::addToMatrixElement(mat, row, column, value);
246}
247
248void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
249 Matrix::Index column, Complex value,
250 Int maxFreq, Int freqIdx,
251 const Logger::Log &mSLog) {
252 SPDLOG_LOGGER_DEBUG(mSLog,
253 "- Adding {:s} to system matrix element ({:d},{:d})",
254 Logger::complexToString(value), row, column);
255
256 Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
257}
258
259void MNAStampUtils::addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row,
260 Matrix::Index column,
261 const Matrix &value, Int maxFreq,
262 Int freqIdx, const Logger::Log &mSLog) {
263 SPDLOG_LOGGER_DEBUG(
264 mSLog,
265 "- Adding packed real 2x2 block to system matrix element ({:d},{:d})",
266 row, column);
267
268 Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
269}
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