Bullet Collision Detection & Physics Library
btConjugateGradient.h
Go to the documentation of this file.
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 <iostream>
19 #include <cmath>
20 #include <limits>
22 #include <LinearMath/btVector3.h>
23 #include "LinearMath/btQuickprof.h"
24 template <class MatrixX>
26 {
31 public:
32  btConjugateGradient(const int max_it_in)
33  : max_iterations(max_it_in)
34  {
35  tolerance_squared = 1e-5;
36  }
37 
39 
40  // return the number of iterations taken
41  int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
42  {
43  BT_PROFILE("CGSolve");
44  btAssert(x.size() == b.size());
45  reinitialize(b);
46  // r = b - A * x --with assigned dof zeroed out
47  A.multiply(x, temp);
48  r = 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 = dot(z,r);
54  if (r_dot_z <= tolerance_squared) {
55  if (verbose)
56  {
57  std::cout << "Iteration = 0" << std::endl;
58  std::cout << "Two norm of the residual = " << r_dot_z << std::endl;
59  }
60  return 0;
61  }
62  p = z;
63  btScalar r_dot_z_new = r_dot_z;
64  for (int k = 1; k <= max_iterations; k++) {
65  // temp = A*p
66  A.multiply(p, temp);
67  A.project(temp);
68  if (dot(p,temp) < SIMD_EPSILON)
69  {
70  if (verbose)
71  std::cout << "Encountered negative direction in CG!" << std::endl;
72  if (k == 1)
73  {
74  x = b;
75  }
76  return k;
77  }
78  // alpha = r^T * z / (p^T * A * p)
79  btScalar alpha = r_dot_z_new / dot(p, temp);
80  // x += alpha * p;
81  multAndAddTo(alpha, p, x);
82  // r -= alpha * temp;
83  multAndAddTo(-alpha, temp, r);
84  // z = M^(-1) * r
85  A.precondition(r, z);
86  r_dot_z = r_dot_z_new;
87  r_dot_z_new = dot(r,z);
88  if (r_dot_z_new < tolerance_squared) {
89  if (verbose)
90  {
91  std::cout << "ConjugateGradient iterations " << k << std::endl;
92  }
93  return k;
94  }
95 
96  btScalar beta = r_dot_z_new/r_dot_z;
97  p = multAndAdd(beta, p, z);
98  }
99  if (verbose)
100  {
101  std::cout << "ConjugateGradient max iterations reached " << max_iterations << std::endl;
102  }
103  return max_iterations;
104  }
105 
106  void reinitialize(const TVStack& b)
107  {
108  r.resize(b.size());
109  p.resize(b.size());
110  z.resize(b.size());
111  temp.resize(b.size());
112  }
113 
114  TVStack sub(const TVStack& a, const TVStack& b)
115  {
116  // c = a-b
117  btAssert(a.size() == b.size());
118  TVStack c;
119  c.resize(a.size());
120  for (int i = 0; i < a.size(); ++i)
121  {
122  c[i] = a[i] - b[i];
123  }
124  return c;
125  }
126 
128  {
129  return dot(a,a);
130  }
131 
132  btScalar dot(const TVStack& a, const TVStack& b)
133  {
134  btScalar ans(0);
135  for (int i = 0; i < a.size(); ++i)
136  ans += a[i].dot(b[i]);
137  return ans;
138  }
139 
140  void multAndAddTo(btScalar s, const TVStack& a, TVStack& result)
141  {
142 // result += s*a
143  btAssert(a.size() == result.size());
144  for (int i = 0; i < a.size(); ++i)
145  result[i] += s * a[i];
146  }
147 
148  TVStack multAndAdd(btScalar s, const TVStack& a, const TVStack& b)
149  {
150  // result = a*s + b
151  TVStack result;
152  result.resize(a.size());
153  for (int i = 0; i < a.size(); ++i)
154  result[i] = s * a[i] + b[i];
155  return result;
156  }
157 };
158 #endif /* btConjugateGradient_h */
#define BT_PROFILE(name)
Definition: btQuickprof.h:198
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_EPSILON
Definition: btScalar.h:543
#define btAssert(x)
Definition: btScalar.h:153
int size() const
return the number of elements in the array
void resize(int newsize, const T &fillData=T())
btConjugateGradient(const int max_it_in)
btScalar dot(const TVStack &a, const TVStack &b)
btScalar squaredNorm(const TVStack &a)
int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)
void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
btAlignedObjectArray< btVector3 > TVStack
void reinitialize(const TVStack &b)
TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)
TVStack sub(const TVStack &a, const TVStack &b)