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.
amesos_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: Riccardo Rossi
11 // Collaborator: Philipp Bucher
12 //
13 
14 #if !defined (KRATOS_AMESOS_SOLVER_H_INCLUDED)
15 #define KRATOS_AMESOS_SOLVER_H_INCLUDED
16 
17 // External includes
18 
19 // Project includes
20 #include "includes/define.h"
22 
23 // Amesos solver includes
24 #include "Amesos.h"
25 #include "Epetra_LinearProblem.h"
26 
27 namespace Kratos
28 {
31 
33 
39 template< class TSparseSpaceType, class TDenseSpaceType,
40  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
41 class AmesosSolver : public LinearSolver< TSparseSpaceType,
42  TDenseSpaceType, TReordererType>
43 {
44 public:
47 
50 
52 
54 
56 
60 
63  {
64  Parameters default_settings( R"({
65  "solver_type" : "amesos",
66  "amesos_solver_type" : "Amesos_Klu",
67  "trilinos_amesos_parameter_list" : { }
68  } )" );
69 
70  // choose solver-type
71  if (settings["solver_type"].GetString() == "klu") {
72  if (settings.Has("amesos_solver_type")) {
73  KRATOS_INFO("Amesos-Solver") << "Ignoring setting \"amesos_solver_type\"" << std::endl;
74  } else {
75  settings.AddEmptyValue("amesos_solver_type");
76  }
77  settings["amesos_solver_type"].SetString("Amesos_Klu");
78  }
79  else if (settings["solver_type"].GetString() == "super_lu_dist") {
80  if (settings.Has("amesos_solver_type")) {
81  KRATOS_INFO("Amesos-Solver") << "Ignoring setting \"amesos_solver_type\"" << std::endl;
82  }
83  else {
84  settings.AddEmptyValue("amesos_solver_type");
85  }
86  settings["amesos_solver_type"].SetString("Amesos_Superludist");
87  }
88  else if (settings["solver_type"].GetString() == "mumps") {
89  if (settings.Has("amesos_solver_type")) {
90  KRATOS_INFO("Amesos-Solver") << "Ignoring setting \"amesos_solver_type\"" << std::endl;
91  }
92  else {
93  settings.AddEmptyValue("amesos_solver_type");
94  }
95  settings["amesos_solver_type"].SetString("Amesos_Mumps");
96  }
97  else if (settings["solver_type"].GetString() == "amesos") {
98  // do nothing here. Leave full control to the user through the "trilinos_amesos_parameter_list"
99  // and the "amesos_solver_type"
100  }
101  else {
102  KRATOS_ERROR << "The solver type specified: \"" << settings["solver_type"].GetString() << "\" is not supported";
103  }
104 
105  settings.ValidateAndAssignDefaults(default_settings);
106 
107  //assign the amesos parameter list, which may contain parameters IN TRILINOS INTERNAL FORMAT to mParameterList
108  mParameterList = Teuchos::ParameterList();
109  for(auto it = settings["trilinos_amesos_parameter_list"].begin(); it != settings["trilinos_amesos_parameter_list"].end(); it++) {
110  if(it->IsString()) mParameterList.set(it.name(), it->GetString());
111  else if(it->IsInt()) mParameterList.set(it.name(), it->GetInt());
112  else if(it->IsBool()) mParameterList.set(it.name(), it->GetBool());
113  else if(it->IsDouble()) mParameterList.set(it.name(), it->GetDouble());
114  }
115 
116  mSolverName = settings["amesos_solver_type"].GetString();
117 
118  KRATOS_ERROR_IF_NOT(HasSolver(mSolverName)) << "attempting to use Amesos solver \"" << mSolverName
119  << "\" unfortunately the current compilation of Trilinos does not include it" << std::endl;
120  }
121 
123  AmesosSolver(const std::string& SolverName, Teuchos::ParameterList& rParameterList)
124  {
125  mParameterList = rParameterList;
126  mSolverName = SolverName;
127 
128  KRATOS_ERROR_IF_NOT(HasSolver(mSolverName)) << "attempting to use Amesos solver \"" << mSolverName
129  << "\" unfortunately the current compilation of Trilinos does not include it" << std::endl;
130  }
131 
133  AmesosSolver(const AmesosSolver& Other) = delete;
134 
136  ~AmesosSolver() override = default;
137 
141 
143  AmesosSolver& operator=(const AmesosSolver& Other) = delete;
144 
148 
157  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
158  {
159  KRATOS_TRY
160  rA.Comm().Barrier();
161  Epetra_LinearProblem linear_problem(&rA,&rX,&rB);
162  Amesos_BaseSolver* p_amesos_solver;
163  Amesos amesos_factory;
164  p_amesos_solver = amesos_factory.Create(mSolverName, linear_problem); // that the solver exists is checked in the constructor
165 
166  p_amesos_solver->SetParameters( mParameterList );
167 
168  p_amesos_solver->SymbolicFactorization();
169  p_amesos_solver->NumericFactorization();
170  p_amesos_solver->Solve();
171 
172  delete p_amesos_solver;
173 
174  rA.Comm().Barrier();
175 
176  return true;
177  KRATOS_CATCH("");
178  }
179 
189  {
190  return false;
191  }
192 
196 
202  static bool HasSolver(const std::string& rAmesosSolverName)
203  {
204  Amesos amesos_factory;
205  return amesos_factory.Query(rAmesosSolverName);
206  }
207 
211 
213  void PrintInfo(std::ostream& rOStream) const override
214  {
215  rOStream << "Trilinos Amesos-Solver";
216  }
217 
219 
220 private:
223 
224  Teuchos::ParameterList mParameterList;
225  std::string mSolverName;
226 
228 
229 }; // Class AmesosSolver
230 
232 template<class TSparseSpaceType, class TDenseSpaceType, class TReordererType>
233 inline std::ostream& operator << (std::ostream& rOStream,
234  const AmesosSolver<TSparseSpaceType,
235  TDenseSpaceType, TReordererType>& rThis)
236 {
237  rThis.PrintInfo(rOStream);
238  rOStream << std::endl;
239  rThis.PrintData(rOStream);
240 
241  return rOStream;
242 }
243 
244 } // namespace Kratos.
245 
246 #endif // KRATOS_AMESOS_SOLVER_H_INCLUDED defined
Wrapper for Trilinos-Amesos Direct Solvers.
Definition: amesos_solver.h:43
AmesosSolver(Parameters settings)
Constructor with Parameters.
Definition: amesos_solver.h:62
KRATOS_CLASS_POINTER_DEFINITION(AmesosSolver)
Pointer definition of AmesosSolver.
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: amesos_solver.h:213
AmesosSolver(const AmesosSolver &Other)=delete
Copy constructor.
TSparseSpaceType::VectorType VectorType
Definition: amesos_solver.h:53
AmesosSolver & operator=(const AmesosSolver &Other)=delete
Assignment operator.
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: amesos_solver.h:188
AmesosSolver(const std::string &SolverName, Teuchos::ParameterList &rParameterList)
Constructor with solver-name and Teuchos::ParameterList.
Definition: amesos_solver.h:123
static bool HasSolver(const std::string &rAmesosSolverName)
Definition: amesos_solver.h:202
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: amesos_solver.h:157
TDenseSpaceType::MatrixType DenseMatrixType
Definition: amesos_solver.h:55
~AmesosSolver() override=default
Destructor.
TSparseSpaceType::MatrixType SparseMatrixType
Definition: amesos_solver.h:51
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
iterator end()
This returns the end iterator.
Definition: kratos_parameters.cpp:969
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
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