// @HEADER // ***************************************************************************** // Panzer: A partial differential equation assembly // engine for strongly coupled complex multiphysics systems // // Copyright 2011 NTESS and the Panzer contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef __Panzer_ResponseMESupport_Default_hpp__ #define __Panzer_ResponseMESupport_Default_hpp__ #include "Teuchos_DefaultMpiComm.hpp" #include "Teuchos_ArrayRCP.hpp" #include "Teuchos_RCP.hpp" #include "Teuchos_OpaqueWrapper.hpp" #include "Panzer_ResponseMESupportBase.hpp" #include "Thyra_DefaultSpmdVectorSpace.hpp" #include "Thyra_SpmdVectorBase.hpp" #include "PanzerDiscFE_config.hpp" #ifdef PANZER_HAVE_EPETRA_STACK #include "Epetra_LocalMap.h" #include "Epetra_Map.h" #include "Thyra_EpetraThyraWrappers.hpp" #endif namespace panzer { template class ResponseMESupport_Default : public ResponseMESupportBase { public: ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm) : ResponseMESupportBase(responseName), useEpetra_(false), #ifdef PANZER_HAVE_EPETRA_STACK eComm_(comm), #endif useThyra_(false) { tComm_ = Teuchos::rcp(new Teuchos::MpiComm(Teuchos::opaqueWrapper(comm))); } virtual ~ResponseMESupport_Default() {} //! What is the number of values you need locally virtual std::size_t localSizeRequired() const = 0; //! Is the vector distributed (or replicated) virtual bool vectorIsDistributed() const = 0; #ifdef PANZER_HAVE_EPETRA_STACK // This is the epetra view of the world /////////////////////////////////////////////////////////// //! Get the Epetra_Map for this response, map is constructed lazily. Teuchos::RCP getMap() const; /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getMap. */ void setVector(const Teuchos::RCP & destVec); #endif // This is the Thyra view of the world /////////////////////////////////////////////////////////// //! Get the vector space for this response, vector space is constructed lazily. Teuchos::RCP > getVectorSpace() const; /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getVectorSpace. */ void setVector(const Teuchos::RCP > & destVec); //! set the vector space for this response void setVectorSpace(Teuchos::RCP > vs) { vSpace_ = vs; } //! Access the response vector Teuchos::RCP > getVector() const { return tVector_; } protected: //! Get the teuchos comm object Teuchos::RCP > getComm() const { return tComm_; } //! Is Epetra the right vector bool useEpetra() const { return useEpetra_; } //! Is Thyra the right vector bool useThyra() const { return useThyra_; } #ifdef PANZER_HAVE_EPETRA_STACK //! Access the epetra vector Epetra_Vector & getEpetraVector() const; #endif //! Access the thyra vector Thyra::ArrayRCP getThyraVector() const; //! Access the thyra MultiVector Teuchos::RCP > getThyraMultiVector() const { return tVector_;} private: // hide these methods ResponseMESupport_Default(); ResponseMESupport_Default(const ResponseMESupport_Default &); bool useEpetra_; #ifdef PANZER_HAVE_EPETRA_STACK Epetra_MpiComm eComm_; mutable Teuchos::RCP map_; Teuchos::RCP eVector_; #endif bool useThyra_; mutable Teuchos::RCP > vSpace_; Teuchos::RCP > tVector_; Teuchos::RCP > tComm_; }; template < > class ResponseMESupport_Default : public ResponseMESupportBase { public: ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm, const Teuchos::RCP > & derivVecSpace=Teuchos::null) : ResponseMESupportBase(responseName), derivVecSpace_(derivVecSpace) { tComm_ = Teuchos::rcp(new Teuchos::MpiComm(Teuchos::opaqueWrapper(comm))); } virtual ~ResponseMESupport_Default() {} //! What is the number of values you need locally virtual std::size_t localSizeRequired() const = 0; //! Is the vector distributed (or replicated). For derivative assembly this must be false! virtual bool vectorIsDistributed() const = 0; //! Does this response support derivative evaluation? bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; } /** Get the vector for this response. This must be * constructed from the vector space returned by getMap. */ Teuchos::RCP > getDerivative() const { return derivative_; } #ifdef PANZER_HAVE_EPETRA_STACK // This is the epetra view of the world /////////////////////////////////////////////////////////// //! Get the Epetra_Map for this response, map is constructed lazily. virtual Teuchos::RCP buildEpetraDerivative() const { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); if(eMap_==Teuchos::null) eMap_ = Thyra::get_Epetra_Map(*getDerivativeVectorSpace(),Thyra::get_Epetra_Comm(*tComm_)); return Teuchos::rcp(new Epetra_Vector(*eMap_)); } /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getMap. */ virtual void setDerivative(const Teuchos::RCP & derivative) { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); TEUCHOS_ASSERT(eMap_!=Teuchos::null); derivative_ = Thyra::create_MultiVector(derivative,getDerivativeVectorSpace()); } #endif // This is the Thyra view of the world /////////////////////////////////////////////////////////// //! Get the Epetra_Map for this response, map is constructed lazily. virtual Teuchos::RCP > buildDerivative() const { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); return Thyra::createMember(*getDerivativeVectorSpace()); } /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getMap. */ virtual void setDerivative(const Teuchos::RCP > & derivative) { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); derivative_ = derivative; } protected: //! Get the teuchos comm object Teuchos::RCP > getComm() const { return tComm_; } //! Get the derivative vector space Teuchos::RCP > getDerivativeVectorSpace() const { return derivVecSpace_; } //! Set the derivative vector space void setDerivativeVectorSpace(const Teuchos::RCP > & vs) { derivVecSpace_ = vs; } private: // hide these methods ResponseMESupport_Default(); ResponseMESupport_Default(const ResponseMESupport_Default &); Teuchos::RCP > tComm_; Teuchos::RCP > derivVecSpace_; #ifdef PANZER_HAVE_EPETRA_STACK mutable Teuchos::RCP eMap_; #endif Teuchos::RCP > derivative_; }; template < > class ResponseMESupport_Default : public ResponseMESupportBase { public: typedef panzer::Traits::Tangent EvalT; ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm) : ResponseMESupportBase(responseName), useEpetra_(false), #ifdef PANZER_HAVE_EPETRA_STACK eComm_(comm), #endif useThyra_(false) { tComm_ = Teuchos::rcp(new Teuchos::MpiComm(Teuchos::opaqueWrapper(comm))); } virtual ~ResponseMESupport_Default() {} //! What is the number of values you need locally virtual std::size_t localSizeRequired() const = 0; //! Is the vector distributed (or replicated) virtual bool vectorIsDistributed() const = 0; #ifdef PANZER_HAVE_EPETRA_STACK // This is the epetra view of the world /////////////////////////////////////////////////////////// //! Get the Epetra_Map for this response, map is constructed lazily. Teuchos::RCP getMap() const { TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error, "Reponse field \"" << this->getName() << "\" has previously been initialized as a " "Thyra object, now trying to initalize as a Epetra! Error!"); // lazily construct the map only as needed if(map_==Teuchos::null) { if(this->vectorIsDistributed()) map_ = Teuchos::rcp(new Epetra_Map(-1,(int) this->localSizeRequired(),0,eComm_)); else map_ = Teuchos::rcp(new Epetra_LocalMap((int) this->localSizeRequired(),0,eComm_)); } return map_; } /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getMap. */ void setVector(const Teuchos::RCP & destVec) { TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error, "Reponse field \"" << this->getName() << "\" has previously been initialized as a " "Thyra object, now trying to initalize as a Epetra! Error!"); eVector_ = destVec; useEpetra_ = true; } #endif // This is the Thyra view of the world /////////////////////////////////////////////////////////// //! Get the vector space for this response, vector space is constructed lazily. Teuchos::RCP > getVectorSpace() const { TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error, "Reponse field \"" << this->getName() << "\" has previously been initialized as an " "Epetra object, now trying to initalize as a Thyra object! Error!"); // lazily build the space and return it if(vSpace_==Teuchos::null) { if(this->vectorIsDistributed()) vSpace_ = Thyra::defaultSpmdVectorSpace(tComm_,this->localSizeRequired(),-1); else vSpace_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace(tComm_,this->localSizeRequired()); } return vSpace_; } /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getVectorSpace. */ void setVector(const Teuchos::RCP > & destVec) { TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error, "Reponse field \"" << this->getName() << "\" has previously been initialized as an " "Epetra object, now trying to initalize as a Thyra object! Error!"); tVector_ = destVec; useThyra_ = true; } protected: //! Get the teuchos comm object Teuchos::RCP > getComm() const { return tComm_; } //! Is Epetra the right vector bool useEpetra() const { return useEpetra_; } //! Is Thyra the right vector bool useThyra() const { return useThyra_; } #ifdef PANZER_HAVE_EPETRA_STACK //! Access the epetra vector Epetra_MultiVector & getEpetraMultiVector() const { TEUCHOS_ASSERT(useEpetra()); return *eVector_; } #endif //! Access the thyra vector Thyra::ArrayRCP< Thyra::ArrayRCP > getThyraMultiVector() const { TEUCHOS_ASSERT(useThyra()); const int num_col = tVector_->domain()->dim(); Thyra::ArrayRCP< Thyra::ArrayRCP > data(num_col); for (int i=0; i >(tVector_->col(i),true)->getNonconstLocalData(Teuchos::outArg(data[i])); return data; } //! Return the number of columns in the multivector int numDeriv() const { #ifdef PANZER_HAVE_EPETRA_STACK if (useEpetra()) return eVector_->NumVectors(); else #endif return tVector_->domain()->dim(); } private: // hide these methods ResponseMESupport_Default(); ResponseMESupport_Default(const ResponseMESupport_Default &); bool useEpetra_; #ifdef PANZER_HAVE_EPETRA_STACK Epetra_MpiComm eComm_; mutable Teuchos::RCP map_; Teuchos::RCP eVector_; #endif bool useThyra_; mutable Teuchos::RCP > vSpace_; Teuchos::RCP > tVector_; Teuchos::RCP > tComm_; }; #ifdef Panzer_BUILD_HESSIAN_SUPPORT template < > class ResponseMESupport_Default : public ResponseMESupportBase { public: ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm, const Teuchos::RCP > & derivVecSpace=Teuchos::null) : ResponseMESupportBase(responseName), derivVecSpace_(derivVecSpace) { tComm_ = Teuchos::rcp(new Teuchos::MpiComm(Teuchos::opaqueWrapper(comm))); } virtual ~ResponseMESupport_Default() {} //! What is the number of values you need locally virtual std::size_t localSizeRequired() const = 0; //! Is the vector distributed (or replicated). For derivative assembly this must be false! virtual bool vectorIsDistributed() const = 0; //! Does this response support derivative evaluation? bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; } /** Get the vector for this response. This must be * constructed from the vector space returned by getMap. */ Teuchos::RCP > getDerivative() const { return derivative_; } //! Get the Epetra_Map for this response, map is constructed lazily. virtual Teuchos::RCP > buildDerivative() const { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); return Thyra::createMember(*getDerivativeVectorSpace()); } /** Set the vector (to be filled) for this response. This must be * constructed from the vector space returned by getMap. */ virtual void setDerivative(const Teuchos::RCP > & derivative) { TEUCHOS_ASSERT(!vectorIsDistributed()); TEUCHOS_ASSERT(localSizeRequired()==1); TEUCHOS_ASSERT(supportsDerivative()); derivative_ = derivative; } protected: //! Get the teuchos comm object Teuchos::RCP > getComm() const { return tComm_; } //! Get the derivative vector space Teuchos::RCP > getDerivativeVectorSpace() const { return derivVecSpace_; } //! Set the derivative vector space void setDerivativeVectorSpace(const Teuchos::RCP > & vs) { derivVecSpace_ = vs; } private: // hide these methods ResponseMESupport_Default(); ResponseMESupport_Default(const ResponseMESupport_Default &); Teuchos::RCP > tComm_; Teuchos::RCP > derivVecSpace_; Teuchos::RCP > derivative_; }; #endif } #include "Panzer_ResponseMESupport_Default_impl.hpp" #endif