9 #include <dpsim/KLUAdapter.h>
11 using namespace DPsim;
16 klu_free_symbolic(&mSymbolic, &mCommon);
18 klu_free_numeric(&mNumeric, &mCommon);
19 SPDLOG_LOGGER_INFO(
mSLog,
"Number of Pivot Faults: {}", mPivotFaults);
23 klu_defaults(&mCommon);
29 mPreordering = AMD_ORDERING;
32 mVaryingColumns.clear();
41 SparseMatrix &systemMatrix,
42 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
44 klu_free_symbolic(&mSymbolic, &mCommon);
47 const Int n = Eigen::internal::convert_index<Int>(systemMatrix.rows());
49 auto Ap = Eigen::internal::convert_index<Int *>(systemMatrix.outerIndexPtr());
50 auto Ai = Eigen::internal::convert_index<Int *>(systemMatrix.innerIndexPtr());
52 mVaryingColumns.clear();
55 mChangedEntries = listVariableSystemMatrixEntries;
57 Eigen::internal::convert_index<Int>(mChangedEntries.size());
59 for (
auto &changedEntry : mChangedEntries) {
60 mVaryingRows.push_back(changedEntry.first);
61 mVaryingColumns.push_back(changedEntry.second);
66 klu_analyze_partial(n, Ap, Ai, &mVaryingColumns[0], &mVaryingRows[0],
67 varying_entries, mPreordering, &mCommon);
71 nnz = Eigen::internal::convert_index<Int>(systemMatrix.nonZeros());
76 klu_free_numeric(&mNumeric, &mCommon);
79 auto Ap = Eigen::internal::convert_index<Int *>(systemMatrix.outerIndexPtr());
80 auto Ai = Eigen::internal::convert_index<Int *>(systemMatrix.innerIndexPtr());
81 auto Ax = Eigen::internal::convert_index<Real *>(systemMatrix.valuePtr());
83 mNumeric = klu_factor(Ap, Ai, Ax, mSymbolic, &mCommon);
86 Eigen::internal::convert_index<Int>(mChangedEntries.size());
90 if (!(mVaryingColumns.empty()) && !(mVaryingRows.empty())) {
91 if (mPartialRefactorizationMethod ==
92 DPsim::PARTIAL_REFACTORIZATION_METHOD::FACTORIZATION_PATH) {
93 klu_compute_path(mSymbolic, mNumeric, &mCommon, Ap, Ai,
94 &mVaryingColumns[0], &mVaryingRows[0], varying_entries);
95 }
else if (mPartialRefactorizationMethod ==
96 DPsim::PARTIAL_REFACTORIZATION_METHOD::REFACTORIZATION_RESTART) {
97 klu_determine_start(mSymbolic, mNumeric, &mCommon, Ap, Ai,
98 &mVaryingColumns[0], &mVaryingRows[0],
106 if (systemMatrix.nonZeros() != nnz) {
111 Eigen::internal::convert_index<Int *>(systemMatrix.outerIndexPtr());
113 Eigen::internal::convert_index<Int *>(systemMatrix.innerIndexPtr());
114 auto Ax = Eigen::internal::convert_index<Real *>(systemMatrix.valuePtr());
115 klu_refactor(Ap, Ai, Ax, mSymbolic, mNumeric, &mCommon);
120 SparseMatrix &systemMatrix,
121 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
122 if (systemMatrix.nonZeros() != nnz) {
123 preprocessing(systemMatrix, listVariableSystemMatrixEntries);
127 Eigen::internal::convert_index<Int *>(systemMatrix.outerIndexPtr());
129 Eigen::internal::convert_index<Int *>(systemMatrix.innerIndexPtr());
130 auto Ax = Eigen::internal::convert_index<Real *>(systemMatrix.valuePtr());
132 if (mPartialRefactorizationMethod ==
133 PARTIAL_REFACTORIZATION_METHOD::FACTORIZATION_PATH) {
134 klu_partial_factorization_path(Ap, Ai, Ax, mSymbolic, mNumeric, &mCommon);
135 }
else if (mPartialRefactorizationMethod ==
136 PARTIAL_REFACTORIZATION_METHOD::REFACTORIZATION_RESTART) {
137 klu_partial_refactorization_restart(Ap, Ai, Ax, mSymbolic, mNumeric,
140 klu_refactor(Ap, Ai, Ax, mSymbolic, mNumeric, &mCommon);
143 if (mCommon.status == KLU_PIVOT_FAULT) {
154 Matrix x = rightSideVector;
158 Int rhsCols = Eigen::internal::convert_index<Int>(rightSideVector.cols());
161 Int rhsRows = Eigen::internal::convert_index<Int>(rightSideVector.rows());
166 klu_tsolve(mSymbolic, mNumeric, rhsRows, rhsCols,
167 x.const_cast_derived().data(), &mCommon);
173 std::string outputName =
"A" + std::to_string(counter) +
".mtx";
174 Int n = Eigen::internal::convert_index<Int>(matrix.rows());
176 auto Ap = Eigen::internal::convert_index<const Int *>(matrix.outerIndexPtr());
177 auto Ai = Eigen::internal::convert_index<const Int *>(matrix.innerIndexPtr());
178 auto Ax = Eigen::internal::convert_index<const Real *>(matrix.valuePtr());
179 Int nz = Eigen::internal::convert_index<Int>(matrix.nonZeros());
182 ofs.open(outputName);
186 ofs <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
187 ofs << n <<
" " << n <<
" " << nz << std::endl;
188 for (
int i = 0; i < n; i++) {
189 for (
int j = Ap[i]; j < Ap[i + 1]; j++) {
190 ofs << i + 1 <<
" " << Ai[j] + 1 <<
" " << Ax[j] << std::endl;
198 case SCALING_METHOD::NO_SCALING:
201 case SCALING_METHOD::SUM_SCALING:
204 case SCALING_METHOD::MAX_SCALING:
211 SPDLOG_LOGGER_INFO(
mSLog,
"Matrix is scaled using " +
216 case FILL_IN_REDUCTION_METHOD::AMD:
217 mPreordering = AMD_ORDERING;
219 case FILL_IN_REDUCTION_METHOD::AMD_NV:
220 mPreordering = AMD_ORDERING_NV;
222 case FILL_IN_REDUCTION_METHOD::AMD_RA:
223 mPreordering = AMD_ORDERING_RA;
226 mPreordering = AMD_ORDERING;
229 SPDLOG_LOGGER_INFO(
mSLog,
230 "Matrix is fill reduced with " +
234 mPartialRefactorizationMethod =
238 mSLog,
"Matrix is refactored " +
242 case USE_BTF::DO_BTF:
245 case USE_BTF::NO_BTF:
252 SPDLOG_LOGGER_INFO(
mSLog,
DirectLinearSolverConfiguration mConfiguration
Object that carries configuration options.
CPS::Logger::Log mSLog
Stores logger of solver class.
void applyConfiguration() override
Apply configuration.
void preprocessing(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
preprocessing function pre-ordering and scaling the matrix
void factorize(SparseMatrix &systemMatrix) override
factorization function with partial pivoting
Matrix solve(Matrix &rightSideVector) override
solution function for a right hand side
void partialRefactorize(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
partial refactorization withouth partial pivoting
void refactorize(SparseMatrix &systemMatrix) override
refactorization without partial pivoting
void printMatrixMarket(SparseMatrix &systemMatrix, int counter) const
Function to print matrix in MatrixMarket's coo format.
~KLUAdapter() override
Destructor.