Reference documentation for deal.II version GIT 9042b9283b 2023-12-02 14:50: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\}}\)
mg_coarse.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mg_coarse_h
17 #define dealii_mg_coarse_h
18 
19 
20 #include <deal.II/base/config.h>
21 
25 
27 
29 
39 template <typename VectorType = Vector<double>>
40 class MGCoarseGridApplySmoother : public MGCoarseGridBase<VectorType>
41 {
42 public:
47 
52 
56  void
57  clear();
58 
62  void
64 
68  void
69  operator()(const unsigned int level,
70  VectorType &dst,
71  const VectorType &src) const override;
72 
73 private:
80 };
81 
82 
83 
90 template <typename VectorType,
91  typename SolverType,
92  typename MatrixType,
93  typename PreconditionerType>
95 {
96 public:
101 
107  const MatrixType &matrix,
108  const PreconditionerType &precondition);
109 
114  void
115  initialize(SolverType &solver,
116  const MatrixType &matrix,
117  const PreconditionerType &precondition);
118 
122  void
123  clear();
124 
129  virtual void
130  operator()(const unsigned int level,
131  VectorType &dst,
132  const VectorType &src) const override;
133 
134 private:
138  SmartPointer<SolverType,
139  MGCoarseGridIterativeSolver<VectorType,
140  SolverType,
141  MatrixType,
142  PreconditionerType>>
144 
148  SmartPointer<const MatrixType,
149  MGCoarseGridIterativeSolver<VectorType,
150  SolverType,
151  MatrixType,
152  PreconditionerType>>
154 
158  SmartPointer<const PreconditionerType,
159  MGCoarseGridIterativeSolver<VectorType,
160  SolverType,
161  MatrixType,
162  PreconditionerType>>
164 };
165 
166 
167 
176 template <typename number = double, typename VectorType = Vector<number>>
177 class MGCoarseGridHouseholder : public MGCoarseGridBase<VectorType>
178 {
179 public:
184 
188  void
190 
191  void
192  operator()(const unsigned int level,
193  VectorType &dst,
194  const VectorType &src) const override;
195 
196 private:
201 };
202 
209 template <typename number = double, typename VectorType = Vector<number>>
210 class MGCoarseGridSVD : public MGCoarseGridBase<VectorType>
211 {
212 public:
216  MGCoarseGridSVD() = default;
217 
221  void
222  initialize(const FullMatrix<number> &A, const double threshold = 0);
223 
224  void
225  operator()(const unsigned int level,
226  VectorType &dst,
227  const VectorType &src) const;
228 
232  void
233  log() const;
234 
235 private:
240 };
241 
244 #ifndef DOXYGEN
245 /* ------------------ Functions for MGCoarseGridApplySmoother -----------*/
246 template <typename VectorType>
248  : coarse_smooth(nullptr)
249 {}
250 
251 template <typename VectorType>
253  const MGSmootherBase<VectorType> &coarse_smooth)
254  : coarse_smooth(nullptr)
255 {
256  initialize(coarse_smooth);
257 }
258 
259 
260 template <typename VectorType>
261 void
263  const MGSmootherBase<VectorType> &coarse_smooth_)
264 {
265  coarse_smooth =
267  MGCoarseGridApplySmoother<VectorType>>(&coarse_smooth_,
268  typeid(*this).name());
269 }
270 
271 
272 template <typename VectorType>
273 void
275 {
276  coarse_smooth = nullptr;
277 }
278 
279 
280 template <typename VectorType>
281 void
283  VectorType &dst,
284  const VectorType &src) const
285 {
286  coarse_smooth->apply(level, dst, src);
287 }
288 
289 /* ------------------ Functions for MGCoarseGridIterativeSolver ------------ */
290 
291 template <typename VectorType,
292  typename SolverType,
293  typename MatrixType,
294  typename PreconditionerType>
295 MGCoarseGridIterativeSolver<VectorType,
296  SolverType,
297  MatrixType,
298  PreconditionerType>::MGCoarseGridIterativeSolver()
299  : solver(0, typeid(*this).name())
300  , matrix(0, typeid(*this).name())
301  , preconditioner(0, typeid(*this).name())
302 {}
303 
304 
305 
306 template <typename VectorType,
307  typename SolverType,
308  typename MatrixType,
309  typename PreconditionerType>
310 MGCoarseGridIterativeSolver<VectorType,
311  SolverType,
312  MatrixType,
313  PreconditionerType>::
314  MGCoarseGridIterativeSolver(SolverType &solver,
315  const MatrixType &matrix,
316  const PreconditionerType &preconditioner)
317  : solver(&solver, typeid(*this).name())
318  , matrix(&matrix, typeid(*this).name())
319  , preconditioner(&preconditioner, typeid(*this).name())
320 {}
321 
322 
323 
324 template <typename VectorType,
325  typename SolverType,
326  typename MatrixType,
327  typename PreconditionerType>
328 void
330  VectorType,
331  SolverType,
332  MatrixType,
333  PreconditionerType>::initialize(SolverType &solver_,
334  const MatrixType &matrix_,
335  const PreconditionerType &preconditioner_)
336 {
337  solver = &solver_;
338  matrix = &matrix_;
339  preconditioner = &preconditioner_;
340 }
341 
342 
343 
344 template <typename VectorType,
345  typename SolverType,
346  typename MatrixType,
347  typename PreconditionerType>
348 void
349 MGCoarseGridIterativeSolver<VectorType,
350  SolverType,
351  MatrixType,
352  PreconditionerType>::clear()
353 {
354  solver = 0;
355  matrix = 0;
356  preconditioner = 0;
357 }
358 
359 
360 
361 namespace internal
362 {
364  {
365  template <typename VectorType,
366  typename SolverType,
367  typename MatrixType,
368  typename PreconditionerType,
369  std::enable_if_t<
370  std::is_same_v<VectorType, typename SolverType::vector_type>,
371  VectorType> * = nullptr>
372  void
373  solve(SolverType &solver,
374  const MatrixType &matrix,
375  const PreconditionerType &preconditioner,
376  VectorType &dst,
377  const VectorType &src)
378  {
379  solver.solve(matrix, dst, src, preconditioner);
380  }
381 
382  template <typename VectorType,
383  typename SolverType,
384  typename MatrixType,
385  typename PreconditionerType,
386  std::enable_if_t<
387  !std::is_same_v<VectorType, typename SolverType::vector_type>,
388  VectorType> * = nullptr>
389  void
390  solve(SolverType &solver,
391  const MatrixType &matrix,
392  const PreconditionerType &preconditioner,
393  VectorType &dst,
394  const VectorType &src)
395  {
396  typename SolverType::vector_type src_;
397  typename SolverType::vector_type dst_;
398 
399  src_ = src;
400  dst_ = dst;
401 
402  solver.solve(matrix, dst_, src_, preconditioner);
403 
404  dst = dst_;
405  }
406  } // namespace MGCoarseGridIterativeSolver
407 } // namespace internal
408 
409 
410 
411 template <typename VectorType,
412  typename SolverType,
413  typename MatrixType,
414  typename PreconditionerType>
415 void
417  VectorType,
418  SolverType,
419  MatrixType,
420  PreconditionerType>::operator()(const unsigned int /*level*/,
421  VectorType &dst,
422  const VectorType &src) const
423 {
424  Assert(solver != nullptr, ExcNotInitialized());
425  Assert(matrix != nullptr, ExcNotInitialized());
426  Assert(preconditioner != nullptr, ExcNotInitialized());
427 
428  dst = 0;
429  internal::MGCoarseGridIterativeSolver::solve(
430  *solver, *matrix, *preconditioner, dst, src);
431 }
432 
433 
434 
435 /* ------------------ Functions for MGCoarseGridHouseholder ------------ */
436 
437 template <typename number, typename VectorType>
439  const FullMatrix<number> *A)
440 {
441  if (A != nullptr)
442  householder.initialize(*A);
443 }
444 
445 
446 
447 template <typename number, typename VectorType>
448 void
450  const FullMatrix<number> &A)
451 {
452  householder.initialize(A);
453 }
454 
455 
456 
457 template <typename number, typename VectorType>
458 void
460  const unsigned int /*level*/,
461  VectorType &dst,
462  const VectorType &src) const
463 {
464  householder.least_squares(dst, src);
465 }
466 
467 //---------------------------------------------------------------------------
468 
469 
470 
471 template <typename number, typename VectorType>
472 void
474  double threshold)
475 {
476  matrix.reinit(A.n_rows(), A.n_cols());
477  matrix = A;
478  matrix.compute_inverse_svd(threshold);
479 }
480 
481 
482 template <typename number, typename VectorType>
483 void
484 MGCoarseGridSVD<number, VectorType>::operator()(const unsigned int /*level*/,
485  VectorType &dst,
486  const VectorType &src) const
487 {
488  matrix.vmult(dst, src);
489 }
490 
491 
492 template <typename number, typename VectorType>
493 void
495 {
496  const unsigned int n = std::min(matrix.n_rows(), matrix.n_cols());
497 
498  for (unsigned int i = 0; i < n; ++i)
499  deallog << ' ' << matrix.singular_value(i);
500  deallog << std::endl;
501 }
502 
503 
504 #endif // DOXYGEN
505 
507 
508 #endif
void initialize(const MGSmootherBase< VectorType > &coarse_smooth)
void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override
SmartPointer< const MGSmootherBase< VectorType >, MGCoarseGridApplySmoother< VectorType > > coarse_smooth
Definition: mg_coarse.h:79
MGCoarseGridApplySmoother(const MGSmootherBase< VectorType > &coarse_smooth)
void initialize(const FullMatrix< number > &A)
Householder< number > householder
Definition: mg_coarse.h:200
MGCoarseGridHouseholder(const FullMatrix< number > *A=nullptr)
void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override
SmartPointer< const MatrixType, MGCoarseGridIterativeSolver< VectorType, SolverType, MatrixType, PreconditionerType > > matrix
Definition: mg_coarse.h:153
SmartPointer< SolverType, MGCoarseGridIterativeSolver< VectorType, SolverType, MatrixType, PreconditionerType > > solver
Definition: mg_coarse.h:143
SmartPointer< const PreconditionerType, MGCoarseGridIterativeSolver< VectorType, SolverType, MatrixType, PreconditionerType > > preconditioner
Definition: mg_coarse.h:163
void initialize(SolverType &solver, const MatrixType &matrix, const PreconditionerType &precondition)
virtual void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override
MGCoarseGridIterativeSolver(SolverType &solver, const MatrixType &matrix, const PreconditionerType &precondition)
void log() const
MGCoarseGridSVD()=default
LAPACKFullMatrix< number > matrix
Definition: mg_coarse.h:239
void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const
void initialize(const FullMatrix< number > &A, const double threshold=0)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
unsigned int level
Definition: grid_out.cc:4617
static ::ExceptionBase & ExcNotInitialized()
#define Assert(cond, exc)
Definition: exceptions.h:1631
LogStream deallog
Definition: logstream.cc:37
@ matrix
Contents is actually a matrix.
static const char A