Blender  V3.3
btKrylovSolver.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_KRYLOV_SOLVER_H
17 #define BT_KRYLOV_SOLVER_H
18 #include <iostream>
19 #include <cmath>
20 #include <limits>
22 #include <LinearMath/btVector3.h>
23 #include <LinearMath/btScalar.h>
24 #include "LinearMath/btQuickprof.h"
25 
26 template <class MatrixX>
28 {
30 
31 public:
34  btKrylovSolver(int maxIterations, btScalar tolerance)
35  : m_maxIterations(maxIterations), m_tolerance(tolerance)
36  {
37  }
38 
39  virtual ~btKrylovSolver() {}
40 
41  virtual int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false) = 0;
42 
43  virtual void reinitialize(const TVStack& b) = 0;
44 
45  virtual SIMD_FORCE_INLINE TVStack sub(const TVStack& a, const TVStack& b)
46  {
47  // c = a-b
48  btAssert(a.size() == b.size());
49  TVStack c;
50  c.resize(a.size());
51  for (int i = 0; i < a.size(); ++i)
52  {
53  c[i] = a[i] - b[i];
54  }
55  return c;
56  }
57 
59  {
60  return dot(a, a);
61  }
62 
64  {
65  btScalar ret = 0;
66  for (int i = 0; i < a.size(); ++i)
67  {
68  for (int d = 0; d < 3; ++d)
69  {
70  ret = btMax(ret, btFabs(a[i][d]));
71  }
72  }
73  return ret;
74  }
75 
76  virtual SIMD_FORCE_INLINE btScalar dot(const TVStack& a, const TVStack& b)
77  {
78  btScalar ans(0);
79  for (int i = 0; i < a.size(); ++i)
80  ans += a[i].dot(b[i]);
81  return ans;
82  }
83 
85  {
86  // result += s*a
87  btAssert(a.size() == result.size());
88  for (int i = 0; i < a.size(); ++i)
89  result[i] += s * a[i];
90  }
91 
93  {
94  // result = a*s + b
96  result.resize(a.size());
97  for (int i = 0; i < a.size(); ++i)
98  result[i] = s * a[i] + b[i];
99  return result;
100  }
101 
102  virtual SIMD_FORCE_INLINE void setTolerance(btScalar tolerance)
103  {
104  m_tolerance = tolerance;
105  }
106 };
107 #endif /* BT_KRYLOV_SOLVER_H */
#define A
SIMD_FORCE_INLINE const T & btMax(const T &a, const T &b)
Definition: btMinMax.h:27
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_FORCE_INLINE btScalar btFabs(btScalar x)
Definition: btScalar.h:497
#define SIMD_FORCE_INLINE
Definition: btScalar.h:280
#define btAssert(x)
Definition: btScalar.h:295
static int verbose
Definition: cineonlib.c:29
virtual ~btKrylovSolver()
virtual SIMD_FORCE_INLINE TVStack sub(const TVStack &a, const TVStack &b)
virtual int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)=0
virtual SIMD_FORCE_INLINE TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)
virtual SIMD_FORCE_INLINE void setTolerance(btScalar tolerance)
virtual void reinitialize(const TVStack &b)=0
btScalar m_tolerance
btKrylovSolver(int maxIterations, btScalar tolerance)
virtual SIMD_FORCE_INLINE btScalar norm(const TVStack &a)
virtual SIMD_FORCE_INLINE btScalar squaredNorm(const TVStack &a)
virtual SIMD_FORCE_INLINE btScalar dot(const TVStack &a, const TVStack &b)
virtual SIMD_FORCE_INLINE void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
return ret