Panzer  Version of the Day
Panzer_ResponseMESupport_Default.hpp
Go to the documentation of this file.
1 #ifndef __Panzer_ResponseMESupport_Default_hpp__
2 #define __Panzer_ResponseMESupport_Default_hpp__
3 
5 #include "Teuchos_ArrayRCP.hpp"
6 #include "Teuchos_RCP.hpp"
7 #include "Teuchos_OpaqueWrapper.hpp"
8 
10 
11 #include "Thyra_EpetraThyraWrappers.hpp"
12 
13 #include "Thyra_DefaultSpmdVectorSpace.hpp"
14 #include "Thyra_SpmdVectorBase.hpp"
15 
16 #include "Epetra_LocalMap.h"
17 #include "Epetra_Map.h"
18 
19 namespace panzer {
20 
21 template <typename EvalT>
23 public:
24  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
25  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false), eComm_(comm), useThyra_(false)
26  {
27  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
28  }
29 
31 
33  virtual std::size_t localSizeRequired() const = 0;
34 
36  virtual bool vectorIsDistributed() const = 0;
37 
38  // This is the epetra view of the world
40 
43 
47  void setVector(const Teuchos::RCP<Epetra_Vector> & destVec);
48 
49  // This is the Thyra view of the world
51 
54 
58  void setVector(const Teuchos::RCP<Thyra::VectorBase<double> > & destVec);
59 
60 protected:
63 
65  bool useEpetra() const { return useEpetra_; }
66 
68  bool useThyra() const { return useThyra_; }
69 
72 
74  Thyra::ArrayRCP<double> getThyraVector() const;
75 
76 
77 private:
78  // hide these methods
81 
82  bool useEpetra_;
86 
87  bool useThyra_;
91 };
92 
93 template < >
94 class ResponseMESupport_Default<panzer::Traits::Jacobian> : public ResponseMESupportBase<panzer::Traits::Jacobian> {
95 public:
96 
97  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
98  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
99  : ResponseMESupportBase<panzer::Traits::Jacobian>(responseName), derivVecSpace_(derivVecSpace)
100  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
101 
103 
105  virtual std::size_t localSizeRequired() const = 0;
106 
108  virtual bool vectorIsDistributed() const = 0;
109 
111  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
112 
117  { return derivative_; }
118 
119  // This is the epetra view of the world
121 
124  {
127  TEUCHOS_ASSERT(supportsDerivative());
128 
129  if(eMap_==Teuchos::null)
130  eMap_ = Thyra::get_Epetra_Map(*getDerivativeVectorSpace(),Thyra::get_Epetra_Comm(*tComm_));
131 
132  return Teuchos::rcp(new Epetra_Vector(*eMap_));
133  }
134 
138  virtual void setDerivative(const Teuchos::RCP<Epetra_MultiVector> & derivative)
139  {
142  TEUCHOS_ASSERT(supportsDerivative());
143  TEUCHOS_ASSERT(eMap_!=Teuchos::null);
144 
145  derivative_ = Thyra::create_MultiVector(derivative,getDerivativeVectorSpace());
146  }
147 
148  // This is the Thyra view of the world
150 
153  {
156  TEUCHOS_ASSERT(supportsDerivative());
157  return Thyra::createMember(*getDerivativeVectorSpace());
158  }
159 
163  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
164  {
167  TEUCHOS_ASSERT(supportsDerivative());
168  derivative_ = derivative;
169  }
170 
171 protected:
174 
177  { return derivVecSpace_; }
178 
181  { derivVecSpace_ = vs; }
182 
183 private:
184  // hide these methods
187 
191 
193 };
194 
195 template < >
196 class ResponseMESupport_Default<panzer::Traits::Tangent> : public ResponseMESupportBase<panzer::Traits::Tangent> {
197 public:
199 
200  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm)
201  : ResponseMESupportBase<EvalT>(responseName), useEpetra_(false), eComm_(comm), useThyra_(false)
202  {
203  tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm)));
204  }
205 
207 
209  virtual std::size_t localSizeRequired() const = 0;
210 
212  virtual bool vectorIsDistributed() const = 0;
213 
214  // This is the epetra view of the world
216 
219  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
220  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
221  "Thyra object, now trying to initalize as a Epetra! Error!");
222  // lazily construct the map only as needed
223  if(map_==Teuchos::null) {
224  if(this->vectorIsDistributed())
225  map_ = Teuchos::rcp(new Epetra_Map(-1,(int) this->localSizeRequired(),0,eComm_));
226  else
227  map_ = Teuchos::rcp(new Epetra_LocalMap((int) this->localSizeRequired(),0,eComm_));
228  }
229  return map_;
230  }
231 
236  TEUCHOS_TEST_FOR_EXCEPTION(useThyra_,std::logic_error,
237  "Reponse field \"" << this->getName() << "\" has previously been initialized as a "
238  "Thyra object, now trying to initalize as a Epetra! Error!");
239  eVector_ = destVec;
240  useEpetra_ = true;
241  }
242 
243  // This is the Thyra view of the world
245 
248  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
249  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
250  "Epetra object, now trying to initalize as a Thyra object! Error!");
251  // lazily build the space and return it
252  if(vSpace_==Teuchos::null) {
253  if(this->vectorIsDistributed())
254  vSpace_ = Thyra::defaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired(),-1);
255  else
256  vSpace_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<double>(tComm_,this->localSizeRequired());
257  }
258  return vSpace_;
259  }
260 
265  TEUCHOS_TEST_FOR_EXCEPTION(useEpetra_,std::logic_error,
266  "Reponse field \"" << this->getName() << "\" has previously been initialized as an "
267  "Epetra object, now trying to initalize as a Thyra object! Error!");
268  tVector_ = destVec;
269  useThyra_ = true;
270  }
271 
272 protected:
275 
277  bool useEpetra() const { return useEpetra_; }
278 
280  bool useThyra() const { return useThyra_; }
281 
285  return *eVector_;
286  }
287 
289  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > getThyraMultiVector() const {
291  const int num_col = tVector_->domain()->dim();
292  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > data(num_col);
293  for (int i=0; i<num_col; ++i)
294  Teuchos::rcp_dynamic_cast<Thyra::SpmdVectorBase<double> >(tVector_->col(i),true)->getNonconstLocalData(Teuchos::outArg(data[i]));
295  return data;
296  }
297 
299  int numDeriv() const {
300  if (useEpetra())
301  return eVector_->NumVectors();
302  else
303  return tVector_->domain()->dim();
304  }
305 
306 
307 private:
308  // hide these methods
311 
316 
317  bool useThyra_;
321 };
322 
323 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
324 
325 template < >
326 class ResponseMESupport_Default<panzer::Traits::Hessian> : public ResponseMESupportBase<panzer::Traits::Hessian> {
327 public:
328 
329  ResponseMESupport_Default(const std::string & responseName,MPI_Comm comm,
330  const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & derivVecSpace=Teuchos::null)
331  : ResponseMESupportBase<panzer::Traits::Hessian>(responseName), derivVecSpace_(derivVecSpace)
332  { tComm_ = Teuchos::rcp(new Teuchos::MpiComm<Thyra::Ordinal>(Teuchos::opaqueWrapper(comm))); }
333 
334  virtual ~ResponseMESupport_Default() {}
335 
337  virtual std::size_t localSizeRequired() const = 0;
338 
340  virtual bool vectorIsDistributed() const = 0;
341 
343  bool supportsDerivative() const { return getDerivativeVectorSpace()!=Teuchos::null; }
344 
348  Teuchos::RCP<Thyra::MultiVectorBase<double> > getDerivative() const
349  { return derivative_; }
350 
352  virtual Teuchos::RCP<Thyra::MultiVectorBase<double> > buildDerivative() const
353  {
356  TEUCHOS_ASSERT(supportsDerivative());
357  return Thyra::createMember(*getDerivativeVectorSpace());
358  }
359 
363  virtual void setDerivative(const Teuchos::RCP<Thyra::MultiVectorBase<double> > & derivative)
364  {
367  TEUCHOS_ASSERT(supportsDerivative());
368  derivative_ = derivative;
369  }
370 
371 protected:
374 
376  Teuchos::RCP<const Thyra::VectorSpaceBase<double> > getDerivativeVectorSpace() const
377  { return derivVecSpace_; }
378 
380  void setDerivativeVectorSpace(const Teuchos::RCP<const Thyra::VectorSpaceBase<double> > & vs)
381  { derivVecSpace_ = vs; }
382 
383 private:
384  // hide these methods
386  ResponseMESupport_Default(const ResponseMESupport_Default<panzer::Traits::Hessian> &);
387 
390 
392 };
393 
394 #endif
395 
396 }
397 
399 
400 #endif
Teuchos::RCP< Thyra::MultiVectorBase< double > > getDerivative() const
virtual void setDerivative(const Teuchos::RCP< Thyra::MultiVectorBase< double > > &derivative)
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 useThyra() const
Is Thyra the right vector.
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 Epetra_Map > getMap() const
Get the Epetra_Map for this response, map is constructed lazily.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > tComm_
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > derivVecSpace_
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getDerivativeVectorSpace() const
Get the derivative vector space.
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getVectorSpace() const
Get the vector space for this response, vector space is constructed lazily.
bool useEpetra() const
Is Epetra the right vector.
virtual Teuchos::RCP< Thyra::MultiVectorBase< double > > buildDerivative() const
Get the Epetra_Map for this response, map is constructed lazily.
bool supportsDerivative() const
Does this response support derivative evaluation?
void setVector(const Teuchos::RCP< Thyra::MultiVectorBase< double > > &destVec)
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > getComm() const
Get the teuchos comm object.
int NumVectors() const
int numDeriv() const
Return the number of columns in the multivector.
Teuchos::RCP< const Teuchos::Comm< Thyra::Ordinal > > tComm_
Teuchos::RCP< const Thyra::VectorSpaceBase< double > > getVectorSpace() const
Get the vector space for this response, vector space is constructed lazily.
virtual std::size_t localSizeRequired() const =0
What is the number of values you need locally.
Thyra::ArrayRCP< double > getThyraVector() const
Access the thyra vector.
void setVector(const Teuchos::RCP< Epetra_Vector > &destVec)
Teuchos::RCP< Thyra::VectorBase< double > > tVector_
std::string getName() const
Epetra_Vector & getEpetraVector() const
Access the epetra vector.
Teuchos::RCP< const Teuchos::Comm< int > > comm
Thyra::ArrayRCP< Thyra::ArrayRCP< double > > getThyraMultiVector() const
Access the thyra vector.
Epetra_MultiVector & getEpetraMultiVector() const
Access the epetra vector.
void setVector(const Teuchos::RCP< Epetra_MultiVector > &destVec)
Teuchos::RCP< const Epetra_Map > getMap() const
Get the Epetra_Map for this response, map is constructed lazily.
#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)
virtual Teuchos::RCP< Epetra_MultiVector > buildEpetraDerivative() const
Get the Epetra_Map for this response, map is constructed lazily.