Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Response_Functional_impl.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_Response_Functional_impl_hpp__
44 #define __Panzer_Response_Functional_impl_hpp__
45 
46 #include "Teuchos_Comm.hpp"
47 #include "Teuchos_CommHelpers.hpp"
48 #include "Teuchos_dyn_cast.hpp"
49 
50 #include "Epetra_LocalMap.h"
51 
52 #include "Sacado_Traits.hpp"
53 
54 namespace panzer {
55 
56 template <typename EvalT>
59 {
60  double locValue = Sacado::ScalarValue<ScalarT>::eval(value);
61  double glbValue = 0.0;
62 
63  // do global summation
64  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, static_cast<Thyra::Ordinal>(1), &locValue,&glbValue);
65 
66  value = glbValue;
67 
68  // built data in vectors
69  if(this->useEpetra()) {
70  // use epetra
71  this->getEpetraVector()[0] = glbValue;
72  }
73  else {
74  // use thyra
75  TEUCHOS_ASSERT(this->useThyra());
76 
77  this->getThyraVector()[0] = glbValue;
78  }
79 }
80 
81 template < >
84 {
85  using Teuchos::rcp_dynamic_cast;
86 
87  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
88 
89  // if its null, don't do anything
90  if(dgdx_unique==Teuchos::null)
91  return;
92 
93  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
94  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
95 
96  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
97 
98  uniqueContainer_ = Teuchos::null;
99 }
100 
101 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
102 template < >
105 {
106  using Teuchos::rcp_dynamic_cast;
107 
108  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
109 
110  // if its null, don't do anything
111  if(dgdx_unique==Teuchos::null)
112  return;
113 
114  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
115  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
116 
117  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
118 
119  uniqueContainer_ = Teuchos::null;
120 }
121 #endif
122 
123 template < >
126 {
127  const int n = value.size();
128  const int num_deriv = this->numDeriv();
129  TEUCHOS_ASSERT(n == 0 || n == num_deriv);
130  ScalarT glbValue = ScalarT(num_deriv, 0.0);
131 
132  // do global summation -- it is possible to do the reduceAll() on the Fad's directly, but it is somewhat
133  // complicated for DFad (due to temporaries that might get created). Since this is just a sum, it is
134  // easier to do the reduction for each value and derivative component.
135  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, Thyra::Ordinal(1), &value.val(), &glbValue.val());
136  if (num_deriv > 0)
137  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_SUM, Thyra::Ordinal(n), value.dx(), &glbValue.fastAccessDx(0));
138 
139  value = glbValue;
140 
141  // copy data in vectors
142  if(this->useEpetra()) {
143  // use epetra
144  Epetra_MultiVector& deriv = this->getEpetraMultiVector();
145  for (int i=0; i<num_deriv; ++i)
146  deriv[i][0] = glbValue.dx(i);
147  }
148  else {
149  // use thyra
150  TEUCHOS_ASSERT(this->useThyra());
151  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > deriv = this->getThyraMultiVector();
152  for (int i=0; i<num_deriv; ++i)
153  deriv[i][0] = glbValue.dx(i);
154  }
155 }
156 
157 // Do nothing unless derivatives are actually required
158 template <typename EvalT>
161 
162 // derivatives are required for
163 template < >
166 {
167  setDerivativeVectorSpace(soln_vs);
168 }
169 
170 // derivatives are required for
171 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
172 template < >
175 {
176  setDerivativeVectorSpace(soln_vs);
177 }
178 #endif
179 
180 // Do nothing unless derivatives are required
181 template <typename EvalT>
183 adjustForDirichletConditions(const GlobalEvaluationData & /* localBCRows */, const GlobalEvaluationData & /* globalBCRows */) { }
184 
185 // Do nothing unless derivatives are required
186 template < >
189 {
190  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
191  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
192  *ghostedContainer_,true,true);
193 }
194 
195 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
196 // Do nothing unless derivatives are required
197 template < >
200 {
201  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
202  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
203  *ghostedContainer_,true,true);
204 }
205 #endif
206 
207 }
208 
209 #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)