All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PGS.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SOLVERS_PGS_H_
36 #define _BLAZE_MATH_SOLVERS_PGS_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
50 #include <blaze/util/Assert.h>
51 #include <blaze/util/ColorMacros.h>
53 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS DEFINITION
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
72 class PGS : public Solver
73 {
74  public:
75  //**Constructors********************************************************************************
78  explicit PGS();
80  //**********************************************************************************************
81 
82  //**Utility functions***************************************************************************
85  template< typename CP > bool solve( CP& cp );
87  //**********************************************************************************************
88 
89  private:
90  //**Utility functions***************************************************************************
93  template< typename CP > inline real sweep( CP& cp ) const;
95  //**********************************************************************************************
96 
97  //**Member variables****************************************************************************
101 
104  //**********************************************************************************************
105 };
106 //*************************************************************************************************
107 
108 
109 
110 
111 //=================================================================================================
112 //
113 // UTILITY FUNCTIONS
114 //
115 //=================================================================================================
116 
117 //*************************************************************************************************
125 template< typename CP > // Type of the complementarity problem
126 bool PGS::solve( CP& cp )
127 {
128  const size_t n( cp.size() );
129  const CMatMxN& A( cp.A_ );
130  bool converged( false );
131 
132  // Allocating the helper data
133  diagonal_.resize( n, false );
134 
135  // Locating the diagonal entries in system matrix and precomputing the inverse values
136  for( size_t i=0; i<n; ++i ) {
137  const real tmp( A(i,i) );
138  BLAZE_INTERNAL_ASSERT( tmp != real(0), "Invalid diagonal element in the LCP matrix" );
139  diagonal_[i] = real(1) / tmp;
140  }
141 
142  // Projecting the initial solution to a feasible region
143  for( size_t i=0; i<n; ++i ) {
144  cp.project( i );
145  }
146 
147  // Computing the initial residual
148  lastPrecision_ = cp.residual();
149  if( lastPrecision_ < threshold_ )
150  converged = true;
151 
152  // The main iteration loop
153  size_t it( 0 );
154 
155  for( ; !converged && it<maxIterations_; ++it ) {
156  lastPrecision_ = sweep( cp );
157  if( lastPrecision_ < threshold_ )
158  converged = true;
159  }
160 
161  BLAZE_LOG_DEBUG_SECTION( log ) {
162  if( converged )
163  log << " Solved the complementarity problem in " << it << " PGS iterations.";
164  else
165  log << BLAZE_YELLOW << " WARNING: Did not solve the complementarity problem within accuracy. (" << lastPrecision_ << ")" << BLAZE_OLDCOLOR;
166  }
167 
168  lastIterations_ = it;
169 
170  return converged;
171 }
172 //*************************************************************************************************
173 
174 
175 //*************************************************************************************************
183 template< typename CP > // Type of the complementarity problem
184 inline real PGS::sweep( CP& cp ) const
185 {
186  real maxResidual( 0 ), xold( 0 );
187  const size_t n( cp.size() );
188 
189  const CMatMxN& A( cp.A_ );
190  const VecN& b( cp.b_ );
191  VecN& x( cp.x_ );
192 
193  for( size_t i=0; i<n; ++i )
194  {
195  const real residual( - b[i] - ( A * x )[i] );
196 
197  // Updating and projecting the unknown
198  xold = x[i];
199  x[i] += diagonal_[i] * residual;
200  cp.project( i );
201  maxResidual = max( maxResidual, std::fabs( xold - x[i] ) );
202  }
203 
204  return maxResidual;
205 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
218 template<>
219 inline real PGS::sweep( ContactLCP& cp ) const
220 {
221  const size_t N( cp.size() / 3 );
222  real rmax( 0 ), residual, flimit, aux;
223  size_t j;
224 
225  const CMatMxN& A( cp.A_ );
226  const VecN& b( cp.b_ );
227  VecN& x( cp.x_ );
228 
229  for( size_t i=0; i<N; ++i )
230  {
231  j = i * 3;
232  residual = -b[j] - ( A * x )[j];
233  aux = max( 0, x[j] + diagonal_[j] * residual );
234  rmax = max( rmax, std::fabs( x[j] - aux ) );
235  x[j] = aux;
236 
237  flimit = cp.cof_[i] * x[j];
238 
239  ++j;
240  residual = -b[j] - ( A * x )[j];
241  aux = max( -flimit, min( flimit, x[j] + diagonal_[j] * residual ) );
242  rmax = max( rmax, std::fabs( x[j] - aux ) );
243  x[j] = aux;
244 
245  ++j;
246  residual = -b[j] - ( A * x )[j];
247  aux = max( -flimit, min( flimit, x[j] + diagonal_[j] * residual ) );
248  rmax = max( rmax, std::fabs( x[j] - aux ) );
249  x[j] = aux;
250  }
251 
252  return rmax;
253 }
254 //*************************************************************************************************
255 
256 
257 
258 
259 //=================================================================================================
260 //
261 // EXPLICIT TEMPLATE INSTANTIATIONS
262 //
263 //=================================================================================================
264 
265 //*************************************************************************************************
267 #if !defined(_MSC_VER)
268 extern template bool PGS::solve<LCP>( LCP& );
269 extern template bool PGS::solve<BoxLCP>( BoxLCP& );
270 extern template bool PGS::solve<ContactLCP>( ContactLCP& );
271 #endif
272 
273 //*************************************************************************************************
274 
275 } // namespace blaze
276 
277 #endif
A data structure for box linear complementarity problems.
VecN cof_
The corresponding coefficients of friction.
Definition: ContactLCP.h:86
A data structure for linear complementarity problems for contact mechanics.
real lastPrecision_
The precision of the solution after the solution process.
Definition: Solver.h:99
Header file for color macros.
Header file for the log debug section.
const StaticMatrix< Type, 3UL, 3UL, false > fabs(const RotationMatrix< Type > &m)
Returns a matrix containing the absolute values of each single element of m.
Definition: RotationMatrix.h:1102
A box linear complementarity problem (BLCP) data structure.The BoxLCP class represent a box linear co...
Definition: BoxLCP.h:66
CMatMxN A_
The system matrix .
Definition: ContactLCP.h:83
bool solve(CP &cp)
Solves the provided complementarity problem.
Definition: PGS.h:126
#define BLAZE_YELLOW
Switches the text color to yellow in case the BLAZE_COLOR_OUTPUT macro is set.
Definition: ColorMacros.h:147
void resize(size_t n, bool preserve=true)
Changing the size of the vector.
Definition: DynamicVector.h:1158
size_t maxIterations_
The maximum number of iterations.
Definition: Solver.h:95
VecN b_
The right-hand side vector .
Definition: ContactLCP.h:84
#define BLAZE_LOG_DEBUG_SECTION(NAME)
Logging section for debug information.This macro starts a log section for debug information. These messages are written to the log file(s) in case the blaze::loglevel has been set to debug or higher. The following example demonstrates how this log section is used:
Definition: DebugSection.h:96
PGS()
The default constructor for the PGS class.
Definition: PGS.cpp:54
VecN diagonal_
Vector for the diagonal entries of the LCP matrix.
Definition: PGS.h:100
Header file for the complete DynamicVector implementation.
Header file for the base class of all solvers.
#define BLAZE_OLDCOLOR
Switches the text color back to the default color.
Definition: ColorMacros.h:162
Header file for run time assertion macros.
A data structure for linear complementarity problems.
double real
Floating point data type of the Blaze library.This type definition offers the possibility to switch t...
Definition: Precision.h:47
A projected Gauss-Seidel Solver for (box) LCPs.TODO: description of the PGS solver TODO: capabilities...
Definition: PGS.h:72
Header file for the complete CompressedMatrix implementation.
real threshold_
Precision threshold for the solution.
Definition: Solver.h:100
VecN x_
The vector of unknowns .
Definition: ContactLCP.h:85
real sweep(CP &cp) const
TODO.
Definition: PGS.h:184
A data structure for linear complementarity problems (LCPs) for contact mechanics.TODO.
Definition: ContactLCP.h:66
Header file for basic type definitions.
size_t lastIterations_
The number of iterations spent in the last solution process.
Definition: Solver.h:98
A linear complementarity problem (LCP) data structure.The LCP class represent a linear complementarit...
Definition: LCP.h:67
Base class for all solver classes.TODO: description of the Solver class TODO: description of its func...
Definition: Solver.h:63
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
size_t size() const
Returns the size of the contact linear complementarity problem.
Definition: ContactLCP.h:106