Debian Codegraph @

packages/nox/test/epetra/Thyra/Thyra_Rosenbrock_SafeguardedStepLineSearch.cpp

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 #include <Teuchos_ConfigDefs.hpp>
11 #include <Teuchos_UnitTestHarness.hpp>
12 #include <Teuchos_TimeMonitor.hpp>
13
14 // NOX Objects
15 #include "NOX.H"
16 #include "NOX_Thyra.H"
17
18 // Trilinos Objects
19 #ifdef HAVE_MPI
20 #include "Epetra_MpiComm.h"
21 #else
22 #include "Epetra_SerialComm.h"
23 #endif
24 #include "Epetra_Map.h"
25 #include "Epetra_Vector.h"
26 #include "Epetra_RowMatrix.h"
27 #include "Epetra_CrsMatrix.h"
28 #include "Epetra_Map.h"
29 #include "Epetra_LinearProblem.h"
30 #include "AztecOO.h"
31
32 #include "Teuchos_ParameterList.hpp"
33 #include "Teuchos_Assert.hpp"
34
35 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
36 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
37 #include "Thyra_ScaledModelEvaluator.hpp"
38 #include "ModelEvaluatorRosenbrock.hpp"
39 #include "Thyra_SpmdVectorBase.hpp"
40 #include "Thyra_DefaultSpmdVectorSpace.hpp"
41
42 #include "NOX_PrePostOperator_RowSumScaling.H"
43 #include "Thyra_VectorBase.hpp"
44
45 #include "NOX_TOpEleWiseMinSwap.hpp"
46
47 using namespace std;
48
49 TEUCHOS_UNIT_TEST(rosenbrock, user_limiting_ls)
50 {
51 int status = 0;
52
53 // Create a communicator for Epetra objects
54 #ifdef HAVE_MPI
55 Epetra_MpiComm Comm( MPI_COMM_WORLD );
56 #else
57 Epetra_SerialComm Comm;
58 #endif
59
60 TEST_ASSERT(Comm.NumProc() == 1);
61
62 ::Stratimikos::DefaultLinearSolverBuilder builder;
63
64 Teuchos::RCP<Teuchos::ParameterList> p =
65 Teuchos::rcp(new Teuchos::ParameterList);
66 {
67 p->set("Linear Solver Type", "AztecOO");
68 //p->set("Preconditioner Type", "Ifpack");
69 p->set("Preconditioner Type", "None");
70 Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO");
71 az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1);
72 az.sublist("VerboseObject").set("Verbosity Level", "high");
73 Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack");
74 ip.sublist("VerboseObject").set("Verbosity Level", "high");
75 }
76
77 builder.setParameterList(p);
78
79 Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> >
80 lowsFactory = builder.createLinearSolveStrategy("");
81
82 Teuchos::RCP<RosenbrockModelEvaluator> thyraModel =
83 Teuchos::rcp(new RosenbrockModelEvaluator(Teuchos::rcp(&Comm,false)));
84
85 thyraModel->set_W_factory(lowsFactory);
86
87 // Create nox parameter list
88 Teuchos::RCP<Teuchos::ParameterList> nl_params =
89 Teuchos::rcp(new Teuchos::ParameterList);
90 nl_params->set("Nonlinear Solver", "Line Search Based");
91 nl_params->sublist("Line Search").set("Method", "Safeguarded Step");
92 Teuchos::RCP< ::Thyra::VectorBase<double> > updateLimits = ::Thyra::createMember<double>(thyraModel->get_x_space());
93 ::Thyra::put_scalar(Teuchos::ScalarTraits<double>::rmax(),updateLimits.ptr());
94 ::Thyra::set_ele(1,0.5,updateLimits.ptr());
95 Teuchos::RCP<NOX::Abstract::Vector> noxUpdateLimits = Teuchos::rcp(new NOX::Thyra::Vector(updateLimits));
96 nl_params->sublist("Line Search").sublist("Safeguarded Step").set("Update Limit Vector",noxUpdateLimits);
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", "Row Sum");
115
116 // Create Status Tests
117 {
118 Teuchos::ParameterList& st = nl_params->sublist("Status Tests");
119 st.set("Test Type", "Combo");
120 st.set("Combo Type", "OR");
121 st.set("Number of Tests", 3);
122
123 {
124 Teuchos::ParameterList& conv = st.sublist("Test 0");
125 conv.set("Test Type", "Combo");
126 conv.set("Combo Type", "AND");
127 conv.set("Number of Tests", 2);
128
129 Teuchos::ParameterList& normF_rel = conv.sublist("Test 0");
130 normF_rel.set("Test Type", "NormF");
131 normF_rel.set("Tolerance", 1.0e-8);
132
133 Teuchos::ParameterList& normWRMS = conv.sublist("Test 1");
134 normWRMS.set("Test Type", "NormWRMS");
135 normWRMS.set("Absolute Tolerance", 1.0e-8);
136 normWRMS.set("Relative Tolerance", 1.0e-5);
137 normWRMS.set("Tolerance", 1.0);
138 normWRMS.set("BDF Multiplier", 1.0);
139 normWRMS.set("Alpha", 1.0);
140 normWRMS.set("Beta", 0.5);
141 normWRMS.set("Disable Implicit Weighting", true);
142 }
143
144 {
145 Teuchos::ParameterList& fv = st.sublist("Test 1");
146 fv.set("Test Type", "FiniteValue");
147 fv.set("Vector Type", "F Vector");
148 fv.set("Norm Type", "Two Norm");
149 }
150
151 {
152 Teuchos::ParameterList& maxiters = st.sublist("Test 2");
153 maxiters.set("Test Type", "MaxIters");
154 maxiters.set("Maximum Iterations", 20);
155 }
156
157 }
158
159 // Create a Thyra nonlinear solver
160 Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver =
161 Teuchos::rcp(new ::Thyra::NOXNonlinearSolver);
162
163 solver->setParameterList(nl_params);
164 solver->setModel(thyraModel);
165
166 Teuchos::RCP< ::Thyra::VectorBase<double> >
167 initial_guess = thyraModel->getNominalValues().get_x()->clone_v();
168
169 ::Thyra::SolveCriteria<double> solve_criteria;
170 ::Thyra::SolveStatus<double> solve_status;
171
172 solve_status = solver->solve(initial_guess.get(), &solve_criteria);
173
174
175 Teuchos::RCP< ::Thyra::NOXNonlinearSolver> thyra_nox_solver =
176 Teuchos::rcp_dynamic_cast< ::Thyra::NOXNonlinearSolver>(solver);
177 TEST_EQUALITY(thyra_nox_solver->getNOXSolver()->getNumIterations(), 8);
178
179 Teuchos::RCP<const Epetra_Vector> x_analytic = thyraModel->get_analytic_solution();
180
181 Teuchos::RCP<const NOX::Abstract::Vector> x = thyra_nox_solver->getNOXSolver()->getSolutionGroup().getXPtr();
182
183 Teuchos::RCP<const NOX::Thyra::Vector> nox_thyra_x =
184 Teuchos::rcp_dynamic_cast<const NOX::Thyra::Vector>(x,true);
185
186 Teuchos::RCP<const Thyra::SpmdVectorBase<double> > spmd_x =
187 Teuchos::rcp_dynamic_cast<const Thyra::SpmdVectorBase<double> >(nox_thyra_x->getThyraRCPVector(),true);
188
189 Teuchos::ArrayRCP<const double> local_values;
190 spmd_x->getLocalData(outArg(local_values));
191
192 double tol = 1.0e-8;
193 TEST_FLOATING_EQUALITY((*x_analytic)[0],local_values[0],tol);
194 TEST_FLOATING_EQUALITY((*x_analytic)[1],local_values[1],tol);
195
196 if (solve_status.solveStatus == ::Thyra::SOLVE_STATUS_CONVERGED)
197 std::cout << "Test passed!" << std::endl;
198
199 // std::cout << *p << std::endl;
200
201 Teuchos::TimeMonitor::summarize();
202
203 // Final return value (0 = successfull, non-zero = failure)
204 TEST_ASSERT(status == 0);
205 }