Пример #1
0
PETScLinearSolver::PETScLinearSolver(const std::string /*prefix*/,
                                     BaseLib::ConfigTree const* const option)
{
    // Insert options into petsc database. Default options are given in the
    // string below.
    std::string petsc_options =
        "-ksp_type cg -pc_type bjacobi -ksp_rtol 1e-16 -ksp_max_it 10000";

    std::string prefix;

    if (option)
    {
        ignoreOtherLinearSolvers(*option, "petsc");

        //! \ogs_file_param{prj__linear_solvers__linear_solver__petsc}
        if (auto const subtree = option->getConfigSubtreeOptional("petsc"))
        {
            if (auto const parameters =
                    //! \ogs_file_param{prj__linear_solvers__linear_solver__petsc__parameters}
                subtree->getConfigParameterOptional<std::string>("parameters"))
            {
                petsc_options = *parameters;
            }

            if (auto const pre =
                    //! \ogs_file_param{prj__linear_solvers__linear_solver__petsc__prefix}
                subtree->getConfigParameterOptional<std::string>("prefix"))
            {
                if (!pre->empty())
                    prefix = *pre + "_";
            }
        }
    }
#if PETSC_VERSION_LT(3, 7, 0)
    PetscOptionsInsertString(petsc_options.c_str());
#else
    PetscOptionsInsertString(nullptr, petsc_options.c_str());
#endif

    KSPCreate(PETSC_COMM_WORLD, &_solver);

    KSPGetPC(_solver, &_pc);

    if (!prefix.empty())
    {
        KSPSetOptionsPrefix(_solver, prefix.c_str());
    }

    KSPSetInitialGuessNonzero(_solver, PETSC_TRUE);
    KSPSetFromOptions(_solver);  // set run-time options
}
Пример #2
0
void EigenLinearSolver::setOption(BaseLib::ConfigTree const& option)
{
    ignoreOtherLinearSolvers(option, "eigen");
    //! \ogs_file_param{linear_solver__eigen}
    auto const ptSolver = option.getConfigSubtreeOptional("eigen");
    if (!ptSolver)
        return;

    if (auto solver_type =
            //! \ogs_file_param{linear_solver__eigen__solver_type}
            ptSolver->getConfigParameterOptional<std::string>("solver_type")) {
        _option.solver_type = _option.getSolverType(*solver_type);
    }
    if (auto precon_type =
            //! \ogs_file_param{linear_solver__eigen__precon_type}
            ptSolver->getConfigParameterOptional<std::string>("precon_type")) {
        _option.precon_type = _option.getPreconType(*precon_type);
    }
    if (auto error_tolerance =
            //! \ogs_file_param{linear_solver__eigen__error_tolerance}
            ptSolver->getConfigParameterOptional<double>("error_tolerance")) {
        _option.error_tolerance = *error_tolerance;
    }
    if (auto max_iteration_step =
            //! \ogs_file_param{linear_solver__eigen__max_iteration_step}
            ptSolver->getConfigParameterOptional<int>("max_iteration_step")) {
        _option.max_iterations = *max_iteration_step;
    }
    if (auto scaling =
            //! \ogs_file_param{linear_solver__eigen__scaling}
            ptSolver->getConfigParameterOptional<bool>("scaling")) {
#ifdef USE_EIGEN_UNSUPPORTED
        _option.scaling = *scaling;
#else
        OGS_FATAL(
            "The code is not compiled with the Eigen unsupported modules. "
            "scaling is not available.");
#endif
    }
}