Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_ParameterListCallbackBlocked_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 #ifdef PANZER_HAVE_TEKO
44 
45 namespace panzer_stk {
46 
47 using Teuchos::RCP;
48 using Teuchos::rcp;
49 
50 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
51 ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::ParameterListCallbackBlocked(
52  const Teuchos::RCP<const panzer_stk::STKConnManager<GlobalOrdinalT> > & connManager,
54  const Teuchos::RCP<const panzer::BlockedDOFManager<int,GlobalOrdinalT> > & aux_blocked_ugi)
55  : connManager_(connManager), blocked_ugi_(blocked_ugi), aux_blocked_ugi_(aux_blocked_ugi)
56 {
57 }
58 
59 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
61 ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::request(const Teko::RequestMesg & rm)
62 {
63  TEUCHOS_ASSERT(handlesRequest(rm)); // design by contract
64 
65  // loop over parameter list and set the field by a particular key
67  Teuchos::RCP<const Teuchos::ParameterList> inputPL = rm.getParameterList();
69  for(itr=inputPL->begin();itr!=inputPL->end();++itr) {
70  std::string * str_ptr = 0; // just used as a template specifier
71  std::string field = inputPL->entry(itr).getValue(str_ptr);
72  setFieldByKey(itr->first,field,*outputPL);
73  }
74 
75  return outputPL;
76 }
77 
78 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
79 bool ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::handlesRequest(const Teko::RequestMesg & rm)
80 {
81  // check if is a parameter list message, and that the parameter
82  // list contains the right fields
83  if(rm.getName()=="Parameter List") {
84  bool isHandled = true;
85  Teuchos::RCP<const Teuchos::ParameterList> pl = rm.getParameterList();
86  std::string field;
87  if(pl->isType<std::string>("x-coordinates")) {
88  field = pl->get<std::string>("x-coordinates");
89  if(!isField(field)) {
90  return false;
91  }
92  }
93  if(pl->isType<std::string>("y-coordinates")) {
94  // we assume that the fields must be the same
95  if(field != pl->get<std::string>("y-coordinates")) {
96  return false;
97  }
98  }
99  if(pl->isType<std::string>("z-coordinates")) {
100  // we assume that the fields must be the same
101  if(field != pl->get<std::string>("z-coordinates")) {
102  return false;
103  }
104  }
105  if(pl->isType<std::string>("Coordinates")){
106  field = pl->get<std::string>("Coordinates");
107  }
108  if(pl->isType<std::string>("Coordinates-Epetra")){
109  field = pl->get<std::string>("Coordinates-Epetra");
110  }
111 
112  return isHandled;
113  }
114  else return false;
115 }
116 
117 template <typename LocalOrdinalT, typename GlobalOrdinalT, typename Node>
118 void ParameterListCallbackBlocked<LocalOrdinalT, GlobalOrdinalT, Node>::
119 preRequest(const Teko::RequestMesg & rm)
120 {
121  TEUCHOS_ASSERT(handlesRequest(rm)); // design by contract
122 
123  const std::string& field(getHandledField(*rm.getParameterList()));
124 
125  // Check if the field is in the main UGI. If it's not, assume it's in the
126  // auxiliary UGI.
127  bool useAux(true);
128  std::vector<Teuchos::RCP<panzer::UniqueGlobalIndexer<int, GlobalOrdinalT>>>
129  fieldDOFMngrs = blocked_ugi_->getFieldDOFManagers();
130  for (int b(0); b < static_cast<int>(fieldDOFMngrs.size()); ++b)
131  {
132  for (int f(0); f < fieldDOFMngrs[b]->getNumFields(); ++f)
133  {
134  if (fieldDOFMngrs[b]->getFieldString(f) == field)
135  useAux = false;
136  }
137  }
138 
139  int block(-1);
140  if (useAux)
141  block =
142  aux_blocked_ugi_->getFieldBlock(aux_blocked_ugi_->getFieldNum(field));
143  else
144  block = blocked_ugi_->getFieldBlock(blocked_ugi_->getFieldNum(field));
145 
146  // Empty... Nothing to do.
147  if (rm.getParameterList()->isType<std::string>("Coordinates-Epetra")) {
148  buildArrayToVectorEpetra(block, field, useAux);
149  buildCoordinatesEpetra(field, useAux);
150  } else {
151  buildArrayToVectorTpetra(block, field, useAux);
152  buildCoordinatesTpetra(field, useAux);
153  }
154 }
155 
156 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
157 void ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::setFieldByKey(const std::string & key,const std::string & field,Teuchos::ParameterList & pl) const
158 {
159  // x-, y-, z-coordinates needed for ML, Coordinates needed for MueLu
160  if(key=="x-coordinates") {
161  double * x = const_cast<double *>(&getCoordinateByField(0,field)[0]);
162  pl.set<double*>(key,x);
163  }
164  else if(key=="y-coordinates") {
165  double * y = const_cast<double *>(&getCoordinateByField(1,field)[0]);
166  pl.set<double*>(key,y);
167  }
168  else if(key=="z-coordinates") {
169  double * z = const_cast<double *>(&getCoordinateByField(2,field)[0]);
170  pl.set<double*>(key,z);
171  } else if(key == "Coordinates") {
173  } else if(key == "Coordinates-Epetra") {
174  pl.set<Teuchos::RCP<Epetra_MultiVector> >("Coordinates",coordsVecEp_);
175  // pl.remove("Coordinates-Epetra");
176  }
177  else
178  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,
179  "ParameterListCallback cannot handle key=\"" << key << "\"");
180 }
181 
182 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
183 void ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::buildArrayToVectorTpetra(int block,const std::string & field, const bool useAux)
184 {
185  if(arrayToVectorTpetra_[field]==Teuchos::null) {
187  if(useAux)
188  ugi = aux_blocked_ugi_->getFieldDOFManagers()[block];
189  else
190  ugi = blocked_ugi_->getFieldDOFManagers()[block];
192  }
193 }
194 
195 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
196 void ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::buildArrayToVectorEpetra(int block,const std::string & field, const bool useAux)
197 {
198  if(arrayToVectorEpetra_[field]==Teuchos::null) {
200  if(useAux)
201  ugi = aux_blocked_ugi_->getFieldDOFManagers()[block];
202  else
203  ugi = blocked_ugi_->getFieldDOFManagers()[block];
205  }
206 }
207 
208 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
209 void ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::buildCoordinatesTpetra(const std::string & field, const bool useAux)
210 {
211  std::map<std::string,Kokkos::DynRankView<double,PHX::Device> > data;
212 
213  Teuchos::RCP<const panzer::Intrepid2FieldPattern> fieldPattern = getFieldPattern(field,useAux);
214 
215  std::vector<std::string> elementBlocks;
216  if(useAux)
217  aux_blocked_ugi_->getElementBlockIds(elementBlocks);
218  else
219  blocked_ugi_->getElementBlockIds(elementBlocks);
220  for(std::size_t i=0;i<elementBlocks.size();++i) {
221  std::string blockId = elementBlocks[i];
222  std::vector<std::size_t> localCellIds;
223 
224  // allocate block of data to store coordinates
225  Kokkos::DynRankView<double,PHX::Device> & fieldData = data[blockId];
226  fieldData = Kokkos::DynRankView<double,PHX::Device>("fieldData",connManager_->getElementBlock(blockId).size(),fieldPattern->numberIds());
227 
228  if(fieldPattern->supportsInterpolatoryCoordinates()) {
229  // get degree of freedom coordiantes
230  connManager_->getDofCoords(blockId,*fieldPattern,localCellIds,fieldData);
231  }
232  else {
233  Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
234  out.setOutputToRootOnly(-1);
235  out << "WARNING: In ParameterListCallback::buildCoordinates(), the Intrepid2::FieldPattern in "
236  << "block \"" << blockId << "\" does not support interpolatory coordinates. "
237  << "This may be fine if coordinates are not actually needed. However if they are then bad things "
238  << "will happen. Enjoy!" << std::endl;
239 
240  return;
241  }
242  }
243 
244  coordsVecTp_ = arrayToVectorTpetra_[field]->template getDataVector<double>(field,data);
245 
246  switch(coordsVecTp_->getNumVectors()) {
247  case 3:
248  zcoords_[field].resize(coordsVecTp_->getLocalLength());
249  coordsVecTp_->getVector(2)->get1dCopy(Teuchos::arrayViewFromVector(zcoords_[field]));
250  // Intentional fall-through.
251  case 2:
252  ycoords_[field].resize(coordsVecTp_->getLocalLength());
253  coordsVecTp_->getVector(1)->get1dCopy(Teuchos::arrayViewFromVector(ycoords_[field]));
254  // Intentional fall-through.
255  case 1:
256  xcoords_[field].resize(coordsVecTp_->getLocalLength());
257  coordsVecTp_->getVector(0)->get1dCopy(Teuchos::arrayViewFromVector(xcoords_[field]));
258  break;
259  default:
260  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
261  "ParameterListCallback::buildCoordinates: Constructed multivector has nonphysical dimensions.");
262  break;
263  }
264 }
265 
266 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
267 void ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::buildCoordinatesEpetra(const std::string & field, const bool useAux)
268 {
269  std::map<std::string,Kokkos::DynRankView<double,PHX::Device> > data;
270 
271  Teuchos::RCP<const panzer::Intrepid2FieldPattern> fieldPattern = getFieldPattern(field,useAux);
272 
273  std::vector<std::string> elementBlocks;
274  if(useAux)
275  aux_blocked_ugi_->getElementBlockIds(elementBlocks);
276  else
277  blocked_ugi_->getElementBlockIds(elementBlocks);
278  for(std::size_t i=0;i<elementBlocks.size();++i) {
279  std::string blockId = elementBlocks[i];
280  std::vector<std::size_t> localCellIds;
281 
282  // allocate block of data to store coordinates
283  Kokkos::DynRankView<double,PHX::Device> & fieldData = data[blockId];
284  fieldData = Kokkos::DynRankView<double,PHX::Device>("fieldData",connManager_->getElementBlock(blockId).size(),fieldPattern->numberIds());
285 
286  if(fieldPattern->supportsInterpolatoryCoordinates()) {
287  // get degree of freedom coordiantes
288  connManager_->getDofCoords(blockId,*fieldPattern,localCellIds,fieldData);
289  }
290  else {
291  Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
292  out.setOutputToRootOnly(-1);
293  out << "WARNING: In ParameterListCallback::buildCoordinates(), the Intrepid2::FieldPattern in "
294  << "block \"" << blockId << "\" does not support interpolatory coordinates. "
295  << "This may be fine if coordinates are not actually needed. However if they are then bad things "
296  << "will happen. Enjoy!" << std::endl;
297 
298  return;
299  }
300  }
301 
302  coordsVecEp_ = arrayToVectorEpetra_[field]->template getDataVector<double>(field,data);
303 }
304 
305 
306 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
307 std::string ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::
308 getHandledField(const Teuchos::ParameterList & pl) const
309 {
310  // because this method assumes handlesRequest is true, this call will succeed
311  if(pl.isType<std::string>("x-coordinates"))
312  return pl.get<std::string>("x-coordinates");
313  else if(pl.isType<std::string>("Coordinates"))
314  return pl.get<std::string>("Coordinates");
315  else if(pl.isType<std::string>("Coordinates-Epetra"))
316  return pl.get<std::string>("Coordinates-Epetra");
317  else
318  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Neither x-coordinates nor Coordinates or Coordinates-Epetra field provided.");
319 
320 }
321 
322 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
323 const std::vector<double> & ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>::
324 getCoordinateByField(int dim,const std::string & field) const
325 {
326  TEUCHOS_ASSERT(dim>=0);
327  TEUCHOS_ASSERT(dim<=2);
328 
329  // get the coordinate vector you want
330  const std::map<std::string,std::vector<double> > * coord = nullptr;
331  if(dim==0) coord = &xcoords_;
332  else if(dim==1) coord = &ycoords_;
333  else if(dim==2) coord = &zcoords_;
334  else {
335  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,
336  "ParameterListCallbackBlocked::getCoordinateByField: dim is not in range of [0,2] for field \""
337  << field << "\".");
338  }
339 
340  std::map<std::string,std::vector<double> >::const_iterator itr;
341  itr = coord->find(field);
342 
343  TEUCHOS_TEST_FOR_EXCEPTION(itr==coord->end(),std::runtime_error,
344  "ParameterListCallbackBlocked::getCoordinateByField: Coordinates for field \"" + field +
345  "\" dimension " << dim << " have not been built!");
346 
347  return itr->second;
348 }
349 
350 template <typename LocalOrdinalT,typename GlobalOrdinalT,typename Node>
351 Teuchos::RCP<const panzer::Intrepid2FieldPattern> ParameterListCallbackBlocked<LocalOrdinalT,GlobalOrdinalT,Node>
352 ::getFieldPattern(const std::string & fieldName, const bool useAux) const
353 {
354  std::vector<std::string> elementBlocks;
355  if(useAux)
356  aux_blocked_ugi_->getElementBlockIds(elementBlocks);
357  else
358  blocked_ugi_->getElementBlockIds(elementBlocks);
359 
360  for(std::size_t e=0;e<elementBlocks.size();e++) {
361  std::string blockId = elementBlocks[e];
362 
363  if(blocked_ugi_->fieldInBlock(fieldName,blockId))
364  return Teuchos::rcp_dynamic_cast<const panzer::Intrepid2FieldPattern>(blocked_ugi_->getFieldPattern(blockId,fieldName),true);
365 
366  if(aux_blocked_ugi_->fieldInBlock(fieldName,blockId))
367  return Teuchos::rcp_dynamic_cast<const panzer::Intrepid2FieldPattern>(aux_blocked_ugi_->getFieldPattern(blockId,fieldName),true);
368  }
369 
370  return Teuchos::null;
371 }
372 
373 
374 }
375 
376 #endif
ConstIterator end() const
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T & getValue(T *ptr) const
virtual int numberIds() const
bool supportsInterpolatoryCoordinates() const
Does this field pattern support interpolatory coordinates?
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
params_t::ConstIterator ConstIterator
ConstIterator begin() const
const ParameterEntry & entry(ConstIterator i) const
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
bool isType(const std::string &name) const
#define TEUCHOS_ASSERT(assertion_test)