Debian Codegraph @

packages/nox/examples/petsc/DS6.5.1/Example.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // Simple 2 equation test for quadratic and cubic line searches
11 // from Dennis & Schnabel's book, chp 6. The test problem is from
12 // Example 6.5.1
13 /*
14 * U0**2 + U1**2 - 2 = 0
15 * exp(U0-1) + U1**3 -2 = 0
16 *
17 * NOTE: To reproduce the results from the reference, the linesearch option
18 * must be set to polynomial. This can be done either from the
19 * command line using "-nox_linesearch_type polynomial" or by
20 * placing the same option specification in an input file,
21 * eg ${HOME}/.petscrc
22 * Also, the solver must be -nox_linesearch_based with
23 * "-nox_direction_type newton", which can be used by not
24 * explicitly setting either of these since they are the defaults.
25 */
26
27 static char help[] =
28 "Solves Dennis & Schnabel example problem in parallel.\n\n";
29
30
31 // Petsc Objects
32 #include "petscksp.h"
33 /*
34 Include "petscsles.h" so that we can use SLES solvers. Note that this file
35 automatically includes:
36 petsc.h - base PETSc routines petscvec.h - vectors
37 petscsys.h - system routines petscmat.h - matrices
38 petscis.h - index sets petscksp.h - Krylov subspace methods
39 petscviewer.h - viewers petscpc.h - preconditioners
40 */
41
42 // NOX Library
43 #include "NOX.H"
44 #include "NOX_Abstract_Vector.H"
45 #include "NOX_Petsc_Vector.H"
46 #include "NOX_Petsc_SharedJacobian.H"
47 #include "NOX_Petsc_Group.H"
48 #include "NOX_Petsc_Options.H"
49
50 // User's application specific files
51 #include "Problem_Interface.H" // Interface file to NOX
52 #include "DennisSchnabel.H"
53
54 using namespace std;
55
56 #undef __FUNCT__
57 #define __FUNCT__ "main"
58
59 int main(int argc, char *argv[])
60 {
61 int ierr = 0;
62
63 // Initialize MPI
64 ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr);
65
66 // Get the process ID and the total number of processors
67 int MyPID, NumProc;
68 #ifdef HAVE_MPI
69 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&MyPID);CHKERRQ(ierr);
70 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&NumProc);CHKERRQ(ierr);
71 #else
72 MyPID = 0;
73 NumProc = 1;
74 #endif
75
76 int NumGlobalElements = 2; // Hardcoded for D&S Example problem
77
78 // A maximum of 2 procesors is allowed since there are only 2 equations
79 if (NumProc >= 3) {
80 std::cout << "ERROR: Maximum number of processors is 2!" << std::endl;
81 exit(1);
82 }
83
84 // Create the Problem class. This creates all required
85 // Petsc objects for the problem and allows calls to the
86 // function (RHS) and Jacobian evaluation routines.
87 DennisSchnabel Problem(NumGlobalElements);
88
89 // Get the vector from the Problem
90 Vec& soln = Problem.getSolution();
91
92 // Initialize Solution. For simplicity, this is done on all (both) procs.
93 int globalIndex[2];
94 double doubleArray[2];
95 globalIndex[0] = 0;
96 globalIndex[1] = 1;
97 doubleArray[0] = 2.0;
98 doubleArray[1] = 0.5;
99 ierr = VecSetValues(soln,2,globalIndex,doubleArray,
100 INSERT_VALUES);CHKERRQ(ierr);
101
102 ierr = VecAssemblyBegin(soln);CHKERRQ(ierr);
103 ierr = VecAssemblyEnd(soln);CHKERRQ(ierr);
104
105 // Begin Nonlinear Solver ************************************
106
107 // Create the top level parameter list
108 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
109 Teuchos::rcp(new Teuchos::ParameterList);
110 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
111
112 NOX::Petsc::Options optionHandler(nlParams, MyPID);
113
114 // Warn if not using Polynimial linesearch
115 std::string lsMethod = nlParams.sublist("Line Search").get("Method", "Full Step");
116 if( "Polynomial" != lsMethod )
117 if (MyPID==0)
118 std::cout << "Line Search Method is set to \"" << lsMethod << "\". This test is designed to "
119 << "work with \"Polynomial\". You can set this using \"-nox_linesearch_type polynomial\" "
120 << "on the command line." << std::endl;
121
122 // Create the interface between the test problem and the nonlinear solver
123 // This is created using inheritance of the abstract base class:
124 // NOX::Petsc::Interface
125 Problem_Interface interface(Problem);
126
127 // Get a reference to the Petsc_RowMatrix created in Problem_Interface.
128 Mat& A = Problem.getJacobian();
129
130 // Create the Group
131 Teuchos::RCP<NOX::Petsc::Group> grp = Teuchos::rcp( new NOX::Petsc::Group(interface, soln, A) );
132 grp->computeF(); // Needed to establish the initial convergence state
133
134 // Create the method and solve
135 Teuchos::RCP<NOX::Solver::Generic> solver =
136 NOX::Solver::buildSolver(grp, optionHandler.getStatusTest(), nlParamsPtr);
137 NOX::StatusTest::StatusType status = solver->solve();
138
139 if (status != NOX::StatusTest::Converged)
140 {
141 if (MyPID==0)
142 std::cout << "Nonlinear solver failed to converge!" << std::endl;
143 if( "Polynomial" != lsMethod )
144 if (MyPID==0)
145 std::cout << "\nLine Search Method is set to \"" << lsMethod << "\". This test is designed to "
146 << "work with \"Polynomial\". You can set this using \"-nox_linesearch_type polynomial\" "
147 << "on the command line." << std::endl;
148 }
149
150
151 // Get the Petsc_Vector with the final solution from the solver
152 const NOX::Petsc::Group& finalGroup =
153 dynamic_cast<const NOX::Petsc::Group&>(solver->getSolutionGroup());
154 const Vec& finalSolution = (dynamic_cast<const NOX::Petsc::Vector&>
155 (finalGroup.getX())).getPetscVector();
156
157 // End Nonlinear Solver **************************************
158
159 // Print solution
160 if(MyPID==0)
161 printf("Final solution :\n");
162 char file_name[25];
163 FILE *ifp;
164 (void) sprintf(file_name, "output.%d",MyPID);
165 double* finalVals;
166 ifp = fopen(file_name, "w");
167 VecGetArray( finalSolution, &finalVals );
168 for(int i=0; i<3-NumProc; i++) {
169 printf("(proc %d)\t%d\t%e\n",MyPID,i,finalVals[i]);
170 fprintf(ifp, "%d %E\n",i, finalVals[i]);
171 }
172 VecRestoreArray( finalSolution, &finalVals );
173 fclose(ifp);
174
175 ierr = PetscFinalize();CHKERRQ(ierr);
176
177 return 0;
178
179 } // end main