MG-Impl.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_MULTI_GRID_IMPL_HPP
12 #define CUBBYFLOW_MULTI_GRID_IMPL_HPP
13 
14 namespace CubbyFlow
15 {
16 namespace Internal
17 {
18 template <typename BlasType>
20  unsigned int currentLevel, MGVector<BlasType>* x,
22 {
23  // 1) Relax a few times on Ax = b, with arbitrary x
24  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
26  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
27 
28  // 2) if currentLevel is the coarsest grid, goto 5)
29  if (currentLevel < A.levels.size() - 1)
30  {
31  auto r = buffer;
32  BlasType::Residual(A[currentLevel], (*x)[currentLevel],
33  (*b)[currentLevel], &(*r)[currentLevel]);
34  params.restrictFunc((*r)[currentLevel], &(*b)[currentLevel + 1]);
35 
36  BlasType::Set(0.0, &(*x)[currentLevel + 1]);
37 
38  params.maxTolerance *= 0.5;
39  // Solve Ae = r
40  MGVCycle(A, params, currentLevel + 1, x, b, buffer);
41  params.maxTolerance *= 2.0;
42 
43  // 3) correct
44  params.correctFunc((*x)[currentLevel + 1], &(*x)[currentLevel]);
45 
46  // 4) relax nIter times on Ax = b, with initial guess x
47  if (currentLevel > 0)
48  {
49  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
50  params.numberOfCorrectionIter, params.maxTolerance,
51  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
52  }
53  else
54  {
55  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
56  params.numberOfFinalIter, params.maxTolerance,
57  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
58  }
59  }
60  else
61  {
62  // 5) solve directly with initial guess x
63  params.relaxFunc(A[currentLevel], (*b)[currentLevel],
64  params.numberOfCoarsestIter, params.maxTolerance,
65  &((*x)[currentLevel]), &((*buffer)[currentLevel]));
66 
67  BlasType::Residual(A[currentLevel], (*x)[currentLevel],
68  (*b)[currentLevel], &(*buffer)[currentLevel]);
69  }
70 
71  BlasType::Residual(A[currentLevel], (*x)[currentLevel], (*b)[currentLevel],
72  &(*buffer)[currentLevel]);
73 
74  MGResult result;
75  result.lastResidualNorm = BlasType::L2Norm((*buffer)[currentLevel]);
76  return result;
77 }
78 } // namespace Internal
79 
80 template <typename BlasType>
81 const typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](
82  size_t i) const
83 {
84  return levels[i];
85 }
86 
87 template <typename BlasType>
88 typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](size_t i)
89 {
90  return levels[i];
91 }
92 
93 template <typename BlasType>
94 const typename BlasType::MatrixType& MGMatrix<BlasType>::Finest() const
95 {
96  return levels.Front();
97 }
98 
99 template <typename BlasType>
100 typename BlasType::MatrixType& MGMatrix<BlasType>::Finest()
101 {
102  return levels.Front();
103 }
104 
105 template <typename BlasType>
106 const typename BlasType::VectorType& MGVector<BlasType>::operator[](
107  size_t i) const
108 {
109  return levels[i];
110 }
111 
112 template <typename BlasType>
113 typename BlasType::VectorType& MGVector<BlasType>::operator[](size_t i)
114 {
115  return levels[i];
116 }
117 
118 template <typename BlasType>
119 const typename BlasType::VectorType& MGVector<BlasType>::Finest() const
120 {
121  return levels.Front();
122 }
123 
124 template <typename BlasType>
125 typename BlasType::VectorType& MGVector<BlasType>::Finest()
126 {
127  return levels.Front();
128 }
129 
130 template <typename BlasType>
133  MGVector<BlasType>* buffer)
134 {
135  return Internal::MGVCycle<BlasType>(A, params, 0u, x, b, buffer);
136 }
137 } // namespace CubbyFlow
138 
139 #endif
unsigned int numberOfRestrictionIter
Number of iteration at restriction step.
Definition: MG.hpp:68
std::vector< typename BlasType::MatrixType > levels
Definition: MG.hpp:22
MGRelaxFunc< BlasType > relaxFunc
Relaxation function such as Jacobi or Gauss-Seidel.
Definition: MG.hpp:80
const BlasType::VectorType & Finest() const
Definition: MG-Impl.hpp:119
const BlasType::VectorType & operator[](size_t i) const
Definition: MG-Impl.hpp:106
const BlasType::MatrixType & Finest() const
Definition: MG-Impl.hpp:94
Multi-grid matrix wrapper.
Definition: MG.hpp:20
unsigned int numberOfCoarsestIter
Number of iteration at coarsest step.
Definition: MG.hpp:74
double maxTolerance
Max error tolerance.
Definition: MG.hpp:89
Multi-grid result type.
Definition: MG.hpp:93
unsigned int numberOfFinalIter
Number of iteration at final step.
Definition: MG.hpp:77
Definition: pybind11Utils.hpp:20
unsigned int numberOfCorrectionIter
Number of iteration at correction step.
Definition: MG.hpp:71
double lastResidualNorm
Lastly measured norm of residual.
Definition: MG.hpp:96
Multi-grid vector wrapper.
Definition: MG.hpp:31
MGResult MGVCycle(const MGMatrix< BlasType > &A, MGParameters< BlasType > &params, unsigned int currentLevel, MGVector< BlasType > *x, MGVector< BlasType > *b, MGVector< BlasType > *buffer)
Definition: MG-Impl.hpp:19
Multi-grid input parameter set.
Definition: MG.hpp:62
MGCorrectFunc< BlasType > correctFunc
Correction function that maps coarser to finer grid.
Definition: MG.hpp:86
const BlasType::MatrixType & operator[](size_t i) const
Definition: MG-Impl.hpp:81
MGRestrictFunc< BlasType > restrictFunc
Restrict function that maps finer to coarser grid.
Definition: MG.hpp:83