bool EigenLinearSolver::solve(EigenMatrix &A, EigenVector& b, EigenVector &x) { INFO("------------------------------------------------------------------"); INFO("*** Eigen solver computation"); #ifdef USE_EIGEN_UNSUPPORTED std::unique_ptr<Eigen::IterScaling<EigenMatrix::RawMatrixType>> scal; if (_option.scaling) { INFO("-> scale"); scal.reset(new Eigen::IterScaling<EigenMatrix::RawMatrixType>()); scal->computeRef(A.getRawMatrix()); b.getRawVector() = scal->LeftScaling().cwiseProduct(b.getRawVector()); } #endif auto const success = _solver->solve(A.getRawMatrix(), b.getRawVector(), x.getRawVector(), _option); #ifdef USE_EIGEN_UNSUPPORTED if (scal) x.getRawVector() = scal->RightScaling().cwiseProduct(x.getRawVector()); #endif INFO("------------------------------------------------------------------"); return success; }
bool EigenLisLinearSolver::solve(EigenMatrix &A_, EigenVector& b_, EigenVector &x_) { static_assert(EigenMatrix::RawMatrixType::IsRowMajor, "Sparse matrix is required to be in row major storage."); auto &A = A_.getRawMatrix(); auto &b = b_.getRawVector(); auto &x = x_.getRawVector(); if (!A.isCompressed()) A.makeCompressed(); int nnz = A.nonZeros(); int* ptr = A.outerIndexPtr(); int* col = A.innerIndexPtr(); double* data = A.valuePtr(); LisMatrix lisA(A_.getNumberOfRows(), nnz, ptr, col, data); LisVector lisb(b.rows(), b.data()); LisVector lisx(x.rows(), x.data()); LisLinearSolver lissol; // TODO not always creat Lis solver here lissol.setOption(_lis_option); lissol.solve(lisA, lisb, lisx); for (std::size_t i=0; i<lisx.size(); i++) x[i] = lisx[i]; return true; // TODO implement checks }
EigenLinearSolver::EigenLinearSolver(EigenMatrix &A, const std::string /*solver_name*/, BaseLib::ConfigTree const*const option) { if (option) setOption(*option); if (!A.getRawMatrix().isCompressed()) A.getRawMatrix().makeCompressed(); if (_option.solver_type==EigenOption::SolverType::SparseLU) { using SolverType = Eigen::SparseLU<EigenMatrix::RawMatrixType, Eigen::COLAMDOrdering<int>>; _solver = new details::EigenDirectLinearSolver<SolverType, IEigenSolver>(A.getRawMatrix()); } else if (_option.solver_type==EigenOption::SolverType::BiCGSTAB) { using SolverType = Eigen::BiCGSTAB<EigenMatrix::RawMatrixType, Eigen::DiagonalPreconditioner<double>>; _solver = new details::EigenIterativeLinearSolver<SolverType, IEigenSolver>(A.getRawMatrix()); } else if (_option.solver_type==EigenOption::SolverType::CG) { using SolverType = Eigen::ConjugateGradient<EigenMatrix::RawMatrixType, Eigen::Lower, Eigen::DiagonalPreconditioner<double>>; _solver = new details::EigenIterativeLinearSolver<SolverType, IEigenSolver>(A.getRawMatrix()); } }
void addToMatrix(EigenMatrix& m, std::initializer_list<double> values) { auto const rows = m.getNumberOfRows(); auto const cols = m.getNumberOfColumns(); assert(static_cast<EigenMatrix::IndexType>(values.size()) == rows*cols); Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp(rows, cols); auto it = values.begin(); for (GlobalIndexType r=0; r<rows; ++r) { for (GlobalIndexType c=0; c<cols; ++c) { tmp(r, c) = *(it++); } } m.getRawMatrix() += tmp.sparseView(); }
void setMatrix(EigenMatrix& m, Eigen::MatrixXd const& tmp) { m.getRawMatrix() = tmp.sparseView(); }