// @HEADER // ***************************************************************************** // NOX: An Object-Oriented Nonlinear Solver Package // // Copyright 2002 NTESS and the NOX contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // 1D Finite Element Test Problem /* Solves the nonlinear equation: * * d2u * --- - k * u**2 = 0 * dx2 * * subject to @ x=0, u=1 */ // NOX Objects #include "NOX.H" #include "NOX_Epetra.H" // Trilinos Objects #ifdef HAVE_MPI #include "Epetra_MpiComm.h" #else #include "Epetra_SerialComm.h" #endif #include "Epetra_Map.h" #include "Epetra_Vector.h" #include "Epetra_RowMatrix.h" #include "Epetra_CrsMatrix.h" #include "Epetra_Map.h" #include "Epetra_LinearProblem.h" #include "AztecOO.h" #include "Teuchos_StandardCatchMacros.hpp" // User's application specific files #include "Problem_Interface.H" // Interface file to NOX #include "FiniteElementProblem.H" // Required for reading and writing parameter lists from xml format // Configure Trilinos with --enable-teuchos-extended #ifdef HAVE_TEUCHOS_EXTENDED #include "Teuchos_XMLParameterListHelpers.hpp" #include #endif using namespace std; int main(int argc, char *argv[]) { // Initialize MPI Teuchos::GlobalMPISession session(&argc, &argv, NULL); bool success = false; bool verbose = false; try { // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif int ierr = 0, i; // Get the process ID and the total number of processors int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); // Get the number of elements from the command line int NumGlobalElements = 100 + 1; // The number of unknowns must be at least equal to the // number of processors. if (NumGlobalElements < NumProc) { std::cout << "numGlobalBlocks = " << NumGlobalElements << " cannot be < number of processors = " << NumProc << std::endl; exit(1); } // Create the FiniteElementProblem class. This creates all required // Epetra objects for the problem and allows calls to the // function (RHS) and Jacobian evaluation routines. FiniteElementProblem Problem(NumGlobalElements, Comm); // Get the vector from the Problem Teuchos::RCP soln = Problem.getSolution(); NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView); // Initialize Solution soln->PutScalar(1.0); // Begin Nonlinear Solver ************************************ // Create the top level parameter list Teuchos::RCP nlParamsPtr = Teuchos::rcp(new Teuchos::ParameterList); Teuchos::ParameterList& nlParams = *nlParamsPtr.get(); // Set the nonlinear solver method nlParams.set("Nonlinear Solver", "Line Search Based"); //nlParams.set("Nonlinear Solver", "Trust Region Based"); // Set the printing parameters in the "Printing" sublist Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); printParams.set("MyPID", MyPID); printParams.set("Output Precision", 3); printParams.set("Output Processor", 0); printParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::Parameters + NOX::Utils::Details + NOX::Utils::Warning); // Create printing utilities NOX::Utils utils(printParams); // Sublist for line search Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); searchParams.set("Method", "Full Step"); //searchParams.set("Method", "Interval Halving"); //searchParams.set("Method", "Polynomial"); //searchParams.set("Method", "NonlinearCG"); //searchParams.set("Method", "Quadratic"); //searchParams.set("Method", "More'-Thuente"); // Sublist for direction Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); // dirParams.set("Method", "Modified-Newton"); // Teuchos::ParameterList& newtonParams = dirParams.sublist("Modified-Newton"); // newtonParams.set("Max Age of Jacobian", 2); dirParams.set("Method", "Newton"); Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); newtonParams.set("Forcing Term Method", "Constant"); //newtonParams.set("Forcing Term Method", "Type 1"); //newtonParams.set("Forcing Term Method", "Type 2"); //newtonParams.set("Forcing Term Minimum Tolerance", 1.0e-4); //newtonParams.set("Forcing Term Maximum Tolerance", 0.1); //dirParams.set("Method", "Steepest Descent"); //Teuchos::ParameterList& sdParams = dirParams.sublist("Steepest Descent"); //sdParams.set("Scaling Type", "None"); //sdParams.set("Scaling Type", "2-Norm"); //sdParams.set("Scaling Type", "Quadratic Model Min"); //dirParams.set("Method", "NonlinearCG"); //Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG"); //nlcgParams.set("Restart Frequency", 2000); //nlcgParams.set("Precondition", "On"); //nlcgParams.set("Orthogonalize", "Polak-Ribiere"); //nlcgParams.set("Orthogonalize", "Fletcher-Reeves"); // Sublist for linear solver for the Newton method Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); lsParams.set("Aztec Solver", "GMRES"); lsParams.set("Max Iterations", 800); lsParams.set("Tolerance", 1e-4); lsParams.set("Preconditioner", "None"); //lsParams.set("Preconditioner", "Ifpack"); lsParams.set("Max Age Of Prec", 5); // Create the interface between the test problem and the nonlinear solver // This is created by the user using inheritance of the abstract base class: // NOX_Epetra_Interface Teuchos::RCP interface = Teuchos::rcp(new Problem_Interface(Problem)); // Create the Epetra_RowMatrix. Uncomment one or more of the following: // 1. User supplied (Epetra_RowMatrix) Teuchos::RCP Analytic = Problem.getJacobian(); // 2. Matrix-Free (Epetra_Operator) Teuchos::RCP MF = Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface, noxSoln)); // 3. Finite Difference (Epetra_RowMatrix) Teuchos::RCP FD = Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface, noxSoln)); // Create the linear system Teuchos::RCP iReq = interface; Teuchos::RCP iJac = MF; Teuchos::RCP iPrec = interface; Teuchos::RCP linSys = Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, iReq, iJac, MF, noxSoln)); // Create the Group Teuchos::RCP grp = Teuchos::rcp(new NOX::Epetra::Group(printParams, iReq, noxSoln, linSys)); // Create the convergence tests Teuchos::RCP absresid = Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); Teuchos::RCP relresid = Teuchos::rcp(new NOX::StatusTest::NormF(*grp.get(), 1.0e-2)); Teuchos::RCP update = Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5)); Teuchos::RCP wrms = Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); Teuchos::RCP converged = Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); converged->addStatusTest(absresid); converged->addStatusTest(relresid); converged->addStatusTest(wrms); converged->addStatusTest(update); Teuchos::RCP maxiters = Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); Teuchos::RCP fv = Teuchos::rcp(new NOX::StatusTest::FiniteValue); Teuchos::RCP combo = Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); combo->addStatusTest(fv); combo->addStatusTest(converged); combo->addStatusTest(maxiters); #ifdef HAVE_TEUCHOS_EXTENDED std::stringstream input_file_name; input_file_name << "input_" << MyPID << ".xml"; // Write the parameter list to a file std::cout << "Writing parameter list to "<< input_file_name.str() << std::endl; Teuchos::writeParameterListToXmlFile(*nlParamsPtr, input_file_name.str()); // Read in the parameter list from a file std::cout << "Reading parameter list from " << input_file_name.str() << std::endl; Teuchos::RCP finalParamsPtr = Teuchos::rcp(new Teuchos::ParameterList); Teuchos::updateParametersFromXmlFile(input_file_name.str(), outArg(*finalParamsPtr)); #else Teuchos::RCP finalParamsPtr = nlParamsPtr; #endif // Create the method Teuchos::RCP solver = NOX::Solver::buildSolver(grp, combo, finalParamsPtr); NOX::StatusTest::StatusType status = solver->solve(); if (status == NOX::StatusTest::Converged) utils.out() << "Test Passed!" << std::endl; else { if (MyPID==0) utils.out() << "Nonlinear solver failed to converge!" << std::endl; } // Get the Epetra_Vector with the final solution from the solver const NOX::Epetra::Group& finalGroup = dynamic_cast(solver->getSolutionGroup()); const Epetra_Vector& finalSolution = (dynamic_cast(finalGroup.getX())).getEpetraVector(); // End Nonlinear Solver ************************************** // Output the parameter list if (utils.isPrintType(NOX::Utils::Parameters)) { utils.out() << std::endl << "Final Parameters" << std::endl << "****************" << std::endl; solver->getList().print(utils.out()); utils.out() << std::endl; } // Print solution char file_name[25]; FILE *ifp; int NumMyElements = soln->Map().NumMyElements(); (void) sprintf(file_name, "output.%d",MyPID); ifp = fopen(file_name, "w"); for (i=0; iMap().MinMyGID()+i, finalSolution[i]); fclose(ifp); success = ierr==0; } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); /* end main */ return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); }