Debian Codegraph @

packages/nox/test/epetra/Thyra/Thyra_RightScaling.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 #include <Teuchos_ConfigDefs.hpp>
10 #include <Teuchos_UnitTestHarness.hpp>
11
12 // NOX Objects
13 #include "NOX.H"
14 #include "NOX_Thyra.H"
15
16 // Trilinos Objects
17 #ifdef HAVE_MPI
18 #include "Epetra_MpiComm.h"
19 #else
20 #include "Epetra_SerialComm.h"
21 #endif
22 #include "Epetra_Map.h"
23 #include "Epetra_Vector.h"
24 #include "Epetra_RowMatrix.h"
25 #include "Epetra_CrsMatrix.h"
26 #include "Epetra_Map.h"
27 #include "Epetra_LinearProblem.h"
28 #include "AztecOO.h"
29
30 #include "Teuchos_ParameterList.hpp"
31 #include "Teuchos_RCP.hpp"
32 #include "Teuchos_GlobalMPISession.hpp"
33 #include "Teuchos_CommandLineProcessor.hpp"
34 #include "Teuchos_Assert.hpp"
35 #include "Teuchos_StandardCatchMacros.hpp"
36
37 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
38 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
39 #include "Thyra_ScaledModelEvaluator.hpp"
40 #include "ModelEvaluator2DSim.hpp"
41
42 #include "NOX_PrePostOperator_RowSumScaling.H"
43 #include "Thyra_VectorBase.hpp"
44
45 using namespace std;
46
47 TEUCHOS_UNIT_TEST(NOX_Thyra_RightScaling, CTOR1)
48 {
49 // Create a communicator for Epetra objects
50 #ifdef HAVE_MPI
51 Epetra_MpiComm Comm( MPI_COMM_WORLD );
52 #else
53 Epetra_SerialComm Comm;
54 #endif
55
56 // Check we have only one processor since this problem doesn't work
57 // for more than one proc
58 TEST_ASSERT(Comm.NumProc() == 1);
59
60 // Create the model evaluator object
61 double d = 1.0;
62 double p0 = 1.0e6;
63 double p1 = -1.0e-6;
64 double x00 = 0.0;
65 double x01 = 0.0;
66 Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel =
67 Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),
68 d,p0,p1,x00,x01));
69
70 ::Stratimikos::DefaultLinearSolverBuilder builder;
71
72 Teuchos::RCP<Teuchos::ParameterList> p =
73 Teuchos::rcp(new Teuchos::ParameterList);
74 {
75 p->set("Linear Solver Type", "AztecOO");
76 //p->set("Preconditioner Type", "Ifpack");
77 p->set("Preconditioner Type", "None");
78 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
79 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
80 az.sublist("VerboseObject").set("Verbosity Level", "high");
81 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
82 ip.sublist("VerboseObject").set("Verbosity Level", "high");
83 }
84
85 builder.setParameterList(p);
86
87 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
88 lowsFactory = builder.createLinearSolveStrategy("");
89
90 thyraModel->set_W_factory(lowsFactory);
91
92 // Create nox parameter list
93 Teuchos::RCP<Teuchos::ParameterList> nl_params =
94 Teuchos::rcp(new Teuchos::ParameterList);
95 nl_params->set("Nonlinear Solver", "Line Search Based");
96 nl_params->sublist("Line Search").set("Method", "Full Step");
97
98 Teuchos::ParameterList& printParams = nl_params->sublist("Printing");
99 printParams.set("Output Information",
100 NOX::Utils::OuterIteration +
101 NOX::Utils::OuterIterationStatusTest +
102 NOX::Utils::InnerIteration +
103 NOX::Utils::LinearSolverDetails +
104 NOX::Utils::Parameters +
105 NOX::Utils::Details +
106 NOX::Utils::Warning +
107 NOX::Utils::Debug +
108 NOX::Utils::TestDetails +
109 NOX::Utils::Error);
110
111 nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete");
112
113 // Enable row sum scaling
114 nl_params->sublist("Thyra Group Options").set("Function Scaling", "None");
115
116 // Create right scaling vector
117 Teuchos::RCP<Thyra::VectorBase<double> > values = Thyra::createMember(thyraModel->get_x_space());
118 Thyra::put_scalar(0.0,values.ptr());
119
120 // Values chosen such that WRMS convergence is achieved in 1 step
121 // Unscaled will not converge in less than 20 steps
122 Thyra::set_ele(0,1.0e14,values.ptr());
123 Thyra::set_ele(1,1.0e2,values.ptr());
124 Teuchos::RCP<NOX::Abstract::Vector > right_scaling = Teuchos::rcp<NOX::Abstract::Vector>(new NOX::Thyra::Vector(values));
125
126 // Insert scaling vector into NOX parameter list
127 Teuchos::ParameterEntry entry(right_scaling);
128 nl_params->setEntry("Right Scaling Vector", entry);
129
130 // Create Status Tests
131 {
132 Teuchos::ParameterList& st = nl_params->sublist("Status Tests");
133 st.set("Test Type", "Combo");
134 st.set("Combo Type", "OR");
135 st.set("Number of Tests", 3);
136
137 {
138 Teuchos::ParameterList& normWRMS = st.sublist("Test 0");
139 normWRMS.set("Test Type", "NormWRMS");
140 normWRMS.set("Absolute Tolerance", 2.0e-8);
141 normWRMS.set("Relative Tolerance", 1.0e-5);
142 normWRMS.set("Tolerance", 1.0);
143 normWRMS.set("BDF Multiplier", 1.0);
144 normWRMS.set("Alpha", 1.0);
145 normWRMS.set("Beta", 0.5);
146 normWRMS.set("Disable Implicit Weighting", true);
147 }
148
149 {
150 Teuchos::ParameterList& fv = st.sublist("Test 1");
151 fv.set("Test Type", "FiniteValue");
152 fv.set("Vector Type", "F Vector");
153 fv.set("Norm Type", "Two Norm");
154 }
155
156 {
157 Teuchos::ParameterList& maxiters = st.sublist("Test 2");
158 maxiters.set("Test Type", "MaxIters");
159 maxiters.set("Maximum Iterations", 20);
160 }
161
162 }
163
164 // Create a Thyra nonlinear solver
165 Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver =
166 Teuchos::rcp(new ::Thyra::NOXNonlinearSolver);
167
168 solver->setParameterList(nl_params);
169 solver->setModel(thyraModel);
170
171 Teuchos::RCP< ::Thyra::VectorBase<double> >
172 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
173
174 ::Thyra::SolveCriteria<double> solve_criteria;
175 ::Thyra::SolveStatus<double> solve_status;
176
177 solve_status = solver->solve(initial_guess.get(), &solve_criteria);
178
179 // Test total num iterations
180 {
181 // Problem converges in >20 nonlinear iterations with NO scaling
182 // Problem converges in 1 nonlinear iterations with scaling
183 Teuchos::RCP< ::Thyra::NOXNonlinearSolver> thyra_nox_solver =
184 Teuchos::rcp_dynamic_cast< ::Thyra::NOXNonlinearSolver>(solver);
185 TEST_EQUALITY(thyra_nox_solver->getNOXSolver()->getNumIterations(), 1);
186 }
187 }
188
189
190 TEUCHOS_UNIT_TEST(NOX_Thyra_RightScaling, CTOR1_withRowSum)
191 {
192 // Create a communicator for Epetra objects
193 #ifdef HAVE_MPI
194 Epetra_MpiComm Comm( MPI_COMM_WORLD );
195 #else
196 Epetra_SerialComm Comm;
197 #endif
198
199 // Check we have only one processor since this problem doesn't work
200 // for more than one proc
201 TEST_ASSERT(Comm.NumProc() == 1);
202
203 // Create the model evaluator object
204 double d = 1.0;
205 double p0 = 1.0e6;
206 double p1 = -1.0e-6;
207 double x00 = 0.0;
208 double x01 = 0.0;
209 Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel =
210 Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),
211 d,p0,p1,x00,x01));
212
213 ::Stratimikos::DefaultLinearSolverBuilder builder;
214
215 Teuchos::RCP<Teuchos::ParameterList> p =
216 Teuchos::rcp(new Teuchos::ParameterList);
217 {
218 p->set("Linear Solver Type", "AztecOO");
219 //p->set("Preconditioner Type", "Ifpack");
220 p->set("Preconditioner Type", "None");
221 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
222 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
223 az.sublist("VerboseObject").set("Verbosity Level", "high");
224 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
225 ip.sublist("VerboseObject").set("Verbosity Level", "high");
226 }
227
228 builder.setParameterList(p);
229
230 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
231 lowsFactory = builder.createLinearSolveStrategy("");
232
233 thyraModel->set_W_factory(lowsFactory);
234
235 // Create nox parameter list
236 Teuchos::RCP<Teuchos::ParameterList> nl_params =
237 Teuchos::rcp(new Teuchos::ParameterList);
238 nl_params->set("Nonlinear Solver", "Line Search Based");
239 nl_params->sublist("Line Search").set("Method", "Full Step");
240
241 Teuchos::ParameterList& printParams = nl_params->sublist("Printing");
242 printParams.set("Output Information",
243 NOX::Utils::OuterIteration +
244 NOX::Utils::OuterIterationStatusTest +
245 NOX::Utils::InnerIteration +
246 NOX::Utils::LinearSolverDetails +
247 NOX::Utils::Parameters +
248 NOX::Utils::Details +
249 NOX::Utils::Warning +
250 NOX::Utils::Debug +
251 NOX::Utils::TestDetails +
252 NOX::Utils::Error);
253
254 nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete");
255
256 // Enable row sum scaling
257 nl_params->sublist("Thyra Group Options").set("Function Scaling", "Row Sum");
258
259 // Create right scaling vector
260 Teuchos::RCP<Thyra::VectorBase<double> > values = Thyra::createMember(thyraModel->get_x_space());
261 Thyra::put_scalar(0.0,values.ptr());
262
263 // Values chosen such that WRMS convergence is achieved in 1 step
264 // Unscaled will not converge in less than 20 steps
265 Thyra::set_ele(0,1.0e14,values.ptr());
266 Thyra::set_ele(1,1.0e2,values.ptr());
267 Teuchos::RCP<NOX::Abstract::Vector > right_scaling = Teuchos::rcp<NOX::Abstract::Vector>(new NOX::Thyra::Vector(values));
268
269 // Insert scaling vector into NOX parameter list
270 Teuchos::ParameterEntry entry(right_scaling);
271 nl_params->setEntry("Right Scaling Vector", entry);
272 nl_params->sublist("Thyra Group Options").set<bool>("Do Right Scaling First", true);
273
274 // Create Status Tests
275 {
276 Teuchos::ParameterList& st = nl_params->sublist("Status Tests");
277 st.set("Test Type", "Combo");
278 st.set("Combo Type", "OR");
279 st.set("Number of Tests", 3);
280
281 {
282 Teuchos::ParameterList& normWRMS = st.sublist("Test 0");
283 normWRMS.set("Test Type", "NormWRMS");
284 normWRMS.set("Absolute Tolerance", 2.0e-8);
285 normWRMS.set("Relative Tolerance", 1.0e-5);
286 normWRMS.set("Tolerance", 1.0);
287 normWRMS.set("BDF Multiplier", 1.0);
288 normWRMS.set("Alpha", 1.0);
289 normWRMS.set("Beta", 0.5);
290 normWRMS.set("Disable Implicit Weighting", true);
291 }
292
293 {
294 Teuchos::ParameterList& fv = st.sublist("Test 1");
295 fv.set("Test Type", "FiniteValue");
296 fv.set("Vector Type", "F Vector");
297 fv.set("Norm Type", "Two Norm");
298 }
299
300 {
301 Teuchos::ParameterList& maxiters = st.sublist("Test 2");
302 maxiters.set("Test Type", "MaxIters");
303 maxiters.set("Maximum Iterations", 20);
304 }
305
306 }
307
308 // Create a Thyra nonlinear solver
309 Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver =
310 Teuchos::rcp(new ::Thyra::NOXNonlinearSolver);
311
312 solver->setParameterList(nl_params);
313 solver->setModel(thyraModel);
314
315 Teuchos::RCP< ::Thyra::VectorBase<double> >
316 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
317
318 ::Thyra::SolveCriteria<double> solve_criteria;
319 ::Thyra::SolveStatus<double> solve_status;
320
321 solve_status = solver->solve(initial_guess.get(), &solve_criteria);
322
323 // Test total num iterations
324 {
325 // Problem converges in >20 nonlinear iterations with NO scaling
326 // Problem converges in 1 nonlinear iterations with scaling
327 Teuchos::RCP< ::Thyra::NOXNonlinearSolver> thyra_nox_solver =
328 Teuchos::rcp_dynamic_cast< ::Thyra::NOXNonlinearSolver>(solver);
329 TEST_EQUALITY(thyra_nox_solver->getNOXSolver()->getNumIterations(), 1);
330 }
331 }
332
333
334 TEUCHOS_UNIT_TEST(NOX_Thyra_RightScaling, CTOR2)
335 {
336 // Create a communicator for Epetra objects
337 #ifdef HAVE_MPI
338 Epetra_MpiComm Comm( MPI_COMM_WORLD );
339 #else
340 Epetra_SerialComm Comm;
341 #endif
342
343 // Check we have only one processor since this problem doesn't work
344 // for more than one proc
345 TEST_ASSERT(Comm.NumProc() == 1);
346
347 // Create the model evaluator object
348 double d = 1.0;
349 double p0 = 1.0e6;
350 double p1 = -1.0e-6;
351 double x00 = 0.0;
352 double x01 = 0.0;
353 Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel =
354 Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),
355 d,p0,p1,x00,x01));
356
357 ::Stratimikos::DefaultLinearSolverBuilder builder;
358
359 Teuchos::RCP<Teuchos::ParameterList> p =
360 Teuchos::rcp(new Teuchos::ParameterList);
361 {
362 p->set("Linear Solver Type", "AztecOO");
363 //p->set("Preconditioner Type", "Ifpack");
364 p->set("Preconditioner Type", "None");
365 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
366 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
367 az.sublist("VerboseObject").set("Verbosity Level", "high");
368 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
369 ip.sublist("VerboseObject").set("Verbosity Level", "high");
370 }
371
372 builder.setParameterList(p);
373
374 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
375 lowsFactory = builder.createLinearSolveStrategy("");
376
377 thyraModel->set_W_factory(lowsFactory);
378
379 // Create nox parameter list
380 Teuchos::RCP<Teuchos::ParameterList> nl_params =
381 Teuchos::rcp(new Teuchos::ParameterList);
382 nl_params->set("Nonlinear Solver", "Line Search Based");
383 nl_params->sublist("Line Search").set("Method", "Full Step");
384
385 Teuchos::ParameterList& printParams = nl_params->sublist("Printing");
386 printParams.set("Output Information",
387 NOX::Utils::OuterIteration +
388 NOX::Utils::OuterIterationStatusTest +
389 NOX::Utils::InnerIteration +
390 NOX::Utils::LinearSolverDetails +
391 NOX::Utils::Parameters +
392 NOX::Utils::Details +
393 NOX::Utils::Warning +
394 NOX::Utils::Debug +
395 NOX::Utils::TestDetails +
396 NOX::Utils::Error);
397
398 nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete");
399
400 // Enable row sum scaling
401 nl_params->sublist("Thyra Group Options").set("Function Scaling", "None");
402
403 // Create right scaling vector
404 Teuchos::RCP<Thyra::VectorBase<double> > values = Thyra::createMember(thyraModel->get_x_space());
405 Thyra::put_scalar(0.0,values.ptr());
406
407 // Values chosen such that WRMS convergence is achieved in 1 step
408 // Unscaled will not converge in less than 20 steps
409 Thyra::set_ele(0,1.0e14,values.ptr());
410 Thyra::set_ele(1,1.0e2,values.ptr());
411 Teuchos::RCP<NOX::Abstract::Vector > right_scaling = Teuchos::rcp<NOX::Abstract::Vector>(new NOX::Thyra::Vector(values));
412
413 // Create Status Tests
414 Teuchos::RCP<NOX::StatusTest::Generic> status_tests;
415 {
416 Teuchos::RCP<Teuchos::ParameterList> st =
417 Teuchos::parameterList("Status Tests");
418 st->set("Test Type", "Combo");
419 st->set("Combo Type", "OR");
420 st->set("Number of Tests", 3);
421
422 {
423 Teuchos::ParameterList& normWRMS = st->sublist("Test 0");
424 normWRMS.set("Test Type", "NormWRMS");
425 normWRMS.set("Absolute Tolerance", 2.0e-8);
426 normWRMS.set("Relative Tolerance", 1.0e-5);
427 normWRMS.set("Tolerance", 1.0);
428 normWRMS.set("BDF Multiplier", 1.0);
429 normWRMS.set("Alpha", 1.0);
430 normWRMS.set("Beta", 0.5);
431 normWRMS.set("Disable Implicit Weighting", true);
432 }
433
434 {
435 Teuchos::ParameterList& fv = st->sublist("Test 1");
436 fv.set("Test Type", "FiniteValue");
437 fv.set("Vector Type", "F Vector");
438 fv.set("Norm Type", "Two Norm");
439 }
440
441 {
442 Teuchos::ParameterList& maxiters = st->sublist("Test 2");
443 maxiters.set("Test Type", "MaxIters");
444 maxiters.set("Maximum Iterations", 20);
445 }
446 // Create a print class for controlling output below
447 nl_params->sublist("Printing").set("MyPID", Comm.MyPID());
448 nl_params->sublist("Printing").set("Output Precision", 3);
449 nl_params->sublist("Printing").set("Output Processor", 0);
450 NOX::Utils printing(nl_params->sublist("Printing"));
451
452 NOX::StatusTest::Factory st_factory;
453 status_tests = st_factory.buildStatusTests(*st,printing);
454 }
455
456 Teuchos::RCP< ::Thyra::VectorBase<double> >
457 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
458
459 Teuchos::RCP<NOX::Thyra::Vector> noxThyraRightScaleVec =
460 Teuchos::rcp_dynamic_cast<NOX::Thyra::Vector>(right_scaling);
461
462 Teuchos::RCP<NOX::Thyra::Group> nox_group =
463 Teuchos::rcp(new NOX::Thyra::Group(*initial_guess,
464 thyraModel,
465 thyraModel->create_W_op(),
466 lowsFactory,
467 thyraModel->create_W_prec(),
468 lowsFactory->getPreconditionerFactory(),
469 Teuchos::null,
470 noxThyraRightScaleVec->getThyraRCPVector()
471 ));
472
473 Teuchos::RCP<NOX::Solver::Generic> solver =
474 NOX::Solver::buildSolver(nox_group, status_tests, nl_params);
475 NOX::StatusTest::StatusType solveStatus = solver->solve();
476
477 TEST_ASSERT(solveStatus == NOX::StatusTest::Converged);
478
479 // Test total num iterations
480 {
481 // Problem converges in >20 nonlinear iterations with NO scaling
482 // Problem converges in 1 nonlinear iterations with scaling
483 TEST_EQUALITY(solver->getNumIterations(), 1);
484 }
485 }
486