Loading...
Searching...
No Matches
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
14namespace CubbyFlow
15{
16namespace Internal
17{
18template <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],
25 params.numberOfRestrictionIter, params.maxTolerance,
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]);
36 BlasType::Set(0.0, &(*x)[currentLevel + 1]);
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
80template <typename BlasType>
81const typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](
82 size_t i) const
83{
84 return levels[i];
85}
86
87template <typename BlasType>
88typename BlasType::MatrixType& MGMatrix<BlasType>::operator[](size_t i)
89{
90 return levels[i];
91}
92
93template <typename BlasType>
94const typename BlasType::MatrixType& MGMatrix<BlasType>::Finest() const
95{
96 return levels.Front();
97}
98
99template <typename BlasType>
100typename BlasType::MatrixType& MGMatrix<BlasType>::Finest()
101{
102 return levels.Front();
103}
104
105template <typename BlasType>
106const typename BlasType::VectorType& MGVector<BlasType>::operator[](
107 size_t i) const
108{
109 return levels[i];
110}
111
112template <typename BlasType>
113typename BlasType::VectorType& MGVector<BlasType>::operator[](size_t i)
114{
115 return levels[i];
116}
117
118template <typename BlasType>
119const typename BlasType::VectorType& MGVector<BlasType>::Finest() const
120{
121 return levels.Front();
122}
123
124template <typename BlasType>
125typename BlasType::VectorType& MGVector<BlasType>::Finest()
126{
127 return levels.Front();
128}
129
130template <typename BlasType>
134{
135 return Internal::MGVCycle<BlasType>(A, params, 0u, x, b, buffer);
136}
137} // namespace CubbyFlow
138
139#endif
Definition Matrix.hpp:30
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
Definition pybind11Utils.hpp:21
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
MGResult MGVCycle(const MGMatrix< BlasType > &A, MGParameters< BlasType > params, MGVector< BlasType > *x, MGVector< BlasType > *b, MGVector< BlasType > *buffer)
Performs Multi-grid with V-cycle.
Definition MG-Impl.hpp:131
const BlasType::MatrixType & Finest() const
Definition MG-Impl.hpp:94
const BlasType::MatrixType & operator[](size_t i) const
Definition MG-Impl.hpp:81
Multi-grid result type.
Definition MG.hpp:94
const BlasType::VectorType & operator[](size_t i) const
Definition MG-Impl.hpp:106
const BlasType::VectorType & Finest() const
Definition MG-Impl.hpp:119