Panzer  Version of the Day
Panzer_GatherSolution_BlockedEpetra_Hessian_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_GatherSolution_BlockedEpetra_Hessian_impl_hpp__
44 #define __Panzer_GatherSolution_BlockedEpetra_Hessian_impl_hpp__
45 
46 // only do this if required by the user
47 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
48 
49 #include "Teuchos_Assert.hpp"
50 #include "Phalanx_DataLayout.hpp"
51 
53 #include "Panzer_PureBasis.hpp"
59 
60 #include "Teuchos_FancyOStream.hpp"
61 
62 #include "Thyra_SpmdVectorBase.hpp"
63 #include "Thyra_ProductVectorBase.hpp"
64 
65 namespace panzer {
66 
67 // **********************************************************************
68 // Specialization: Hessian
69 // **********************************************************************
70 
71 template<typename TRAITS,typename LO,typename GO>
73 GatherSolution_BlockedEpetra(const std::vector<Teuchos::RCP<const UniqueGlobalIndexer<LO,int> > > & indexers,
74  const Teuchos::ParameterList& p)
75  : indexers_(indexers)
76 {
77  typedef std::vector< std::vector<std::string> > vvstring;
78 
79  GatherSolution_Input input;
80  input.setParameterList(p);
81 
82  const std::vector<std::string> & names = input.getDofNames();
83  Teuchos::RCP<const panzer::PureBasis> basis = input.getBasis();
84 
85  indexerNames_ = input.getIndexerNames();
86  useTimeDerivativeSolutionVector_ = input.useTimeDerivativeSolutionVector();
87  globalDataKey_ = input.getGlobalDataKey();
88 
89  gatherSeedIndex_ = input.getGatherSeedIndex();
90  sensitivitiesName_ = input.getSensitivitiesName();
91  firstSensitivitiesAvailable_ = input.firstSensitivitiesAvailable();
92 
93  secondSensitivitiesAvailable_ = input.secondSensitivitiesAvailable();
94  sensitivities2ndPrefix_ = input.getSecondSensitivityDataKeyPrefix();
95 
96  // allocate fields
97  gatherFields_.resize(names.size());
98  for (std::size_t fd = 0; fd < names.size(); ++fd) {
99  PHX::MDField<ScalarT,Cell,NODE> f(names[fd],basis->functional);
100  gatherFields_[fd] = f;
101  this->addEvaluatedField(gatherFields_[fd]);
102  }
103 
104  // figure out what the first active name is
105  std::string firstName = "<none>";
106  if(names.size()>0)
107  firstName = names[0];
108 
109  // print out convenience
110  if(!firstSensitivitiesAvailable_) {
111  std::string n = "GatherSolution (BlockedEpetra, No First Sensitivities): "+firstName+" (Hessian)";
112  this->setName(n);
113  }
114  else {
115  std::string n = "GatherSolution (BlockedEpetra): "+firstName+" ("+PHX::typeAsString<EvalT>()+") ";
116  this->setName(n);
117  }
118 }
119 
120 // **********************************************************************
121 template<typename TRAITS,typename LO,typename GO>
123 postRegistrationSetup(typename TRAITS::SetupData d,
125 {
126  TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size());
127 
128  indexerIds_.resize(gatherFields_.size());
129  subFieldIds_.resize(gatherFields_.size());
130 
131  for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
132  // get field ID from DOF manager
133  const std::string& fieldName = indexerNames_[fd];
134 
135  indexerIds_[fd] = getFieldBlock(fieldName,indexers_);
136  subFieldIds_[fd] = indexers_[indexerIds_[fd]]->getFieldNum(fieldName);
137 
138  TEUCHOS_ASSERT(indexerIds_[fd]>=0);
139 
140  // setup the field data object
141  this->utils.setFieldData(gatherFields_[fd],fm);
142  }
143 
144  indexerNames_.clear(); // Don't need this anymore
145 }
146 
147 // **********************************************************************
148 template<typename TRAITS,typename LO,typename GO>
150 preEvaluate(typename TRAITS::PreEvalData d)
151 {
152  typedef BlockedEpetraLinearObjContainer BLOC;
153 
154  using Teuchos::RCP;
155  using Teuchos::rcp_dynamic_cast;
156 
157  // manage sensitivities
159  if(firstSensitivitiesAvailable_) {
160  if(d.first_sensitivities_name==sensitivitiesName_)
161  firstApplySensitivities_ = true;
162  else
163  firstApplySensitivities_ = false;
164  }
165  else
166  firstApplySensitivities_ = false;
167 
168  if(secondSensitivitiesAvailable_) {
169  if(d.second_sensitivities_name==sensitivitiesName_)
170  secondApplySensitivities_ = true;
171  else
172  secondApplySensitivities_ = false;
173  }
174  else
175  secondApplySensitivities_ = false;
176 
178 
179  RCP<GlobalEvaluationData> ged;
180 
181  // first try refactored ReadOnly container
182  std::string post = useTimeDerivativeSolutionVector_ ? " - Xdot" : " - X";
183  if(d.gedc.containsDataObject(globalDataKey_+post)) {
184  ged = d.gedc.getDataObject(globalDataKey_+post);
185 
186  RCP<BlockedVector_ReadOnly_GlobalEvaluationData> ro_ged = rcp_dynamic_cast<BlockedVector_ReadOnly_GlobalEvaluationData>(ged,true);
187 
188  x_ = rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(ro_ged->getGhostedVector());
189 
190  // post condition
191  TEUCHOS_TEST_FOR_EXCEPTION(x_==Teuchos::null,std::logic_error,
192  "Gather Hessian: Can't find x vector in GEDC \"" << globalDataKey_ << post << "\""
193  "A cast failed for " << ged << ". Type is " << Teuchos::typeName(*ged) << std::endl);
194  }
195  else if(d.gedc.containsDataObject(globalDataKey_)) {
196  ged = d.gedc.getDataObject(globalDataKey_);
197 
198  // extract linear object container
199  RCP<const BlockedVector_ReadOnly_GlobalEvaluationData> ro_ged = rcp_dynamic_cast<const BlockedVector_ReadOnly_GlobalEvaluationData>(ged);
200  RCP<const BlockedEpetraLinearObjContainer> blockedContainer = rcp_dynamic_cast<const BLOC>(ged);
201 
202  if(ro_ged!=Teuchos::null) {
203  RCP<BlockedVector_ReadOnly_GlobalEvaluationData> ro_ged = rcp_dynamic_cast<BlockedVector_ReadOnly_GlobalEvaluationData>(ged,true);
204 
205  x_ = rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(ro_ged->getGhostedVector());
206  }
207  else if(blockedContainer!=Teuchos::null) {
208  if (useTimeDerivativeSolutionVector_)
209  x_ = rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(blockedContainer->get_dxdt());
210  else
211  x_ = rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(blockedContainer->get_x());
212  }
213 
214  // post condition
215  TEUCHOS_TEST_FOR_EXCEPTION(x_==Teuchos::null,std::logic_error,
216  "Gather Hessian: Can't find x vector in GEDC \"" << globalDataKey_ << "\" (" << post << "). "
217  "A cast failed for " << ged << ". Type is " << Teuchos::typeName(*ged));
218  } // end "else if" contains(globalDataKey)
219 
220  // post condition
221  TEUCHOS_ASSERT(x_!=Teuchos::null); // someone has to find the x_ vector
222 
223  // don't try to extract the dx, if its not required
224  if(!secondApplySensitivities_) {
225  dx_ = Teuchos::null; // do set it to null though!
226  return;
227  }
228 
229  // get second derivative perturbation
231 
232  // now parse the second derivative direction
233  if(d.gedc.containsDataObject(sensitivities2ndPrefix_+globalDataKey_)) {
234  ged = d.gedc.getDataObject(sensitivities2ndPrefix_+globalDataKey_);
235 
236  RCP<BlockedVector_ReadOnly_GlobalEvaluationData> ro_ged = rcp_dynamic_cast<BlockedVector_ReadOnly_GlobalEvaluationData>(ged,true);
237 
238  dx_ = rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(ro_ged->getGhostedVector());
239  }
240 
241  // post conditions
242  TEUCHOS_TEST_FOR_EXCEPTION(dx_==Teuchos::null,std::logic_error,
243  "Cannot find sensitivity vector associated with \""+sensitivities2ndPrefix_+globalDataKey_+"\" and \""+post+"\""); // someone has to find the dx_ vector
244 }
245 
246 // **********************************************************************
247 template<typename TRAITS,typename LO,typename GO>
249 evaluateFields(typename TRAITS::EvalData workset)
250 {
251  using Teuchos::RCP;
252  using Teuchos::rcp_dynamic_cast;
253  using Teuchos::ptrFromRef;
254  using Teuchos::ArrayRCP;
255 
256  using Thyra::VectorBase;
257  using Thyra::SpmdVectorBase;
259 
260  // for convenience pull out some objects from workset
261  std::string blockId = this->wda(workset).block_id;
262  const std::vector<std::size_t> & localCellIds = this->wda(workset).cell_local_ids;
263 
264  double seed_value = 0.0;
265  if(firstApplySensitivities_) {
266  if (useTimeDerivativeSolutionVector_ && gatherSeedIndex_<0) {
267  seed_value = workset.alpha;
268  }
269  else if (gatherSeedIndex_<0) {
270  seed_value = workset.beta;
271  }
272  else if(!useTimeDerivativeSolutionVector_) {
273  seed_value = workset.gather_seeds[gatherSeedIndex_];
274  }
275  else {
276  TEUCHOS_ASSERT(false);
277  }
278  }
279 
280  // turn off sensitivies: this may be faster if we don't expand the term
281  // but I suspect not because anywhere it is used the full complement of
282  // sensitivies will be needed anyway.
283  if(!firstApplySensitivities_)
284  seed_value = 0.0;
285 
286  std::vector<int> blockOffsets;
287  computeBlockOffsets(blockId,indexers_,blockOffsets);
288 
289  // loop over the fields to be gathered
290  for(std::size_t fieldIndex=0;
291  fieldIndex<gatherFields_.size();fieldIndex++) {
292 
293  PHX::MDField<ScalarT,Cell,NODE> & field = gatherFields_[fieldIndex];
294 
295  int indexerId = indexerIds_[fieldIndex];
296  int subFieldNum = subFieldIds_[fieldIndex];
297 
298  // grab local data for inputing
299  Teuchos::ArrayRCP<const double> local_x, local_dx;
300  rcp_dynamic_cast<SpmdVectorBase<double> >(x_->getNonconstVectorBlock(indexerId))->getLocalData(ptrFromRef(local_x));
301  if(secondApplySensitivities_)
302  rcp_dynamic_cast<SpmdVectorBase<double> >(dx_->getNonconstVectorBlock(indexerId))->getLocalData(ptrFromRef(local_dx));
303 
304  auto subRowIndexer = indexers_[indexerId];
305  const std::vector<int> & elmtOffset = subRowIndexer->getGIDFieldOffsets(blockId,subFieldNum);
306 
307  int startBlkOffset = blockOffsets[indexerId];
308 
309  // gather operation for each cell in workset
310  for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
311  std::size_t cellLocalId = localCellIds[worksetCellIndex];
312 
313  const std::vector<int> & LIDs = subRowIndexer->getElementLIDs(cellLocalId);
314 
315  if(!firstApplySensitivities_) {
316  // loop over basis functions and fill the fields
317  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
318  int offset = elmtOffset[basis];
319  int lid = LIDs[offset];
320 
321  // set the value and seed the FAD object
322  field(worksetCellIndex,basis) = local_x[lid];
323  }
324  }
325  else {
326  // loop over basis functions and fill the fields
327  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
328  int offset = elmtOffset[basis];
329  int lid = LIDs[offset];
330 
331  // set the value and seed the FAD object
332  field(worksetCellIndex,basis).val() = local_x[lid];
333  field(worksetCellIndex,basis).fastAccessDx(startBlkOffset+offset) = seed_value;
334  }
335  }
336 
337  // this is the direction to use for the second derivative
338  if(secondApplySensitivities_) {
339  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
340  int offset = elmtOffset[basis];
341  int lid = LIDs[offset];
342  field(worksetCellIndex,basis).val().fastAccessDx(0) = local_dx[lid];
343  }
344  }
345  }
346  }
347 }
348 
349 // **********************************************************************
350 
351 } // end namespace panzer
352 
353 #endif
354 
355 #endif
int getFieldBlock(const std::string &fieldName, const std::vector< Teuchos::RCP< UniqueGlobalIndexer< LocalOrdinalT, GlobalOrdinalT > > > &ugis)
void computeBlockOffsets(const std::string &blockId, const std::vector< Teuchos::RCP< UniqueGlobalIndexer< LocalOrdinalT, GlobalOrdinalT > > > &ugis, std::vector< int > &blockOffsets)
PHX::MDField< const ScalarT > input
PHX::MDField< ScalarT > vector
void postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager< TRAITS > &vm)
PHX::MDField< const ScalarT, Cell, IP > field
Gathers solution values from the Newton solution vector into the nodal fields of the field manager...
Teuchos::RCP< const panzer::PureBasis > basis
Interpolates basis DOF values to IP DOF values.