SolverILU2.cpp 2.96 KB
Newer Older
Dmitry Bagaev's avatar
Dmitry Bagaev committed
1 2 3 4
#include "SolverILU2.h"

namespace INMOST {

Dmitry Bagaev's avatar
Dmitry Bagaev committed
5
    SolverILU2::SolverILU2() {
6 7
        Method *preconditioner = new ILU2_preconditioner(info);
        solver = new KSOLVER(preconditioner, info);
Dmitry Bagaev's avatar
Dmitry Bagaev committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
        matrix = NULL;
    }

    SolverILU2::SolverILU2(const SolverInterface *other) {
        //You should not really want to copy solver's information
        throw INMOST::SolverUnsupportedOperation;
    }

    void SolverILU2::Assign(const SolverInterface *other) {
        //You should not really want to copy solver's information
        throw INMOST::SolverUnsupportedOperation;
    }

    void SolverILU2::Initialize(int *argc, char ***argv, const char *parameters_file, std::string prefix) {

    }

    void SolverILU2::SetMatrix(Sparse::Matrix &A, bool ModifiedPattern, bool OldPreconditioner) {
        if (matrix != NULL) {
            delete matrix;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
28
        }
29
        matrix = new Sparse::Matrix(A);
30
        info.PrepareMatrix(*matrix, parameters.GetParameterEnum("additive_schwartz_overlap"));
31
        solver->ReplaceMAT(*matrix);
Dmitry Bagaev's avatar
Dmitry Bagaev committed
32

33 34 35 36
        solver->RealParameter(":tau") = parameters.GetParameterReal("drop_tolerance");
        solver->RealParameter(":tau2") = parameters.GetParameterReal("reuse_tolerance");
        solver->EnumParameter(":scale_iters") = parameters.GetParameterEnum("rescale_iterations");
        solver->EnumParameter(":fill") = static_cast<INMOST_DATA_ENUM_TYPE>(parameters.GetParameterReal("fill_level"));
Dmitry Bagaev's avatar
Dmitry Bagaev committed
37

38
        if (sizeof(KSOLVER) == sizeof(BCGSL_solver)) {
39
            solver->EnumParameter("levels") = parameters.GetParameterEnum("gmres_substeps");
40 41
        }

Dmitry Bagaev's avatar
Dmitry Bagaev committed
42 43
        if (!solver->isInitialized()) {
            solver->Initialize();
Dmitry Bagaev's avatar
Dmitry Bagaev committed
44
        }
Dmitry Bagaev's avatar
Dmitry Bagaev committed
45 46 47
    }

    bool SolverILU2::Solve(Sparse::Vector &RHS, Sparse::Vector &SOL) {
48 49 50 51
        solver->EnumParameter("maxits") = parameters.GetParameterEnum("maximum_iterations");
        solver->RealParameter("rtol") = parameters.GetParameterReal("relative_tolerance");
        solver->RealParameter("atol") = parameters.GetParameterReal("absolute_tolerance");
        solver->RealParameter("divtol") = parameters.GetParameterReal("divergence_tolerance");
Dmitry Bagaev's avatar
Dmitry Bagaev committed
52 53 54 55 56 57 58 59 60

        return solver->Solve(RHS, SOL);
    }

    bool SolverILU2::Clear() {
        info.Clear();
        if (matrix != NULL) {
            delete matrix;
            matrix = NULL;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
61
        }
Dmitry Bagaev's avatar
Dmitry Bagaev committed
62 63 64
        if (solver != NULL) {
            delete solver;
            solver = NULL;
Dmitry Bagaev's avatar
Dmitry Bagaev committed
65
        }
Dmitry Bagaev's avatar
Dmitry Bagaev committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        return true;
    }

    bool SolverILU2::isMatrixSet() {
        return matrix != NULL;
    }

    const INMOST_DATA_ENUM_TYPE SolverILU2::Iterations() const {
        return solver->GetIterations();
    }

    const INMOST_DATA_REAL_TYPE SolverILU2::Residual() const {
        return solver->GetResidual();
    }

    const std::string SolverILU2::ReturnReason() const {
        return solver->GetReason();
    }

    const std::string SolverILU2::SolverName() const {
        return "inner_ilu2";
    }

    void SolverILU2::Finalize() {

    }

    SolverILU2::~SolverILU2() {
        Clear();
    }
Dmitry Bagaev's avatar
Dmitry Bagaev committed
96 97

}