Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_GlobalIndexer_Utilities_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 #include <vector>
44 #include <map>
45 
46 #include "Teuchos_FancyOStream.hpp"
47 #include "Teuchos_ArrayView.hpp"
48 #include "Teuchos_CommHelpers.hpp"
49 
50 #include "Tpetra_Map.hpp"
51 #include "Tpetra_Vector.hpp"
52 #include "Tpetra_Import.hpp"
53 
54 #include <sstream>
55 #include <cmath>
56 
57 namespace panzer {
58 
59 template <typename ScalarT,typename ArrayT>
60 void updateGhostedDataReducedVector(const std::string & fieldName,const std::string blockId,
61  const GlobalIndexer & ugi,
62  const ArrayT & data,Tpetra::MultiVector<ScalarT,int,panzer::GlobalOrdinal,panzer::TpetraNodeType> & dataVector)
63 {
64  typedef Tpetra::Map<int,panzer::GlobalOrdinal,panzer::TpetraNodeType> Map;
65 
66  TEUCHOS_TEST_FOR_EXCEPTION(!ugi.fieldInBlock(fieldName,blockId),std::runtime_error,
67  "panzer::updateGhostedDataReducedVector: field name = \""+fieldName+"\" is not in element block = \"" +blockId +"\"!");
68 
69  Teuchos::RCP<const Map> dataMap = dataVector.getMap();
70 
71  int fieldNum = ugi.getFieldNum(fieldName);
72  const std::vector<panzer::LocalOrdinal> & elements = ugi.getElementBlock(blockId);
73  const std::vector<int> & fieldOffsets = ugi.getGIDFieldOffsets(blockId,fieldNum);
74 
75  TEUCHOS_TEST_FOR_EXCEPTION(data.extent(0)!=elements.size(),std::runtime_error,
76  "panzer::updateGhostedDataReducedVector: data cell dimension does not match up with block cell count");
77 
78  int rank = data.rank();
79 
80  if(rank==2) {
81  // loop over elements distributing relevent data to vector
82  std::vector<panzer::GlobalOrdinal> gids;
83  for(std::size_t e=0;e<elements.size();e++) {
84  ugi.getElementGIDs(elements[e],gids);
85 
86  for(std::size_t f=0;f<fieldOffsets.size();f++) {
87  std::size_t localIndex = dataMap->getLocalElement(gids[fieldOffsets[f]]); // hash table lookup
88  dataVector.replaceLocalValue(localIndex,0,data(e,f));
89  }
90  }
91  }
92  else if(rank==3) {
93  std::size_t entries = data.extent(2);
94 
95  TEUCHOS_TEST_FOR_EXCEPTION(dataVector.getNumVectors()!=entries,std::runtime_error,
96  "panzer::updateGhostedDataReducedVector: number of columns in data vector inconsistent with data array");
97 
98  // loop over elements distributing relevent data to vector
99  std::vector<panzer::GlobalOrdinal> gids;
100  for(std::size_t e=0;e<elements.size();e++) {
101  ugi.getElementGIDs(elements[e],gids);
102 
103  for(std::size_t f=0;f<fieldOffsets.size();f++) {
104  std::size_t localIndex = dataMap->getLocalElement(gids[fieldOffsets[f]]); // hash table lookup
105  for(std::size_t v=0;v<entries;v++)
106  dataVector.replaceLocalValue(localIndex,v,data(e,f,v));
107  }
108  }
109  }
110  else
111  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,
112  "panzer::updateGhostedDataReducedVector: data array rank must be 2 or 3");
113 }
114 
115 template <typename ScalarT,typename ArrayT>
117 ArrayToFieldVector::getGhostedDataVector(const std::string & fieldName,const std::map<std::string,ArrayT> & data) const
118 {
119  TEUCHOS_ASSERT(data.size()>0); // there must be at least one "data" item
120 
121  int fieldNum = ugi_->getFieldNum(fieldName);
122  std::vector<std::string> blockIds;
123  ugi_->getElementBlockIds(blockIds);
124 
125  // get rank of first data array, determine column count
126  int rank = data.begin()->second.rank();
127  int numCols = 0;
128  if(rank==2)
129  numCols = 1;
130  else if(rank==3)
131  numCols = data.begin()->second.extent(2);
132  else {
133  TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,
134  "ArrayToFieldVector::getGhostedDataVector: data array must have rank 2 or 3. This array has rank " << rank << ".");
135  }
136 
137 
138  // first build and fill in final reduced field vector
140 
141  // build field maps as needed
142  Teuchos::RCP<const Map> reducedMap = gh_reducedFieldMaps_[fieldNum];
143  if(gh_reducedFieldMaps_[fieldNum]==Teuchos::null) {
144  reducedMap = panzer::getFieldMap(fieldNum,*gh_reducedFieldVector_);
145  gh_reducedFieldMaps_[fieldNum] = reducedMap;
146  }
147 
149  = Teuchos::rcp(new Tpetra::MultiVector<ScalarT,int,panzer::GlobalOrdinal,panzer::TpetraNodeType>(reducedMap,numCols));
150  for(std::size_t b=0;b<blockIds.size();b++) {
151  std::string block = blockIds[b];
152 
153  // make sure field is in correct block
154  if(!ugi_->fieldInBlock(fieldName,block))
155  continue;
156 
157  // extract data vector
158  typename std::map<std::string,ArrayT>::const_iterator blockItr = data.find(block);
159  TEUCHOS_TEST_FOR_EXCEPTION(blockItr==data.end(),std::runtime_error,
160  "ArrayToFieldVector::getDataVector: can not find block \""+block+"\".");
161 
162  const ArrayT & d = blockItr->second;
163  updateGhostedDataReducedVector<ScalarT,ArrayT>(fieldName,block,*ugi_,d,*finalReducedVec);
164  }
165 
166  // build final (not reduced vector)
168 
169  Teuchos::RCP<const Map> map = gh_fieldMaps_[fieldNum];
170  if(gh_fieldMaps_[fieldNum]==Teuchos::null) {
171  map = panzer::getFieldMap(fieldNum,*gh_fieldVector_);
172  gh_fieldMaps_[fieldNum] = map;
173  }
174 
176  = Teuchos::rcp(new Tpetra::MultiVector<ScalarT,int,panzer::GlobalOrdinal,panzer::TpetraNodeType>(map,numCols));
177 
178  // do import from finalReducedVec
179  Tpetra::Import<int,panzer::GlobalOrdinal,panzer::TpetraNodeType> importer(reducedMap,map);
180  finalVec->doImport(*finalReducedVec,importer,Tpetra::INSERT);
181 
182  return finalVec;
183 }
184 
185 template <typename ScalarT,typename ArrayT>
187 ArrayToFieldVector::getDataVector(const std::string & fieldName,const std::map<std::string,ArrayT> & data) const
188 {
189  // if neccessary build field vector
190  if(fieldVector_==Teuchos::null)
192 
194  = getGhostedDataVector<ScalarT,ArrayT>(fieldName,data);
195 
196  // use lazy construction for each field
197  int fieldNum = ugi_->getFieldNum(fieldName);
198  Teuchos::RCP<const Map> destMap = fieldMaps_[fieldNum];
199  if(fieldMaps_[fieldNum]==Teuchos::null) {
200  destMap = panzer::getFieldMap(fieldNum,*fieldVector_);
201  fieldMaps_[fieldNum] = destMap;
202  }
203 
205  = Teuchos::rcp(new Tpetra::MultiVector<ScalarT,int,panzer::GlobalOrdinal,panzer::TpetraNodeType>(destMap,sourceVec->getNumVectors()));
206 
207  // do import
208  Tpetra::Import<int,panzer::GlobalOrdinal> importer(sourceVec->getMap(),destMap);
209  destVec->doImport(*sourceVec,importer,Tpetra::INSERT);
210 
211  return destVec;
212 }
213 
214 } // end namspace panzer
std::map< int, Teuchos::RCP< const Map > > fieldMaps_
(unghosted) field vector (as needed)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::map< int, Teuchos::RCP< const Map > > gh_fieldMaps_
Maps for each field (as needed)
Teuchos::RCP< const IntVector > gh_reducedFieldVector_
virtual const std::vector< panzer::LocalOrdinal > & getElementBlock(const std::string &blockId) const =0
std::map< int, Teuchos::RCP< const Map > > gh_reducedFieldMaps_
ghosted field vector
Teuchos::RCP< const IntVector > gh_fieldVector_
ghosted reduced field vector
Teuchos::RCP< const Tpetra::Map< int, panzer::GlobalOrdinal, panzer::TpetraNodeType > > getFieldMap(int fieldNum, const Tpetra::Vector< int, int, panzer::GlobalOrdinal, panzer::TpetraNodeType > &fieldTVector)
virtual bool fieldInBlock(const std::string &field, const std::string &block) const =0
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void updateGhostedDataReducedVector(const std::string &fieldName, const std::string blockId, const GlobalIndexer &ugi, const ArrayT &data, Tpetra::MultiVector< ScalarT, int, panzer::GlobalOrdinal, panzer::TpetraNodeType > &dataVector)
Teuchos::RCP< const GlobalIndexer > ugi_
DOF mapping.
Teuchos::RCP< Tpetra::MultiVector< ScalarT, int, panzer::GlobalOrdinal, panzer::TpetraNodeType > > getGhostedDataVector(const std::string &fieldName, const std::map< std::string, ArrayT > &data) const
virtual int getFieldNum(const std::string &str) const =0
Get the number used for access to this field.
Teuchos::RCP< Tpetra::MultiVector< ScalarT, int, panzer::GlobalOrdinal, panzer::TpetraNodeType > > getDataVector(const std::string &fieldName, const std::map< std::string, ArrayT > &data) const
virtual const std::vector< int > & getGIDFieldOffsets(const std::string &blockId, int fieldNum) const =0
Use the field pattern so that you can find a particular field in the GIDs array.
virtual void getElementGIDs(panzer::LocalOrdinal localElmtId, std::vector< panzer::GlobalOrdinal > &gids, const std::string &blockIdHint="") const =0
Get the global IDs for a particular element. This function overwrites the gids variable.
void buildFieldVector(const Tpetra::Vector< int, int, panzer::GlobalOrdinal, panzer::TpetraNodeType > &source) const
build unghosted field vector from ghosted field vector
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< const IntVector > fieldVector_
Maps for each field (as needed)