preconditioner.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_PRECONDITION_HEADER
28 #define OOMPH_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 "matrices.h"
37 
38 
39 namespace oomph
40 {
41  // Forward declaration of block preconditioners (for turn into subs.)
42  template<typename MATRIX>
43  class BlockPreconditioner;
44 
45  // Forward declaration of problem class (for obsolete setup function)
46  class Problem;
47 
48  //=============================================================================
52  //=============================================================================
54  {
55  public:
59  Stream_pt(0),
60  Matrix_pt(0),
61  Comm_pt(0),
62  Setup_time(0){};
63 
65  Preconditioner(const Preconditioner&) = delete;
66 
68  void operator=(const Preconditioner&) = delete;
69 
71  virtual ~Preconditioner() {}
72 
76  virtual void preconditioner_solve(const DoubleVector& r,
77  DoubleVector& z) = 0;
78 
83  DoubleVector& z)
84  {
85  // Throw an error
86  throw OomphLibError("This function hasn't been implemented yet!",
89  }
90 
95  {
96  // Store matrix pointer
98 
99  // Extract and store communicator pointer
100  DistributableLinearAlgebraObject* dist_obj_pt =
102  if (dist_obj_pt != 0)
103  {
104  set_comm_pt(dist_obj_pt->distribution_pt()->communicator_pt());
105  }
106  else
107  {
108  set_comm_pt(0);
109  }
110 
111  double setup_time_start = TimingHelpers::timer();
112  setup();
113  double setup_time_finish = TimingHelpers::timer();
114  Setup_time = setup_time_finish - setup_time_start;
115  }
116 
120  void setup(const Problem* problem_pt, DoubleMatrixBase* matrix_pt)
121  {
123  setup(matrix_pt);
124  }
125 
128  {
129  // Set the appropriate (silencing) boolean to true
131  } // End of enable_silent_preconditioner_setup
132 
133 
136  {
137  // Set the appropriate (silencing) boolean to false
139  } // End of disable_silent_preconditioner_setup
140 
141 
144  virtual void setup() = 0;
145 
147  virtual void clean_up_memory() {}
148 
150  virtual DoubleMatrixBase* matrix_pt() const
151  {
152 #ifdef PARANOID
153  if (Matrix_pt == 0)
154  {
155  std::ostringstream error_msg;
156  error_msg << "Matrix pointer is null.";
157  throw OomphLibError(
159  }
160 #endif
161  return Matrix_pt;
162  }
163 
166  {
168  }
169 
171  virtual const OomphCommunicator* comm_pt() const
172  {
173  // If we are using MPI then the comm pointer should not be null.
174 #ifdef OOMPH_HAS_MPI
175 #ifdef PARANOID
176  if (Comm_pt == 0)
177  {
178  std::ostringstream error_msg;
179  error_msg << "Tried to access a null communicator pointer. This might "
180  "mean you are\n"
181  << "trying to use it in a non-parallel case. Or it might "
182  "mean you haven't\n"
183  << "set it properly.";
184  throw OomphLibError(
186  }
187 #endif
188 #endif
189  return Comm_pt;
190  }
191 
193  virtual void set_comm_pt(const OomphCommunicator* const comm_pt)
194  {
195  Comm_pt = comm_pt;
196  }
197 
199  double setup_time() const
200  {
201  return Setup_time;
202  }
203 
211  BlockPreconditioner<CRDoubleMatrix>* master_block_prec_pt,
212  const Vector<unsigned>& doftype_in_master_preconditioner_coarse)
213  {
214  }
215 
221  BlockPreconditioner<CRDoubleMatrix>* master_block_prec_pt,
222  const Vector<unsigned>& doftype_in_master_preconditioner_coarse,
223  const Vector<Vector<unsigned>>& doftype_coarsen_map_coarse)
224  {
225  }
226 
227  protected:
230 
232  std::ostream* Stream_pt;
233 
234  private:
237 
241 
243  double Setup_time;
244 
245  }; // Preconditioner
246 
247 
248  //=============================================================================
250  //=============================================================================
252  {
253  public:
254  // Constructor - nothing to construct
256 
259 
261  void operator=(const IdentityPreconditioner&) = delete;
262 
265 
267  virtual void setup()
268  {
269  // first attempt to cast to DistributableLinearAlgebraObject
270  DistributableLinearAlgebraObject* dist_matrix_pt =
272 
273  // if it is a distributable matrix
274  if (dist_matrix_pt != 0)
275  {
276  // store the distribution
277  this->build_distribution(dist_matrix_pt->distribution_pt());
278  }
279  // else it is not a distributable matrix
280  else
281  {
282  // # of rows in the matrix
283  unsigned n_row = matrix_pt()->nrow();
284 
285  LinearAlgebraDistribution dist(comm_pt(), n_row, false);
286  this->build_distribution(dist);
287  }
288  }
289 
293  {
294 #ifdef PARANOID
295  if (*r.distribution_pt() != *this->distribution_pt())
296  {
297  std::ostringstream error_message_stream;
298  error_message_stream
299  << "The r vector must have the same distribution as the "
300  "preconditioner. "
301  << "(this is the same as the matrix passed to setup())";
302  throw OomphLibError(error_message_stream.str(),
305  }
306  if (z.built())
307  {
308  if (*z.distribution_pt() != *this->distribution_pt())
309  {
310  std::ostringstream error_message_stream;
311  error_message_stream
312  << "The z vector distribution has been setup; it must have the "
313  << "same distribution as the r vector (and preconditioner).";
314  throw OomphLibError(error_message_stream.str(),
317  }
318  }
319 #endif
320 
321  // apply
322  z = r;
323  }
324 
325 
329  {
330  // Applying the preconditioner to the transposed system is exactly the
331  // same as applying the preconditioner to the original system
333  }
334  };
335 } // namespace oomph
336 
337 #endif
Definition: linear_algebra_distribution.h:435
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
Definition: linear_algebra_distribution.h:457
void build_distribution(const LinearAlgebraDistribution *const dist_pt)
Definition: linear_algebra_distribution.h:507
Definition: matrices.h:261
virtual unsigned long nrow() const =0
Return the number of rows of the matrix.
Definition: double_vector.h:58
bool built() const
Definition: double_vector.h:154
The Identity Preconditioner.
Definition: preconditioner.h:252
void preconditioner_solve_transpose(const DoubleVector &r, DoubleVector &z)
Definition: preconditioner.h:328
IdentityPreconditioner(const IdentityPreconditioner &)=delete
Broken copy constructor.
virtual ~IdentityPreconditioner()
Destructor (empty)
Definition: preconditioner.h:264
void operator=(const IdentityPreconditioner &)=delete
Broken assignment operator.
IdentityPreconditioner()
Definition: preconditioner.h:255
virtual void setup()
setup method - just sets the distribution
Definition: preconditioner.h:267
virtual void preconditioner_solve(const DoubleVector &r, DoubleVector &z)
Definition: preconditioner.h:292
Definition: linear_algebra_distribution.h:64
OomphCommunicator * communicator_pt() const
const access to the communicator pointer
Definition: linear_algebra_distribution.h:335
Definition: communicator.h:54
Definition: oomph_definitions.h:222
Definition: preconditioner.h:54
double Setup_time
The time it takes to set up this preconditioner.
Definition: preconditioner.h:243
DoubleMatrixBase * Matrix_pt
Storage for a pointer to the matrix.
Definition: preconditioner.h:236
void setup(DoubleMatrixBase *matrix_pt)
Definition: preconditioner.h:94
virtual void turn_into_subsidiary_block_preconditioner(BlockPreconditioner< CRDoubleMatrix > *master_block_prec_pt, const Vector< unsigned > &doftype_in_master_preconditioner_coarse)
Definition: preconditioner.h:210
virtual void preconditioner_solve_transpose(const DoubleVector &r, DoubleVector &z)
Definition: preconditioner.h:82
void disable_silent_preconditioner_setup()
Be verbose in the block preconditioner setup.
Definition: preconditioner.h:135
std::ostream * Stream_pt
Pointer to the output stream – defaults to std::cout.
Definition: preconditioner.h:232
virtual void set_comm_pt(const OomphCommunicator *const comm_pt)
Set the communicator pointer.
Definition: preconditioner.h:193
virtual void clean_up_memory()
Clean up memory (empty). Generic interface function.
Definition: preconditioner.h:147
void operator=(const Preconditioner &)=delete
Broken assignment operator.
virtual DoubleMatrixBase * matrix_pt() const
Get function for matrix pointer.
Definition: preconditioner.h:150
void setup(const Problem *problem_pt, DoubleMatrixBase *matrix_pt)
Definition: preconditioner.h:120
Preconditioner()
Constructor.
Definition: preconditioner.h:57
double setup_time() const
Returns the time to setup the preconditioner.
Definition: preconditioner.h:199
const OomphCommunicator * Comm_pt
Definition: preconditioner.h:240
void enable_silent_preconditioner_setup()
Set up the block preconditioner quietly!
Definition: preconditioner.h:127
virtual ~Preconditioner()
Destructor (empty)
Definition: preconditioner.h:71
Preconditioner(const Preconditioner &)=delete
Broken copy constructor.
virtual void preconditioner_solve(const DoubleVector &r, DoubleVector &z)=0
bool Silent_preconditioner_setup
Boolean to indicate whether or not the build should be done silently.
Definition: preconditioner.h:229
virtual void turn_into_subsidiary_block_preconditioner(BlockPreconditioner< CRDoubleMatrix > *master_block_prec_pt, const Vector< unsigned > &doftype_in_master_preconditioner_coarse, const Vector< Vector< unsigned >> &doftype_coarsen_map_coarse)
Definition: preconditioner.h:220
virtual const OomphCommunicator * comm_pt() const
Get function for comm pointer.
Definition: preconditioner.h:171
virtual void set_matrix_pt(DoubleMatrixBase *matrix_pt)
Set the matrix pointer.
Definition: preconditioner.h:165
virtual void setup()=0
Definition: problem.h:151
r
Definition: UniformPSDSelfTest.py:20
void obsolete()
Output warning message.
Definition: oomph_utilities.cc:1102
double timer()
returns the time in seconds after some point in past
Definition: oomph_utilities.cc:1295
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86