Skip to content
Snippets Groups Projects
Commit afc00b21 authored by Norihiro Watanabe's avatar Norihiro Watanabe
Browse files

remove abstract class MathLib::LinearSolver

parent ebda2c3a
No related branches found
No related tags found
No related merge requests found
......@@ -358,8 +358,8 @@ void ProjectData::parseLinearSolvers(BaseLib::ConfigTree const& config)
BaseLib::insertIfKeyUniqueElseError(
_linear_solvers,
name,
MathLib::createLinearSolver<GlobalMatrix, GlobalVector,
GlobalLinearSolver>(&conf),
std::unique_ptr<GlobalLinearSolver>(
new GlobalLinearSolver("", &conf)),
"The linear solver name is not unique");
}
}
......
......@@ -195,7 +195,7 @@ private:
std::unique_ptr<TimeLoop> _time_loop;
std::map<std::string,
std::unique_ptr<MathLib::LinearSolver<GlobalMatrix, GlobalVector>>>
std::unique_ptr<GlobalLinearSolver>>
_linear_solvers;
using NonlinearSolver = NumLib::NonlinearSolverBase;
......
......@@ -13,7 +13,6 @@
#include <vector>
#include "BaseLib/ConfigTree.h"
#include "MathLib/LinAlg/LinearSolver.h"
#include "EigenOption.h"
namespace MathLib
......@@ -25,7 +24,6 @@ class EigenVector;
class EigenLinearSolverBase;
class EigenLinearSolver final
: public LinearSolver<EigenMatrix, EigenVector>
{
public:
/**
......@@ -56,7 +54,7 @@ public:
*/
EigenOption &getOption() { return _option; }
bool solve(EigenMatrix &A, EigenVector& b, EigenVector &x) override;
bool solve(EigenMatrix &A, EigenVector& b, EigenVector &x);
protected:
EigenOption _option;
......
......@@ -15,7 +15,6 @@
#include <lis.h>
#include "BaseLib/ConfigTree.h"
#include "MathLib/LinAlg/LinearSolver.h"
#include "MathLib/LinAlg/Lis/LisOption.h"
namespace MathLib
......@@ -28,7 +27,6 @@ class EigenMatrix;
* Linear solver using Lis library with Eigen matrix and vector objects
*/
class EigenLisLinearSolver final
: public LinearSolver<EigenMatrix, EigenVector>
{
public:
/**
......@@ -47,7 +45,7 @@ public:
*/
void setOption(const LisOption &option) { _lis_option = option; }
bool solve(EigenMatrix &A, EigenVector& b, EigenVector &x) override;
bool solve(EigenMatrix &A, EigenVector& b, EigenVector &x);
private:
LisOption _lis_option;
......
/**
* \copyright
* Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "LinearSolver.h"
namespace
{
//! Describes the default linear solver type to be used together with the given
//! \c Matrix and \c Vector types.
template<typename Matrix, typename Vector>
struct DefaultLinearSolver;
}
namespace MathLib
{
template <typename Matrix, typename Vector, typename Solver>
std::unique_ptr<LinearSolver<Matrix, Vector>> createLinearSolver(
BaseLib::ConfigTree const* const config)
{
using AbstractLS = LinearSolver<Matrix, Vector>;
return std::unique_ptr<AbstractLS>{new Solver{"", config}};
}
template <typename Matrix, typename Vector>
std::unique_ptr<LinearSolver<Matrix, Vector>> createLinearSolver(
BaseLib::ConfigTree const* const config)
{
using Solver = typename DefaultLinearSolver<Matrix, Vector>::Solver;
return createLinearSolver<Matrix, Vector, Solver>(config);
}
} // MathLib
// specializes the DefaultLinearSolver<> class
#define DEFAULT_LINEAR_SOLVER(MAT, VEC, SLV) \
namespace { \
template<> struct DefaultLinearSolver<MAT, VEC> { \
using Solver = SLV; \
};}
// does explicit template instantiation of createLinearSolver<>()
#define CREATE_LINEAR_SOLVER(MAT, VEC, SLV) \
namespace MathLib { \
template std::unique_ptr<LinearSolver<MAT, VEC> > \
createLinearSolver<MAT, VEC, SLV>(BaseLib::ConfigTree const*const); \
template std::unique_ptr<LinearSolver<MAT, VEC> > \
createLinearSolver<MAT, VEC>(BaseLib::ConfigTree const*const); \
}
// both of the above
#define SPECIALIZE_CREATE_LINEAR_SOLVER(MAT, VEC, SLV) \
DEFAULT_LINEAR_SOLVER(MAT, VEC, SLV) \
CREATE_LINEAR_SOLVER(MAT, VEC, SLV)
#ifdef OGS_USE_EIGEN
#include <Eigen/QR>
namespace MathLib
{
//! Matches Eigen solvers to our interface.
class EigenDenseSolverWrapper
: public LinearSolver<Eigen::MatrixXd, Eigen::VectorXd>
{
public:
EigenDenseSolverWrapper(std::string const&, BaseLib::ConfigTree const*const) {}
bool solve(Eigen::MatrixXd &A, Eigen::VectorXd &b, Eigen::VectorXd &x) override
{
_solver.compute(A);
x = _solver.solve(b);
return true; // TODO add checks
}
private:
Eigen::ColPivHouseholderQR<Eigen::MatrixXd> _solver;
};
} // namespace MathLib
SPECIALIZE_CREATE_LINEAR_SOLVER(Eigen::MatrixXd, Eigen::VectorXd,
MathLib::EigenDenseSolverWrapper)
#endif
#ifdef USE_LIS
#include "MathLib/LinAlg/EigenLis/EigenLisLinearSolver.h"
SPECIALIZE_CREATE_LINEAR_SOLVER(MathLib::EigenMatrix, MathLib::EigenVector,
MathLib::EigenLisLinearSolver)
#elif defined(USE_PETSC)
#include "MathLib/LinAlg/PETSc/PETScLinearSolver.h"
SPECIALIZE_CREATE_LINEAR_SOLVER(MathLib::PETScMatrix, MathLib::PETScVector,
MathLib::PETScLinearSolver)
#elif defined(OGS_USE_EIGEN)
#include "MathLib/LinAlg/Eigen/EigenLinearSolver.h"
SPECIALIZE_CREATE_LINEAR_SOLVER(MathLib::EigenMatrix, MathLib::EigenVector,
MathLib::EigenLinearSolver)
#endif
/**
* \copyright
* Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#ifndef MATHLIB_LINEAR_SOLVER_H
#define MATHLIB_LINEAR_SOLVER_H
#include <memory>
namespace BaseLib
{
class ConfigTree;
}
namespace MathLib
{
//! Basic linear solver interface.
template<typename Matrix, typename Vector>
class LinearSolver
{
public:
/*! Solves the linear equation system \f$ Ax=b \f$ for \f$x \f$
*
* \retval true if the system has been successfully solved
* \retval false otherwise
*/
virtual bool solve(Matrix& A, Vector& b, Vector& x) = 0;
virtual ~LinearSolver() = default;
};
// TODO also pass a name argument, or done within config?
/*! Creates a new linear solver instance.
*
* \tparam Matrix the matrix type used in the equation system.
* \tparam Vector the vector type used in the equation system.
* \tparam Solver the type of the linear solver to be created.
*
* \param config configuration options.
*/
template<typename Matrix, typename Vector, typename Solver>
std::unique_ptr<LinearSolver<Matrix, Vector> >
createLinearSolver(BaseLib::ConfigTree const*const config);
/*! Creates a new linear solver instance; this method creates
* a linear solver of the default type for the given
* \c Matrix and \c Vector types.
*
* Said default type is spcified in the LinearSolver.cpp file.
*
* This method is an easy way to generate a linear solver, e.g.,
* in tests.
*
* \tparam Matrix the matrix type used in the equation system.
* \tparam Vector the vector type used in the equation system.
*
* \param config configuration options.
*/
template<typename Matrix, typename Vector>
std::unique_ptr<LinearSolver<Matrix, Vector> >
createLinearSolver(BaseLib::ConfigTree const*const config);
} // namespace MathLib
#endif // MATHLIB_LINEAR_SOLVER_H
......@@ -21,7 +21,6 @@
#include <lis.h>
#include "BaseLib/ConfigTree.h"
#include "MathLib/LinAlg/LinearSolver.h"
#include "LisOption.h"
......@@ -36,7 +35,6 @@ class LisVector;
*
*/
class LisLinearSolver final
: public LinearSolver<LisMatrix, LisVector>
{
public:
/**
......@@ -56,7 +54,7 @@ public:
*/
void setOption(const LisOption &option) { _lis_option = option; }
bool solve(LisMatrix& A, LisVector &b, LisVector &x) override;
bool solve(LisMatrix& A, LisVector &b, LisVector &x);
private:
LisOption _lis_option;
......
......@@ -24,7 +24,6 @@
#include <logog/include/logog.hpp>
#include "BaseLib/ConfigTree.h"
#include "MathLib/LinAlg/LinearSolver.h"
#include "PETScMatrix.h"
#include "PETScVector.h"
......@@ -40,7 +39,6 @@ namespace MathLib
command-line options, because the former are processed at a later stage.
*/
class PETScLinearSolver
: public LinearSolver<PETScMatrix, PETScVector>
{
public:
......@@ -64,7 +62,7 @@ class PETScLinearSolver
}
// TODO check if some args in LinearSolver interface can be made const&.
bool solve(PETScMatrix& A, PETScVector &b, PETScVector &x) override;
bool solve(PETScMatrix& A, PETScVector &b, PETScVector &x);
/// Get number of iterations.
PetscInt getNumberOfIterations() const
......
......@@ -280,7 +280,7 @@ bool NonlinearSolver<NonlinearSolverTag::Newton>::solve(
}
std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
createNonlinearSolver(MathLib::LinearSolver<GlobalMatrix, GlobalVector>& linear_solver,
createNonlinearSolver(GlobalLinearSolver& linear_solver,
BaseLib::ConfigTree const& config)
{
using AbstractNLS = NonlinearSolverBase;
......
......@@ -79,7 +79,6 @@ class NonlinearSolver<NonlinearSolverTag::Newton> final
public:
//! Type of the nonlinear equation system to be solved.
using System = NonlinearSystem<NonlinearSolverTag::Newton>;
using LinearSolver = MathLib::LinearSolver<GlobalMatrix, GlobalVector>;
/*! Constructs a new instance.
*
......@@ -89,7 +88,7 @@ public:
* \param maxiter the maximum number of iterations used to solve the
* equation.
*/
explicit NonlinearSolver(LinearSolver& linear_solver, double const tol,
explicit NonlinearSolver(GlobalLinearSolver& linear_solver, double const tol,
const unsigned maxiter)
: _linear_solver(linear_solver), _tol(tol), _maxiter(maxiter)
{
......@@ -104,7 +103,7 @@ public:
postIterationCallback) override;
private:
LinearSolver& _linear_solver;
GlobalLinearSolver& _linear_solver;
System* _equation_system = nullptr;
const double _tol; //!< tolerance of the solver
......@@ -131,7 +130,6 @@ class NonlinearSolver<NonlinearSolverTag::Picard> final
public:
//! Type of the nonlinear equation system to be solved.
using System = NonlinearSystem<NonlinearSolverTag::Picard>;
using LinearSolver = MathLib::LinearSolver<GlobalMatrix, GlobalVector>;
/*! Constructs a new instance.
*
......@@ -141,7 +139,7 @@ public:
* \param maxiter the maximum number of iterations used to solve the
* equation.
*/
explicit NonlinearSolver(LinearSolver& linear_solver, double const tol,
explicit NonlinearSolver(GlobalLinearSolver& linear_solver, double const tol,
const unsigned maxiter)
: _linear_solver(linear_solver), _tol(tol), _maxiter(maxiter)
{
......@@ -156,7 +154,7 @@ public:
postIterationCallback) override;
private:
LinearSolver& _linear_solver;
GlobalLinearSolver& _linear_solver;
System* _equation_system = nullptr;
const double _tol; //!< tolerance of the solver
......@@ -180,7 +178,7 @@ private:
*/
std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
createNonlinearSolver(
MathLib::LinearSolver<GlobalMatrix, GlobalVector>& linear_solver,
GlobalLinearSolver& linear_solver,
BaseLib::ConfigTree const& config);
//! @}
......
......@@ -28,7 +28,6 @@ class TimeLoopSingleODE final
{
public:
using TDiscODESys = TimeDiscretizedODESystemBase<NLTag>;
using LinearSolver = MathLib::LinearSolver<GlobalMatrix, GlobalVector>;
using NLSolver = NonlinearSolver<NLTag>;
/*! Constructs an new instance.
......@@ -39,7 +38,7 @@ public:
* \param nonlinear_solver The solver to be used to resolve nonlinearities.
*/
TimeLoopSingleODE(TDiscODESys& ode_sys,
std::unique_ptr<LinearSolver>&& linear_solver,
std::unique_ptr<GlobalLinearSolver>&& linear_solver,
std::unique_ptr<NLSolver>&& nonlinear_solver)
: _ode_sys(ode_sys),
_linear_solver(std::move(linear_solver)),
......@@ -68,7 +67,7 @@ public:
private:
TDiscODESys& _ode_sys;
std::unique_ptr<LinearSolver> _linear_solver;
std::unique_ptr<GlobalLinearSolver> _linear_solver;
std::unique_ptr<NLSolver> _nonlinear_solver;
};
......
......@@ -29,7 +29,6 @@ class TestOutput
{
public:
using TimeDisc = NumLib::TimeDiscretization;
using LinearSolver = MathLib::LinearSolver<Matrix, Vector>;
using NLSolver = NumLib::NonlinearSolver<NLTag>;
explicit TestOutput(const char* name)
......@@ -51,8 +50,8 @@ public:
NumLib::TimeDiscretizedODESystem<ODE_::ODETag, NLTag>
ode_sys(ode, timeDisc);
auto linear_solver = MathLib::createLinearSolver<Matrix, Vector>(nullptr);
auto linear_solver = std::unique_ptr<GlobalLinearSolver>{
new GlobalLinearSolver{"", nullptr}};
std::unique_ptr<NLSolver> nonlinear_solver(
new NLSolver(*linear_solver, _tol, _maxiter));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment