Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_GatherTangent_Tpetra_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_GATHER_TANGENT_TPETRA_IMPL_HPP
44 #define PANZER_GATHER_TANGENT_TPETRA_IMPL_HPP
45 
46 #include "Teuchos_Assert.hpp"
47 #include "Phalanx_DataLayout.hpp"
48 
49 #include "Panzer_GlobalIndexer.hpp"
50 #include "Panzer_PureBasis.hpp"
54 
55 #include "Teuchos_FancyOStream.hpp"
56 
57 #include "Tpetra_Vector.hpp"
58 #include "Tpetra_Map.hpp"
59 
60 template<typename EvalT,typename TRAITS,typename LO,typename GO,typename NodeT>
64  const Teuchos::ParameterList& p)
65  : globalIndexer_(indexer)
66  , useTimeDerivativeSolutionVector_(false)
67  , globalDataKey_("Tangent Gather Container")
68 {
69  const std::vector<std::string>& names =
70  *(p.get< Teuchos::RCP< std::vector<std::string> > >("DOF Names"));
71 
73 
74  // this is beging to fix the issues with incorrect use of const
76  if(p.isType< Teuchos::RCP<panzer::PureBasis> >("Basis"))
77  basis = p.get< Teuchos::RCP<panzer::PureBasis> >("Basis");
78  else
79  basis = p.get< Teuchos::RCP<const panzer::PureBasis> >("Basis");
80 
81  gatherFields_.resize(names.size());
82  for (std::size_t fd = 0; fd < names.size(); ++fd) {
83  gatherFields_[fd] =
84  PHX::MDField<ScalarT,Cell,NODE>(names[fd],basis->functional);
85  this->addEvaluatedField(gatherFields_[fd]);
86  // If tpetraContainer_ is null, the evalaution is a no-op. In this
87  // case we need to preserve zero initial value. Do this by not
88  // sharing.
89  this->addUnsharedField(gatherFields_[fd].fieldTag().clone());
90  }
91 
92  if (p.isType<bool>("Use Time Derivative Solution Vector"))
93  useTimeDerivativeSolutionVector_ = p.get<bool>("Use Time Derivative Solution Vector");
94 
95  if (p.isType<std::string>("Global Data Key"))
96  globalDataKey_ = p.get<std::string>("Global Data Key");
97 
98  this->setName("Gather Tangent");
99 }
100 
101 // **********************************************************************
102 template<typename EvalT,typename TRAITS,typename LO,typename GO,typename NodeT>
104 postRegistrationSetup(typename TRAITS::SetupData /* d */,
105  PHX::FieldManager<TRAITS>& /* fm */)
106 {
107  TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_->size());
108 
109  fieldIds_.resize(gatherFields_.size());
110 
111  for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
112  const std::string& fieldName = (*indexerNames_)[fd];
113  fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName);
114  }
115 
116  indexerNames_ = Teuchos::null; // Don't need this anymore
117 }
118 
119 // **********************************************************************
120 template<typename EvalT,typename TRAITS,typename LO,typename GO,typename NodeT>
122 preEvaluate(typename TRAITS::PreEvalData d)
123 {
124  using Teuchos::RCP;
125  using Teuchos::rcp_dynamic_cast;
126 
128 
129  // try to extract linear object container
130  if (d.gedc->containsDataObject(globalDataKey_)) {
131  RCP<GlobalEvaluationData> ged = d.gedc->getDataObject(globalDataKey_);
133  rcp_dynamic_cast<LOCPair_GlobalEvaluationData>(ged);
134 
135  if(loc_pair!=Teuchos::null) {
137  tpetraContainer_ = rcp_dynamic_cast<LOC>(loc,true);
138  }
139 
140  if(tpetraContainer_==Teuchos::null) {
141  tpetraContainer_ = rcp_dynamic_cast<LOC>(ged,true);
142  }
143  }
144 }
145 
146 // **********************************************************************
147 template<typename EvalT,typename TRAITS,typename LO,typename GO,typename NodeT>
149 evaluateFields(typename TRAITS::EvalData workset)
150 {
151  // If tpetraContainer_ was not initialized, then no global evaluation data
152  // container was set, in which case this evaluator becomes a no-op
153  if (tpetraContainer_ == Teuchos::null)
154  return;
155 
157  // for convenience pull out some objects from workset
158  std::string blockId = this->wda(workset).block_id;
159  const std::vector<std::size_t> & localCellIds = this->wda(workset).cell_local_ids;
160 
162  if (useTimeDerivativeSolutionVector_)
163  x = tpetraContainer_->get_dxdt();
164  else
165  x = tpetraContainer_->get_x();
166 
167  Teuchos::ArrayRCP<const double> x_array = x->get1dView();
168 
169  // NOTE: A reordering of these loops will likely improve performance
170  // The "getGIDFieldOffsets may be expensive. However the
171  // "getElementGIDs" can be cheaper. However the lookup for LIDs
172  // may be more expensive!
173 
174  // gather operation for each cell in workset
175  for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
176  std::size_t cellLocalId = localCellIds[worksetCellIndex];
177 
178  auto LIDs = globalIndexer_->getElementLIDs(cellLocalId);
179 
180  // loop over the fields to be gathered
181  for (std::size_t fieldIndex=0; fieldIndex<gatherFields_.size();fieldIndex++) {
182  int fieldNum = fieldIds_[fieldIndex];
183  const std::vector<int> & elmtOffset = globalIndexer_->getGIDFieldOffsets(blockId,fieldNum);
184 
185  // loop over basis functions and fill the fields
186  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
187  int offset = elmtOffset[basis];
188  LO lid = LIDs[offset];
189  (gatherFields_[fieldIndex])(worksetCellIndex,basis) = x_array[lid];
190  }
191  }
192  }
193 }
194 
195 // **********************************************************************
196 
197 #endif
Teuchos::RCP< std::vector< std::string > > indexerNames_
void preEvaluate(typename TRAITS::PreEvalData d)
void evaluateFields(typename TRAITS::EvalData d)
T & get(const std::string &name, T def_value)
virtual Teuchos::RCP< CloneableEvaluator > clone(const Teuchos::ParameterList &pl) const
std::vector< PHX::MDField< ScalarT, Cell, NODE > > gatherFields_
Teuchos::RCP< LinearObjContainer > getGhostedLOC() const
bool isType(const std::string &name) const
void postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager< TRAITS > &vm)
#define TEUCHOS_ASSERT(assertion_test)