// @HEADER // ***************************************************************************** // Trilinos: An Object-Oriented Solver Framework // // Copyright 2001-2024 NTESS and the Trilinos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** \file IntrepidPoisson_Pamgen_Tpetra_main.cpp \brief Example: Discretize Poisson's equation with Dirichlet boundary conditions on a hexahedral mesh using nodal (Hgrad) elements. The system is assembled into Tpetra data structures, and optionally solved. This example uses the following Trilinos packages: \li Pamgen to generate a Hexahedral mesh. \li Sacado to form the source term from user-specified manufactured solution. \li Intrepid to build the discretization matrix and right-hand side. \li Tpetra to handle the global sparse matrix and dense vector. \verbatim Poisson system: div A grad u = f in Omega u = g on Gamma where A is a material tensor (typically symmetric positive definite) f is a given source term Corresponding discrete linear system for nodal coefficients(x): Kx = b K - HGrad stiffness matrix b - right hand side vector \endverbatim \author Created by P. Bochev, D. Ridzal, K. Peterson, D. Hensinger, C. Siefert. Converted to Tpetra by I. Kalashnikova (ikalash@sandia.gov). Modified by Mark Hoemmen (mhoemme@sandia.gov) and a bunch of other people. \remark Use the "--help" command-line argument for usage info. \remark Example driver has an option to use an input file (XML serialization of a Teuchos::ParameterList) containing a Pamgen mesh description. A version, Poisson.xml, is included in the same directory as this driver. If not included, this program will use a default mesh description. \remark The exact solution (u) and material tensor (A) are set in the functions "exactSolution" and "materialTensor" and may be modified by the user. We compute the source term f from u using Sacado automatic differentiation, so that u really is the exact solution. The current implementation of exactSolution() has notes to guide your choice of solution. For example, you might want to pick a solution in the finite element space, so that the discrete solution is exact if the solution of the linear system is exact (modulo rounding error when constructing the linear system). */ #include #include "Teuchos_oblackholestream.hpp" #include "Teuchos_TimeMonitor.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include "Teuchos_StandardCatchMacros.hpp" #include "TrilinosCouplings_config.h" #include "TrilinosCouplings_TpetraIntrepidPoissonExample.hpp" #include "TrilinosCouplings_IntrepidPoissonExampleHelpers.hpp" #ifdef HAVE_TRILINOSCOUPLINGS_MUELU # include "MueLu_CreateTpetraPreconditioner.hpp" #ifdef HAVE_TRILINOSCOUPLINGS_AVATAR # include "MueLu_AvatarInterface.hpp" #endif //HAVE_TRILINOSCOUPLINGS_AVATAR #endif // HAVE_TRILINOSCOUPLINGS_MUELU #include "Tpetra_Core.hpp" #include "MatrixMarket_Tpetra.hpp" int main (int argc, char *argv[]) { using namespace TrilinosCouplings; // Yes, this means I'm lazy. using TpetraIntrepidPoissonExample::exactResidualNorm; using TpetraIntrepidPoissonExample::makeMatrixAndRightHandSide; using TpetraIntrepidPoissonExample::solveWithBelos; using TpetraIntrepidPoissonExample::solveWithBelosGPU; using namespace IntrepidPoissonExample; using Teuchos::Comm; using Teuchos::outArg; using Teuchos::ParameterList; using Teuchos::parameterList; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcpFromRef; using Teuchos::getFancyOStream; using Teuchos::FancyOStream; using std::endl; // Pull in typedefs from the example's namespace. typedef TpetraIntrepidPoissonExample::ST ST; #ifdef HAVE_TRILINOSCOUPLINGS_MUELU typedef TpetraIntrepidPoissonExample::LO LO; typedef TpetraIntrepidPoissonExample::GO GO; #endif // HAVE_TRILINOSCOUPLINGS_MUELU typedef TpetraIntrepidPoissonExample::Node Node; typedef Teuchos::ScalarTraits STS; typedef STS::magnitudeType MT; typedef Teuchos::ScalarTraits STM; typedef TpetraIntrepidPoissonExample::sparse_matrix_type sparse_matrix_type; typedef TpetraIntrepidPoissonExample::vector_type vector_type; typedef TpetraIntrepidPoissonExample::multivector_type multivector_type; typedef TpetraIntrepidPoissonExample::operator_type operator_type; bool success = true; try { Teuchos::oblackholestream blackHole; Tpetra::ScopeGuard mpiSession (&argc, &argv); RCP > comm = Tpetra::getDefaultComm (); const int myRank = comm->getRank (); //const int numProcs = comm->getSize (); // Did the user specify --help at the command line to print help // with command-line arguments? bool printedHelp = false; // Values of command-line arguments. int nx, ny, nz; std::string xmlInputParamsFile; bool verbose, debug; int maxNumItersFromCmdLine = -1; // -1 means "read from XML file" double tolFromCmdLine = -1.0; // -1 means "read from XML file" std::string solverName = "GMRES"; ST materialTensorOffDiagonalValue = Teuchos::ScalarTraits::nan(); std::vector diff_rotation_angle {0.0, 0.0, 0.0}; std::vector diff_strength {1.0, 1.0, 1.0}; Teuchos::ParameterList problemStatistics; // Set default values of command-line arguments. setCommandLineArgumentDefaults (nx, ny, nz, xmlInputParamsFile, solverName, verbose, debug); // Parse and validate command-line arguments. Teuchos::CommandLineProcessor cmdp (false, true); setUpCommandLineArguments (cmdp, nx, ny, nz, xmlInputParamsFile, solverName, tolFromCmdLine, maxNumItersFromCmdLine, verbose, debug); // Diffusion tensor information (if we're using it) cmdp.setOption ("materialTensorOffDiagonalValue", &materialTensorOffDiagonalValue, "Off-diagonal value in " "the material tensor. This controls the iteration count. " "Be careful with this if you use CG, since you can easily " "make the matrix indefinite."); for(int i=0; i<3; i++) { char letter[4] = "xyz"; char str1[80], str2[80]; // Rotation sprintf(str1,"rot_%c_angle",letter[i]); sprintf(str2,"Rotation around %c axis, in degrees",letter[i]); cmdp.setOption(str1,&diff_rotation_angle[i],str2); // Strength sprintf(str1,"strength_%c",letter[i]); sprintf(str2,"Strength of pre-rotation %c-diffusion",letter[i]); cmdp.setOption(str1,&diff_strength[i],str2); } // Additional command-line arguments for GPU experimentation. bool gpu = false; cmdp.setOption ("gpu", "no-gpu", &gpu, "Run example using GPU node (if supported)"); int ranks_per_node = 1; cmdp.setOption ("ranks_per_node", &ranks_per_node, "Number of MPI ranks per node"); int gpu_ranks_per_node = 1; cmdp.setOption ("gpu_ranks_per_node", &gpu_ranks_per_node, "Number of MPI ranks per node for GPUs"); int device_offset = 0; cmdp.setOption ("device_offset", &device_offset, "Offset for attaching MPI ranks to CUDA devices"); // Additional command-line arguments for dumping the generated // matrix or its row Map to output files. // // FIXME (mfh 09 Apr 2014) Need to port these command-line // arguments to the Epetra version. // If matrixFilename is nonempty, dump the matrix to that file // in MatrixMarket format. std::string matrixFilename; cmdp.setOption ("matrixFilename", &matrixFilename, "If nonempty, dump the " "generated matrix to that file in MatrixMarket format."); // If coordsFilename is nonempty, dump the coords to that file // in MatrixMarket format. std::string coordsFilename; cmdp.setOption ("coordsFilename", &coordsFilename, "If nonempty, dump the " "generated coordinates to that file in MatrixMarket format."); // If rowMapFilename is nonempty, dump the matrix's row Map to // that file in MatrixMarket format. std::string rowMapFilename; cmdp.setOption ("rowMapFilename", &rowMapFilename, "If nonempty, dump the " "generated matrix's row Map to that file in a format that " "Tpetra::MatrixMarket::Reader can read."); // Option to exit after building A and b (and dumping stuff to // files, if requested). bool exitAfterAssembly = false; cmdp.setOption ("exitAfterAssembly", "dontExitAfterAssembly", &exitAfterAssembly, "If true, exit after building the " "sparse matrix and dense right-hand side vector. If " " --matrixFilename --coordsFilename or --rowMapFilename are nonempty strings" ", dump the matrix resp. row Map to their respective files " "before exiting."); // Option to exit after building the preconditioner bool exitAfterPrecond = false; cmdp.setOption ("exitAfterPrecond", "dontExitAfterPrecond", &exitAfterPrecond, "If true, exit after building the " "preconditioner."); // Number of rebuilds of the MueLu hierarchy int numMueluRebuilds=0; cmdp.setOption ("rebuild", &numMueluRebuilds, "Number of times to rebuild the MueLu hierarchy."); // Random number seed int randomSeed=24601; cmdp.setOption ("seed", &randomSeed, "Random Seed."); parseCommandLineArguments (cmdp, printedHelp, argc, argv, nx, ny, nz, xmlInputParamsFile, solverName, verbose, debug); if (printedHelp) { // The user specified --help at the command line to print help // with command-line arguments. We printed help already, so quit // with a happy return code. return EXIT_SUCCESS; } // Initialize RNG srand(randomSeed); // Set material information if(Teuchos::ScalarTraits::isnaninf(materialTensorOffDiagonalValue)) setDiffusionRotationAndStrength(diff_rotation_angle, diff_strength); else setMaterialTensorOffDiagonalValue (materialTensorOffDiagonalValue); // Both streams only print on MPI Rank 0. "out" only prints if the // user specified --verbose. RCP out = getFancyOStream (rcpFromRef ((myRank == 0 && verbose) ? std::cout : blackHole)); RCP err = getFancyOStream (rcpFromRef ((myRank == 0 && debug) ? std::cerr : blackHole)); #ifdef HAVE_MPI *out << "PARALLEL executable" << endl; #else *out << "SERIAL executable" << endl; #endif if(useDiffusionMatrix()) { const std::vector & A = getDiffusionMatrix(); *out<<"[ "< A; RCP B, X_exact, X, node_sigma; RCP coords; { TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Assembly", total_assembly); makeMatrixAndRightHandSide (A, B, X_exact, X, coords, node_sigma, comm, meshInput, inputList, problemStatistics, out, err, verbose, debug); } // Print Problem Statistics *out<<"*** Problem Statistics ***\n"< writer_type; if (matrixFilename != "") { writer_type::writeSparseFile (matrixFilename, A); } if (coordsFilename != "") { writer_type::writeDenseFile (coordsFilename, coords); } if (rowMapFilename != "") { writer_type::writeMapFile (rowMapFilename, * (A->getRowMap ())); } } if (exitAfterAssembly) { // Users might still be interested in assembly time. Teuchos::TimeMonitor::report (comm.ptr (), std::cout); return EXIT_SUCCESS; } const std::vector norms = exactResidualNorm (A, B, X_exact); // X_exact is the exact solution of the PDE, projected onto the // discrete mesh. It may not necessarily equal the exact solution // of the linear system. *out << "||B - A*X_exact||_2 = " << norms[0] << endl << "||B||_2 = " << norms[1] << endl << "||A||_F = " << norms[2] << endl; // Setup preconditioner std::string prec_type = inputList.get ("Preconditioner", "None"); RCP M; RCP opA(A); { TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Preconditioner Setup", total_prec); if (prec_type == "MueLu") { #ifdef HAVE_TRILINOSCOUPLINGS_MUELU #ifdef HAVE_TRILINOSCOUPLINGS_AVATAR // If we have Avatar, then let's use it if (inputList.isSublist("Avatar-MueLu")) { // NOTE: User will need to make sure these are named consistently with the tree files specified ParameterList problemFeatures = problemStatistics; ParameterList avatarParams = inputList.sublist("Avatar-MueLu"); ParameterList & mueluParams = inputList.sublist("MueLu"); *out<<"*** Avatar Parameters ***\n"<(opA,mueluParams); } else { M = MueLu::CreateTpetraPreconditioner(opA); } } #else // NOT HAVE_TRILINOSCOUPLINGS_MUELU TEUCHOS_TEST_FOR_EXCEPTION( prec_type == "MueLu", std::runtime_error, "Tpetra scaling example: " "In order to precondition with MueLu, you must have built Trilinos " "with the MueLu package enabled."); #endif // HAVE_TRILINOSCOUPLINGS_MUELU } } // setup preconditioner // exit if we only wanted the preconditioner if(exitAfterPrecond) { Teuchos::TimeMonitor::report (comm.ptr (), std::cout); return EXIT_SUCCESS; } // Get the convergence tolerance for each linear solve. // If the user provided a nonnegative value at the command // line, it overrides any value in the input ParameterList. MT tol = STM::squareroot (STM::eps ()); // default value if (tolFromCmdLine < STM::zero ()) { tol = inputList.get ("Convergence Tolerance", tol); } else { tol = tolFromCmdLine; } // Get the maximum number of iterations for each linear solve. // If the user provided a value other than -1 at the command // line, it overrides any value in the input ParameterList. int maxNumIters = 200; // default value if (maxNumItersFromCmdLine == -1) { maxNumIters = inputList.get ("Maximum Iterations", maxNumIters); } else { maxNumIters = maxNumItersFromCmdLine; } // Get the number of "time steps." We imitate a time-dependent // PDE by doing this many linear solves. const int num_steps = inputList.get ("Number of Time Steps", 1); // Do the linear solve(s). bool converged = false; int numItersPerformed = 0; if (gpu) { TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total GPU Solve", total_solve); solveWithBelosGPU (converged, numItersPerformed, tol, maxNumIters, num_steps, ranks_per_node, gpu_ranks_per_node, device_offset, prec_type, X, A, B, Teuchos::null, M); } else { TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Solve", total_solve); solveWithBelos (converged, numItersPerformed, solverName, tol, maxNumIters, num_steps, X, A, B, Teuchos::null, M); } *out<<"Total Iterations: "<norm2 (); X_exact->update (-1.0, *X, 1.0); const MT norm_error = X_exact->norm2 (); *out << endl << "||X - X_exact||_2 / ||X_exact||_2 = " << norm_error / norm_x << endl; } // total time block // Summarize timings Teuchos::TimeMonitor::report (comm.ptr (), std::cout); } // try TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success); if (success) { return EXIT_SUCCESS; } else { return EXIT_FAILURE; } }