deal.II version GIT relicensing-2529-g41720a6c44 2025-02-01 18:50:00+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\}}\)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 parameter_list.set("initialize random seed", true);
98
99 parameter_list.set("smoother: sweeps", static_cast<int>(smoother_sweeps));
100 parameter_list.set("cycle applications", static_cast<int>(n_cycles));
101 if (w_cycle == true)
102 parameter_list.set("prec type", "MGW");
103 else
104 parameter_list.set("prec type", "MGV");
105
106 parameter_list.set("smoother: Chebyshev alpha", 10.);
107 parameter_list.set("smoother: ifpack overlap",
108 static_cast<int>(smoother_overlap));
109 parameter_list.set("aggregation: threshold", aggregation_threshold);
110 parameter_list.set("coarse: max size", 2000);
111
112 if (output_details)
113 parameter_list.set("ML output", 10);
114 else
115 parameter_list.set("ML output", 0);
116
117 set_operator_null_space(parameter_list, distributed_constant_modes, matrix);
118 }
119
120
121
122 void
124 Teuchos::ParameterList &parameter_list,
125 std::unique_ptr<Epetra_MultiVector> &ptr_distributed_constant_modes,
126 const Epetra_RowMatrix &matrix) const
127 {
128 const auto run = [&](const auto &constant_modes) {
129 const Epetra_Map &domain_map = matrix.OperatorDomainMap();
130
131 const size_type constant_modes_dimension = constant_modes.size();
132 ptr_distributed_constant_modes =
133 std::make_unique<Epetra_MultiVector>(domain_map,
134 constant_modes_dimension > 0 ?
135 constant_modes_dimension :
136 1);
137 Assert(ptr_distributed_constant_modes, ExcNotInitialized());
138 Epetra_MultiVector &distributed_constant_modes =
139 *ptr_distributed_constant_modes;
140
141 if (constant_modes_dimension > 0)
142 {
143 const size_type global_size = TrilinosWrappers::n_global_rows(matrix);
144 (void)global_length; // work around compiler warning about unused
145 // function in release mode
146 Assert(global_size ==
148 distributed_constant_modes)),
149 ExcDimensionMismatch(global_size,
151 distributed_constant_modes)));
152 const bool constant_modes_are_global =
153 constant_modes[0].size() == global_size;
154 const size_type my_size = domain_map.NumMyElements();
155
156 // Reshape null space as a contiguous vector of doubles so that
157 // Trilinos can read from it.
158 const size_type expected_mode_size =
159 constant_modes_are_global ? global_size : my_size;
160 for (size_type d = 0; d < constant_modes_dimension; ++d)
161 {
162 Assert(constant_modes[d].size() == expected_mode_size,
163 ExcDimensionMismatch(constant_modes[d].size(),
164 expected_mode_size));
165 for (size_type row = 0; row < my_size; ++row)
166 {
167 const TrilinosWrappers::types::int_type mode_index =
168 constant_modes_are_global ?
169 TrilinosWrappers::global_index(domain_map, row) :
170 row;
171 distributed_constant_modes[d][row] =
172 static_cast<double>(constant_modes[d][mode_index]);
173 }
174 }
175 (void)expected_mode_size;
176
177 parameter_list.set("null space: type", "pre-computed");
178 parameter_list.set("null space: dimension",
179 distributed_constant_modes.NumVectors());
180 parameter_list.set("null space: vectors",
181 distributed_constant_modes.Values());
182 }
183 };
184
185 if (!constant_modes_values.empty())
186 {
187 AssertDimension(constant_modes.size(), 0);
188 run(constant_modes_values);
189 }
190 else
191 run(constant_modes);
192 }
193
194
195
196 void
198 Teuchos::ParameterList &parameter_list,
199 std::unique_ptr<Epetra_MultiVector> &distributed_constant_modes,
200 const SparseMatrix &matrix) const
201 {
202 return set_parameters(parameter_list,
203 distributed_constant_modes,
204 matrix.trilinos_matrix());
205 }
206
207
208
209 void
211 Teuchos::ParameterList &parameter_list,
212 std::unique_ptr<Epetra_MultiVector> &distributed_constant_modes,
213 const SparseMatrix &matrix) const
214 {
215 return set_operator_null_space(parameter_list,
216 distributed_constant_modes,
217 matrix.trilinos_matrix());
218 }
219
220
221
223 {
224 preconditioner.reset();
225 trilinos_matrix.reset();
226 }
227
228
229
230 void
232 const AdditionalData &additional_data)
233 {
234 initialize(matrix.trilinos_matrix(), additional_data);
235 }
236
237
238
239 void
240 PreconditionAMG::initialize(const Epetra_RowMatrix &matrix,
241 const AdditionalData &additional_data)
242 {
243 // Build the AMG preconditioner.
244 Teuchos::ParameterList ml_parameters;
245 std::unique_ptr<Epetra_MultiVector> distributed_constant_modes;
246 additional_data.set_parameters(ml_parameters,
247 distributed_constant_modes,
248 matrix);
249
250 initialize(matrix, ml_parameters);
251
252 if (additional_data.output_details)
253 {
254 ML_Epetra::MultiLevelPreconditioner *multilevel_operator =
255 dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(
256 preconditioner.get());
257 Assert(multilevel_operator != nullptr,
258 ExcMessage("Preconditioner setup failed."));
259 multilevel_operator->PrintUnused(0);
260 }
261 }
262
263
264
265 void
267 const Teuchos::ParameterList &ml_parameters)
268 {
269 initialize(matrix.trilinos_matrix(), ml_parameters);
270 }
271
272
273
274 void
275 PreconditionAMG::initialize(const Epetra_RowMatrix &matrix,
276 const Teuchos::ParameterList &ml_parameters)
277 {
278 preconditioner.reset(
279 new ML_Epetra::MultiLevelPreconditioner(matrix, ml_parameters));
280 }
281
282
283
284 template <typename number>
285 void
287 const ::SparseMatrix<number> &deal_ii_sparse_matrix,
288 const AdditionalData &additional_data,
289 const double drop_tolerance,
290 const ::SparsityPattern *use_this_sparsity)
291 {
292 preconditioner.reset();
293 const size_type n_rows = deal_ii_sparse_matrix.m();
294
295 // Init Epetra Matrix using an equidistributed map; avoid storing the
296 // nonzero elements.
297 IndexSet distributor(n_rows);
298 const unsigned int n_mpi_processes = communicator.NumProc();
299 const unsigned int my_id = communicator.MyPID();
300 distributor.add_range(my_id * n_rows / n_mpi_processes,
301 (my_id + 1) * n_rows / n_mpi_processes);
302
303 if (trilinos_matrix.get() == nullptr)
304 trilinos_matrix = std::make_shared<SparseMatrix>();
305
306 trilinos_matrix->reinit(distributor,
307 distributor,
308 deal_ii_sparse_matrix,
309 communicator.Comm(),
310 drop_tolerance,
311 true,
312 use_this_sparsity);
313
314 initialize(*trilinos_matrix, additional_data);
315 }
316
317
318
319 void
321 {
322 ML_Epetra::MultiLevelPreconditioner *multilevel_operator =
323 dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(preconditioner.get());
324 multilevel_operator->ReComputePreconditioner();
325 }
326
327
328
329 void
335
336
337
340 {
341 unsigned int memory = sizeof(*this);
342
343 // todo: find a way to read out ML's data
344 // sizes
345 if (trilinos_matrix.get() != nullptr)
346 memory += trilinos_matrix->memory_consumption();
347 return memory;
348 }
349
350
351
352# ifndef DOXYGEN
353 // explicit instantiations
354 template void
355 PreconditionAMG::initialize(const ::SparseMatrix<double> &,
356 const AdditionalData &,
357 const double,
358 const ::SparsityPattern *);
359 template void
360 PreconditionAMG::initialize(const ::SparseMatrix<float> &,
361 const AdditionalData &,
362 const double,
363 const ::SparsityPattern *);
364# endif
365
366
367
368} // namespace TrilinosWrappers
369
371
372#endif // DEAL_II_WITH_TRILINOS
void add_range(const size_type begin, const size_type end)
Definition index_set.h:1813
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:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:734
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