Panzer  Version of the Day
Panzer_Response_Functional_impl.hpp
Go to the documentation of this file.
1 #ifndef __Panzer_Response_Functional_impl_hpp__
2 #define __Panzer_Response_Functional_impl_hpp__
3 
4 #include "Teuchos_Comm.hpp"
5 #include "Teuchos_CommHelpers.hpp"
6 #include "Teuchos_dyn_cast.hpp"
7 
8 #include "Epetra_LocalMap.h"
9 
10 #include "Sacado_Traits.hpp"
11 
12 namespace panzer {
13 
14 template <typename EvalT>
17 {
18  double locValue = Sacado::ScalarValue<ScalarT>::eval(value);
19  double glbValue = 0.0;
20 
21  // do global summation
22  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, static_cast<Thyra::Ordinal>(1), &locValue,&glbValue);
23 
24  value = glbValue;
25 
26  // built data in vectors
27  if(this->useEpetra()) {
28  // use epetra
29  this->getEpetraVector()[0] = glbValue;
30  }
31  else {
32  // use thyra
33  TEUCHOS_ASSERT(this->useThyra());
34 
35  this->getThyraVector()[0] = glbValue;
36  }
37 }
38 
39 template < >
42 {
43  using Teuchos::rcp_dynamic_cast;
44 
45  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
46 
47  // if its null, don't do anything
48  if(dgdx_unique==Teuchos::null)
49  return;
50 
51  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
52  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
53 
54  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
55 
56  uniqueContainer_ = Teuchos::null;
57 }
58 
59 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
60 template < >
63 {
64  using Teuchos::rcp_dynamic_cast;
65 
66  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
67 
68  // if its null, don't do anything
69  if(dgdx_unique==Teuchos::null)
70  return;
71 
72  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
73  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
74 
75  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
76 
77  uniqueContainer_ = Teuchos::null;
78 }
79 #endif
80 
81 template < >
84 {
85  const int n = value.size();
86  const int num_deriv = this->numDeriv();
87  TEUCHOS_ASSERT(n == 0 || n == num_deriv);
88  ScalarT glbValue = ScalarT(num_deriv, 0.0);
89 
90  // do global summation -- it is possible to do the reduceAll() on the Fad's directly, but it is somewhat
91  // complicated for DFad (due to temporaries that might get created). Since this is just a sum, it is
92  // easier to do the reduction for each value and derivative component.
93  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, Thyra::Ordinal(1), &value.val(), &glbValue.val());
94  if (num_deriv > 0)
95  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, Thyra::Ordinal(n), value.dx(), &glbValue.fastAccessDx(0));
96 
97  value = glbValue;
98 
99  // copy data in vectors
100  if(this->useEpetra()) {
101  // use epetra
102  Epetra_MultiVector& deriv = this->getEpetraMultiVector();
103  for (int i=0; i<num_deriv; ++i)
104  deriv[i][0] = glbValue.dx(i);
105  }
106  else {
107  // use thyra
108  TEUCHOS_ASSERT(this->useThyra());
109  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > deriv = this->getThyraMultiVector();
110  for (int i=0; i<num_deriv; ++i)
111  deriv[i][0] = glbValue.dx(i);
112  }
113 }
114 
115 // Do nothing unless derivatives are actually required
116 template <typename EvalT>
119 
120 // derivatives are required for
121 template < >
124 {
125  setDerivativeVectorSpace(soln_vs);
126 }
127 
128 // derivatives are required for
129 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
130 template < >
133 {
134  setDerivativeVectorSpace(soln_vs);
135 }
136 #endif
137 
138 // Do nothing unless derivatives are required
139 template <typename EvalT>
141 adjustForDirichletConditions(const GlobalEvaluationData & localBCRows,const GlobalEvaluationData & globalBCRows) { }
142 
143 // Do nothing unless derivatives are required
144 template < >
147 {
148  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
149  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
150  *ghostedContainer_,true,true);
151 }
152 
153 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
154 // Do nothing unless derivatives are required
155 template < >
157 adjustForDirichletConditions(const GlobalEvaluationData & localBCRows,const GlobalEvaluationData & globalBCRows)
158 {
159  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
160  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
161  *ghostedContainer_,true,true);
162 }
163 #endif
164 
165 }
166 
167 #endif
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
virtual void scatterResponse()
This simply does global summation, then shoves the result into a vector.
void setSolnVectorSpace(const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &soln_vs)
Set solution vector space.
#define TEUCHOS_ASSERT(assertion_test)
void adjustForDirichletConditions(const GlobalEvaluationData &localBCRows, const GlobalEvaluationData &globalBCRows)