// @HEADER // ***************************************************************************** // Thyra: Interfaces and Support for Abstract Numerical Algorithms // // Copyright 2004 NTESS and the Thyra contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef Thyra_TpetraExplicitAdjointModelEvaluator_hpp #define Thyra_TpetraExplicitAdjointModelEvaluator_hpp #include "Thyra_ModelEvaluatorDelegatorBase.hpp" #include "Thyra_TpetraLinearOp.hpp" #include "Tpetra_RowMatrixTransposer.hpp" namespace Thyra { /** \brief A model evaluator decorator for computing an explicit adjoint. */ /** * This ModelEvaluator only supports computing W_op and forms the adjoint by * computing W_op from the underlying ModelEvaluator and then explicitly * transposes it. * * Since it derives from ModelEvaluatorDelegatorBase, it forwards all * ModelEvaluator methods to the supplied underlying ModelEvaluator except * those overloaded here. */ template class TpetraExplicitAdjointModelEvaluator : public ModelEvaluatorDelegatorBase{ public: //! Constructor TpetraExplicitAdjointModelEvaluator( const RCP >& model) : ModelEvaluatorDelegatorBase(model) {} //! Constructor TpetraExplicitAdjointModelEvaluator( const RCP >& model) : ModelEvaluatorDelegatorBase(model) {} //! Destructor virtual ~TpetraExplicitAdjointModelEvaluator() = default; ModelEvaluatorBase::InArgs createInArgs() const { // This ME should use the same InArgs as the underlying model. However // we can't just use it's InArgs directly because the description won't // match (which is checked in debug builds). Instead create a new // InArgsSetup initialized by the underlying model's createInArgs() and // set the description appropriately. ModelEvaluatorBase::InArgsSetup inArgs = this->getUnderlyingModel()->createInArgs(); inArgs.setModelEvalDescription(this->description()); return inArgs; } RCP > create_W_op() const { typedef TpetraLinearOp TLO; typedef Tpetra::Operator TO; typedef Tpetra::CrsMatrix TCM; typedef Tpetra::RowMatrixTransposer TRMT; if (thyra_fwd_op == Teuchos::null) thyra_fwd_op = this->getUnderlyingModel()->create_W_op(); RCP thyra_tpetra_fwd_op = Teuchos::rcp_dynamic_cast(thyra_fwd_op,true); RCP tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator(); RCP tpetra_fwd_mat = Teuchos::rcp_dynamic_cast(tpetra_fwd_op,true); TRMT transposer(tpetra_fwd_mat); RCP tpetra_trans_mat = transposer.createTranspose(); return tpetraLinearOp(thyra_op->range(), thyra_op->domain(), tpetra_trans_mat); } private: mutable RCP< Thyra::LinearOpBase > thyra_fwd_op; ModelEvaluatorBase::OutArgs createOutArgsImpl() const { typedef ModelEvaluatorBase MEB; MEB::OutArgs model_outArgs = this->getUnderlyingModel()->createOutArgs(); MEB::OutArgsSetup outArgs; outArgs.setModelEvalDescription(this->description()); outArgs.setSupports(MEB::OUT_ARG_f); //All models must support f, apparently outArgs.setSupports(MEB::OUT_ARG_W_op); TEUCHOS_ASSERT(model_outArgs.supports(MEB::OUT_ARG_W_op)); return outArgs; } void evalModelImpl( const ModelEvaluatorBase::InArgs &inArgs, const ModelEvaluatorBase::OutArgs &outArgs) const { typedef ModelEvaluatorBase MEB; typedef TpetraLinearOp TLO; typedef Tpetra::Operator TO; typedef Tpetra::CrsMatrix TCM; typedef Tpetra::RowMatrixTransposer TRMT; MEB::OutArgs model_outArgs = this->getUnderlyingModel()->createOutArgs(); if (model_outArgs.supports(MEB::OUT_ARG_W_op) && outArgs.get_W_op() != Teuchos::null) { // Compute underlying W_op. if (thyra_fwd_op == Teuchos::null) thyra_fwd_op = this->getUnderlyingModel()->create_W_op(); model_outArgs->set_W_op(thyra_fwd_op); this->getUnderlyingModel()->evalModel(inArgs, model_outArgs); // Transpose W_op // Unfortunately, because of the horrendous design of the Tpetra // RowMatrixTransposer, this creates a new transposed matrix each time RCP thyra_tpetra_fwd_op = Teuchos::rcp_dynamic_cast(thyra_fwd_op,true); RCP tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator(); RCP tpetra_fwd_mat = Teuchos::rcp_dynamic_cast(tpetra_fwd_op,true); TRMT transposer(tpetra_fwd_mat); RCP tpetra_trans_mat = transposer.createTranspose(); // Copy transposed matrix into our outArg RCP thyra_adj_op = outArgs.get_W_op(); RCP thyra_tpetra_adj_op = Teuchos::rcp_dynamic_cast(thyra_adj_op,true); RCP tpetra_adj_op = thyra_tpetra_adj_op->getTpetraOperator(); RCP tpetra_adj_mat = Teuchos::rcp_dynamic_cast(tpetra_adj_op,true); *tpetra_adj_mat = *tpetra_trans_mat; } } }; template RCP > tpetraExplicitAdjointModelEvaluator( const RCP >& model) { return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator(model)); } template RCP > tpetraExplicitAdjointModelEvaluator( const RCP >& model) { return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator(model)); } } // namespace Thyra #endif