| 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 |
|
| 29 |
#include "Teuchos_ParameterList.hpp" |
| 30 |
#include "Teuchos_RCP.hpp" |
| 31 |
#include "Teuchos_GlobalMPISession.hpp" |
| 32 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 33 |
#include "Teuchos_FancyOStream.hpp" |
| 34 |
|
| 35 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 36 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 37 |
#include "Thyra_SpmdVectorBase.hpp" |
| 38 |
#include "ModelEvaluator2DSim.hpp" |
| 39 |
|
| 40 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 41 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 42 |
#include "ModelEvaluator_LoggerDecorator.hpp" |
| 43 |
|
| 44 |
using namespace std; |
| 45 |
|
| 46 |
TEUCHOS_UNIT_TEST(NOX_Thyra_2DSim_JFNK, perturbation_unit_tests) |
| 47 |
{ |
| 48 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 49 |
|
| 50 |
// Create a communicator for Epetra objects |
| 51 |
#ifdef HAVE_MPI |
| 52 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 53 |
#else |
| 54 |
Epetra_SerialComm Comm; |
| 55 |
#endif |
| 56 |
|
| 57 |
// Check we have only one processor since this problem doesn't work |
| 58 |
// for more than one proc |
| 59 |
TEST_ASSERT(Comm.NumProc() == 1); |
| 60 |
|
| 61 |
// Create the model evaluator object |
| 62 |
double d = 10.0; |
| 63 |
double p0 = 2.0; |
| 64 |
double p1 = 0.0; |
| 65 |
double x00 = 0.0; |
| 66 |
double x01 = 1.0; |
| 67 |
Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel = |
| 68 |
modelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),d,p0,p1,x00,x01); |
| 69 |
|
| 70 |
// Create the linear solver type with Stratimikos |
| 71 |
//Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > |
| 72 |
//lowsFactory = rcp(new Thyra::AmesosLinearOpWithSolveFactory()); |
| 73 |
|
| 74 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 75 |
|
| 76 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 77 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 78 |
p->set("Linear Solver Type", "AztecOO"); |
| 79 |
p->set("Preconditioner Type", "Ifpack"); |
| 80 |
//p->set("Enable Delayed Solver Construction", true); |
| 81 |
builder.setParameterList(p); |
| 82 |
|
| 83 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 84 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 85 |
|
| 86 |
thyraModel->set_W_factory(lowsFactory); |
| 87 |
|
| 88 |
// Create the initial guess |
| 89 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 90 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 91 |
|
| 92 |
// Create the NOX::Thyra::Group |
| 93 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 94 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel)); |
| 95 |
|
| 96 |
nox_group->computeF(); |
| 97 |
|
| 98 |
// Test the default JFNK parameter values (LOCA delta evaluation) |
| 99 |
{ |
| 100 |
out << "\n **** Testing Salinger LOCA perturbation ****\n" << std::endl; |
| 101 |
|
| 102 |
// Create the JFNK operator |
| 103 |
Teuchos::ParameterList printParams; |
| 104 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 105 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 106 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 107 |
jfnkOp->setParameterList(jfnkParams); |
| 108 |
jfnkParams->print(out); |
| 109 |
|
| 110 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group); |
| 111 |
|
| 112 |
// Experiment with JFNK object |
| 113 |
Teuchos::RCP< ::Thyra::VectorBase<double> > input = initial_guess->clone_v(); |
| 114 |
Teuchos::RCP< ::Thyra::VectorBase<double> > output = initial_guess->clone_v(); |
| 115 |
::Thyra::put_scalar(1.0,input.ptr()); |
| 116 |
::Thyra::put_scalar(0.0,output.ptr()); |
| 117 |
::Thyra::apply(*jfnkOp,::Thyra::NOTRANS,*input,output.ptr()); |
| 118 |
|
| 119 |
output->describe(out,Teuchos::VERB_EXTREME); |
| 120 |
|
| 121 |
const double tol_mach_eps = 10.0 * std::numeric_limits<double>::epsilon(); |
| 122 |
TEST_FLOATING_EQUALITY(jfnkOp->getLambda(),1.0e-6,tol_mach_eps); |
| 123 |
|
| 124 |
Teuchos::RCP< ::Thyra::SpmdVectorBase<double> > output_spmd = |
| 125 |
Teuchos::rcp_dynamic_cast< ::Thyra::SpmdVectorBase<double> >(output); |
| 126 |
Teuchos::ArrayRCP<const double> data; |
| 127 |
output_spmd->getLocalData(Teuchos::ptrFromRef(data)); |
| 128 |
const double tol_perturb = 10.0 * jfnkOp->getDelta(); |
| 129 |
TEST_FLOATING_EQUALITY(data[0],3.0,tol_perturb); |
| 130 |
TEST_FLOATING_EQUALITY(data[1],-10.0,tol_perturb); |
| 131 |
|
| 132 |
TEST_THROW(jfnkOp->setUserDefinedDelta(1.0e-8),std::logic_error); |
| 133 |
|
| 134 |
double delta_expected = jfnkOp->getLambda() * (jfnkOp->getLambda() + ::Thyra::norm(*initial_guess) / ::Thyra::norm(*input)); |
| 135 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),delta_expected,tol_mach_eps); |
| 136 |
} |
| 137 |
|
| 138 |
// Test the KelleySalinger delta computation |
| 139 |
{ |
| 140 |
out << "\n **** Testing Kelley Salinger Pawlowski 2001 perturbation ****\n" << std::endl; |
| 141 |
|
| 142 |
// Create the JFNK operator |
| 143 |
Teuchos::ParameterList printParams; |
| 144 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 145 |
jfnkParams->set("Difference Type","Forward"); |
| 146 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 147 |
jfnkParams->set("lambda",1.0e-4); |
| 148 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 149 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 150 |
jfnkOp->setParameterList(jfnkParams); |
| 151 |
jfnkParams->print(out); |
| 152 |
|
| 153 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group); |
| 154 |
|
| 155 |
// Experiment with JFNK object |
| 156 |
Teuchos::RCP< ::Thyra::VectorBase<double> > input = initial_guess->clone_v(); |
| 157 |
Teuchos::RCP< ::Thyra::VectorBase<double> > output = initial_guess->clone_v(); |
| 158 |
::Thyra::put_scalar(1.0,input.ptr()); |
| 159 |
::Thyra::put_scalar(0.0,output.ptr()); |
| 160 |
::Thyra::apply(*jfnkOp,::Thyra::NOTRANS,*input,output.ptr()); |
| 161 |
|
| 162 |
const double tol_mach_eps = 10.0 * std::numeric_limits<double>::epsilon(); |
| 163 |
TEST_FLOATING_EQUALITY(jfnkOp->getLambda(),1.0e-4,tol_mach_eps); |
| 164 |
|
| 165 |
Teuchos::RCP< ::Thyra::SpmdVectorBase<double> > output_spmd = |
| 166 |
Teuchos::rcp_dynamic_cast< ::Thyra::SpmdVectorBase<double> >(output); |
| 167 |
Teuchos::ArrayRCP<const double> data; |
| 168 |
output_spmd->getLocalData(Teuchos::ptrFromRef(data)); |
| 169 |
const double tol_perturb = 10.0 * jfnkOp->getDelta(); |
| 170 |
TEST_FLOATING_EQUALITY(data[0],3.0,tol_perturb); |
| 171 |
TEST_FLOATING_EQUALITY(data[1],-10.0,tol_perturb); |
| 172 |
|
| 173 |
double dotprod = ::Thyra::inner(*initial_guess,*input); |
| 174 |
double vectorNorm = ::Thyra::norm(*input); |
| 175 |
double ks_delta_expected = jfnkOp->getLambda() * (1.0e-12 / jfnkOp->getLambda() + std::fabs(dotprod) / (vectorNorm * vectorNorm) ) * dotprod / std::fabs(dotprod); |
| 176 |
|
| 177 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),ks_delta_expected,tol_mach_eps); |
| 178 |
} |
| 179 |
|
| 180 |
// Test the KnollKeyes delta computation |
| 181 |
{ |
| 182 |
out << "\n **** Testing Knoll Keyes JCP 2004 perturbation ****\n" << std::endl; |
| 183 |
|
| 184 |
// Create the JFNK operator |
| 185 |
Teuchos::ParameterList printParams; |
| 186 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 187 |
jfnkParams->set("Difference Type","Forward"); |
| 188 |
jfnkParams->set("Perturbation Algorithm","Knoll Keyes JCP 2004"); |
| 189 |
jfnkParams->set("lambda",1.0e-6); |
| 190 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 191 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 192 |
jfnkOp->setParameterList(jfnkParams); |
| 193 |
jfnkParams->print(out); |
| 194 |
|
| 195 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group); |
| 196 |
|
| 197 |
// Experiment with JFNK object |
| 198 |
Teuchos::RCP< ::Thyra::VectorBase<double> > input = initial_guess->clone_v(); |
| 199 |
Teuchos::RCP< ::Thyra::VectorBase<double> > output = initial_guess->clone_v(); |
| 200 |
::Thyra::put_scalar(1.0,input.ptr()); |
| 201 |
::Thyra::put_scalar(0.0,output.ptr()); |
| 202 |
::Thyra::apply(*jfnkOp,::Thyra::NOTRANS,*input,output.ptr()); |
| 203 |
|
| 204 |
const double tol_mach_eps = 10.0 * std::numeric_limits<double>::epsilon(); |
| 205 |
const double tol_perturb = 10.0 * jfnkOp->getDelta(); |
| 206 |
|
| 207 |
Teuchos::RCP< ::Thyra::SpmdVectorBase<double> > output_spmd = |
| 208 |
Teuchos::rcp_dynamic_cast< ::Thyra::SpmdVectorBase<double> >(output); |
| 209 |
Teuchos::ArrayRCP<const double> data; |
| 210 |
output_spmd->getLocalData(Teuchos::ptrFromRef(data)); |
| 211 |
TEST_FLOATING_EQUALITY(data[0],3.0,tol_perturb); |
| 212 |
TEST_FLOATING_EQUALITY(data[1],-10.0,tol_perturb); |
| 213 |
|
| 214 |
double kk_delta_expected = jfnkOp->getLambda() * ::Thyra::norm_1(*initial_guess) / (Teuchos::as<double>(initial_guess->space()->dim()) * ::Thyra::norm(*input)) + jfnkOp->getLambda(); |
| 215 |
|
| 216 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),kk_delta_expected,tol_mach_eps); |
| 217 |
} |
| 218 |
|
| 219 |
// Test the user defined delta computation |
| 220 |
{ |
| 221 |
out << "\n **** Testing User Defined perturbation ****\n" << std::endl; |
| 222 |
|
| 223 |
// Create the JFNK operator |
| 224 |
Teuchos::ParameterList printParams; |
| 225 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 226 |
jfnkParams->set("Difference Type","Forward"); |
| 227 |
jfnkParams->set("Perturbation Algorithm","User Defined"); |
| 228 |
jfnkParams->set("lambda",1.0e-4); |
| 229 |
jfnkParams->set("User Defined delta Value",1.0e-5); |
| 230 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 231 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 232 |
jfnkOp->setParameterList(jfnkParams); |
| 233 |
jfnkParams->print(std::cout); |
| 234 |
|
| 235 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group); |
| 236 |
|
| 237 |
// Experiment with JFNK object |
| 238 |
Teuchos::RCP< ::Thyra::VectorBase<double> > input = initial_guess->clone_v(); |
| 239 |
Teuchos::RCP< ::Thyra::VectorBase<double> > output = initial_guess->clone_v(); |
| 240 |
::Thyra::put_scalar(1.0,input.ptr()); |
| 241 |
::Thyra::put_scalar(0.0,output.ptr()); |
| 242 |
::Thyra::apply(*jfnkOp,::Thyra::NOTRANS,*input,output.ptr()); |
| 243 |
|
| 244 |
const double tol_mach_eps = 10.0 * std::numeric_limits<double>::epsilon(); |
| 245 |
TEST_FLOATING_EQUALITY(jfnkOp->getLambda(),1.0e-4,tol_mach_eps); |
| 246 |
|
| 247 |
Teuchos::RCP< ::Thyra::SpmdVectorBase<double> > output_spmd = |
| 248 |
Teuchos::rcp_dynamic_cast< ::Thyra::SpmdVectorBase<double> >(output); |
| 249 |
Teuchos::ArrayRCP<const double> data; |
| 250 |
output_spmd->getLocalData(Teuchos::ptrFromRef(data)); |
| 251 |
const double tol_perturb = 10.0 * jfnkOp->getDelta(); |
| 252 |
TEST_FLOATING_EQUALITY(data[0],3.0,tol_perturb); |
| 253 |
TEST_FLOATING_EQUALITY(data[1],-10.0,tol_perturb); |
| 254 |
double user_defined_delta_expected = 1.0e-5; |
| 255 |
|
| 256 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),user_defined_delta_expected,tol_mach_eps); |
| 257 |
|
| 258 |
// test being able to change the value at runtime |
| 259 |
jfnkOp->setUserDefinedDelta(1.0e-7); |
| 260 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),1.0e-5,tol_mach_eps); |
| 261 |
::Thyra::apply(*jfnkOp,::Thyra::NOTRANS,*input,output.ptr()); |
| 262 |
TEST_FLOATING_EQUALITY(jfnkOp->getDelta(),1.0e-7,tol_mach_eps); |
| 263 |
} |
| 264 |
|
| 265 |
Teuchos::TimeMonitor::summarize(); |
| 266 |
} |
| 267 |
|
| 268 |
TEUCHOS_UNIT_TEST(NOX_Thyra_2DSim_JFNK, solve_no_prec) |
| 269 |
{ |
| 270 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 271 |
|
| 272 |
// Create a communicator for Epetra objects |
| 273 |
#ifdef HAVE_MPI |
| 274 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 275 |
#else |
| 276 |
Epetra_SerialComm Comm; |
| 277 |
#endif |
| 278 |
|
| 279 |
// Check we have only one processor since this problem doesn't work |
| 280 |
// for more than one proc |
| 281 |
TEST_ASSERT(Comm.NumProc() == 1); |
| 282 |
|
| 283 |
// Create the model evaluator object |
| 284 |
double d = 10.0; |
| 285 |
double p0 = 2.0; |
| 286 |
double p1 = 0.0; |
| 287 |
double x00 = 0.0; |
| 288 |
double x01 = 1.0; |
| 289 |
Teuchos::RCP<ModelEvaluator2DSim<double> > model = |
| 290 |
modelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),d,p0,p1,x00,x01); |
| 291 |
|
| 292 |
// Create the linear solver type with Stratimikos |
| 293 |
//Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > |
| 294 |
//lowsFactory = rcp(new Thyra::AmesosLinearOpWithSolveFactory()); |
| 295 |
|
| 296 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 297 |
|
| 298 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 299 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 300 |
p->set("Linear Solver Type", "AztecOO"); |
| 301 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").set("Tolerance",1.0e-1); |
| 302 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",1); |
| 303 |
//p->set("Linear Solver Type", "Belos"); |
| 304 |
p->set("Preconditioner Type", "None"); |
| 305 |
//p->set("Enable Delayed Solver Construction", true); |
| 306 |
builder.setParameterList(p); |
| 307 |
|
| 308 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 309 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 310 |
|
| 311 |
model->set_W_factory(lowsFactory); |
| 312 |
|
| 313 |
// Create the initial guess |
| 314 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 315 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 316 |
|
| 317 |
// Create the JFNK operator |
| 318 |
Teuchos::ParameterList printParams; |
| 319 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 320 |
jfnkParams->set("Difference Type","Forward"); |
| 321 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 322 |
jfnkParams->set("lambda",1.0e-4); |
| 323 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 324 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 325 |
jfnkOp->setParameterList(jfnkParams); |
| 326 |
jfnkParams->print(out); |
| 327 |
|
| 328 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 329 |
Teuchos::RCP< ::Thyra::ModelEvaluator<double> > thyraModel = |
| 330 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<double>(model)); |
| 331 |
|
| 332 |
// Wrap model in logger decrator for unit testing |
| 333 |
Teuchos::RCP<NOX_TEST::ModelEvaluatorLoggerDecorator<double> > loggedModel = |
| 334 |
Teuchos::rcp(new NOX_TEST::ModelEvaluatorLoggerDecorator<double>(thyraModel)); |
| 335 |
|
| 336 |
// Create the NOX::Thyra::Group |
| 337 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 338 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, loggedModel, jfnkOp, lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 339 |
|
| 340 |
nox_group->computeF(); |
| 341 |
|
| 342 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 343 |
// This creates a circular dependency, so use a weak pointer. |
| 344 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 345 |
|
| 346 |
// Create the NOX status tests and the solver |
| 347 |
// Create the convergence tests |
| 348 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 349 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 350 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 351 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 352 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 353 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 354 |
converged->addStatusTest(absresid); |
| 355 |
converged->addStatusTest(wrms); |
| 356 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 357 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 358 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 359 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 360 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 361 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 362 |
combo->addStatusTest(fv); |
| 363 |
combo->addStatusTest(converged); |
| 364 |
combo->addStatusTest(maxiters); |
| 365 |
|
| 366 |
// Create nox parameter list |
| 367 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 368 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 369 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 370 |
|
| 371 |
// Create the solver |
| 372 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 373 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 374 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 375 |
|
| 376 |
// Final return value (0 = successfull, non-zero = failure) |
| 377 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 378 |
TEST_EQUALITY(loggedModel->getNumExactCalls(),8); |
| 379 |
TEST_EQUALITY(loggedModel->getNumApproxDerivCalls(),21); |
| 380 |
TEST_EQUALITY(loggedModel->getNumVeryApproxDerivCalls(),0); |
| 381 |
|
| 382 |
Teuchos::TimeMonitor::summarize(); |
| 383 |
} |
| 384 |
|
| 385 |
TEUCHOS_UNIT_TEST(NOX_Thyra_2DSim_JFNK, solve_Jacobi_prec_using_ME) |
| 386 |
{ |
| 387 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 388 |
|
| 389 |
// Create a communicator for Epetra objects |
| 390 |
#ifdef HAVE_MPI |
| 391 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 392 |
#else |
| 393 |
Epetra_SerialComm Comm; |
| 394 |
#endif |
| 395 |
|
| 396 |
// Check we have only one processor since this problem doesn't work |
| 397 |
// for more than one proc |
| 398 |
TEST_ASSERT(Comm.NumProc() == 1); |
| 399 |
|
| 400 |
// Create the model evaluator object |
| 401 |
double d = 10.0; |
| 402 |
double p0 = 2.0; |
| 403 |
double p1 = 0.0; |
| 404 |
double x00 = 0.0; |
| 405 |
double x01 = 1.0; |
| 406 |
Teuchos::RCP<ModelEvaluator2DSim<double> > model = |
| 407 |
modelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false),d,p0,p1,x00,x01); |
| 408 |
|
| 409 |
// Create the linear solver type with Stratimikos |
| 410 |
//Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > |
| 411 |
//lowsFactory = rcp(new Thyra::AmesosLinearOpWithSolveFactory()); |
| 412 |
|
| 413 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 414 |
|
| 415 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 416 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 417 |
p->set("Linear Solver Type", "AztecOO"); |
| 418 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").set("Tolerance",1.0e-1); |
| 419 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",1); |
| 420 |
// p->set("Linear Solver Type", "Belos"); |
| 421 |
p->set("Preconditioner Type", "None"); |
| 422 |
//p->set("Enable Delayed Solver Construction", true); |
| 423 |
builder.setParameterList(p); |
| 424 |
|
| 425 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 426 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 427 |
|
| 428 |
model->set_W_factory(lowsFactory); |
| 429 |
|
| 430 |
// Create the initial guess |
| 431 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 432 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 433 |
|
| 434 |
// Create the JFNK operator |
| 435 |
Teuchos::ParameterList printParams; |
| 436 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 437 |
jfnkParams->set("Difference Type","Forward"); |
| 438 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 439 |
jfnkParams->set("lambda",1.0e-4); |
| 440 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 441 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 442 |
jfnkOp->setParameterList(jfnkParams); |
| 443 |
jfnkParams->print(out); |
| 444 |
|
| 445 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 446 |
Teuchos::RCP< ::Thyra::ModelEvaluator<double> > thyraModel = |
| 447 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<double>(model)); |
| 448 |
|
| 449 |
// Create the Preconditioner operator |
| 450 |
Teuchos::RCP< ::Thyra::PreconditionerBase<double> > precOp = |
| 451 |
thyraModel->create_W_prec(); |
| 452 |
|
| 453 |
// Create the NOX::Thyra::Group |
| 454 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 455 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, jfnkOp, lowsFactory, precOp, Teuchos::null, Teuchos::null)); |
| 456 |
|
| 457 |
nox_group->computeF(); |
| 458 |
|
| 459 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 460 |
// This creates a circular dependency, so use a weak pointer. |
| 461 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 462 |
|
| 463 |
// Create a preconditioner |
| 464 |
|
| 465 |
// Create the NOX status tests and the solver |
| 466 |
// Create the convergence tests |
| 467 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 468 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 469 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 470 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 471 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 472 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 473 |
converged->addStatusTest(absresid); |
| 474 |
converged->addStatusTest(wrms); |
| 475 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 476 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 477 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 478 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 479 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 480 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 481 |
combo->addStatusTest(fv); |
| 482 |
combo->addStatusTest(converged); |
| 483 |
combo->addStatusTest(maxiters); |
| 484 |
|
| 485 |
// Create nox parameter list |
| 486 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 487 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 488 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 489 |
|
| 490 |
// Create the solver |
| 491 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 492 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 493 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 494 |
|
| 495 |
// Final return value (0 = successfull, non-zero = failure) |
| 496 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 497 |
|
| 498 |
Teuchos::TimeMonitor::summarize(); |
| 499 |
} |