14 #if !defined(KRATOS_AMGCL_SOLVER )
15 #define KRATOS_AMGCL_SOLVER
17 #ifndef AMGCL_PARAM_UNKNOWN
19 # define AMGCL_PARAM_UNKNOWN(name) \
20 Kratos::Logger("AMGCL") << KRATOS_CODE_LOCATION << Kratos::Logger::Severity::WARNING << "Unknown parameter " << name << std::endl
30 #include <boost/range/iterator_range.hpp>
31 #include <boost/property_tree/json_parser.hpp>
40 #include <amgcl/coarsening/rigid_body_modes.hpp>
89 boost::property_tree::ptree amgclParams,
108 template<
class TSparseSpaceType,
class TDenseSpaceType,
109 class TReordererType = Reorderer<TSparseSpaceType, TDenseSpaceType> >
111 TDenseSpaceType, TReordererType>
153 "preconditioner_type" : "amg",
154 "solver_type" : "AMGCL",
155 "smoother_type" : "ilu0",
156 "krylov_type" : "gmres",
157 "coarsening_type" : "aggregation",
158 "max_iteration" : 100,
159 "provide_coordinates" : false,
160 "gmres_krylov_space_dimension" : 100,
165 "use_block_matrices_if_possible" : true,
166 "coarse_enough" : 1000,
174 ThisParameters.ValidateAndAssignDefaults(default_parameters);
177 std::set<std::string> available_smoothers = {
"spai0",
"spai1",
"ilu0",
"ilut",
"iluk",
"damped_jacobi",
"gauss_seidel",
"chebyshev"};
178 std::set<std::string> available_solvers = {
"gmres",
"bicgstab",
"cg",
"bicgstabl",
"lgmres",
"fgmres",
"bicgstab_with_gmres_fallback",
"idrs"};
179 std::set<std::string> available_coarsening = {
"ruge_stuben",
"aggregation",
"smoothed_aggregation",
"smoothed_aggr_emin"};
180 std::set<std::string> available_preconditioner = {
"amg",
"relaxation",
"dummy"};
189 mAMGCLParameters.put(
"precond.class", ThisParameters[
"preconditioner_type"].GetString());
190 if(ThisParameters[
"preconditioner_type"].GetString() !=
"amg"){
194 if(ThisParameters[
"preconditioner_type"].GetString() ==
"relaxation")
196 mAMGCLParameters.put(
"precond.type", ThisParameters[
"smoother_type"].GetString());
202 mBlockSize = ThisParameters[
"block_size"].GetInt();
203 mTolerance = ThisParameters[
"tolerance"].GetDouble();
205 mVerbosity=ThisParameters[
"verbosity"].GetInt();
206 mGMRESSize = ThisParameters[
"gmres_krylov_space_dimension"].GetInt();
208 const std::string& solver_type = ThisParameters[
"krylov_type"].GetString();
212 if(solver_type ==
"bicgstab_with_gmres_fallback") {
220 mAMGCLParameters.put(
"precond.relax.type", ThisParameters[
"smoother_type"].GetString());
221 mAMGCLParameters.put(
"precond.coarsening.type", ThisParameters[
"coarsening_type"].GetString());
223 int max_levels = ThisParameters[
"max_levels"].GetInt();
227 mAMGCLParameters.put(
"precond.npre", ThisParameters[
"pre_sweeps"].GetInt());
228 mAMGCLParameters.put(
"precond.npost", ThisParameters[
"post_sweeps"].GetInt());
233 mUseGPGPU = ThisParameters[
"use_gpgpu"].GetBool();
249 int MaxIterationsNumber,
287 int MaxIterationsNumber,
290 bool ProvideCoordinates =
false
353 std::vector<double>
B;
355 int nmodes = amgcl::coarsening::rigid_body_modes(
mBlockSize,
356 boost::make_iterator_range(
361 if (static_block_size != 1 && static_block_size != 3) {
362 KRATOS_WARNING(
"AMGCL Linear Solver") <<
"Can only combine block matrices with coordinates in 3D. Falling back to scalar matrices" << std::endl;
363 static_block_size = 1;
382 std::stringstream matrix_market_name;
383 matrix_market_name <<
"A" <<
".mm";
386 std::stringstream matrix_market_vectname;
387 matrix_market_vectname <<
"b" <<
".mm.rhs";
392 std::ofstream coordsfile;
393 coordsfile.open (
"coordinates.txt");
400 KRATOS_ERROR <<
" Verbosity = 4 prints the matrix and exits" << std::endl;
431 <<
"Iterations: " << iters << std::endl
432 <<
"Error: " << resid << std::endl;
525 unsigned int old_node_id = rDofSet.
size() ? rDofSet.
begin()->Id() : 0;
526 for (
auto it = rDofSet.
begin(); it!=rDofSet.
end(); it++) {
529 if(
id != old_node_id) {
531 if(old_ndof == -1) old_ndof = ndof;
532 else if(old_ndof != ndof) {
554 unsigned int old_node_id = rDofSet.
size() ? rDofSet.
begin()->Id() : 0;
555 for (
auto it = rDofSet.
begin(); it!=rDofSet.
end(); it++) {
556 if(it->EquationId() < system_size && it->GetSolutionStepValue(PARTITION_INDEX) == current_rank) {
558 if(
id != old_node_id) {
560 if(old_ndof == -1) old_ndof = ndof;
561 else if(old_ndof != ndof) {
578 if( old_ndof == -1) {
592 auto it_node = rModelPart.
Nodes().find(it_dof->Id());
619 rOStream <<
"AMGCL solver:";
627 rOStream <<
"Settings: ";
662 const std::string& rOptionName,
663 const std::set<std::string>& rAvailableOptions)
665 if (rAvailableOptions.find(ThisParameters[rOptionName].
GetString()) == rAvailableOptions.end()) {
666 std::stringstream msg;
667 msg <<
"Currently prescribed " << rOptionName <<
" : " << ThisParameters[rOptionName].
GetString() << std::endl;
668 msg <<
"Admissible values are :";
669 for (
const auto& r_name : rAvailableOptions) {
670 msg << std::endl <<
" " << r_name;
672 KRATOS_ERROR <<
"AMGCL Linear Solver : " << rOptionName <<
" is invalid!" << std::endl << msg.str() << std::endl;
818 switch(CoarseningType)
873 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
875 TDenseSpaceType, TReordererType>& rThis)
883 template<
class TSparseSpaceType,
class TDenseSpaceType,
class TReordererType>
886 TDenseSpaceType, TReordererType>& rThis)
888 rThis.PrintInfo(rOStream);
889 rOStream << std::endl;
890 rThis.PrintData(rOStream);
This is a multigrid solver based on the AMGCL library.
Definition: amgcl_solver.h:112
bool mUseBlockMatricesIfPossible
If the coordinates are provided (TODO: Local flag?)
Definition: amgcl_solver.h:705
~AMGCLSolver() override
Definition: amgcl_solver.h:314
virtual double GetResidualNorm()
This method returns the current residual norm.
Definition: amgcl_solver.h:476
void SetIterativeSolverType(const AMGCLIterativeSolverType SolverType)
This method sets the iterative solver to be considered.
Definition: amgcl_solver.h:765
std::vector< array_1d< double, 3 > > mCoordinates
Use GPGPU if available.
Definition: amgcl_solver.h:708
bool Solve(SparseMatrixType &rA, DenseMatrixType &rX, DenseMatrixType &rB) override
Multi solve method for solving a set of linear systems with same coefficient matrix.
Definition: amgcl_solver.h:488
bool mUseGPGPU
If use the bloack matrices if possible (TODO: Local flag?)
Definition: amgcl_solver.h:706
AMGCLSolver(Parameters ThisParameters=Parameters(R"({})"))
This is the default constructor.
Definition: amgcl_solver.h:149
TSparseSpaceType::VectorType VectorType
Vector type definition.
Definition: amgcl_solver.h:127
int mBlockSize
The versoisty level.
Definition: amgcl_solver.h:700
void PrintData(std::ostream &rOStream) const override
Definition: amgcl_solver.h:625
AMGCLSolver(AMGCLSmoother Smoother, AMGCLIterativeSolverType Solver, AMGCLCoarseningType Coarsening, double Tolerance, int MaxIterationsNumber, int Verbosity, int GMRESSize=50, bool ProvideCoordinates=false)
Definition: amgcl_solver.h:282
LinearSolver< TSparseSpaceType, TDenseSpaceType, TReordererType > BaseType
The base class definition.
Definition: amgcl_solver.h:121
double mResidualNorm
The configuration parameters of the AMGCl.
Definition: amgcl_solver.h:712
double mTolerance
Definition: amgcl_solver.h:697
IndexType mMaxIterationsNumber
The tolerance considered.
Definition: amgcl_solver.h:698
KRATOS_CLASS_POINTER_DEFINITION(AMGCLSolver)
Pointer definition of AMGCLSolver.
SizeType mGMRESSize
The size of the dof block.
Definition: amgcl_solver.h:701
IndexType GetIterationsNumber() override
This method returns the current iteration number.
Definition: amgcl_solver.h:467
TSparseSpaceType::IndexType IndexType
The index type definition to be consistent.
Definition: amgcl_solver.h:136
bool mFallbackToGMRES
The level of coarsening allowed.
Definition: amgcl_solver.h:703
bool mUseAMGPreconditioning
The current iteration number.
Definition: amgcl_solver.h:714
AMGCLSolver(const AMGCLSolver &Other)
bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB) override
Normal solve method.
Definition: amgcl_solver.h:331
virtual void SetIterationsNumber(const IndexType IterationsNumber)
This method sets the current iteration number.
Definition: amgcl_solver.h:449
void SetSmootherType(const AMGCLSmoother SmootherType)
This method sets the smother type to be considered.
Definition: amgcl_solver.h:724
AMGCLSolver(AMGCLSmoother Smoother, AMGCLIterativeSolverType Solver, double Tolerance, int MaxIterationsNumber, int Verbosity, int GMRESSize=50)
Default constructor - uses ILU+GMRES.
Definition: amgcl_solver.h:245
void CheckIfSelectedOptionIsAvailable(const Parameters ThisParameters, const std::string &rOptionName, const std::set< std::string > &rAvailableOptions)
Definition: amgcl_solver.h:660
int mVerbosity
The maximum number of iterations considered.
Definition: amgcl_solver.h:699
ModelPart::DofsArrayType DofsArrayType
DofArray type.
Definition: amgcl_solver.h:133
void PrintInfo(std::ostream &rOStream) const override
Definition: amgcl_solver.h:617
TDenseSpaceType::MatrixType DenseMatrixType
Dense matrix type.
Definition: amgcl_solver.h:130
SizeType mCoarseEnough
The size of the GMRES.
Definition: amgcl_solver.h:702
IndexType mIterationsNumber
The current residual norm.
Definition: amgcl_solver.h:713
void SetCoarseningType(const AMGCLCoarseningType CoarseningType)
This method sets the coarsening type to be considered.
Definition: amgcl_solver.h:816
bool mProvideCoordinates
Of consider GMRES as fallback (TODO: Local flag?)
Definition: amgcl_solver.h:704
std::size_t SizeType
The size type definition.
Definition: amgcl_solver.h:139
boost::property_tree::ptree mAMGCLParameters
The vector containing the local coordinates.
Definition: amgcl_solver.h:710
bool AdditionalPhysicalDataIsNeeded() override
Some solvers may require a minimum degree of knowledge of the structure of the matrix....
Definition: amgcl_solver.h:500
virtual void SetResidualNorm(const double ResidualNorm)
This method sets the current residual norm.
Definition: amgcl_solver.h:458
TSparseSpaceType::MatrixType SparseMatrixType
The sparse matric type.
Definition: amgcl_solver.h:124
void ProvideAdditionalData(SparseMatrixType &rA, VectorType &rX, VectorType &rB, DofsArrayType &rDofSet, ModelPart &rModelPart) override
Some solvers may require a minimum degree of knowledge of the structure of the matrix.
Definition: amgcl_solver.h:512
AMGCLSolver & operator=(const AMGCLSolver &Other)
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Communicator & GetCommunicator()
Definition: model_part.h:1821
bool IsDistributed() const
Definition: model_part.h:1898
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
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
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
TMatrixType MatrixType
The matrix type considered.
Definition: ublas_space.h:133
std::size_t IndexType
The index type considered.
Definition: ublas_space.h:139
TVectorType VectorType
The vector type considered.
Definition: ublas_space.h:136
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
#define KRATOS_WARNING(label)
Definition: logger.h:265
#define KRATOS_API(...)
Definition: kratos_export_api.h:40
std::size_t IndexType
Definition: binary_expression.cpp:25
TSpaceType::IndexType Size1(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:117
TSpaceType::IndexType Size2(TSpaceType &dummy, typename TSpaceType::MatrixType const &rM)
Definition: add_strategies_to_python.cpp:123
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AMGCLSolve(int block_size, TUblasSparseSpace< double >::MatrixType &rA, TUblasSparseSpace< double >::VectorType &rX, TUblasSparseSpace< double >::VectorType &rB, TUblasSparseSpace< double >::IndexType &rIterationNumber, double &rResidual, boost::property_tree::ptree amgclParams, int verbosity_level, bool use_gpgpu)
This function solves with Ublas Matrix type.
Definition: amgcl_solver_impl.cpp:189
bool WriteMatrixMarketVector(const char *FileName, VectorType &V)
Definition: matrix_market_interface.h:539
bool WriteMatrixMarketMatrix(const char *FileName, CompressedMatrixType &M, bool Symmetric)
Definition: matrix_market_interface.h:308
AMGCLSmoother
Definition: amgcl_solver.h:55
@ GAUSS_SEIDEL
Definition: amgcl_solver.h:56
@ CHEBYSHEV
Definition: amgcl_solver.h:56
@ ILU0
Definition: amgcl_solver.h:56
@ DAMPED_JACOBI
Definition: amgcl_solver.h:56
@ SPAI1
Definition: amgcl_solver.h:56
@ SPAI0
Definition: amgcl_solver.h:56
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
AMGCLCoarseningType
Definition: amgcl_solver.h:65
@ SA
Definition: amgcl_solver.h:66
@ RUGE_STUBEN
Definition: amgcl_solver.h:66
@ AGGREGATION
Definition: amgcl_solver.h:66
@ SA_EMIN
Definition: amgcl_solver.h:66
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
AMGCLIterativeSolverType
Definition: amgcl_solver.h:60
@ BICGSTAB
Definition: amgcl_solver.h:61
@ CG
Definition: amgcl_solver.h:61
@ BICGSTAB_WITH_GMRES_FALLBACK
Definition: amgcl_solver.h:61
@ FGMRES
Definition: amgcl_solver.h:61
@ LGMRES
Definition: amgcl_solver.h:61
@ BICGSTAB2
Definition: amgcl_solver.h:61
@ GMRES
Definition: amgcl_solver.h:61
string SolverType
Definition: fluid_only_var.py:5
int block_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:16
B
Definition: sensitivityMatrix.py:76
integer i
Definition: TensorModule.f:17