Panzer  Version of the Day
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_ScatterResidual_TpetraSG_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_SCATTER_RESIDUAL_TPETRA_SG_IMPL_HPP
44 #define PANZER_SCATTER_RESIDUAL_TPETRA_SG_IMPL_HPP
45 
46 #include "Panzer_config.hpp"
47 #ifdef HAVE_STOKHOS
48 
50 #include "Phalanx_DataLayout_MDALayout.hpp"
51 
52 // **********************************************************************
53 // Specialization: SGResidual
54 // **********************************************************************
55 
56 template<typename TRAITS,typename LO,typename GO,typename NodeT>
59  const Teuchos::ParameterList& p)
60  : globalIndexer_(indexer)
61 {
62  std::string scatterName = p.get<std::string>("Scatter Name");
63  scatterHolder_ =
64  Teuchos::rcp(new PHX::Tag<ScalarT>(scatterName,Teuchos::rcp(new PHX::MDALayout<Dummy>(0))));
65 
66  // get names to be evaluated
67  const std::vector<std::string>& names =
68  *(p.get< Teuchos::RCP< std::vector<std::string> > >("Dependent Names"));
69 
70  // grab map from evaluated names to field names
71  fieldMap_ = p.get< Teuchos::RCP< std::map<std::string,std::string> > >("Dependent Map");
72 
74  p.get< Teuchos::RCP<const panzer::PureBasis> >("Basis")->functional;
75 
76  // build the vector of fields that this is dependent on
77  scatterFields_.resize(names.size());
78  for (std::size_t eq = 0; eq < names.size(); ++eq) {
79  scatterFields_[eq] = PHX::MDField<ScalarT,Cell,NODE>(names[eq],dl);
80 
81  // tell the field manager that we depend on this field
82  this->addDependentField(scatterFields_[eq]);
83  }
84 
85  // this is what this evaluator provides
86  this->addEvaluatedField(*scatterHolder_);
87 
88  this->setName(scatterName+" Scatter Residual (SGResidual)");
89 }
90 
91 // **********************************************************************
92 template<typename TRAITS,typename LO,typename GO,typename NodeT>
94 postRegistrationSetup(typename TRAITS::SetupData d,
96 {
97  // globalIndexer_ = d.globalIndexer_;
98 
99  fieldIds_.resize(scatterFields_.size());
100  // load required field numbers for fast use
101  for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
102  // get field ID from DOF manager
103  std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
104  fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName);
105 
106  // fill field data object
107  this->utils.setFieldData(scatterFields_[fd],fm);
108  }
109 }
110 
111 // **********************************************************************
112 template<typename TRAITS,typename LO,typename GO,typename NodeT>
114 evaluateFields(typename TRAITS::EvalData workset)
115 {
116  TEUCHOS_ASSERT(false); // this doesn't even work
117 /*
118  typedef TpetraLinearObjContainer<double,LO,GO,NodeT> LOC;
119  typedef SGTpetraLinearObjContainer<double,LO,GO,NodeT> SGLOC;
120 
121  std::vector<GO> GIDs;
122  std::vector<LO> LIDs;
123 
124  // for convenience pull out some objects from workset
125  std::string blockId = this->wda(workset).block_id;
126  const std::vector<std::size_t> & localCellIds = this->wda(workset).cell_local_ids;
127 
128  Teuchos::RCP<SGLOC> sgTpetraContainer
129  = Teuchos::rcp_dynamic_cast<SGLOC>(workset.ghostedLinContainer);
130  Teuchos::RCP<typename LOC::VectorType> r_template = (*sgTpetraContainer->begin())->get_f();
131  Teuchos::RCP<const typename LOC::MapType> map = r_template->getMap();
132 
133  // NOTE: A reordering of these loops will likely improve performance
134  // The "getGIDFieldOffsets may be expensive. However the
135  // "getElementGIDs" can be cheaper. However the lookup for LIDs
136  // may be more expensive!
137 
138  // scatter operation for each cell in workset
139  for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
140  std::size_t cellLocalId = localCellIds[worksetCellIndex];
141 
142  globalIndexer_->getElementGIDs(cellLocalId,GIDs,blockId);
143 
144  // caculate the local IDs for this element
145  LIDs.resize(GIDs.size());
146  for(std::size_t i=0;i<GIDs.size();i++)
147  LIDs[i] = map->getLocalElement(GIDs[i]);
148 
149  // loop over each field to be scattered
150  for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
151  int fieldNum = fieldIds_[fieldIndex];
152  const std::vector<int> & elmtOffset = globalIndexer_->getGIDFieldOffsets(blockId,fieldNum);
153 
154  // loop over basis functions
155  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
156  int offset = elmtOffset[basis];
157  LO lid = LIDs[offset];
158 
159  const ScalarT field = (scatterFields_[fieldIndex])(worksetCellIndex,basis);
160 
161  // loop over stochastic basis scatter field values to residual vectors
162  int stochIndex = 0;
163  typename SGLOC::iterator itr;
164  for(itr=sgTpetraContainer->begin();itr!=sgTpetraContainer->end();++itr,++stochIndex) {
165  Teuchos::ArrayRCP<double> f_array = (*itr)->get_f()->get1dViewNonConst();
166  f_array[lid] += field.coeff(stochIndex);
167  }
168  }
169  }
170  }
171 */
172 }
173 
174 // **********************************************************************
175 // Specialization: SGJacobian
176 // **********************************************************************
177 
178 template<typename TRAITS,typename LO,typename GO,typename NodeT>
181  const Teuchos::ParameterList& p)
182  : globalIndexer_(indexer)
183 {
184  std::string scatterName = p.get<std::string>("Scatter Name");
185  scatterHolder_ =
186  Teuchos::rcp(new PHX::Tag<ScalarT>(scatterName,Teuchos::rcp(new PHX::MDALayout<Dummy>(0))));
187 
188  // get names to be evaluated
189  const std::vector<std::string>& names =
190  *(p.get< Teuchos::RCP< std::vector<std::string> > >("Dependent Names"));
191 
192  // grab map from evaluated names to field names
193  fieldMap_ = p.get< Teuchos::RCP< std::map<std::string,std::string> > >("Dependent Map");
194 
196  p.get< Teuchos::RCP<const panzer::PureBasis> >("Basis")->functional;
197 
198  // build the vector of fields that this is dependent on
199  scatterFields_.resize(names.size());
200  for (std::size_t eq = 0; eq < names.size(); ++eq) {
201  scatterFields_[eq] = PHX::MDField<ScalarT,Cell,NODE>(names[eq],dl);
202 
203  // tell the field manager that we depend on this field
204  this->addDependentField(scatterFields_[eq]);
205  }
206 
207  // this is what this evaluator provides
208  this->addEvaluatedField(*scatterHolder_);
209 
210  this->setName(scatterName+" Scatter Residual (SGJacobian)");
211 }
212 
213 // **********************************************************************
214 template<typename TRAITS,typename LO,typename GO,typename NodeT>
216 postRegistrationSetup(typename TRAITS::SetupData d,
218 {
219  // globalIndexer_ = d.globalIndexer_;
220 
221  fieldIds_.resize(scatterFields_.size());
222  // load required field numbers for fast use
223  for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
224  // get field ID from DOF manager
225  std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
226  fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName);
227 
228  // fill field data object
229  this->utils.setFieldData(scatterFields_[fd],fm);
230  }
231 }
232 
233 // **********************************************************************
234 template<typename TRAITS,typename LO,typename GO,typename NodeT>
236 evaluateFields(typename TRAITS::EvalData workset)
237 {
238  TEUCHOS_ASSERT(false);
239 
240 /*
241  typedef TpetraLinearObjContainer<double,LO,GO,NodeT> LOC;
242  typedef SGTpetraLinearObjContainer<double,LO,GO,NodeT> SGLOC;
243 
244  std::vector<GO> GIDs;
245  std::vector<LO> rLIDs, cLIDs;
246  std::vector<double> jacRow;
247 
248  // for convenience pull out some objects from workset
249  std::string blockId = this->wda(workset).block_id;
250  const std::vector<std::size_t> & localCellIds = this->wda(workset).cell_local_ids;
251 
252  Teuchos::RCP<SGLOC> sgTpetraContainer
253  = Teuchos::rcp_dynamic_cast<SGLOC>(workset.ghostedLinContainer);
254  Teuchos::RCP<typename LOC::CrsMatrixType> Jac_template = (*sgTpetraContainer->begin())->get_A();
255  Teuchos::RCP<const typename LOC::MapType> rMap = Jac_template->getRowMap();
256  Teuchos::RCP<const typename LOC::MapType> cMap = Jac_template->getColMap();
257 
258  // NOTE: A reordering of these loops will likely improve performance
259  // The "getGIDFieldOffsets" may be expensive. However the
260  // "getElementGIDs" can be cheaper. However the lookup for LIDs
261  // may be more expensive!
262 
263  // scatter operation for each cell in workset
264  for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
265  std::size_t cellLocalId = localCellIds[worksetCellIndex];
266 
267  globalIndexer_->getElementGIDs(cellLocalId,GIDs,blockId);
268 
269  // caculate the local IDs for this element
270  rLIDs.resize(GIDs.size());
271  cLIDs.resize(GIDs.size());
272  for(std::size_t i=0;i<GIDs.size();i++) {
273  rLIDs[i] = rMap->getLocalElement(GIDs[i]);
274  cLIDs[i] = cMap->getLocalElement(GIDs[i]);
275  }
276 
277  // loop over each field to be scattered
278  for(std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
279  int fieldNum = fieldIds_[fieldIndex];
280  const std::vector<int> & elmtOffset = globalIndexer_->getGIDFieldOffsets(blockId,fieldNum);
281 
282  // loop over the basis functions (currently they are nodes)
283  for(std::size_t rowBasisNum = 0; rowBasisNum < elmtOffset.size(); rowBasisNum++) {
284  const ScalarT scatterField = (scatterFields_[fieldIndex])(worksetCellIndex,rowBasisNum);
285  int rowOffset = elmtOffset[rowBasisNum];
286  LO row = rLIDs[rowOffset];
287 
288  // loop over stochastic basis scatter field values to residual vectors
289  int stochIndex = 0;
290  typename SGLOC::iterator itr;
291  for(itr=sgTpetraContainer->begin();itr!=sgTpetraContainer->end();++itr,++stochIndex) {
292  Teuchos::RCP<typename LOC::VectorType> r = (*itr)->get_f();
293  Teuchos::RCP<typename LOC::CrsMatrixType> Jac = (*itr)->get_A();
294 
295  // Sum residual
296  if(r!=Teuchos::null) {
297  r->sumIntoLocalValue(row,scatterField.val().coeff(stochIndex));
298  }
299 
300  // loop over the sensitivity indices: all DOFs on a cell
301  jacRow.resize(scatterField.size());
302 
303  for(int sensIndex=0;sensIndex<scatterField.size();++sensIndex)
304  jacRow[sensIndex] = scatterField.fastAccessDx(sensIndex).coeff(stochIndex);
305 
306  // Sum SGJacobian
307  Jac->sumIntoLocalValues(row, cLIDs, jacRow);
308  }
309  } // end rowBasisNum
310  } // end fieldIndex
311  }
312 */
313 }
314 
315 // **********************************************************************
316 
317 #endif
318 
319 #endif
std::vector< PHX::MDField< const ScalarT, panzer::Cell, panzer::Point > > scatterFields_
T & get(const std::string &name, T def_value)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pushes residual values into the residual vector for a Newton-based solve.
#define TEUCHOS_ASSERT(assertion_test)