Debian Codegraph @

packages/trilinoscouplings/examples/scaling/TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef __TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos_hpp
11 #define __TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos_hpp
12
13 /// \file TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos.hpp
14 /// \brief Generic Belos solver for the Intrepid Poisson test problem example.
15
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosSolverFactory.hpp"
19
20
21 namespace TrilinosCouplings {
22 namespace IntrepidPoissonExample {
23
24 /// \brief Solve the linear system(s) AX=B with Belos.
25 ///
26 /// This is a generic solve function: you can use any scalar,
27 /// multivector, and operator types that Belos supports. We in turn
28 /// use this as the implementation of solveWithBelos() functions for a
29 /// specific Scalar, multivector, and operator type combination. This
30 /// hopefully increases parallelism in the build.
31 ///
32 /// \tparam ST The type of entries in the matrix and vectors. For
33 /// Epetra objects, this is always double. For Tpetra objects, this
34 /// corresponds to the \c Scalar template parameter of
35 /// Tpetra::MultiVector and Tpetra::Operator.
36 ///
37 /// \tparam MV The type of multivectors: for example,
38 /// Epetra_MultiVector or a Tpetra::MultiVector specialization.
39 ///
40 /// \tparam OP The type of operators (from multivectors to
41 /// multivectors): for example, Epetra_Operator or a
42 /// Tpetra::Operator specialization.
43 ///
44 /// The X and B arguments are both multivectors, meaning that you may
45 /// ask Belos to solve more than one linear system at a time. X and B
46 /// must have the same number of columns.
47 ///
48 /// This interface will change in the future to accept the name of the
49 /// Belos solver to use. For now, the solver is hard-coded to
50 /// Pseudoblock CG (implemented by Belos::PseudoBlockCGSolMgr).
51 ///
52 /// \param converged [out] Whether Belos reported that the iterative
53 /// method converged (solved the linear system to the desired
54 /// tolerance).
55 ///
56 /// \param numItersPerformed [out] Number of iterations that the Belos
57 /// solver performed.
58 ///
59 /// \param solverName [in] Name of Belos solver to use. You may use
60 /// any name that Belos::SolverFactory understands.
61 ///
62 /// \param tol [in] Convergence tolerance for the iterative method.
63 /// The meaning of this depends on the particular iterative method.
64 ///
65 /// \param maxNumIters [in] Maximum number of iterations that the
66 /// iterative method should perform, regardless of whether it
67 /// converged.
68 ///
69 /// \param num_steps [in] Number of "time steps", i.e., the number of
70 // times the solver is called in a fake time-step loop.
71 ///
72 /// \param X [in/out] On input: the initial guess(es) for the iterative
73 /// method. On output: the computed approximate solution.
74 ///
75 /// \param A [in] The matrix in the linear system(s) AX=B to solve.
76 ///
77 /// \param B [in] The right-hand side(s) in the linear system AX=B to solve.
78 ///
79 /// \param M_left [in] If nonnull, a left preconditioner that the
80 /// iterative method may use. If null, the iterative method will
81 /// not use a left preconditioner.
82 ///
83 /// \param M_right [in] If nonnull, a right preconditioner that the
84 /// iterative method may use. If null, the iterative method will
85 /// not use a right preconditioner.
86 template<class ST, class MV, class OP>
87 void
88 solveWithBelos (bool& converged,
89 int& numItersPerformed,
90 const std::string& solverName,
91 const typename Teuchos::ScalarTraits<ST>::magnitudeType& tol,
92 const int maxNumIters,
93 const int num_steps,
94 const Teuchos::RCP<MV>& X,
95 const Teuchos::RCP<const OP>& A,
96 const Teuchos::RCP<const MV>& B,
97 const Teuchos::RCP<const OP>& M_left,
98 const Teuchos::RCP<const OP>& M_right)
99 {
100 using Teuchos::ParameterList;
101 using Teuchos::parameterList;
102 using Teuchos::RCP;
103 using Teuchos::rcp;
104 typedef Belos::LinearProblem<ST, MV, OP > problem_type;
105 typedef Belos::SolverFactory<ST, MV, OP> solver_factory_type;
106 typedef Belos::SolverManager<ST, MV, OP> solver_type;
107 typedef Belos::MultiVecTraits<ST, MV> MVT;
108
109 // Set these in advance, so that if the Belos solver throws an
110 // exception for some reason, these will have sensible values.
111 converged = false;
112 numItersPerformed = 0;
113
114 TEUCHOS_TEST_FOR_EXCEPTION(A.is_null () || X.is_null () || B.is_null (),
115 std::invalid_argument, "solveWithBelos: The A, X, and B arguments must all "
116 "be nonnull.");
117 const int numColsB = MVT::GetNumberVecs (*B);
118 const int numColsX = MVT::GetNumberVecs (*X);
119 TEUCHOS_TEST_FOR_EXCEPTION(numColsB != numColsX, std::invalid_argument,
120 "solveWithBelos: X and B must have the same number of columns. X has "
121 << numColsX << " columns, but B has " << numColsB << " columns.");
122
123 RCP<ParameterList> belosParams = parameterList ();
124 belosParams->set ("Block Size", numColsB);
125 belosParams->set ("Maximum Iterations", maxNumIters);
126 belosParams->set ("Num Blocks", maxNumIters);
127 belosParams->set ("Convergence Tolerance", tol);
128 if (solverName == "GMRES") {
129 belosParams->set ("Orthogonalization", "ICGS");
130 belosParams->set ("maxNumOrthogPasses", 1);
131 }
132 belosParams->set ("Output Frequency", 10);
133 belosParams->set ("Output Style", 1);
134 belosParams->set ("Verbosity", 33);
135
136 RCP<problem_type> problem = rcp (new problem_type (A, X, B));
137 if (! M_left.is_null ()) {
138 problem->setLeftPrec (M_left);
139 }
140 if (! M_right.is_null ()) {
141 problem->setRightPrec (M_right);
142 }
143
144 // Create solver
145 RCP<solver_type> solver;
146 {
147 solver_factory_type factory;
148 solver = factory.create (solverName, belosParams);
149 }
150
151 // Enter "time step" loop -- we're really solving the same system repeatedly
152 converged = true;
153 numItersPerformed = 0;
154 for (int step = 0; step < num_steps; ++step) {
155 // Set x
156 MVT::MvInit(*X);
157
158 // Reset problem
159 const bool set = problem->setProblem ();
160 TEUCHOS_TEST_FOR_EXCEPTION(
161 ! set, std::runtime_error, "solveWithBelos: The "
162 "Belos::LinearProblem's setProblem() method returned false. "
163 "This probably indicates that there is something wrong with A, X, or B.");
164 solver->setProblem (problem);
165
166 // Solve
167 Belos::ReturnType result = solver->solve ();
168
169 converged = converged && (result == Belos::Converged);
170 numItersPerformed += solver->getNumIters ();
171 }
172 }
173
174 } // namespace IntrepidPoissonExample
175 } // namespace TrilinosCouplings
176
177 #endif // __TrilinosCouplings_IntrepidPoissonExample_SolveWithBelos_hpp