MG-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: MultiGrid-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Multi-grid wrapper functions for CubbyFlow.
6 > Created Time: 2017/09/26
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MULTI_GRID_IMPL_H
10 #define CUBBYFLOW_MULTI_GRID_IMPL_H
11 
12 namespace CubbyFlow
13 {
14  namespace Internal
15  {
16  template <typename BlasType>
19  unsigned int currentLevel, MGVector<BlasType>* x,
21  {
22  // 1) Relax a few times on Ax = b, with arbitrary x
23  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
25  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
26 
27  // 2) if currentLevel is the coarsest grid, goto 5)
28  if (currentLevel < A.levels.size() - 1)
29  {
30  auto r = buffer;
31  BlasType::Residual(A[currentLevel], (*x)[currentLevel], (*b)[currentLevel], &(*r)[currentLevel]);
32  params.restrictFunc((*r)[currentLevel], &(*b)[currentLevel + 1]);
33 
34  BlasType::Set(0.0, &(*x)[currentLevel + 1]);
35 
36  params.maxTolerance *= 0.5;
37  // Solve Ae = r
38  MGVCycle(A, params, currentLevel + 1, x, b, buffer);
39  params.maxTolerance *= 2.0;
40 
41  // 3) correct
42  params.correctFunc((*x)[currentLevel + 1], &(*x)[currentLevel]);
43 
44  // 4) relax nIter times on Ax = b, with initial guess x
45  if (currentLevel > 0)
46  {
47  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
48  params.numberOfCorrectionIter, params.maxTolerance,
49  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
50  }
51  else
52  {
53  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
54  params.numberOfFinalIter, params.maxTolerance,
55  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
56  }
57  }
58  else
59  {
60  // 5) solve directly with initial guess x
61  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
62  params.numberOfCoarsestIter, params.maxTolerance,
63  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
64 
65  BlasType::Residual(A[currentLevel], (*x)[currentLevel], (*b)[currentLevel], &(*buffer)[currentLevel]);
66  }
67 
68  BlasType::Residual(A[currentLevel], (*x)[currentLevel], (*b)[currentLevel], &(*buffer)[currentLevel]);
69 
70  MGResult result;
71  result.lastResidualNorm = BlasType::L2Norm((*buffer)[currentLevel]);
72  return result;
73  }
74  }
75 
76  template <typename BlasType>
77  const typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](size_t i) const
78  {
79  return levels[i];
80  }
81 
82  template <typename BlasType>
83  typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](size_t i)
84  {
85  return levels[i];
86  }
87 
88  template <typename BlasType>
89  const typename BlasType::MatrixType& MGMatrix<BlasType>::Finest() const
90  {
91  return levels.Front();
92  }
93 
94  template <typename BlasType>
95  typename BlasType::MatrixType& MGMatrix<BlasType>::Finest()
96  {
97  return levels.Front();
98  }
99 
100  template <typename BlasType>
101  const typename BlasType::VectorType& MGVector<BlasType>::operator[](size_t i) const
102  {
103  return levels[i];
104  }
105 
106  template <typename BlasType>
107  typename BlasType::VectorType& MGVector<BlasType>::operator[](size_t i)
108  {
109  return levels[i];
110  }
111 
112  template <typename BlasType>
113  const typename BlasType::VectorType& MGVector<BlasType>::Finest() const
114  {
115  return levels.Front();
116  }
117 
118  template <typename BlasType>
119  typename BlasType::VectorType& MGVector<BlasType>::Finest()
120  {
121  return levels.Front();
122  }
123 
124  template <typename BlasType>
128  MGVector<BlasType>* buffer)
129  {
130  return Internal::MGVCycle<BlasType>(A, params, 0u, x, b, buffer);
131  }
132 }
133 
134 #endif
unsigned int numberOfRestrictionIter
Number of iteration at restriction step.
Definition: MG.h:66
std::vector< typename BlasType::MatrixType > levels
Definition: MG.h:20
MGResult MGVCycle(const MGMatrix< BlasType > &A, MGParameters< BlasType > params, MGVector< BlasType > *x, MGVector< BlasType > *b, MGVector< BlasType > *buffer)
Definition: MG-Impl.h:125
MGRelaxFunc< BlasType > relaxFunc
Relaxation function such as Jacobi or Gauss-Seidel.
Definition: MG.h:78
const BlasType::VectorType & Finest() const
Definition: MG-Impl.h:113
const BlasType::VectorType & operator[](size_t i) const
Definition: MG-Impl.h:101
const BlasType::MatrixType & Finest() const
Definition: MG-Impl.h:89
Multi-grid matrix wrapper.
Definition: MG.h:18
unsigned int numberOfCoarsestIter
Number of iteration at coarsest step.
Definition: MG.h:72
double maxTolerance
Max error tolerance.
Definition: MG.h:87
Multi-grid result type.
Definition: MG.h:91
unsigned int numberOfFinalIter
Number of iteration at final step.
Definition: MG.h:75
Definition: pybind11Utils.h:24
MGResult MGVCycle(const MGMatrix< BlasType > &A, MGParameters< BlasType > params, unsigned int currentLevel, MGVector< BlasType > *x, MGVector< BlasType > *b, MGVector< BlasType > *buffer)
Definition: MG-Impl.h:17
unsigned int numberOfCorrectionIter
Number of iteration at correction step.
Definition: MG.h:69
double lastResidualNorm
Lastly measured norm of residual.
Definition: MG.h:94
Multi-grid vector wrapper.
Definition: MG.h:29
Multi-grid input parameter set.
Definition: MG.h:60
MGCorrectFunc< BlasType > correctFunc
Correction function that maps coarser to finer grid.
Definition: MG.h:84
const BlasType::MatrixType & operator[](size_t i) const
Definition: MG-Impl.h:77
MGRestrictFunc< BlasType > restrictFunc
Restrict function that maps finer to coarser grid.
Definition: MG.h:81