// @HEADER // ***************************************************************************** // LOCA: Library of Continuation Algorithms Package // // Copyright 2001-2005 NTESS and the LOCA contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER // 1D Finite Element Test Problem /* Solves continuation problem (Parameter c="Right BC") * * d2u * --- + a * u**3 = 0 * dx2 * * subject to @ x=0, u=b * subject to @ x=1, u=c */ // LOCA Objects #include "LOCA.H" #include "LOCA_Epetra.H" #include "NOX_TestCompare.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" // User's application specific files #include "Problem_Interface.H" // Interface file to NOX #include "Tcubed_FiniteElementProblem.H" #include "NormConstraint.H" using namespace std; int main(int argc, char *argv[]) { int ierr = 0; int MyPID = 0; try { // scale factor to test arc-length scaling double scale = 1.0; // Number of constraints int numConstraints = 1; // Initialize MPI #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif // Get the process ID and the total number of processors MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); // Check for verbose output bool verbose = false; if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; // Get the number of elements from the command line int NumGlobalElements = 0; if ((argc > 2) && (verbose)) NumGlobalElements = atoi(argv[2]) + 1; else if ((argc > 1) && (!verbose)) NumGlobalElements = atoi(argv[1]) + 1; else NumGlobalElements = 101; // 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. Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm, scale); // Get the vector from the Problem Epetra_Vector& soln = Problem.getSolution(); // Initialize Solution soln.PutScalar(1.0); // Begin LOCA Solver ************************************ // Create parameter list Teuchos::RCP paramList = Teuchos::rcp(new Teuchos::ParameterList); // Create LOCA sublist Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA"); // Create the stepper sublist and set the stepper parameters Teuchos::ParameterList& locaStepperList = locaParamsList.sublist("Stepper"); locaStepperList.set("Bordered Solver Method", "Bordering"); locaStepperList.set("Continuation Parameter", "Right BC"); locaStepperList.set("Initial Value", 0.1/scale); locaStepperList.set("Max Value", 100.0/scale); locaStepperList.set("Min Value", 0.05/scale); locaStepperList.set("Max Steps", 50); locaStepperList.set("Max Nonlinear Iterations", 15); // Create predictor sublist Teuchos::ParameterList& predictorList = locaParamsList.sublist("Predictor"); predictorList.set("Method", "Tangent"); // Create step size sublist Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size"); stepSizeList.set("Initial Step Size", 0.1/scale); stepSizeList.set("Min Step Size", 1.0e-3/scale); stepSizeList.set("Max Step Size", 2000.0/scale); stepSizeList.set("Aggressiveness", 0.1); // Create the "Solver" parameters sublist to be used with NOX Solvers Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); // Create the NOX printing parameter list Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); nlPrintParams.set("MyPID", MyPID); if (verbose) nlPrintParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::Details + NOX::Utils::Warning + NOX::Utils::TestDetails + NOX::Utils::Error + NOX::Utils::StepperIteration + NOX::Utils::StepperDetails + NOX::Utils::StepperParameters); else nlPrintParams.set("Output Information", NOX::Utils::Error); // Create the "Linear Solver" sublist Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); Teuchos::ParameterList& newParams = dirParams.sublist("Newton"); Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver"); lsParams.set("Aztec Solver", "GMRES"); lsParams.set("Max Iterations", 100); lsParams.set("Tolerance", 1e-4); if (verbose) lsParams.set("Output Frequency", 1); else lsParams.set("Output Frequency", 0); lsParams.set("Scaling", "None"); lsParams.set("Preconditioner", "Ifpack"); // Create the interface between the test problem and the nonlinear solver // This is created by the user using inheritance of the abstract base // class: Teuchos::RCP interface = Teuchos::rcp(new Problem_Interface(Problem)); Teuchos::RCP iReq = interface; Teuchos::RCP iJac = interface; // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner Teuchos::RCP Amat = Teuchos::rcp(&Problem.getJacobian(),false); // Create the linear systems Teuchos::RCP linsys = Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams, lsParams, iReq, iJac, Amat, soln)); // Create the loca vector NOX::Epetra::Vector locaSoln(soln); // Create and initialize the parameter vector LOCA::ParameterVector pVector; pVector.addParameter("Nonlinear Factor",1.0); pVector.addParameter("Left BC", 0.0); pVector.addParameter("Right BC", 0.1); pVector.addParameter("Constraint Param", 0.5*locaSoln.innerProduct(locaSoln) / locaSoln.length()); // Create Epetra factory Teuchos::RCP epetraFactory = Teuchos::rcp(new LOCA::Epetra::Factory); // Create global data object Teuchos::RCP globalData = LOCA::createGlobalData(paramList, epetraFactory); // Create the Group Teuchos::RCP grp = Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, iReq, locaSoln, linsys, pVector)); grp->computeF(); // Create the constraints object & constraint param names list Teuchos::RCP normConstraint = Teuchos::rcp(new NormConstraint(locaSoln.length(), pVector, locaSoln)); Teuchos::RCP constraints = normConstraint; Teuchos::RCP< std::vector > constraintParamNames = Teuchos::rcp(new std::vector(numConstraints)); //(*constraintParamNames)[0] = "Constraint Param"; (*constraintParamNames)[0] = "Left BC"; // Create the constraints list Teuchos::ParameterList& constraintsList = locaParamsList.sublist("Constraints"); constraintsList.set("Constraint Object", constraints); constraintsList.set("Constraint Parameter Names", constraintParamNames); //constraintsList.set("Bordered Solver Method", "Bordering"); constraintsList.set("Bordered Solver Method", "Householder"); // Create the Solver convergence test Teuchos::RCP wrms = Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); Teuchos::RCP maxiters = Teuchos::rcp(new NOX::StatusTest::MaxIters(15)); Teuchos::RCP combo = Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); combo->addStatusTest(wrms); combo->addStatusTest(maxiters); // Create the stepper LOCA::Stepper stepper(globalData, grp, combo, paramList); LOCA::Abstract::Iterator::IteratorStatus status = stepper.run(); if (status != LOCA::Abstract::Iterator::Finished) { ierr = 1; if (globalData->locaUtils->isPrintType(NOX::Utils::Error)) globalData->locaUtils->out() << "Stepper failed to converge!" << std::endl; } // Get the final solution from the stepper Teuchos::RCP finalGroup = Teuchos::rcp_dynamic_cast(stepper.getSolutionGroup()); const NOX::Epetra::Vector& finalSolution = dynamic_cast(finalGroup->getX()); // Output the parameter list if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) { globalData->locaUtils->out() << std::endl << "Final Parameters" << std::endl << "****************" << std::endl; stepper.getList()->print(globalData->locaUtils->out()); globalData->locaUtils->out() << std::endl; } // Check some statistics on the solution NOX::TestCompare testCompare(globalData->locaUtils->out(), *(globalData->locaUtils)); if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) globalData->locaUtils->out() << std::endl << "***** Checking solution statistics *****" << std::endl; // Check number of steps int numSteps = stepper.getStepNumber(); int numSteps_expected = 23; ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0, "number of continuation steps", NOX::TestCompare::Absolute); // Check number of failed steps int numFailedSteps = stepper.getNumFailedSteps(); int numFailedSteps_expected = 0; ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0, "number of failed continuation steps", NOX::TestCompare::Absolute); // Check final value of continuation parameter double right_bc_final = finalGroup->getParam("Right BC"); double right_bc_expected = 0.05; ierr += testCompare.testValue(right_bc_final, right_bc_expected, 1.0e-14, "final value of continuation parameter", NOX::TestCompare::Relative); // Check norm of solution double norm_x = finalSolution.norm(); double norm_x_expected = 10.04987562; ierr += testCompare.testValue(norm_x, norm_x_expected, 1.0e-7, "norm of final solution", NOX::TestCompare::Relative); LOCA::destroyGlobalData(globalData); } catch (std::exception& e) { std::cout << e.what() << std::endl; ierr = 1; } catch (const char *s) { std::cout << s << std::endl; ierr = 1; } catch (...) { std::cout << "Caught unknown exception!" << std::endl; ierr = 1; } if (MyPID == 0) { if (ierr == 0) std::cout << "All tests passed!" << std::endl; else std::cout << ierr << " test(s) failed!" << std::endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif /* end main */ return ierr ; }