10 #ifndef TEMPUS_CDR_MODEL_TPETRA_IMPL_HPP
11 #define TEMPUS_CDR_MODEL_TPETRA_IMPL_HPP
16 #include "Kokkos_Core_fwd.hpp"
17 #include "Teuchos_Assert.hpp"
18 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
19 #include "Thyra_DefaultSpmdVectorSpace.hpp"
20 #include "Thyra_DetachedMultiVectorView.hpp"
21 #include "Thyra_DetachedVectorView.hpp"
22 #include "Thyra_MultiVectorStdOps.hpp"
23 #include "Thyra_PreconditionerBase.hpp"
24 #include "Thyra_VectorStdOps.hpp"
27 #include "Thyra_TpetraLinearOp.hpp"
28 #include "Thyra_TpetraThyraWrappers.hpp"
29 #include "Tpetra_CrsGraph_def.hpp"
30 #include "Tpetra_CrsMatrix_def.hpp"
31 #include "Tpetra_Import_def.hpp"
32 #include "Tpetra_Map_def.hpp"
33 #include "Tpetra_Vector_def.hpp"
34 #include "impl/Kokkos_HostThreadTeam.hpp"
37 namespace Tempus_Test {
41 template <
typename SC,
typename LO,
typename GO,
typename Node>
44 const GO numGlobalElements,
const SC zMin,
const SC zMax,
const SC a,
47 numGlobalElements_(numGlobalElements),
52 showGetInvalidArg_(false)
56 using ::Thyra::VectorBase;
63 const auto myRank =
comm_->getRank();
70 if (
comm_->getSize() == 1) {
74 LO overlapNumMyElements;
75 GO overlapGetMinGLobalIndex;
76 overlapNumMyElements =
xOwnedMap_->getLocalNumElements() + 2;
77 if ((myRank == 0) || (myRank == (
comm_->getSize() - 1))) {
78 overlapNumMyElements--;
82 overlapGetMinGLobalIndex =
xOwnedMap_->getMinGlobalIndex();
85 overlapGetMinGLobalIndex =
xOwnedMap_->getMinGlobalIndex() - 1;
90 GO getGlobalElement = overlapGetMinGLobalIndex;
91 for (
auto &globalElem : overlapMyGlobalNodes) {
92 globalElem = overlapGetMinGLobalIndex;
110 V_S(
x0_.
ptr(), ST::zero());
120 const auto minGI =
xOwnedMap_->getMinGlobalIndex();
123 Kokkos::parallel_for(
"coords_fill",
xOwnedMap_->getLocalNumElements(),
129 MEB::InArgsSetup<SC> inArgs;
130 inArgs.setModelEvalDescription(this->
description());
131 inArgs.setSupports(MEB::IN_ARG_t);
132 inArgs.setSupports(MEB::IN_ARG_x);
133 inArgs.setSupports(MEB::IN_ARG_beta);
134 inArgs.setSupports(MEB::IN_ARG_x_dot);
135 inArgs.setSupports(MEB::IN_ARG_alpha);
138 MEB::OutArgsSetup<SC> outArgs;
139 outArgs.setModelEvalDescription(this->
description());
140 outArgs.setSupports(MEB::OUT_ARG_f);
141 outArgs.setSupports(MEB::OUT_ARG_W);
142 outArgs.setSupports(MEB::OUT_ARG_W_op);
143 outArgs.setSupports(MEB::OUT_ARG_W_prec);
150 auto x_dot_init = Thyra::createMember(this->
get_x_space());
151 Thyra::put_scalar(SC(0.0), x_dot_init.ptr());
157 template <
typename SC,
typename LO,
typename GO,
typename Node>
162 W_graph->resumeFill();
164 auto overlapNumMyElements =
165 static_cast<LO
>(xGhostedMap_->getLocalNumElements());
168 for (LO elem = 0; elem < overlapNumMyElements - 1; elem++) {
170 for (LO i = 0; i < 2; i++) {
171 auto row = xGhostedMap_->getGlobalElement(elem + i);
174 for (LO j = 0; j < 2; j++) {
176 if (xOwnedMap_->isNodeGlobalElement(row)) {
177 auto colIndex = xGhostedMap_->getGlobalElement(elem + j);
179 W_graph->insertGlobalIndices(row, column);
184 W_graph->fillComplete();
189 template <
typename SC,
typename LO,
typename GO,
typename Node>
197 x0.sv().values()().assign(x0_in);
200 template <
typename SC,
typename LO,
typename GO,
typename Node>
202 bool showGetInvalidArg)
204 showGetInvalidArg_ = showGetInvalidArg;
207 template <
typename SC,
typename LO,
typename GO,
typename Node>
209 const Teuchos::RCP<const ::Thyra::LinearOpWithSolveFactoryBase<SC>>
212 wFactory_ = W_factory;
217 template <
typename SC,
typename LO,
typename GO,
typename Node>
224 template <
typename SC,
typename LO,
typename GO,
typename Node>
231 template <
typename SC,
typename LO,
typename GO,
typename Node>
235 return nominalValues_;
238 template <
typename SC,
typename LO,
typename GO,
typename Node>
242 auto W_factory = this->get_W_factory();
245 is_null(W_factory), std::runtime_error,
246 "W_factory in CDR_Model_Tpetra has a null W_factory!");
248 auto matrix = this->create_W_op();
250 return Thyra::linearOpWithSolve<SC>(*W_factory, matrix);
253 template <
typename SC,
typename LO,
typename GO,
typename Node>
259 return Thyra::tpetraLinearOp<SC, LO, GO, Node>(fSpace_, xSpace_, W_tpetra);
262 template <
typename SC,
typename LO,
typename GO,
typename Node>
266 auto W_op = create_W_op();
269 prec->initializeRight(W_op);
274 template <
typename SC,
typename LO,
typename GO,
typename Node>
281 template <
typename SC,
typename LO,
typename GO,
typename Node>
285 return prototypeInArgs_;
290 template <
typename SC,
typename LO,
typename GO,
typename Node>
294 return prototypeOutArgs_;
297 template <
typename SC,
typename LO,
typename GO,
typename Node>
304 using Teuchos::rcp_dynamic_cast;
309 auto f_out = outArgs.
get_f();
320 f = tpetra_extract::getTpetraVector(outArgs.
get_f());
325 auto W_epetra = tpetra_extract::getTpetraOperator(W_out);
332 auto M_tpetra = tpetra_extract::getTpetraOperator(
333 W_prec_out->getNonconstRightPrecOp());
337 jDiag_->putScalar(0.0);
345 if (comm_->getRank() == 0) {
347 auto xVec = tpetra_extract::getTpetraVector(x);
348 auto xView = xVec->getLocalViewHost(Tpetra::Access::ReadWrite);
356 uPtr_->doImport(*(tpetra_extract::getConstTpetraVector(inArgs.
get_x())),
357 *importer_, Tpetra::INSERT);
364 *(tpetra_extract::getConstTpetraVector(inArgs.
get_x_dot())), *importer_,
369 xPtr_->doImport(*nodeCoordinates_, *importer_, Tpetra::INSERT);
372 auto overlapNumMyElements =
373 static_cast<LO
>(xGhostedMap_->getLocalNumElements());
381 J->setAllToScalar(0.0);
385 M_inv->setAllToScalar(0.0);
390 comm_->getRank(), a_, k_);
391 Kokkos::parallel_for(
"DfDp2EvaluatorFunctor", overlapNumMyElements - 1,
397 const auto beta = inArgs.
get_beta();
401 *J, *xPtr_, *uPtr_, *uDotPtr_, comm_->getRank(), a_, k_, alpha, beta);
404 Kokkos::parallel_for(
"JacobianEvaluatorFunctor", overlapNumMyElements - 1,
412 *M_inv, *xPtr_, *uPtr_, *uDotPtr_, comm_->getRank(), a_, k_, alpha,
416 Kokkos::parallel_for(
"PreconditionerEvaluatorFunctor",
417 overlapNumMyElements - 1, mFunctor);
419 M_inv->fillComplete();
424 auto &diag = *jDiag_;
425 M_inv->getLocalDiagCopy(diag);
426 diag.reciprocal(diag);
428 M_inv->rightScale(diag);
429 M_inv->rightScale(diag);
436 #endif // TEMPUS_CDR_MODEL_TPETRA_IMPL_HPP
Teuchos::RCP< const tpetra_map > xGhostedMap_
Teuchos::RCP<::Thyra::VectorBase< SC > > x0_
void evalModelImpl(const ::Thyra::ModelEvaluatorBase::InArgs< SC > &inArgs, const ::Thyra::ModelEvaluatorBase::OutArgs< SC > &outArgs) const
::Thyra::ModelEvaluatorBase::InArgs< SC > createInArgs() const
bool is_null(const boost::shared_ptr< T > &p)
::Thyra::ModelEvaluatorBase::InArgs< SC > prototypeInArgs_
RCP< const VectorBase< Scalar > > get_x_dot() const
Teuchos::RCP< const ::Thyra::VectorSpaceBase< SC > > xSpace_
Teuchos::RCP< const Tpetra::Import< LO, GO, Node > > importer_
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP<::Thyra::PreconditionerBase< SC > > create_W_prec() const
Evaluation< VectorBase< Scalar > > get_f() const
Tpetra::CrsGraph< LO, GO, Node > tpetra_graph
void set_x0(const Teuchos::ArrayView< const SC > &x0)
Teuchos::RCP< const ::Thyra::VectorSpaceBase< SC > > get_f_space() const
::Thyra::ModelEvaluatorBase::OutArgs< SC > createOutArgsImpl() const
Teuchos::RCP< const ::Thyra::VectorSpaceBase< SC > > get_x_space() const
void set_x(const RCP< const VectorBase< Scalar > > &x)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const ::Thyra::VectorSpaceBase< SC > > fSpace_
void setShowGetInvalidArgs(bool showGetInvalidArg)
Teuchos::RCP< const tpetra_graph > wGraph_
Tpetra::Vector< SC, LO, GO, Node > tpetra_vec
Teuchos::RCP< Thyra::LinearOpWithSolveBase< double > > create_W() const
virtual std::string description() const
Teuchos::RCP< tpetra_vec > nodeCoordinates_
void set_x_dot(const RCP< const VectorBase< Scalar > > &x_dot)
Teuchos::RCP< const ::Thyra::LinearOpWithSolveFactoryBase< SC > > get_W_factory() const
void set_W_factory(const Teuchos::RCP< const ::Thyra::LinearOpWithSolveFactoryBase< SC >> &W_factory)
Teuchos::RCP< const tpetra_map > xOwnedMap_
bool nonnull(const boost::shared_ptr< T > &p)
Tpetra::Map< LO, GO, Node > tpetra_map
Teuchos::RCP< const tpetra_map > fOwnedMap_
const Teuchos::RCP< const Teuchos::Comm< int > > comm_
RCP< PreconditionerBase< Scalar > > get_W_prec() const
#define TEUCHOS_ASSERT(assertion_test)
::Thyra::ModelEvaluatorBase::InArgs< SC > nominalValues_
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
::Thyra::ModelEvaluatorBase::OutArgs< SC > prototypeOutArgs_
Tpetra::CrsMatrix< SC, LO, GO, Node > tpetra_matrix
RCP< LinearOpBase< Scalar > > get_W_op() const
const int numGlobalElements_
Teuchos::RCP<::Thyra::LinearOpBase< SC > > create_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
virtual Teuchos::RCP< const Tpetra::CrsGraph< LO, GO, Node > > createGraph()
CDR_Model_Tpetra(const Teuchos::RCP< const Teuchos::Comm< int >> &comm, const GO numGlobalElements, const SC zMin, const SC zMax, const SC a, const SC k)
::Thyra::ModelEvaluatorBase::InArgs< SC > getNominalValues() const