18 #ifdef DEAL_II_WITH_TRILINOS
27 # include <AztecOO_StatusTest.h>
28 # include <AztecOO_StatusTestCombo.h>
29 # include <AztecOO_StatusTestMaxIters.h>
30 # include <AztecOO_StatusTestResNorm.h>
31 # include <AztecOO_StatusType.h>
41 const bool output_solver_details,
42 const unsigned int gmres_restart_parameter)
43 : output_solver_details(output_solver_details)
44 , gmres_restart_parameter(gmres_restart_parameter)
60 : solver_name(solver_name)
62 , additional_data(data)
84 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()),
86 const_cast<Epetra_MultiVector *
>(&
b.trilinos_vector()));
106 const_cast<Epetra_MultiVector *
>(&
b.trilinos_vector()));
126 const_cast<Epetra_MultiVector *
>(&
b.trilinos_vector()));
137 Epetra_MultiVector & x,
138 const Epetra_MultiVector &
b,
146 const_cast<Epetra_MultiVector *
>(&
b));
157 Epetra_MultiVector & x,
158 const Epetra_MultiVector &
b,
166 const_cast<Epetra_MultiVector *
>(&
b));
176 const ::Vector<double> &
b,
183 Assert(
A.local_range().second ==
A.m(),
184 ExcMessage(
"Can only work in serial when using deal.II vectors."));
185 Assert(
A.trilinos_matrix().Filled(),
186 ExcMessage(
"Matrix is not compressed. Call compress() method."));
188 Epetra_Vector ep_x(
View,
A.trilinos_matrix().DomainMap(), x.
begin());
189 Epetra_Vector ep_b(
View,
190 A.trilinos_matrix().RangeMap(),
191 const_cast<double *
>(
b.begin()));
196 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()), &ep_x, &ep_b);
206 const ::Vector<double> &
b,
209 Epetra_Vector ep_x(
View,
A.OperatorDomainMap(), x.
begin());
210 Epetra_Vector ep_b(
View,
211 A.OperatorRangeMap(),
212 const_cast<double *
>(
b.begin()));
217 std_cxx14::make_unique<Epetra_LinearProblem>(&
A, &ep_x, &ep_b);
227 const ::LinearAlgebra::distributed::Vector<double> &
b,
233 A.trilinos_matrix().DomainMap().NumMyElements());
235 A.trilinos_matrix().RangeMap().NumMyElements());
237 Epetra_Vector ep_x(
View,
A.trilinos_matrix().DomainMap(), x.
begin());
238 Epetra_Vector ep_b(
View,
239 A.trilinos_matrix().RangeMap(),
240 const_cast<double *
>(
b.begin()));
245 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()), &ep_x, &ep_b);
255 const ::LinearAlgebra::distributed::Vector<double> &
b,
261 Epetra_Vector ep_x(
View,
A.OperatorDomainMap(), x.
begin());
262 Epetra_Vector ep_b(
View,
263 A.OperatorRangeMap(),
264 const_cast<double *
>(
b.begin()));
269 std_cxx14::make_unique<Epetra_LinearProblem>(&
A, &ep_x, &ep_b);
280 compute_residual(
const Epetra_MultiVector *
const residual_vector)
282 Assert(residual_vector->NumVectors() == 1,
283 ExcMessage(
"Residual multivector holds more than one vector"));
285 const int ierr = residual_vector->Norm2(&res_l2_norm);
290 class TrilinosReductionControl :
public AztecOO_StatusTest
293 TrilinosReductionControl(
const int max_steps,
294 const double tolerance,
296 const Epetra_LinearProblem &linear_problem);
298 virtual ~TrilinosReductionControl()
override =
default;
301 ResidualVectorRequired()
const override
306 virtual AztecOO_StatusType
307 CheckStatus(
int CurrentIter,
308 Epetra_MultiVector *CurrentResVector,
309 double CurrentResNormEst,
310 bool SolutionUpdated)
override
315 (CurrentResNormEst < 0.0 ? compute_residual(CurrentResVector) :
317 if (CurrentIter == 0)
326 virtual AztecOO_StatusType
327 GetStatus()
const override
332 virtual std::ostream &
333 Print(std::ostream &stream,
int indent = 0)
const override
339 get_initial_residual()
const
345 get_current_residual()
const
360 TrilinosReductionControl::TrilinosReductionControl(
362 const double tolerance,
364 const Epetra_LinearProblem &linear_problem)
370 std_cxx14::make_unique<AztecOO_StatusTestCombo>(
371 AztecOO_StatusTestCombo::OR))
376 std_cxx14::make_unique<AztecOO_StatusTestMaxIters>(max_steps);
379 Assert(linear_problem.GetRHS()->NumVectors() == 1,
380 ExcMessage(
"RHS multivector holds more than one vector"));
384 *linear_problem.GetOperator(),
385 *(linear_problem.GetLHS()->operator()(0)),
386 *(linear_problem.GetRHS()->operator()(0)),
389 AztecOO_StatusTestResNorm::TwoNorm);
391 AztecOO_StatusTestResNorm::None, AztecOO_StatusTestResNorm::TwoNorm);
396 *linear_problem.GetOperator(),
397 *(linear_problem.GetLHS()->operator()(0)),
398 *(linear_problem.GetRHS()->operator()(0)),
401 AztecOO_StatusTestResNorm::TwoNorm);
403 AztecOO_StatusTestResNorm::NormOfInitRes,
404 AztecOO_StatusTestResNorm::TwoNorm);
412 template <
typename Preconditioner>
414 SolverBase::do_solve(
const Preconditioner &preconditioner)
419 solver.SetProblem(*linear_problem);
425 solver.SetAztecOption(AZ_solver, AZ_cg);
428 solver.SetAztecOption(AZ_solver, AZ_cgs);
431 solver.SetAztecOption(AZ_solver, AZ_gmres);
432 solver.SetAztecOption(AZ_kspace,
433 additional_data.gmres_restart_parameter);
436 solver.SetAztecOption(AZ_solver, AZ_bicgstab);
439 solver.SetAztecOption(AZ_solver, AZ_tfqmr);
446 set_preconditioner(solver, preconditioner);
449 solver.SetAztecOption(AZ_output,
450 additional_data.output_solver_details ? AZ_all :
452 solver.SetAztecOption(AZ_conv, AZ_noscaled);
473 std_cxx14::make_unique<internal::TrilinosReductionControl>(
474 reduction_control->max_steps(),
475 reduction_control->tolerance(),
476 reduction_control->reduction(),
478 solver.SetStatusTest(status_test.get());
484 solver.Iterate(solver_control.max_steps(), solver_control.tolerance());
494 "option not implemented"));
499 "numerical breakdown"));
504 "loss of precision"));
509 "GMRES Hessenberg ill-conditioned"));
522 if (
const internal::TrilinosReductionControl
523 *
const reduction_control_status =
524 dynamic_cast<const internal::TrilinosReductionControl *const
>(
533 if (solver.NumIters() > 0)
538 solver_control.check(
539 0, reduction_control_status->get_initial_residual());
540 solver_control.check(
542 reduction_control_status->get_current_residual());
545 solver_control.check(
547 reduction_control_status->get_current_residual());
552 solver_control.check(solver.NumIters(), solver.TrueResidual());
558 solver_control.last_value()));
565 SolverBase::set_preconditioner(AztecOO & solver,
572 const int ierr = solver.SetPrecOperator(
577 solver.SetAztecOption(AZ_precond, AZ_none);
583 SolverBase::set_preconditioner(AztecOO & solver,
587 solver.SetPrecOperator(
const_cast<Epetra_Operator *
>(&preconditioner));
594 SolverCG::AdditionalData::AdditionalData(
const bool output_solver_details)
611 const bool output_solver_details,
612 const unsigned int restart_parameter)
629 const bool output_solver_details)
679 const std::string &solver_type)
680 : output_solver_details(output_solver_details)
681 , solver_type(solver_type)
710 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()));
727 std::string(
"You tried to select the solver type <") +
729 "> but this solver is not supported by Trilinos either "
730 "because it does not exist, or because Trilinos was not "
731 "configured for its use."));
736 verbose_cout <<
"Starting symbolic factorization" << std::endl;
737 ierr =
solver->SymbolicFactorization();
740 verbose_cout <<
"Starting numeric factorization" << std::endl;
741 ierr =
solver->NumericFactorization();
754 const_cast<Epetra_MultiVector *
>(&
b.trilinos_vector()));
762 verbose_cout <<
"Starting solve" << std::endl;
765 int ierr =
solver->Solve();
777 const ::LinearAlgebra::distributed::Vector<double> &
b)
779 Epetra_Vector ep_x(
View,
782 Epetra_Vector ep_b(
View,
784 const_cast<double *
>(
b.begin()));
797 verbose_cout <<
"Starting solve" << std::endl;
800 int ierr =
solver->Solve();
827 std::string(
"You tried to select the solver type <") +
829 "> but this solver is not supported by Trilinos either "
830 "because it does not exist, or because Trilinos was not "
831 "configured for its use."));
836 verbose_cout <<
"Starting symbolic factorization" << std::endl;
837 ierr =
solver->SymbolicFactorization();
840 verbose_cout <<
"Starting numeric factorization" << std::endl;
841 ierr =
solver->NumericFactorization();
844 verbose_cout <<
"Starting solve" << std::endl;
868 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()),
870 const_cast<Epetra_MultiVector *
>(&
b.trilinos_vector()));
880 const ::Vector<double> &
b)
886 Assert(
A.local_range().second ==
A.m(),
887 ExcMessage(
"Can only work in serial when using deal.II vectors."));
888 Epetra_Vector ep_x(
View,
A.trilinos_matrix().DomainMap(), x.
begin());
889 Epetra_Vector ep_b(
View,
890 A.trilinos_matrix().RangeMap(),
891 const_cast<double *
>(
b.begin()));
896 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()), &ep_x, &ep_b);
907 const ::LinearAlgebra::distributed::Vector<double> &
b)
910 A.trilinos_matrix().DomainMap().NumMyElements());
912 A.trilinos_matrix().RangeMap().NumMyElements());
913 Epetra_Vector ep_x(
View,
A.trilinos_matrix().DomainMap(), x.
begin());
914 Epetra_Vector ep_b(
View,
915 A.trilinos_matrix().RangeMap(),
916 const_cast<double *
>(
b.begin()));
921 const_cast<Epetra_CrsMatrix *
>(&
A.trilinos_matrix()), &ep_x, &ep_b);
941 #endif // DEAL_II_WITH_PETSC