KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
amesos2_solver.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Philipp Bucher (https://github.com/philbucher)
11 //
12 
13 #if !defined (KRATOS_AMESOS2_SOLVER_H_INCLUDED)
14 #define KRATOS_AMESOS2_SOLVER_H_INCLUDED
15 
16 // System includes
17 #include <unordered_map>
18 
19 // External includes
20 #include <Teuchos_RCP.hpp>
21 #include "Amesos2.hpp"
22 
23 // Project includes
24 #include "includes/define.h"
27 
28 
29 namespace Kratos
30 {
33 
35 
41 template< class TSparseSpaceType, class TDenseSpaceType,
42  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
43 class Amesos2Solver : public LinearSolver< TSparseSpaceType,
44  TDenseSpaceType, TReordererType>
45 {
46 public:
49 
52 
54 
56 
58 
62 
65  {
66  Parameters default_settings( R"({
67  "solver_type" : "amesos2",
68  "amesos2_solver_type" : "amesos2_klu2",
69  "trilinos_amesos2_parameter_list" : { }
70  } )" );
71 
72  // map from Kratos names to Trilinos internal Amesos2 names
73  std::unordered_map<std::string, std::string> kratos_to_amesos2_names = {
74  {"klu2", "amesos2_klu2"},
75  {"basker", "basker"},
76  {"super_lu_dist2", "amesos2_superludist"},
77  {"mumps2", "amesos2_mumps"}
78  };
79 
80  const std::string solver_type = settings["solver_type"].GetString();
81 
82  auto iter_amesos2_name = kratos_to_amesos2_names.find(solver_type);
83 
84  if (iter_amesos2_name != kratos_to_amesos2_names.end()) {
85  if (settings.Has("amesos2_solver_type")) {
86  KRATOS_INFO("Amesos2-Solver") << "Ignoring setting \"amesos2_solver_type\"" << std::endl;
87  } else {
88  settings.AddEmptyValue("amesos2_solver_type");
89  }
90  settings["amesos2_solver_type"].SetString(iter_amesos2_name->second);
91 
92  } else if (solver_type == "amesos2") {
93  // do nothing here.
94  // Leave full control to the user through the "trilinos_amesos2_parameter_list"
95  // and the "amesos2_solver_type"
96  }
97 
98  else {
99  KRATOS_ERROR << "The solver type specified: \"" << solver_type << "\" is not supported";
100  }
101 
102  settings.ValidateAndAssignDefaults(default_settings);
103 
104  //assign the amesos parameter list, which may contain parameters IN TRILINOS INTERNAL FORMAT to mParameterList
105  //NOTE: this will OVERWRITE PREVIOUS SETTINGS TO GIVE FULL CONTROL
106  TrilinosSolverUtilities::SetTeuchosParameters(settings["trilinos_amesos2_parameter_list"], mParameterList);
107 
108  mSolverName = settings["amesos2_solver_type"].GetString();
109 
110  KRATOS_ERROR_IF_NOT(HasSolver(mSolverName)) << "attempting to use Amesos solver \"" << mSolverName
111  << "\" unfortunately the current compilation of Trilinos does not include it" << std::endl;
112  }
113 
115  Amesos2Solver(const std::string& SolverName, Teuchos::ParameterList& rParameterList)
116  {
117  mParameterList = rParameterList;
118  mSolverName = SolverName;
119 
120  KRATOS_ERROR_IF_NOT(HasSolver(mSolverName)) << "attempting to use Amesos solver \"" << mSolverName
121  << "\" unfortunately the current compilation of Trilinos does not include it" << std::endl;
122  }
123 
125  Amesos2Solver(const Amesos2Solver& Other) = delete;
126 
128  ~Amesos2Solver() override = default;
129 
133 
135  Amesos2Solver& operator=(const Amesos2Solver& Other) = delete;
136 
140 
149  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
150  {
151  // implemented following this example:
152  // https://github.com/trilinos/Trilinos/blob/master/packages/amesos2/example/SimpleSolve_File.cpp
153 
154  KRATOS_TRY
155 
156  // it is not strictly necessary to use rcp, however skipping it could not yet be achieved
157  // in any case doesn't hurt to have it
158  using Teuchos::RCP;
159  using Teuchos::rcp;
160 
161  // Amesos2 is missing specializations for FE-matrices and vectors
162  // hence here we need to use the corresponding baseclasses
163  // see "Amesos2_MatrixTraits.hpp" and "Amesos2_VectorTraits.hpp"
164  using MAT = Epetra_CrsMatrix; // baseclass of "SparseMatrixType" aka "Epetra_FECrsMatrix"
165  using MV = Epetra_MultiVector; // baseclass of "VectorType" aka "Epetra_FEVector
166 
167  RCP<Amesos2::Solver<MAT,MV> > solver;
168 
169  RCP<SparseMatrixType> rcp_A = rcp(&rA, false);
170  RCP<VectorType> rcp_X = rcp(&rX, false);
171  RCP<VectorType> rcp_B = rcp(&rB, false);
172  RCP<Teuchos::ParameterList> rcp_params = rcp(&mParameterList, false);
173 
174  solver = Amesos2::create<MAT,MV>(mSolverName, rcp_A, rcp_X, rcp_B);
175 
176  solver->setParameters(rcp_params);
177 
178  solver->symbolicFactorization().numericFactorization().solve();
179 
180  return true;
181 
182  KRATOS_CATCH("");
183  }
184 
194  {
195  return false;
196  }
197 
201 
208  static bool HasSolver(const std::string& rAmesosSolverName)
209  {
210  return Amesos2::query(rAmesosSolverName);
211  }
212 
216 
218  void PrintInfo(std::ostream& rOStream) const override
219  {
220  rOStream << "Trilinos Amesos2-Solver";
221  }
222 
224 
225 private:
228 
229  Teuchos::ParameterList mParameterList;
230  std::string mSolverName;
231 
233 
234 }; // Class Amesos2Solver
235 
237 template<class TSparseSpaceType, class TDenseSpaceType, class TReordererType>
238 inline std::ostream& operator << (std::ostream& rOStream,
239  const Amesos2Solver<TSparseSpaceType,
240  TDenseSpaceType, TReordererType>& rThis)
241 {
242  rThis.PrintInfo(rOStream);
243  rOStream << std::endl;
244  rThis.PrintData(rOStream);
245 
246  return rOStream;
247 }
248 
249 } // namespace Kratos.
250 
251 #endif // KRATOS_AMESOS2_SOLVER_H_INCLUDED defined
Wrapper for Trilinos-Amesos2 Direct Solvers.
Definition: amesos2_solver.h:45
static bool HasSolver(const std::string &rAmesosSolverName)
Definition: amesos2_solver.h:208
Amesos2Solver(const std::string &SolverName, Teuchos::ParameterList &rParameterList)
Constructor with solver-name and Teuchos::ParameterList.
Definition: amesos2_solver.h:115
TSparseSpaceType::MatrixType SparseMatrixType
Definition: amesos2_solver.h:53
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: amesos2_solver.h:149
TDenseSpaceType::MatrixType DenseMatrixType
Definition: amesos2_solver.h:57
KRATOS_CLASS_POINTER_DEFINITION(Amesos2Solver)
Pointer definition of Amesos2Solver.
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: amesos2_solver.h:193
~Amesos2Solver() override=default
Destructor.
Amesos2Solver & operator=(const Amesos2Solver &Other)=delete
Assignment operator.
TSparseSpaceType::VectorType VectorType
Definition: amesos2_solver.h:55
Amesos2Solver(const Amesos2Solver &Other)=delete
Copy constructor.
Amesos2Solver(Parameters settings)
Constructor with Parameters.
Definition: amesos2_solver.h:64
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: amesos2_solver.h:218
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void SetString(const std::string &rValue)
This method sets the string contained in the current Parameter.
Definition: kratos_parameters.cpp:811
Parameters AddEmptyValue(const std::string &rEntry)
This method adds an empty parameter.
Definition: kratos_parameters.cpp:471
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
bool Has(const std::string &rEntry) const
This method checks if the Parameter contains a certain entry.
Definition: kratos_parameters.cpp:520
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_INFO(label)
Definition: logger.h:250
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
void SetTeuchosParameters(const Parameters rSettings, Teuchos::ParameterList &rParameterlist)
Definition: trilinos_solver_utilities.cpp:22
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
solver
Definition: script.py:98