trilinos_solver.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 #ifndef OOMPH_TRILINOS_SOLVER_HEADER
27 #define OOMPH_TRILINOS_SOLVER_HEADER
28 
31 
32 #include "AztecOO.h"
33 
34 namespace oomph
35 {
36  //=============================================================================
41  //=============================================================================
43  {
44  public:
53  bool use_epetra_values = false)
54 #ifdef OOMPH_HAS_MPI
55  : Operator_comm(
56  preconditioner_pt->distribution_pt()->communicator_pt()->mpi_comm()),
57  Use_epetra_values(use_epetra_values)
58 #else
59  : Operator_comm(), Use_epetra_values(use_epetra_values)
60 #endif
61  {
62  // set the ooomph-lib preconditioner
63  Oomph_lib_preconditioner_pt = preconditioner_pt;
64 
65  // set the preconditioner label
66  Preconditioner_label = "oomph-lib Preconditioner";
67 
68  // setup the Epetra_map
71  }
72 
76  {
77  delete Operator_map_pt;
78  Operator_map_pt = 0;
79  }
80 
83  const OomphLibPreconditionerEpetraOperator&) = delete;
84 
87 
90  {
91  std::ostringstream error_message;
92  error_message << "SetUseTranspose() is a pure virtual Epetra_Operator "
93  << "member that is not required for a Preconditioner"
94  << std::endl;
95  throw OomphLibError(
96  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
97  }
98 
100  int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
101  {
102  std::ostringstream error_message;
103  error_message << "Apply() is a pure virtual Epetra_Operator member"
104  << "that is not required for a Preconditioner" << std::endl;
105  throw OomphLibError(
106  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
107  }
108 
109 
115  int ApplyInverse(const Epetra_MultiVector& epetra_r,
116  Epetra_MultiVector& epetra_z) const
117  {
118  // the oomph-lib vector for r
119  DoubleVector oomph_r;
120 
121  // copy the Epetra_MultiVector r into an oomph-lib vector
122  double** r_pt;
123  epetra_r.ExtractView(&r_pt);
124  if (Use_epetra_values)
125  {
126  oomph_r.set_external_values(
128  }
129  else
130  {
132  unsigned nrow_local =
134  for (unsigned i = 0; i < nrow_local; i++)
135  {
136  oomph_r[i] = r_pt[0][i];
137  }
138  }
139 
140  // oomph-lib vector for Y
141  DoubleVector oomph_z;
142  if (Use_epetra_values)
143  {
144  double** z_pt;
145  epetra_z.ExtractView(&z_pt);
146  DoubleVector oomph_z;
147  oomph_z.set_external_values(
149  }
150  else
151  {
153  }
154 
155  // apply the preconditioner
157 
158  // if not using external data copy the oomph-lib vector oomph_Y back to Y
159  if (!Use_epetra_values)
160  {
161  unsigned nrow_local =
163  for (unsigned i = 0; i < nrow_local; i++)
164  {
165  epetra_z.ReplaceMyValue(i, 0, oomph_z[i]);
166  }
167  }
168 
169  // return 0 to indicate success
170  return 0;
171  }
172 
174  double NormInf() const
175  {
176  std::ostringstream error_message;
177  error_message << "NormInf() is a pure virtual Epetra_Operator member"
178  << "that is not required for a Preconditioner" << std::endl;
179  throw OomphLibError(
180  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
181  }
182 
184  const char* Label() const
185  {
186  return Preconditioner_label.c_str();
187  }
188 
190  bool UseTranspose() const
191  {
192  std::ostringstream error_message;
193  error_message
194  << "UseTranspose() is a pure virtual Epetra_Operator member "
195  << "that is not required for a Preconditioner" << std::endl;
196  throw OomphLibError(
197  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
198  }
199 
201  bool HasNormInf() const
202  {
203  std::ostringstream error_message;
204  error_message << "HasNormInf() is a pure virtual Epetra_Operator member "
205  << "that is not required for a Preconditioner" << std::endl;
206  throw OomphLibError(
207  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
208  }
209 
211  const Epetra_Comm& Comm() const
212  {
213  return Operator_comm;
214  }
215 
217  const Epetra_Map& OperatorDomainMap() const
218  {
219  return *Operator_map_pt;
220  }
221 
223  const Epetra_Map& OperatorRangeMap() const
224  {
225  return *Operator_map_pt;
226  }
227 
228 
229  private:
232 
233 #ifdef OOMPH_HAS_MPI
235  Epetra_MpiComm Operator_comm;
236 #else
238  Epetra_SerialComm Operator_comm;
239 #endif
240 
245 
250  Epetra_Map* Operator_map_pt;
251 
254  };
255 
256 
257  //=============================================================================
265  //=============================================================================
267  {
268  public:
271  {
272  // By default use workaround for creating of epetra matrix that
273  // respects aztecoo's ordering requirements
275 
276  // set pointer to Null
277  AztecOO_solver_pt = 0;
278 
279  // initially assume not problem based solve
281 
282  // if a problem based solve is performed then it should generate a
283  // serial matrix
284  Assemble_serial_jacobian = false;
285 
286  // by default we do not use external values in the oomph-lib
287  // preconditioner
289 
290  // null the pts
291  Problem_pt = 0;
292  Epetra_matrix_pt = 0;
294 
295  // set solver defaults
296  Solver_type = GMRES;
297  Tolerance = 10e-10;
298 
299  // don't delete the matrix
300  Delete_matrix = false;
301  }
302 
305  {
306  // delete
307  clean_up_memory();
308 
309  // if Problem_based solve then the oomph matrix was generated by this
310  // class and should be deleted
312  {
313  delete Oomph_matrix_pt;
314  Oomph_matrix_pt = 0;
315  }
316  }
317 
320 
322  void operator=(const TrilinosAztecOOSolver&) = delete;
323 
327  {
329  }
330 
334  {
336  }
337 
341  {
343  }
344 
348  {
349  // delete the solver
350  delete AztecOO_solver_pt;
351  AztecOO_solver_pt = 0;
352 
353  // delete the Epetra_Operator preconditioner (only if it is a wrapper to
354  // an oomphlib preconditioner in which case only the wrapper is deleted
355  // and not the actual preconditioner). if the preconditioner is Trilinos
356  // preconditioner then the Epetra_Operator is deleted when that
357  // preconditioner is deleted
358  if (Epetra_preconditioner_pt != 0)
359  {
360  if (dynamic_cast<OomphLibPreconditionerEpetraOperator*>(
362  {
365  }
366  }
367 
368  // don't delete the preconditioner but call its clean up memory method
369  if (this->preconditioner_pt() != 0)
370  {
372  }
373 
374 
375  // delete the matrices
376  // This must now happen after the preconditioner delete because the
377  // ML preconditioner (and maybe others) use the communicator from the
378  // matrix in the destructor
379  delete Epetra_matrix_pt;
380  Epetra_matrix_pt = 0;
381  }
382 
386  void solve(Problem* const& problem_pt, DoubleVector& solution);
387 
393  void solve(DoubleMatrixBase* const& matrix_pt,
394  const DoubleVector& rhs,
396 
401  void resolve(const DoubleVector& rhs, DoubleVector& solution);
402 
406  {
407  Enable_resolve = false;
408  clean_up_memory();
409  }
410 
413  {
414  Delete_matrix = true;
415  }
416 
419  {
420  Delete_matrix = false;
421  }
422 
424  unsigned& max_iter()
425  {
426  return Max_iter;
427  }
428 
430  unsigned iterations() const
431  {
432  return Iterations;
433  }
434 
436  double& tolerance()
437  {
438  return Tolerance;
439  }
440 
442  unsigned& solver_type()
443  {
444  return Solver_type;
445  }
446 
449  {
450  return Jacobian_setup_time;
451  }
452 
455  {
457  }
458 
462  {
464  }
465 
468  {
469  Assemble_serial_jacobian = false;
470  }
471 
477  // bool& if_oomphlib_preconditioner_use_epetra_values()
478  // {
479  // return If_oomphlib_preconditioner_use_epetra_values;
480  // }
481 
484  {
485  CG,
487  BiCGStab
488  };
489 
490  protected:
493  void solve_using_AztecOO(Epetra_Vector*& rhs_pt, Epetra_Vector*& soln_pt);
494 
499  void solver_setup(DoubleMatrixBase* const& matrix_pt);
500 
504 
506  unsigned Iterations;
507 
510 
513 
516 
524 
528 
531  unsigned Solver_type;
532 
536 
538  Epetra_CrsMatrix* Epetra_matrix_pt;
539 
543 
546 
551 
557  };
558 
559 } // namespace oomph
560 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
The conjugate gradient method.
Definition: iterative_linear_solver.h:410
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
Definition: linear_algebra_distribution.h:457
Definition: matrices.h:261
Definition: double_vector.h:58
void set_external_values(const LinearAlgebraDistribution *const &dist_pt, double *external_values, bool delete_external_values)
Definition: double_vector.h:167
void build(const DoubleVector &old_vector)
Just copys the argument DoubleVector.
Definition: double_vector.cc:35
Definition: iterative_linear_solver.h:54
double Tolerance
Convergence tolerance.
Definition: iterative_linear_solver.h:239
Preconditioner *& preconditioner_pt()
Access function to preconditioner.
Definition: iterative_linear_solver.h:95
unsigned Max_iter
Maximum number of iterations.
Definition: iterative_linear_solver.h:242
OomphCommunicator * communicator_pt() const
const access to the communicator pointer
Definition: linear_algebra_distribution.h:335
unsigned nrow_local() const
Definition: linear_algebra_distribution.h:193
bool Enable_resolve
Definition: linear_solver.h:73
Definition: oomph_definitions.h:222
Definition: trilinos_solver.h:43
Epetra_SerialComm Operator_comm
An Epetra Serial Comm object.
Definition: trilinos_solver.h:238
Preconditioner * Oomph_lib_preconditioner_pt
A pointer to the oomph-lib preconditioner.
Definition: trilinos_solver.h:231
const Epetra_Map & OperatorDomainMap() const
Epetra_Operator member - OperatorDomainMap.
Definition: trilinos_solver.h:217
const Epetra_Map & OperatorRangeMap() const
Epetra_Operator member - OperatorRangeMap.
Definition: trilinos_solver.h:223
OomphLibPreconditionerEpetraOperator(const OomphLibPreconditionerEpetraOperator &)=delete
Broken copy constructor.
bool Use_epetra_values
Definition: trilinos_solver.h:244
const char * Label() const
Epetra_Operator::Label - returns a string describing the operator.
Definition: trilinos_solver.h:184
bool HasNormInf() const
Broken Epetra_Operator member - HasNormInf.
Definition: trilinos_solver.h:201
int ApplyInverse(const Epetra_MultiVector &epetra_r, Epetra_MultiVector &epetra_z) const
Definition: trilinos_solver.h:115
~OomphLibPreconditionerEpetraOperator()
Definition: trilinos_solver.h:75
void operator=(const OomphLibPreconditionerEpetraOperator &)=delete
Broken assignment operator.
std::string Preconditioner_label
a label for the preconditioner ( for Epetra_Operator::Label() )
Definition: trilinos_solver.h:253
bool UseTranspose() const
Broken Epetra_Operator member - UseTranspose.
Definition: trilinos_solver.h:190
double NormInf() const
Broken Epetra_Operator member - NormInf.
Definition: trilinos_solver.h:174
OomphLibPreconditionerEpetraOperator(Preconditioner *preconditioner_pt, bool use_epetra_values=false)
Definition: trilinos_solver.h:52
int SetUseTranspose(bool UseTranspose)
Broken Epetra_Operator member - SetUseTranspose.
Definition: trilinos_solver.h:89
const Epetra_Comm & Comm() const
Returns the Epetra MPI_Comm object.
Definition: trilinos_solver.h:211
int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Broken Epetra_Operator member - Apply.
Definition: trilinos_solver.h:100
Epetra_Map * Operator_map_pt
Definition: trilinos_solver.h:250
Definition: preconditioner.h:54
virtual void clean_up_memory()
Clean up memory (empty). Generic interface function.
Definition: preconditioner.h:147
virtual void preconditioner_solve(const DoubleVector &r, DoubleVector &z)=0
Definition: problem.h:151
Definition: trilinos_solver.h:267
bool Using_problem_based_solve
Definition: trilinos_solver.h:535
void resolve(const DoubleVector &rhs, DoubleVector &solution)
Definition: trilinos_solver.cc:489
bool If_oomphlib_preconditioner_use_epetra_values
Definition: trilinos_solver.h:556
void solver_setup(DoubleMatrixBase *const &matrix_pt)
Definition: trilinos_solver.cc:283
void disable_aztecoo_workaround_for_epetra_matrix_setup()
Definition: trilinos_solver.h:333
unsigned Iterations
Stores number of iterations used.
Definition: trilinos_solver.h:506
TrilinosAztecOOSolver()
Constructor.
Definition: trilinos_solver.h:270
Epetra_CrsMatrix * Epetra_matrix_pt
A pointer for the linear system matrix in Epetra_CrsMatrix format.
Definition: trilinos_solver.h:538
void clean_up_memory()
Definition: trilinos_solver.h:347
double jacobian_setup_time()
Function to return Jacobian_setup_time;.
Definition: trilinos_solver.h:448
bool Assemble_serial_jacobian
Definition: trilinos_solver.h:527
void solve_using_AztecOO(Epetra_Vector *&rhs_pt, Epetra_Vector *&soln_pt)
Definition: trilinos_solver.cc:570
unsigned & max_iter()
Access function to Max_iter.
Definition: trilinos_solver.h:424
bool Delete_matrix
Definition: trilinos_solver.h:523
void disable_delete_matrix()
Call if the matrix can not be deleted (default)
Definition: trilinos_solver.h:418
DoubleMatrixBase * Oomph_matrix_pt
Oomph lib matrix pointer.
Definition: trilinos_solver.h:545
Epetra_Operator * Epetra_preconditioner_pt
Definition: trilinos_solver.h:542
~TrilinosAztecOOSolver()
Destructor - delete the solver and the matrices.
Definition: trilinos_solver.h:304
void disable_resolve()
Definition: trilinos_solver.h:405
void solve(Problem *const &problem_pt, DoubleVector &solution)
Definition: trilinos_solver.cc:43
void enable_assemble_serial_jacobian()
Definition: trilinos_solver.h:461
double linear_solver_solution_time()
Function to return Linear_solver_solution_time.
Definition: trilinos_solver.h:454
AztecOO_solver_types
Enumerated list to define which AztecOO solver is used.
Definition: trilinos_solver.h:484
@ GMRES
Definition: trilinos_solver.h:486
@ CG
Definition: trilinos_solver.h:485
unsigned & solver_type()
Access function to Solver_type.
Definition: trilinos_solver.h:442
void disable_assemble_serial_jacobian()
Unset the assembly of the serial jacobian.
Definition: trilinos_solver.h:467
TrilinosAztecOOSolver(const TrilinosAztecOOSolver &)=delete
Broken copy constructor.
void enable_aztecoo_workaround_for_epetra_matrix_setup()
Definition: trilinos_solver.h:326
unsigned Solver_type
Definition: trilinos_solver.h:531
unsigned iterations() const
Acess function to Iterations.
Definition: trilinos_solver.h:430
double Linear_solver_solution_time
Stores time for the solution (excludes time to set up preconditioner)
Definition: trilinos_solver.h:515
Problem * Problem_pt
Definition: trilinos_solver.h:550
bool is_aztecoo_workaround_for_epetra_matrix_setup_enabled()
Definition: trilinos_solver.h:340
void enable_delete_matrix()
Call if the matrix can be deleted.
Definition: trilinos_solver.h:412
AztecOO * AztecOO_solver_pt
Pointer to the AztecOO solver.
Definition: trilinos_solver.h:509
void operator=(const TrilinosAztecOOSolver &)=delete
Broken assignment operator.
bool Use_aztecoo_workaround_for_epetra_matrix_setup
Definition: trilinos_solver.h:503
double & tolerance()
Access function to Tolerance.
Definition: trilinos_solver.h:436
double Jacobian_setup_time
Stores set up time for Jacobian.
Definition: trilinos_solver.h:512
#define X
Definition: icosphere.cpp:20
void solution(const Vector< double > &x, Vector< double > &u)
Definition: two_d_biharmonic.cc:113
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
Epetra_Map * create_epetra_map(const LinearAlgebraDistribution *const dist)
create an Epetra_Map corresponding to the LinearAlgebraDistribution
Definition: trilinos_helpers.cc:977
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
const char Y
Definition: test/EulerAngles.cpp:32