adv_diff_iterative_linear_solver_tester.cc File Reference

Classes

class  TwoMeshFluxAdvectionDiffusionProblem< ELEMENT >
 

Namespaces

 TanhSolnForAdvectionDiffusion
 

Functions

void TanhSolnForAdvectionDiffusion::get_exact_u (const Vector< double > &x, Vector< double > &u)
 Exact solution as a Vector. More...
 
void TanhSolnForAdvectionDiffusion::get_exact_u (const Vector< double > &x, double &u)
 Exact solution as a scalar. More...
 
void TanhSolnForAdvectionDiffusion::source_function (const Vector< double > &x_vect, double &source)
 Source function required to make the solution above an exact solution. More...
 
void TanhSolnForAdvectionDiffusion::prescribed_flux_on_fixed_x_boundary (const Vector< double > &x, double &flux)
 Flux required by the exact solution on a boundary on which x is fixed. More...
 
void TanhSolnForAdvectionDiffusion::wind_function (const Vector< double > &x, Vector< double > &wind)
 Wind. More...
 
void run_it (LinearSolver *linear_solver_pt)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)

Check out various iterative linear solvers on an advection diffusion problem. Command line arg specifies the solver.

Possible endless loop for memory leak testing

635 {
636  // Store command line arguments
637  CommandLineArgs::setup(argc,argv);
638 
639  //Check
640  if (CommandLineArgs::Argc!=2)
641  {
642  throw OomphLibError(
643  "Code needs one argument to specify which solver to use",
646  }
647 
648  // Default value for Peclet number -- can be reset to zero for
649  // solvers that require symmetric matrices
651 
652  // Tolerance for iterative linear solvers
653  double tol=1.0e-12;
654 
655  // Pointer to linear solver
656  LinearSolver* linear_solver_pt=new SuperLUSolver;
657 
658  // Pointer to iterative linear solver
659  IterativeLinearSolver* it_linear_solver_pt=0;
660 
661  // Preconditioner
663 
664 
666  bool doit=true;
667  while (doit)
668  {
669 
670  // Switch based on one and only command line argument
671  int solver_flag=atoi(CommandLineArgs::Argv[1]);
672  switch (solver_flag)
673  {
674 
675 
676  // SuperLU
677  //--------
678  case 0:
679 
680  // This solver can handle non-symmetric matrices
682 
683  std::cout << "Solving with SuperLU direct solver"
684  << std::endl;
685  break;
686 
687 
688  // GMRES
689  //------
690  case 1:
691 
692  // This solver can handle non-symmetric matrices
694 
695  // Build solver
696  it_linear_solver_pt = new GMRES<CRDoubleMatrix>;
697 
698  // Build/pass preconditioner
699  it_linear_solver_pt->preconditioner_pt() = prec_pt;
700 
701  // Switch on doc of convergence history and adjust tolerance
702  it_linear_solver_pt->tolerance()=tol;
703  it_linear_solver_pt->
704  open_convergence_history_file_stream("RESLT/convergence.dat");
705 
706  // Set solver flag
707  linear_solver_pt=it_linear_solver_pt;
708 
709  // doc
710  std::cout << "Solving with GMRES" << std::endl;
711 
712  break;
713 
714 
715 
716 
717  // BiCGStab
718  //---------
719  case 2:
720 
721  // This solver can handle non-symmetric matrices
723 
724  // Build solver
725  it_linear_solver_pt = new BiCGStab<CRDoubleMatrix>;
726 
727  // Build/pass preconditioner
728  it_linear_solver_pt->preconditioner_pt() = prec_pt;
729 
730  // Switch on doc of convergence history and adjust tolerance
731  it_linear_solver_pt->tolerance()=tol;
732  it_linear_solver_pt->
733  open_convergence_history_file_stream("RESLT/convergence.dat");
734 
735  // Set solver flag
736  linear_solver_pt=it_linear_solver_pt;
737 
738  // doc
739  std::cout << "Solving with BiCGStab" << std::endl;
740 
741  break;
742 
743 
744 
745  // CG
746  //----
747  case 3:
748 
749  // This solver can't handle non-symmetric matrices
751 
752  // Build solver
753  it_linear_solver_pt = new CG<CRDoubleMatrix>;
754 
755  // Build/pass preconditioner
756  it_linear_solver_pt->preconditioner_pt() = prec_pt;
757 
758  // Switch on doc of convergence history and adjust tolerance
759  it_linear_solver_pt->tolerance()=tol;
760  it_linear_solver_pt->
761  open_convergence_history_file_stream("RESLT/convergence.dat");
762 
763  // Set solver flag
764  linear_solver_pt=it_linear_solver_pt;
765 
766  // doc
767  std::cout << "Solving with CG" << std::endl;
768 
769  break;
770 
771 
772  // GS
773  //----
774  case 4:
775 
776  // This solver can't handle non-symmetric matrices
778 
779  // Build solver
780  it_linear_solver_pt = new GS<CRDoubleMatrix>;
781 
782  // Note: Doesn't have a preconditioner
783 
784  // Switch on doc of convergence history and adjust tolerance
785  it_linear_solver_pt->tolerance()=tol;
786  it_linear_solver_pt->
787  open_convergence_history_file_stream("RESLT/convergence.dat");
788 
789  // Set solver flag
790  linear_solver_pt=it_linear_solver_pt;
791 
792  // doc
793  std::cout << "Solving with GS" << std::endl;
794 
795  break;
796 
797  // Damped Jacobi
798  //--------------
799  case 5:
800 
801  // This solver can't handle non-symmetric matrices
803 
804  // Build solver
805  it_linear_solver_pt = new DampedJacobi<CRDoubleMatrix>;
806 
807  // Note: Doesn't have a preconditioner
808 
809  // Switch on doc of convergence history and adjust tolerance
810  it_linear_solver_pt->tolerance()=tol;
811  it_linear_solver_pt->
812  open_convergence_history_file_stream("RESLT/convergence.dat");
813 
814  // Set solver flag
815  linear_solver_pt=it_linear_solver_pt;
816 
817  // doc
818  std::cout << "Solving with damped Jacobi" << std::endl;
819 
820  break;
821 
822 
823  // Wrong
824  //------
825  default:
826 
827  throw OomphLibError("Invalid command line argument: solver flag: ",
830  break;
831 
832  }
833 
834  // Build problem and solve it with specified linear solver
835  run_it(linear_solver_pt);
836 
837  delete it_linear_solver_pt;
838 
839  // Comment this out if you want an endless loop to test
840  // for memory leaks.
841  doit=false;
842 
843  }
844 
845 } //end of main
void run_it(LinearSolver *linear_solver_pt)
Definition: adv_diff_iterative_linear_solver_tester.cc:502
The conjugate gradient method.
Definition: iterative_linear_solver.h:410
The conjugate gradient method.
Definition: iterative_linear_solver.h:284
Definition: iterative_linear_solver.h:1002
The GMRES method.
Definition: iterative_linear_solver.h:1227
Definition: iterative_linear_solver.h:785
ILU(0) Preconditioner for matrices of CRDoubleMatrix Format.
Definition: general_purpose_preconditioners.h:313
Definition: iterative_linear_solver.h:54
Preconditioner *& preconditioner_pt()
Access function to preconditioner.
Definition: iterative_linear_solver.h:95
double & tolerance()
Access to convergence tolerance.
Definition: iterative_linear_solver.h:107
Definition: linear_solver.h:68
Definition: oomph_definitions.h:222
Definition: preconditioner.h:54
Definition: linear_solver.h:486
void setup(Time *time_pt)
Create all GeomObjects needed to define the cylinder and the flag.
Definition: turek_flag_non_fsi.cc:277
double Peclet
Peclet number.
Definition: two_d_adv_diff_adapt.cc:50
char ** Argv
Arguments themselves.
Definition: oomph_utilities.cc:410
int Argc
Number of arguments + 1.
Definition: oomph_utilities.cc:407
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References oomph::CommandLineArgs::Argc, oomph::CommandLineArgs::Argv, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, TanhSolnForAdvectionDiffusion::Peclet, oomph::IterativeLinearSolver::preconditioner_pt(), run_it(), Flag_definition::setup(), and oomph::IterativeLinearSolver::tolerance().

◆ run_it()

void run_it ( LinearSolver linear_solver_pt)

Run 2D AdvectionDiffusion problem with flux boundary conditions, using the iterative linear solver and preconditioner specified via the pointers.

503 {
504 
505  //Set up the problem
506  //------------------
507 
508  //Set up the problem with 2D nine-node elements from the
509  //RefineableQuadAdvectionDiffusionElement family.
510  //Pass pointer to source function.
514 
515  // Set linear solver
516  problem.linear_solver_pt()=linear_solver_pt;
517 
518  // Change max. number of iterations (some of the solvers used here
519  // are rubbish!
520  IterativeLinearSolver* iter_solver_pt=
521  dynamic_cast<IterativeLinearSolver*>(linear_solver_pt);
522  if (iter_solver_pt!=0)
523  {
524  iter_solver_pt->max_iter()=1000;
525  }
526 
527  // Get Jacobian matrix and create dummy rhs for re-solve
528  // (solution from re-solve should be a vector of ones)
529  unsigned n_dof=problem.ndof();
531  DoubleVector residual;
532  DoubleVector other_rhs;
533  DoubleVector one;
535  problem.get_jacobian(residual,matrix);
536  one.build(residual.distribution_pt(),0.0);
537  other_rhs.build(residual.distribution_pt(),0.0);
538  one.initialise(1.0);
539  matrix.multiply(one,other_rhs);
540  linear_solver_pt->enable_resolve();
541 
542 
543  // Normal solve
544  //-------------
545 
546  // Create label for output
547  DocInfo doc_info;
548 
549  // Set output directory
550  doc_info.set_directory("RESLT");
551 
552  // Solve the problem
553  problem.newton_solve();
554 
555  //Output solution
556  problem.doc_solution(doc_info);
557 
558 
559  // Re-solve
560  //---------
561 
562  // Open convergence history for resolve
563  IterativeLinearSolver* it_solver_pt=
564  dynamic_cast<IterativeLinearSolver*>(linear_solver_pt);
565  if (it_solver_pt!=0)
566  {
567  it_solver_pt->
568  open_convergence_history_file_stream("RESLT/resolve_convergence.dat");
569  }
570 
571  // Resolve
572  linear_solver_pt->resolve(other_rhs,solution);
573 
574  // Get error
575  double error=0.0;
576  for (unsigned i=0;i<n_dof;i++)
577  {
578  error+=pow((solution[i]-1.0),2);
579  }
580 
581  // Doc
582  ofstream error_file("RESLT/resolve_error.dat");
583  error_file << "Error for resolve: "
584  << sqrt(error/double(n_dof))
585  << std::endl;
586  std::cout << "Error for resolve: "
587  << sqrt(error/double(n_dof))
588  << std::endl;
589  error_file.close();
590 
591  // Switch of ability to resolve
592  linear_solver_pt->disable_resolve();
593 
594 
595 
596  // Linear-algebra solve
597  //---------------------
598 
599  if (it_solver_pt!=0)
600  {
601  it_solver_pt->
602  open_convergence_history_file_stream("RESLT/la_solve_convergence.dat");
603  }
604 
605  DoubleVector solution2;
606  linear_solver_pt->solve(&matrix,other_rhs,solution2);
607 
608  // Get error
609  error=0.0;
610  for (unsigned i=0;i<n_dof;i++)
611  {
612  error+=pow((solution2[i]-1.0),2);
613  }
614 
615  // Doc
616  ofstream error_file2("RESLT/la_solve_error.dat");
617  error_file2 << "Error for LA solve: "
618  << sqrt(error/double(n_dof))
619  << std::endl;
620  std::cout << "Error for LA solve: "
621  << sqrt(error/double(n_dof))
622  << std::endl;
623  error_file2.close();
624 
625 } //end of run_it
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137
Definition: two_d_adv_diff_flux_bc.cc:120
Definition: matrices.h:888
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
Definition: linear_algebra_distribution.h:457
Definition: oomph_utilities.h:499
void set_directory(const std::string &directory)
Definition: oomph_utilities.cc:298
Definition: double_vector.h:58
void initialise(const double &v)
initialise the whole vector with value v
Definition: double_vector.cc:123
void build(const DoubleVector &old_vector)
Just copys the argument DoubleVector.
Definition: double_vector.cc:35
unsigned & max_iter()
Access to max. number of iterations.
Definition: iterative_linear_solver.h:113
virtual void solve(Problem *const &problem_pt, DoubleVector &result)=0
virtual void enable_resolve()
Definition: linear_solver.h:135
virtual void resolve(const DoubleVector &rhs, DoubleVector &result)
Definition: linear_solver.h:225
virtual void disable_resolve()
Definition: linear_solver.h:144
Definition: refineable_advection_diffusion_elements.h:355
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
void solution(const Vector< double > &x, Vector< double > &u)
Definition: two_d_biharmonic.cc:113
void source_function(const Vector< double > &x_vect, double &source)
Source function required to make the solution above an exact solution.
Definition: two_d_adv_diff_adapt.cc:71
void wind_function(const Vector< double > &x, Vector< double > &wind)
Wind.
Definition: two_d_adv_diff_adapt.cc:84
int error
Definition: calibrate.py:297
Constructor for SteadyAxisymAdvectionDiffusion problem
Definition: steady_axisym_advection_diffusion.cc:213

References oomph::DoubleVector::build(), oomph::LinearSolver::disable_resolve(), oomph::DistributableLinearAlgebraObject::distribution_pt(), oomph::LinearSolver::enable_resolve(), calibrate::error, i, oomph::DoubleVector::initialise(), matrix(), oomph::IterativeLinearSolver::max_iter(), Eigen::ArrayBase< Derived >::pow(), problem, oomph::LinearSolver::resolve(), oomph::DocInfo::set_directory(), BiharmonicTestFunctions1::solution(), oomph::LinearSolver::solve(), TanhSolnForAdvectionDiffusion::source_function(), sqrt(), and TanhSolnForAdvectionDiffusion::wind_function().

Referenced by main().