general_purpose_preconditioners.h
Go to the documentation of this file.
1 // LIC// ====================================================================
2 // LIC// This file forms part of oomph-lib, the object-oriented,
3 // LIC// multi-physics finite-element library, available
4 // LIC// at http://www.oomph-lib.org.
5 // LIC//
6 // LIC// Copyright (C) 2006-2022 Matthias Heil and Andrew Hazel
7 // LIC//
8 // LIC// This library is free software; you can redistribute it and/or
9 // LIC// modify it under the terms of the GNU Lesser General Public
10 // LIC// License as published by the Free Software Foundation; either
11 // LIC// version 2.1 of the License, or (at your option) any later version.
12 // LIC//
13 // LIC// This library is distributed in the hope that it will be useful,
14 // LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // LIC// Lesser General Public License for more details.
17 // LIC//
18 // LIC// You should have received a copy of the GNU Lesser General Public
19 // LIC// License along with this library; if not, write to the Free Software
20 // LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 // LIC// 02110-1301 USA.
22 // LIC//
23 // LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24 // LIC//
25 // LIC//====================================================================
26 // Include guards
27 #ifndef OOMPH_GENERAL_PRECONDITION_HEADER
28 #define OOMPH_GENERAL_PRECONDITION_HEADER
29 
30 
31 // Config header generated by autoconfig
32 #ifdef HAVE_CONFIG_H
33 #include <oomph-lib-config.h>
34 #endif
35 
36 #include "preconditioner.h"
38 #include "matrices.h"
39 #include "problem.h"
40 #include <algorithm>
41 
42 
43 namespace oomph
44 {
45  //=====================================================================
47  //=====================================================================
49  {
50  public:
53 
56 
59  delete;
60 
63 
66 
69  void setup();
70 
71  private:
74  };
75 
76  //=============================================================================
78  //=============================================================================
79  template<typename MATRIX>
81  {
82  public:
85  {
86  // default the positive matrix boolean to false
87  Positive_matrix = false;
88 
89  // set the pointers to the lumped vector to 0
91  };
92 
95  {
96  this->clean_up_memory();
97  }
98 
101  delete;
102 
105 
108 
111  void setup();
112 
115  using Preconditioner::setup;
116 
119  bool positive_matrix() const
120  {
122 #ifdef PARANOID
123  if (Inv_lumped_diag_pt == 0)
124  {
125  throw OomphLibError("The preconditioner has not been setup.",
128  }
129 #endif
130  return Positive_matrix;
131  }
132 
133 
137  {
139 #ifdef PARANOID
140  if (Inv_lumped_diag_pt == 0)
141  {
142  throw OomphLibError("The inverse lumped vector has not been created. "
143  "Created in setup(...)",
146  }
147 #endif
148  return Inv_lumped_diag_pt;
149  }
150 
151 
153  unsigned& nrow()
154  {
155  return Nrow;
156  }
157 
160  {
161  delete[] Inv_lumped_diag_pt;
162  }
163 
164  private:
167 
168  // boolean to indicate whether the lumped matrix was positive
170 
171  // number of rows in preconditioner
172  unsigned Nrow;
173  };
174 
175 
176  //=============================================================================
183  //=============================================================================
185  {
186  public:
189 
191  CompressedMatrixCoefficient(const unsigned& index, const double& value)
192  {
193  // store the index and value
194  Index = index;
195  Value = value;
196  }
197 
200 
203  {
204  Index = a.index();
205  Value = a.value();
206  }
207 
210  {
211  Index = a.index();
212  Value = a.value();
213  }
214 
217  {
218  return Index < a.index();
219  }
220 
222  unsigned& index()
223  {
224  return Index;
225  }
226 
228  double& value()
229  {
230  return Value;
231  }
232 
235  unsigned index() const
236  {
237  return Index;
238  }
239 
241  double value() const
242  {
243  return Value;
244  }
245 
246  private:
248  unsigned Index;
249 
251  double Value;
252  };
253 
254 
255  //=============================================================================
257  //=============================================================================
258  template<typename MATRIX>
260  {
261  };
262 
263 
264  //=============================================================================
266  //=============================================================================
267  template<>
269  {
270  public:
273 
276 
277 
280 
282  void operator=(const ILUZeroPreconditioner&) = delete;
283 
286 
289  void setup();
290 
291  private:
294 
298 
301 
305  };
306 
307 
308  //=============================================================================
310  //=============================================================================
311  template<>
313  {
314  public:
317 
318 
321 
323  void operator=(const ILUZeroPreconditioner&) = delete;
324 
327 
330 
333  void setup();
334 
335 
336  private:
339 
343 
346 
350  };
351 
352  //=============================================================================
359  //=============================================================================
360  template<class SOLVER, class PRECONDITIONER>
362  {
363  public:
366  {
367  // create the solver
368  Solver_pt = new SOLVER;
369 
370  // create the preconditioner
371  Preconditioner_pt = new PRECONDITIONER;
372 
373 #ifdef PARANOID
374  // paranoid check that the solver is an iterative solver and
375  // the preconditioner is a preconditioner
376  if (dynamic_cast<IterativeLinearSolver*>(Solver_pt) == 0)
377  {
378  throw OomphLibError(
379  "The template argument SOLVER must be of type IterativeLinearSolver",
382  }
383  if (dynamic_cast<Preconditioner*>(Preconditioner_pt) == 0)
384  {
385  throw OomphLibError(
386  "The template argument PRECONDITIONER must be of type Preconditioner",
389  }
390 #endif
391 
392  // ensure the solver does not re-setup the preconditioner
393  Solver_pt->disable_setup_preconditioner_before_solve();
394 
395  // pass the preconditioner to the solver
396  Solver_pt->preconditioner_pt() = Preconditioner_pt;
397  }
398 
399  // destructor
401  {
402  delete Solver_pt;
403  delete Preconditioner_pt;
404  }
405 
406  // clean the memory
408  {
409  Preconditioner_pt->clean_up_memory();
410  Solver_pt->clean_up_memory();
411  }
412 
415  void setup()
416  {
417  // set the distribution
420  if (dist_pt != 0)
421  {
422  this->build_distribution(dist_pt->distribution_pt());
423  }
424  else
425  {
426  LinearAlgebraDistribution dist(comm_pt(), matrix_pt()->nrow(), false);
427  this->build_distribution(dist);
428  }
429 
430  // Setup the inner iteration preconditioner (For some reason we need to
431  // remind the compiler that there is also a function named setup in the
432  // base class.)
433  Preconditioner_pt->Preconditioner::setup(matrix_pt());
434 
435  // setup the solverready for resolve
436  unsigned max_iter = Solver_pt->max_iter();
437  Solver_pt->max_iter() = 1;
438  DoubleVector x(this->distribution_pt(), 0.0);
439  DoubleVector y(x);
440  Solver_pt->enable_resolve();
441  Solver_pt->solve(matrix_pt(), x, y);
442  Solver_pt->max_iter() = max_iter;
443  }
444 
448  {
449  Solver_pt->resolve(r, z);
450  }
451 
453  double& tolerance()
454  {
455  return Solver_pt->tolerance();
456  }
457 
459  unsigned& max_iter()
460  {
461  return Solver_pt->max_iter();
462  }
463 
465  SOLVER* solver_pt()
466  {
467  return Solver_pt;
468  }
469 
471  PRECONDITIONER* preconditioner_pt()
472  {
473  return Preconditioner_pt;
474  }
475 
476  private:
478  SOLVER* Solver_pt;
479 
481  PRECONDITIONER* Preconditioner_pt;
482  };
483 } // namespace oomph
484 #endif
A class for compressed column matrices that store doubles.
Definition: matrices.h:2791
Definition: matrices.h:888
Definition: general_purpose_preconditioners.h:185
~CompressedMatrixCoefficient()
Destructor (does nothing)
Definition: general_purpose_preconditioners.h:199
unsigned & index()
access function for the coefficient's (row or column) index
Definition: general_purpose_preconditioners.h:222
bool operator<(const CompressedMatrixCoefficient &a) const
Less Than Operator (for the STL sort function)
Definition: general_purpose_preconditioners.h:216
CompressedMatrixCoefficient()
Constructor (no arguments)
Definition: general_purpose_preconditioners.h:188
CompressedMatrixCoefficient(const unsigned &index, const double &value)
Constructor (takes the index and value as arguments)
Definition: general_purpose_preconditioners.h:191
double value() const
access function for the coefficient's value (const version)
Definition: general_purpose_preconditioners.h:241
double Value
the value of the compressed-matrix coefficient
Definition: general_purpose_preconditioners.h:251
CompressedMatrixCoefficient(const CompressedMatrixCoefficient &a)
Copy Constructor. Not Broken. Required for STL sort function.
Definition: general_purpose_preconditioners.h:202
void operator=(const CompressedMatrixCoefficient &a)
Assignment Operator. Not Broken. Required for STL sort function.
Definition: general_purpose_preconditioners.h:209
unsigned Index
the row or column index of the compressed-matrix coefficient
Definition: general_purpose_preconditioners.h:248
unsigned index() const
Definition: general_purpose_preconditioners.h:235
double & value()
access function for the coefficient value
Definition: general_purpose_preconditioners.h:228
Definition: linear_algebra_distribution.h:435
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
Definition: linear_algebra_distribution.h:457
unsigned nrow() const
access function to the number of global rows.
Definition: linear_algebra_distribution.h:463
void build_distribution(const LinearAlgebraDistribution *const dist_pt)
Definition: linear_algebra_distribution.h:507
Definition: double_vector.h:58
Vector< unsigned > L_column_start
Column start for lower triangular matrix.
Definition: general_purpose_preconditioners.h:300
Vector< CompressedMatrixCoefficient > U_row_entry
Definition: general_purpose_preconditioners.h:297
void operator=(const ILUZeroPreconditioner &)=delete
Broken assignment operator.
Vector< CompressedMatrixCoefficient > L_row_entry
Definition: general_purpose_preconditioners.h:304
~ILUZeroPreconditioner()
Destructor (empty)
Definition: general_purpose_preconditioners.h:275
Vector< unsigned > U_column_start
Column start for upper triangular matrix.
Definition: general_purpose_preconditioners.h:293
ILUZeroPreconditioner(const ILUZeroPreconditioner &)=delete
Broken copy constructor.
ILUZeroPreconditioner()
Constructor (empty)
Definition: general_purpose_preconditioners.h:272
~ILUZeroPreconditioner()
Destructor (empty)
Definition: general_purpose_preconditioners.h:326
ILUZeroPreconditioner(const ILUZeroPreconditioner &)=delete
Broken copy constructor.
ILUZeroPreconditioner()
Constructor (empty)
Definition: general_purpose_preconditioners.h:316
Vector< CompressedMatrixCoefficient > U_row_entry
Definition: general_purpose_preconditioners.h:342
Vector< unsigned > U_row_start
Row start for upper triangular matrix.
Definition: general_purpose_preconditioners.h:338
Vector< unsigned > L_row_start
Row start for lower triangular matrix.
Definition: general_purpose_preconditioners.h:345
void operator=(const ILUZeroPreconditioner &)=delete
Broken assignment operator.
Vector< CompressedMatrixCoefficient > L_row_entry
Definition: general_purpose_preconditioners.h:349
ILU(0) Preconditioner.
Definition: general_purpose_preconditioners.h:260
Definition: general_purpose_preconditioners.h:362
void setup()
Definition: general_purpose_preconditioners.h:415
void clean_up_memory()
Clean up memory (empty). Generic interface function.
Definition: general_purpose_preconditioners.h:407
InnerIterationPreconditioner()
Constructor.
Definition: general_purpose_preconditioners.h:365
double & tolerance()
Access to convergence tolerance of the inner iteration solver.
Definition: general_purpose_preconditioners.h:453
void preconditioner_solve(const DoubleVector &r, DoubleVector &z)
Definition: general_purpose_preconditioners.h:447
~InnerIterationPreconditioner()
Definition: general_purpose_preconditioners.h:400
unsigned & max_iter()
Access to max. number of iterations of the inner iteration solver.
Definition: general_purpose_preconditioners.h:459
SOLVER * solver_pt()
Definition: general_purpose_preconditioners.h:465
SOLVER * Solver_pt
pointer to the underlying solver
Definition: general_purpose_preconditioners.h:478
PRECONDITIONER * preconditioner_pt()
Definition: general_purpose_preconditioners.h:471
PRECONDITIONER * Preconditioner_pt
pointer to the underlying preconditioner
Definition: general_purpose_preconditioners.h:481
Definition: iterative_linear_solver.h:54
Definition: linear_algebra_distribution.h:64
Matrix-based diagonal preconditioner.
Definition: general_purpose_preconditioners.h:49
void operator=(const MatrixBasedDiagPreconditioner &)=delete
Broken assignment operator.
void preconditioner_solve(const DoubleVector &r, DoubleVector &z)
Apply preconditioner to z, i.e. z=D^-1.
Definition: general_purpose_preconditioners.cc:117
Vector< double > Inv_diag
Vector of inverse diagonal entries.
Definition: general_purpose_preconditioners.h:73
MatrixBasedDiagPreconditioner(const MatrixBasedDiagPreconditioner &)=delete
Broken copy constructor.
MatrixBasedDiagPreconditioner()
Constructor (empty)
Definition: general_purpose_preconditioners.h:52
void setup()
Definition: general_purpose_preconditioners.cc:43
~MatrixBasedDiagPreconditioner()
Destructor (empty)
Definition: general_purpose_preconditioners.h:55
Matrix-based lumped preconditioner.
Definition: general_purpose_preconditioners.h:81
void clean_up_memory()
clean up memory - just delete the inverse lumped vector
Definition: general_purpose_preconditioners.h:159
~MatrixBasedLumpedPreconditioner()
Destructor.
Definition: general_purpose_preconditioners.h:94
MatrixBasedLumpedPreconditioner(const MatrixBasedDiagPreconditioner &)=delete
Broken copy constructor.
bool positive_matrix() const
Definition: general_purpose_preconditioners.h:119
void preconditioner_solve(const DoubleVector &r, DoubleVector &z)
Apply preconditioner to z, i.e. z=D^-1.
Definition: general_purpose_preconditioners.cc:280
double * Inv_lumped_diag_pt
Vector of inverse diagonal entries.
Definition: general_purpose_preconditioners.h:166
unsigned Nrow
Definition: general_purpose_preconditioners.h:172
void operator=(const MatrixBasedLumpedPreconditioner &)=delete
Broken assignment operator.
double * inverse_lumped_vector_pt()
Definition: general_purpose_preconditioners.h:136
MatrixBasedLumpedPreconditioner()
Constructor.
Definition: general_purpose_preconditioners.h:84
unsigned & nrow()
Access function to number of rows for this preconditioner.
Definition: general_purpose_preconditioners.h:153
bool Positive_matrix
Definition: general_purpose_preconditioners.h:169
Definition: oomph_definitions.h:222
Definition: preconditioner.h:54
virtual DoubleMatrixBase * matrix_pt() const
Get function for matrix pointer.
Definition: preconditioner.h:150
virtual void preconditioner_solve(const DoubleVector &r, DoubleVector &z)=0
virtual const OomphCommunicator * comm_pt() const
Get function for comm pointer.
Definition: preconditioner.h:171
virtual void setup()=0
Scalar * y
Definition: level1_cplx_impl.h:128
const Scalar * a
Definition: level2_cplx_impl.h:32
r
Definition: UniformPSDSelfTest.py:20
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
list x
Definition: plotDoE.py:28
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86