[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletSoftBody / btConjugateGradient.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_GRADIENT_H
17 #define BT_CONJUGATE_GRADIENT_H
18 #include "btKrylovSolver.h"
19 template <class MatrixX>
20 class btConjugateGradient : public btKrylovSolver<MatrixX>
21 {
22         typedef btAlignedObjectArray<btVector3> TVStack;
23         typedef btKrylovSolver<MatrixX> Base;
24         TVStack r, p, z, temp;
25
26 public:
27         btConjugateGradient(const int max_it_in)
28                 : btKrylovSolver<MatrixX>(max_it_in, SIMD_EPSILON)
29         {
30         }
31
32         virtual ~btConjugateGradient() {}
33
34         // return the number of iterations taken
35         int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
36         {
37                 BT_PROFILE("CGSolve");
38                 btAssert(x.size() == b.size());
39                 reinitialize(b);
40                 temp = b;
41                 A.project(temp);
42                 p = temp;
43                 A.precondition(p, z);
44                 btScalar d0 = this->dot(z, temp);
45                 d0 = btMin(btScalar(1), d0);
46                 // r = b - A * x --with assigned dof zeroed out
47                 A.multiply(x, temp);
48                 r = this->sub(b, temp);
49                 A.project(r);
50                 // z = M^(-1) * r
51                 A.precondition(r, z);
52                 A.project(z);
53                 btScalar r_dot_z = this->dot(z, r);
54                 if (r_dot_z <= Base::m_tolerance * d0)
55                 {
56                         if (verbose)
57                         {
58                                 std::cout << "Iteration = 0" << std::endl;
59                                 std::cout << "Two norm of the residual = " << r_dot_z << std::endl;
60                         }
61                         return 0;
62                 }
63                 p = z;
64                 btScalar r_dot_z_new = r_dot_z;
65                 for (int k = 1; k <= Base::m_maxIterations; k++)
66                 {
67                         // temp = A*p
68                         A.multiply(p, temp);
69                         A.project(temp);
70                         if (this->dot(p, temp) < 0)
71                         {
72                                 if (verbose)
73                                         std::cout << "Encountered negative direction in CG!" << std::endl;
74                                 if (k == 1)
75                                 {
76                                         x = b;
77                                 }
78                                 return k;
79                         }
80                         // alpha = r^T * z / (p^T * A * p)
81                         btScalar alpha = r_dot_z_new / this->dot(p, temp);
82                         //  x += alpha * p;
83                         this->multAndAddTo(alpha, p, x);
84                         //  r -= alpha * temp;
85                         this->multAndAddTo(-alpha, temp, r);
86                         // z = M^(-1) * r
87                         A.precondition(r, z);
88                         r_dot_z = r_dot_z_new;
89                         r_dot_z_new = this->dot(r, z);
90                         if (r_dot_z_new < Base::m_tolerance * d0)
91                         {
92                                 if (verbose)
93                                 {
94                                         std::cout << "ConjugateGradient iterations " << k << " residual = " << r_dot_z_new << std::endl;
95                                 }
96                                 return k;
97                         }
98
99                         btScalar beta = r_dot_z_new / r_dot_z;
100                         p = this->multAndAdd(beta, p, z);
101                 }
102                 if (verbose)
103                 {
104                         std::cout << "ConjugateGradient max iterations reached " << Base::m_maxIterations << " error = " << r_dot_z_new << std::endl;
105                 }
106                 return Base::m_maxIterations;
107         }
108
109         void reinitialize(const TVStack& b)
110         {
111                 r.resize(b.size());
112                 p.resize(b.size());
113                 z.resize(b.size());
114                 temp.resize(b.size());
115         }
116 };
117 #endif /* btConjugateGradient_h */