Reference documentation for deal.II version GIT relicensing-43-g3fc2fae7c3 2024-03-03 21:10:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
trilinos_precondition_ml.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2015 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
17
18#ifdef DEAL_II_WITH_TRILINOS
19
22# include <deal.II/lac/vector.h>
23
24# include <Epetra_MultiVector.h>
25# include <Ifpack.h>
26# include <Ifpack_Chebyshev.h>
27# include <Teuchos_ParameterList.hpp>
28# include <Teuchos_RCP.hpp>
29# include <ml_MultiLevelPreconditioner.h>
30# include <ml_include.h>
31
33
34namespace TrilinosWrappers
35{
36 /* -------------------------- PreconditionAMG -------------------------- */
37
39 const bool elliptic,
40 const bool higher_order_elements,
41 const unsigned int n_cycles,
42 const bool w_cycle,
43 const double aggregation_threshold,
44 const std::vector<std::vector<bool>> &constant_modes,
45 const unsigned int smoother_sweeps,
46 const unsigned int smoother_overlap,
47 const bool output_details,
48 const char *smoother_type,
49 const char *coarse_type)
50 : elliptic(elliptic)
51 , higher_order_elements(higher_order_elements)
52 , n_cycles(n_cycles)
53 , w_cycle(w_cycle)
54 , aggregation_threshold(aggregation_threshold)
55 , constant_modes(constant_modes)
56 , smoother_sweeps(smoother_sweeps)
57 , smoother_overlap(smoother_overlap)
58 , output_details(output_details)
59 , smoother_type(smoother_type)
60 , coarse_type(coarse_type)
61 {}
62
63
64
65 void
67 Teuchos::ParameterList &parameter_list,
68 std::unique_ptr<Epetra_MultiVector> &distributed_constant_modes,
69 const Epetra_RowMatrix &matrix) const
70 {
71 if (elliptic == true)
72 {
73 ML_Epetra::SetDefaults("SA", parameter_list);
74
75 // uncoupled mode can give a lot of warnings or even fail when there
76 // are too many entries per row and aggregation gets complicated, but
77 // MIS does not work if too few elements are located on one
78 // processor. work around these warnings by choosing the different
79 // strategies in different situations: for low order, always use the
80 // standard choice uncoupled. if higher order, right now we also just
81 // use Uncoupled, but we should be aware that maybe MIS might be
82 // needed
83 if (higher_order_elements)
84 parameter_list.set("aggregation: type", "Uncoupled");
85 }
86 else
87 {
88 ML_Epetra::SetDefaults("NSSA", parameter_list);
89 parameter_list.set("aggregation: type", "Uncoupled");
90 parameter_list.set("aggregation: block scaling", true);
91 }
92
93 parameter_list.set("smoother: type", smoother_type);
94 parameter_list.set("coarse: type", coarse_type);
95
96 // Force re-initialization of the random seed to make ML deterministic
97 // (only supported in trilinos >12.2):
98# if DEAL_II_TRILINOS_VERSION_GTE(12, 4, 0)
99 parameter_list.set("initialize random seed", true);
100# endif
101
102 parameter_list.set("smoother: sweeps", static_cast<int>(smoother_sweeps));
103 parameter_list.set("cycle applications", static_cast<int>(n_cycles));
104 if (w_cycle == true)
105 parameter_list.set("prec type", "MGW");
106 else
107 parameter_list.set("prec type", "MGV");
108
109 parameter_list.set("smoother: Chebyshev alpha", 10.);
110 parameter_list.set("smoother: ifpack overlap",
111 static_cast<int>(smoother_overlap));
112 parameter_list.set("aggregation: threshold", aggregation_threshold);
113 parameter_list.set("coarse: max size", 2000);
114
115 if (output_details)
116 parameter_list.set("ML output", 10);
117 else
118 parameter_list.set("ML output", 0);
119
120 set_operator_null_space(parameter_list, distributed_constant_modes, matrix);
121 }
122
123
124
125 void
127 Teuchos::ParameterList &parameter_list,
128 std::unique_ptr<Epetra_MultiVector> &ptr_distributed_constant_modes,
129 const Epetra_RowMatrix &matrix) const
130 {
131 const Epetra_Map &domain_map = matrix.OperatorDomainMap();
132
133 const size_type constant_modes_dimension = constant_modes.size();
134 ptr_distributed_constant_modes = std::make_unique<Epetra_MultiVector>(
135 domain_map, constant_modes_dimension > 0 ? constant_modes_dimension : 1);
136 Assert(ptr_distributed_constant_modes, ExcNotInitialized());
137 Epetra_MultiVector &distributed_constant_modes =
138 *ptr_distributed_constant_modes;
139
140 if (constant_modes_dimension > 0)
141 {
142 const size_type global_size = TrilinosWrappers::n_global_rows(matrix);
143 (void)global_length; // work around compiler warning about unused
144 // function in release mode
145 Assert(global_size ==
146 static_cast<size_type>(
147 TrilinosWrappers::global_length(distributed_constant_modes)),
148 ExcDimensionMismatch(global_size,
150 distributed_constant_modes)));
151 const bool constant_modes_are_global =
152 constant_modes[0].size() == global_size;
153 const size_type my_size = domain_map.NumMyElements();
154
155 // Reshape null space as a contiguous vector of doubles so that
156 // Trilinos can read from it.
157 const size_type expected_mode_size =
158 constant_modes_are_global ? global_size : my_size;
159 for (size_type d = 0; d < constant_modes_dimension; ++d)
160 {
161 Assert(constant_modes[d].size() == expected_mode_size,
162 ExcDimensionMismatch(constant_modes[d].size(),
163 expected_mode_size));
164 for (size_type row = 0; row < my_size; ++row)
165 {
166 const TrilinosWrappers::types::int_type mode_index =
167 constant_modes_are_global ?
168 TrilinosWrappers::global_index(domain_map, row) :
169 row;
170 distributed_constant_modes[d][row] =
171 static_cast<double>(constant_modes[d][mode_index]);
172 }
173 }
174 (void)expected_mode_size;
175
176 parameter_list.set("null space: type", "pre-computed");
177 parameter_list.set("null space: dimension",
178 distributed_constant_modes.NumVectors());
179 parameter_list.set("null space: vectors",
180 distributed_constant_modes.Values());
181 }
182 }
183
184
185
186 void
188 Teuchos::ParameterList &parameter_list,
189 std::unique_ptr<Epetra_MultiVector> &distributed_constant_modes,
190 const SparseMatrix &matrix) const
191 {
192 return set_parameters(parameter_list,
193 distributed_constant_modes,
194 matrix.trilinos_matrix());
195 }
196
197
198
199 void
201 Teuchos::ParameterList &parameter_list,
202 std::unique_ptr<Epetra_MultiVector> &distributed_constant_modes,
203 const SparseMatrix &matrix) const
204 {
205 return set_operator_null_space(parameter_list,
206 distributed_constant_modes,
207 matrix.trilinos_matrix());
208 }
209
210
211
213 {
214 preconditioner.reset();
215 trilinos_matrix.reset();
216 }
217
218
219
220 void
222 const AdditionalData &additional_data)
223 {
224 initialize(matrix.trilinos_matrix(), additional_data);
225 }
226
227
228
229 void
230 PreconditionAMG::initialize(const Epetra_RowMatrix &matrix,
231 const AdditionalData &additional_data)
232 {
233 // Build the AMG preconditioner.
234 Teuchos::ParameterList ml_parameters;
235 std::unique_ptr<Epetra_MultiVector> distributed_constant_modes;
236 additional_data.set_parameters(ml_parameters,
237 distributed_constant_modes,
238 matrix);
239
240 initialize(matrix, ml_parameters);
241
242 if (additional_data.output_details)
243 {
244 ML_Epetra::MultiLevelPreconditioner *multilevel_operator =
245 dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(
246 preconditioner.get());
247 Assert(multilevel_operator != nullptr,
248 ExcMessage("Preconditioner setup failed."));
249 multilevel_operator->PrintUnused(0);
250 }
251 }
252
253
254
255 void
257 const Teuchos::ParameterList &ml_parameters)
258 {
259 initialize(matrix.trilinos_matrix(), ml_parameters);
260 }
261
262
263
264 void
265 PreconditionAMG::initialize(const Epetra_RowMatrix &matrix,
266 const Teuchos::ParameterList &ml_parameters)
267 {
268 preconditioner.reset(
269 new ML_Epetra::MultiLevelPreconditioner(matrix, ml_parameters));
270 }
271
272
273
274 template <typename number>
275 void
277 const ::SparseMatrix<number> &deal_ii_sparse_matrix,
278 const AdditionalData &additional_data,
279 const double drop_tolerance,
280 const ::SparsityPattern *use_this_sparsity)
281 {
282 preconditioner.reset();
283 const size_type n_rows = deal_ii_sparse_matrix.m();
284
285 // Init Epetra Matrix using an equidistributed map; avoid storing the
286 // nonzero elements.
287 IndexSet distributor(n_rows);
288 const unsigned int n_mpi_processes = communicator.NumProc();
289 const unsigned int my_id = communicator.MyPID();
290 distributor.add_range(my_id * n_rows / n_mpi_processes,
291 (my_id + 1) * n_rows / n_mpi_processes);
292
293 if (trilinos_matrix.get() == nullptr)
294 trilinos_matrix = std::make_shared<SparseMatrix>();
295
296 trilinos_matrix->reinit(distributor,
297 distributor,
298 deal_ii_sparse_matrix,
299 communicator.Comm(),
300 drop_tolerance,
301 true,
302 use_this_sparsity);
303
304 initialize(*trilinos_matrix, additional_data);
305 }
306
307
308
309 void
311 {
312 ML_Epetra::MultiLevelPreconditioner *multilevel_operator =
313 dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(preconditioner.get());
314 multilevel_operator->ReComputePreconditioner();
315 }
316
317
318
319 void
325
326
327
330 {
331 unsigned int memory = sizeof(*this);
332
333 // todo: find a way to read out ML's data
334 // sizes
335 if (trilinos_matrix.get() != nullptr)
336 memory += trilinos_matrix->memory_consumption();
337 return memory;
338 }
339
340
341
342# ifndef DOXYGEN
343 // explicit instantiations
344 template void
345 PreconditionAMG::initialize(const ::SparseMatrix<double> &,
346 const AdditionalData &,
347 const double,
348 const ::SparsityPattern *);
349 template void
350 PreconditionAMG::initialize(const ::SparseMatrix<float> &,
351 const AdditionalData &,
352 const double,
353 const ::SparsityPattern *);
354# endif
355
356
357
358} // namespace TrilinosWrappers
359
361
362#endif // DEAL_II_WITH_TRILINOS
void add_range(const size_type begin, const size_type end)
Definition index_set.h:1792
std::shared_ptr< SparseMatrix > trilinos_matrix
void initialize(const SparseMatrix &matrix, const AdditionalData &additional_data=AdditionalData())
Teuchos::RCP< Epetra_Operator > preconditioner
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
TrilinosWrappers::types::int_type global_index(const Epetra_BlockMap &map, const ::types::global_dof_index i)
TrilinosWrappers::types::int_type n_global_rows(const Epetra_CrsGraph &graph)
TrilinosWrappers::types::int_type global_length(const Epetra_MultiVector &vector)
AdditionalData(const bool elliptic=true, const bool higher_order_elements=false, const unsigned int n_cycles=1, const bool w_cycle=false, const double aggregation_threshold=1e-4, const std::vector< std::vector< bool > > &constant_modes=std::vector< std::vector< bool > >(0), const unsigned int smoother_sweeps=2, const unsigned int smoother_overlap=0, const bool output_details=false, const char *smoother_type="Chebyshev", const char *coarse_type="Amesos-KLU")
void set_operator_null_space(Teuchos::ParameterList &parameter_list, std::unique_ptr< Epetra_MultiVector > &distributed_constant_modes, const Epetra_RowMatrix &matrix) const
void set_parameters(Teuchos::ParameterList &parameter_list, std::unique_ptr< Epetra_MultiVector > &distributed_constant_modes, const Epetra_RowMatrix &matrix) const