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.
ml_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_MULTILEVEL_SOLVER_H_INCLUDED)
15 #define KRATOS_MULTILEVEL_SOLVER_H_INCLUDED
16 
17 // External includes
18 
19 // Project includes
20 #include "includes/define.h"
24 
25 //aztec solver includes
26 #include "AztecOO.h"
27 #include "Epetra_LinearProblem.h"
28 #include "ml_include.h"
29 #include "ml_MultiLevelPreconditioner.h"
30 #include "Teuchos_ParameterList.hpp"
31 
32 namespace Kratos
33 {
36 
38 
44 template< class TSparseSpaceType, class TDenseSpaceType,
45  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
46 class MultiLevelSolver : public LinearSolver< TSparseSpaceType,
47  TDenseSpaceType, TReordererType>
48 {
49 public:
52 
55 
57 
59 
61 
62  typedef typename BaseType::VectorType VectorType;
63 
65 
67 
69 
73 
76  {
77  const Parameters default_settings( R"( {
78  "solver_type" : "multi_level",
79  "tolerance" : 1.0e-6,
80  "max_iteration" : 200,
81  "max_levels" : 3,
82  "scaling" : false,
83  "reform_preconditioner_at_each_step" : true,
84  "symmetric" : false,
85  "verbosity" : 0,
86  "trilinos_aztec_parameter_list" : {},
87  "trilinos_ml_parameter_list" : {}
88  } )" );
89 
90  Settings.ValidateAndAssignDefaults(default_settings);
91 
92  //settings for the MultiLevel solver
93  mTolerance = Settings["tolerance"].GetDouble();
94  mMaxIterations = Settings["max_iteration"].GetInt();
95  mReformPrecAtEachStep = Settings["reform_preconditioner_at_each_step"].GetBool();
96 
97  //scaling settings
98  if (!Settings["scaling"].GetBool()) {
99  mScalingType = NoScaling;
100  }
101 
102  //assign the amesos parameter list, which may contain parameters IN TRILINOS INTERNAL FORMAT to mparameter_list
103  mAztecParameterList = Teuchos::ParameterList();
104  const int verbosity = Settings["verbosity"].GetInt();
105  if (verbosity == 0) {
106  mAztecParameterList.set("AZ_output", "AZ_none");
107  } else {
108  mAztecParameterList.set("AZ_output", verbosity);
109  }
110 
111  mMLParameterList = Teuchos::ParameterList();
112 
113  mMLParameterList.set("ML output", verbosity);
114  mMLParameterList.set("max levels", Settings["max_levels"].GetInt());
115  if (!Settings["symmetric"].GetBool()) {
116  ML_Epetra::SetDefaults("NSSA",mMLParameterList);
117  mMLParameterList.set("aggregation: type", "Uncoupled");
118  mAztecParameterList.set("AZ_solver", "AZ_gmres");
119  } else {
120  ML_Epetra::SetDefaults("SA",mMLParameterList);
121  mMLParameterList.set("increasing or decreasing", "increasing");
122  mMLParameterList.set("aggregation: type", "MIS");
123  mMLParameterList.set("smoother: type", "Chebyshev");
124  mMLParameterList.set("smoother: sweeps", 3);
125  mMLParameterList.set("smoother: pre or post", "both");
126  mAztecParameterList.set("AZ_solver", "AZ_bicgstab");
127  }
128 
129  //NOTE: this will OVERWRITE PREVIOUS SETTINGS TO GIVE FULL CONTROL
130  TrilinosSolverUtilities::SetTeuchosParameters(Settings["trilinos_aztec_parameter_list"], mAztecParameterList);
131 
132  //NOTE: this will OVERWRITE PREVIOUS SETTINGS TO GIVE FULL CONTROL
133  TrilinosSolverUtilities::SetTeuchosParameters(Settings["trilinos_ml_parameter_list"], mMLParameterList);
134  }
135 
136  MultiLevelSolver(Teuchos::ParameterList& rAztecParameterList, Teuchos::ParameterList& rMLParameterList, double Tolerance, int MaxIterations)
137  {
138  mAztecParameterList = rAztecParameterList;
139  mMLParameterList = rMLParameterList;
140  mTolerance = Tolerance;
141  mMaxIterations = MaxIterations;
142  }
143 
145  MultiLevelSolver(const MultiLevelSolver& Other) = delete;
146 
148  ~MultiLevelSolver() override
149  {
150  Clear();
151  }
152 
156 
158  MultiLevelSolver& operator=(const MultiLevelSolver& Other) = delete;
159 
163 
165  {
166  mScalingType = Value;
167  }
168 
170  {
171  return mScalingType;
172  }
173 
174  void SetReformPrecAtEachStep(bool Value)
175  {
176  mReformPrecAtEachStep = Value;
177  }
178 
180  {
181  mpMLPrec.reset();
182  }
183 
184  void Clear() override
185  {
187  }
188 
197  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
198  {
199  KRATOS_TRY
200  Epetra_LinearProblem aztec_problem(&rA,&rX,&rB);
201 
202  if (this->GetScalingType() == LeftScaling) {
203  // don't use this with conjugate gradient
204  // it destroys the symmetry
205  Epetra_Vector scaling_vect(rA.RowMap());
206  rA.InvColSums(scaling_vect);
207  aztec_problem.LeftScale(scaling_vect);
208  }
209 
210  mMLParameterList.set("PDE equations", mNumDof);
211 
212  // create the preconditioner now. this is expensive.
213  // the preconditioner stores a pointer to the system
214  // matrix. if the system matrix is freed from heap
215  // before the preconditioner, a memory error can occur
216  // when the preconditioner is freed. the strategy
217  // should take care to Clear() the linear solver
218  // before the system matrix.
219  if (mReformPrecAtEachStep || !mpMLPrec) {
220  this->ResetPreconditioner();
221  MLPreconditionerPointerType tmp(Kratos::make_unique<ML_Epetra::MultiLevelPreconditioner>(rA, mMLParameterList, true));
222  mpMLPrec.swap(tmp);
223  }
224 
225  // create an AztecOO solver
226  AztecOO aztec_solver(aztec_problem);
227  aztec_solver.SetParameters(mAztecParameterList);
228 
229  // set preconditioner and solve
230  aztec_solver.SetPrecOperator(&(*mpMLPrec));
231 
232  aztec_solver.Iterate(mMaxIterations, mTolerance);
233 
234  return true;
235  KRATOS_CATCH("");
236  }
237 
247  {
248  return false;
249  }
250 
258  {
259  return true;
260  }
261 
269  SparseMatrixType& rA,
270  VectorType& rX,
271  VectorType& rB,
272  typename ModelPart::DofsArrayType& rdof_set,
273  ModelPart& r_model_part
274  ) override
275  {
276  int old_ndof = -1;
277  unsigned int old_node_id = rdof_set.begin()->Id();
278  int ndof = 0;
279  for (auto it = rdof_set.begin(); it!=rdof_set.end(); it++) {
280  unsigned int id = it->Id();
281  if (id != old_node_id) {
282  old_node_id = id;
283  if (old_ndof == -1) {
284  old_ndof = ndof;
285  } else if (old_ndof != ndof) { //if it is different than the block size is 1
286  old_ndof = -1;
287  break;
288  }
289  ndof = 1;
290  } else {
291  ndof++;
292  }
293  }
294 
295  old_ndof = r_model_part.GetCommunicator().GetDataCommunicator().MinAll(old_ndof);
296 
297  if (old_ndof == -1) {
298  mNumDof = 1;
299  } else {
300  mNumDof = ndof;
301  }
302  }
303 
305  void PrintInfo(std::ostream& rOStream) const override
306  {
307  rOStream << "Trilinos MultiLevel-Solver";
308  }
309 
310  static void SetDefaults(Teuchos::ParameterList& rParameterlist, const std::string& rSettingsName)
311  {
312  ML_Epetra::SetDefaults(rSettingsName.c_str(), rParameterlist);
313  }
314 
315 private:
318 
319  Teuchos::ParameterList mAztecParameterList;
320  Teuchos::ParameterList mMLParameterList;
321  MLPreconditionerPointerType mpMLPrec = nullptr;
322  ScalingType mScalingType = LeftScaling;
323  bool mReformPrecAtEachStep = true;
324  double mTolerance;
325  int mMaxIterations;
326  int mNumDof = 1;
327 
329 
330 }; // Class MultiLevelSolver
331 
333 template<class TSparseSpaceType, class TDenseSpaceType, class TReordererType>
334 inline std::ostream& operator << (std::ostream& rOStream,
335  const MultiLevelSolver<TSparseSpaceType,
336  TDenseSpaceType, TReordererType>& rThis)
337 {
338  rThis.PrintInfo(rOStream);
339  rOStream << std::endl;
340  rThis.PrintData(rOStream);
341 
342  return rOStream;
343 }
344 
345 } // namespace Kratos.
346 
347 #endif // KRATOS_MULTILEVEL_SOLVER_H_INCLUDED defined
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
TSparseSpaceType::MatrixType SparseMatrixType
Definition: linear_solver.h:73
TDenseSpaceType::MatrixType DenseMatrixType
Definition: linear_solver.h:81
TSparseSpaceType::MatrixPointerType SparseMatrixPointerType
Definition: linear_solver.h:75
TSparseSpaceType::VectorType VectorType
Definition: linear_solver.h:77
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Communicator & GetCommunicator()
Definition: model_part.h:1821
Wrapper for Trilinos-ML preconditioner using the Aztec-Solver.
Definition: ml_solver.h:48
BaseType::VectorType VectorType
Definition: ml_solver.h:62
void SetReformPrecAtEachStep(bool Value)
Definition: ml_solver.h:174
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: ml_solver.h:197
MultiLevelSolver(const MultiLevelSolver &Other)=delete
Copy constructor.
MultiLevelSolver(Teuchos::ParameterList &rAztecParameterList, Teuchos::ParameterList &rMLParameterList, double Tolerance, int MaxIterations)
Definition: ml_solver.h:136
BaseType::SparseMatrixType SparseMatrixType
Definition: ml_solver.h:60
void ResetPreconditioner()
Definition: ml_solver.h:179
MultiLevelSolver & operator=(const MultiLevelSolver &Other)=delete
Assignment operator.
ScalingType
Definition: ml_solver.h:56
@ LeftScaling
Definition: ml_solver.h:56
@ NoScaling
Definition: ml_solver.h:56
static void SetDefaults(Teuchos::ParameterList &rParameterlist, const std::string &rSettingsName)
Definition: ml_solver.h:310
void ProvideAdditionalData(SparseMatrixType &rA, VectorType &rX, VectorType &rB, typename ModelPart::DofsArrayType &rdof_set, ModelPart &r_model_part) override
Definition: ml_solver.h:268
BaseType::DenseMatrixType DenseMatrixType
Definition: ml_solver.h:64
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: ml_solver.h:305
LinearSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
Definition: ml_solver.h:58
bool AdditionalPhysicalDataIsNeeded() override
Definition: ml_solver.h:257
void Clear() override
Definition: ml_solver.h:184
KRATOS_CLASS_POINTER_DEFINITION(MultiLevelSolver)
Pointer definition of MultiLevelSolver.
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: ml_solver.h:246
MultiLevelSolver(Parameters Settings)
Constructor with Parameters.
Definition: ml_solver.h:75
BaseType::SparseMatrixPointerType SparseMatrixPointerType
Definition: ml_solver.h:66
ScalingType GetScalingType()
Definition: ml_solver.h:169
~MultiLevelSolver() override
Destructor.
Definition: ml_solver.h:148
void SetScalingType(ScalingType Value)
Definition: ml_solver.h:164
Kratos::unique_ptr< ML_Epetra::MultiLevelPreconditioner > MLPreconditionerPointerType
Definition: ml_solver.h:68
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
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
bool GetBool() const
This method returns the boolean contained in the current Parameter.
Definition: kratos_parameters.cpp:675
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
iterator begin()
Returns an iterator pointing to the beginning of the container.
Definition: pointer_vector_set.h:278
iterator end()
Returns an iterator pointing to the end of the container.
Definition: pointer_vector_set.h:314
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
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::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98