Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_Utilities.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 
45 #include "Panzer_STK_Utilities.hpp"
47 
48 #include "Kokkos_DynRankView.hpp"
49 
50 #include <stk_mesh/base/FieldBase.hpp>
51 
52 namespace panzer_stk {
53 
54 template <typename GlobalOrdinal>
55 static void gather_in_block(const std::string & blockId, const panzer::UniqueGlobalIndexer<int,GlobalOrdinal> & dofMngr,
56  const Epetra_Vector & x,const std::vector<std::size_t> & localCellIds,
57  std::map<std::string,Kokkos::DynRankView<double,PHX::Device> > & fc);
58 
59 static void build_local_ids(const panzer_stk::STK_Interface & mesh,
60  std::map<std::string,Teuchos::RCP<std::vector<std::size_t> > > & localIds);
61 
62 void write_cell_data(panzer_stk::STK_Interface & mesh,const std::vector<double> & data,const std::string & fieldName)
63 {
64  std::vector<std::string> blocks;
65  mesh.getElementBlockNames(blocks);
66 
67  // loop over element blocks
68  for(std::size_t eb=0;eb<blocks.size();eb++) {
69  const std::string & blockId = blocks[eb];
71 
72  std::vector<stk::mesh::Entity> elements;
73  mesh.getMyElements(blockId,elements);
74 
75  // loop over elements in this block
76  for(std::size_t el=0;el<elements.size();el++) {
77  std::size_t localId = mesh.elementLocalId(elements[el]);
78  double * solnData = stk::mesh::field_data(*field,elements[el]);
79  TEUCHOS_ASSERT(solnData!=0); // sanity check
80  solnData[0] = data[localId];
81  }
82  }
83 }
84 
85 template <typename GlobalOrdinal>
86 void write_solution_data(const panzer::UniqueGlobalIndexer<int,GlobalOrdinal> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_MultiVector & x,const std::string & prefix,const std::string & postfix)
87 {
88  write_solution_data(dofMngr,mesh,*x(0),prefix,postfix);
89 }
90 
91 template
92 void write_solution_data<int>(const panzer::UniqueGlobalIndexer<int,int> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_MultiVector & x,const std::string & prefix,const std::string & postfix);
93 #ifdef PANZER_HAVE_LONG_LONG_INT
94 template
95 void write_solution_data<panzer::Ordinal64>(const panzer::UniqueGlobalIndexer<int,panzer::Ordinal64> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_MultiVector & x,const std::string & prefix,const std::string & postfix);
96 #endif
97 
98 template <typename GlobalOrdinal>
99 void write_solution_data(const panzer::UniqueGlobalIndexer<int,GlobalOrdinal> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_Vector & x,const std::string & prefix,const std::string & postfix)
100 {
101  typedef Kokkos::DynRankView<double,PHX::Device> FieldContainer;
102 
103  // get local IDs
104  std::map<std::string,Teuchos::RCP<std::vector<std::size_t> > > localIds;
105  build_local_ids(mesh,localIds);
106 
107  // loop over all element blocks
108  std::map<std::string,Teuchos::RCP<std::vector<std::size_t> > >::const_iterator itr;
109  for(itr=localIds.begin();itr!=localIds.end();++itr) {
110  std::string blockId = itr->first;
111  const std::vector<std::size_t> & localCellIds = *(itr->second);
112 
113  std::map<std::string,FieldContainer> data;
114 
115  // get all solution data for this block
116  gather_in_block(blockId,dofMngr,x,localCellIds,data);
117 
118  // write out to stk mesh
119  std::map<std::string,FieldContainer>::iterator dataItr;
120  for(dataItr=data.begin();dataItr!=data.end();++dataItr)
121  mesh.setSolutionFieldData(prefix+dataItr->first+postfix,blockId,localCellIds,dataItr->second);
122  }
123 }
124 
125 template
126 void write_solution_data<int>(const panzer::UniqueGlobalIndexer<int,int> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_Vector & x,const std::string & prefix,const std::string & postfix);
127 #ifdef PANZER_HAVE_LONG_LONG_INT
128 template
129 void write_solution_data<panzer::Ordinal64>(const panzer::UniqueGlobalIndexer<int,panzer::Ordinal64> & dofMngr,panzer_stk::STK_Interface & mesh,const Epetra_Vector & x,const std::string & prefix,const std::string & postfix);
130 #endif
131 
132 
133 template <typename GlobalOrdinal>
134 void gather_in_block(const std::string & blockId, const panzer::UniqueGlobalIndexer<int,GlobalOrdinal> & dofMngr,
135  const Epetra_Vector & x,const std::vector<std::size_t> & localCellIds,
136  std::map<std::string,Kokkos::DynRankView<double,PHX::Device> > & fc)
137 {
138  const std::vector<int> & fieldNums = dofMngr.getBlockFieldNumbers(blockId);
139 
140  for(std::size_t fieldIndex=0;fieldIndex<fieldNums.size();fieldIndex++) {
141  int fieldNum = fieldNums[fieldIndex];
142  std::string fieldStr = dofMngr.getFieldString(fieldNum);
143 
144  // grab the field
145  const std::vector<int> & elmtOffset = dofMngr.getGIDFieldOffsets(blockId,fieldNum);
146  fc[fieldStr] = Kokkos::DynRankView<double,PHX::Device>("fc",localCellIds.size(),elmtOffset.size());
147 
148  // gather operation for each cell in workset
149  for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
150  std::vector<GlobalOrdinal> GIDs;
151  std::vector<int> LIDs;
152  std::size_t cellLocalId = localCellIds[worksetCellIndex];
153 
154  dofMngr.getElementGIDs(cellLocalId,GIDs);
155 
156  // caculate the local IDs for this element
157  LIDs.resize(GIDs.size());
158  for(std::size_t i=0;i<GIDs.size();i++)
159  LIDs[i] = x.Map().LID(GIDs[i]);
160 
161  // loop over basis functions and fill the fields
162  for(std::size_t basis=0;basis<elmtOffset.size();basis++) {
163  int offset = elmtOffset[basis];
164  int lid = LIDs[offset];
165  fc[fieldStr](worksetCellIndex,basis) = x[lid];
166  }
167  }
168  }
169 }
170 
172  std::map<std::string,Teuchos::RCP<std::vector<std::size_t> > > & localIds)
173 {
174  // defines ordering of blocks
175  std::vector<std::string> blockIds;
176  mesh.getElementBlockNames(blockIds);
177 
178  std::vector<std::string>::const_iterator idItr;
179  for(idItr=blockIds.begin();idItr!=blockIds.end();++idItr) {
180  std::string blockId = *idItr;
181 
182  localIds[blockId] = Teuchos::rcp(new std::vector<std::size_t>);
183  std::vector<std::size_t> & localBlockIds = *localIds[blockId];
184 
185  // grab elements on this block
186  std::vector<stk::mesh::Entity> blockElmts;
187  mesh.getMyElements(blockId,blockElmts);
188 
189  std::vector<stk::mesh::Entity>::const_iterator itr;
190  for(itr=blockElmts.begin();itr!=blockElmts.end();++itr)
191  localBlockIds.push_back(mesh.elementLocalId(*itr));
192 
193  std::sort(localBlockIds.begin(),localBlockIds.end());
194  }
195 }
196 
197 }
virtual void getElementGIDs(LocalOrdinalT localElmtId, std::vector< GlobalOrdinalT > &gids, const std::string &blockIdHint="") const =0
Get the global IDs for a particular element. This function overwrites the gids variable.
void setSolutionFieldData(const std::string &fieldName, const std::string &blockId, const std::vector< std::size_t > &localElementIds, const ArrayT &solutionValues, double scaleValue=1.0)
void getElementBlockNames(std::vector< std::string > &names) const
stk::mesh::Field< double > SolutionFieldType
void write_solution_data(const panzer::UniqueGlobalIndexer< int, GlobalOrdinal > &dofMngr, panzer_stk::STK_Interface &mesh, const Epetra_MultiVector &x, const std::string &prefix, const std::string &postfix)
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.
std::size_t elementLocalId(stk::mesh::Entity elmt) const
static void gather_in_block(const std::string &blockId, const panzer::UniqueGlobalIndexer< int, GlobalOrdinal > &dofMngr, const Epetra_Vector &x, const std::vector< std::size_t > &localCellIds, std::map< std::string, Kokkos::DynRankView< double, PHX::Device > > &fc)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
template void write_solution_data< int >(const panzer::UniqueGlobalIndexer< int, int > &dofMngr, panzer_stk::STK_Interface &mesh, const Epetra_MultiVector &x, const std::string &prefix, const std::string &postfix)
static void build_local_ids(const panzer_stk::STK_Interface &mesh, std::map< std::string, Teuchos::RCP< std::vector< std::size_t > > > &localIds)
virtual const std::string & getFieldString(int num) const =0
Reverse lookup of the field string from a field number.
void write_cell_data(panzer_stk::STK_Interface &mesh, const std::vector< double > &data, const std::string &fieldName)
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...
virtual const std::vector< int > & getBlockFieldNumbers(const std::string &blockId) const =0
#define TEUCHOS_ASSERT(assertion_test)
void getMyElements(std::vector< stk::mesh::Entity > &elements) const
stk::mesh::Field< double > * getCellField(const std::string &fieldName, const std::string &blockId) const