| 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 "LOCA_Stepper.H" |
| 41 |
#include "NOX_SolverStats.hpp" |
| 42 |
#include "NOX_Observer.hpp" |
| 43 |
#include "NOX_Observer_Vector.cpp" |
| 44 |
|
| 45 |
// For solution io |
| 46 |
#include "Thyra_TpetraVector.hpp" |
| 47 |
#include <iostream> |
| 48 |
#include <fstream> |
| 49 |
#include <tuple> |
| 50 |
|
| 51 |
// ************************************************************* |
| 52 |
// ************************************************************* |
| 53 |
// This test does a continuation in the T_left dirichlet BC. A |
| 54 |
// constraint of 2*T_left=T_right is enforced by allowing the source |
| 55 |
// term multiplier k to be adjusted as the independent parameter. |
| 56 |
|
| 57 |
// A nox observer is used to check that the constraint is enforced at |
| 58 |
// each step of the continuation. |
| 59 |
|
| 60 |
// There are two tests. The first does a manual continuation run |
| 61 |
// outside of LOCA. The second test uses LOCA for the continuation. |
| 62 |
// ************************************************************* |
| 63 |
// ************************************************************* |
| 64 |
|
| 65 |
namespace test_utils { |
| 66 |
|
| 67 |
// Returns the solution view from the nox vector. Accounts for |
| 68 |
// potentially wrapped LOCA Vectors. |
| 69 |
auto getConstView(const NOX::Abstract::Vector& nox_vector) |
| 70 |
{ |
| 71 |
const auto* nox_thyra_vector = dynamic_cast<const NOX::Thyra::Vector*>(&nox_vector); |
| 72 |
|
| 73 |
// If the cast above failed, pull it out of the constrained vector. |
| 74 |
if (nox_thyra_vector == nullptr) { |
| 75 |
const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(nox_vector).getXVec()); |
| 76 |
nox_thyra_vector = dynamic_cast<const NOX::Thyra::Vector*>(&final_x_nox); |
| 77 |
|
| 78 |
// If the LOCA::Stepper is building the ConstraintGroup, then there |
| 79 |
// is potentially some double nesting for the arc length constraint |
| 80 |
// depending on if you unroll the nested group. |
| 81 |
if (nox_thyra_vector == nullptr) { |
| 82 |
const auto* loca_nested_extended_vector_ptr = dynamic_cast<const LOCA::MultiContinuation::ExtendedVector*>(&final_x_nox); |
| 83 |
TEUCHOS_ASSERT(loca_nested_extended_vector_ptr != nullptr); |
| 84 |
nox_thyra_vector = Teuchos::rcp_dynamic_cast<const NOX::Thyra::Vector>(loca_nested_extended_vector_ptr->getXVec(),true).get(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
const auto& final_x_thyra = nox_thyra_vector->getThyraVector(); |
| 89 |
const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector()); |
| 90 |
auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const); |
| 91 |
const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly); |
| 92 |
|
| 93 |
return final_x_view; |
| 94 |
} |
| 95 |
|
| 96 |
auto getComm(const NOX::Abstract::Vector& nox_vector) |
| 97 |
{ |
| 98 |
const auto* nox_thyra_vector = dynamic_cast<const NOX::Thyra::Vector*>(&nox_vector); |
| 99 |
|
| 100 |
// If the cast above failed, pull it out of the constrained vector. |
| 101 |
if (nox_thyra_vector == nullptr) { |
| 102 |
const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(nox_vector).getXVec()); |
| 103 |
nox_thyra_vector = dynamic_cast<const NOX::Thyra::Vector*>(&final_x_nox); |
| 104 |
|
| 105 |
// If the LOCA::Stepper is building the ConstraintGroup, then there |
| 106 |
// is potentially some double nesting for the arc length constraint |
| 107 |
// depending on if you unroll the nested group. |
| 108 |
if (nox_thyra_vector == nullptr) { |
| 109 |
const auto* loca_nested_extended_vector_ptr = dynamic_cast<const LOCA::MultiContinuation::ExtendedVector*>(&final_x_nox); |
| 110 |
TEUCHOS_ASSERT(loca_nested_extended_vector_ptr != nullptr); |
| 111 |
nox_thyra_vector = Teuchos::rcp_dynamic_cast<const NOX::Thyra::Vector>(loca_nested_extended_vector_ptr->getXVec(),true).get(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
const auto& final_x_thyra = nox_thyra_vector->getThyraVector(); |
| 116 |
const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector()); |
| 117 |
auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const); |
| 118 |
|
| 119 |
return final_x_tpetra.getMap()->getComm(); |
| 120 |
} |
| 121 |
|
| 122 |
} // namespace test_utils |
| 123 |
|
| 124 |
class WriteParametersObserver : public NOX::Observer { |
| 125 |
bool first_; |
| 126 |
std::string file_name_; |
| 127 |
bool on_print_rank_; |
| 128 |
Teuchos::RCP<LOCA::MultiContinuation::ConstraintModelEvaluator> cme_; |
| 129 |
int w_; |
| 130 |
std::ofstream file_; |
| 131 |
|
| 132 |
public: |
| 133 |
WriteParametersObserver(const std::string& file_name, |
| 134 |
const Teuchos::RCP<const Teuchos::Comm<int>>& comm, |
| 135 |
const Teuchos::RCP<LOCA::MultiContinuation::ConstraintModelEvaluator>& cme) |
| 136 |
: first_(true),file_name_(file_name),on_print_rank_(false), cme_(cme), w_(16) |
| 137 |
{ |
| 138 |
// Make the last rank the print rank because that is where T_right |
| 139 |
// can be found (avoids mpi communication). |
| 140 |
if ( comm->getRank() == (comm->getSize()-1) ) |
| 141 |
on_print_rank_ = true; |
| 142 |
} |
| 143 |
|
| 144 |
~WriteParametersObserver() { file_.close(); } |
| 145 |
|
| 146 |
void runPreSolve(const NOX::Solver::Generic& solver) |
| 147 |
{ |
| 148 |
if (first_ && on_print_rank_) { |
| 149 |
file_.open(file_name_,std::ios::trunc); |
| 150 |
file_ << std::left; |
| 151 |
file_ << std::setw(w_) << "T_left" |
| 152 |
<< std::setw(w_) << "k" |
| 153 |
<< std::setw(w_) << "T_right" |
| 154 |
<< std::setw(w_) << "Error in Constraint" |
| 155 |
<< std::endl; |
| 156 |
first_ = false; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void runPostSolve(const NOX::Solver::Generic& solver) |
| 161 |
{ this->print(solver); } |
| 162 |
|
| 163 |
void print(const NOX::Solver::Generic& solver) { |
| 164 |
if (on_print_rank_) { |
| 165 |
auto vals = test_utils::getConstView(solver.getSolutionGroup().getX()); |
| 166 |
|
| 167 |
file_ << std::setw(w_) << cme_->getParams().getValue("T_left") |
| 168 |
<< std::setw(w_) << cme_->getParams().getValue("k") |
| 169 |
<< std::setw(w_) << vals(vals.extent(0)-1,0) |
| 170 |
<< std::setw(w_) << std::fabs(vals(vals.extent(0)-1,0)-2.0*cme_->getParams().getValue("T_left")) |
| 171 |
<< std::endl; |
| 172 |
} |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
class CheckConstraintObserver : public NOX::Observer { |
| 177 |
void runPostSolve(const NOX::Solver::Generic& solver) |
| 178 |
{ |
| 179 |
auto final_x_view = test_utils::getConstView(solver.getSolutionGroup().getX()); |
| 180 |
auto comm = test_utils::getComm(solver.getSolutionGroup().getX()); |
| 181 |
auto rank = comm->getRank(); |
| 182 |
auto size = comm->getSize(); |
| 183 |
|
| 184 |
double T_left{0.0}; |
| 185 |
double T_right{0.0}; |
| 186 |
if (rank == 0) |
| 187 |
T_left = final_x_view(0,0); |
| 188 |
if (rank == (size-1)) |
| 189 |
T_right = final_x_view( (final_x_view.extent(0)-1) ,0); |
| 190 |
Teuchos::broadcast(*comm,0,1,&T_left); |
| 191 |
Teuchos::broadcast(*comm,size-1,1,&T_right); |
| 192 |
|
| 193 |
if (rank == 0) { |
| 194 |
std::cout << "\nCheck Constraint Observer:\n"; |
| 195 |
std::cout << "T_left = " << T_left << "\n"; |
| 196 |
std::cout << "T_right = " << T_right << "\n\n"; |
| 197 |
} |
| 198 |
|
| 199 |
TEUCHOS_ASSERT(solver.getStatus() == NOX::StatusTest::Converged); |
| 200 |
TEUCHOS_ASSERT( (2.0 * T_left - T_right) < 1.0e-8); |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_Householder, Manual_Continuation) |
| 205 |
{ |
| 206 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 207 |
|
| 208 |
// Get default Tpetra template types |
| 209 |
using Scalar = NOX::Scalar; |
| 210 |
using LO = NOX::LocalOrdinal; |
| 211 |
using GO = NOX::GlobalOrdinal; |
| 212 |
using Node = NOX::NodeType; |
| 213 |
|
| 214 |
// Create the model evaluator object |
| 215 |
Scalar x00 = 0.0; |
| 216 |
Scalar x01 = 1.0; |
| 217 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 218 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 219 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 220 |
|
| 221 |
// Create the linear solver and register on model evaluator |
| 222 |
{ |
| 223 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 224 |
|
| 225 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 226 |
p->set("Linear Solver Type", "Belos"); |
| 227 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 228 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 229 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 230 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 231 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", Belos::Errors+Belos::IterationDetails+Belos::FinalSummary); |
| 232 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 5); |
| 233 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 234 |
p->set("Preconditioner Type", "Ifpack2"); |
| 235 |
// p->set("Preconditioner Type", "None"); |
| 236 |
Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2"); |
| 237 |
ifpackList.set("Prec Type", "ILUT"); |
| 238 |
|
| 239 |
builder.setParameterList(p); |
| 240 |
|
| 241 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 242 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 243 |
|
| 244 |
model->set_W_factory(lowsFactory); |
| 245 |
} |
| 246 |
|
| 247 |
// Create the initial guess |
| 248 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 249 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 250 |
// Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 251 |
Thyra::V_S(initial_guess.ptr(),2.0); |
| 252 |
|
| 253 |
// Create top level nox/loca solver parameter list |
| 254 |
Teuchos::RCP<Teuchos::ParameterList> pList = Teuchos::parameterList("Top Level"); |
| 255 |
|
| 256 |
// Create nox parameter list |
| 257 |
auto& nl_params = pList->sublist("NOX"); |
| 258 |
nl_params.set("Nonlinear Solver", "Line Search Based"); |
| 259 |
nl_params.sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-8); |
| 260 |
auto& ls_params = nl_params.sublist("Line Search"); |
| 261 |
ls_params.set("Method","Full Step"); |
| 262 |
auto& output_list = nl_params.sublist("Printing").sublist("Output Information"); |
| 263 |
output_list.set("Debug",true); |
| 264 |
output_list.set("Warning",true); |
| 265 |
output_list.set("Error",true); |
| 266 |
output_list.set("Test Details",true); |
| 267 |
output_list.set("Details",true); |
| 268 |
output_list.set("Parameters",true); |
| 269 |
output_list.set("Linear Solver Details",true); |
| 270 |
output_list.set("Inner Iteration",true); |
| 271 |
output_list.set("Outer Iteration",true); |
| 272 |
output_list.set("Outer Iteration StatusTest",true); |
| 273 |
|
| 274 |
// Set the observer to check the continuation values |
| 275 |
Teuchos::RCP<NOX::Observer> observer = Teuchos::rcp(new CheckConstraintObserver); |
| 276 |
nl_params.sublist("Solver Options").set("Observer",observer); |
| 277 |
|
| 278 |
// Create the LOCA Group: |
| 279 |
// (NOX::Thyra::Group-->LOCA::Thyra::Group-->LOCA::Constrained::Group) |
| 280 |
// For Tpetra Householder, we need to actively set the |
| 281 |
// preconditioner and preconditioner factory so that it uses the |
| 282 |
// precOp separate from the Jacobian operator. Householder replaces |
| 283 |
// the Jacobian operator with a matrix-free version that has the |
| 284 |
// uv^T tacked on. |
| 285 |
auto explicit_jacobian = model->create_W_op(); |
| 286 |
auto prec_matrix = Teuchos::rcp(new Thyra::DefaultPreconditioner<NOX::Scalar>(Teuchos::null,explicit_jacobian)); |
| 287 |
TEST_ASSERT(nonnull(model->get_W_factory()->getPreconditionerFactory())); |
| 288 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 289 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, |
| 290 |
model, |
| 291 |
explicit_jacobian, |
| 292 |
model->get_W_factory(), |
| 293 |
prec_matrix, // Reuse Jac for approx preconditioner |
| 294 |
model->get_W_factory()->getPreconditionerFactory(), |
| 295 |
Teuchos::null)); |
| 296 |
|
| 297 |
Teuchos::RCP<LOCA::Abstract::Factory> tpetra_factory = Teuchos::rcp(new LOCA::Tpetra::Factory); |
| 298 |
|
| 299 |
Teuchos::RCP<LOCA::GlobalData> global_data = LOCA::createGlobalData(pList, tpetra_factory); |
| 300 |
|
| 301 |
Teuchos::RCP<LOCA::ParameterVector> p_vec = Teuchos::rcp(new LOCA::ParameterVector); |
| 302 |
p_vec->addParameter("k", 1.0); // Source term multiplier |
| 303 |
p_vec->addParameter("T_left", 1.2); // Source term multiplier |
| 304 |
|
| 305 |
std::vector<int> me_p_indices; |
| 306 |
me_p_indices.push_back(model->get_p_index("k").first); |
| 307 |
me_p_indices.push_back(model->get_p_index("T_left").first); |
| 308 |
Teuchos::RCP<LOCA::Thyra::Group> loca_group = Teuchos::rcp(new LOCA::Thyra::Group(global_data, |
| 309 |
*nox_group, |
| 310 |
*p_vec, |
| 311 |
me_p_indices)); |
| 312 |
|
| 313 |
auto g_names = Teuchos::rcp(new std::vector<std::string>); |
| 314 |
// g_names->push_back("Constraint: T_right=2"); |
| 315 |
g_names->push_back("Constraint: 2*T_left=T_right"); |
| 316 |
auto x_thyra = ::Thyra::createMember(model->get_x_space(),"x"); |
| 317 |
NOX::Thyra::Vector x(x_thyra); |
| 318 |
auto constraints = Teuchos::rcp(new LOCA::MultiContinuation::ConstraintModelEvaluator(model,*p_vec,*g_names,x)); |
| 319 |
|
| 320 |
// Set initial parameter conditions |
| 321 |
constraints->setX(x); |
| 322 |
constraints->setParam(p_vec->getIndex("k"),1.0); |
| 323 |
constraints->setParam(p_vec->getIndex("T_left"),1.2); |
| 324 |
|
| 325 |
// Create the constraints list |
| 326 |
auto& locaParamsList = pList->sublist("LOCA"); |
| 327 |
auto& constraint_list = locaParamsList.sublist("Constraints"); |
| 328 |
constraint_list.set("Bordered Solver Method", "Householder"); |
| 329 |
constraint_list.set<Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface>>("Constraint Object", constraints); |
| 330 |
constraint_list.set("Constraint Parameter Names", g_names); |
| 331 |
|
| 332 |
auto loca_parser = Teuchos::rcp(new LOCA::Parameter::SublistParser(global_data)); |
| 333 |
loca_parser->parseSublists(pList); |
| 334 |
|
| 335 |
// Sets the params that are allowed to change to enforce constraints |
| 336 |
std::vector<int> param_ids(1); |
| 337 |
param_ids[0] = p_vec->getIndex("k"); |
| 338 |
auto constraint_list_ptr = Teuchos::rcpFromRef(constraint_list); |
| 339 |
Teuchos::RCP<LOCA::MultiContinuation::ConstrainedGroup> loca_constrained_group = |
| 340 |
Teuchos::rcp(new LOCA::MultiContinuation::ConstrainedGroup(global_data, |
| 341 |
loca_parser, |
| 342 |
constraint_list_ptr, |
| 343 |
loca_group, |
| 344 |
constraints, |
| 345 |
param_ids, |
| 346 |
false)); |
| 347 |
|
| 348 |
loca_constrained_group->computeF(); |
| 349 |
|
| 350 |
// Create the NOX status tests and the solver |
| 351 |
// Create the convergence tests |
| 352 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 353 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 354 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 355 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 356 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 357 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 358 |
converged->addStatusTest(absresid); |
| 359 |
converged->addStatusTest(wrms); |
| 360 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 361 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(10)); |
| 362 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 363 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 364 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 365 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 366 |
combo->addStatusTest(fv); |
| 367 |
combo->addStatusTest(converged); |
| 368 |
combo->addStatusTest(maxiters); |
| 369 |
|
| 370 |
// Create the solver |
| 371 |
// auto solver = NOX::Solver::buildSolver(nox_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 372 |
// auto solver = NOX::Solver::buildSolver(loca_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 373 |
auto solver = NOX::Solver::buildSolver(loca_constrained_group, combo, Teuchos::rcpFromRef(pList->sublist("NOX"))); |
| 374 |
|
| 375 |
solver->solve(); |
| 376 |
out << "\n\nStepping from T_left=1.2 to 2.0\n\n"; |
| 377 |
auto& nonconst_group = const_cast<NOX::Abstract::Group&>(solver->getSolutionGroup()); |
| 378 |
dynamic_cast<LOCA::MultiContinuation::ConstrainedGroup&>(nonconst_group).setParam("T_left",2.0); |
| 379 |
solver->reset(); |
| 380 |
solver->solve(); |
| 381 |
|
| 382 |
// Output |
| 383 |
{ |
| 384 |
Teuchos::TimeMonitor::getStackedTimer()->stopBaseTimer(); |
| 385 |
Teuchos::StackedTimer::OutputOptions options; |
| 386 |
options.output_fraction = true; |
| 387 |
options.output_minmax = true; |
| 388 |
Teuchos::TimeMonitor::getStackedTimer()->report(out,comm,options); |
| 389 |
} |
| 390 |
|
| 391 |
// Write solution to file |
| 392 |
const bool printSolution = true; |
| 393 |
if (printSolution) { |
| 394 |
for (int i=0; i < comm->getSize(); ++i) { |
| 395 |
if (comm->getRank() == i) { |
| 396 |
std::ofstream file; |
| 397 |
if (comm->getRank() == 0) |
| 398 |
file.open("householder_continuation_manual_solution.txt",std::ios::trunc); |
| 399 |
else |
| 400 |
file.open("householder_continuation_manual_solution.txt",std::ios::app); |
| 401 |
|
| 402 |
const auto& final_x = solver->getSolutionGroup().getX(); |
| 403 |
const auto& final_x_nox = *(dynamic_cast<const LOCA::MultiContinuation::ExtendedVector&>(final_x).getXVec()); |
| 404 |
const auto& final_x_thyra = dynamic_cast<const NOX::Thyra::Vector&>(final_x_nox).getThyraVector(); |
| 405 |
const auto& final_x_tpetra_const = *(dynamic_cast<const ::Thyra::TpetraVector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_thyra).getConstTpetraVector()); |
| 406 |
auto& final_x_tpetra = const_cast<::Tpetra::Vector<NOX::Scalar,NOX::LocalOrdinal,NOX::GlobalOrdinal,NOX::NodeType>&>(final_x_tpetra_const); |
| 407 |
const auto& final_x_view = final_x_tpetra.getLocalViewHost(Tpetra::Access::ReadOnly); |
| 408 |
for (size_t j=0; j < final_x_view.extent(0); ++j) |
| 409 |
file << final_x_view(j,0) << std::endl; |
| 410 |
} |
| 411 |
comm->barrier(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
// Second step ends in 3 newton iterations |
| 416 |
TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearIterations,3); |
| 417 |
|
| 418 |
// Check final values |
| 419 |
{ |
| 420 |
const auto& group = solver->getSolutionGroup(); |
| 421 |
const auto& c_group = dynamic_cast<const LOCA::MultiContinuation::ConstrainedGroup&>(group); |
| 422 |
|
| 423 |
out << "\nFinal Parameter Value for \"k\" = " << std::setprecision(10) << c_group.getParam(0) << std::endl; |
| 424 |
out << "Final Parameter Value for \"T_left\" = " << std::setprecision(10) << c_group.getParam(p_vec->getIndex("T_left")) << std::endl; |
| 425 |
|
| 426 |
const double tol = 1.0e-5; |
| 427 |
TEST_FLOATING_EQUALITY(c_group.getParam(p_vec->getIndex("k")),-0.2996638603,tol); |
| 428 |
TEST_FLOATING_EQUALITY(c_group.getParam(p_vec->getIndex("T_left")),2.0,tol); |
| 429 |
} |
| 430 |
|
| 431 |
// Breaks RCP cyclic dependency |
| 432 |
LOCA::destroyGlobalData(global_data); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_Householder, LOCA_Continuation) |
| 437 |
{ |
| 438 |
Teuchos::TimeMonitor::setStackedTimer(Teuchos::rcp(new Teuchos::StackedTimer("LOCA_Continuation"))); // start a new stacked timer |
| 439 |
// Teuchos::TimeMonitor::getStackedTimer()->startBaseTimer(); |
| 440 |
|
| 441 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 442 |
|
| 443 |
// Get default Tpetra template types |
| 444 |
using Scalar = NOX::Scalar; |
| 445 |
using LO = NOX::LocalOrdinal; |
| 446 |
using GO = NOX::GlobalOrdinal; |
| 447 |
using Node = NOX::NodeType; |
| 448 |
|
| 449 |
// Create the model evaluator object |
| 450 |
Scalar x00 = 0.0; |
| 451 |
Scalar x01 = 1.0; |
| 452 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 453 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 454 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 455 |
|
| 456 |
// Create the linear solver and register on model evaluator |
| 457 |
{ |
| 458 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 459 |
|
| 460 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 461 |
p->set("Linear Solver Type", "Belos"); |
| 462 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 463 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 464 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 465 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 466 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", Belos::Errors+Belos::IterationDetails+Belos::FinalSummary); |
| 467 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 5); |
| 468 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 469 |
p->set("Preconditioner Type", "Ifpack2"); |
| 470 |
// p->set("Preconditioner Type", "None"); |
| 471 |
Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2"); |
| 472 |
ifpackList.set("Prec Type", "ILUT"); |
| 473 |
|
| 474 |
builder.setParameterList(p); |
| 475 |
|
| 476 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 477 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 478 |
|
| 479 |
model->set_W_factory(lowsFactory); |
| 480 |
} |
| 481 |
|
| 482 |
// Create the initial guess |
| 483 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 484 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 485 |
// Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 486 |
Thyra::V_S(initial_guess.ptr(),2.0); |
| 487 |
|
| 488 |
// Create top level nox/loca solver parameter list |
| 489 |
Teuchos::RCP<Teuchos::ParameterList> pList = Teuchos::parameterList("Top Level"); |
| 490 |
|
| 491 |
// Create the LOCA parameter list |
| 492 |
auto& loca_params = pList->sublist("LOCA"); |
| 493 |
Teuchos::ParameterList& stepperList = loca_params.sublist("Stepper"); |
| 494 |
stepperList.set("Continuation Method", "Arc Length"); |
| 495 |
stepperList.set("Continuation Parameter", "T_left"); |
| 496 |
stepperList.set("Initial Value", 1.0); |
| 497 |
stepperList.set("Max Value", 4.0); |
| 498 |
stepperList.set("Min Value", 1.0); |
| 499 |
stepperList.set("Max Steps", 50); |
| 500 |
stepperList.set("Max Nonlinear Iterations", 20); |
| 501 |
|
| 502 |
// Combine arc-length and turning point bordered rows & columns |
| 503 |
stepperList.set("Bordered Solver Method", "Nested"); |
| 504 |
auto& nestedList = stepperList.sublist("Nested Bordered Solver"); |
| 505 |
nestedList.set("Bordered Solver Method", "Householder"); |
| 506 |
|
| 507 |
// Create step size sublist |
| 508 |
Teuchos::ParameterList& stepSizeList = loca_params.sublist("Step Size"); |
| 509 |
stepSizeList.set("Initial Step Size", 0.5); |
| 510 |
stepSizeList.set("Min Step Size", 0.5); |
| 511 |
stepSizeList.set("Max Step Size", 1.0); |
| 512 |
|
| 513 |
// Create NOX parameter list |
| 514 |
auto& nl_params = pList->sublist("NOX"); |
| 515 |
nl_params.set("Nonlinear Solver", "Line Search Based"); |
| 516 |
nl_params.sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-8); |
| 517 |
auto& ls_params = nl_params.sublist("Line Search"); |
| 518 |
ls_params.set("Method","Full Step"); |
| 519 |
|
| 520 |
auto& output_list = nl_params.sublist("Printing").sublist("Output Information"); |
| 521 |
output_list.set("Debug",true); |
| 522 |
output_list.set("Warning",true); |
| 523 |
output_list.set("Error",true); |
| 524 |
output_list.set("Test Details",true); |
| 525 |
output_list.set("Details",false); |
| 526 |
output_list.set("Parameters",false); |
| 527 |
output_list.set("Linear Solver Details",true); |
| 528 |
output_list.set("Inner Iteration",true); |
| 529 |
output_list.set("Outer Iteration",true); |
| 530 |
output_list.set("Outer Iteration StatusTest",true); |
| 531 |
output_list.set("Stepper Iteration",true); |
| 532 |
output_list.set("Stepper Details",true); |
| 533 |
output_list.set("Stepper Parameters",true); |
| 534 |
|
| 535 |
// Create the LOCA Group: |
| 536 |
// (NOX::Thyra::Group-->LOCA::Thyra::Group-->LOCA::Constrained::Group) |
| 537 |
// For Tpetra Householder, we need to actively set the |
| 538 |
// preconditioner and preconditioner factory so that it uses the |
| 539 |
// precOp separate from the Jacobian operator. Householder replaces |
| 540 |
// the Jacobian operator with a matrix-free version that has the |
| 541 |
// uv^T tacked on. |
| 542 |
auto explicit_jacobian = model->create_W_op(); |
| 543 |
auto prec_matrix = Teuchos::rcp(new Thyra::DefaultPreconditioner<NOX::Scalar>(Teuchos::null,explicit_jacobian)); |
| 544 |
TEST_ASSERT(nonnull(model->get_W_factory()->getPreconditionerFactory())); |
| 545 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 546 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, |
| 547 |
model, |
| 548 |
explicit_jacobian, |
| 549 |
model->get_W_factory(), |
| 550 |
prec_matrix, // Reuse Jac for approx preconditioner |
| 551 |
model->get_W_factory()->getPreconditionerFactory(), |
| 552 |
Teuchos::null)); |
| 553 |
|
| 554 |
Teuchos::RCP<LOCA::Abstract::Factory> tpetra_factory = Teuchos::rcp(new LOCA::Tpetra::Factory); |
| 555 |
|
| 556 |
Teuchos::RCP<LOCA::GlobalData> global_data = LOCA::createGlobalData(pList, tpetra_factory); |
| 557 |
|
| 558 |
Teuchos::RCP<LOCA::ParameterVector> p_vec = Teuchos::rcp(new LOCA::ParameterVector); |
| 559 |
p_vec->addParameter("k", 1.0); // Source term multiplier |
| 560 |
p_vec->addParameter("T_left", 1.2); // Source term multiplier |
| 561 |
|
| 562 |
std::vector<int> me_p_indices; |
| 563 |
me_p_indices.push_back(model->get_p_index("k").first); |
| 564 |
me_p_indices.push_back(model->get_p_index("T_left").first); |
| 565 |
Teuchos::RCP<LOCA::Thyra::Group> loca_group = Teuchos::rcp(new LOCA::Thyra::Group(global_data, |
| 566 |
*nox_group, |
| 567 |
*p_vec, |
| 568 |
me_p_indices)); |
| 569 |
|
| 570 |
auto g_names = Teuchos::rcp(new std::vector<std::string>); |
| 571 |
// g_names->push_back("Constraint: T_right=2"); |
| 572 |
g_names->push_back("Constraint: 2*T_left=T_right"); |
| 573 |
auto x_thyra = ::Thyra::createMember(model->get_x_space(),"x"); |
| 574 |
NOX::Thyra::Vector x(x_thyra); |
| 575 |
auto constraints = Teuchos::rcp(new LOCA::MultiContinuation::ConstraintModelEvaluator(model,*p_vec,*g_names,x)); |
| 576 |
|
| 577 |
// Set initial parameter conditions |
| 578 |
constraints->setX(x); |
| 579 |
constraints->setParam(p_vec->getIndex("k"),1.0); |
| 580 |
constraints->setParam(p_vec->getIndex("T_left"),1.2); |
| 581 |
|
| 582 |
// Create the constraints list |
| 583 |
auto& locaParamsList = pList->sublist("LOCA"); |
| 584 |
auto& constraint_list = locaParamsList.sublist("Constraints"); |
| 585 |
constraint_list.set("Bordered Solver Method", "Householder"); |
| 586 |
constraint_list.set<Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface>>("Constraint Object", constraints); |
| 587 |
// Sets the params that are allowed to change to enforce constraints |
| 588 |
auto p_names = Teuchos::rcp(new std::vector<std::string>); |
| 589 |
p_names->push_back("k"); |
| 590 |
constraint_list.set("Constraint Parameter Names", p_names); |
| 591 |
|
| 592 |
// Set the observer to check the continuation values |
| 593 |
{ |
| 594 |
auto observer1 = Teuchos::rcp(new CheckConstraintObserver); |
| 595 |
auto observer2 = Teuchos::rcp(new WriteParametersObserver("householder_continuation_loca.txt",comm,constraints)); |
| 596 |
auto observer = Teuchos::rcp(new NOX::ObserverVector); |
| 597 |
observer->pushBack(observer1); |
| 598 |
observer->pushBack(observer2); |
| 599 |
nl_params.sublist("Solver Options").set<Teuchos::RCP<NOX::Observer>>("Observer",observer); |
| 600 |
} |
| 601 |
|
| 602 |
// Create the NOX status tests and the solver |
| 603 |
// Create the convergence tests |
| 604 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 605 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 606 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 607 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 608 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 609 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 610 |
converged->addStatusTest(absresid); |
| 611 |
converged->addStatusTest(wrms); |
| 612 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 613 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(10)); |
| 614 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 615 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 616 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 617 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 618 |
combo->addStatusTest(fv); |
| 619 |
combo->addStatusTest(converged); |
| 620 |
combo->addStatusTest(maxiters); |
| 621 |
|
| 622 |
// Create the stepper |
| 623 |
LOCA::Stepper stepper(global_data, loca_group, combo, pList); |
| 624 |
|
| 625 |
// Perform continuation run |
| 626 |
LOCA::Abstract::Iterator::IteratorStatus status = stepper.run(); |
| 627 |
TEST_EQUALITY(status, LOCA::Abstract::Iterator::Finished); |
| 628 |
|
| 629 |
// Output |
| 630 |
{ |
| 631 |
Teuchos::TimeMonitor::getStackedTimer()->stopBaseTimer(); |
| 632 |
Teuchos::StackedTimer::OutputOptions options; |
| 633 |
options.output_fraction = true; |
| 634 |
options.output_minmax = true; |
| 635 |
Teuchos::TimeMonitor::getStackedTimer()->report(out,comm,options); |
| 636 |
} |
| 637 |
|
| 638 |
// Write solution to file |
| 639 |
const bool printSolution = true; |
| 640 |
if (printSolution) { |
| 641 |
for (int i=0; i < comm->getSize(); ++i) { |
| 642 |
if (comm->getRank() == i) { |
| 643 |
std::ofstream file; |
| 644 |
if (comm->getRank() == 0) |
| 645 |
file.open("householder_conintuation_loca_solution.txt",std::ios::trunc); |
| 646 |
else |
| 647 |
file.open("householder_continuation_loca_solution.txt",std::ios::app); |
| 648 |
|
| 649 |
auto final_x_view = test_utils::getConstView(stepper.getSolutionGroup()->getX()); |
| 650 |
|
| 651 |
for (size_t j=0; j < final_x_view.extent(0); ++j) |
| 652 |
file << final_x_view(j,0) << std::endl; |
| 653 |
} |
| 654 |
comm->barrier(); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
// Second step ends in 3 newton iterations |
| 659 |
TEST_EQUALITY(stepper.getSolver()->getSolverStatistics()->numNonlinearIterations,1); |
| 660 |
// TODO: Could LOCA be rebuilding the nox solver at every step??? |
| 661 |
TEST_EQUALITY(stepper.getSolver()->getSolverStatistics()->numTotalNonlinearIterations,1); |
| 662 |
|
| 663 |
// Breaks RCP cyclic dependency |
| 664 |
LOCA::destroyGlobalData(global_data); |
| 665 |
} |