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

Bullet Collision Detection & Physics Library

btConjugateResidual.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_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 */
btKrylovSolver.h
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_INFINITY
#define SIMD_INFINITY
Definition: btScalar.h:544
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
btConjugateResidual
Definition: btConjugateResidual.h:22
btConjugateResidual::best_r
btScalar best_r
Definition: btConjugateResidual.h:29
btConjugateResidual::btConjugateResidual
btConjugateResidual(const int max_it_in)
Definition: btConjugateResidual.h:32
btConjugateResidual::solve
int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)
Definition: btConjugateResidual.h:40
btConjugateResidual::reinitialize
void reinitialize(const TVStack &b)
Definition: btConjugateResidual.h:101
btConjugateResidual::Base
btKrylovSolver< MatrixX > Base
Definition: btConjugateResidual.h:24
btConjugateResidual::z
TVStack z
Definition: btConjugateResidual.h:25
btConjugateResidual::r
TVStack r
Definition: btConjugateResidual.h:25
btConjugateResidual::~btConjugateResidual
virtual ~btConjugateResidual()
Definition: btConjugateResidual.h:37
btConjugateResidual::p
TVStack p
Definition: btConjugateResidual.h:25
btConjugateResidual::best_x
TVStack best_x
Definition: btConjugateResidual.h:25
btConjugateResidual::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btConjugateResidual.h:23
btConjugateResidual::temp_p
TVStack temp_p
Definition: btConjugateResidual.h:25
btConjugateResidual::temp_r
TVStack temp_r
Definition: btConjugateResidual.h:25
btKrylovSolver
Definition: btKrylovSolver.h:28
btKrylovSolver::norm
virtual btScalar norm(const TVStack &a)
Definition: btKrylovSolver.h:63
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