Bullet Collision Detection & Physics Library: btConjugateGradient.h Source File

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 "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 */
btKrylovSolver.h
btMin
const T & btMin(const T &a, const T &b)
Definition: btMinMax.h:21
BT_PROFILE
#define BT_PROFILE(name)
Definition: btQuickprof.h:198
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:543
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btAlignedObjectArray< btVector3 >
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:203
btConjugateGradient
Definition: btConjugateGradient.h:21
btConjugateGradient::z
TVStack z
Definition: btConjugateGradient.h:24
btConjugateGradient::r
TVStack r
Definition: btConjugateGradient.h:24
btConjugateGradient::btConjugateGradient
btConjugateGradient(const int max_it_in)
Definition: btConjugateGradient.h:27
btConjugateGradient::~btConjugateGradient
virtual ~btConjugateGradient()
Definition: btConjugateGradient.h:32
btConjugateGradient::solve
int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)
Definition: btConjugateGradient.h:35
btConjugateGradient::temp
TVStack temp
Definition: btConjugateGradient.h:24
btConjugateGradient::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btConjugateGradient.h:22
btConjugateGradient::reinitialize
void reinitialize(const TVStack &b)
Definition: btConjugateGradient.h:109
btConjugateGradient::p
TVStack p
Definition: btConjugateGradient.h:24
btConjugateGradient::Base
btKrylovSolver< MatrixX > Base
Definition: btConjugateGradient.h:23
btKrylovSolver
Definition: btKrylovSolver.h:28
btKrylovSolver::dot
virtual btScalar dot(const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:76
btKrylovSolver::sub
virtual TVStack sub(const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:45
btKrylovSolver::m_tolerance
btScalar m_tolerance
Definition: btKrylovSolver.h:33
btKrylovSolver::multAndAddTo
virtual void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
Definition: btKrylovSolver.h:84
btKrylovSolver::m_maxIterations
int m_maxIterations
Definition: btKrylovSolver.h:32
btKrylovSolver::multAndAdd
virtual TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:92