[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletSoftBody / btConjugateResidual.h
1 /*
2  Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3  
4  Bullet Continuous Collision Detection and Physics Library
5  Copyright (c) 2019 Google Inc. http://bulletphysics.org
6  This software is provided 'as-is', without any express or implied warranty.
7  In no event will the authors be held liable for any damages arising from the use of this software.
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it freely,
10  subject to the following restrictions:
11  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13  3. This notice may not be removed or altered from any source distribution.
14  */
15
16 #ifndef BT_CONJUGATE_RESIDUAL_H
17 #define BT_CONJUGATE_RESIDUAL_H
18 #include "btKrylovSolver.h"
19
20 template <class MatrixX>
21 class btConjugateResidual : public btKrylovSolver<MatrixX>
22 {
23         typedef btAlignedObjectArray<btVector3> TVStack;
24         typedef btKrylovSolver<MatrixX> Base;
25         TVStack r, p, z, temp_p, temp_r, best_x;
26         // temp_r = A*r
27         // temp_p = A*p
28         // z = M^(-1) * temp_p = M^(-1) * A * p
29         btScalar best_r;
30
31 public:
32         btConjugateResidual(const int max_it_in)
33                 : Base(max_it_in, 1e-8)
34         {
35         }
36
37         virtual ~btConjugateResidual() {}
38
39         // return the number of iterations taken
40         int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
41         {
42                 BT_PROFILE("CRSolve");
43                 btAssert(x.size() == b.size());
44                 reinitialize(b);
45                 // r = b - A * x --with assigned dof zeroed out
46                 A.multiply(x, temp_r);  // borrow temp_r here to store A*x
47                 r = this->sub(b, temp_r);
48                 // z = M^(-1) * r
49                 A.precondition(r, z);  // borrow z to store preconditioned r
50                 r = z;
51                 btScalar residual_norm = this->norm(r);
52                 if (residual_norm <= Base::m_tolerance)
53                 {
54                         return 0;
55                 }
56                 p = r;
57                 btScalar r_dot_Ar, r_dot_Ar_new;
58                 // temp_p = A*p
59                 A.multiply(p, temp_p);
60                 // temp_r = A*r
61                 temp_r = temp_p;
62                 r_dot_Ar = this->dot(r, temp_r);
63                 for (int k = 1; k <= Base::m_maxIterations; k++)
64                 {
65                         // z = M^(-1) * Ap
66                         A.precondition(temp_p, z);
67                         // alpha = r^T * A * r / (Ap)^T * M^-1 * Ap)
68                         btScalar alpha = r_dot_Ar / this->dot(temp_p, z);
69                         //  x += alpha * p;
70                         this->multAndAddTo(alpha, p, x);
71                         //  r -= alpha * z;
72                         this->multAndAddTo(-alpha, z, r);
73                         btScalar norm_r = this->norm(r);
74                         if (norm_r < best_r)
75                         {
76                                 best_x = x;
77                                 best_r = norm_r;
78                                 if (norm_r < Base::m_tolerance)
79                                 {
80                                         return k;
81                                 }
82                         }
83                         // temp_r = A * r;
84                         A.multiply(r, temp_r);
85                         r_dot_Ar_new = this->dot(r, temp_r);
86                         btScalar beta = r_dot_Ar_new / r_dot_Ar;
87                         r_dot_Ar = r_dot_Ar_new;
88                         // p = beta*p + r;
89                         p = this->multAndAdd(beta, p, r);
90                         // temp_p = beta*temp_p + temp_r;
91                         temp_p = this->multAndAdd(beta, temp_p, temp_r);
92                 }
93                 if (verbose)
94                 {
95                         std::cout << "ConjugateResidual max iterations reached, residual = " << best_r << std::endl;
96                 }
97                 x = best_x;
98                 return Base::m_maxIterations;
99         }
100
101         void reinitialize(const TVStack& b)
102         {
103                 r.resize(b.size());
104                 p.resize(b.size());
105                 z.resize(b.size());
106                 temp_p.resize(b.size());
107                 temp_r.resize(b.size());
108                 best_x.resize(b.size());
109                 best_r = SIMD_INFINITY;
110         }
111 };
112 #endif /* btConjugateResidual_h */