oomph::TrilinosAztecOOSolver Class Reference

#include <trilinos_solver.h>

+ Inheritance diagram for oomph::TrilinosAztecOOSolver:

Public Types

enum  AztecOO_solver_types { CG , GMRES , BiCGStab }
 Enumerated list to define which AztecOO solver is used. More...
 

Public Member Functions

 TrilinosAztecOOSolver ()
 Constructor. More...
 
 ~TrilinosAztecOOSolver ()
 Destructor - delete the solver and the matrices. More...
 
 TrilinosAztecOOSolver (const TrilinosAztecOOSolver &)=delete
 Broken copy constructor. More...
 
void operator= (const TrilinosAztecOOSolver &)=delete
 Broken assignment operator. More...
 
void enable_aztecoo_workaround_for_epetra_matrix_setup ()
 
void disable_aztecoo_workaround_for_epetra_matrix_setup ()
 
bool is_aztecoo_workaround_for_epetra_matrix_setup_enabled ()
 
void clean_up_memory ()
 
void solve (Problem *const &problem_pt, DoubleVector &solution)
 
void solve (DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
 
void resolve (const DoubleVector &rhs, DoubleVector &solution)
 
void disable_resolve ()
 
void enable_delete_matrix ()
 Call if the matrix can be deleted. More...
 
void disable_delete_matrix ()
 Call if the matrix can not be deleted (default) More...
 
unsignedmax_iter ()
 Access function to Max_iter. More...
 
unsigned iterations () const
 Acess function to Iterations. More...
 
doubletolerance ()
 Access function to Tolerance. More...
 
unsignedsolver_type ()
 Access function to Solver_type. More...
 
double jacobian_setup_time ()
 Function to return Jacobian_setup_time;. More...
 
double linear_solver_solution_time ()
 Function to return Linear_solver_solution_time. More...
 
void enable_assemble_serial_jacobian ()
 
void disable_assemble_serial_jacobian ()
 Unset the assembly of the serial jacobian. More...
 
- Public Member Functions inherited from oomph::IterativeLinearSolver
 IterativeLinearSolver ()
 
 IterativeLinearSolver (const IterativeLinearSolver &)=delete
 Broken copy constructor. More...
 
void operator= (const IterativeLinearSolver &)=delete
 Broken assignment operator. More...
 
virtual ~IterativeLinearSolver ()
 Destructor (empty) More...
 
Preconditioner *& preconditioner_pt ()
 Access function to preconditioner. More...
 
Preconditioner *const & preconditioner_pt () const
 Access function to preconditioner (const version) More...
 
doubletolerance ()
 Access to convergence tolerance. More...
 
unsignedmax_iter ()
 Access to max. number of iterations. More...
 
void enable_doc_convergence_history ()
 Enable documentation of the convergence history. More...
 
void disable_doc_convergence_history ()
 Disable documentation of the convergence history. More...
 
void open_convergence_history_file_stream (const std::string &file_name, const std::string &zone_title="")
 
void close_convergence_history_file_stream ()
 Close convergence history output stream. More...
 
double jacobian_setup_time () const
 
double linear_solver_solution_time () const
 return the time taken to solve the linear system More...
 
virtual double preconditioner_setup_time () const
 returns the the time taken to setup the preconditioner More...
 
void enable_setup_preconditioner_before_solve ()
 Setup the preconditioner before the solve. More...
 
void disable_setup_preconditioner_before_solve ()
 Don't set up the preconditioner before the solve. More...
 
void enable_error_after_max_iter ()
 Throw an error if we don't converge within max_iter. More...
 
void disable_error_after_max_iter ()
 Don't throw an error if we don't converge within max_iter (default). More...
 
void enable_iterative_solver_as_preconditioner ()
 
void disable_iterative_solver_as_preconditioner ()
 
- Public Member Functions inherited from oomph::LinearSolver
 LinearSolver ()
 Empty constructor, initialise the member data. More...
 
 LinearSolver (const LinearSolver &dummy)=delete
 Broken copy constructor. More...
 
void operator= (const LinearSolver &)=delete
 Broken assignment operator. More...
 
virtual ~LinearSolver ()
 Empty virtual destructor. More...
 
void enable_doc_time ()
 Enable documentation of solve times. More...
 
void disable_doc_time ()
 Disable documentation of solve times. More...
 
bool is_doc_time_enabled () const
 Is documentation of solve times enabled? More...
 
bool is_resolve_enabled () const
 Boolean flag indicating if resolves are enabled. More...
 
virtual void enable_resolve ()
 
virtual void solve (DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
 
virtual void solve_transpose (Problem *const &problem_pt, DoubleVector &result)
 
virtual void solve_transpose (DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &result)
 
virtual void solve_transpose (DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
 
virtual void resolve_transpose (const DoubleVector &rhs, DoubleVector &result)
 
virtual void enable_computation_of_gradient ()
 
void disable_computation_of_gradient ()
 
void reset_gradient ()
 
void get_gradient (DoubleVector &gradient)
 function to access the gradient, provided it has been computed More...
 
- Public Member Functions inherited from oomph::DistributableLinearAlgebraObject
 DistributableLinearAlgebraObject ()
 Default constructor - create a distribution. More...
 
 DistributableLinearAlgebraObject (const DistributableLinearAlgebraObject &matrix)=delete
 Broken copy constructor. More...
 
void operator= (const DistributableLinearAlgebraObject &)=delete
 Broken assignment operator. More...
 
virtual ~DistributableLinearAlgebraObject ()
 Destructor. More...
 
LinearAlgebraDistributiondistribution_pt () const
 access to the LinearAlgebraDistribution More...
 
unsigned nrow () const
 access function to the number of global rows. More...
 
unsigned nrow_local () const
 access function for the num of local rows on this processor. More...
 
unsigned nrow_local (const unsigned &p) const
 access function for the num of local rows on this processor. More...
 
unsigned first_row () const
 access function for the first row on this processor More...
 
unsigned first_row (const unsigned &p) const
 access function for the first row on this processor More...
 
bool distributed () const
 distribution is serial or distributed More...
 
bool distribution_built () const
 
void build_distribution (const LinearAlgebraDistribution *const dist_pt)
 
void build_distribution (const LinearAlgebraDistribution &dist)
 

Protected Member Functions

void solve_using_AztecOO (Epetra_Vector *&rhs_pt, Epetra_Vector *&soln_pt)
 
void solver_setup (DoubleMatrixBase *const &matrix_pt)
 
- Protected Member Functions inherited from oomph::DistributableLinearAlgebraObject
void clear_distribution ()
 

Protected Attributes

bool Use_aztecoo_workaround_for_epetra_matrix_setup
 
unsigned Iterations
 Stores number of iterations used. More...
 
AztecOO * AztecOO_solver_pt
 Pointer to the AztecOO solver. More...
 
double Jacobian_setup_time
 Stores set up time for Jacobian. More...
 
double Linear_solver_solution_time
 Stores time for the solution (excludes time to set up preconditioner) More...
 
bool Delete_matrix
 
bool Assemble_serial_jacobian
 
unsigned Solver_type
 
bool Using_problem_based_solve
 
Epetra_CrsMatrix * Epetra_matrix_pt
 A pointer for the linear system matrix in Epetra_CrsMatrix format. More...
 
Epetra_OperatorEpetra_preconditioner_pt
 
DoubleMatrixBaseOomph_matrix_pt
 Oomph lib matrix pointer. More...
 
ProblemProblem_pt
 
bool If_oomphlib_preconditioner_use_epetra_values
 
- Protected Attributes inherited from oomph::IterativeLinearSolver
bool Doc_convergence_history
 
std::ofstream Output_file_stream
 Output file stream for convergence history. More...
 
double Tolerance
 Convergence tolerance. More...
 
unsigned Max_iter
 Maximum number of iterations. More...
 
PreconditionerPreconditioner_pt
 Pointer to the preconditioner. More...
 
double Jacobian_setup_time
 Jacobian setup time. More...
 
double Solution_time
 linear solver solution time More...
 
double Preconditioner_setup_time
 Preconditioner setup time. More...
 
bool Setup_preconditioner_before_solve
 
bool Throw_error_after_max_iter
 
bool Use_iterative_solver_as_preconditioner
 Use the iterative solver as preconditioner. More...
 
bool First_time_solve_when_used_as_preconditioner
 
- Protected Attributes inherited from oomph::LinearSolver
bool Enable_resolve
 
bool Doc_time
 Boolean flag that indicates whether the time taken. More...
 
bool Compute_gradient
 
bool Gradient_has_been_computed
 flag that indicates whether the gradient was computed or not More...
 
DoubleVector Gradient_for_glob_conv_newton_solve
 

Additional Inherited Members

- Static Protected Attributes inherited from oomph::IterativeLinearSolver
static IdentityPreconditioner Default_preconditioner
 

Detailed Description

An interface to the Trilinos AztecOO classes allowing it to be used as an Oomph-lib LinearSolver. The AztecOO solver is a Krylov Subspace solver; the solver type (either CG, GMRES or BiCGStab) can be set using solver_type(). This solver can be preconditioned with Trilinos Preconditioners (derived from TrilinosPreconditionerBase) or Oomph-lib preconditioners (derived from Preconditioner). Preconditioners are set using preconditioner_pt().

Member Enumeration Documentation

◆ AztecOO_solver_types

Enumerated list to define which AztecOO solver is used.

if this solver is using an oomph-lib preconditioner then the vectors passed to preconditioner_solve(...) should be using the values in the epetra vector as external data. If the vectors are using external data then rebuild(...) methods cannot be used be used in the preconditioner.

Enumerator
CG 
GMRES 
BiCGStab 
484  {
485  CG,
486  GMRES,
487  BiCGStab
488  };
@ GMRES
Definition: trilinos_solver.h:486
@ CG
Definition: trilinos_solver.h:485
@ BiCGStab
Definition: trilinos_solver.h:487

Constructor & Destructor Documentation

◆ TrilinosAztecOOSolver() [1/2]

oomph::TrilinosAztecOOSolver::TrilinosAztecOOSolver ( )
inline

Constructor.

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  }
Array< double, 1, 3 > e(1./3., 0.5, 2.)
double Tolerance
Convergence tolerance.
Definition: iterative_linear_solver.h:239
bool Using_problem_based_solve
Definition: trilinos_solver.h:535
bool If_oomphlib_preconditioner_use_epetra_values
Definition: trilinos_solver.h:556
Epetra_CrsMatrix * Epetra_matrix_pt
A pointer for the linear system matrix in Epetra_CrsMatrix format.
Definition: trilinos_solver.h:538
bool Assemble_serial_jacobian
Definition: trilinos_solver.h:527
bool Delete_matrix
Definition: trilinos_solver.h:523
Epetra_Operator * Epetra_preconditioner_pt
Definition: trilinos_solver.h:542
unsigned Solver_type
Definition: trilinos_solver.h:531
Problem * Problem_pt
Definition: trilinos_solver.h:550
AztecOO * AztecOO_solver_pt
Pointer to the AztecOO solver.
Definition: trilinos_solver.h:509
bool Use_aztecoo_workaround_for_epetra_matrix_setup
Definition: trilinos_solver.h:503

References Assemble_serial_jacobian, AztecOO_solver_pt, Delete_matrix, e(), Epetra_matrix_pt, Epetra_preconditioner_pt, GMRES, If_oomphlib_preconditioner_use_epetra_values, Problem_pt, Solver_type, oomph::IterativeLinearSolver::Tolerance, Use_aztecoo_workaround_for_epetra_matrix_setup, and Using_problem_based_solve.

◆ ~TrilinosAztecOOSolver()

oomph::TrilinosAztecOOSolver::~TrilinosAztecOOSolver ( )
inline

Destructor - delete the solver and the matrices.

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  }
void clean_up_memory()
Definition: trilinos_solver.h:347
DoubleMatrixBase * Oomph_matrix_pt
Oomph lib matrix pointer.
Definition: trilinos_solver.h:545

References clean_up_memory(), Oomph_matrix_pt, and Using_problem_based_solve.

◆ TrilinosAztecOOSolver() [2/2]

oomph::TrilinosAztecOOSolver::TrilinosAztecOOSolver ( const TrilinosAztecOOSolver )
delete

Broken copy constructor.

Member Function Documentation

◆ clean_up_memory()

void oomph::TrilinosAztecOOSolver::clean_up_memory ( )
inlinevirtual

Clean up method - deletes the solver, the matrices and the preconditioner

Reimplemented from oomph::LinearSolver.

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  }
Preconditioner *& preconditioner_pt()
Access function to preconditioner.
Definition: iterative_linear_solver.h:95
virtual void clean_up_memory()
Clean up memory (empty). Generic interface function.
Definition: preconditioner.h:147

References AztecOO_solver_pt, oomph::Preconditioner::clean_up_memory(), Epetra_matrix_pt, Epetra_preconditioner_pt, and oomph::IterativeLinearSolver::preconditioner_pt().

Referenced by disable_resolve(), solve(), solver_setup(), and ~TrilinosAztecOOSolver().

◆ disable_assemble_serial_jacobian()

void oomph::TrilinosAztecOOSolver::disable_assemble_serial_jacobian ( )
inline

Unset the assembly of the serial jacobian.

468  {
469  Assemble_serial_jacobian = false;
470  }

References Assemble_serial_jacobian.

◆ disable_aztecoo_workaround_for_epetra_matrix_setup()

void oomph::TrilinosAztecOOSolver::disable_aztecoo_workaround_for_epetra_matrix_setup ( )
inline

Disable workaround for creating of epetra matrix that respects aztecoo's ordering requirements

334  {
336  }

References Use_aztecoo_workaround_for_epetra_matrix_setup.

◆ disable_delete_matrix()

void oomph::TrilinosAztecOOSolver::disable_delete_matrix ( )
inline

Call if the matrix can not be deleted (default)

419  {
420  Delete_matrix = false;
421  }

References Delete_matrix.

◆ disable_resolve()

void oomph::TrilinosAztecOOSolver::disable_resolve ( )
inlinevirtual

Disable resolve function (overloads the LinearSolver disable_resolve function).

Reimplemented from oomph::LinearSolver.

406  {
407  Enable_resolve = false;
408  clean_up_memory();
409  }
bool Enable_resolve
Definition: linear_solver.h:73

References clean_up_memory(), and oomph::LinearSolver::Enable_resolve.

◆ enable_assemble_serial_jacobian()

void oomph::TrilinosAztecOOSolver::enable_assemble_serial_jacobian ( )
inline

Set the assembly of the serial jacobian when performing a problem-based solve

462  {
464  }

References Assemble_serial_jacobian.

◆ enable_aztecoo_workaround_for_epetra_matrix_setup()

void oomph::TrilinosAztecOOSolver::enable_aztecoo_workaround_for_epetra_matrix_setup ( )
inline

Enable workaround for creating of epetra matrix that respects aztecoo's ordering requirements

327  {
329  }

References Use_aztecoo_workaround_for_epetra_matrix_setup.

◆ enable_delete_matrix()

void oomph::TrilinosAztecOOSolver::enable_delete_matrix ( )
inline

Call if the matrix can be deleted.

413  {
414  Delete_matrix = true;
415  }

References Delete_matrix.

◆ is_aztecoo_workaround_for_epetra_matrix_setup_enabled()

bool oomph::TrilinosAztecOOSolver::is_aztecoo_workaround_for_epetra_matrix_setup_enabled ( )
inline

Is workaround for creating of epetra matrix that respects aztecoo's ordering requirements enabled?

341  {
343  }

References Use_aztecoo_workaround_for_epetra_matrix_setup.

◆ iterations()

unsigned oomph::TrilinosAztecOOSolver::iterations ( ) const
inlinevirtual

Acess function to Iterations.

Implements oomph::IterativeLinearSolver.

431  {
432  return Iterations;
433  }
unsigned Iterations
Stores number of iterations used.
Definition: trilinos_solver.h:506

References Iterations.

◆ jacobian_setup_time()

double oomph::TrilinosAztecOOSolver::jacobian_setup_time ( )
inline

Function to return Jacobian_setup_time;.

449  {
450  return Jacobian_setup_time;
451  }
double Jacobian_setup_time
Stores set up time for Jacobian.
Definition: trilinos_solver.h:512

References Jacobian_setup_time.

◆ linear_solver_solution_time()

double oomph::TrilinosAztecOOSolver::linear_solver_solution_time ( )
inline

Function to return Linear_solver_solution_time.

455  {
457  }
double Linear_solver_solution_time
Stores time for the solution (excludes time to set up preconditioner)
Definition: trilinos_solver.h:515

References Linear_solver_solution_time.

◆ max_iter()

unsigned& oomph::TrilinosAztecOOSolver::max_iter ( )
inline

Access function to Max_iter.

425  {
426  return Max_iter;
427  }
unsigned Max_iter
Maximum number of iterations.
Definition: iterative_linear_solver.h:242

References oomph::IterativeLinearSolver::Max_iter.

Referenced by PseudoElasticCollapsibleChannelProblem< FLUID_ELEMENT, SOLID_ELEMENT >::set_pseudo_elastic_fsi_solver().

◆ operator=()

void oomph::TrilinosAztecOOSolver::operator= ( const TrilinosAztecOOSolver )
delete

Broken assignment operator.

◆ resolve()

void oomph::TrilinosAztecOOSolver::resolve ( const DoubleVector rhs,
DoubleVector solution 
)
virtual

Function to resolve a linear system using the existing solver data, allowing a solve with a new right hand side vector. This function must be used after a call to solve(...) with enable_resolve set to true.

Reimplemented from oomph::LinearSolver.

491  {
492  // start the timer
493  double start_t = TimingHelpers::timer();
494 
495 #ifdef PARANOID
496  if (Epetra_matrix_pt->NumGlobalRows() != static_cast<int>(rhs.nrow()))
497  {
498  std::ostringstream error_message;
499  error_message
500  << "The rhs vector and the matrix must have the same number "
501  << "of rows.\n"
502  << "The rhs vector has " << rhs.nrow() << " rows.\n"
503  << "The matrix has " << Epetra_matrix_pt->NumGlobalRows() << " rows.\n";
504  throw OomphLibError(
505  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
506  }
507 
508  // if the result vector is setup then check it is not distributed and has
509  // the same communicator as the rhs vector
510  if (solution.built())
511  {
512  if (!(*solution.distribution_pt() == *rhs.distribution_pt()))
513  {
514  std::ostringstream error_message_stream;
515  error_message_stream
516  << "The result vector distribution has been setup; it must have the "
517  << "same distribution as the rhs vector.";
518  throw OomphLibError(error_message_stream.str(),
521  }
522  }
523 #endif
524 
525  // build the result if not built
526  if (!solution.built())
527  {
528  solution.build(rhs.distribution_pt(), 0.0);
529  }
530 
531 
532  // create Epetra version of r
533  Epetra_Vector* epetra_r_pt =
535 
536  // create an empty Epetra vector for z
537  Epetra_Vector* epetra_z_pt =
539 
540  // solve the system
541  solve_using_AztecOO(epetra_r_pt, epetra_z_pt);
542 
543  // Copy result to z
544  if (!solution.distribution_built())
545  {
546  solution.build(rhs.distribution_pt(), 0.0);
547  }
549 
550  // clean up memory
551  delete epetra_r_pt;
552  delete epetra_z_pt;
553 
554  double end_t = TimingHelpers::timer();
555  Linear_solver_solution_time = end_t - start_t;
556 
557  // output timings and info
558  if (this->Doc_time)
559  {
560  oomph_info << "Time for resolve : "
561  << Linear_solver_solution_time << "s" << std::endl;
562  }
563  }
bool Doc_time
Boolean flag that indicates whether the time taken.
Definition: linear_solver.h:77
void solve_using_AztecOO(Epetra_Vector *&rhs_pt, Epetra_Vector *&soln_pt)
Definition: trilinos_solver.cc:570
void solution(const Vector< double > &x, Vector< double > &u)
Definition: two_d_biharmonic.cc:113
double timer()
returns the time in seconds after some point in past
Definition: oomph_utilities.cc:1295
void copy_to_oomphlib_vector(const Epetra_Vector *epetra_vec_pt, DoubleVector &oomph_vec)
Definition: trilinos_helpers.cc:180
Epetra_Vector * create_distributed_epetra_vector(const DoubleVector &oomph_vec)
Definition: trilinos_helpers.cc:41
OomphInfo oomph_info
Definition: oomph_definitions.cc:319
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References oomph::TrilinosEpetraHelpers::copy_to_oomphlib_vector(), oomph::TrilinosEpetraHelpers::create_distributed_epetra_vector(), oomph::DistributableLinearAlgebraObject::distribution_pt(), oomph::LinearSolver::Doc_time, Epetra_matrix_pt, Linear_solver_solution_time, oomph::DistributableLinearAlgebraObject::nrow(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, oomph::oomph_info, BiharmonicTestFunctions1::solution(), solve_using_AztecOO(), and oomph::TimingHelpers::timer().

◆ solve() [1/2]

void oomph::TrilinosAztecOOSolver::solve ( DoubleMatrixBase *const &  matrix_pt,
const DoubleVector rhs,
DoubleVector result 
)
virtual

Function to solve the linear system defined by matrix_pt and rhs. NOTE 1. The matrix has to be of type CRDoubleMatrix or DistributedCRDoubleMatrix. NOTE 2. This function will delete any existing internal data and generate a new AztecOO solver.

Reimplemented from oomph::LinearSolver.

138  {
139  // start the timer
140  double start_t = TimingHelpers::timer();
141 
142 #ifdef PARANOID
143  // check that the matrix is square
144  if (matrix_pt->nrow() != matrix_pt->ncol())
145  {
146  std::ostringstream error_message_stream;
147  error_message_stream << "The matrix at matrix_pt must be square.";
148  throw OomphLibError(error_message_stream.str(),
151  }
152  // check that the matrix and the rhs vector have the same nrow()
153  if (matrix_pt->nrow() != rhs.nrow())
154  {
155  std::ostringstream error_message_stream;
156  error_message_stream
157  << "The matrix and the rhs vector must have the same number of rows.";
158  throw OomphLibError(error_message_stream.str(),
161  }
162 
163  // if the matrix is distributable then it too should have the same
164  // communicator as the rhs vector and should not be distributed
165  CRDoubleMatrix* cr_matrix_pt = dynamic_cast<CRDoubleMatrix*>(matrix_pt);
166  if (cr_matrix_pt != 0)
167  {
168  OomphCommunicator temp_comm(*rhs.distribution_pt()->communicator_pt());
169  if (!(temp_comm == *cr_matrix_pt->distribution_pt()->communicator_pt()))
170  {
171  std::ostringstream error_message_stream;
172  error_message_stream
173  << "The matrix matrix_pt must have the same communicator as the "
174  "vectors"
175  << " rhs and result must have the same communicator";
176  throw OomphLibError(error_message_stream.str(),
179  }
180  }
181  else
182  {
183  throw OomphLibError("Matrix must be of type CRDoubleMatrix",
186  }
187 
188  // if the result vector is setup then check it is not distributed and has
189  // the same communicator as the rhs vector
190  if (result.built())
191  {
192  if (!(*result.distribution_pt() == *rhs.distribution_pt()))
193  {
194  std::ostringstream error_message_stream;
195  error_message_stream
196  << "The result vector distribution has been setup; it must have the "
197  << "same distribution as the rhs vector.";
198  throw OomphLibError(error_message_stream.str(),
201  }
202  }
203 #endif
204 
205  // build the result if not built
206  if (!result.built())
207  {
208  result.build(rhs.distribution_pt(), 0.0);
209  }
210 
212  {
213  // Only call the setup method if this is the first time we call the
214  // solve method
216  {
217  // setup the solver
218  solver_setup(matrix_pt);
219  // Do not call the setup again
221  // Enable resolve since we are not going to build the solver, the
222  // matrix and the wrapper to the preconditioner again
223  Enable_resolve = true;
224  }
225  }
226  else
227  {
228  // setup the solver
229  solver_setup(matrix_pt);
230  }
231 
232  // create Epetra version of r
233  Epetra_Vector* epetra_r_pt =
235 
236  // create an empty Epetra vector for z
237  Epetra_Vector* epetra_z_pt =
239 
240  double start_t_trilinos = TimingHelpers::timer();
241 
242  // solve the system
243  solve_using_AztecOO(epetra_r_pt, epetra_z_pt);
244 
245  double end_t_trilinos = TimingHelpers::timer();
246  if (this->Doc_time)
247  {
248  oomph_info << "Time for trilinos solve itself : "
249  << end_t_trilinos - start_t_trilinos << "s" << std::endl;
250  }
251  // Copy result to z
253 
254  // clean up memory
255  delete epetra_r_pt;
256  delete epetra_z_pt;
257 
258  // delete solver data if required
259  if (!Enable_resolve)
260  {
261  clean_up_memory();
262  }
263 
264  // stop timers and compute solve time
265  double end_t = TimingHelpers::timer();
266  Linear_solver_solution_time = end_t - start_t;
267 
268  // output timings and info
269  if (this->Doc_time)
270  {
271  oomph_info << "Time for complete trilinos solve : "
272  << Linear_solver_solution_time << "s" << std::endl;
273  }
274  }
bool Use_iterative_solver_as_preconditioner
Use the iterative solver as preconditioner.
Definition: iterative_linear_solver.h:265
bool First_time_solve_when_used_as_preconditioner
Definition: iterative_linear_solver.h:270
void solver_setup(DoubleMatrixBase *const &matrix_pt)
Definition: trilinos_solver.cc:283

References oomph::DoubleVector::build(), oomph::DoubleVector::built(), clean_up_memory(), oomph::LinearAlgebraDistribution::communicator_pt(), oomph::TrilinosEpetraHelpers::copy_to_oomphlib_vector(), oomph::TrilinosEpetraHelpers::create_distributed_epetra_vector(), oomph::DistributableLinearAlgebraObject::distribution_pt(), oomph::LinearSolver::Doc_time, oomph::LinearSolver::Enable_resolve, oomph::IterativeLinearSolver::First_time_solve_when_used_as_preconditioner, Linear_solver_solution_time, oomph::DoubleMatrixBase::ncol(), oomph::DistributableLinearAlgebraObject::nrow(), oomph::DoubleMatrixBase::nrow(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, oomph::oomph_info, solve_using_AztecOO(), solver_setup(), oomph::TimingHelpers::timer(), and oomph::IterativeLinearSolver::Use_iterative_solver_as_preconditioner.

◆ solve() [2/2]

void oomph::TrilinosAztecOOSolver::solve ( Problem *const &  problem_pt,
DoubleVector solution 
)
virtual

Function which uses problem_pt's get_jacobian(...) function to generate a linear system which is then solved. This function deletes any existing internal data and then generates a new AztecOO solver.

//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// Function which uses problem_pt's get_jacobian(...) function to generate a linear system which is then solved. This function deletes any existing internal data and then generates a new AztecOO solver.

Implements oomph::LinearSolver.

45  {
46  // MemoryUsage::doc_memory_usage("start of TrilinosAztecOOSolver::solve");
47  // MemoryUsage::insert_comment_to_continous_top(
48  // "start of TrilinosAztecOOSolver::solve");
49 
50  // clean up from previous solve
52 
53  // if we were previously using a problem based solve then we should delete
54  // the oomphlib matrix as it was created during the last solve
56  {
57  delete Oomph_matrix_pt;
58  Oomph_matrix_pt = NULL;
59  }
60 
61  // this is a problem based solve
63 
64  // store the problem_pt
65  Problem_pt = problem_pt;
66 
67  // Get oomph-lib Jacobian matrix and residual vector
68 
69  // record the start time
70  double start_t = TimingHelpers::timer();
71 
72  // MemoryUsage::doc_memory_usage("start of get_jacobian()");
73  // MemoryUsage::insert_comment_to_continous_top("start of
74  // get_jacobian()");
75 
76  // create the residual
77  DoubleVector residual;
78 
79  // create the jacobian
80  CRDoubleMatrix* cr_matrix_pt = new CRDoubleMatrix;
81  Oomph_matrix_pt = cr_matrix_pt;
82  problem_pt->get_jacobian(residual, *cr_matrix_pt);
83  this->build_distribution(residual.distribution_pt());
84 
85  // record the end time and compute the matrix setup time
86  double end_t = TimingHelpers::timer();
87  Jacobian_setup_time = end_t - start_t;
88  if (this->Doc_time)
89  {
90  oomph_info << "Time to generate Jacobian [sec] : "
91  << Jacobian_setup_time << std::endl;
92  }
93 
94 
95  // MemoryUsage::doc_memory_usage("after get_jacobian() in trilinos
96  // solver"); MemoryUsage::insert_comment_to_continous_top(
97  // "after get_jacobian() in trilinos solver");
98 
99  // store the distribution of the solution vector
100  if (!solution.built())
101  {
102  solution.build(this->distribution_pt(), 0.0);
103  }
104  LinearAlgebraDistribution solution_dist(solution.distribution_pt());
105 
106  // redistribute the distribution
107  solution.redistribute(this->distribution_pt());
108 
109  // MemoryUsage::doc_memory_usage("before trilinos solve");
110  // MemoryUsage::insert_comment_to_continous_top("before trilinos solve ");
111 
112  // continue solving using matrix based solve function
113  solve(Oomph_matrix_pt, residual, solution);
114 
115 
116  // MemoryUsage::doc_memory_usage("after trilinos solve");
117  // MemoryUsage::insert_comment_to_continous_top("after trilinos solve ");
118 
119  // return to the original distribution
120  solution.redistribute(&solution_dist);
121 
122  // MemoryUsage::doc_memory_usage("end of TrilinosAztecOOSolver::solve");
123  // MemoryUsage::insert_comment_to_continous_top(
124  // "end of TrilinosAztecOOSolver::solve");
125  }
std::vector< double > DoubleVector
loads clump configuration
Definition: ClumpInput.h:26
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
void solve(Problem *const &problem_pt, DoubleVector &solution)
Definition: trilinos_solver.cc:43

References oomph::DistributableLinearAlgebraObject::build_distribution(), clean_up_memory(), oomph::DistributableLinearAlgebraObject::distribution_pt(), oomph::LinearSolver::Doc_time, oomph::Problem::get_jacobian(), Jacobian_setup_time, oomph::oomph_info, Oomph_matrix_pt, Problem_pt, BiharmonicTestFunctions1::solution(), oomph::TimingHelpers::timer(), and Using_problem_based_solve.

◆ solve_using_AztecOO()

void oomph::TrilinosAztecOOSolver::solve_using_AztecOO ( Epetra_Vector *&  rhs_pt,
Epetra_Vector *&  soln_pt 
)
protected

Helper function performs the actual solve once the AztecOO solver is set up

Helper function performs the actual solve once the AztecOO solver is set up (i.e. solver_setup() is called)

572  {
573 #ifdef PARANOID
574  // check the matrix and rhs are of consistent sizes
575  if (AztecOO_solver_pt == 0)
576  {
577  std::ostringstream error_message;
578  error_message << "Solver must be called with solve(...) "
579  << "before resolve(...) to set it up.\n";
580  throw OomphLibError(
581  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
582  }
583 #endif
584 
585  // set the vectors
586  AztecOO_solver_pt->SetLHS(soln_pt);
587  AztecOO_solver_pt->SetRHS(rhs_pt);
588 
589  // perform solve
591 
592 
593  // output iterations and final norm
594  Iterations = AztecOO_solver_pt->NumIters();
595  if (Doc_time)
596  {
597  double norm = AztecOO_solver_pt->TrueResidual();
598  oomph_info << "Linear solver iterations : " << Iterations << std::endl;
599  oomph_info << "Final Relative Residual Norm: " << norm << std::endl;
600  }
601  }

References AztecOO_solver_pt, oomph::LinearSolver::Doc_time, Iterations, oomph::IterativeLinearSolver::Max_iter, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, oomph::oomph_info, and oomph::IterativeLinearSolver::Tolerance.

Referenced by resolve(), and solve().

◆ solver_setup()

void oomph::TrilinosAztecOOSolver::solver_setup ( DoubleMatrixBase *const &  matrix_pt)
protected

Helper function for setting up the solver. Converts the oomph-lib matrices to Epetra matrices, sets up the preconditioner, creates the Trilinos Aztec00 solver and passes in the matrix, preconditioner and parameters.

284  {
285  // clean up the memory
286  // - delete all except Oomph_matrix_pt, which may have been set in the
287  // problem based solve
288  clean_up_memory();
289 
290  // cast to CRDoubleMatrix
291  // note cast check performed in matrix based solve(...) method
292  CRDoubleMatrix* cast_matrix_pt = dynamic_cast<CRDoubleMatrix*>(matrix_pt);
293 
294  // store the distribution
295  // distribution of preconditioner is same as matrix
296  this->build_distribution(cast_matrix_pt->distribution_pt());
297 
298  // create the new solver
299  AztecOO_solver_pt = new AztecOO();
300 
301  // if the preconditioner is an oomph-lib preconditioner then we set it up
302  TrilinosPreconditionerBase* trilinos_prec_pt =
303  dynamic_cast<TrilinosPreconditionerBase*>(Preconditioner_pt);
304  if (trilinos_prec_pt == 0)
305  {
307  {
308  // setup the preconditioner
309  // start of prec setup
310  double prec_setup_start_t = TimingHelpers::timer();
311  Preconditioner_pt->setup(matrix_pt);
312  // start of prec setup
313  double prec_setup_finish_t = TimingHelpers::timer();
314  if (Doc_time)
315  {
316  double t_prec_setup = prec_setup_finish_t - prec_setup_start_t;
317  oomph_info << "Time for preconditioner setup [sec]: " << t_prec_setup
318  << std::endl;
319  }
320 #ifdef PARANOID
321  if (*Preconditioner_pt->distribution_pt() != *this->distribution_pt())
322  {
323  std::ostringstream error_message;
324  error_message << "The oomph-lib preconditioner and the solver must "
325  << "have the same distribution";
326  throw OomphLibError(error_message.str(),
329  }
330 #endif
331  }
332 
333  // wrap the oomphlib preconditioner in the Epetra_Operator derived
334  // OoomphLibPreconditionerEpetraOperator to allow it to be passed to the
335  // trilinos preconditioner
337  new OomphLibPreconditionerEpetraOperator(Preconditioner_pt);
338  }
339 
340  // create the matrix
341  double start_t_matrix = TimingHelpers::timer();
343  {
346  cast_matrix_pt);
347  }
348  else
349  {
352  cast_matrix_pt, cast_matrix_pt->distribution_pt());
353  }
354 
355  // record the end time and compute the matrix setup time
356  double end_t_matrix = TimingHelpers::timer();
357  if (trilinos_prec_pt == 0)
358  {
360  {
361  dynamic_cast<CRDoubleMatrix*>(Oomph_matrix_pt)->clear();
362  delete Oomph_matrix_pt;
363  Oomph_matrix_pt = NULL;
364  }
365 
366  // delete Oomph-lib matrix if requested
367  else if (Delete_matrix)
368  {
369  dynamic_cast<CRDoubleMatrix*>(matrix_pt)->clear();
370  }
371  }
372 
373  // output times
374  if (Doc_time)
375  {
376  oomph_info << "Time to generate Trilinos matrix : "
377  << double(end_t_matrix - start_t_matrix) << "s" << std::endl;
378  }
379 
380  // set the matrix
381  AztecOO_solver_pt->SetUserMatrix(Epetra_matrix_pt);
382 
383  // set the preconditioner
384  if (trilinos_prec_pt == 0)
385  {
387  }
388 
389 #ifdef PARANOID
390  // paranoid check the preconditioner exists
391  if (Preconditioner_pt == 0)
392  {
393  std::ostringstream error_message;
394  error_message << "Preconditioner_pt == 0. (Remember default "
395  << "preconditioner is IdentityPreconditioner)";
396  throw OomphLibError(
397  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
398  }
399 #endif
400 
401  // if the preconditioner is a trilinos preconditioner then setup the
402  // preconditioner
403  if (trilinos_prec_pt != 0)
404  {
405  // only setup the preconditioner if required
407  {
408  // start of prec setup
409  double prec_setup_start_t = TimingHelpers::timer();
410 
411  // setup the preconditioner
412  trilinos_prec_pt->set_matrix_pt(matrix_pt);
413  trilinos_prec_pt->setup(Epetra_matrix_pt);
414 
415 
416  // set the preconditioner
417  AztecOO_solver_pt->SetPrecOperator(
418  trilinos_prec_pt->epetra_operator_pt());
419 
420  // start of prec setup
421  double prec_setup_finish_t = TimingHelpers::timer();
422  if (Doc_time)
423  {
424  double t_prec_setup = prec_setup_finish_t - prec_setup_start_t;
425  oomph_info << "Time for preconditioner setup [sec]: " << t_prec_setup
426  << std::endl;
427  }
428  }
429 
430  // delete the oomph-matrix if required
432  {
433  dynamic_cast<CRDoubleMatrix*>(Oomph_matrix_pt)->clear();
434  delete Oomph_matrix_pt;
435  Oomph_matrix_pt = NULL;
436  }
437 
438  // delete Oomph-lib matrix if requested
439  else if (Delete_matrix)
440  {
441  dynamic_cast<CRDoubleMatrix*>(matrix_pt)->clear();
442  }
443  }
444 
445  // set solver options
446  if (Doc_time)
447  {
448  AztecOO_solver_pt->SetAztecOption(AZ_output, AZ_warnings);
449  }
450  else
451  {
452  AztecOO_solver_pt->SetAztecOption(AZ_output, AZ_none);
453  }
454  AztecOO_solver_pt->SetAztecOption(AZ_kspace, Max_iter);
455 
456  // set solver type
457  switch (Solver_type)
458  {
459  case CG:
460  AztecOO_solver_pt->SetAztecOption(AZ_solver, AZ_cg);
461  break;
462 
463  case GMRES:
464  AztecOO_solver_pt->SetAztecOption(AZ_solver, AZ_gmres);
465  break;
466 
467  case BiCGStab:
468  AztecOO_solver_pt->SetAztecOption(AZ_solver, AZ_bicgstab);
469  break;
470 
471  default:
472  std::ostringstream error_message;
473  error_message << "Solver_type set to incorrect value. "
474  << "Acceptable values are " << CG << ", " << GMRES
475  << " and " << BiCGStab << ". Current value is "
476  << Solver_type << ".";
477  throw OomphLibError(error_message.str(),
480  }
481  }
Evaluates time-resolved continuum fields and writes the data into a stat file.
Definition: CG.h:55
Preconditioner * Preconditioner_pt
Pointer to the preconditioner.
Definition: iterative_linear_solver.h:245
bool Setup_preconditioner_before_solve
Definition: iterative_linear_solver.h:258
void setup(DoubleMatrixBase *matrix_pt)
Definition: preconditioner.h:94
Epetra_CrsMatrix * create_distributed_epetra_matrix(const CRDoubleMatrix *oomph_matrix_pt, const LinearAlgebraDistribution *dist_pt)
Definition: trilinos_helpers.cc:289
Epetra_CrsMatrix * create_distributed_epetra_matrix_for_aztecoo(CRDoubleMatrix *oomph_matrix_pt)
Definition: trilinos_helpers.cc:474

References AztecOO_solver_pt, BiCGStab, oomph::DistributableLinearAlgebraObject::build_distribution(), CG, clean_up_memory(), oomph::TrilinosEpetraHelpers::create_distributed_epetra_matrix(), oomph::TrilinosEpetraHelpers::create_distributed_epetra_matrix_for_aztecoo(), Delete_matrix, oomph::DistributableLinearAlgebraObject::distribution_pt(), oomph::LinearSolver::Doc_time, Epetra_matrix_pt, oomph::TrilinosPreconditionerBase::epetra_operator_pt(), Epetra_preconditioner_pt, GMRES, oomph::IterativeLinearSolver::Max_iter, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, oomph::oomph_info, Oomph_matrix_pt, oomph::IterativeLinearSolver::Preconditioner_pt, oomph::Preconditioner::set_matrix_pt(), oomph::TrilinosPreconditionerBase::setup(), oomph::Preconditioner::setup(), oomph::IterativeLinearSolver::Setup_preconditioner_before_solve, Solver_type, oomph::TimingHelpers::timer(), Use_aztecoo_workaround_for_epetra_matrix_setup, and Using_problem_based_solve.

Referenced by solve().

◆ solver_type()

unsigned& oomph::TrilinosAztecOOSolver::solver_type ( )
inline

◆ tolerance()

double& oomph::TrilinosAztecOOSolver::tolerance ( )
inline

Member Data Documentation

◆ Assemble_serial_jacobian

bool oomph::TrilinosAztecOOSolver::Assemble_serial_jacobian
protected

If true, when performing a problem based solve a serial matrix will be requested from Problem::get_jacobian(...). Defaults to true

Referenced by disable_assemble_serial_jacobian(), enable_assemble_serial_jacobian(), and TrilinosAztecOOSolver().

◆ AztecOO_solver_pt

AztecOO* oomph::TrilinosAztecOOSolver::AztecOO_solver_pt
protected

Pointer to the AztecOO solver.

Referenced by clean_up_memory(), solve_using_AztecOO(), solver_setup(), and TrilinosAztecOOSolver().

◆ Delete_matrix

bool oomph::TrilinosAztecOOSolver::Delete_matrix
protected

Trilinos copies matrix data from oomph-lib's own CRDoubleMatrix or DistributedCRDoubleMatrix to Trilinos's Epetra format - the Trilinos solver no longer requires the oomph-lib matrices and therefore they could be deleted to save memory. This must be requested explicitly by setting this flag to TRUE. NOTE: The matrix is deleted after the preconditioner is setup.

Referenced by disable_delete_matrix(), enable_delete_matrix(), solver_setup(), and TrilinosAztecOOSolver().

◆ Epetra_matrix_pt

Epetra_CrsMatrix* oomph::TrilinosAztecOOSolver::Epetra_matrix_pt
protected

A pointer for the linear system matrix in Epetra_CrsMatrix format.

Referenced by clean_up_memory(), resolve(), solver_setup(), and TrilinosAztecOOSolver().

◆ Epetra_preconditioner_pt

Epetra_Operator* oomph::TrilinosAztecOOSolver::Epetra_preconditioner_pt
protected

A pointer to the Epetra_Operator for the preconditioner. This is only used if the preconditioner NOT a Trilinos preconditioner.

Referenced by clean_up_memory(), solver_setup(), and TrilinosAztecOOSolver().

◆ If_oomphlib_preconditioner_use_epetra_values

bool oomph::TrilinosAztecOOSolver::If_oomphlib_preconditioner_use_epetra_values
protected

if this solver is using an oomph-lib preconditioner then the vectors passed to preconditioner_solve(...) should be using the values in the epetra vector as external data. If the vectors are using external data then rebuild(...) methods cannot be used.

Referenced by TrilinosAztecOOSolver().

◆ Iterations

unsigned oomph::TrilinosAztecOOSolver::Iterations
protected

Stores number of iterations used.

Referenced by iterations(), and solve_using_AztecOO().

◆ Jacobian_setup_time

double oomph::TrilinosAztecOOSolver::Jacobian_setup_time
protected

Stores set up time for Jacobian.

Referenced by jacobian_setup_time(), and solve().

◆ Linear_solver_solution_time

double oomph::TrilinosAztecOOSolver::Linear_solver_solution_time
protected

Stores time for the solution (excludes time to set up preconditioner)

Referenced by linear_solver_solution_time(), resolve(), and solve().

◆ Oomph_matrix_pt

DoubleMatrixBase* oomph::TrilinosAztecOOSolver::Oomph_matrix_pt
protected

Oomph lib matrix pointer.

Referenced by solve(), solver_setup(), and ~TrilinosAztecOOSolver().

◆ Problem_pt

Problem* oomph::TrilinosAztecOOSolver::Problem_pt
protected

A pointer to the underlying problem (NULL if MATRIX based solve) The problem_pt is stored here in a problem based solve for the preconditioner

Referenced by solve(), and TrilinosAztecOOSolver().

◆ Solver_type

unsigned oomph::TrilinosAztecOOSolver::Solver_type
protected

Defines which solver is set up - available types are defined in AztecOO_solver_types

Referenced by solver_setup(), solver_type(), and TrilinosAztecOOSolver().

◆ Use_aztecoo_workaround_for_epetra_matrix_setup

bool oomph::TrilinosAztecOOSolver::Use_aztecoo_workaround_for_epetra_matrix_setup
protected

◆ Using_problem_based_solve

bool oomph::TrilinosAztecOOSolver::Using_problem_based_solve
protected

Helper flag keeping track of whether we called the linear algebra or problem-based solve function.

Referenced by solve(), solver_setup(), TrilinosAztecOOSolver(), and ~TrilinosAztecOOSolver().


The documentation for this class was generated from the following files: