Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_ResponseMESupport_Default.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef __Panzer_ResponseMESupport_Default_hpp__
44 #define __Panzer_ResponseMESupport_Default_hpp__
45 
47 #include "Teuchos_ArrayRCP.hpp"
48 #include "Teuchos_RCP.hpp"
49 #include "Teuchos_OpaqueWrapper.hpp"
50 
52 
53 #include "Thyra_EpetraThyraWrappers.hpp"
54 
55 #include "Thyra_DefaultSpmdVectorSpace.hpp"
56 #include "Thyra_SpmdVectorBase.hpp"
57 
58 #include "Epetra_LocalMap.h"
59 #include "Epetra_Map.h"
60 
61 namespace panzer {
62 
63 template <typename EvalT>
65 public:
66  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
67  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false), eComm_(comm), useThyra_(false)
68  {
69  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
70  }
71 
73 
75  virtual std::size_t localSizeRequired() const = 0;
76 
78  virtual bool vectorIsDistributed() const = 0;
79 
80  // This is the epetra view of the world
82 
85 
89  void setVector(const Teuchos::RCP<Epetra_Vector> & destVec);
90 
91  // This is the Thyra view of the world
93 
96 
100  void setVector(const Teuchos::RCP<Thyra::VectorBase<double> > & destVec);
101 
104  { vSpace_ = vs; }
105 
108  { return tVector_; }
109 
110 protected:
113 
115  bool useEpetra() const { return useEpetra_; }
116 
118  bool useThyra() const { return useThyra_; }
119 
121  Epetra_Vector & getEpetraVector() const;
122 
124  Thyra::ArrayRCP<double> getThyraVector() const;
125 
128  { return tVector_;}
129 
130 
131 private:
132  // hide these methods
135 
140 
141  bool useThyra_;
145 };
146 
147 template < >
148 class ResponseMESupport_Default<panzer::Traits::Jacobian> : public ResponseMESupportBase<panzer::Traits::Jacobian> {
149 public:
150 
151  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
152  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
153  : ResponseMESupportBase<panzer::Traits::Jacobian>(responseName), derivVecSpace_(derivVecSpace)
154  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
155 
157 
159  virtual std::size_t localSizeRequired() const = 0;
160 
162  virtual bool vectorIsDistributed() const = 0;
163 
165  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
166 
171  { return derivative_; }
172 
173  // This is the epetra view of the world
175 
178  {
181  TEUCHOS_ASSERT(supportsDerivative());
182 
183  if(eMap_==Teuchos::null)
184  eMap_ = Thyra::get_Epetra_Map(*getDerivativeVectorSpace(),Thyra::get_Epetra_Comm(*tComm_));
185 
186  return Teuchos::rcp(new Epetra_Vector(*eMap_));
187  }
188 
192  virtual void setDerivative(const Teuchos::RCP<Epetra_MultiVector> & derivative)
193  {
196  TEUCHOS_ASSERT(supportsDerivative());
197  TEUCHOS_ASSERT(eMap_!=Teuchos::null);
198 
199  derivative_ = Thyra::create_MultiVector(derivative,getDerivativeVectorSpace());
200  }
201 
202  // This is the Thyra view of the world
204 
207  {
210  TEUCHOS_ASSERT(supportsDerivative());
211  return Thyra::createMember(*getDerivativeVectorSpace());
212  }
213 
217  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
218  {
221  TEUCHOS_ASSERT(supportsDerivative());
222  derivative_ = derivative;
223  }
224 
225 protected:
228 
231  { return derivVecSpace_; }
232 
235  { derivVecSpace_ = vs; }
236 
237 private:
238  // hide these methods
241 
245 
247 };
248 
249 template < >
250 class ResponseMESupport_Default<panzer::Traits::Tangent> : public ResponseMESupportBase<panzer::Traits::Tangent> {
251 public:
253 
254  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
255  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false), eComm_(comm), useThyra_(false)
256  {
257  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
258  }
259 
261 
263  virtual std::size_t localSizeRequired() const = 0;
264 
266  virtual bool vectorIsDistributed() const = 0;
267 
268  // This is the epetra view of the world
270 
273  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
274  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
275  "Thyra object, now trying to initalize as a Epetra! Error!");
276  // lazily construct the map only as needed
277  if(map_==Teuchos::null) {
278  if(this->vectorIsDistributed())
279  map_ = Teuchos::rcp(new Epetra_Map(-1,(int) this->localSizeRequired(),0,eComm_));
280  else
281  map_ = Teuchos::rcp(new Epetra_LocalMap((int) this->localSizeRequired(),0,eComm_));
282  }
283  return map_;
284  }
285 
290  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
291  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
292  "Thyra object, now trying to initalize as a Epetra! Error!");
293  eVector_ = destVec;
294  useEpetra_ = true;
295  }
296 
297  // This is the Thyra view of the world
299 
302  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
303  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
304  "Epetra object, now trying to initalize as a Thyra object! Error!");
305  // lazily build the space and return it
306  if(vSpace_==Teuchos::null) {
307  if(this->vectorIsDistributed())
308  vSpace_ = Thyra::defaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired(),-1);
309  else
310  vSpace_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired());
311  }
312  return vSpace_;
313  }
314 
319  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
320  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
321  "Epetra object, now trying to initalize as a Thyra object! Error!");
322  tVector_ = destVec;
323  useThyra_ = true;
324  }
325 
326 protected:
329 
331  bool useEpetra() const { return useEpetra_; }
332 
334  bool useThyra() const { return useThyra_; }
335 
339  return *eVector_;
340  }
341 
343  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > getThyraMultiVector() const {
345  const int num_col = tVector_->domain()->dim();
346  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > data(num_col);
347  for (int i=0; i<num_col; ++i)
348  Teuchos::rcp_dynamic_cast<Thyra::SpmdVectorBase<double> >(tVector_->col(i),true)->getNonconstLocalData(Teuchos::outArg(data[i]));
349  return data;
350  }
351 
353  int numDeriv() const {
354  if (useEpetra())
355  return eVector_->NumVectors();
356  else
357  return tVector_->domain()->dim();
358  }
359 
360 
361 private:
362  // hide these methods
365 
370 
371  bool useThyra_;
375 };
376 
377 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
378 
379 template < >
380 class ResponseMESupport_Default<panzer::Traits::Hessian> : public ResponseMESupportBase<panzer::Traits::Hessian> {
381 public:
382 
383  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
384  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
385  : ResponseMESupportBase<panzer::Traits::Hessian>(responseName), derivVecSpace_(derivVecSpace)
386  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
387 
389 
391  virtual std::size_t localSizeRequired() const = 0;
392 
394  virtual bool vectorIsDistributed() const = 0;
395 
397  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
398 
403  { return derivative_; }
404 
407  {
410  TEUCHOS_ASSERT(supportsDerivative());
411  return Thyra::createMember(*getDerivativeVectorSpace());
412  }
413 
417  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
418  {
421  TEUCHOS_ASSERT(supportsDerivative());
422  derivative_ = derivative;
423  }
424 
425 protected:
428 
431  { return derivVecSpace_; }
432 
435  { derivVecSpace_ = vs; }
436 
437 private:
438  // hide these methods
441 
444 
446 };
447 
448 #endif
449 
450 }
451 
453 
454 #endif
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getDerivativeVectorSpace() const
Get the derivative vector space.
bool supportsDerivative() const
Does this response support derivative evaluation?
bool useEpetra() const
Is Epetra the right vector.
virtual void setDerivative(const Teuchos::RCP< Thyra::MultiVectorBase< double > > &derivative)
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
virtual Teuchos::RCP< Thyra::MultiVectorBase< double > > buildDerivative() const
Get the Epetra_Map for this response, map is constructed lazily.
Teuchos::RCP< Thyra::VectorBase< double > > getVector() const
Access the response vector.
ResponseMESupport_Default(const std::string &responseName, MPI_Comm comm)
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > vSpace_
virtual void setDerivative(const Teuchos::RCP< Epetra_MultiVector > &derivative)
bool supportsDerivative() const
Does this response support derivative evaluation?
virtual Teuchos::RCP< Epetra_MultiVector > buildEpetraDerivative() const
Get the Epetra_Map for this response, map is constructed lazily.
void setDerivativeVectorSpace(const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &vs)
Set the derivative vector space.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > vSpace_
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > tComm_
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > derivVecSpace_
int numDeriv() const
Return the number of columns in the multivector.
virtual Teuchos::RCP< Thyra::MultiVectorBase< double > > buildDerivative() const
Get the Epetra_Map for this response, map is constructed lazily.
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getVectorSpace() const
Get the vector space for this response, vector space is constructed lazily.
Teuchos::RCP< const Epetra_Map > getMap() const
Get the Epetra_Map for this response, map is constructed lazily.
virtual void setDerivative(const Teuchos::RCP< Thyra::MultiVectorBase< double > > &derivative)
ResponseMESupport_Default(const std::string &responseName, MPI_Comm comm, const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &derivVecSpace=Teuchos::null)
void setVector(const Teuchos::RCP< Thyra::MultiVectorBase< double > > &destVec)
std::string getName() const
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getDerivativeVectorSpace() const
Get the derivative vector space.
Teuchos::RCP< const Epetra_Map > getMap() const
Get the Epetra_Map for this response, map is constructed lazily.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< Thyra::MultiVectorBase< double > > getThyraMultiVector() const
Access the thyra MultiVector.
Thyra::ArrayRCP< double > getThyraVector() const
Access the thyra vector.
Epetra_Vector & getEpetraVector() const
Access the epetra vector.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > tComm_
Teuchos::RCP< Thyra::MultiVectorBase< double > > getDerivative() const
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > derivVecSpace_
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getVectorSpace() const
Get the vector space for this response, vector space is constructed lazily.
bool useThyra() const
Is Thyra the right vector.
void setDerivativeVectorSpace(const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &vs)
Set the derivative vector space.
Epetra_MultiVector & getEpetraMultiVector() const
Access the epetra vector.
virtual std::size_t localSizeRequired() const =0
What is the number of values you need locally.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
void setVector(const Teuchos::RCP< Epetra_Vector > &destVec)
void setVectorSpace(Teuchos::RCP< const Thyra::VectorSpaceBase< double > > vs)
set the vector space for this response
Teuchos::RCP< Thyra::VectorBase< double > > tVector_
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > tComm_
void setVector(const Teuchos::RCP< Epetra_MultiVector > &destVec)
#define TEUCHOS_ASSERT(assertion_test)
ResponseMESupport_Default(const std::string &responseName, MPI_Comm comm, const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &derivVecSpace=Teuchos::null)
virtual bool vectorIsDistributed() const =0
Is the vector distributed (or replicated)
ResponseMESupport_Default(const std::string &responseName, MPI_Comm comm)
Thyra::ArrayRCP< Thyra::ArrayRCP< double > > getThyraMultiVector() const
Access the thyra vector.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
Teuchos::RCP< Thyra::MultiVectorBase< double > > getDerivative() const