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) {
152 Matrix x = rightSideVector;
156 Int rhsCols = Eigen::internal::convert_index<Int>(rightSideVector.cols());
159 Int rhsRows = Eigen::internal::convert_index<Int>(rightSideVector.rows());
164 klu_tsolve(mSymbolic, mNumeric, rhsRows, rhsCols,
165 x.const_cast_derived().data(), &mCommon);
171 std::string outputName =
"A" + std::to_string(counter) +
".mtx";
172 Int n = Eigen::internal::convert_index<Int>(matrix.rows());
174 auto Ap = Eigen::internal::convert_index<const Int *>(matrix.outerIndexPtr());
175 auto Ai = Eigen::internal::convert_index<const Int *>(matrix.innerIndexPtr());
176 auto Ax = Eigen::internal::convert_index<const Real *>(matrix.valuePtr());
177 Int nz = Eigen::internal::convert_index<Int>(matrix.nonZeros());
180 ofs.open(outputName);
184 ofs <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
185 ofs << n <<
" " << n <<
" " << nz << std::endl;
186 for (
int i = 0; i < n; i++) {
187 for (
int j = Ap[i]; j < Ap[i + 1]; j++) {
188 ofs << i + 1 <<
" " << Ai[j] + 1 <<
" " << Ax[j] << std::endl;
196 case SCALING_METHOD::NO_SCALING:
199 case SCALING_METHOD::SUM_SCALING:
202 case SCALING_METHOD::MAX_SCALING:
209 SPDLOG_LOGGER_INFO(
mSLog,
"Matrix is scaled using " +
214 case FILL_IN_REDUCTION_METHOD::AMD:
215 mPreordering = AMD_ORDERING;
217 case FILL_IN_REDUCTION_METHOD::AMD_NV:
218 mPreordering = AMD_ORDERING_NV;
220 case FILL_IN_REDUCTION_METHOD::AMD_RA:
221 mPreordering = AMD_ORDERING_RA;
224 mPreordering = AMD_ORDERING;
227 SPDLOG_LOGGER_INFO(
mSLog,
228 "Matrix is fill reduced with " +
232 mPartialRefactorizationMethod =
236 mSLog,
"Matrix is refactored " +
240 case USE_BTF::DO_BTF:
243 case USE_BTF::NO_BTF:
250 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.