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_DefaultSpmdVectorSpace.hpp"
54 #include "Thyra_SpmdVectorBase.hpp"
55 
56 #include "PanzerDiscFE_config.hpp"
57 #ifdef PANZER_HAVE_EPETRA_STACK
58 #include "Epetra_LocalMap.h"
59 #include "Epetra_Map.h"
60 #include "Thyra_EpetraThyraWrappers.hpp"
61 #endif
62 
63 namespace panzer {
64 
65 template <typename EvalT>
67 public:
68  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
69  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false),
70 #ifdef PANZER_HAVE_EPETRA_STACK
71  eComm_(comm),
72 #endif
73  useThyra_(false)
74  {
75  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
76  }
77 
79 
81  virtual std::size_t localSizeRequired() const = 0;
82 
84  virtual bool vectorIsDistributed() const = 0;
85 
86 #ifdef PANZER_HAVE_EPETRA_STACK
87  // This is the epetra view of the world
89 
91  Teuchos::RCP<const Epetra_Map> getMap() const;
92 
96  void setVector(const Teuchos::RCP<Epetra_Vector> & destVec);
97 #endif
98 
99  // This is the Thyra view of the world
101 
104 
108  void setVector(const Teuchos::RCP<Thyra::VectorBase<double> > & destVec);
109 
112  { vSpace_ = vs; }
113 
116  { return tVector_; }
117 
118 protected:
121 
123  bool useEpetra() const { return useEpetra_; }
124 
126  bool useThyra() const { return useThyra_; }
127 
128 #ifdef PANZER_HAVE_EPETRA_STACK
129  Epetra_Vector & getEpetraVector() const;
131 #endif
132 
134  Thyra::ArrayRCP<double> getThyraVector() const;
135 
138  { return tVector_;}
139 
140 
141 private:
142  // hide these methods
145 
147 #ifdef PANZER_HAVE_EPETRA_STACK
148  Epetra_MpiComm eComm_;
149  mutable Teuchos::RCP<const Epetra_Map> map_;
151 #endif
152 
153  bool useThyra_;
157 };
158 
159 template < >
160 class ResponseMESupport_Default<panzer::Traits::Jacobian> : public ResponseMESupportBase<panzer::Traits::Jacobian> {
161 public:
162 
163  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
164  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
165  : ResponseMESupportBase<panzer::Traits::Jacobian>(responseName), derivVecSpace_(derivVecSpace)
166  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
167 
169 
171  virtual std::size_t localSizeRequired() const = 0;
172 
174  virtual bool vectorIsDistributed() const = 0;
175 
177  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
178 
183  { return derivative_; }
184 
185 #ifdef PANZER_HAVE_EPETRA_STACK
186  // This is the epetra view of the world
188 
190  virtual Teuchos::RCP<Epetra_MultiVector> buildEpetraDerivative() const
191  {
194  TEUCHOS_ASSERT(supportsDerivative());
195 
196  if(eMap_==Teuchos::null)
197  eMap_ = Thyra::get_Epetra_Map(*getDerivativeVectorSpace(),Thyra::get_Epetra_Comm(*tComm_));
198 
199  return Teuchos::rcp(new Epetra_Vector(*eMap_));
200  }
201 
205  virtual void setDerivative(const Teuchos::RCP<Epetra_MultiVector> & derivative)
206  {
209  TEUCHOS_ASSERT(supportsDerivative());
210  TEUCHOS_ASSERT(eMap_!=Teuchos::null);
211 
212  derivative_ = Thyra::create_MultiVector(derivative,getDerivativeVectorSpace());
213  }
214 #endif
215 
216  // This is the Thyra view of the world
218 
221  {
224  TEUCHOS_ASSERT(supportsDerivative());
225  return Thyra::createMember(*getDerivativeVectorSpace());
226  }
227 
231  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
232  {
235  TEUCHOS_ASSERT(supportsDerivative());
236  derivative_ = derivative;
237  }
238 
239 protected:
242 
245  { return derivVecSpace_; }
246 
249  { derivVecSpace_ = vs; }
250 
251 private:
252  // hide these methods
255 
258 #ifdef PANZER_HAVE_EPETRA_STACK
259  mutable Teuchos::RCP<const Epetra_Map> eMap_;
260 #endif
261 
263 };
264 
265 template < >
266 class ResponseMESupport_Default<panzer::Traits::Tangent> : public ResponseMESupportBase<panzer::Traits::Tangent> {
267 public:
269 
270  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
271  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false),
272 #ifdef PANZER_HAVE_EPETRA_STACK
273  eComm_(comm),
274 #endif
275  useThyra_(false)
276  {
277  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
278  }
279 
281 
283  virtual std::size_t localSizeRequired() const = 0;
284 
286  virtual bool vectorIsDistributed() const = 0;
287 
288 #ifdef PANZER_HAVE_EPETRA_STACK
289  // This is the epetra view of the world
291 
293  Teuchos::RCP<const Epetra_Map> getMap() const {
294  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
295  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
296  "Thyra object, now trying to initalize as a Epetra! Error!");
297  // lazily construct the map only as needed
298  if(map_==Teuchos::null) {
299  if(this->vectorIsDistributed())
300  map_ = Teuchos::rcp(new Epetra_Map(-1,(int) this->localSizeRequired(),0,eComm_));
301  else
302  map_ = Teuchos::rcp(new Epetra_LocalMap((int) this->localSizeRequired(),0,eComm_));
303  }
304  return map_;
305  }
306 
310  void setVector(const Teuchos::RCP<Epetra_MultiVector> & destVec) {
311  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
312  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
313  "Thyra object, now trying to initalize as a Epetra! Error!");
314  eVector_ = destVec;
315  useEpetra_ = true;
316  }
317 #endif
318 
319  // This is the Thyra view of the world
321 
324  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
325  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
326  "Epetra object, now trying to initalize as a Thyra object! Error!");
327  // lazily build the space and return it
328  if(vSpace_==Teuchos::null) {
329  if(this->vectorIsDistributed())
330  vSpace_ = Thyra::defaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired(),-1);
331  else
332  vSpace_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired());
333  }
334  return vSpace_;
335  }
336 
341  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
342  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
343  "Epetra object, now trying to initalize as a Thyra object! Error!");
344  tVector_ = destVec;
345  useThyra_ = true;
346  }
347 
348 protected:
351 
353  bool useEpetra() const { return useEpetra_; }
354 
356  bool useThyra() const { return useThyra_; }
357 
358 #ifdef PANZER_HAVE_EPETRA_STACK
359  Epetra_MultiVector & getEpetraMultiVector() const {
362  return *eVector_;
363  }
364 #endif
365 
367  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > getThyraMultiVector() const {
369  const int num_col = tVector_->domain()->dim();
370  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > data(num_col);
371  for (int i=0; i<num_col; ++i)
372  Teuchos::rcp_dynamic_cast<Thyra::SpmdVectorBase<double> >(tVector_->col(i),true)->getNonconstLocalData(Teuchos::outArg(data[i]));
373  return data;
374  }
375 
377  int numDeriv() const {
378 #ifdef PANZER_HAVE_EPETRA_STACK
379  if (useEpetra())
380  return eVector_->NumVectors();
381  else
382 #endif
383  return tVector_->domain()->dim();
384  }
385 
386 
387 private:
388  // hide these methods
391 
393 #ifdef PANZER_HAVE_EPETRA_STACK
394  Epetra_MpiComm eComm_;
395  mutable Teuchos::RCP<const Epetra_Map> map_;
397 #endif
398 
399  bool useThyra_;
403 };
404 
405 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
406 
407 template < >
408 class ResponseMESupport_Default<panzer::Traits::Hessian> : public ResponseMESupportBase<panzer::Traits::Hessian> {
409 public:
410 
411  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
412  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
413  : ResponseMESupportBase<panzer::Traits::Hessian>(responseName), derivVecSpace_(derivVecSpace)
414  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
415 
417 
419  virtual std::size_t localSizeRequired() const = 0;
420 
422  virtual bool vectorIsDistributed() const = 0;
423 
425  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
426 
431  { return derivative_; }
432 
435  {
438  TEUCHOS_ASSERT(supportsDerivative());
439  return Thyra::createMember(*getDerivativeVectorSpace());
440  }
441 
445  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
446  {
449  TEUCHOS_ASSERT(supportsDerivative());
450  derivative_ = derivative;
451  }
452 
453 protected:
456 
459  { return derivVecSpace_; }
460 
463  { derivVecSpace_ = vs; }
464 
465 private:
466  // hide these methods
469 
472 
474 };
475 
476 #endif
477 
478 }
479 
481 
482 #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_
bool supportsDerivative() const
Does this response support derivative evaluation?
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.
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_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.
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.
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 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_
#define TEUCHOS_ASSERT(assertion_test)
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::VectorBase< double > > &destVec)
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