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.
aztec_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_AZTEC_SOLVER_H_INCLUDED)
15 #define KRATOS_AZTEC_SOLVER_H_INCLUDED
16 
17 // External includes
18 #include "string.h"
19 
20 // Project includes
21 #include "includes/define.h"
25 
26 // Aztec solver includes
27 #include "AztecOO.h"
28 #include "Epetra_LinearProblem.h"
29 #include "Teuchos_ParameterList.hpp"
30 #include "Ifpack.h"
31 #include "Ifpack_ConfigDefs.h"
32 
33 namespace Kratos
34 {
37 
39 
43 
45 
53 template< class TSparseSpaceType, class TDenseSpaceType,
54  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
55 class AztecSolver : public LinearSolver< TSparseSpaceType,
56  TDenseSpaceType, TReordererType>
57 {
58 public:
61 
64 
66 
68 
70 
74 
77  {
78  Parameters default_settings( R"( {
79  "solver_type" : "aztec",
80  "tolerance" : 1.0e-6,
81  "max_iteration" : 200,
82  "preconditioner_type" : "none",
83  "overlap_level" : 1,
84  "gmres_krylov_space_dimension" : 100,
85  "scaling" : false,
86  "verbosity" : 0,
87  "trilinos_aztec_parameter_list" : {},
88  "trilinos_preconditioner_parameter_list" : {}
89  } )" );
90 
91  settings.ValidateAndAssignDefaults(default_settings);
92 
93  //settings for the AZTEC solver
94  mTolerance = settings["tolerance"].GetDouble();
95  mMaxIterations = settings["max_iteration"].GetDouble();
96 
97  //IFpack settings
98  mOverlapLevel = settings["overlap_level"].GetInt();
99 
100  //scaling settings
101  if (!settings["scaling"].GetBool()) {
102  mScalingType = NoScaling;
103  }
104 
105  //assign the amesos parameter list, which may contain parameters IN TRILINOS INTERNAL FORMAT to mparameter_list
106  mAztecParameterList = Teuchos::ParameterList();
107 
108  if (settings["verbosity"].GetInt() == 0) {
109  mAztecParameterList.set("AZ_output", "AZ_none");
110  } else {
111  mAztecParameterList.set("AZ_output", settings["verbosity"].GetInt());
112  }
113 
114  //choose the solver type
115  const std::string solver_type = settings["solver_type"].GetString();
116  if (solver_type == "CGSolver" || solver_type == "cg") {
117  mAztecParameterList.set("AZ_solver", "AZ_cg");
118  } else if (solver_type == "BICGSTABSolver" || solver_type == "bicgstab") {
119  mAztecParameterList.set("AZ_solver", "AZ_bicgstab");
120  } else if (solver_type == "GMRESSolver" || solver_type == "gmres") {
121  mAztecParameterList.set("AZ_solver", "AZ_gmres");
122  mAztecParameterList.set("AZ_kspace", settings["gmres_krylov_space_dimension"].GetInt());
123  } else if (solver_type == "AztecSolver" || solver_type == "aztec") {
124  //do nothing here. Leave full control to the user through the "trilinos_aztec_parameter_list"
125  }
126  else {
127  KRATOS_ERROR << "The \"solver_type\" specified: \"" << solver_type << "\" is not supported\n"
128  << " Available options are: \"cg\", \"bicgstab\", \"gmres\", \"aztec\"" << std::endl;
129  }
130 
131  //NOTE: this will OVERWRITE PREVIOUS SETTINGS TO GIVE FULL CONTROL
132  TrilinosSolverUtilities::SetTeuchosParameters(settings["trilinos_aztec_parameter_list"], mAztecParameterList);
133 
134  mPreconditionerParameterList = Teuchos::ParameterList();
135  const std::string preconditioner_type = settings["preconditioner_type"].GetString();
136  if (preconditioner_type == "diagonal" || preconditioner_type == "DiagonalPreconditioner") {
137  mIFPreconditionerType = "AZ_none";
138  } else if (preconditioner_type == "ilu0" || preconditioner_type == "ILU0") {
139  mIFPreconditionerType = "ILU";
140  mPreconditionerParameterList.set("fact: drop tolerance", 1e-9);
141  mPreconditionerParameterList.set("fact: level-of-fill", 1);
142  } else if (preconditioner_type == "ilut" || preconditioner_type == "ILUT") {
143  mIFPreconditionerType = "ILU";
144  mPreconditionerParameterList.set("fact: drop tolerance", 1e-9);
145  mPreconditionerParameterList.set("fact: level-of-fill", 10);
146  } else if (preconditioner_type == "icc" || preconditioner_type == "ICC") {
147  mIFPreconditionerType = "ICC";
148  mPreconditionerParameterList.set("fact: drop tolerance", 1e-9);
149  mPreconditionerParameterList.set("fact: level-of-fill", 10);
150  } else if (preconditioner_type == "amesos" || preconditioner_type == "AmesosPreconditioner") {
151  mIFPreconditionerType = "Amesos";
152  mPreconditionerParameterList.set("amesos: solver type", "Amesos_Klu");
153  } else if (preconditioner_type == "none" || preconditioner_type == "None") {
154  mIFPreconditionerType = "AZ_none";
155  } else {
156  KRATOS_ERROR << "The \"preconditioner_type\" specified: \"" << preconditioner_type << "\" is not supported\n"
157  << " Available options are: \"diagonal\", \"ilu0\", \"ilut\", \"icc\", \"amesos\", \"none\"" << std::endl;
158  }
159 
160  //NOTE: this will OVERWRITE PREVIOUS SETTINGS TO GIVE FULL CONTROL
161  TrilinosSolverUtilities::SetTeuchosParameters(settings["trilinos_preconditioner_parameter_list"], mPreconditionerParameterList);
162  }
163 
164  AztecSolver(Teuchos::ParameterList& aztec_parameter_list, std::string IFPreconditionerType, Teuchos::ParameterList& preconditioner_parameter_list, double tol, int nit_max, int overlap_level)
165  {
166  //settings for the AZTEC solver
167  mAztecParameterList = aztec_parameter_list;
168  mTolerance = tol;
169  mMaxIterations = nit_max;
170 
171  //IFpack settings
172  mIFPreconditionerType = IFPreconditionerType;
173  mPreconditionerParameterList = preconditioner_parameter_list;
174  mOverlapLevel = overlap_level;
175  }
176 
178  AztecSolver(const AztecSolver& Other) = delete;
179 
181  ~AztecSolver() override = default;
182 
186 
188  AztecSolver& operator=(const AztecSolver& Other) = delete;
189 
193 
194  //function to set the scaling typedef
195  void SetScalingType(AztecScalingType scaling_type)
196  {
197  mScalingType = scaling_type;
198  }
199 
208  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
209  {
210  KRATOS_TRY
211  rA.Comm().Barrier();
212 
213  Epetra_LinearProblem aztec_problem(&rA,&rX,&rB);
214 
215  //perform GS1 scaling if required
216  if (mScalingType == SymmetricScaling) {
217  KRATOS_ERROR << "something wrong with the scaling, to be further teststed" << std::endl;
218  Epetra_Vector scaling_vect(rA.RowMap());
219  rA.InvColSums(scaling_vect);
220 
221  for (int i=0; i<scaling_vect.MyLength(); ++i) scaling_vect[i] = std::sqrt(scaling_vect[i]);
222 
223  aztec_problem.LeftScale(scaling_vect);
224  aztec_problem.RightScale(scaling_vect);
225  } else if (mScalingType == LeftScaling) {
226  Epetra_Vector scaling_vect(rA.RowMap());
227  rA.InvColSums(scaling_vect);
228 
229  aztec_problem.LeftScale(scaling_vect);
230  }
231 
232  AztecOO aztec_solver(aztec_problem);
233  aztec_solver.SetParameters(mAztecParameterList);
234 
235  //here we verify if we want a preconditioner
236  if (mIFPreconditionerType != std::string("AZ_none")) {
237  //ifpack preconditioner type
238  Ifpack preconditioner_factory;
239 
240  std::string preconditioner_type = mIFPreconditionerType;
241  Ifpack_Preconditioner* p_preconditioner = preconditioner_factory.Create(preconditioner_type, &rA, mOverlapLevel);
242  KRATOS_ERROR_IF(p_preconditioner == 0) << "Preconditioner-initialization went wrong" << std::endl;
243 
244  IFPACK_CHK_ERR(p_preconditioner->SetParameters(mPreconditionerParameterList));
245  IFPACK_CHK_ERR(p_preconditioner->Initialize());
246  IFPACK_CHK_ERR(p_preconditioner->Compute());
247 
248  // HERE WE SET THE IFPACK PRECONDITIONER
249  aztec_solver.SetPrecOperator(p_preconditioner);
250 
251  //and ... here we solve
252  aztec_solver.Iterate(mMaxIterations,mTolerance);
253 
254  delete p_preconditioner;
255  } else {
256  aztec_solver.Iterate(mMaxIterations,mTolerance);
257  }
258 
259  rA.Comm().Barrier();
260 
261  return true;
262  KRATOS_CATCH("");
263  }
264 
274  {
275  return false;
276  }
277 
279  void PrintInfo(std::ostream& rOStream) const override
280  {
281  rOStream << "Trilinos Aztec-Solver";
282  }
283 
284 private:
287 
288  //aztec solver settings
289  Teuchos::ParameterList mAztecParameterList;
290  double mTolerance;
291  int mMaxIterations;
292  AztecScalingType mScalingType = LeftScaling;
293 
294  std::string mIFPreconditionerType;
295 
296  Teuchos::ParameterList mPreconditionerParameterList;
297  int mOverlapLevel;
298 
300 
301 }; // Class AztecSolver
302 
304 template<class TSparseSpaceType, class TDenseSpaceType, class TReordererType>
305 inline std::ostream& operator << (std::ostream& rOStream,
306  const AztecSolver<TSparseSpaceType,
307  TDenseSpaceType, TReordererType>& rThis)
308 {
309  rThis.PrintInfo(rOStream);
310  rOStream << std::endl;
311  rThis.PrintData(rOStream);
312 
313  return rOStream;
314 }
315 
316 } // namespace Kratos.
317 
318 #endif // KRATOS_AZTEC_SOLVER_H_INCLUDED defined
Wrapper for Trilinos-Aztec Iterative Solvers.
Definition: aztec_solver.h:57
AztecSolver(Parameters settings)
Constructor with Parameters.
Definition: aztec_solver.h:76
TDenseSpaceType::MatrixType DenseMatrixType
Definition: aztec_solver.h:69
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: aztec_solver.h:279
AztecSolver(Teuchos::ParameterList &aztec_parameter_list, std::string IFPreconditionerType, Teuchos::ParameterList &preconditioner_parameter_list, double tol, int nit_max, int overlap_level)
Definition: aztec_solver.h:164
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: aztec_solver.h:208
AztecSolver & operator=(const AztecSolver &Other)=delete
Assignment operator.
~AztecSolver() override=default
Destructor.
void SetScalingType(AztecScalingType scaling_type)
Definition: aztec_solver.h:195
KRATOS_CLASS_POINTER_DEFINITION(AztecSolver)
Pointer definition of AztecSolver.
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Definition: aztec_solver.h:273
TSparseSpaceType::VectorType VectorType
Definition: aztec_solver.h:67
AztecSolver(const AztecSolver &Other)=delete
Copy constructor.
TSparseSpaceType::MatrixType SparseMatrixType
Definition: aztec_solver.h:65
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
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
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
#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(conditional)
Definition: exception.h:162
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
AztecScalingType
Definition: aztec_solver.h:38
@ LeftScaling
Definition: aztec_solver.h:38
@ NoScaling
Definition: aztec_solver.h:38
@ SymmetricScaling
Definition: aztec_solver.h:38
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
int tol
Definition: hinsberg_optimization.py:138
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31