Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Response_ExtremeValue_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_ExtremeValue_impl_hpp__
44 #define __Panzer_Response_ExtremeValue_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  if(useMax_)
65  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_MAX, static_cast<Thyra::Ordinal>(1), &locValue,&glbValue);
66  else
67  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_MIN, static_cast<Thyra::Ordinal>(1), &locValue,&glbValue);
68 
69  value = glbValue;
70 
71  // built data in vectors
72  if(this->useEpetra()) {
73  // use epetra
74  this->getEpetraVector()[0] = glbValue;
75  }
76  else {
77  // use thyra
78  TEUCHOS_ASSERT(this->useThyra());
79 
80  this->getThyraVector()[0] = glbValue;
81  }
82 }
83 
84 template < >
87 {
88  using Teuchos::rcp_dynamic_cast;
89 
90  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
91 
92  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
93  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
94 
95  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
96 
97  uniqueContainer_ = Teuchos::null;
98 }
99 
100 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
101 template < >
104 {
105  using Teuchos::rcp_dynamic_cast;
106 
107  Teuchos::RCP<Thyra::MultiVectorBase<double> > dgdx_unique = getDerivative();
108 
109  uniqueContainer_ = linObjFactory_->buildLinearObjContainer();
110  Teuchos::rcp_dynamic_cast<ThyraObjContainer<double> >(uniqueContainer_)->set_x_th(dgdx_unique->col(0));
111 
112  linObjFactory_->ghostToGlobalContainer(*ghostedContainer_,*uniqueContainer_,LinearObjContainer::X);
113 
114  uniqueContainer_ = Teuchos::null;
115 }
116 #endif
117 
118 template < >
121 {
122  const int n = value.size();
123  const int num_deriv = this->numDeriv();
124  TEUCHOS_ASSERT(n == 0 || n == num_deriv);
125  ScalarT glbValue = ScalarT(num_deriv, 0.0);
126 
127  // do global min/max on value
128  if(useMax_)
129  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_MAX, Thyra::Ordinal(1), &value.val(), &glbValue.val());
130  else
131  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_MIN, Thyra::Ordinal(1), &value.val(), &glbValue.val());
132 
133  // find the minimum processor who's local value == the global min/max value
134  if (num_deriv > 0) {
135  int locProc = value.val() == glbValue.val() ? this->getComm()->getRank() : this->getComm()->getSize();
136  int glbProc = 0;
137  Teuchos::reduceAll(*this->getComm(), Teuchos::REDUCE_MIN, Thyra::Ordinal(1), &locProc, &glbProc);
138 
139  // now broadcast the derivatives from proc glbProc
140  Teuchos::broadcast(*this->getComm(), glbProc, Thyra::Ordinal(n), &glbValue.fastAccessDx(0));
141  }
142 
143  value = glbValue;
144 
145  // copy data in vectors
146  if(this->useEpetra()) {
147  // use epetra
148  Epetra_MultiVector& deriv = this->getEpetraMultiVector();
149  for (int i=0; i<num_deriv; ++i)
150  deriv[i][0] = glbValue.dx(i);
151  }
152  else {
153  // use thyra
154  TEUCHOS_ASSERT(this->useThyra());
155  Thyra::ArrayRCP< Thyra::ArrayRCP<double> > deriv = this->getThyraMultiVector();
156  for (int i=0; i<num_deriv; ++i)
157  deriv[i][0] = glbValue.dx(i);
158  }
159 }
160 
161 // Do nothing unless derivatives are actually required
162 template <typename EvalT>
165 
166 // derivatives are required for
167 template < >
170 {
171  setDerivativeVectorSpace(soln_vs);
172 }
173 
174 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
175 // derivatives are required for
176 template < >
179 {
180  setDerivativeVectorSpace(soln_vs);
181 }
182 #endif
183 
184 // Do nothing unless derivatives are required
185 template <typename EvalT>
187 adjustForDirichletConditions(const GlobalEvaluationData & /* localBCRows */, const GlobalEvaluationData & /* globalBCRows */) { }
188 
189 // Do nothing unless derivatives are required
190 template < >
193 {
194  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
195  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
196  *ghostedContainer_,true,true);
197 }
198 
199 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
200 // Do nothing unless derivatives are required
201 template < >
204 {
205  linObjFactory_->adjustForDirichletConditions(Teuchos::dyn_cast<const LinearObjContainer>(localBCRows),
206  Teuchos::dyn_cast<const LinearObjContainer>(globalBCRows),
207  *ghostedContainer_,true,true);
208 }
209 #endif
210 
211 }
212 
213 #endif
virtual void scatterResponse()
This simply does global summation, then shoves the result into a vector.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
void setSolnVectorSpace(const Teuchos::RCP< const Thyra::VectorSpaceBase< double > > &soln_vs)
Set solution vector space.
void adjustForDirichletConditions(const GlobalEvaluationData &localBCRows, const GlobalEvaluationData &globalBCRows)
#define TEUCHOS_ASSERT(assertion_test)