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.
trilinos_monotonicity_preserving_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: Daniel Diez
11 //
12 
13 #if !defined(KRATOS_TRILINOS_MONOTONICITY_PRESERVING_SOLVER_H_INCLUDED )
14 #define KRATOS_TRILINOS_MONOTONICITY_PRESERVING_SOLVER_H_INCLUDED
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/define.h"
25 
26 namespace Kratos
27 {
28 
31 
35 
39 
43 
47 
58 template<class TSparseSpaceType, class TDenseSpaceType,
59  class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
61  : public LinearSolver<TSparseSpaceType, TDenseSpaceType, TReordererType>
62 {
63 public:
66 
69 
72 
75 
78 
81 
84 
88 
91  {
92  }
93 
99  typename BaseType::Pointer pLinearSolver
100  ) : BaseType (),
101  mpLinearSolver(pLinearSolver)
102  {
103  }
104 
110  : BaseType ()
111  {
112  KRATOS_TRY
113 
114  Parameters default_parameters( R"(
115  {
116  "solver_type" : "monotonicity_preserving",
117  "inner_solver_settings" : {
118  "preconditioner_type" : "amg",
119  "solver_type" : "amgcl",
120  "smoother_type" : "ilu0",
121  "krylov_type" : "lgmres",
122  "coarsening_type" : "aggregation",
123  "max_iteration" : 100,
124  "provide_coordinates" : false,
125  "gmres_krylov_space_dimension" : 100,
126  "verbosity" : 1,
127  "tolerance" : 1e-6,
128  "scaling" : false,
129  "block_size" : 1,
130  "use_block_matrices_if_possible" : true,
131  "coarse_enough" : 1000,
132  "max_levels" : -1,
133  "pre_sweeps" : 1,
134  "post_sweeps" : 1,
135  "use_gpgpu" : false
136  }
137 
138  } )" );
139 
140  // Now validate agains defaults -- this also ensures no type mismatch
141  ThisParameters.ValidateAndAssignDefaults(default_parameters);
142 
143  mpLinearSolver = LinearSolverFactoryType().Create(ThisParameters["inner_solver_settings"]);
144 
145  KRATOS_CATCH("")
146  }
147 
150 
151 
154 
155 
159 
162  {
163  BaseType::operator=(Other);
164  return *this;
165  }
166 
170 
177  {
178  return true;
179  }
180 
188  SparseMatrixType& rA,
189  VectorType& rX,
190  VectorType& rB,
191  typename ModelPart::DofsArrayType& rdof_set,
192  ModelPart& r_model_part
193  ) override
194  {
195  std::vector<int> global_ids(rdof_set.size());
196  std::vector<double> dofs_values(rdof_set.size());
197 
198  IndexType i = 0;
199  for (const auto& dof : rdof_set) {
200  const int global_id = dof.EquationId();
201  global_ids[i] = global_id;
202  dofs_values[i] = dof.GetSolutionStepValue();
203  ++i;
204  }
205 
206  Epetra_Map localmap(-1, global_ids.size(), global_ids.data(), 0, rA.Comm());
207  Epetra_Vector dofs_local(Copy, localmap, dofs_values.data());
208 
209  Epetra_Import dirichlet_importer(rA.ColMap(), dofs_local.Map());
210 
211  // defining a temporary vector to gather all of the values needed
212  Epetra_Vector dofs(rA.ColMap());
213 
214  // importing in the new temp vector the values
215  int ierr = dofs.Import(dofs_local, dirichlet_importer, Insert);
216  if (ierr != 0)
217  KRATOS_ERROR << "Epetra failure found";
218 
219  for (int i = 0; i < rA.NumMyRows(); i++) {
220  int numEntries; // number of non-zero entries
221  double* vals; // row non-zero values
222  int* cols; // column indices of row non-zero values
223  rA.ExtractMyRowView(i, numEntries, vals, cols);
224 
225  for (int j = 0; j < numEntries; j++) {
226  const double value = vals[j];
227  if (value > 0.0) {
228  const int row_gid = rA.RowMap().GID(i);
229  const int col_gid = rA.ColMap().GID(cols[j]);
230  if (row_gid > col_gid) {
231  Matrix LHS(2, 2);
232  LHS(0, 0) = value;
233  LHS(0, 1) = -value;
234  LHS(1, 0) = -value;
235  LHS(1, 1) = value;
236  Vector dofs_sol(2);
237  int row_lid = localmap.LID(row_gid);
238  dofs_sol[0] = dofs_local[row_lid];
239  dofs_sol[1] = dofs[cols[j]];
240  Vector RHS = -prod(LHS, dofs_sol);
241  Element::EquationIdVectorType equations_ids = {static_cast<std::size_t>(row_gid), static_cast<std::size_t>(col_gid)};
242  TSparseSpaceType::AssembleLHS(rA, LHS, equations_ids);
243  TSparseSpaceType::AssembleRHS(rB, RHS, equations_ids);
244 
245  }
246  }
247  }
248  }
249 
250  rA.GlobalAssemble();
251  rB.GlobalAssemble();
252 
253  if (mpLinearSolver->AdditionalPhysicalDataIsNeeded()) {
254  mpLinearSolver->ProvideAdditionalData(rA,rX,rB,rdof_set,r_model_part);
255  }
256  }
257 
259  {
260  mpLinearSolver->InitializeSolutionStep(rA,rX,rB);
261  }
262 
270  {
271  mpLinearSolver->FinalizeSolutionStep(rA,rX,rB);
272  }
273 
278  void Clear() override
279  {
280  mpLinearSolver->Clear();
281  }
282 
289  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
290  {
291  return mpLinearSolver->Solve(rA,rX,rB);
292  }
293 
294 
295 
299 
300 
304 
305 
309 
311  std::string Info() const override
312  {
313  std::stringstream buffer;
314  buffer << "Composite Linear Solver. Uses internally the following linear solver " << mpLinearSolver->Info();
315  return buffer.str();
316  }
317 
319  void PrintInfo(std::ostream& rOStream) const override
320  {
321  rOStream << Info();
322  }
323 
325  void PrintData(std::ostream& rOStream) const override
326  {
327  BaseType::PrintData(rOStream);
328  }
329 
330 
334 
335 
337 
338 
339 private:
342 
343 
348 
352 
353 
357 
358 
362 
363 
367 
368 
370 
371 }; // Class TrilinosMonotonicityPreservingSolver
372 
374 
377 
378 
382 
383 
385 template<class TSparseSpaceType, class TDenseSpaceType,
386  class TPreconditionerType,
387  class TReordererType>
388 inline std::istream& operator >> (std::istream& IStream,
389  TrilinosMonotonicityPreservingSolver<TSparseSpaceType, TDenseSpaceType,
390  TReordererType>& rThis)
391 {
392  return IStream;
393 }
394 
396 template<class TSparseSpaceType, class TDenseSpaceType,
397  class TPreconditionerType,
398  class TReordererType>
399 inline std::ostream& operator << (std::ostream& OStream,
400  const TrilinosMonotonicityPreservingSolver<TSparseSpaceType, TDenseSpaceType,
401  TReordererType>& rThis)
402 {
403  rThis.PrintInfo(OStream);
404  OStream << std::endl;
405  rThis.PrintData(OStream);
406 
407  return OStream;
408 }
410 
411 
412 } // namespace Kratos.
413 
414 #endif // KRATOS_TRILINOS_MONOTONICITY_PRESERVING_SOLVER_H_INCLUDED defined
415 
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
Here we add the functions needed for the registration of linear solvers.
Definition: linear_solver_factory.h:62
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
TSparseSpaceType::IndexType IndexType
The index type definition to be consistent.
Definition: linear_solver.h:88
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: linear_solver.h:388
LinearSolver & operator=(const LinearSolver &Other)
Assignment operator.
Definition: linear_solver.h:112
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
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
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
size_type size() const
Returns the number of elements in the container.
Definition: pointer_vector_set.h:502
Definition: trilinos_monotonicity_preserving_solver.h:62
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: trilinos_monotonicity_preserving_solver.h:325
TrilinosMonotonicityPreservingSolver()
Default constructor.
Definition: trilinos_monotonicity_preserving_solver.h:90
TSparseSpaceType::VectorType VectorType
The definition of the spaces (vector)
Definition: trilinos_monotonicity_preserving_solver.h:77
~TrilinosMonotonicityPreservingSolver() override
Destructor.
Definition: trilinos_monotonicity_preserving_solver.h:153
TSparseSpaceType::MatrixType SparseMatrixType
The definition of the spaces (sparse matrix)
Definition: trilinos_monotonicity_preserving_solver.h:74
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: trilinos_monotonicity_preserving_solver.h:289
TrilinosMonotonicityPreservingSolver & operator=(const TrilinosMonotonicityPreservingSolver &Other)
Assignment operator.
Definition: trilinos_monotonicity_preserving_solver.h:161
KRATOS_CLASS_POINTER_DEFINITION(TrilinosMonotonicityPreservingSolver)
Pointer definition of TrilinosMonotonicityPreservingSolver.
void Clear() override
Definition: trilinos_monotonicity_preserving_solver.h:278
bool AdditionalPhysicalDataIsNeeded() override
Definition: trilinos_monotonicity_preserving_solver.h:176
LinearSolverFactory< TSparseSpaceType, TDenseSpaceType > LinearSolverFactoryType
The definition of the linear solver factory type.
Definition: trilinos_monotonicity_preserving_solver.h:83
LinearSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
Definition of the base type.
Definition: trilinos_monotonicity_preserving_solver.h:71
void ProvideAdditionalData(SparseMatrixType &rA, VectorType &rX, VectorType &rB, typename ModelPart::DofsArrayType &rdof_set, ModelPart &r_model_part) override
Definition: trilinos_monotonicity_preserving_solver.h:187
TDenseSpaceType::MatrixType DenseMatrixType
The definition of the spaces (dense matrix)
Definition: trilinos_monotonicity_preserving_solver.h:80
void InitializeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: trilinos_monotonicity_preserving_solver.h:258
void FinalizeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: trilinos_monotonicity_preserving_solver.h:269
TrilinosMonotonicityPreservingSolver(typename BaseType::Pointer pLinearSolver)
Constructor without parameters.
Definition: trilinos_monotonicity_preserving_solver.h:98
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: trilinos_monotonicity_preserving_solver.h:319
std::string Info() const override
Turn back information as a string.
Definition: trilinos_monotonicity_preserving_solver.h:311
TrilinosMonotonicityPreservingSolver(Parameters ThisParameters)
Constructor with parameters.
Definition: trilinos_monotonicity_preserving_solver.h:109
TrilinosMonotonicityPreservingSolver(const TrilinosMonotonicityPreservingSolver &Other)
Copy constructor.
Definition: trilinos_monotonicity_preserving_solver.h:149
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
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
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
dofs
Enforced auxTangentSlipNonObjective = delta_time * gap_time_derivative_non_objective....
Definition: generate_frictional_mortar_condition.py:210
int dof
Definition: ode_solve.py:393
int j
Definition: quadrature.py:648
integer i
Definition: TensorModule.f:17