Debian Codegraph @

packages/nox/test/tpetra/tTpetra_HouseholderBorderedSolve_WithUVInPrec.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 #include "Teuchos_ConfigDefs.hpp"
10 #include "Teuchos_UnitTestHarness.hpp"
11 #include "Teuchos_StackedTimer.hpp"
12
13 // NOX Objects
14 #include "NOX.H"
15 #include "NOX_Thyra.H"
16
17 // Trilinos Objects
18 #include "Teuchos_Comm.hpp"
19 #include "Teuchos_ParameterList.hpp"
20 #include "Teuchos_RCP.hpp"
21 #include "Teuchos_FancyOStream.hpp"
22 #include "Teuchos_AbstractFactoryStd.hpp"
23
24 #include "Tpetra_Core.hpp"
25 #include "Tpetra_Vector.hpp"
26
27 #include "BelosTypes.hpp"
28 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
29 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
30 #include "ME_Tpetra_1DFEM.hpp"
31
32 #include "NOX_Thyra_MatrixFreeJacobianOperator.hpp"
33 #include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp"
34 #include "NOX_TpetraTypedefs.hpp"
35 #include "LOCA_Tpetra_Factory.hpp"
36 #include "LOCA_Thyra_Group.H"
37 #include "LOCA_MultiContinuation_ConstrainedGroup.H"
38 #include "LOCA_Tpetra_ConstraintModelEvaluator.hpp"
39 #include "LOCA_Parameter_SublistParser.H"
40 #include "NOX_SolverStats.hpp"
41
42 // For solution io
43 #include "Thyra_TpetraVector.hpp"
44 #include <iostream>
45 #include <fstream>
46
47 TEUCHOS_UNIT_TEST(NOX_Tpetra_Householder, BasicSolve)
48 {
49 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
50
51 // Get default Tpetra template types
52 using Scalar = NOX::Scalar;
53 using LO = NOX::LocalOrdinal;
54 using GO = NOX::GlobalOrdinal;
55 using Node = NOX::NodeType;
56
57 // Create the model evaluator object
58 Scalar x00 = 0.0;
59 Scalar x01 = 1.0;
60 const Tpetra::global_size_t numGlobalElements = 100;
61 Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model =
62 evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01);
63
64 // Create the linear solver and register on model evaluator
65 {
66 Stratimikos::DefaultLinearSolverBuilder builder;
67
68 Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList();
69 p->set("Linear Solver Type", "Belos");
70 Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos");
71 belosList.set("Solver Type", "Pseudo Block GMRES");
72 belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200);
73 belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200);
74 belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", Belos::Errors+Belos::IterationDetails+Belos::FinalSummary);
75 belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 5);
76 belosList.sublist("VerboseObject").set("Verbosity Level", "medium");
77 p->set("Preconditioner Type", "Ifpack2");
78 // p->set("Preconditioner Type", "None");
79 Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2");
80 ifpackList.set("Prec Type", "ILUT");
81
82 builder.setParameterList(p);
83
84 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
85 lowsFactory = builder.createLinearSolveStrategy("");
86
87 model->set_W_factory(lowsFactory);
88 }
89
90 // Create the initial guess
91 Teuchos::RCP<Thyra::VectorBase<Scalar> >
92 initial_guess = model->getNominalValues().get_x()->clone_v();
93 Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one());
94
95 // Create top level nox/loca solver parameter list
96 Teuchos::RCP<Teuchos::ParameterList> pList = Teuchos::parameterList("Top Level");
97
98 // Create nox parameter list
99 auto& nl_params = pList->sublist("NOX");
100 nl_params.set("Nonlinear Solver", "Line Search Based");
101 nl_params.sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-8);
102 auto& ls_params = nl_params.sublist("Line Search");
103 ls_params.set("Method","Full Step");
104 auto& output_list = nl_params.sublist("Printing").sublist("Output Information");
105 output_list.set("Debug",true);
106 output_list.set("Warning",true);
107 output_list.set("Error",true);
108 output_list.set("Test Details",true);
109 output_list.set("Details",true);
110 output_list.set("Parameters",true);
111 output_list.set("Linear Solver Details",true);
112 output_list.set("Inner Iteration",true);
113 output_list.set("Outer Iteration",true);
114 output_list.set("Outer Iteration StatusTest",true);
115
116 // Create the LOCA Group:
117 // (NOX::Thyra::Group-->LOCA::Thyra::Group-->LOCA::Constrained::Group)
118 // For Tpetra Householder, we need to actively set the
119 // preconditioner and preconditioner factory so that it uses the
120 // precOp separate from the Jacobian operator. Householder replaces
121 // the Jacobian operator with a matrix-free version that has the
122 // uv^T tacked on.
123 auto explicit_jacobian = model->create_W_op();
124 auto prec_matrix = Teuchos::rcp(new Thyra::DefaultPreconditioner<NOX::Scalar>(Teuchos::null,explicit_jacobian));
125 TEST_ASSERT(nonnull(model->get_W_factory()->getPreconditionerFactory()));
126 Teuchos::RCP<NOX::Thyra::Group> nox_group =
127 Teuchos::rcp(new NOX::Thyra::Group(*initial_guess,
128 model,
129 explicit_jacobian,
130 model->get_W_factory(),
131 prec_matrix, // Reuse Jac for approx preconditioner
132 model->get_W_factory()->getPreconditionerFactory(),
133 Teuchos::null));
134
135 Teuchos::RCP<LOCA::Abstract::Factory> tpetra_factory = Teuchos::rcp(new LOCA::Tpetra::Factory);
136
137 Teuchos::RCP<LOCA::GlobalData> global_data = LOCA::createGlobalData(pList, tpetra_factory);
138
139 Teuchos::RCP<LOCA::ParameterVector> p_vec = Teuchos::rcp(new LOCA::ParameterVector);
140 p_vec->addParameter("k", 1.0); // Source term multiplier
141 p_vec->addParameter("T_left", 1.2); // Source term multiplier
142
143 std::vector<int> me_p_indices;
144 me_p_indices.push_back(2);
145 me_p_indices.push_back(4);
146 Teuchos::RCP<LOCA::Thyra::Group> loca_group = Teuchos::rcp(new LOCA::Thyra::Group(global_data,
147 *nox_group,
148 *p_vec,
149 me_p_indices));
150
151 auto g_names = Teuchos::rcp(new std::vector<std::string>);
152 g_names->push_back("Constraint: T_right=2");
153 g_names->push_back("Constraint: 2*T_left=T_right");
154 auto x_thyra = ::Thyra::createMember(model->get_x_space(),"x");
155 NOX::Thyra::Vector x(x_thyra);
156 auto constraints = Teuchos::rcp(new LOCA::MultiContinuation::ConstraintModelEvaluator(model,*p_vec,*g_names,x));
157
158 // Set initial parameter conditions
159 constraints->setX(x);
160 constraints->setParam(0,1.0);
161 constraints->setParam(1,1.2);
162
163 // Create the constraints list
164 auto& locaParamsList = pList->sublist("LOCA");
165 auto& constraint_list = locaParamsList.sublist("Constraints");
166 constraint_list.set("Bordered Solver Method", "Householder");
167 constraint_list.set<Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface>>("Constraint Object", constraints);
168 constraint_list.set("Constraint Parameter Names", g_names);
169 constraint_list.set("Include UV In Preconditioner",true);
170
171 auto loca_parser = Teuchos::rcp(new LOCA::Parameter::SublistParser(global_data));
172 loca_parser->parseSublists(pList);
173
174 std::vector<int> param_ids(2);
175 param_ids[0] = 0;
176 param_ids[1] = 1;
177 auto constraint_list_ptr = Teuchos::rcpFromRef(constraint_list);
178 Teuchos::RCP<LOCA::MultiContinuation::ConstrainedGroup> loca_constrained_group =
179 Teuchos::rcp(new LOCA::MultiContinuation::ConstrainedGroup(global_data,
180 loca_parser,
181 constraint_list_ptr,
182 loca_group,
183 constraints,
184 param_ids,
185 false));
186
187 loca_constrained_group->computeF();
188
189 // Create the NOX status tests and the solver
190 // Create the convergence tests
191 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
192 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
193 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
194 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
195 Teuchos::RCP<NOX::StatusTest::Combo> converged =
196 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
197 converged->addStatusTest(absresid);
198 converged->addStatusTest(wrms);
199 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
200 Teuchos::rcp(new NOX::StatusTest::MaxIters(10));
201 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
202 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
203 Teuchos::RCP<NOX::StatusTest::Combo> combo =
204 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
205 combo->addStatusTest(fv);
206 combo->addStatusTest(converged);
207 combo->addStatusTest(maxiters);
208
209 // Create the solver
210 // auto solver = NOX::Solver::buildSolver(nox_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX")));
211 // auto solver = NOX::Solver::buildSolver(loca_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX")));
212 auto solver = NOX::Solver::buildSolver(loca_constrained_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX")));
213
214 NOX::StatusTest::StatusType solvStatus = solver->solve();
215
216 // Output
217 {
218 Teuchos::TimeMonitor::getStackedTimer()->stopBaseTimer();
219 Teuchos::StackedTimer::OutputOptions options;
220 options.output_fraction = true;
221 options.output_minmax = true;
222 Teuchos::TimeMonitor::getStackedTimer()->report(out,comm,options);
223 }
224
225 // Write solution to file
226 const bool printSolution = true;
227 if (printSolution) {
228 for (int i=0; i < comm->getSize(); ++i) {
229 if (comm->getRank() == i) {
230 std::ofstream file;
231 if (comm->getRank() == 0)
232 file.open("householder_solution.txt",std::ios::trunc);
233 else
234 file.open("householder_solution.txt",std::ios::app);
235
236 const auto& final_x = solver->getSolutionGroup().getX();
237 const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(final_x).getXVec());
238 const auto& final_x_thyra = dynamic_cast<const NOX::Thyra::Vector&>(final_x_nox).getThyraVector();
239 const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector());
240 auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const);
241 const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly);
242 for (size_t j=0; j < final_x_view.extent(0); ++j)
243 file << final_x_view(j,0) << std::endl;
244 }
245 comm->barrier();
246 }
247 }
248
249 TEST_ASSERT(solvStatus == NOX::StatusTest::Converged);
250 TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearIterations,5);
251
252 // Check final values
253 {
254 const auto& group = solver->getSolutionGroup();
255 const auto& c_group = dynamic_cast<const LOCA::MultiContinuation::ConstrainedGroup&>(group);
256
257 out << "\nFinal Parameter Value for \"k\" = " << std::setprecision(10) << c_group.getParam(0) << std::endl;
258 out << "Final Parameter Value for \"T_left\" = " << std::setprecision(10) << c_group.getParam(1) << std::endl;
259
260 const double tol = 1.0e-3;
261 // Check parameter values
262 TEST_FLOATING_EQUALITY(c_group.getParam(0),-0.5993277206,tol);
263 TEST_FLOATING_EQUALITY(c_group.getParam(1),1.0,tol);
264 // Check constraint values
265 {
266 // get the solution
267 const auto& final_x = solver->getSolutionGroup().getX();
268 const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(final_x).getXVec());
269 const auto& final_x_thyra = dynamic_cast<const NOX::Thyra::Vector&>(final_x_nox).getThyraVector();
270 const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector());
271 auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const);
272 const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly);
273
274 // Left T value
275 if (comm->getRank() == 0) {
276 TEST_FLOATING_EQUALITY(final_x_view(0,0),1.0,tol);
277 }
278 // Right T value
279 if (comm->getRank() == (comm->getSize()-1)) {
280 TEST_FLOATING_EQUALITY(final_x_view(final_x_view.extent(0)-1,0),2.0,tol);
281 }
282 }
283 }
284
285 // Breaks RCP cyclic dependency
286 LOCA::destroyGlobalData(global_data);
287 }