| 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 "Thyra_TpetraThyraWrappers.hpp" |
| 31 |
#include "Thyra_VectorStdOps.hpp" |
| 32 |
#include "ME_Tpetra_1DFEM.hpp" |
| 33 |
|
| 34 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 35 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 36 |
#include "NOX_TpetraTypedefs.hpp" |
| 37 |
|
| 38 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, Responses_g4_p2) |
| 39 |
{ |
| 40 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 41 |
|
| 42 |
// Get default Tpetra template types |
| 43 |
using Scalar = NOX::Scalar; |
| 44 |
using LO = NOX::LocalOrdinal; |
| 45 |
using GO = NOX::GlobalOrdinal; |
| 46 |
using Node = NOX::NodeType; |
| 47 |
using converter = ::Thyra::TpetraOperatorVectorExtraction<Scalar,LO,GO,Node>; |
| 48 |
|
| 49 |
// Create the model evaluator object |
| 50 |
Scalar x00 = 0.0; |
| 51 |
Scalar x01 = 1.0; |
| 52 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 53 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 54 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 55 |
|
| 56 |
auto g_thyra = ::Thyra::createMember(*model->get_g_space(4),"g"); |
| 57 |
auto DfDp_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DfDp_op(2),true); |
| 58 |
auto DgDx_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDx_op(4),true); |
| 59 |
auto Dg4Dp2_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDp_op(4,2),true); |
| 60 |
auto Dg4Dp4_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDp_op(4,4),true); |
| 61 |
|
| 62 |
auto inArgs = model->createInArgs(); |
| 63 |
auto x = ::Thyra::createMember(model->get_x_space(),"x"); |
| 64 |
::Thyra::assign(x.ptr(),1.0); |
| 65 |
inArgs.set_x(x); |
| 66 |
auto outArgs = model->createOutArgs(); |
| 67 |
|
| 68 |
outArgs.set_g(4,::Thyra::ModelEvaluatorBase::Evaluation<::Thyra::VectorBase<Scalar>>(g_thyra)); |
| 69 |
outArgs.set_DfDp(2,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(DfDp_thyra)); |
| 70 |
outArgs.set_DgDx(4,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(DgDx_thyra,Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM)); |
| 71 |
outArgs.set_DgDp(4,2,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(Dg4Dp2_thyra)); |
| 72 |
outArgs.set_DgDp(4,4,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(Dg4Dp4_thyra)); |
| 73 |
|
| 74 |
model->evalModel(inArgs,outArgs); |
| 75 |
|
| 76 |
TEST_EQUALITY(model->Np(),5); |
| 77 |
TEST_EQUALITY(model->Ng(),7); |
| 78 |
|
| 79 |
auto g = converter::getTpetraMultiVector(g_thyra); |
| 80 |
auto DfDp = converter::getTpetraMultiVector(DfDp_thyra); |
| 81 |
auto DgDx = converter::getTpetraMultiVector(DgDx_thyra); |
| 82 |
auto Dg4Dp2 = converter::getTpetraMultiVector(Dg4Dp2_thyra); |
| 83 |
auto Dg4Dp4 = converter::getTpetraMultiVector(Dg4Dp4_thyra); |
| 84 |
|
| 85 |
// g = T(Zmax) - 2.0 |
| 86 |
// x=1.0 |
| 87 |
auto g_host = g->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 88 |
out << "g4 = " << g_host(0,0) << std::endl; |
| 89 |
auto tol = std::numeric_limits<Scalar>::epsilon()*100.0; |
| 90 |
TEST_FLOATING_EQUALITY(g_host(0,0),-1.0,tol); |
| 91 |
|
| 92 |
// DgDx: Right end node is 1, the rest of the vector is zero. |
| 93 |
out << "Dg4Dx:\n"; |
| 94 |
DgDx->describe(out,Teuchos::VERB_EXTREME); |
| 95 |
{ |
| 96 |
auto DgDx_host = DgDx->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 97 |
if (comm->getRank() == (comm->getSize() - 1)) { |
| 98 |
TEST_FLOATING_EQUALITY(DgDx_host(DgDx_host.extent(0) - 1, 0), 1.0, tol); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
Teuchos::Array<NOX::TMultiVector::mag_type> norms(1); |
| 103 |
DgDx->norm2(norms); |
| 104 |
TEST_FLOATING_EQUALITY(norms[0],1.0,tol); |
| 105 |
|
| 106 |
// DfDp |
| 107 |
out << "DfDp2:\n"; |
| 108 |
DfDp->describe(out,Teuchos::VERB_EXTREME); |
| 109 |
auto DfDp_host = DfDp->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 110 |
auto rank = comm->getRank(); |
| 111 |
auto size = comm->getSize(); |
| 112 |
for (size_t i=0; i < DfDp_host.extent(0); ++i) { |
| 113 |
if ((rank == 0) && (i == 0)) { // left end |
| 114 |
TEST_FLOATING_EQUALITY(DfDp_host(0,0),0.0,tol); |
| 115 |
} |
| 116 |
else if (rank == (size - 1) && (i==(DfDp_host.extent(0)-1))) { // right end |
| 117 |
TEST_FLOATING_EQUALITY(DfDp_host(DfDp_host.extent(0)-1,0),0.005,tol); |
| 118 |
} |
| 119 |
else { // interior nodes |
| 120 |
out << "rank=" << rank << ", i=" << i <<std::endl; |
| 121 |
TEST_FLOATING_EQUALITY(DfDp_host(i,0),0.01,tol); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
out << "Dg4Dp2:\n"; |
| 126 |
Dg4Dp2->describe(out,Teuchos::VERB_EXTREME); |
| 127 |
auto Dg4Dp2_host = Dg4Dp2->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 128 |
TEST_FLOATING_EQUALITY(Dg4Dp2_host(0,0),0.0,tol); |
| 129 |
|
| 130 |
out << "Dg4Dp4:\n"; |
| 131 |
Dg4Dp4->describe(out,Teuchos::VERB_EXTREME); |
| 132 |
auto Dg4Dp4_host = Dg4Dp4->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 133 |
TEST_FLOATING_EQUALITY(Dg4Dp4_host(0,0),0.0,tol); |
| 134 |
} |
| 135 |
|
| 136 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, Responses_g6_p4) |
| 137 |
{ |
| 138 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 139 |
|
| 140 |
// Get default Tpetra template types |
| 141 |
using Scalar = NOX::Scalar; |
| 142 |
using LO = NOX::LocalOrdinal; |
| 143 |
using GO = NOX::GlobalOrdinal; |
| 144 |
using Node = NOX::NodeType; |
| 145 |
using converter = ::Thyra::TpetraOperatorVectorExtraction<Scalar,LO,GO,Node>; |
| 146 |
|
| 147 |
// Create the model evaluator object |
| 148 |
Scalar x00 = 0.0; |
| 149 |
Scalar x01 = 1.0; |
| 150 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 151 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 152 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 153 |
|
| 154 |
auto g_thyra = ::Thyra::createMember(*model->get_g_space(6),"g"); |
| 155 |
auto DfDp4_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DfDp_op(4),true); |
| 156 |
auto DgDx_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDx_op(6),true); |
| 157 |
auto Dg6Dp2_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDp_op(6,2),true); |
| 158 |
auto Dg6Dp4_thyra = Teuchos::rcp_dynamic_cast<::Thyra::MultiVectorBase<Scalar>>(model->create_DgDp_op(6,4),true); |
| 159 |
|
| 160 |
auto inArgs = model->createInArgs(); |
| 161 |
auto x = ::Thyra::createMember(model->get_x_space(),"x"); |
| 162 |
::Thyra::assign(x.ptr(),1.0); |
| 163 |
inArgs.set_x(x); |
| 164 |
auto outArgs = model->createOutArgs(); |
| 165 |
|
| 166 |
outArgs.set_g(6,::Thyra::ModelEvaluatorBase::Evaluation<::Thyra::VectorBase<Scalar>>(g_thyra)); |
| 167 |
outArgs.set_DfDp(4,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(DfDp4_thyra)); |
| 168 |
outArgs.set_DgDx(6,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(DgDx_thyra,Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM)); |
| 169 |
outArgs.set_DgDp(6,2,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(Dg6Dp2_thyra)); |
| 170 |
outArgs.set_DgDp(6,4,::Thyra::ModelEvaluatorBase::Derivative<Scalar>(Dg6Dp4_thyra)); |
| 171 |
|
| 172 |
model->evalModel(inArgs,outArgs); |
| 173 |
|
| 174 |
TEST_EQUALITY(model->Np(),5); |
| 175 |
TEST_EQUALITY(model->Ng(),7); |
| 176 |
|
| 177 |
auto g = converter::getTpetraMultiVector(g_thyra); |
| 178 |
auto DfDp4 = converter::getTpetraMultiVector(DfDp4_thyra); |
| 179 |
auto DgDx = converter::getTpetraMultiVector(DgDx_thyra); |
| 180 |
auto Dg6Dp2 = converter::getTpetraMultiVector(Dg6Dp2_thyra); |
| 181 |
auto Dg6Dp4 = converter::getTpetraMultiVector(Dg6Dp4_thyra); |
| 182 |
|
| 183 |
// g = 2.0 * T(Zmax) - T(zMin) |
| 184 |
// x=1.0 |
| 185 |
auto g_host = g->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 186 |
out << "g6 = " << g_host(0,0) << std::endl; |
| 187 |
auto tol = std::numeric_limits<Scalar>::epsilon()*100.0; |
| 188 |
TEST_FLOATING_EQUALITY(g_host(0,0),1.0,tol); |
| 189 |
|
| 190 |
// DgDx: Left end node is 2, right end node is -1, the rest of the vector is zero. |
| 191 |
out << "Dg6Dx:\n"; |
| 192 |
DgDx->describe(out,Teuchos::VERB_EXTREME); |
| 193 |
|
| 194 |
{ |
| 195 |
auto DgDx_host = DgDx->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 196 |
if (comm->getRank() == 0) { |
| 197 |
TEST_FLOATING_EQUALITY(DgDx_host(0, 0), 2.0, tol); |
| 198 |
} |
| 199 |
else if (comm->getRank() == (comm->getSize() - 1)) { |
| 200 |
TEST_FLOATING_EQUALITY(DgDx_host(DgDx_host.extent(0) - 1, 0), -1.0, tol); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
Teuchos::Array<NOX::TMultiVector::mag_type> norms(1); |
| 205 |
DgDx->norm2(norms); |
| 206 |
TEST_FLOATING_EQUALITY(norms[0],std::sqrt(5.0),tol); |
| 207 |
|
| 208 |
// DfDp4 |
| 209 |
out << "DfDp4:\n"; |
| 210 |
DfDp4->describe(out,Teuchos::VERB_EXTREME); |
| 211 |
auto DfDp4_host = DfDp4->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 212 |
auto rank = comm->getRank(); |
| 213 |
for (size_t i=0; i < DfDp4_host.extent(0); ++i) { |
| 214 |
if ((rank == 0) && (i == 0)) { // left end |
| 215 |
TEST_FLOATING_EQUALITY(DfDp4_host(0,0),-1.0,tol); |
| 216 |
} |
| 217 |
else { // all other nodes are zero |
| 218 |
out << "rank=" << rank << ", i=" << i <<std::endl; |
| 219 |
TEST_FLOATING_EQUALITY(DfDp4_host(i,0),0.0,tol); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
out << "Dg6Dp2:\n"; |
| 224 |
Dg6Dp2->describe(out,Teuchos::VERB_EXTREME); |
| 225 |
auto Dg6Dp2_host = Dg6Dp2->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 226 |
TEST_FLOATING_EQUALITY(Dg6Dp2_host(0,0),0.0,tol); |
| 227 |
|
| 228 |
out << "Dg6Dp4:\n"; |
| 229 |
Dg6Dp4->describe(out,Teuchos::VERB_EXTREME); |
| 230 |
auto Dg6Dp4_host = Dg6Dp4->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 231 |
TEST_FLOATING_EQUALITY(Dg6Dp4_host(0,0),0.0,tol); |
| 232 |
} |
| 233 |
|
| 234 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM,get_index) |
| 235 |
{ |
| 236 |
// Get default Tpetra template types |
| 237 |
using Scalar = NOX::Scalar; |
| 238 |
using LO = NOX::LocalOrdinal; |
| 239 |
using GO = NOX::GlobalOrdinal; |
| 240 |
using Node = NOX::NodeType; |
| 241 |
|
| 242 |
// Create the model evaluator object |
| 243 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 244 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 245 |
Scalar x00 = 0.0; |
| 246 |
Scalar x01 = 1.0; |
| 247 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 248 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 249 |
|
| 250 |
TEST_EQUALITY(model->get_p_index("Dummy p(0)"),std::make_pair(0,0)); |
| 251 |
TEST_EQUALITY(model->get_p_index("Dummy p(1)"),std::make_pair(1,0)); |
| 252 |
TEST_EQUALITY(model->get_p_index("k"),std::make_pair(2,0)); |
| 253 |
TEST_EQUALITY(model->get_p_index("Dummy p(3)"),std::make_pair(3,0)); |
| 254 |
TEST_EQUALITY(model->get_p_index("T_left"),std::make_pair(4,0)); |
| 255 |
|
| 256 |
TEST_EQUALITY(model->get_g_index("Dummy g(0)"),std::make_pair(0,0)); |
| 257 |
TEST_EQUALITY(model->get_g_index("Dummy g(1)"),std::make_pair(1,0)); |
| 258 |
TEST_EQUALITY(model->get_g_index("Dummy g(2)"),std::make_pair(2,0)); |
| 259 |
TEST_EQUALITY(model->get_g_index("Dummy g(3)"),std::make_pair(3,0)); |
| 260 |
TEST_EQUALITY(model->get_g_index("Constraint: T_right=2"),std::make_pair(4,0)); |
| 261 |
TEST_EQUALITY(model->get_g_index("Dummy g(5)"),std::make_pair(5,0)); |
| 262 |
TEST_EQUALITY(model->get_g_index("Constraint: 2*T_left=T_right"),std::make_pair(6,0)); |
| 263 |
} |