linear_solvers/TrilinosSolver_test.cc File Reference
#include "generic.h"
#include "poisson.h"
#include "meshes/rectangular_quadmesh.h"

Classes

class  PoissonProblem< ELEMENT >
 Micky mouse Poisson problem. More...
 

Namespaces

 TanhSolnForPoisson
 Namespace for exact solution for Poisson equation with "sharp step".
 

Functions

void TanhSolnForPoisson::get_exact_u (const Vector< double > &x, Vector< double > &u)
 Exact solution as a Vector. More...
 
void TanhSolnForPoisson::source_function (const Vector< double > &x, double &source)
 Source function required to make the solution above an exact solution. More...
 
int main (int argc, char **argv)
 Driver code for 2D Poisson problem. More...
 

Function Documentation

◆ main()

int main ( int argc  ,
char **  argv 
)

Driver code for 2D Poisson problem.

312 {
313  // Note: there is a memory leak in trilinos itself as
314  // demonstrated by this bit of code!
315  if (false)
316  {
317  // set up a parameter list
318  Teuchos::ParameterList ml_parameters;
319  ML_Epetra::SetDefaults("SA", ml_parameters);
320 
321  std::cout << "hello" << std::endl;
322  exit(0);
323  }
324 
325  // Set the orientation of the "step" to 45 degrees
327 
328  // Initial value for the steepness of the "step"
330 
331  // Create the problem with 2D nine-node elements from the
332  // QPoissonElement family. Pass pointer to source function.
335 
336 
337  DocInfo doc_info;
338  doc_info.set_directory("RESLT");
339 
340  // File to report the number of Newton iterations
341  ofstream conv_file;
342  char filename[100];
343  sprintf(filename,"%s/conv.dat",doc_info.directory().c_str());
344  conv_file.open(filename);
345 
346 
347  // Trilinos solvers with the ML preconditioner
348  //--------------------------------------------
349  {
350 
351  cout << "==================================="
352  << "=================================" << endl;
353  cout << "Testing Trilinos solver preconditioned with "
354  << "TrilinosMLPreconditioner" << endl;
355  cout << "======================================="
356  << "=============================" << endl;
357 
358  // Create a Trilinos Solver
359  TrilinosAztecOOSolver* linear_solver_pt = new TrilinosAztecOOSolver;
360 
361  // Create the Trilinos ML preconditioner
362  TrilinosMLPreconditioner* preconditioner_pt = new TrilinosMLPreconditioner;
363 
364  // Set the preconditioner pointer
365  linear_solver_pt->preconditioner_pt() = preconditioner_pt;
366 
367  // Set linear solver
368  problem.linear_solver_pt() = linear_solver_pt;
369 
370  linear_solver_pt->enable_doc_time();
371 
372  // Loop over specific built-in Trilinos solvers
373  for (unsigned s=0; s<=2; s++)
374  {
375  // Select the linear solver
376  switch(s)
377  {
378  case 0:
379  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::CG;
380  cout << "Using CG as solver" << endl;
381  cout << "==================" << endl;
382  break;
383  case 1:
384  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::GMRES;
385  cout << "Using GMRES as solver" << endl;
386  cout << "=====================" << endl;
387  break;
388  case 2:
389  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::BiCGStab;
390  cout << "Using BiCGStab as solver" << endl;
391  cout << "========================" << endl;
392  break;
393  default: cout << "Error selecting solver" << endl;
394  }
395 
396  // Solve the problem
397  problem.newton_solve();
398 
399  // Doc
400  problem.doc_solution(doc_info);
401  doc_info.number()++;
402  oomph_info << "Number of Newton iterations: " << problem.newton_count()
403  << std::endl << std::endl;
404  conv_file << problem.newton_count() << std::endl;
405 
406  } // end of loop over preconditioner
407 
408  // delete the Trilinos solver
409  delete linear_solver_pt;
410  delete preconditioner_pt;
411  }
412 
413 
414 
415  // Trilinos solvers with the IFPACK preconditioner
416  //------------------------------------------------
417  {
418 
419  cout << "================================="
420  << "=======================================" << endl;
421  cout << "Testing Trilinos solver preconditioned"
422  << " with TrilinosIFPACKPreconditioner" << endl;
423  cout << "==========================================="
424  << "=============================" << endl;
425 
426  // Create a Trilinos Solver
427  TrilinosAztecOOSolver* linear_solver_pt = new TrilinosAztecOOSolver;
428 
429  // create the Trilinos IFPACK preconditioner
430  TrilinosIFPACKPreconditioner* preconditioner_pt =
432 
433  // set the preconditioner pointer
434  linear_solver_pt->preconditioner_pt() = preconditioner_pt;
435 
436  // Set linear solver
437  problem.linear_solver_pt() = linear_solver_pt;
438 
439  linear_solver_pt->enable_doc_time();
440 
441  // Loop over specific built-in Trilinos solvers
442  for (unsigned s=0; s<=2; s++)
443  {
444  // Select the linear solver
445  switch(s)
446  {
447  case 0:
448  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::CG;
449  cout << "Using CG as solver" << endl;
450  cout << "==================" << endl;
451  break;
452  case 1:
453  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::GMRES;
454  cout << "Using GMRES as solver" << endl;
455  cout << "=====================" << endl;
456  break;
457  case 2:
458  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::BiCGStab;
459  cout << "Using BiCGStab as solver" << endl;
460  cout << "========================" << endl;
461  break;
462  default: cout << "Error selecting solver" << endl;
463  }
464 
465  // Solve the problem
466  problem.newton_solve();
467 
468  // Doc
469  problem.doc_solution(doc_info);
470  doc_info.number()++;
471  oomph_info << "Number of Newton iterations: " << problem.newton_count()
472  << std::endl << std::endl;
473  conv_file << problem.newton_count() << std::endl;
474 
475  } // end of loop over preconditioner
476 
477  // delete the Trilinos solver
478  delete linear_solver_pt;
479  delete preconditioner_pt;
480  }
481 
482 
483 
484 
485 
486  // Test the oomph-lib iterative linear solvers with Trilinos preconditioners
487  //--------------------------------------------------------------------------
488 
489  cout
490  << "======================================"
491  << "=================================="
492  << endl;
493  cout
494  << "Testing oomph-lib iterative linear solvers "
495  << "with Trilinos preconditioners"
496  << endl;
497  cout
498  << "======================================="
499  << "================================="
500  << endl << endl << endl;
501 
502 
503 
504  // Pointer to linear solver
505  LinearSolver* linear_solver_pt=0;
506 
507  // Loop over two Trilonos preconditioners
508  for (unsigned i_prec=0;i_prec<2;i_prec++)
509  {
510 
511  // Pointer to preconditioner
512  Preconditioner* preconditioner_pt=0;
513 
514  if (i_prec==0)
515  {
516  // Create a IFPACK preconditioner
517  preconditioner_pt = new TrilinosIFPACKPreconditioner;
518  cout << "Using TrilinosIFPACKPreconditioner as preconditioner" << endl;
519  cout << "=====================================================" << endl;
520  }
521  else
522  {
523  // Create a ML preconditioner
524  preconditioner_pt = new TrilinosMLPreconditioner;
525  cout << "Using TrilinosMLPreconditioner as preconditioner" << endl;
526  cout << "=================================================" << endl;
527  }
528 
529 
530  // Test CG
531  //--------
532  cout << "CG iterative solver" << endl;
533  cout << "-------------------" << endl;
534  linear_solver_pt = new CG<CRDoubleMatrix>;
535  static_cast<IterativeLinearSolver*>(linear_solver_pt)->tolerance()=1.0e-10;
536  problem.linear_solver_pt() = linear_solver_pt;
537 
538  // set the preconditioner in the iterative solver
539  static_cast<IterativeLinearSolver*>(linear_solver_pt)->
540  preconditioner_pt()= preconditioner_pt;
541 
542  // Solve the problem
543  problem.newton_solve();
544 
545 
546  // Doc
547  problem.doc_solution(doc_info);
548  doc_info.number()++;
549  oomph_info << "Number of Newton iterations: " << problem.newton_count()
550  << std::endl << std::endl;
551  conv_file << problem.newton_count() << std::endl;
552 
553  delete linear_solver_pt;
554 
555 
556  // Test BiCGStab
557  //--------------
558  cout << "BiCGStab iterative solver" << endl;
559  cout << "-------------------------" << endl;
560  linear_solver_pt = new BiCGStab<CRDoubleMatrix>;
561  static_cast<IterativeLinearSolver*>(linear_solver_pt)->tolerance()=1.0e-10;
562  problem.linear_solver_pt() = linear_solver_pt;
563 
564  // set the preconditioner in the iterative solver
565  static_cast<IterativeLinearSolver*>(linear_solver_pt)->preconditioner_pt()
566  = preconditioner_pt;
567 
568  // Solve the problem
569  problem.newton_solve();
570 
571  // Doc
572  problem.doc_solution(doc_info);
573  doc_info.number()++;
574  oomph_info << "Number of Newton iterations: " << problem.newton_count()
575  << std::endl << std::endl;
576  conv_file << problem.newton_count() << std::endl;
577 
578  delete linear_solver_pt;
579 
580 
581  // Test GMRES
582  //-----------
583  cout << "GMRES iterative solver" << endl;
584  cout << "----------------------" << endl;
585  linear_solver_pt = new GMRES<CRDoubleMatrix>;
586  static_cast<IterativeLinearSolver*>(linear_solver_pt)->tolerance()=1.0e-10;
587  problem.linear_solver_pt() = linear_solver_pt;
588 
589  // set the preconditioner in the iterative solver
590  static_cast<IterativeLinearSolver*>(linear_solver_pt)->preconditioner_pt()
591  = preconditioner_pt;
592 
593  // Solve the problem
594  problem.newton_solve();
595 
596  // Doc
597  problem.doc_solution(doc_info);
598  doc_info.number()++;
599  oomph_info << "Number of Newton iterations: " << problem.newton_count()
600  << std::endl << std::endl;
601  conv_file << problem.newton_count() << std::endl;
602 
603  delete linear_solver_pt;
604 
605  // Kill preconditioner
606  delete preconditioner_pt;
607  preconditioner_pt=0;
608 
609  } // end of loop over preconditioners
610 
611 
612  // Trilinos solvers with the OOMPH-lib
613  //------------------------------------------------
614  {
615 
616  cout << "=================================="
617  << "=======================================" << endl;
618  cout << "Testing Trilinos solver preconditioned with"
619  << " MatrixBasedDiagPreconditioner" << endl;
620  cout << "======================================="
621  << "==================================" << endl;
622 
623  // Create a Trilinos Solver
624  TrilinosAztecOOSolver* linear_solver_pt = new TrilinosAztecOOSolver;
625 
626  // create the oomphlib diagonal preconditioner
627  Preconditioner* preconditioner_pt =
629 
630  // set the preconditioner pointer
631  linear_solver_pt->preconditioner_pt() = preconditioner_pt;
632 
633  // Set linear solver
634  problem.linear_solver_pt() = linear_solver_pt;
635 
636  linear_solver_pt->enable_doc_time();
637 
638  // Loop over specific built-in Trilinos solvers
639  for (unsigned s=0; s<=2; s++)
640  {
641  // Select the linear solver
642  switch(s)
643  {
644  case 0:
645  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::CG;
646  cout << "Using CG as solver" << endl;
647  cout << "==================" << endl;
648  break;
649  case 1:
650  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::GMRES;
651  cout << "Using GMRES as solver" << endl;
652  cout << "=====================" << endl;
653  break;
654  case 2:
655  linear_solver_pt->solver_type() = TrilinosAztecOOSolver::BiCGStab;
656  cout << "Using BiCGStab as solver" << endl;
657  cout << "========================" << endl;
658  break;
659  default: cout << "Error selecting solver" << endl;
660  }
661 
662  // Solve the problem
663  problem.newton_solve();
664 
665  // Doc
666  problem.doc_solution(doc_info);
667  doc_info.number()++;
668  oomph_info << "Number of Newton iterations: " << problem.newton_count()
669  << std::endl << std::endl;
670  conv_file << problem.newton_count() << std::endl;
671 
672  } // end of loop over preconditioner
673 
674  // delete the Trilinos solver
675  delete linear_solver_pt;
676 
677  // Delete oomph-lib preconditioner
678  delete preconditioner_pt;
679  }
680 
681  conv_file.close();
682 
683  // This code is not used, just compiled -- don't delete as it's
684  // extracted by doxygen for a tutorial
685  bool never_get_here=true;
686  if (!never_get_here)
687  {
688  // Create oomph-lib linear solver
689  IterativeLinearSolver* linear_solver_pt=new GMRES<CRDoubleMatrix>;
690 
691  // Create Trilinos IFPACK preconditioner as oomph-lib Preconditioner
692  Preconditioner* preconditioner_pt=new TrilinosIFPACKPreconditioner;
693 
694  // Pass pointer to preconditioner to oomph-lib IterativeLinearSolver
695  linear_solver_pt->preconditioner_pt()=preconditioner_pt;
696  }
697 
698 } //end of main
Micky mouse Poisson problem.
Definition: HypreSolver_test.cc:81
The conjugate gradient method.
Definition: iterative_linear_solver.h:410
The conjugate gradient method.
Definition: iterative_linear_solver.h:284
Definition: oomph_utilities.h:499
std::string directory() const
Output directory.
Definition: oomph_utilities.h:524
void set_directory(const std::string &directory)
Definition: oomph_utilities.cc:298
unsigned & number()
Number used (e.g.) for labeling output files.
Definition: oomph_utilities.h:554
The GMRES method.
Definition: iterative_linear_solver.h:1227
Definition: iterative_linear_solver.h:54
Preconditioner *& preconditioner_pt()
Access function to preconditioner.
Definition: iterative_linear_solver.h:95
Definition: linear_solver.h:68
void enable_doc_time()
Enable documentation of solve times.
Definition: linear_solver.h:110
Matrix-based diagonal preconditioner.
Definition: general_purpose_preconditioners.h:49
Definition: preconditioner.h:54
Definition: trilinos_solver.h:267
unsigned & solver_type()
Access function to Solver_type.
Definition: trilinos_solver.h:442
Definition: trilinos_preconditioners.h:268
Definition: trilinos_preconditioners.h:152
RealScalar s
Definition: level1_cplx_impl.h:130
string filename
Definition: MergeRestartFiles.py:39
double TanPhi
Parameter for angle Phi of "step".
Definition: HypreSolver_test.cc:51
void source_function(const Vector< double > &x, double &source)
Source function required to make the solution above an exact solution.
Definition: HypreSolver_test.cc:60
double Alpha
Parameter for steepness of step.
Definition: extrude_with_macro_element_representation.cc:185
OomphInfo oomph_info
Definition: oomph_definitions.cc:319
Constructor for SteadyAxisymAdvectionDiffusion problem
Definition: steady_axisym_advection_diffusion.cc:213

References TanhSolnForPoisson::Alpha, oomph::DocInfo::directory(), oomph::LinearSolver::enable_doc_time(), MergeRestartFiles::filename, oomph::DocInfo::number(), oomph::oomph_info, oomph::IterativeLinearSolver::preconditioner_pt(), problem, s, oomph::DocInfo::set_directory(), oomph::TrilinosAztecOOSolver::solver_type(), TanhSolnForPoisson::source_function(), and TanhSolnForPoisson::TanPhi.