Debian Codegraph @

packages/trilinoscouplings/examples/scaling/IntrepidPoisson_Pamgen_EpetraAztecOO_main.cpp

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 /** \file example_IntrepidPoisson_Pamgen_EpetraAztecOO.cpp
11 \brief Example setup of a discretization of a Poisson equation on
12 a hexahedral mesh using nodal (Hgrad) elements. The
13 system is assembled but not solved.
14
15 This example uses the following Trilinos packages:
16 \li Pamgen to generate a Hexahedral mesh.
17 \li Sacado to form the source term from user-specified manufactured solution.
18 \li Intrepid to build the discretization matrix and right-hand side.
19 \li Tpetra to handle the global sparse matrix and dense vector.
20
21 \verbatim
22
23 Poisson system:
24
25 div A grad u = f in Omega
26 u = g on Gamma
27
28 where
29 A is a material tensor (typically symmetric positive definite)
30 f is a given source term
31
32 Corresponding discrete linear system for nodal coefficients(x):
33
34 Kx = b
35
36 K - HGrad stiffness matrix
37 b - right hand side vector
38
39 \endverbatim
40
41 \author Created by P. Bochev, D. Ridzal, K. Peterson,
42 D. Hensinger, C. Siefert. Converted to Tpetra by
43 I. Kalashnikova ([email protected]). Modified by Mark
44 Hoemmen ([email protected]) and back-ported to Epetra for
45 a fair comparison with Tpetra.
46
47 \remark Use the "--help" command-line argument for usage info.
48
49 \remark Example driver has an option to use an input file (XML
50 serialization of a Teuchos::ParameterList) containing a
51 Pamgen mesh description. A version, Poisson.xml, is
52 included in the same directory as this driver. If not
53 included, this program will use a default mesh
54 description.
55
56 \remark The exact solution (u) and material tensor (A) are set in
57 the functions "exactSolution" and "materialTensor" and may
58 be modified by the user. We compute the source term f
59 from u using Sacado automatic differentiation, so that u
60 really is the exact solution. The current implementation
61 of exactSolution() has notes to guide your choice of
62 solution. For example, you might want to pick a solution
63 in the finite element space, so that the discrete solution
64 is exact if the solution of the linear system is exact
65 (modulo rounding error when constructing the linear
66 system).
67 */
68
69 #include "Teuchos_oblackholestream.hpp"
70 #include "Teuchos_GlobalMPISession.hpp"
71 #include "Teuchos_TimeMonitor.hpp"
72 #include "Teuchos_XMLParameterListHelpers.hpp"
73 #include "Teuchos_StandardCatchMacros.hpp"
74
75 #include "Epetra_Comm.h"
76 #ifdef EPETRA_MPI
77 # include "mpi.h"
78 # include "Epetra_MpiComm.h"
79 #else
80 # include "Epetra_SerialComm.h"
81 #endif // EPETRA_MPI
82
83 #include "TrilinosCouplings_EpetraIntrepidPoissonExample.hpp"
84 #include "TrilinosCouplings_IntrepidPoissonExampleHelpers.hpp"
85 #include "TrilinosCouplings_EpetraIntrepidPoissonExample_SolveWithAztecOO.hpp"
86
87 // ML
88 #include "ml_include.h"
89 #include "ml_MultiLevelPreconditioner.h"
90
91 // MueLu includes
92 #include "MueLu.hpp"
93 #include "MueLu_ParameterListInterpreter.hpp"
94 #include "MueLu_EpetraOperator.hpp"
95
96 int
97 main (int argc, char *argv[])
98 {
99 using namespace TrilinosCouplings; // Yes, this means I'm lazy.
100
101 using EpetraIntrepidPoissonExample::exactResidualNorm;
102 using EpetraIntrepidPoissonExample::makeMatrixAndRightHandSide;
103 using EpetraIntrepidPoissonExample::solveWithAztecOO;
104 using IntrepidPoissonExample::makeMeshInput;
105 using IntrepidPoissonExample::setCommandLineArgumentDefaults;
106 using IntrepidPoissonExample::setUpCommandLineArguments;
107 using IntrepidPoissonExample::parseCommandLineArguments;
108 //using Teuchos::Comm;
109 using Teuchos::outArg;
110 using Teuchos::ParameterList;
111 using Teuchos::parameterList;
112 using Teuchos::RCP;
113 using Teuchos::rcp;
114 using Teuchos::rcp_implicit_cast;
115 using Teuchos::rcpFromRef;
116 using Teuchos::getFancyOStream;
117 using Teuchos::FancyOStream;
118 using std::endl;
119 // Pull in typedefs from the example's namespace.
120 typedef EpetraIntrepidPoissonExample::ST ST;
121 //typedef EpetraIntrepidPoissonExample::Node Node;
122 typedef Teuchos::ScalarTraits<ST> STS;
123 typedef STS::magnitudeType MT;
124 typedef Teuchos::ScalarTraits<MT> STM;
125 typedef EpetraIntrepidPoissonExample::sparse_matrix_type sparse_matrix_type;
126 typedef EpetraIntrepidPoissonExample::vector_type vector_type;
127 typedef EpetraIntrepidPoissonExample::operator_type operator_type;
128
129 bool success = true;
130 try {
131
132 Epetra_Object::SetTracebackMode(2);
133
134 Teuchos::oblackholestream blackHole;
135 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackHole);
136 const int myRank = mpiSession.getRank ();
137 //const int numProcs = mpiSession.getNProc ();
138
139 // Get the default communicator and Kokkos Node instance
140 RCP<Epetra_Comm> comm;
141 #ifdef EPETRA_MPI
142 comm = rcp_implicit_cast<Epetra_Comm> (rcp (new Epetra_MpiComm (MPI_COMM_WORLD)));
143 #else
144 comm = rcp_implicit_cast<Epetra_Comm> (rcp (new Epetra_SerialComm));
145 #endif // EPETRA_MPI
146
147 // Did the user specify --help at the command line to print help
148 // with command-line arguments?
149 bool printedHelp = false;
150 // Values of command-line arguments.
151 int nx, ny, nz;
152 std::string xmlInputParamsFile;
153 std::string solverName;
154 bool verbose, debug;
155 double tol;
156 int maxNumIters;
157
158 // Set default values of command-line arguments.
159 setCommandLineArgumentDefaults (nx, ny, nz, xmlInputParamsFile, solverName,
160 verbose, debug);
161 // Parse and validate command-line arguments.
162 Teuchos::CommandLineProcessor cmdp (false, true);
163 setUpCommandLineArguments (cmdp, nx, ny, nz, xmlInputParamsFile, solverName,
164 tol, maxNumIters, verbose, debug);
165 parseCommandLineArguments (cmdp, printedHelp, argc, argv, nx, ny, nz,
166 xmlInputParamsFile, solverName, verbose, debug);
167 if (printedHelp) {
168 // The user specified --help at the command line to print help
169 // with command-line arguments. We printed help already, so quit
170 // with a happy return code.
171 return EXIT_SUCCESS;
172 }
173
174 // Both streams only print on MPI Rank 0. "out" only prints if the
175 // user specified --verbose.
176 RCP<FancyOStream> out =
177 getFancyOStream (rcpFromRef ((myRank == 0 && verbose) ? std::cout : blackHole));
178 RCP<FancyOStream> err =
179 getFancyOStream (rcpFromRef ((myRank == 0 && debug) ? std::cerr : blackHole));
180
181 #ifdef HAVE_MPI
182 *out << "PARALLEL executable" << endl;
183 #else
184 *out << "SERIAL executable" << endl;
185 #endif
186
187 /**********************************************************************************/
188 /********************************** GET XML INPUTS ********************************/
189 /**********************************************************************************/
190 ParameterList inputList;
191 if (xmlInputParamsFile != "") {
192 *out << "Reading parameters from XML file \""
193 << xmlInputParamsFile << "\"..." << endl;
194 Teuchos::updateParametersFromXmlFile (xmlInputParamsFile, outArg (inputList));
195 if (myRank == 0) {
196 inputList.print (*out, 2, true, true);
197 *out << endl;
198 }
199 }
200
201 // Get Pamgen mesh definition string, either from the input
202 // ParameterList or from our function that makes a cube and fills in
203 // the number of cells along each dimension.
204 std::string meshInput = inputList.get("meshInput", "");
205 if (meshInput == "") {
206 *out << "Generating mesh input string: nx = " << nx
207 << ", ny = " << ny
208 << ", nz = " << nz << endl;
209 meshInput = makeMeshInput (nx, ny, nz);
210 }
211
212 // Total application run time
213 {
214 TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Time", total_time);
215
216 // Construct linear system
217 RCP<sparse_matrix_type> A;
218 RCP<vector_type> B, X_exact, X;
219 {
220 TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Assembly", total_assembly);
221 makeMatrixAndRightHandSide (A, B, X_exact, X, comm, meshInput,
222 out, err, verbose, debug);
223 }
224
225 const std::vector<MT> norms = exactResidualNorm (A, B, X_exact);
226 // X_exact is the exact solution of the PDE, projected onto the
227 // discrete mesh. It may not necessarily equal the exact solution
228 // of the linear system.
229 *out << "||B - A*X_exact||_2 = " << norms[0] << endl
230 << "||B||_2 = " << norms[1] << endl
231 << "||A||_F = " << norms[2] << endl;
232
233 // Setup preconditioner
234 std::string prec_type = inputList.get("Preconditioner", "None");
235 RCP<operator_type> M;
236 {
237 TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Preconditioner Setup", total_prec);
238
239 if (prec_type == "ML") {
240 ParameterList mlParams;
241 if (inputList.isSublist("ML"))
242 mlParams = inputList.sublist("ML");
243 else {
244 ML_Epetra::SetDefaults("SA", mlParams);
245 mlParams.set("ML output", 0);
246 }
247 M = rcp(new ML_Epetra::MultiLevelPreconditioner(*A, mlParams));
248 }
249
250 else if (prec_type == "MueLu") {
251 // Turns a Epetra_CrsMatrix into a MueLu::Matrix
252 RCP<Xpetra::CrsMatrix<ST, int, int, Xpetra::EpetraNode> > mueluA_ = rcp(new Xpetra::EpetraCrsMatrixT<int, Xpetra::EpetraNode>(A));
253 RCP<Xpetra::Matrix <ST, int, int, Xpetra::EpetraNode> > mueluA = rcp(new Xpetra::CrsMatrixWrap<ST, int, int, Xpetra::EpetraNode>(mueluA_));
254
255 // Multigrid Hierarchy
256 ParameterList mueluParams;
257 if (inputList.isSublist("MueLu"))
258 mueluParams = inputList.sublist("MueLu");
259 MueLu::ParameterListInterpreter<ST, int, int, Xpetra::EpetraNode> mueLuFactory(mueluParams);
260 RCP<MueLu::Hierarchy<ST, int, int, Xpetra::EpetraNode> > H = mueLuFactory.CreateHierarchy();
261 H->setVerbLevel(Teuchos::VERB_HIGH);
262 H->GetLevel(0)->Set("A", mueluA);
263
264 // Multigrid setup phase
265 H->Setup();
266
267 // Wrap MueLu Hierarchy as a Tpetra::Operator
268 M = rcp(new MueLu::EpetraOperator(H));
269 }
270 }
271
272 bool converged = false;
273 int numItersPerformed = 0;
274 const MT tol = inputList.get("Convergence Tolerance", STM::squareroot (STM::eps ()));
275 const int maxNumIters = inputList.get("Maximum Iterations", 200);
276 const int num_steps = inputList.get("Number of Time Steps", 1);
277 {
278 TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Solve", total_solve);
279 solveWithAztecOO (converged, numItersPerformed, tol, maxNumIters, num_steps, X, A, B, M);
280 }
281
282 // Compute ||X-X_exact||_2
283 MT norm_x, norm_error;
284 X_exact->Norm2(&norm_x);
285 X_exact->Update(-1.0, *X, 1.0);
286 X_exact->Norm2(&norm_error);
287 *out << endl
288 << "||X-X_exact||_2 / ||X_exact||_2 = " << norm_error / norm_x
289 << endl;
290
291 } // total time block
292
293 // Summarize timings
294 // RCP<ParameterList> reportParams = parameterList ("TimeMonitor::report");
295 // reportParams->set ("Report format", std::string ("YAML"));
296 // reportParams->set ("writeGlobalStats", true);
297 // Teuchos::TimeMonitor::report (*out, reportParams);
298 Teuchos::TimeMonitor::summarize(std::cout);
299
300 } //try
301 TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success);
302
303 if (success)
304 return EXIT_SUCCESS;
305 return EXIT_FAILURE;
306 }
307