33 #ifndef OOMPH_ITERATIVE_LINEAR_SOLVER_HEADER 34 #define OOMPH_ITERATIVE_LINEAR_SOLVER_HEADER 39 #include <oomph-lib-config.h> 269 template<
typename MATRIX>
276 CG() : Iterations(0), Matrix_pt(0), Resolving(false),
277 Matrix_can_be_deleted(true)
321 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
325 Matrix_can_be_deleted=
false;
329 if (dynamic_cast<DistributableLinearAlgebraObject*>(matrix_pt))
342 this->solve_helper(matrix_pt,rhs,solution);
368 if ((Matrix_pt!=0)&&(Matrix_can_be_deleted))
399 template<
typename MATRIX>
406 BiCGStab() : Iterations(0), Matrix_pt(0), Resolving(false),
407 Matrix_can_be_deleted(true)
452 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
456 Matrix_can_be_deleted=
false;
460 if (dynamic_cast<DistributableLinearAlgebraObject*>(matrix_pt))
473 this->solve_helper(matrix_pt,rhs,solution);
511 if ((Matrix_pt!=0)&&(Matrix_can_be_deleted))
572 template<
typename MATRIX>
573 void check_validity_of_solve_helper_inputs(MATRIX*
const &matrix_pt,
576 const double& n_dof);
597 template<
typename MATRIX>
604 GS() : Matrix_pt(0), Iterations(0), Resolving(false),
605 Matrix_can_be_deleted(true)
641 Matrix_can_be_deleted=
false;
644 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
654 Use_as_smoother=
true;
657 solve_helper(Matrix_pt,rhs,result);
673 Use_as_smoother=
false;
682 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
686 Matrix_can_be_deleted=
false;
689 this->solve_helper(matrix_pt,rhs,solution);
714 throw OomphLibError(
"No matrix was stored -- cannot re-solve",
715 OOMPH_CURRENT_FUNCTION,
716 OOMPH_EXCEPTION_LOCATION);
721 solve(Matrix_pt,rhs,result);
731 "Gauss Seidel is not a preconditionable iterative solver",
732 OOMPH_CURRENT_FUNCTION,
733 OOMPH_EXCEPTION_LOCATION);
755 if ((Matrix_pt!=0)&&(Matrix_can_be_deleted))
795 GS() : Matrix_pt(0), Iterations(0), Resolving(false),
796 Matrix_can_be_deleted(true)
824 Use_as_smoother=
true;
827 solve_helper(Matrix_pt,rhs,result);
845 Matrix_can_be_deleted=
false;
848 setup_helper(matrix_pt);
868 Use_as_smoother=
false;
891 (matrix_pt)->get_index_of_diagonal_entries();
895 Matrix_can_be_deleted=
false;
898 solve_helper(matrix_pt,rhs,solution);
922 if (this->Matrix_pt==0)
924 throw OomphLibError(
"No matrix was stored -- cannot re-solve",
925 OOMPH_CURRENT_FUNCTION,
926 OOMPH_EXCEPTION_LOCATION);
931 solve(Matrix_pt,rhs,result);
941 std::string error_output_string=
"Gauss Seidel is not a ";
942 error_output_string+=
"preconditionable iterative solver";
946 OOMPH_CURRENT_FUNCTION,
947 OOMPH_EXCEPTION_LOCATION);
973 if ((Matrix_pt!=0)&&(Matrix_can_be_deleted))
1011 template<
typename MATRIX>
1048 if ((Matrix_pt!=0) && (Matrix_can_be_deleted))
1063 Matrix_can_be_deleted=
false;
1066 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
1069 extract_diagonal_entries(matrix_pt);
1076 if (dynamic_cast<CRDoubleMatrix*>(matrix_pt))
1083 (Matrix_pt)->diagonal_entries();
1087 else if (dynamic_cast<CCDoubleMatrix*>(matrix_pt))
1090 std::ostringstream error_message_stream;
1093 error_message_stream <<
"Damped Jacobi can only cater to real-valued " 1094 <<
"matrices. If you require a complex-valued " 1095 <<
"version, please write this yourself. " 1096 <<
"It is likely that the only difference will be " 1097 <<
"the use of complex vectors.";
1101 OOMPH_CURRENT_FUNCTION,
1102 OOMPH_EXCEPTION_LOCATION);
1108 unsigned n_row=Matrix_pt->nrow();
1111 for (
unsigned i=0;
i<n_row;
i++)
1114 Matrix_diagonal[
i]=(*Matrix_pt)(
i,
i);
1119 unsigned n_dof=Matrix_diagonal.size();
1122 for (
unsigned i=0;
i<n_dof;
i++)
1124 Matrix_diagonal[
i]=1.0/Matrix_diagonal[
i];
1135 Use_as_smoother=
true;
1138 solve_helper(Matrix_pt,rhs,solution);
1154 Matrix_can_be_deleted=
false;
1157 Use_as_smoother=
false;
1166 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
1170 extract_diagonal_entries(matrix_pt);
1173 solve_helper(matrix_pt,rhs,solution);
1187 throw OomphLibError(
"No matrix was stored -- cannot re-solve",
1188 OOMPH_CURRENT_FUNCTION,
1189 OOMPH_EXCEPTION_LOCATION);
1194 solve(Matrix_pt,rhs,result);
1245 template<
typename MATRIX>
1255 Matrix_can_be_deleted(true)
1257 Preconditioner_LHS=
true;
1258 Iteration_restart=
false;
1303 Matrix_pt=
dynamic_cast<MATRIX*
>(matrix_pt);
1307 Matrix_can_be_deleted=
false;
1311 this->solve_helper(matrix_pt,rhs,solution);
1339 return Iteration_restart;
1348 Iteration_restart =
true;
1354 Iteration_restart =
false;
1373 if ((Matrix_pt!=0)&&(Matrix_can_be_deleted))
1389 for (
int i=
int(k);
i>=0;
i--)
1395 for (
int j=
i-1;j>=0;j--)
1398 y[j] -= H[
i][j]*y[
i];
1403 unsigned n_x=x.
nrow();
1415 for (
unsigned j=0;j<=k;j++)
1418 const double* vj_pt=v[j].values_pt();
1421 for (
unsigned i=0;
i<n_x;
i++)
1423 temp_pt[
i]+=vj_pt[
i]*y[j];
1428 if(Preconditioner_LHS)
1490 else if(fabs(dy)>fabs(dx))
1496 sn=1.0/sqrt(1.0+temp*temp);
1509 cs=1.0/sqrt(1.0+temp*temp);
1536 double temp=cs*dx+sn*dy;
bool First_time_solve_when_used_as_preconditioner
bool Throw_error_after_max_iter
Should we throw an error instead of just returning when we hit the max iterations?
void clean_up_memory()
Cleanup data that's stored for resolve (if any has been stored)
virtual void solve(Problem *const &problem_pt, DoubleVector &result)=0
Solver: Takes pointer to problem and returns the results vector which contains the solution of the li...
GS(const GS &)
Broken copy constructor.
void clean_up_memory()
Cleanup data that's stored for resolve (if any has been stored)
void smoother_solve(const DoubleVector &rhs, DoubleVector &result)
The smoother_solve function performs fixed number of iterations on the system A*result=rhs. The number of (smoothing) iterations is the same as the max. number of iterations in the underlying IterativeLinearSolver class.
void broken_copy(const std::string &class_name)
Issue error message and terminate execution.
void disable_resolve()
Overload disable resolve so that it cleans up memory too.
void disable_resolve()
Overload disable resolve so that it cleans up memory too.
The Identity Preconditioner.
static IdentityPreconditioner Default_preconditioner
Default preconditioner: The base class for preconditioners is a fully functional (if trivial!) precon...
void resolve(const DoubleVector &rhs, DoubleVector &result)
Re-solve the system defined by the last assembled Jacobian and the rhs vector specified here...
virtual unsigned iterations() const =0
Number of iterations taken.
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
bool Iteration_restart
boolean indicating if iteration restarting is used
Preconditioner *& preconditioner_pt()
Access function to preconditioner.
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
unsigned Iterations
Number of iterations taken.
unsigned iterations() const
Number of iterations taken.
Vector< int > Index_of_diagonal_entries
Vector whose i'th entry contains the index of the last entry below or on the diagonal of the i'th row...
bool iteration_restart() const
access function indicating whether restarted GMRES is used
virtual void preconditioner_solve(const DoubleVector &r, DoubleVector &z)=0
Apply the preconditioner. Pure virtual generic interface function. This method should apply the preco...
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...
double & tolerance()
Access to convergence tolerance.
virtual ~BiCGStab()
Destructor (cleanup storage)
void operator=(const CG &)
Broken assignment operator.
void smoother_setup(DoubleMatrixBase *matrix_pt)
Setup: Pass pointer to the matrix and store in cast form.
void update(const unsigned &k, const Vector< Vector< double > > &H, const Vector< double > &s, const Vector< DoubleVector > &v, DoubleVector &x)
Helper function to update the result vector using the result, x=x_0+V_m*y.
void enable_error_after_max_iter()
Throw an error if we don't converge within max_iter.
double Tolerance
Convergence tolerance.
unsigned Iterations
Number of iterations taken.
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
double * values_pt()
access function to the underlying values
void open_convergence_history_file_stream(const std::string &file_name, const std::string &zone_title="")
Write convergence history into file with specified filename (automatically switches on doc)...
void clean_up_memory()
Clean up data that's stored for resolve (if any has been stored)
void generate_plane_rotation(double &dx, double &dy, double &cs, double &sn)
Helper function: Generate a plane rotation. This is done by finding the values of (i...
void apply_plane_rotation(double &dx, double &dy, double &cs, double &sn)
Helper function: Apply plane rotation. This is done using the update: .
virtual ~GS()
Destructor (cleanup storage)
void disable_resolve()
Overload disable resolve so that it cleans up memory too.
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
void resolve(const DoubleVector &rhs, DoubleVector &result)
Re-solve the system defined by the last assembled Jacobian and the rhs vector specified here...
unsigned Max_iter
Maximum number of iterations.
bool Enable_resolve
Boolean that indicates whether the matrix (or its factors, in the case of direct solver) should be st...
unsigned iterations() const
Number of iterations taken.
Preconditioner * Preconditioner_pt
Pointer to the preconditioner.
virtual void resolve(const DoubleVector &rhs, DoubleVector &result)
Resolve the system defined by the last assembled jacobian and the rhs vector. Solution is returned in...
~DampedJacobi()
Empty destructor.
virtual ~IterativeLinearSolver()
Destructor (empty)
unsigned iterations() const
Number of iterations taken.
MATRIX * Matrix_pt
Pointer to the matrix.
void operator=(const GS &)
Broken assignment operator.
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
void solve(DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
IterativeLinearSolver()
Constructor: Set (default) trivial preconditioner and set defaults for tolerance and max...
std::ofstream Output_file_stream
Output file stream for convergence history.
void disable_iteration_restart()
switches off iteration restart
MATRIX * Matrix_pt
Pointer to matrix.
unsigned iterations() const
Number of iterations taken.
void enable_setup_preconditioner_before_solve()
Setup the preconditioner before the solve.
The conjugate gradient method.
void disable_doc_convergence_history()
Disable documentation of the convergence history.
void operator=(const BiCGStab &)
Broken assignment operator.
bool Use_as_smoother
When a derived class object is being used as a smoother in the MG solver (or elsewhere) the residual ...
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
void enable_doc_convergence_history()
Enable documentation of the convergence history.
unsigned iterations() const
Number of iterations taken.
void disable_resolve()
Overload disable resolve so that it cleans up memory too.
unsigned Restart
The number of iterations before the iteration proceedure is restarted if iteration restart is used...
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
void smoother_solve(const DoubleVector &rhs, DoubleVector &solution)
The smoother_solve function performs fixed number of iterations on the system A*result=rhs. The number of (smoothing) iterations is the same as the max. number of iterations in the underlying IterativeLinearSolver class.
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
GS(const GS &)
Broken copy constructor.
void extract_diagonal_entries(DoubleMatrixBase *matrix_pt)
Function to extract the diagonal entries from the matrix.
double Jacobian_setup_time
Jacobian setup time.
bool Preconditioner_LHS
boolean indicating use of left hand preconditioning (if true) or right hand preconditioning (if false...
void disable_iterative_solver_as_preconditioner()
void clean_up_memory()
Cleanup data that's stored for resolve (if any has been stored)
void operator=(const DampedJacobi &)
Broken assignment operator.
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
Smoother()
Empty constructor.
MATRIX * Matrix_pt
Pointer to matrix.
unsigned Iterations
Number of iterations taken.
void solve(DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
void disable_error_after_max_iter()
Don't throw an error if we don't converge within max_iter (default).
MATRIX * Matrix_pt
System matrix pointer in the format specified by the template argument.
double Omega
Damping factor.
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...
void disable_resolve()
Overload disable resolve so that it cleans up memory too.
void enable_iteration_restart(const unsigned &restart)
switches on iteration restarting and takes as an argument the number of iterations after which the co...
unsigned Iterations
Number of iterations taken.
CG(const CG &)
Broken copy constructor.
double Solution_time
linear solver solution time
virtual ~GMRES()
Destructor (cleanup storage)
IterativeLinearSolver(const IterativeLinearSolver &)
Broken copy constructor.
virtual void disable_resolve()
Disable resolve (i.e. store matrix and/or LU decomposition, say) This function simply resets an inter...
void clean_up_memory()
Cleanup data that's stored for resolve (if any has been stored)
Preconditioner base class. Gives an interface to call all other preconditioners through and stores th...
DampedJacobi(const double &omega=2.0/3.0)
Empty constructor.
virtual void clean_up_memory()
Empty virtual function that can be overloaded in specific linear solvers to clean up any memory that ...
DampedJacobi(const DampedJacobi &)
Broken copy constructor.
double preconditioner_setup_time() const
Returns the time taken to set up the preconditioner.
void operator=(const IterativeLinearSolver &)
Broken assignment operator.
The conjugate gradient method.
unsigned iterations() const
Number of iterations taken.
void solve(DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
void disable_setup_preconditioner_before_solve()
Don't set up the preconditioner before the solve.
void broken_assign(const std::string &class_name)
Issue error message and terminate execution.
Preconditioner *const & preconditioner_pt() const
Access function to preconditioner (const version)
unsigned nrow() const
access function to the number of global rows.
void set_preconditioner_LHS()
Set left preconditioning (the default)
virtual ~Smoother()
Virtual empty destructor.
bool Doc_convergence_history
Flag indicating if the convergence history is to be documented.
unsigned Iterations
Number of iterations taken.
void close_convergence_history_file_stream()
Close convergence history output stream.
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...
void smoother_setup(DoubleMatrixBase *matrix_pt)
Set up the smoother for the matrix specified by the pointer.
std::string string(const unsigned &i)
Return the i-th string or "" if the relevant string hasn't been defined.
unsigned & max_iter()
Access to max. number of iterations.
bool Use_iterative_solver_as_preconditioner
Use the iterative solver as preconditioner.
void build_distribution(const LinearAlgebraDistribution *const dist_pt)
setup the distribution of this distributable linear algebra object
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...
void operator=(const GS &)
Broken assignment operator.
void clean_up_memory()
Cleanup data that's stored for resolve (if any has been stored)
void enable_iterative_solver_as_preconditioner()
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...
bool Matrix_can_be_deleted
Boolean flag to indicate if the matrix pointed to be Matrix_pt can be deleted.
void solve(DoubleMatrixBase *const &matrix_pt, const Vector< double > &rhs, Vector< double > &result)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
Vector< double > Matrix_diagonal
Vector containing the diagonal entries of A.
void smoother_solve(const DoubleVector &rhs, DoubleVector &result)
The smoother_solve function performs fixed number of iterations on the system A*result=rhs. The number of (smoothing) iterations is the same as the max. number of iterations in the underlying IterativeLinearSolver class.
A vector in the mathematical sense, initially developed for linear algebra type applications. If MPI then this vector can be distributed - its distribution is described by the LinearAlgebraDistribution object at Distribution_pt. Data is stored in a C-style pointer vector (double*)
virtual ~GS()
Destructor (cleanup storage)
GMRES(const GMRES &)
Broken copy constructor.
virtual ~CG()
Destructor (cleanup storage)
BiCGStab(const BiCGStab &)
Broken copy constructor.
CRDoubleMatrix * Matrix_pt
System matrix pointer in the format specified by the template argument.
double jacobian_setup_time() const
returns the time taken to assemble the jacobian matrix and residual vector
void smoother_setup(DoubleMatrixBase *matrix_pt)
Set up the smoother for the matrix specified by the pointer.
bool Setup_preconditioner_before_solve
indicates whether the preconditioner should be setup before solve. Default = true; ...
void set_preconditioner_RHS()
Enable right preconditioning.
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
void operator=(const GMRES &)
Broken assignment operator.
Abstract base class for matrices of doubles – adds abstract interfaces for solving, LU decomposition and multiplication by vectors.
unsigned Iterations
Number of iterations taken.
void resolve(const DoubleVector &rhs, DoubleVector &result)
Re-solve the system defined by the last assembled Jacobian and the rhs vector specified here...
A class for compressed row matrices. This is a distributable object.
virtual double preconditioner_setup_time() const
returns the the time taken to setup the preconditioner
LinearAlgebraDistribution * distribution_pt() const
access to the LinearAlgebraDistribution
double linear_solver_solution_time() const
return the time taken to solve the linear system
double Preconditioner_setup_time
Preconditioner setup time.
double preconditioner_setup_time() const
Returns the time taken to set up the preconditioner.
MATRIX * Matrix_pt
Pointer to matrix.
Base class for all linear iterative solvers. This merely defines standard interfaces for linear itera...
void solve(DoubleMatrixBase *const &matrix_pt, const DoubleVector &rhs, DoubleVector &solution)
Linear-algebra-type solver: Takes pointer to a matrix and rhs vector and returns the solution of the ...
bool Resolving
Boolean flag to indicate if the solve is done in re-solve mode, bypassing setup of matrix and precond...