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