Skip to content

Commit

Permalink
Reuse conductance stamp code (#306)
Browse files Browse the repository at this point in the history
### Summary
This PR extends functionality of `MNAStampUtils` with functions to stamp
conductance/admittance as scalar matrix and reuses the stamping logic
for switches.

### Details

- Reuse conductance/admittance stamping logic for all switch components
(at least, all that I found😅): `DP_Ph1_Switch`, `DP_Ph1_varResSwitch`,
`DP_Ph3_SeriesSwitch`, `EMT_Ph3_SeriesSwitch`, `EMT_Ph3_Switch`,
`SP_Ph1_Switch`, `SP_Ph1_varResSwitch.`
- `MNAStampUtils`: Add functions for stamping conductance/admittance
value as a scalar matrix. These functions are used for optimized
conductance/admittance stamping by components with symmetric phase
impedance, such as `DP_Ph3_SeriesSwitch`, `EMT_Ph3_SeriesSwitch`.

### Related work
- **Issue Reference**: These changes were discussed in an issue:
#288
- **Previous PR**: This PR continues the work of:
#297
- **Future Plans**: There are plans for future PRs to reuse stamp code
for other components deriving from the `MNASimPowerComp` class.
  • Loading branch information
m-mirz authored Jun 21, 2024
2 parents ffc5080 + 5fffc7f commit 9665729
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 503 deletions.
20 changes: 20 additions & 0 deletions dpsim-models/include/dpsim-models/MNAStampUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class MNAStampUtils {
UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
const Logger::Log &mSLog, Int maxFreq = 1, Int freqIdx = 0);

/// Stamps conductance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal to conductance).
static void stampConductanceAs3x3ScalarMatrix(
Real conductance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index,
Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded,
const Logger::Log &mSLog);

/// Stamps admittance as a 3x3 scalar matrix (a diagonal matrix, where all diagonal elements are equal to admittance).
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);

private:
template <typename T>
static void stampValue(T value, SparseMatrixRow &mat, UInt node1Index,
Expand All @@ -45,6 +57,14 @@ class MNAStampUtils {
Bool isTerminal2NotGrounded, Int maxFreq, Int freqIdx,
const Logger::Log &mSLog);

template <typename T>
static void stampValueAsScalarMatrix(T value, UInt sizeOfScalarMatrix,
SparseMatrixRow &mat, UInt node1Index,
UInt node2Index,
Bool isTerminal1NotGrounded,
Bool isTerminal2NotGrounded, Int maxFreq,
Int freqIdx, const Logger::Log &mSLog);

template <typename T>
static void stampValueNoConditions(T value, SparseMatrixRow &mat,
UInt node1Index, UInt node2Index,
Expand Down
71 changes: 6 additions & 65 deletions dpsim-models/src/DP/DP_Ph1_Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,78 +54,19 @@ void DP::Ph1::Switch::mnaCompApplySystemMatrixStamp(
Complex conductance = (**mIsClosed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 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_TRACE(mSLog, "-- Stamp ---");
if (terminalNotGrounded(0))
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance),
matrixNodeIndex(0), matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance),
matrixNodeIndex(1), matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-conductance),
matrixNodeIndex(0), matrixNodeIndex(1));
SPDLOG_LOGGER_TRACE(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::Switch::mnaCompApplySwitchSystemMatrixStamp(
Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
Complex conductance = (closed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 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_TRACE(mSLog, "-- Stamp ---");
if (terminalNotGrounded(0))
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance),
matrixNodeIndex(0), matrixNodeIndex(0));
if (terminalNotGrounded(1))
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(conductance),
matrixNodeIndex(1), matrixNodeIndex(1));
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_TRACE(mSLog, "Add {:s} to system at ({:d},{:d})",
Logger::complexToString(-conductance),
matrixNodeIndex(0), matrixNodeIndex(1));
SPDLOG_LOGGER_TRACE(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::Switch::mnaCompApplyRightSideVectorStamp(Matrix &rightVector) {}
Expand Down
53 changes: 6 additions & 47 deletions dpsim-models/src/DP/DP_Ph1_varResSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,60 +47,19 @@ void DP::Ph1::varResSwitch::mnaCompApplySystemMatrixStamp(
Complex conductance = (**mIsClosed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 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);
}
MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
matrixNodeIndex(1), terminalNotGrounded(0),
terminalNotGrounded(1), mSLog);
}

void DP::Ph1::varResSwitch::mnaCompApplySwitchSystemMatrixStamp(
Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
Complex conductance = (closed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 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 ---");
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::varResSwitch::mnaCompApplyRightSideVectorStamp(
Expand Down
60 changes: 6 additions & 54 deletions dpsim-models/src/DP/DP_Ph3_SeriesSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,67 +50,19 @@ void DP::Ph3::SeriesSwitch::mnaCompApplySystemMatrixStamp(
Complex conductance = (**mIsClosed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 0);

// 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);
}

if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(0)[0]);
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(1)[0]);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(1)[0]);
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(0)[0]);
}
MNAStampUtils::stampAdmittanceAs3x3ScalarMatrix(
conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
}

void DP::Ph3::SeriesSwitch::mnaCompApplySwitchSystemMatrixStamp(
Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
Complex conductance = (closed) ? Complex(1. / **mClosedResistance, 0)
: Complex(1. / **mOpenResistance, 0);

// 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);
}

if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(0)[0]);
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(1)[0]);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(1)[0]);
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(0)[0]);
}
MNAStampUtils::stampAdmittanceAs3x3ScalarMatrix(
conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
}

void DP::Ph3::SeriesSwitch::mnaCompAddPostStepDependencies(
Expand Down
60 changes: 6 additions & 54 deletions dpsim-models/src/EMT/EMT_Ph3_SeriesSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,67 +65,19 @@ void EMT::Ph3::SeriesSwitch::mnaCompApplySystemMatrixStamp(
Real conductance =
(**mIsClosed) ? 1. / **mClosedResistance : 1. / **mOpenResistance;

// 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);
}

if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(0)[0]);
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(1)[0]);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(1)[0]);
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(0)[0]);
}
MNAStampUtils::stampConductanceAs3x3ScalarMatrix(
conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
}

void EMT::Ph3::SeriesSwitch::mnaCompApplySwitchSystemMatrixStamp(
Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
Real conductance =
(closed) ? 1. / **mClosedResistance : 1. / **mOpenResistance;

// 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);
}

if (terminalNotGrounded(0))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(0)[0]);
if (terminalNotGrounded(1))
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(1)[0]);
if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(0)[0], matrixNodeIndices(1)[0]);
SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
matrixNodeIndices(1)[0], matrixNodeIndices(0)[0]);
}
MNAStampUtils::stampConductanceAs3x3ScalarMatrix(
conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
}

void EMT::Ph3::SeriesSwitch::mnaCompAddPostStepDependencies(
Expand Down
Loading

0 comments on commit 9665729

Please sign in to comment.