| 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 |
|
| 170 |
auto loca_parser = Teuchos::rcp(new LOCA::Parameter::SublistParser(global_data)); |
| 171 |
loca_parser->parseSublists(pList); |
| 172 |
|
| 173 |
std::vector<int> param_ids(2); |
| 174 |
param_ids[0] = 0; |
| 175 |
param_ids[1] = 1; |
| 176 |
auto constraint_list_ptr = Teuchos::rcpFromRef(constraint_list); |
| 177 |
Teuchos::RCP<LOCA::MultiContinuation::ConstrainedGroup> loca_constrained_group = |
| 178 |
Teuchos::rcp(new LOCA::MultiContinuation::ConstrainedGroup(global_data, |
| 179 |
loca_parser, |
| 180 |
constraint_list_ptr, |
| 181 |
loca_group, |
| 182 |
constraints, |
| 183 |
param_ids, |
| 184 |
false)); |
| 185 |
|
| 186 |
loca_constrained_group->computeF(); |
| 187 |
|
| 188 |
// Create the NOX status tests and the solver |
| 189 |
// Create the convergence tests |
| 190 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 191 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 192 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 193 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 194 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 195 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 196 |
converged->addStatusTest(absresid); |
| 197 |
converged->addStatusTest(wrms); |
| 198 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 199 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(10)); |
| 200 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 201 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 202 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 203 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 204 |
combo->addStatusTest(fv); |
| 205 |
combo->addStatusTest(converged); |
| 206 |
combo->addStatusTest(maxiters); |
| 207 |
|
| 208 |
// Create the solver |
| 209 |
// auto solver = NOX::Solver::buildSolver(nox_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 210 |
// auto solver = NOX::Solver::buildSolver(loca_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 211 |
auto solver = NOX::Solver::buildSolver(loca_constrained_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 212 |
|
| 213 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 214 |
|
| 215 |
// Output |
| 216 |
{ |
| 217 |
Teuchos::TimeMonitor::getStackedTimer()->stopBaseTimer(); |
| 218 |
Teuchos::StackedTimer::OutputOptions options; |
| 219 |
options.output_fraction = true; |
| 220 |
options.output_minmax = true; |
| 221 |
Teuchos::TimeMonitor::getStackedTimer()->report(out,comm,options); |
| 222 |
} |
| 223 |
|
| 224 |
// Write solution to file |
| 225 |
const bool printSolution = true; |
| 226 |
if (printSolution) { |
| 227 |
for (int i=0; i < comm->getSize(); ++i) { |
| 228 |
if (comm->getRank() == i) { |
| 229 |
std::ofstream file; |
| 230 |
if (comm->getRank() == 0) |
| 231 |
file.open("householder_solution.txt",std::ios::trunc); |
| 232 |
else |
| 233 |
file.open("householder_solution.txt",std::ios::app); |
| 234 |
|
| 235 |
const auto& final_x = solver->getSolutionGroup().getX(); |
| 236 |
const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(final_x).getXVec()); |
| 237 |
const auto& final_x_thyra = dynamic_cast<const NOX::Thyra::Vector&>(final_x_nox).getThyraVector(); |
| 238 |
const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector()); |
| 239 |
auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const); |
| 240 |
const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly); |
| 241 |
for (size_t j=0; j < final_x_view.extent(0); ++j) |
| 242 |
file << final_x_view(j,0) << std::endl; |
| 243 |
} |
| 244 |
comm->barrier(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 249 |
TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearIterations,5); |
| 250 |
|
| 251 |
// Check final values |
| 252 |
{ |
| 253 |
const auto& group = solver->getSolutionGroup(); |
| 254 |
const auto& c_group = dynamic_cast<const LOCA::MultiContinuation::ConstrainedGroup&>(group); |
| 255 |
|
| 256 |
out << "\nFinal Parameter Value for \"k\" = " << std::setprecision(10) << c_group.getParam(0) << std::endl; |
| 257 |
out << "Final Parameter Value for \"T_left\" = " << std::setprecision(10) << c_group.getParam(1) << std::endl; |
| 258 |
|
| 259 |
const double tol = 1.0e-8; |
| 260 |
// Check parameter values |
| 261 |
TEST_FLOATING_EQUALITY(c_group.getParam(0),-0.5993277206,tol); |
| 262 |
TEST_FLOATING_EQUALITY(c_group.getParam(1),1.0,tol); |
| 263 |
// Check constraint values |
| 264 |
{ |
| 265 |
// get the solution |
| 266 |
const auto& final_x = solver->getSolutionGroup().getX(); |
| 267 |
const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(final_x).getXVec()); |
| 268 |
const auto& final_x_thyra = dynamic_cast<const NOX::Thyra::Vector&>(final_x_nox).getThyraVector(); |
| 269 |
const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector()); |
| 270 |
auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const); |
| 271 |
const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly); |
| 272 |
|
| 273 |
// Left T value |
| 274 |
if (comm->getRank() == 0) { |
| 275 |
TEST_FLOATING_EQUALITY(final_x_view(0,0),1.0,tol); |
| 276 |
} |
| 277 |
// Right T value |
| 278 |
if (comm->getRank() == (comm->getSize()-1)) { |
| 279 |
TEST_FLOATING_EQUALITY(final_x_view(final_x_view.extent(0)-1,0),2.0,tol); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Breaks RCP cyclic dependency |
| 285 |
LOCA::destroyGlobalData(global_data); |
| 286 |
} |