Commit 93bb3631 authored by Philipp Fensch's avatar Philipp Fensch Committed by Markus Mirz
Browse files

Added wrapper (SparseMatrixRow -> Matrix) for mnaApplySystemMatrixStamp()

parent db688d10
......@@ -214,13 +214,7 @@ void MnaSolver<VarType>::initializeSystemWithPrecomputedMatrices() {
if (mSwitches.size() < 1) {
// Create system matrix if no switches were added
for (auto comp : mMNAComponents) {
#ifdef WITH_SPARSE
Matrix mat = Matrix(mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
comp->mnaApplySystemMatrixStamp(mat);
mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)] = mat.sparseView();
#else
comp->mnaApplySystemMatrixStamp(mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
#endif
auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(comp);
mSLog->debug("Stamping {:s} {:s} into system matrix",
idObj->type(), idObj->name());
......@@ -239,26 +233,15 @@ void MnaSolver<VarType>::initializeSystemWithPrecomputedMatrices() {
else {
// Generate switching state dependent system matrices
for (auto& sys : mSwitchedMatrices) {
#ifdef WITH_SPARSE
for (auto comp : mMNAComponents) {
Matrix mat = Matrix(sys.second);
comp->mnaApplySystemMatrixStamp(mat);
sys.second = mat.sparseView();
}
for (UInt i = 0; i < mSwitches.size(); i++) {
Matrix mat = Matrix(sys.second);
mSwitches[i]->mnaApplySwitchSystemMatrixStamp(mat, sys.first[i]);
sys.second = mat.sparseView();
}
// Compute LU-factorization for system matrix
mLuFactorizations[sys.first].analyzePattern(sys.second);
mLuFactorizations[sys.first].factorize(sys.second);
#else
for (auto comp : mMNAComponents)
comp->mnaApplySystemMatrixStamp(sys.second);
for (UInt i = 0; i < mSwitches.size(); i++)
mSwitches[i]->mnaApplySwitchSystemMatrixStamp(sys.second, sys.first[i]);
// Compute LU-factorization for system matrix
#ifdef WITH_SPARSE
mLuFactorizations[sys.first].analyzePattern(sys.second);
mLuFactorizations[sys.first].factorize(sys.second);
#else
mLuFactorizations[sys.first] = Eigen::PartialPivLU<Matrix>(sys.second);
#endif
}
......@@ -373,16 +356,15 @@ void MnaSolver<Real>::createEmptySystemMatrix() {
if (mSwitches.size() > SWITCH_NUM)
throw SystemError("Too many Switches.");
for (std::size_t i = 0; i < (1ULL << mSwitches.size()); i++) {
#ifdef WITH_SPARSE
for (std::size_t i = 0; i < (1ULL << mSwitches.size()); i++)
mSwitchedMatrices[std::bitset<SWITCH_NUM>(i)].resize(mNumMatrixNodeIndices, mNumMatrixNodeIndices);
#else
mSwitchedMatrices[std::bitset<SWITCH_NUM>(i)] = Matrix::Zero(mNumMatrixNodeIndices, mNumMatrixNodeIndices);
#endif
}
#ifdef WITH_SPARSE
mBaseSystemMatrix.resize(mNumMatrixNodeIndices, mNumMatrixNodeIndices);
#else
for (std::size_t i = 0; i < (1ULL << mSwitches.size()); i++)
mSwitchedMatrices[std::bitset<SWITCH_NUM>(i)] = Matrix::Zero(mNumMatrixNodeIndices, mNumMatrixNodeIndices);
mBaseSystemMatrix = Matrix::Zero(mNumMatrixNodeIndices, mNumMatrixNodeIndices);
#endif
}
......
......@@ -34,13 +34,7 @@ void MnaSolverSysRecomp<VarType>::initializeSystem() {
// Do not stamp variable elements yet
auto varcomp = std::dynamic_pointer_cast<MNAVariableCompInterface>(comp);
if (varcomp) continue;
#ifdef WITH_SPARSE
Matrix mat = Matrix(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
comp->mnaApplySystemMatrixStamp(mat);
this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)] = mat.sparseView();
#else
comp->mnaApplySystemMatrixStamp(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
#endif
comp->mnaApplySystemMatrixStamp(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(comp);
}
......@@ -51,13 +45,7 @@ void MnaSolverSysRecomp<VarType>::initializeSystem() {
// Now stamp variable elements
this->mSLog->info("Stamping variable elements");
for (auto varElem : this->mMNAIntfVariableComps) {
#ifdef WITH_SPARSE
Matrix mat = Matrix(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
varElem->mnaApplySystemMatrixStamp(mat);
this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)] = mat.sparseView();
#else
varElem->mnaApplySystemMatrixStamp(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
#endif
}
#ifdef WITH_SPARSE
this->mLuFactorizations[std::bitset<SWITCH_NUM>(0)].analyzePattern(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
......@@ -92,13 +80,7 @@ void MnaSolverSysRecomp<VarType>::updateSystemMatrix(Real time) {
// Create system matrix with changed variable elements
for (auto comp : this->mMNAIntfVariableComps) {
#ifdef WITH_SPARSE
Matrix mat = Matrix(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
comp->mnaApplySystemMatrixStamp(mat);
this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)] = mat.sparseView();
#else
comp->mnaApplySystemMatrixStamp(this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)]);
#endif
auto idObj = std::dynamic_pointer_cast<IdentifiedObject>(comp);
this->mSLog->debug("Updating {:s} {:s} in system matrix (variabel component)",
idObj->type(), idObj->name());
......
......@@ -30,6 +30,12 @@ namespace CPS {
}
/// Stamps system matrix
virtual void mnaApplySystemMatrixStamp(Matrix& systemMatrix) { }
/// Stamps (sparse) system matrix
virtual void mnaApplySystemMatrixStamp(SparseMatrixRow& systemMatrix) {
Matrix mat = Matrix(systemMatrix);
mnaApplySystemMatrixStamp(mat);
systemMatrix = mat.sparseView();
}
/// Stamps right side (source) vector
virtual void mnaApplyRightSideVectorStamp(Matrix& rightVector) { }
/// Update interface voltage from MNA system result
......
......@@ -24,5 +24,11 @@ namespace CPS {
virtual Bool mnaIsClosed() = 0;
/// Stamps system matrix considering the defined switch position
virtual void mnaApplySwitchSystemMatrixStamp(Matrix& systemMatrix, Bool closed) { }
/// Stamps (sparse) system matrix considering the defined switch position
virtual void mnaApplySwitchSystemMatrixStamp(SparseMatrixRow& systemMatrix, Bool closed) {
Matrix mat = Matrix(systemMatrix);
mnaApplySwitchSystemMatrixStamp(mat, closed);
systemMatrix = mat.sparseView();
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment