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.
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_MONOTONICITY_PRESERVING_SOLVER_H_INCLUDED )
14 #define KRATOS_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  Vector dofs_values = ZeroVector(rdof_set.size());
196 
197  block_for_each(rdof_set, [&](Dof<double>& rDof){
198  const std::size_t id = rDof.EquationId();
199  dofs_values[id] = rDof.GetSolutionStepValue();
200  });
201  double *values_vector = rA.value_data().begin();
202  std::size_t *index1_vector = rA.index1_data().begin();
203  std::size_t *index2_vector = rA.index2_data().begin();
204 
206  [&](std::size_t i)
207  {
208  for (std::size_t k = index1_vector[i]; k < index1_vector[i + 1]; k++) {
209  const double value = values_vector[k];
210  if (value > 0.0) {
211  const auto j = index2_vector[k];
212  if (j > i) {
213  rA(i,j) -= value;
214  rA(j,i) -= value;
215  // Values conflicting with other threads
216  auto& r_aii = rA(i,i).ref();
217  AtomicAdd(r_aii, value);
218  auto& r_ajj = rA(j,j).ref();
219  AtomicAdd(r_ajj, value);
220  auto& r_bi = rB[i];
221  AtomicAdd(r_bi, value*dofs_values[j] - value*dofs_values[i]);
222  auto& r_bj = rB[j];
223  AtomicAdd(r_bj, value*dofs_values[i] - value*dofs_values[j]);
224  }
225  }
226  }
227  }
228  );
229 
230  if (mpLinearSolver->AdditionalPhysicalDataIsNeeded()) {
231  mpLinearSolver->ProvideAdditionalData(rA,rX,rB,rdof_set,r_model_part);
232  }
233  }
234 
236  {
237  mpLinearSolver->InitializeSolutionStep(rA,rX,rB);
238  }
239 
247  {
248  mpLinearSolver->FinalizeSolutionStep(rA,rX,rB);
249  }
250 
255  void Clear() override
256  {
257  mpLinearSolver->Clear();
258  }
259 
266  bool Solve(SparseMatrixType& rA, VectorType& rX, VectorType& rB) override
267  {
268  return mpLinearSolver->Solve(rA,rX,rB);
269  }
270 
271 
272 
276 
277 
281 
282 
286 
288  std::string Info() const override
289  {
290  std::stringstream buffer;
291  buffer << "Composite Linear Solver. Uses internally the following linear solver " << mpLinearSolver->Info();
292  return buffer.str();
293  }
294 
296  void PrintInfo(std::ostream& rOStream) const override
297  {
298  rOStream << Info();
299  }
300 
302  void PrintData(std::ostream& rOStream) const override
303  {
304  BaseType::PrintData(rOStream);
305  }
306 
307 
311 
312 
314 
315 
316 private:
319 
320 
325 
329 
330 
334 
335 
339 
340 
344 
345 
347 
348 }; // Class MonotonicityPreservingSolver
349 
351 
354 
355 
359 
360 
362 template<class TSparseSpaceType, class TDenseSpaceType,
363  class TPreconditionerType,
364  class TReordererType>
365 inline std::istream& operator >> (std::istream& IStream,
366  MonotonicityPreservingSolver<TSparseSpaceType, TDenseSpaceType,
367  TReordererType>& rThis)
368 {
369  return IStream;
370 }
371 
373 template<class TSparseSpaceType, class TDenseSpaceType,
374  class TPreconditionerType,
375  class TReordererType>
376 inline std::ostream& operator << (std::ostream& OStream,
377  const MonotonicityPreservingSolver<TSparseSpaceType, TDenseSpaceType,
378  TReordererType>& rThis)
379 {
380  rThis.PrintInfo(OStream);
381  OStream << std::endl;
382  rThis.PrintData(OStream);
383 
384  return OStream;
385 }
387 
388 
389 } // namespace Kratos.
390 
391 #endif // KRATOS_MONOTONICITY_PRESERVING_SOLVER_H_INCLUDED defined
392 
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
EquationIdType EquationId() const
Definition: dof.h:324
TDataType & GetSolutionStepValue(IndexType SolutionStepIndex=0)
Definition: dof.h:256
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
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
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
Definition: monotonicity_preserving_solver.h:62
LinearSolverFactory< TSparseSpaceType, TDenseSpaceType > LinearSolverFactoryType
The definition of the linear solver factory type.
Definition: monotonicity_preserving_solver.h:83
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: monotonicity_preserving_solver.h:296
MonotonicityPreservingSolver(Parameters ThisParameters)
Constructor with parameters.
Definition: monotonicity_preserving_solver.h:109
LinearSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
Definition of the base type.
Definition: monotonicity_preserving_solver.h:71
MonotonicityPreservingSolver & operator=(const MonotonicityPreservingSolver &Other)
Assignment operator.
Definition: monotonicity_preserving_solver.h:161
MonotonicityPreservingSolver(typename BaseType::Pointer pLinearSolver)
Constructor without parameters.
Definition: monotonicity_preserving_solver.h:98
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: monotonicity_preserving_solver.h:266
KRATOS_CLASS_POINTER_DEFINITION(MonotonicityPreservingSolver)
Pointer definition of MonotonicityPreservingSolver.
void FinalizeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: monotonicity_preserving_solver.h:246
void ProvideAdditionalData(SparseMatrixType &rA, VectorType &rX, VectorType &rB, typename ModelPart::DofsArrayType &rdof_set, ModelPart &r_model_part) override
Definition: monotonicity_preserving_solver.h:187
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: monotonicity_preserving_solver.h:302
std::string Info() const override
Turn back information as a string.
Definition: monotonicity_preserving_solver.h:288
MonotonicityPreservingSolver()
Default constructor.
Definition: monotonicity_preserving_solver.h:90
void InitializeSolutionStep(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Definition: monotonicity_preserving_solver.h:235
~MonotonicityPreservingSolver() override
Destructor.
Definition: monotonicity_preserving_solver.h:153
TDenseSpaceType::MatrixType DenseMatrixType
The definition of the spaces (dense matrix)
Definition: monotonicity_preserving_solver.h:80
MonotonicityPreservingSolver(const MonotonicityPreservingSolver &Other)
Copy constructor.
Definition: monotonicity_preserving_solver.h:149
bool AdditionalPhysicalDataIsNeeded() override
Definition: monotonicity_preserving_solver.h:176
void Clear() override
Definition: monotonicity_preserving_solver.h:255
TSparseSpaceType::MatrixType SparseMatrixType
The definition of the spaces (sparse matrix)
Definition: monotonicity_preserving_solver.h:74
TSparseSpaceType::VectorType VectorType
The definition of the spaces (vector)
Definition: monotonicity_preserving_solver.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
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
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
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
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
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
integer i
Definition: TensorModule.f:17