| 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 "Thyra_TpetraThyraWrappers.hpp" |
| 28 |
#include "Thyra_VectorStdOps.hpp" |
| 29 |
#include "ME_Tpetra_1DFEM.hpp" |
| 30 |
|
| 31 |
#include "LOCA_Tpetra_ConstraintModelEvaluator.hpp" |
| 32 |
#include "NOX_Thyra_Vector.H" |
| 33 |
#include "NOX_Thyra_MultiVector.H" |
| 34 |
#include "NOX_TpetraTypedefs.hpp" |
| 35 |
|
| 36 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, Responses) |
| 37 |
{ |
| 38 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 39 |
|
| 40 |
// Get default Tpetra template types |
| 41 |
using Scalar = NOX::Scalar; |
| 42 |
using LO = NOX::LocalOrdinal; |
| 43 |
using GO = NOX::GlobalOrdinal; |
| 44 |
using Node = NOX::NodeType; |
| 45 |
using converter = ::Thyra::TpetraOperatorVectorExtraction<Scalar,LO,GO,Node>; |
| 46 |
|
| 47 |
// Create the model evaluator object |
| 48 |
Scalar x00 = 0.0; |
| 49 |
Scalar x01 = 1.0; |
| 50 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 51 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 52 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 53 |
|
| 54 |
LOCA::ParameterVector p_vec; |
| 55 |
p_vec.addParameter("k",1.0); |
| 56 |
p_vec.addParameter("T_left",1.0); |
| 57 |
std::vector<std::string> g_names; |
| 58 |
g_names.push_back("Constraint: T_right=2"); |
| 59 |
g_names.push_back("Constraint: 2*T_left=T_right"); |
| 60 |
auto x_thyra = ::Thyra::createMember(model->get_x_space(),"x"); |
| 61 |
NOX::Thyra::Vector x(x_thyra); |
| 62 |
LOCA::MultiContinuation::ConstraintModelEvaluator constraints(model,p_vec,g_names,x); |
| 63 |
|
| 64 |
// Set inputs |
| 65 |
x.init(1.0); |
| 66 |
constraints.setX(x); |
| 67 |
constraints.setParam(0,1.0); |
| 68 |
constraints.setParam(1,1.2); |
| 69 |
|
| 70 |
// Compute constraints |
| 71 |
out << "Checking constraints:\n"; |
| 72 |
constraints.computeConstraints(); |
| 73 |
auto g = constraints.getConstraints(); |
| 74 |
auto tol = std::numeric_limits<Scalar>::epsilon()*100.0; |
| 75 |
out << "g(0) = " << g(0,0) << std::endl; |
| 76 |
TEST_FLOATING_EQUALITY(g(0,0),-1.0,tol); |
| 77 |
out << "g(1) = " << g(1,0) << std::endl; |
| 78 |
TEST_FLOATING_EQUALITY(g(1,0),1.0,tol); |
| 79 |
|
| 80 |
// Compute DX |
| 81 |
out << "\nChecking DgDx:\n"; |
| 82 |
constraints.computeDX(); |
| 83 |
auto input = x.createMultiVector(1,NOX::ShapeCopy); |
| 84 |
input->init(1.0); |
| 85 |
auto result = x.createMultiVector(2,NOX::ShapeCopy); |
| 86 |
NOX::Abstract::MultiVector::DenseMatrix b(2,2); |
| 87 |
b(0,0) = 1.0; |
| 88 |
b(0,1) = 0.0; |
| 89 |
b(1,0) = 0.0; |
| 90 |
b(1,1) = 1.0; |
| 91 |
constraints.addDX(Teuchos::NO_TRANS,1.0,b,0.0,*result); |
| 92 |
auto result_thyra = Teuchos::rcp_dynamic_cast<NOX::Thyra::MultiVector>(result)->getThyraMultiVector(); |
| 93 |
auto DgDx = converter::getTpetraMultiVector(result_thyra); |
| 94 |
DgDx->describe(out,Teuchos::VERB_EXTREME); |
| 95 |
|
| 96 |
{ |
| 97 |
auto DgDx_host = DgDx->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 98 |
for (size_t i=0; i < DgDx_host.extent(0); ++i) { |
| 99 |
if ( (comm->getRank() == 0) && (i == 0) ) { |
| 100 |
TEST_FLOATING_EQUALITY(DgDx_host(0,0),0.0,tol); |
| 101 |
TEST_FLOATING_EQUALITY(DgDx_host(0,1),2.0,tol); |
| 102 |
} |
| 103 |
else if ( (comm->getRank() == (comm->getSize()-1)) && (i == DgDx_host.extent(0)-1) ) { |
| 104 |
TEST_FLOATING_EQUALITY(DgDx_host(DgDx_host.extent(0)-1,0),1.0,tol); |
| 105 |
TEST_FLOATING_EQUALITY(DgDx_host(DgDx_host.extent(0)-1,1),-1.0,tol); |
| 106 |
} |
| 107 |
else { |
| 108 |
TEST_FLOATING_EQUALITY(DgDx_host(i,0),0.0,tol); |
| 109 |
TEST_FLOATING_EQUALITY(DgDx_host(i,1),0.0,tol); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
Teuchos::Array<NOX::TMultiVector::mag_type> norms(1); |
| 115 |
DgDx->norm2(norms); |
| 116 |
TEST_FLOATING_EQUALITY(norms[0],1.0,tol); |
| 117 |
|
| 118 |
// Compute g and DgDp |
| 119 |
out << "\nChecking DgDp:\n"; |
| 120 |
NOX::Abstract::MultiVector::DenseMatrix dgdp(2,3,true); // first col is g |
| 121 |
std::vector<int> paramIDs(2); |
| 122 |
paramIDs[0] = 0; |
| 123 |
paramIDs[1] = 1; |
| 124 |
constraints.computeDP(paramIDs,dgdp,false); |
| 125 |
TEST_FLOATING_EQUALITY(dgdp(0,0),-1.0,tol); |
| 126 |
TEST_FLOATING_EQUALITY(dgdp(1,0),1.0,tol); |
| 127 |
TEST_FLOATING_EQUALITY(dgdp(0,1),0.0,tol); |
| 128 |
TEST_FLOATING_EQUALITY(dgdp(0,2),0.0,tol); |
| 129 |
TEST_FLOATING_EQUALITY(dgdp(1,1),0.0,tol); |
| 130 |
TEST_FLOATING_EQUALITY(dgdp(1,2),0.0,tol); |
| 131 |
|
| 132 |
// Test clone/copy |
| 133 |
out << "\nChecking clone and copy methods:\n"; |
| 134 |
TEST_ASSERT(constraints.isConstraints()); |
| 135 |
TEST_ASSERT(constraints.isDX()); |
| 136 |
|
| 137 |
// Shape copy does not copy values, not equal |
| 138 |
auto constraints_clone = constraints.clone(NOX::ShapeCopy); |
| 139 |
TEST_ASSERT(constraints_clone->isConstraints() != constraints.isConstraints()); |
| 140 |
TEST_ASSERT(constraints_clone->isDX() != constraints.isDX()); |
| 141 |
|
| 142 |
// Copy method should now make them equal |
| 143 |
constraints_clone->copy(constraints); |
| 144 |
TEST_ASSERT(constraints_clone->isConstraints() == constraints.isConstraints()); |
| 145 |
TEST_ASSERT(constraints_clone->isDX() == constraints.isDX()); |
| 146 |
|
| 147 |
// Let's change the solution so everyhting is invalid in the clone |
| 148 |
constraints_clone->setParam(0,0.0); |
| 149 |
TEST_ASSERT(constraints_clone->isConstraints() != constraints.isConstraints()); |
| 150 |
TEST_ASSERT(constraints_clone->isDX() != constraints.isDX()); |
| 151 |
|
| 152 |
// Deep copy should make everything equal |
| 153 |
constraints_clone = constraints.clone(NOX::DeepCopy); |
| 154 |
TEST_ASSERT(constraints_clone->isConstraints() == constraints.isConstraints()); |
| 155 |
TEST_ASSERT(constraints_clone->isDX() == constraints.isDX()); |
| 156 |
} |