| 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 |
#ifndef NOX_TPETRA_ME_1DFEM_DEF_HPP |
| 10 |
#define NOX_TPETRA_ME_1DFEM_DEF_HPP |
| 11 |
|
| 12 |
// Thyra support |
| 13 |
#include "Thyra_DefaultSpmdVectorSpace.hpp" |
| 14 |
#include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp" |
| 15 |
#include "Thyra_DetachedMultiVectorView.hpp" |
| 16 |
#include "Thyra_DetachedVectorView.hpp" |
| 17 |
#include "Thyra_MultiVectorStdOps.hpp" |
| 18 |
#include "Thyra_VectorStdOps.hpp" |
| 19 |
#include "Thyra_PreconditionerBase.hpp" |
| 20 |
#include "Tpetra_Import_Util2.hpp" //for sortCrsEntries |
| 21 |
|
| 22 |
// Tpetra support |
| 23 |
#include "Thyra_TpetraThyraWrappers.hpp" |
| 24 |
#include "NOX_TpetraTypedefs.hpp" |
| 25 |
|
| 26 |
// Kokkos support |
| 27 |
#include "Kokkos_Core.hpp" |
| 28 |
|
| 29 |
#include "1DFEM_Functors.hpp" |
| 30 |
|
| 31 |
// Nonmember constuctors |
| 32 |
|
| 33 |
template<class Scalar, class LO, class GO, class Node> |
| 34 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar, LO, GO, Node> > |
| 35 |
evaluatorTpetra1DFEM(const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 36 |
const Tpetra::global_size_t numGlobalElements, |
| 37 |
const Scalar zMin, |
| 38 |
const Scalar zMax) |
| 39 |
{ |
| 40 |
return Teuchos::rcp(new EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>(comm,numGlobalElements,zMin,zMax)); |
| 41 |
} |
| 42 |
|
| 43 |
// Constructor |
| 44 |
|
| 45 |
template<class Scalar, class LO, class GO, class Node> |
| 46 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 47 |
EvaluatorTpetra1DFEM(const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 48 |
const Tpetra::global_size_t numGlobalElements, |
| 49 |
const Scalar zMin, |
| 50 |
const Scalar zMax) : |
| 51 |
comm_(comm), |
| 52 |
numGlobalElements_(numGlobalElements), |
| 53 |
zMin_(zMin), |
| 54 |
zMax_(zMax), |
| 55 |
Np_(5), |
| 56 |
Ng_(7), |
| 57 |
printDebug_(false), |
| 58 |
showGetInvalidArg_(false), |
| 59 |
pNames_(Np_), |
| 60 |
gNames_(Ng_) |
| 61 |
{ |
| 62 |
typedef ::Thyra::ModelEvaluatorBase MEB; |
| 63 |
typedef Teuchos::ScalarTraits<scalar_type> ST; |
| 64 |
|
| 65 |
TEUCHOS_ASSERT(nonnull(comm_)); |
| 66 |
|
| 67 |
const Tpetra::global_size_t numNodes = numGlobalElements_ + 1; |
| 68 |
|
| 69 |
// owned space |
| 70 |
GO indexBase = 0; |
| 71 |
xOwnedMap_ = Teuchos::rcp(new const tpetra_map(numNodes, indexBase, comm_)); |
| 72 |
xSpace_ = ::Thyra::createVectorSpace<Scalar, LO, GO, Node>(xOwnedMap_); |
| 73 |
|
| 74 |
// ghosted space |
| 75 |
if (comm_->getSize() == 1) { |
| 76 |
xGhostedMap_ = xOwnedMap_; |
| 77 |
} |
| 78 |
else { |
| 79 |
std::size_t overlapNumMyNodes; |
| 80 |
GO overlapMinMyGID; |
| 81 |
overlapNumMyNodes = xOwnedMap_->getLocalNumElements() + 2; |
| 82 |
if ( (comm_->getRank() == 0) || (comm_->getRank() == (comm_->getSize() - 1)) ) |
| 83 |
--overlapNumMyNodes; |
| 84 |
|
| 85 |
if (comm_->getRank() == 0) |
| 86 |
overlapMinMyGID = xOwnedMap_->getMinGlobalIndex(); |
| 87 |
else |
| 88 |
overlapMinMyGID = xOwnedMap_->getMinGlobalIndex() - 1; |
| 89 |
|
| 90 |
Teuchos::Array<GO> overlapMyGlobalNodes(overlapNumMyNodes); |
| 91 |
GO gid = overlapMinMyGID; |
| 92 |
for (auto gidIter = overlapMyGlobalNodes.begin(); gidIter != overlapMyGlobalNodes.end(); ++gidIter) { |
| 93 |
*gidIter = gid; |
| 94 |
++gid; |
| 95 |
} |
| 96 |
|
| 97 |
const Tpetra::global_size_t invalid = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(); |
| 98 |
xGhostedMap_ = Teuchos::rcp(new tpetra_map(invalid, overlapMyGlobalNodes, indexBase, comm_)); |
| 99 |
} |
| 100 |
|
| 101 |
importer_ = Teuchos::rcp(new Tpetra::Import<LO,GO,Node>(xOwnedMap_, xGhostedMap_)); |
| 102 |
|
| 103 |
// residual space |
| 104 |
fOwnedMap_ = xOwnedMap_; |
| 105 |
fSpace_ = xSpace_; |
| 106 |
|
| 107 |
x0_ = ::Thyra::createMember(xSpace_); |
| 108 |
V_S(x0_.ptr(), ST::zero()); |
| 109 |
|
| 110 |
// Initialize the graph for W CrsMatrix object |
| 111 |
W_graph_ = createGraph(); |
| 112 |
|
| 113 |
// Create the nodal coorinates |
| 114 |
std::size_t numLocalNodes = xOwnedMap_->getLocalNumElements(); |
| 115 |
GO minGID = xOwnedMap_->getMinGlobalIndex(); |
| 116 |
Scalar dz = (zMax_ - zMin_)/static_cast<Scalar>(numGlobalElements_); |
| 117 |
nodeCoordinates_ = Teuchos::rcp(new tpetra_vec(xOwnedMap_)); |
| 118 |
|
| 119 |
MeshFillFunctor<tpetra_vec> functor(*nodeCoordinates_, zMin_, dz, minGID); |
| 120 |
Kokkos::parallel_for("coords fill", numLocalNodes, functor); |
| 121 |
|
| 122 |
MEB::InArgsSetup<Scalar> inArgs; |
| 123 |
inArgs.setModelEvalDescription(this->description()); |
| 124 |
inArgs.setSupports(MEB::IN_ARG_x); |
| 125 |
inArgs.set_Np_Ng(Np_,Ng_); |
| 126 |
prototypeInArgs_ = inArgs; |
| 127 |
|
| 128 |
MEB::OutArgsSetup<Scalar> outArgs; |
| 129 |
outArgs.setModelEvalDescription(this->description()); |
| 130 |
outArgs.setSupports(MEB::OUT_ARG_f); |
| 131 |
outArgs.setSupports(MEB::OUT_ARG_W_op); |
| 132 |
outArgs.setSupports(MEB::OUT_ARG_W_prec); |
| 133 |
outArgs.set_Np_Ng(Np_,Ng_); |
| 134 |
outArgs.setSupports(MEB::OUT_ARG_DfDp,2,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 135 |
outArgs.setSupports(MEB::OUT_ARG_DgDx,4,MEB::DerivativeSupport(MEB::DERIV_MV_GRADIENT_FORM)); |
| 136 |
outArgs.setSupports(MEB::OUT_ARG_DgDp,4,2,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 137 |
|
| 138 |
outArgs.setSupports(MEB::OUT_ARG_DfDp,4,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 139 |
outArgs.setSupports(MEB::OUT_ARG_DgDx,6,MEB::DerivativeSupport(MEB::DERIV_MV_GRADIENT_FORM)); |
| 140 |
outArgs.setSupports(MEB::OUT_ARG_DgDp,4,4,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 141 |
outArgs.setSupports(MEB::OUT_ARG_DgDp,6,2,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 142 |
outArgs.setSupports(MEB::OUT_ARG_DgDp,6,4,MEB::DerivativeSupport(MEB::DERIV_MV_JACOBIAN_FORM)); |
| 143 |
|
| 144 |
prototypeOutArgs_ = outArgs; |
| 145 |
|
| 146 |
nominalValues_ = inArgs; |
| 147 |
nominalValues_.set_x(x0_); |
| 148 |
|
| 149 |
residTimer_ = Teuchos::TimeMonitor::getNewCounter("Model Evaluator: Residual Evaluation"); |
| 150 |
jacTimer_ = Teuchos::TimeMonitor::getNewCounter("Model Evaluator: Jacobian Evaluation"); |
| 151 |
|
| 152 |
// Parameter and response support. There exists one parameter and one response. |
| 153 |
for (auto& p : pNames_) |
| 154 |
p = Teuchos::rcp(new Teuchos::Array<std::string>); |
| 155 |
pNames_[0]->push_back("Dummy p(0)"); |
| 156 |
pNames_[1]->push_back("Dummy p(1)"); |
| 157 |
pNames_[2]->push_back("k"); |
| 158 |
pNames_[3]->push_back("Dummy p(3)"); |
| 159 |
pNames_[4]->push_back("T_left"); |
| 160 |
pMap_ = Teuchos::rcp(new const tpetra_map(1, 0, comm_, Tpetra::LocallyReplicated)); |
| 161 |
pSpace_ = ::Thyra::createVectorSpace<Scalar, LO, GO, Node>(pMap_); |
| 162 |
p2_ = ::Thyra::createMember(pSpace_); |
| 163 |
V_S(p2_.ptr(),1.0); |
| 164 |
nominalValues_.set_p(2,p2_); |
| 165 |
p4_ = ::Thyra::createMember(pSpace_); |
| 166 |
V_S(p4_.ptr(),1.0); |
| 167 |
nominalValues_.set_p(4,p4_); |
| 168 |
|
| 169 |
for (auto& g : gNames_) |
| 170 |
g.clear(); |
| 171 |
gNames_[0].push_back("Dummy g(0)"); |
| 172 |
gNames_[1].push_back("Dummy g(1)"); |
| 173 |
gNames_[2].push_back("Dummy g(2)"); |
| 174 |
gNames_[3].push_back("Dummy g(3)"); |
| 175 |
gNames_[4].push_back("Constraint: T_right=2"); |
| 176 |
gNames_[5].push_back("Dummy g(5)"); |
| 177 |
gNames_[6].push_back("Constraint: 2*T_left=T_right"); |
| 178 |
gMap_ = Teuchos::rcp(new const tpetra_map(1, 0, comm_, Tpetra::LocallyReplicated)); |
| 179 |
gSpace_ = ::Thyra::createVectorSpace<Scalar, LO, GO, Node>(gMap_); |
| 180 |
dgdpMap_ = Teuchos::rcp(new const tpetra_map(1, 0, comm_, Tpetra::LocallyReplicated)); |
| 181 |
dgdpSpace_ = ::Thyra::createVectorSpace<Scalar, LO, GO, Node>(dgdpMap_); |
| 182 |
|
| 183 |
p_name_to_index_["Dummy p(0)"] = std::make_pair(0,0); |
| 184 |
p_name_to_index_["Dummy p(1)"] = std::make_pair(1,0); |
| 185 |
p_name_to_index_["k"] = std::make_pair(2,0); |
| 186 |
p_name_to_index_["Dummy p(3)"] = std::make_pair(3,0); |
| 187 |
p_name_to_index_["T_left"] = std::make_pair(4,0); |
| 188 |
|
| 189 |
g_name_to_index_["Dummy g(0)"] = std::make_pair(0,0); |
| 190 |
g_name_to_index_["Dummy g(1)"] = std::make_pair(1,0); |
| 191 |
g_name_to_index_["Dummy g(2)"] = std::make_pair(2,0); |
| 192 |
g_name_to_index_["Dummy g(3)"] = std::make_pair(3,0); |
| 193 |
g_name_to_index_["Constraint: T_right=2"] = std::make_pair(4,0); |
| 194 |
g_name_to_index_["Dummy g(5)"] = std::make_pair(5,0); |
| 195 |
g_name_to_index_["Constraint: 2*T_left=T_right"] = std::make_pair(6,0); |
| 196 |
} |
| 197 |
|
| 198 |
// Initializers/Accessors |
| 199 |
|
| 200 |
template<class Scalar, class LO, class GO, class Node> |
| 201 |
Teuchos::RCP<const Tpetra::CrsGraph<LO, GO, Node> > |
| 202 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::createGraph() |
| 203 |
{ |
| 204 |
using size_type = typename tpetra_graph::local_graph_device_type::size_type; |
| 205 |
|
| 206 |
// Compute graph offset array |
| 207 |
int numProcs = comm_->getSize(); |
| 208 |
int myRank = comm_->getRank(); |
| 209 |
std::size_t numMyNodes = xOwnedMap_->getLocalNumElements(); |
| 210 |
std::size_t numLocalEntries = 0; |
| 211 |
//Kokkos::View<std::size_t*> counts("row counts", numMyNodes); |
| 212 |
Kokkos::View<size_type*> counts("row counts", numMyNodes); |
| 213 |
{ |
| 214 |
RowCountsFunctor<size_type, LO> functor(counts, numMyNodes, numProcs, myRank); |
| 215 |
Kokkos::parallel_reduce("row counts comp", numMyNodes, functor, numLocalEntries); |
| 216 |
} |
| 217 |
|
| 218 |
//Kokkos::View<std::size_t*> offsets("row offsets", numMyNodes+1); |
| 219 |
Kokkos::View<size_type*> offsets("row offsets", numMyNodes+1); |
| 220 |
{ |
| 221 |
RowOffsetsFunctor<size_type, LO> functor(offsets, counts, numMyNodes); |
| 222 |
Kokkos::parallel_scan("row offsets comp", numMyNodes+1, functor); |
| 223 |
} |
| 224 |
|
| 225 |
// Create array of non-zero entry column indices |
| 226 |
Kokkos::View<LO*> indices("column indices", numLocalEntries); |
| 227 |
//typename local_graph_type::entries_type::non_const_type indices("column indices", numLocalEntries); |
| 228 |
{ |
| 229 |
ColumnIndexCompFunctor<size_type, LO> functor(indices, offsets, counts, numMyNodes, numProcs, myRank); |
| 230 |
Kokkos::parallel_for("column indices comp", numMyNodes, functor); |
| 231 |
} |
| 232 |
|
| 233 |
//Sort the indices within each row. |
| 234 |
Tpetra::Import_Util::sortCrsEntries(offsets, indices); |
| 235 |
|
| 236 |
// Construct the graph |
| 237 |
Teuchos::RCP<tpetra_graph> W_graph = |
| 238 |
Teuchos::rcp(new tpetra_graph(xOwnedMap_, xGhostedMap_, offsets, indices)); |
| 239 |
W_graph->fillComplete(); |
| 240 |
return W_graph; |
| 241 |
} |
| 242 |
|
| 243 |
template<class Scalar, class LO, class GO, class Node> |
| 244 |
void EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 245 |
set_x0(const Teuchos::ArrayView<const Scalar> &x0_in) |
| 246 |
{ |
| 247 |
#ifdef TEUCHOS_DEBUG |
| 248 |
TEUCHOS_ASSERT_EQUALITY(xSpace_->dim(), x0_in.size()); |
| 249 |
#endif |
| 250 |
Thyra::DetachedVectorView<Scalar> x0(x0_); |
| 251 |
x0.sv().values()().assign(x0_in); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
template<class Scalar, class LO, class GO, class Node> |
| 256 |
void EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 257 |
setShowGetInvalidArgs(bool showGetInvalidArg) |
| 258 |
{ |
| 259 |
showGetInvalidArg_ = showGetInvalidArg; |
| 260 |
} |
| 261 |
|
| 262 |
template<class Scalar, class LO, class GO, class Node> |
| 263 |
void EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 264 |
set_W_factory(const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<Scalar> >& W_factory) |
| 265 |
{ |
| 266 |
W_factory_ = W_factory; |
| 267 |
} |
| 268 |
|
| 269 |
// Public functions overridden from ModelEvaulator |
| 270 |
|
| 271 |
|
| 272 |
template<class Scalar, class LO, class GO, class Node> |
| 273 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 274 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_x_space() const |
| 275 |
{ |
| 276 |
return xSpace_; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
template<class Scalar, class LO, class GO, class Node> |
| 281 |
Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > |
| 282 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_f_space() const |
| 283 |
{ |
| 284 |
return fSpace_; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
template<class Scalar, class LO, class GO, class Node> |
| 289 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 290 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::getNominalValues() const |
| 291 |
{ |
| 292 |
return nominalValues_; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
template<class Scalar, class LO, class GO, class Node> |
| 297 |
Teuchos::RCP<Thyra::LinearOpBase<Scalar> > |
| 298 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_W_op() const |
| 299 |
{ |
| 300 |
Teuchos::RCP<tpetra_matrix> W_tpetra = Teuchos::rcp(new tpetra_matrix(W_graph_)); |
| 301 |
|
| 302 |
return Thyra::tpetraLinearOp<Scalar, LO, GO, Node>(fSpace_, xSpace_, W_tpetra); |
| 303 |
} |
| 304 |
|
| 305 |
template<class Scalar, class LO, class GO, class Node> |
| 306 |
Teuchos::RCP< ::Thyra::PreconditionerBase<Scalar> > |
| 307 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_W_prec() const |
| 308 |
{ |
| 309 |
Teuchos::RCP<tpetra_matrix> W_tpetra = Teuchos::rcp(new tpetra_matrix(W_graph_)); |
| 310 |
|
| 311 |
Teuchos::RCP<thyra_op> W_op = Thyra::tpetraLinearOp<Scalar, LO, GO, Node>(fSpace_, xSpace_, W_tpetra); |
| 312 |
|
| 313 |
Teuchos::RCP<Thyra::DefaultPreconditioner<Scalar> > prec = |
| 314 |
Teuchos::rcp(new Thyra::DefaultPreconditioner<Scalar>); |
| 315 |
|
| 316 |
prec->initializeRight(W_op); |
| 317 |
|
| 318 |
return prec; |
| 319 |
} |
| 320 |
|
| 321 |
template<class Scalar, class LO, class GO, class Node> |
| 322 |
int EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::Np() const |
| 323 |
{return Np_;} |
| 324 |
|
| 325 |
template<class Scalar, class LO, class GO, class Node> |
| 326 |
int EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::Ng() const |
| 327 |
{return Ng_;} |
| 328 |
|
| 329 |
template<class Scalar, class LO, class GO, class Node> |
| 330 |
Teuchos::RCP<const ::Thyra::VectorSpaceBase<Scalar> > |
| 331 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_p_space(int /* l */) const |
| 332 |
{ |
| 333 |
// All parameters are locally replicated scalars of size 1 |
| 334 |
return pSpace_; |
| 335 |
} |
| 336 |
|
| 337 |
template<class Scalar, class LO, class GO, class Node> |
| 338 |
Teuchos::RCP<const Teuchos::Array<std::string> > |
| 339 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_p_names(int l) const |
| 340 |
{ |
| 341 |
return pNames_[l]; |
| 342 |
} |
| 343 |
|
| 344 |
template<class Scalar, class LO, class GO, class Node> |
| 345 |
Teuchos::RCP<const ::Thyra::VectorSpaceBase<Scalar> > |
| 346 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_g_space(int /* j */) const |
| 347 |
{ |
| 348 |
// All parameters are locally replicated scalars of size 1 |
| 349 |
return gSpace_; |
| 350 |
} |
| 351 |
|
| 352 |
template<class Scalar, class LO, class GO, class Node> |
| 353 |
Teuchos::ArrayView<const std::string> |
| 354 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_g_names(int j) const |
| 355 |
{ |
| 356 |
return gNames_[j]; |
| 357 |
} |
| 358 |
|
| 359 |
template<class Scalar, class LO, class GO, class Node> |
| 360 |
Teuchos::RCP<::Thyra::LinearOpBase<Scalar>> |
| 361 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_DfDp_op(int l) const |
| 362 |
{ |
| 363 |
TEUCHOS_ASSERT( (l == 2) || (l == 4) ); |
| 364 |
return ::Thyra::createMembers(xSpace_,1,"LOCA::DgDx"); |
| 365 |
} |
| 366 |
|
| 367 |
template<class Scalar, class LO, class GO, class Node> |
| 368 |
Teuchos::RCP<::Thyra::LinearOpBase<Scalar> > |
| 369 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_DgDx_op(int j) const |
| 370 |
{ |
| 371 |
TEUCHOS_ASSERT( (j == 4) || (j == 6) ); |
| 372 |
return ::Thyra::createMembers(xSpace_,1,"LOCA::DgDx"); |
| 373 |
} |
| 374 |
|
| 375 |
template<class Scalar, class LO, class GO, class Node> |
| 376 |
Teuchos::RCP<::Thyra::LinearOpBase<Scalar> > |
| 377 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_DgDx_dot_op(int j) const |
| 378 |
{ |
| 379 |
TEUCHOS_ASSERT( (j == 4) || (j == 6) ); |
| 380 |
return ::Thyra::createMembers(xSpace_,1,"LOCA::DgDx_dot"); |
| 381 |
} |
| 382 |
|
| 383 |
template<class Scalar, class LO, class GO, class Node> |
| 384 |
::Teuchos::RCP<::Thyra::LinearOpBase<Scalar> > |
| 385 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::create_DgDp_op( int j, int l ) const |
| 386 |
{ |
| 387 |
TEUCHOS_ASSERT( (j == 4) || (j == 6) ); |
| 388 |
TEUCHOS_ASSERT( (l == 2) || (l == 4) ); |
| 389 |
// Instead of using a dense serial matrix, we use a locally |
| 390 |
// replicated multivector since it provides thyra wrappers. |
| 391 |
return ::Thyra::createMembers(gSpace_,1,"LOCA::DgDp"); |
| 392 |
} |
| 393 |
|
| 394 |
template<class Scalar, class LO, class GO, class Node> |
| 395 |
Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 396 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_W_factory() const |
| 397 |
{ |
| 398 |
return W_factory_; |
| 399 |
} |
| 400 |
|
| 401 |
template<class Scalar, class LO, class GO, class Node> |
| 402 |
Thyra::ModelEvaluatorBase::InArgs<Scalar> |
| 403 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::createInArgs() const |
| 404 |
{ |
| 405 |
return prototypeInArgs_; |
| 406 |
} |
| 407 |
|
| 408 |
template<class Scalar, class LO, class GO, class Node> |
| 409 |
Thyra::ModelEvaluatorBase::OutArgs<Scalar> |
| 410 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::createOutArgs() const |
| 411 |
{ |
| 412 |
return prototypeOutArgs_; |
| 413 |
} |
| 414 |
|
| 415 |
template<class Scalar, class LO, class GO, class Node> |
| 416 |
void EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 417 |
evalModel(const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs, |
| 418 |
const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const |
| 419 |
{ |
| 420 |
TEUCHOS_ASSERT(nonnull(inArgs.get_x())); |
| 421 |
|
| 422 |
const Teuchos::RCP<thyra_vec> f_out = outArgs.get_f(); |
| 423 |
const Teuchos::RCP<thyra_op> W_out = outArgs.get_W_op(); |
| 424 |
const Teuchos::RCP<thyra_prec> W_prec_out = outArgs.get_W_prec(); |
| 425 |
const Teuchos::RCP<thyra_vec> g4_out = outArgs.get_g(4); |
| 426 |
const Teuchos::RCP<thyra_vec> g6_out = outArgs.get_g(6); |
| 427 |
const Teuchos::RCP<thyra_mvec> Dg4Dx_out = outArgs.get_DgDx(4).getMultiVector(); |
| 428 |
const Teuchos::RCP<thyra_mvec> Dg6Dx_out = outArgs.get_DgDx(6).getMultiVector(); |
| 429 |
const Teuchos::RCP<thyra_mvec> DfDp2_out = outArgs.get_DfDp(2).getMultiVector(); |
| 430 |
const Teuchos::RCP<thyra_mvec> DfDp4_out = outArgs.get_DfDp(4).getMultiVector(); |
| 431 |
const Teuchos::RCP<thyra_mvec> Dg4Dp2_out = outArgs.get_DgDp(4,2).getMultiVector(); |
| 432 |
const Teuchos::RCP<thyra_mvec> Dg4Dp4_out = outArgs.get_DgDp(4,4).getMultiVector(); |
| 433 |
const Teuchos::RCP<thyra_mvec> Dg6Dp2_out = outArgs.get_DgDp(6,2).getMultiVector(); |
| 434 |
const Teuchos::RCP<thyra_mvec> Dg6Dp4_out = outArgs.get_DgDp(6,4).getMultiVector(); |
| 435 |
|
| 436 |
const bool fill_f = nonnull(f_out); |
| 437 |
const bool fill_W = nonnull(W_out); |
| 438 |
const bool fill_W_prec = nonnull(W_prec_out); |
| 439 |
const bool fill_g4 = nonnull(g4_out); |
| 440 |
const bool fill_g6 = nonnull(g6_out); |
| 441 |
const bool fill_Dg4Dx = nonnull(Dg4Dx_out); |
| 442 |
const bool fill_Dg6Dx = nonnull(Dg6Dx_out); |
| 443 |
const bool fill_DfDp2 = nonnull(DfDp2_out); |
| 444 |
const bool fill_DfDp4 = nonnull(DfDp4_out); |
| 445 |
const bool fill_Dg4Dp2 = nonnull(Dg4Dp2_out); |
| 446 |
const bool fill_Dg4Dp4 = nonnull(Dg4Dp4_out); |
| 447 |
const bool fill_Dg6Dp2 = nonnull(Dg6Dp2_out); |
| 448 |
const bool fill_Dg6Dp4 = nonnull(Dg6Dp4_out); |
| 449 |
|
| 450 |
if (printDebug_) { |
| 451 |
std::cout << "DEBUG: In evalModel: f=" << fill_f |
| 452 |
<< ",W=" << fill_W |
| 453 |
<< ",W_prec=" << fill_W_prec |
| 454 |
<< ",g4=" << fill_g4 |
| 455 |
<< ",Dg4Dx=" << fill_Dg4Dx |
| 456 |
<< ",Dg6Dx=" << fill_Dg6Dx |
| 457 |
<< ",DfDp2=" << fill_DfDp2 |
| 458 |
<< ",DfDp4=" << fill_DfDp4 |
| 459 |
<< ",Dg4Dp2=" << fill_Dg4Dp2 |
| 460 |
<< ",Dg4Dp4=" << fill_Dg4Dp4 |
| 461 |
<< ",Dg6Dp2=" << fill_Dg6Dp2 |
| 462 |
<< ",Dg6Dp4=" << fill_Dg6Dp4 |
| 463 |
<< std::endl; |
| 464 |
} |
| 465 |
|
| 466 |
using tpetra_op = Tpetra::Operator<Scalar,LO,GO,Node>; |
| 467 |
using tpetra_extract = ::Thyra::TpetraOperatorVectorExtraction<Scalar,LO,GO,Node>; |
| 468 |
|
| 469 |
// Create ghosted objects |
| 470 |
if (is_null(uPtr_)) |
| 471 |
uPtr_ = Teuchos::rcp(new tpetra_vec(xGhostedMap_)); |
| 472 |
|
| 473 |
uPtr_->doImport(*(tpetra_extract::getConstTpetraVector(inArgs.get_x())), *importer_, Tpetra::REPLACE); |
| 474 |
|
| 475 |
if (is_null(xPtr_)) { |
| 476 |
xPtr_ = Teuchos::rcp(new tpetra_vec(xGhostedMap_)); |
| 477 |
xPtr_->doImport(*nodeCoordinates_, *importer_, Tpetra::INSERT); |
| 478 |
} |
| 479 |
|
| 480 |
// Sizes for functors |
| 481 |
int myRank = comm_->getRank(); |
| 482 |
std::size_t numMyElements = xGhostedMap_->getLocalNumElements()-1; |
| 483 |
|
| 484 |
// Get parameters, default is from nominal values |
| 485 |
auto k_tpetra = tpetra_extract::getConstTpetraMultiVector(nominalValues_.get_p(2)); |
| 486 |
if (nonnull(inArgs.get_p(2))) |
| 487 |
k_tpetra = tpetra_extract::getConstTpetraMultiVector(inArgs.get_p(2)); |
| 488 |
|
| 489 |
Scalar k_val = (k_tpetra->getLocalViewHost(Tpetra::Access::ReadOnly))(0,0); |
| 490 |
|
| 491 |
auto p4_tpetra = tpetra_extract::getConstTpetraMultiVector(nominalValues_.get_p(4)); |
| 492 |
if (nonnull(inArgs.get_p(4))) |
| 493 |
p4_tpetra = tpetra_extract::getConstTpetraMultiVector(inArgs.get_p(4)); |
| 494 |
|
| 495 |
Scalar p4_val = (p4_tpetra->getLocalViewHost(Tpetra::Access::ReadOnly))(0,0); |
| 496 |
|
| 497 |
if (printDebug_) { |
| 498 |
if (nonnull(inArgs.get_p(2))) |
| 499 |
std::cout << "*** p2: k (NOT nominal)=" << k_val << std::endl; |
| 500 |
else |
| 501 |
std::cout << "*** p2: k (IS nominal)=" << k_val << std::endl; |
| 502 |
if (nonnull(inArgs.get_p(4))) |
| 503 |
std::cout << "*** p4: T_left (NOT nominal)=" << p4_val << std::endl; |
| 504 |
else |
| 505 |
std::cout << "*** p4: T_left (IS nominal)=" << p4_val << std::endl; |
| 506 |
} |
| 507 |
|
| 508 |
// Residual fill |
| 509 |
if (fill_f) { |
| 510 |
Teuchos::TimeMonitor timer(*residTimer_); |
| 511 |
Teuchos::RCP<tpetra_vec> f = tpetra_extract::getTpetraVector(f_out); |
| 512 |
f->putScalar(0.0); |
| 513 |
ResidualEvaluatorFunctor<tpetra_vec> functor(*f, *xPtr_, *uPtr_, myRank, k_val, p4_val); |
| 514 |
Kokkos::parallel_for("residual evaluation", numMyElements, functor); |
| 515 |
NOX::DeviceSpace().fence(); |
| 516 |
} |
| 517 |
|
| 518 |
// Jacobian fill |
| 519 |
if (fill_W) { |
| 520 |
Teuchos::TimeMonitor timer(*jacTimer_); |
| 521 |
Teuchos::RCP<tpetra_op> W_tpetra = tpetra_extract::getTpetraOperator(W_out); |
| 522 |
Teuchos::RCP<tpetra_matrix> J = Teuchos::rcp_dynamic_cast<tpetra_matrix>(W_tpetra); |
| 523 |
TEUCHOS_ASSERT(nonnull(J)); |
| 524 |
J->resumeFill(); |
| 525 |
J->setAllToScalar(0.0); |
| 526 |
JacobianEvaluatorFunctor<tpetra_vec, tpetra_matrix> functor(*J, *xPtr_, *uPtr_, myRank, k_val); |
| 527 |
Kokkos::parallel_for("jacobian evaluation", numMyElements, functor); |
| 528 |
NOX::DeviceSpace().fence(); |
| 529 |
J->fillComplete(); |
| 530 |
} |
| 531 |
|
| 532 |
// Preconditioner fill |
| 533 |
if (fill_W_prec) { |
| 534 |
Teuchos::RCP<tpetra_matrix> M_inv; |
| 535 |
Teuchos::RCP<tpetra_op> M_tpetra = tpetra_extract::getTpetraOperator(W_prec_out->getNonconstRightPrecOp()); |
| 536 |
M_inv = Teuchos::rcp_dynamic_cast<tpetra_matrix>(M_tpetra); |
| 537 |
TEUCHOS_ASSERT(nonnull(M_inv)); |
| 538 |
M_inv->resumeFill(); |
| 539 |
if (is_null(J_diagonal_)) |
| 540 |
J_diagonal_ = Teuchos::rcp(new tpetra_vec(xOwnedMap_)); |
| 541 |
|
| 542 |
M_inv->setAllToScalar(0.0); |
| 543 |
J_diagonal_->putScalar(0.0); |
| 544 |
PreconditionerEvaluatorFunctor<tpetra_vec, tpetra_matrix> functor(*M_inv, *xPtr_, *uPtr_, myRank, k_val); |
| 545 |
Kokkos::parallel_for("prec evaluation", numMyElements, functor); |
| 546 |
NOX::DeviceSpace().fence(); |
| 547 |
|
| 548 |
// Invert the Jacobian diagonal for the preconditioner |
| 549 |
// For some reason the matrix must be fill complete before calling rightScale |
| 550 |
M_inv->fillComplete(); |
| 551 |
tpetra_vec& diag = *J_diagonal_; |
| 552 |
M_inv->getLocalDiagCopy(diag); |
| 553 |
diag.reciprocal(diag); |
| 554 |
M_inv->rightScale(diag); |
| 555 |
M_inv->rightScale(diag); |
| 556 |
} |
| 557 |
|
| 558 |
// Fill Responses. These are so small, we will do it on host. Don't |
| 559 |
// waste time with parallel dispatch. |
| 560 |
auto x = tpetra_extract::getConstTpetraVector(inArgs.get_x())->getLocalViewHost(Tpetra::Access::ReadOnly); |
| 561 |
|
| 562 |
if (fill_g4) { |
| 563 |
// g4 is locally replicated. |
| 564 |
auto g4_tpetra = tpetra_extract::getTpetraMultiVector(g4_out); |
| 565 |
|
| 566 |
// g4 = T(Zmax) - 2.0 |
| 567 |
Scalar T_right = x(x.extent(0)-1,0); |
| 568 |
Teuchos::broadcast(*comm_,comm_->getSize()-1,&T_right); |
| 569 |
auto g4_host = g4_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 570 |
g4_host(0,0) = T_right - 2.0; |
| 571 |
if (printDebug_) |
| 572 |
std::cout << "evalModel: g(4)= T_right - 2.0 =" << g4_host(0,0) << " T_right=" << T_right << std::endl; |
| 573 |
} |
| 574 |
|
| 575 |
if (fill_g6) { |
| 576 |
Scalar T_left = x(0,0); |
| 577 |
Teuchos::broadcast(*comm_,0,&T_left); |
| 578 |
|
| 579 |
Scalar T_right = x(x.extent(0)-1,0); |
| 580 |
Teuchos::broadcast(*comm_,comm_->getSize()-1,&T_right); |
| 581 |
|
| 582 |
// g6 = 2* T(Zmin) - T(Zmax) |
| 583 |
// g6 is locally replicated. |
| 584 |
auto g6_tpetra = tpetra_extract::getTpetraMultiVector(g6_out); |
| 585 |
auto g6_host = g6_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 586 |
g6_host(0,0) = 2.0 * T_left - T_right; |
| 587 |
if (printDebug_) |
| 588 |
std::cout << "evalModel: g(6)= 2 * T_left - T_right =" << g6_host(0,0) << " T_left=" << T_left << " T_right=" |
| 589 |
<< T_right << std::endl; |
| 590 |
} |
| 591 |
|
| 592 |
if (fill_Dg4Dx) { |
| 593 |
auto Dg4Dx_tpetra = tpetra_extract::getTpetraMultiVector(Dg4Dx_out); |
| 594 |
Dg4Dx_tpetra->putScalar(0.0); |
| 595 |
|
| 596 |
// Right most value |
| 597 |
if (comm_->getRank() == (comm_->getSize()-1)) { |
| 598 |
auto Dg4Dx_host = Dg4Dx_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 599 |
Dg4Dx_host(Dg4Dx_host.extent(0)-1,0) = 1.0; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
if (fill_Dg6Dx) { |
| 604 |
auto Dg6Dx_tpetra = tpetra_extract::getTpetraMultiVector(Dg6Dx_out); |
| 605 |
Dg6Dx_tpetra->putScalar(0.0); |
| 606 |
|
| 607 |
// Left most value |
| 608 |
if (comm_->getRank() == 0) { |
| 609 |
auto Dg6Dx_host = Dg6Dx_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 610 |
Dg6Dx_host(0,0) = 2.0; |
| 611 |
} |
| 612 |
// Right most value |
| 613 |
if (comm_->getRank() == (comm_->getSize()-1)) { |
| 614 |
auto Dg6Dx_host = Dg6Dx_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 615 |
Dg6Dx_host(Dg6Dx_host.extent(0)-1,0) = -1.0; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if (fill_DfDp2) { |
| 620 |
auto DfDp2_tpetra = tpetra_extract::getTpetraMultiVector(DfDp2_out); |
| 621 |
DfDp2_tpetra->putScalar(0.0); |
| 622 |
|
| 623 |
DfDp2EvaluatorFunctor<NOX::TMultiVector> functor(*DfDp2_tpetra, *xPtr_, *uPtr_, myRank, k_val); |
| 624 |
Kokkos::parallel_for("DfDp2 evaluation", numMyElements, functor); |
| 625 |
NOX::DeviceSpace().fence(); |
| 626 |
} |
| 627 |
|
| 628 |
if (fill_DfDp4) { |
| 629 |
auto DfDp4_tpetra = tpetra_extract::getTpetraMultiVector(DfDp4_out); |
| 630 |
DfDp4_tpetra->putScalar(0.0); |
| 631 |
|
| 632 |
// Dirichlet BC on left is the equation: |
| 633 |
// f(0) = T_left - p(4) |
| 634 |
if (comm_->getRank() == 0) { |
| 635 |
auto DfDp4_host = DfDp4_tpetra->getLocalViewHost(Tpetra::Access::ReadWrite); |
| 636 |
DfDp4_host(0,0) = -1.0; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
if (fill_Dg4Dp2) { |
| 641 |
auto Dg4Dp2_tpetra = tpetra_extract::getTpetraMultiVector(Dg4Dp2_out); |
| 642 |
Dg4Dp2_tpetra->putScalar(0.0); |
| 643 |
} |
| 644 |
|
| 645 |
if (fill_Dg4Dp4) { |
| 646 |
auto Dg4Dp4_tpetra = tpetra_extract::getTpetraMultiVector(Dg4Dp4_out); |
| 647 |
Dg4Dp4_tpetra->putScalar(0.0); |
| 648 |
} |
| 649 |
|
| 650 |
if (fill_Dg6Dp2) { |
| 651 |
auto Dg6Dp2_tpetra = tpetra_extract::getTpetraMultiVector(Dg6Dp2_out); |
| 652 |
Dg6Dp2_tpetra->putScalar(0.0); |
| 653 |
} |
| 654 |
|
| 655 |
if (fill_Dg6Dp4) { |
| 656 |
auto Dg6Dp4_tpetra = tpetra_extract::getTpetraMultiVector(Dg6Dp4_out); |
| 657 |
Dg6Dp4_tpetra->putScalar(0.0); |
| 658 |
} |
| 659 |
|
| 660 |
} |
| 661 |
|
| 662 |
template<class Scalar, class LO, class GO, class Node> |
| 663 |
std::pair<int,int> |
| 664 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_p_index(const std::string& p_name) const |
| 665 |
{ |
| 666 |
const auto search = p_name_to_index_.find(p_name); |
| 667 |
TEUCHOS_ASSERT(search != p_name_to_index_.end()); |
| 668 |
return search->second; |
| 669 |
} |
| 670 |
|
| 671 |
template<class Scalar, class LO, class GO, class Node> |
| 672 |
std::pair<int,int> |
| 673 |
EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>::get_g_index(const std::string& g_name) const |
| 674 |
{ |
| 675 |
const auto search = g_name_to_index_.find(g_name); |
| 676 |
TEUCHOS_ASSERT(search != g_name_to_index_.end()); |
| 677 |
return search->second; |
| 678 |
} |
| 679 |
|
| 680 |
template<class Scalar, class LO, class GO, class Node> |
| 681 |
void EvaluatorTpetra1DFEM<Scalar, LO, GO, Node>:: |
| 682 |
reportFinalPoint (const ::Thyra::ModelEvaluatorBase::InArgs<Scalar> &finalPoint, const bool wasSolved) |
| 683 |
{} |
| 684 |
|
| 685 |
#endif |