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 {
26  // temp_r = A*r
27  // temp_p = A*p
28  // z = M^(-1) * temp_p = M^(-1) * A * p
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());
110  }
111 };
112 #endif /* btConjugateResidual_h */
btConjugateResidual::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btConjugateResidual.h:23
btKrylovSolver.h
btConjugateResidual::~btConjugateResidual
virtual ~btConjugateResidual()
Definition: btConjugateResidual.h:37
btConjugateResidual::r
TVStack r
Definition: btConjugateResidual.h:25
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btKrylovSolver::sub
virtual TVStack sub(const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:45
btKrylovSolver::multAndAdd
virtual TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:92
btKrylovSolver::dot
virtual btScalar dot(const TVStack &a, const TVStack &b)
Definition: btKrylovSolver.h:76
btKrylovSolver::m_tolerance
btScalar m_tolerance
Definition: btKrylovSolver.h:33
btConjugateResidual::Base
btKrylovSolver< MatrixX > Base
Definition: btConjugateResidual.h:24
btKrylovSolver::multAndAddTo
virtual void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
Definition: btKrylovSolver.h:84
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btConjugateResidual::reinitialize
void reinitialize(const TVStack &b)
Definition: btConjugateResidual.h:101
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:203
btKrylovSolver
Definition: btKrylovSolver.h:28
btConjugateResidual::best_x
TVStack best_x
Definition: btConjugateResidual.h:25
btConjugateResidual::solve
int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)
Definition: btConjugateResidual.h:40
SIMD_INFINITY
#define SIMD_INFINITY
Definition: btScalar.h:544
btConjugateResidual
Definition: btConjugateResidual.h:22
btConjugateResidual::btConjugateResidual
btConjugateResidual(const int max_it_in)
Definition: btConjugateResidual.h:32
btAlignedObjectArray< btVector3 >
btKrylovSolver::norm
virtual btScalar norm(const TVStack &a)
Definition: btKrylovSolver.h:63
btConjugateResidual::best_r
btScalar best_r
Definition: btConjugateResidual.h:29
btKrylovSolver::m_maxIterations
int m_maxIterations
Definition: btKrylovSolver.h:32
btConjugateResidual::z
TVStack z
Definition: btConjugateResidual.h:25
btConjugateResidual::p
TVStack p
Definition: btConjugateResidual.h:25
BT_PROFILE
#define BT_PROFILE(name)
Definition: btQuickprof.h:198
btConjugateResidual::temp_p
TVStack temp_p
Definition: btConjugateResidual.h:25
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btConjugateResidual::temp_r
TVStack temp_r
Definition: btConjugateResidual.h:25