Debian Codegraph @

packages/nox/test/epetra/LOCA_Tcubed/Tcubed_constraint.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // LOCA: Library of Continuation Algorithms Package
4 //
5 // Copyright 2001-2005 NTESS and the LOCA contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // 1D Finite Element Test Problem
11 /* Solves continuation problem (Parameter c="Right BC")
12 *
13 * d2u
14 * --- + a * u**3 = 0
15 * dx2
16 *
17 * subject to @ x=0, u=b
18 * subject to @ x=1, u=c
19 */
20
21 // LOCA Objects
22 #include "LOCA.H"
23 #include "LOCA_Epetra.H"
24 #include "NOX_TestCompare.H"
25
26 // Trilinos Objects
27 #ifdef HAVE_MPI
28 #include "Epetra_MpiComm.h"
29 #else
30 #include "Epetra_SerialComm.h"
31 #endif
32 #include "Epetra_Map.h"
33 #include "Epetra_Vector.h"
34 #include "Epetra_RowMatrix.h"
35 #include "Epetra_CrsMatrix.h"
36 #include "Epetra_Map.h"
37 #include "Epetra_LinearProblem.h"
38 #include "AztecOO.h"
39
40 // User's application specific files
41 #include "Problem_Interface.H" // Interface file to NOX
42 #include "Tcubed_FiniteElementProblem.H"
43 #include "NormConstraint.H"
44
45 using namespace std;
46
47 int main(int argc, char *argv[])
48 {
49 int ierr = 0;
50 int MyPID = 0;
51
52 try {
53
54 // scale factor to test arc-length scaling
55 double scale = 1.0;
56
57 // Number of constraints
58 int numConstraints = 1;
59
60 // Initialize MPI
61 #ifdef HAVE_MPI
62 MPI_Init(&argc,&argv);
63 #endif
64
65 // Create a communicator for Epetra objects
66 #ifdef HAVE_MPI
67 Epetra_MpiComm Comm( MPI_COMM_WORLD );
68 #else
69 Epetra_SerialComm Comm;
70 #endif
71
72 // Get the process ID and the total number of processors
73 MyPID = Comm.MyPID();
74 int NumProc = Comm.NumProc();
75
76 // Check for verbose output
77 bool verbose = false;
78 if (argc>1)
79 if (argv[1][0]=='-' && argv[1][1]=='v')
80 verbose = true;
81
82 // Get the number of elements from the command line
83 int NumGlobalElements = 0;
84 if ((argc > 2) && (verbose))
85 NumGlobalElements = atoi(argv[2]) + 1;
86 else if ((argc > 1) && (!verbose))
87 NumGlobalElements = atoi(argv[1]) + 1;
88 else
89 NumGlobalElements = 101;
90
91 // The number of unknowns must be at least equal to the
92 // number of processors.
93 if (NumGlobalElements < NumProc) {
94 std::cout << "numGlobalBlocks = " << NumGlobalElements
95 << " cannot be < number of processors = " << NumProc << std::endl;
96 exit(1);
97 }
98
99 // Create the FiniteElementProblem class. This creates all required
100 // Epetra objects for the problem and allows calls to the
101 // function (RHS) and Jacobian evaluation routines.
102 Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm, scale);
103
104 // Get the vector from the Problem
105 Epetra_Vector& soln = Problem.getSolution();
106
107 // Initialize Solution
108 soln.PutScalar(1.0);
109
110 // Begin LOCA Solver ************************************
111
112 // Create parameter list
113 Teuchos::RCP<Teuchos::ParameterList> paramList =
114 Teuchos::rcp(new Teuchos::ParameterList);
115
116 // Create LOCA sublist
117 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
118
119 // Create the stepper sublist and set the stepper parameters
120 Teuchos::ParameterList& locaStepperList =
121 locaParamsList.sublist("Stepper");
122 locaStepperList.set("Bordered Solver Method", "Bordering");
123 locaStepperList.set("Continuation Parameter", "Right BC");
124 locaStepperList.set("Initial Value", 0.1/scale);
125 locaStepperList.set("Max Value", 100.0/scale);
126 locaStepperList.set("Min Value", 0.05/scale);
127 locaStepperList.set("Max Steps", 50);
128 locaStepperList.set("Max Nonlinear Iterations", 15);
129
130 // Create predictor sublist
131 Teuchos::ParameterList& predictorList =
132 locaParamsList.sublist("Predictor");
133 predictorList.set("Method", "Tangent");
134
135 // Create step size sublist
136 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
137 stepSizeList.set("Initial Step Size", 0.1/scale);
138 stepSizeList.set("Min Step Size", 1.0e-3/scale);
139 stepSizeList.set("Max Step Size", 2000.0/scale);
140 stepSizeList.set("Aggressiveness", 0.1);
141
142 // Create the "Solver" parameters sublist to be used with NOX Solvers
143 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
144
145 // Create the NOX printing parameter list
146 Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
147 nlPrintParams.set("MyPID", MyPID);
148 if (verbose)
149 nlPrintParams.set("Output Information",
150 NOX::Utils::OuterIteration +
151 NOX::Utils::OuterIterationStatusTest +
152 NOX::Utils::InnerIteration +
153 NOX::Utils::Details +
154 NOX::Utils::Warning +
155 NOX::Utils::TestDetails +
156 NOX::Utils::Error +
157 NOX::Utils::StepperIteration +
158 NOX::Utils::StepperDetails +
159 NOX::Utils::StepperParameters);
160 else
161 nlPrintParams.set("Output Information", NOX::Utils::Error);
162
163 // Create the "Linear Solver" sublist
164 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
165 Teuchos::ParameterList& newParams = dirParams.sublist("Newton");
166 Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver");
167 lsParams.set("Aztec Solver", "GMRES");
168 lsParams.set("Max Iterations", 100);
169 lsParams.set("Tolerance", 1e-4);
170 if (verbose)
171 lsParams.set("Output Frequency", 1);
172 else
173 lsParams.set("Output Frequency", 0);
174 lsParams.set("Scaling", "None");
175 lsParams.set("Preconditioner", "Ifpack");
176
177 // Create the interface between the test problem and the nonlinear solver
178 // This is created by the user using inheritance of the abstract base
179 // class:
180 Teuchos::RCP<Problem_Interface> interface =
181 Teuchos::rcp(new Problem_Interface(Problem));
182 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface;
183 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
184
185 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
186 Teuchos::RCP<Epetra_RowMatrix> Amat =
187 Teuchos::rcp(&Problem.getJacobian(),false);
188
189 // Create the linear systems
190 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys =
191 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams,
192 lsParams, iReq, iJac,
193 Amat, soln));
194
195 // Create the loca vector
196 NOX::Epetra::Vector locaSoln(soln);
197
198 // Create and initialize the parameter vector
199 LOCA::ParameterVector pVector;
200 pVector.addParameter("Nonlinear Factor",1.0);
201 pVector.addParameter("Left BC", 0.0);
202 pVector.addParameter("Right BC", 0.1);
203 pVector.addParameter("Constraint Param",
204 0.5*locaSoln.innerProduct(locaSoln) /
205 locaSoln.length());
206
207 // Create Epetra factory
208 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
209 Teuchos::rcp(new LOCA::Epetra::Factory);
210
211 // Create global data object
212 Teuchos::RCP<LOCA::GlobalData> globalData =
213 LOCA::createGlobalData(paramList, epetraFactory);
214
215 // Create the Group
216 Teuchos::RCP<LOCA::Epetra::Group> grp =
217 Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams,
218 iReq, locaSoln,
219 linsys, pVector));
220 grp->computeF();
221
222 // Create the constraints object & constraint param names list
223 Teuchos::RCP<NormConstraint> normConstraint =
224 Teuchos::rcp(new NormConstraint(locaSoln.length(), pVector, locaSoln));
225 Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface> constraints = normConstraint;
226
227 Teuchos::RCP< std::vector<string> > constraintParamNames =
228 Teuchos::rcp(new std::vector<string>(numConstraints));
229 //(*constraintParamNames)[0] = "Constraint Param";
230 (*constraintParamNames)[0] = "Left BC";
231
232 // Create the constraints list
233 Teuchos::ParameterList& constraintsList =
234 locaParamsList.sublist("Constraints");
235 constraintsList.set("Constraint Object", constraints);
236 constraintsList.set("Constraint Parameter Names",
237 constraintParamNames);
238 //constraintsList.set("Bordered Solver Method", "Bordering");
239 constraintsList.set("Bordered Solver Method", "Householder");
240
241 // Create the Solver convergence test
242 Teuchos::RCP<NOX::StatusTest::NormF> wrms =
243 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
244 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
245 Teuchos::rcp(new NOX::StatusTest::MaxIters(15));
246 Teuchos::RCP<NOX::StatusTest::Combo> combo =
247 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
248 combo->addStatusTest(wrms);
249 combo->addStatusTest(maxiters);
250
251 // Create the stepper
252 LOCA::Stepper stepper(globalData, grp, combo, paramList);
253 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
254
255 if (status != LOCA::Abstract::Iterator::Finished) {
256 ierr = 1;
257 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
258 globalData->locaUtils->out()
259 << "Stepper failed to converge!" << std::endl;
260 }
261
262 // Get the final solution from the stepper
263 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
264 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
265 const NOX::Epetra::Vector& finalSolution =
266 dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX());
267
268 // Output the parameter list
269 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
270 globalData->locaUtils->out()
271 << std::endl << "Final Parameters" << std::endl
272 << "****************" << std::endl;
273 stepper.getList()->print(globalData->locaUtils->out());
274 globalData->locaUtils->out() << std::endl;
275 }
276
277 // Check some statistics on the solution
278 NOX::TestCompare testCompare(globalData->locaUtils->out(),
279 *(globalData->locaUtils));
280
281 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
282 globalData->locaUtils->out()
283 << std::endl
284 << "***** Checking solution statistics *****"
285 << std::endl;
286
287 // Check number of steps
288 int numSteps = stepper.getStepNumber();
289 int numSteps_expected = 23;
290 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
291 "number of continuation steps",
292 NOX::TestCompare::Absolute);
293
294 // Check number of failed steps
295 int numFailedSteps = stepper.getNumFailedSteps();
296 int numFailedSteps_expected = 0;
297 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
298 "number of failed continuation steps",
299 NOX::TestCompare::Absolute);
300
301 // Check final value of continuation parameter
302 double right_bc_final = finalGroup->getParam("Right BC");
303 double right_bc_expected = 0.05;
304 ierr += testCompare.testValue(right_bc_final, right_bc_expected, 1.0e-14,
305 "final value of continuation parameter",
306 NOX::TestCompare::Relative);
307
308 // Check norm of solution
309 double norm_x = finalSolution.norm();
310 double norm_x_expected = 10.04987562;
311 ierr += testCompare.testValue(norm_x, norm_x_expected, 1.0e-7,
312 "norm of final solution",
313 NOX::TestCompare::Relative);
314
315 LOCA::destroyGlobalData(globalData);
316 }
317
318 catch (std::exception& e) {
319 std::cout << e.what() << std::endl;
320 ierr = 1;
321 }
322 catch (const char *s) {
323 std::cout << s << std::endl;
324 ierr = 1;
325 }
326 catch (...) {
327 std::cout << "Caught unknown exception!" << std::endl;
328 ierr = 1;
329 }
330
331 if (MyPID == 0) {
332 if (ierr == 0)
333 std::cout << "All tests passed!" << std::endl;
334 else
335 std::cout << ierr << " test(s) failed!" << std::endl;
336 }
337
338 #ifdef HAVE_MPI
339 MPI_Finalize() ;
340 #endif
341
342 /* end main
343 */
344 return ierr ;
345 }