AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
UmfpackRunner.hpp
1 #pragma once
2 
3 #ifdef HAVE_UMFPACK
4 
5 #include <algorithm>
6 #include <string>
7 
8 #include <boost/numeric/mtl/operation/two_norm.hpp>
9 #include <boost/numeric/mtl/interface/umfpack_solve.hpp>
10 
11 #include <amdis/linearalgebra/mtl/Traits.hpp>
12 #include <amdis/linearalgebra/RunnerInterface.hpp>
13 #include <amdis/linearalgebra/SolverInfo.hpp>
14 #include <amdis/Output.hpp>
15 
16 namespace AMDiS
17 {
27  template <class Mat, class Vec>
29  : public RunnerInterface<Mat,Vec>
30  {
31  using M = typename Mat::BaseMatrix;
32  using X = typename Vec::BaseVector;
33  using Y = typename Vec::BaseVector;
34 
35  using SolverType = mtl::mat::umfpack::solver<typename Mat::BaseMatrix>;
36 
37  public:
39  UmfpackRunner(std::string const& prefix)
40  {
41  Parameters::get(prefix + "->store symbolic", storeSymbolic_);
42  Parameters::get(prefix + "->symmetric strategy", symmetricStrategy_);
43  Parameters::get(prefix + "->alloc init", allocInit_);
44  }
45 
46  void init(M const& A) override
47  {
48  try {
49  if (bool(solver_) && storeSymbolic_)
50  solver_->update_numeric();
51  else
52  solver_ = std::make_shared<SolverType>(A, symmetricStrategy_, allocInit_);
53  } catch (mtl::mat::umfpack::error const& e) {
54  umfpack_error_exit("factorize", e.code);
55  }
56  }
57 
58  void exit() override {}
59 
61  int solve(M const& A, X& x, Y const& b, SolverInfo& solverInfo) override
62  {
63  test_exit(bool(solver_), "UmfpackRunner must be initialized.");
64  int code = 0;
65  try {
66  code = (*solver_)(x, b);
67  } catch (mtl::mat::umfpack::error& e) {
68  umfpack_error_exit("solve", e.code);
69  }
70 
71  solverInfo.setAbsResidual(two_norm(b - A*x));
72  return code;
73  }
74 
75  static void umfpack_error_exit(std::string solutionPhase, int code)
76  {
77  static std::map<int, std::string> error_message = {
78  {UMFPACK_OK, "UMFPACK was successful"},
79  {UMFPACK_WARNING_singular_matrix, "Matrix is singular. There are exact zeros on the diagonal."},
80  {UMFPACK_WARNING_determinant_underflow, "The determinant is nonzero, but smaller in magnitude than the smallest positive floating-point number."},
81  {UMFPACK_WARNING_determinant_overflow, "The determinant is larger in magnitude than the largest positive floating-point number (IEEE Inf)."},
82  {UMFPACK_ERROR_out_of_memory, "Not enough memory. The ANSI C malloc or realloc routine failed."},
83  {UMFPACK_ERROR_invalid_Numeric_object, "Invalid Numeric object."},
84  {UMFPACK_ERROR_invalid_Symbolic_object, "Invalid Symbolic object."},
85  {UMFPACK_ERROR_argument_missing, "Some required arguments are missing."},
86  {UMFPACK_ERROR_n_nonpositive, "The number of rows or columns of the matrix must be greater than zero."},
87  {UMFPACK_ERROR_invalid_matrix, "The matrix is invalid."},
88  {UMFPACK_ERROR_different_pattern, "The pattern of the matrix has changed between the symbolic and numeric factorization."},
89  {UMFPACK_ERROR_invalid_system, "The sys argument provided to one of the solve routines is invalid."},
90  {UMFPACK_ERROR_invalid_permutation, "The permutation vector provided as input is invalid."},
91  {UMFPACK_ERROR_file_IO, "Error in file IO"},
92  {UMFPACK_ERROR_ordering_failed, "The ordering method failed."},
93  {UMFPACK_ERROR_internal_error, "An internal error has occurred, of unknown cause."}
94  };
95 
96  error_exit("UMFPACK_ERROR({}, {}) = {}", solutionPhase, code, error_message[code]);
97  }
98 
99  protected:
100  std::shared_ptr<SolverType> solver_;
101 
102  bool storeSymbolic_ = false;
103  int symmetricStrategy_ = 0;
104  double allocInit_ = 0.7;
105  };
106 
107 } // end namespace AMDiS
108 
109 #endif // HAVE_UMFPACK
void setAbsResidual(double r)
Sets absResidual_.
Definition: SolverInfo.hpp:80
void error_exit(std::string const &str, Args &&... args)
print a message and exit
Definition: Output.hpp:142
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Definition: UmfpackRunner.hpp:28
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
void exit() override
Is called at the end of a solution procedure.
Definition: UmfpackRunner.hpp:58
int solve(M const &A, X &x, Y const &b, SolverInfo &solverInfo) override
Implements RunnerInterface::solveImpl()
Definition: UmfpackRunner.hpp:61
Definition: SolverInfo.hpp:11
UmfpackRunner(std::string const &prefix)
Constructor. Reads UMFPACK parameters from initfile.
Definition: UmfpackRunner.hpp:39
void init(M const &A) override
Is called at the beginning of a solution procedure.
Definition: UmfpackRunner.hpp:46
void test_exit(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message and exit
Definition: Output.hpp:163
Interface for Runner / Worker types used in solver classes.
Definition: RunnerInterface.hpp:11