DPsim
MNAStampUtils.cpp
1 #include <dpsim-models/MNAStampUtils.h>
2 
3 using namespace CPS;
4 
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...");
11 
12  stampValue(conductance, mat, node1Index, node2Index, isTerminal1NotGrounded,
13  isTerminal2NotGrounded, 1, 0, mSLog);
14 
15  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
16 }
17 
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,
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 
33 void MNAStampUtils::stampConductanceMatrix(const Matrix &conductanceMat,
34  SparseMatrixRow &mat,
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...");
40 
41  stampMatrix(conductanceMat, mat, node1Index, node2Index,
42  isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0, mSLog);
43 
44  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
45 }
46 
47 void MNAStampUtils::stamp3x3ConductanceMatrixBetween2Nodes(
48  const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index,
49  UInt node2Index, const Logger::Log &mSLog) {
50  SPDLOG_LOGGER_DEBUG(
51  mSLog, "Start stamping 3x3 conductance matrix between two nodes...");
52 
53  stampMatrixBetween2Nodes(conductanceMat, 3, mat, node1Index, node2Index, 1, 0,
54  mSLog);
55 
56  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
57 }
58 
59 void MNAStampUtils::stamp3x3ConductanceMatrixNodeToGround(
60  const Matrix &conductanceMat, SparseMatrixRow &mat, UInt nodeIndex,
61  const Logger::Log &mSLog) {
62  SPDLOG_LOGGER_DEBUG(
63  mSLog, "Start stamping 3x3 conductance matrix from node to ground...");
64 
65  stampMatrixNodeToGround(conductanceMat, 3, mat, nodeIndex, 1, 0, mSLog);
66 
67  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
68 }
69 
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) {
74  SPDLOG_LOGGER_DEBUG(
75  mSLog, "Start stamping admittance matrix for frequency index {:d}...",
76  freqIdx);
77 
78  stampMatrix(admittanceMat, mat, node1Index, node2Index,
79  isTerminal1NotGrounded, isTerminal2NotGrounded, maxFreq, freqIdx,
80  mSLog);
81 
82  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
83 }
84 
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...");
91 
92  stampValueAsScalarMatrix(conductance, 3, mat, node1Index, node2Index,
93  isTerminal1NotGrounded, isTerminal2NotGrounded, 1, 0,
94  mSLog);
95 
96  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
97 }
98 
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}...",
106  freqIdx);
107 
108  stampValueAsScalarMatrix(admittance, 3, mat, node1Index, node2Index,
109  isTerminal1NotGrounded, isTerminal2NotGrounded,
110  maxFreq, freqIdx, mSLog);
111 
112  SPDLOG_LOGGER_DEBUG(mSLog, "Stamping completed.");
113 }
114 
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,
125  mSLog);
126  } else if (isTerminal2NotGrounded) {
127  addToMatrixElement(mat, node2Index, node2Index, value, maxFreq, freqIdx,
128  mSLog);
129  }
130 }
131 
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) {
141  throw InvalidArgumentException();
142  }
143 
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,
149  mSLog);
150  } else if (isTerminal2NotGrounded) {
151  stampMatrixNodeToGround(matrix, numRows, mat, node2Index, maxFreq, freqIdx,
152  mSLog);
153  }
154 }
155 
156 template <typename T>
157 void MNAStampUtils::stampMatrixBetween2Nodes(const MatrixVar<T> &matrix,
158  UInt sizeOfMatrix,
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);
167  }
168  }
169 }
170 
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);
179  }
180  }
181 }
182 
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);
192  }
193  } else if (isTerminal1NotGrounded) {
194  for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
195  addToMatrixElement(mat, node1Index + i, node1Index + i, value, maxFreq,
196  freqIdx, mSLog);
197  }
198  } else if (isTerminal2NotGrounded) {
199  for (UInt i = 0; i < sizeOfScalarMatrix; i++) {
200  addToMatrixElement(mat, node2Index + i, node2Index + i, value, maxFreq,
201  freqIdx, mSLog);
202  }
203  }
204 }
205 
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);
215 }
216 
217 // These wrapper functions standardize the signatures of "Math::addToMatrixElement" for Real and Complex "value" parameters,
218 // facilitating the use of templates in the stamping logic.
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);
226 
227  Math::addToMatrixElement(mat, row, column, value);
228 }
229 
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);
237 
238  Math::addToMatrixElement(mat, row, column, value, maxFreq, freqIdx);
239 }
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...