Commit 5dd7d258 authored by Dmitry Bagaev's avatar Dmitry Bagaev

Solver refactoring completed

1) Condest added to final solver interface
2) SolverMaster hided from user
3) Examples fixed with new Solver Class
4) Examples using LLVM coding standarts
parent c35833a7
This diff is collapsed.
This diff is collapsed.
#Actually Solver class in refactoring state right now
#All examples will be refactored too after the main stage
if(USE_MESH)
#add_subdirectory(DrawGrid)
#add_subdirectory(OldDrawGrid)
#add_subdirectory(GridGen)
add_subdirectory(DrawGrid)
add_subdirectory(OldDrawGrid)
add_subdirectory(GridGen)
endif(USE_MESH)
if(USE_SOLVER)
#add_subdirectory(DrawMatrix)
add_subdirectory(DrawMatrix)
add_subdirectory(MatSolve)
endif(USE_SOLVER)
if(USE_SOLVER AND USE_MESH)
#add_subdirectory(FVDiscr)
#add_subdirectory(Solver)
add_subdirectory(FVDiscr)
add_subdirectory(Solver)
endif(USE_SOLVER AND USE_MESH)
if(USE_AUTODIFF AND USE_SOLVER AND USE_MESH)
#add_subdirectory(ADFVDiscr)
#add_subdirectory(ADMFD)
add_subdirectory(ADFVDiscr)
add_subdirectory(ADMFD)
endif(USE_AUTODIFF AND USE_SOLVER AND USE_MESH)
#add_subdirectory(OctreeCutcell)
\ No newline at end of file
project(DrawGrid)
set(SOURCE main.cpp
rotate.cpp
rotate.h
my_glut.h)
rotate.cpp
rotate.h
my_glut.h)
find_package(OpenGL)
find_package(GLUT)
if(OPENGL_FOUND)
if(GLUT_FOUND)
message("linking DrawGrid with GLUT and OpenGL")
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${GLUT_INCLUDE_DIR})
add_executable(DrawGrid ${SOURCE})
target_link_libraries(DrawGrid inmost ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES})
if(USE_MPI)
target_link_libraries(DrawGrid ${MPI_LIBRARIES})
target_link_libraries(DrawGrid ${MPI_LIBRARIES})
if(MPI_LINK_FLAGS)
set_target_properties(DrawGrid PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
endif()
endif()
endif(USE_MPI)
install(TARGETS DrawGrid EXPORT inmost-targets RUNTIME DESTINATION bin)
else(GLUT_FOUND)
message("GLUT not found")
message("GLUT not found, not building DrawGrid")
endif(GLUT_FOUND)
else(OPENGL_FOUND)
message("OpenGL not found")
message("OpenGL not found, not building DrawGrid")
endif(OPENGL_FOUND)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -23,10 +23,6 @@ int main(int argc, char **argv) {
MPI_Comm_size(MPI_COMM_WORLD, &processorsCount); // Get the total number of processors used
#endif
if (processRank == 0) {
std::cout << "Starting MatSolve2" << std::endl;
}
{
std::string matrixFileName = "";
std::string vectorBFileName = "";
......@@ -59,7 +55,7 @@ int main(int argc, char **argv) {
std::cout << "-t, --type <Solver type name>" << std::endl;
std::cout << " Available solvers:" << std::endl;
Solver::Initialize(NULL, NULL, NULL);
std::vector<std::string> availableSolvers = SolverMaster::getAvailableSolvers();
std::vector<std::string> availableSolvers = Solver::getAvailableSolvers();
for (solvers_names_iterator_t it = availableSolvers.begin(); it != availableSolvers.end(); it++) {
std::cout << " " << *it << std::endl;
}
......@@ -157,7 +153,7 @@ int main(int argc, char **argv) {
// Initialize the linear solver in accordance with args
Solver::Initialize(&argc, &argv, parametersFound ? parametersFileName.c_str() : NULL);
if (!SolverMaster::isSolverAvailable(solverName)) {
if (!Solver::isSolverAvailable(solverName)) {
if (processRank == 0) {
std::cout << "Solver " << solverName << " is not available" << std::endl;
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -13,7 +13,6 @@ set(HEADER
${CMAKE_CURRENT_SOURCE_DIR}/inmost_mesh.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver_interface.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver_master.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_partitioner.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_autodiff.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_expression.h
......
......@@ -7,7 +7,6 @@
#include "inmost_dense.h"
#include "inmost_solver.h"
#include "inmost_solver_interface.h"
#include "inmost_solver_master.h"
#include "inmost_partitioner.h"
#include "inmost_variable.h"
#include "inmost_nonlinear.h"
......
......@@ -259,11 +259,31 @@ namespace INMOST {
/// @see Sparse::Solve
const std::string ReturnReason() const;
/// Computes the smallest and the largest eigenvalue with the power method.
/// Requires SetMatrix to be called to compute the preconditioner.
/// Currently works for internal methods only, since it uses internal matrix-vector multiplication.
/// Largest eigenvalue: vprev = 0; v = rand(); while( |v|-|vprev| > tol ) {vprev = v; v = A*v; v /= |v|;}
/// lambda_max = |v|;
/// Smallest eigenvalue: vprev = 0; v = rand(); while( |v|-|vprev| > tol ){vprev = v; solve(A*v = v); v /= |v|;}
/// lambda_min = 1.0/|v|;
/// See answer by Blair Perot in:
/// https://www.researchgate.net/post/What_is_the_best_way_to_estimate_the_condition_number_of_a_sparse_matrix.
/// @param tol Tolerance used for power series.
/// @param maxits Maximum number of iterations allowed.
/// @return Condition number or 1.0e100 if not converged.
INMOST_DATA_REAL_TYPE Condest(INMOST_DATA_REAL_TYPE tol, INMOST_DATA_ENUM_TYPE maxits = 100);
static bool isSolverAvailable(std::string name);
static std::vector<std::string> getAvailableSolvers();
~Solver();
private:
static std::string parseDatabase(std::string solverName);
};
typedef std::vector<std::string>::iterator solvers_names_iterator_t;
}
#endif
......
......@@ -14,6 +14,7 @@ namespace INMOST {
std::string value;
public:
SolverParameter() {};
SolverParameter(std::string value) : value(value) {};
template<typename T>
......@@ -30,6 +31,7 @@ namespace INMOST {
std::map<std::string, SolverParameter> parameters;
public:
SolverParameters();
void SetParameter(std::string name, std::string value);
const SolverParameter GetParameter(std::string name) const;
......@@ -81,6 +83,10 @@ namespace INMOST {
virtual void Finalize() = 0;
virtual const INMOST_DATA_REAL_TYPE Condest(INMOST_DATA_REAL_TYPE tol, INMOST_DATA_ENUM_TYPE maxiter) {
throw INMOST::SolverUnsupportedOperation;
};
virtual ~SolverInterface() {};
......
......@@ -7,6 +7,10 @@ set(SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/sparse.cpp
)
set(HEADER
${HEADER}
${CMAKE_CURRENT_SOURCE_DIR}/SolverMaster.h)
add_subdirectory(solver_inner)
add_subdirectory(solver_fcbiilu2)
add_subdirectory(solver_k3biilu2)
......
#include "inmost.h"
#include "SolverMaster.h"
namespace INMOST {
......
#ifndef INMOST_SOLVER_FACTORY
#define INMOST_SOLVER_FACTORY
#ifndef INMOST_SOLVER_MASTER
#define INMOST_SOLVER_MASTER
#include <inmost_solver.h>
#include <inmost_solver_interface.h>
namespace INMOST {
......@@ -25,7 +28,7 @@ namespace INMOST {
class SolverMaster {
private:
static std::map<std::string, SolverBaseFactory *> solvers;
public:
template<class T>
static void registerSolver(std::string name) {
solvers.insert(std::make_pair(name, new SolverCreateFactory<T>));
......@@ -38,11 +41,19 @@ namespace INMOST {
static std::vector<std::string> getAvailableSolvers();
static bool isSolverAvailable(std::string name);
friend Solver::Solver(std::string solverName, std::string prefix, INMOST_MPI_Comm _comm);
friend Solver::Solver(const Solver &other);
friend void Solver::Initialize(int *argc, char ***argv, const char *database);
friend bool Solver::isSolverAvailable(std::string name);
friend std::vector<std::string> Solver::getAvailableSolvers();
};
typedef std::map<std::string, SolverBaseFactory *>::iterator solvers_map_iterator_t;
typedef std::vector<std::string>::iterator solvers_names_iterator_t;
}
#endif //INMOST_SOLVER_FACTORY
#endif //INMOST_SOLVER_MASTER
#include <inmost.h>
#include "SolverMaster.h"
#include "solver_inner/solver_ilu2/SolverILU2.h"
#include "solver_inner/solver_ddpqiluc2/SolverDDPQILUC2.h"
#include "solver_inner/solver_mptiluc/SolverMPTILUC.h"
#include "solver_inner/solver_mptilu2/SolverMPTILU2.h"
#if defined(USE_SOLVER_PETSC)
#include "solver_petsc/SolverPETSc.h"
#endif
#if defined(USE_SOLVER_TRILINOS)
#include "solver_trilinos/SolverTrilinos.h"
#include "solver_trilinos/solver_aztec/SolverTrilinosAztec.h"
#include "solver_trilinos/solver_belos/SolverTrilinosBelos.h"
#include "solver_trilinos/solver_ml/SolverTrilinosML.h"
#include "solver_trilinos/solver_ifpack/SolverTrilinosIfpack.h"
#endif
#if defined(USE_SOLVER_ANI)
#include "solver_ani/SolverANI.h"
#endif
#if defined(USE_SOLVER_SUPERLU)
......@@ -198,6 +205,19 @@ namespace INMOST {
return solver->ReturnReason();
}
INMOST_DATA_REAL_TYPE Solver::Condest(INMOST_DATA_REAL_TYPE tol, unsigned int maxits) {
if (!solver->isMatrixSet()) throw MatrixNotSetInSolver;
return solver->Condest(tol, maxits);
}
bool Solver::isSolverAvailable(std::string name) {
return SolverMaster::isSolverAvailable(name);
}
std::vector<std::string> Solver::getAvailableSolvers() {
return SolverMaster::getAvailableSolvers();
}
Solver::~Solver() {
solver->Finalize();
delete solver;
......
This diff is collapsed.
......@@ -40,6 +40,8 @@ namespace INMOST {
virtual const std::string SolverName() const = 0;
virtual const INMOST_DATA_REAL_TYPE Condest(INMOST_DATA_REAL_TYPE tol, INMOST_DATA_ENUM_TYPE maxiter);
virtual void Finalize();
virtual ~SolverInner();
......
......@@ -35,6 +35,18 @@ namespace INMOST {
}
}
bool SolverMPTILUC::Solve(Sparse::Vector &RHS, Sparse::Vector &SOL) {
solver->EnumParameter("maxits") = parameters.get<INMOST_DATA_ENUM_TYPE>("maximum_iterations");
solver->RealParameter("rtol") = parameters.get<INMOST_DATA_REAL_TYPE>("relative_tolerance");
solver->RealParameter("atol") = parameters.get<INMOST_DATA_REAL_TYPE>("absolute_tolerance");
solver->RealParameter("divtol") = parameters.get<INMOST_DATA_REAL_TYPE>("divergence_tolerance");
bool solve = solver->Solve(RHS, SOL);
parameters.SetParameter("condition_number_L", std::to_string(solver->RealParameter(":condition_number_L")));
parameters.SetParameter("condition_number_U", std::to_string(solver->RealParameter(":condition_number_U")));
return solve;
}
const std::string SolverMPTILUC::SolverName() const {
return "inner_mptiluc";
}
......
......@@ -15,6 +15,8 @@ namespace INMOST {
virtual void SetMatrix(Sparse::Matrix &A, bool ModifiedPattern, bool OldPreconditioner);
virtual bool Solve(Sparse::Vector &RHS, Sparse::Vector &SOL);
virtual const std::string SolverName() const;
virtual ~SolverMPTILUC();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment