Nalu
Nalu: a generalized unstructured massively parallel low Mach flow code designed to support a variety of energy applications of interest (most notably Wind ECP) built on the Sierra Toolkit and Trilinos solver Tpetra/Epetra stack. The open source BSD, clause 3 license model has been chosen for the code base. See LICENSE for more information. http://NaluCFD.org
ToMesh.h
Go to the documentation of this file.
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2014 Sandia Corporation. */
3 /* This software is released under the license detailed */
4 /* in the file, LICENSE, which is located in the top-level Nalu */
5 /* directory structure */
6 /*------------------------------------------------------------------------*/
7 
8 
9 #ifndef ToMesh_h
10 #define ToMesh_h
11 
12 #include <string>
13 #include <vector>
14 #include <utility>
15 
16 #include <stk_util/parallel/Parallel.hpp>
17 
18 #include <stk_mesh/base/Entity.hpp>
19 #include <stk_mesh/base/BulkData.hpp>
20 #include <stk_mesh/base/MetaData.hpp>
21 
22 #include <stk_search/IdentProc.hpp>
23 #include <stk_search/BoundingBox.hpp>
24 
25 #include <FieldTypeDef.h>
26 
27 // stk
28 namespace stk {
29 namespace mesh {
30 class Part;
31 typedef std::vector<Part*> PartVector;
32 class MetaData;
33 class BulkData;
34 }
35 }
36 
37 namespace sierra{
38 namespace nalu{
39 
40 class ToMesh {
41 public :
42  typedef stk::mesh:: Entity Entity;
43  typedef std::vector<Entity> EntityVec;
44  typedef stk::mesh:: EntityKey EntityKey;
45  typedef std::set <EntityKey> EntityKeySet;
46  typedef stk::search::IdentProc<EntityKey, unsigned> EntityProc;
47  typedef std::vector<EntityProc> EntityProcVec;
48 
49  typedef stk::search::Point<double> Point;
50  typedef stk::search::Sphere<double> Sphere;
51  typedef std::pair<Sphere,EntityProc> BoundingBox;
52 
53  enum {Dimension = 3};
54 
55  typedef std::vector<std::pair<std::string, std::string> > PairNames;
56  std::vector< const stk::mesh::FieldBase *>
57  get_fields(const stk::mesh::MetaData &toMetaData, const PairNames &VarPairName) {
58  // will want to check that all is well with field registration
59  bool allFieldsAreFine = true;
60  std::vector< const stk::mesh::FieldBase *> toFieldVec;
61  // provide field names
62  for( PairNames::const_iterator i=VarPairName.begin(); i!=VarPairName.end(); ++i) {
63  const std::string &fieldName = i->second;
64  const stk::mesh::FieldBase *tofield = stk::mesh::get_field_by_name(fieldName,toMetaData);
65  if ( NULL == tofield ) {
66  allFieldsAreFine = false;
67  NaluEnv::self().naluOutputP0()
68  << "Xfer::ToMesh:Error field: " << fieldName
69  << " has not been registered anywhere within the ToRealm: " << toRealm_.name() << std::endl;
70  }
71  else {
72  // always push back; check for errors below
73  toFieldVec.push_back(tofield);
74 
75  // check that the field is defined on **all** parts
76  stk::mesh::Selector fieldSelector = stk::mesh::selectField(*tofield);
77  for ( size_t k = 0; k < toPartVec_.size(); ++k ) {
78 
79  const stk::mesh::BucketVector &partBuckets
80  = toBulkData_.get_buckets(stk::topology::NODE_RANK, stk::mesh::Selector(*toPartVec_[k]));
81 
82  bool fieldIsFine = true;
83  for ( stk::mesh::BucketVector::const_iterator ib = partBuckets.begin();
84  ib != partBuckets.end() ; ++ib ) {
85  stk::mesh::Bucket & b = **ib ;
86  fieldIsFine &= fieldSelector(b);
87  }
88 
89  // local check to make sure that the field is somewhere (delay the throw)
90  if ( !fieldIsFine ) {
91  NaluEnv::self().naluOutputP0()
92  << "Xfer::ToMesh:Error field: " << tofield->name()
93  << " is not registered on part: " << toPartVec_[k]->name() << std::endl;
94  allFieldsAreFine = false;
95  }
96  }
97  }
98  }
99 
100  // final error check; only return when all is well
101  if ( allFieldsAreFine ) {
102  return toFieldVec;
103  }
104  else {
105  throw std::runtime_error("Xfer::ToMesh:Error field registration on desired parts of the mesh is not complete");
106  }
107  }
108 
110  stk::mesh::MetaData &toMetaData,
111  stk::mesh::BulkData &toBulkData,
112  Realm &toRealm,
113  const std::string &coordinates_name,
114  const PairNames &VarPairName,
115  const stk::mesh::PartVector &toPartVec,
116  const stk::ParallelMachine comm,
117  const double radius)
118  : toMetaData_(toMetaData),
119  toBulkData_(toBulkData),
120  toRealm_ (toRealm),
121  tocoordinates_(toMetaData.get_field<VectorFieldType>(stk::topology::NODE_RANK,coordinates_name)),
122  toPartVec_(toPartVec),
123  toFieldVec_ (get_fields(toMetaData, VarPairName)),
124  comm_(comm),
125  radius_(radius)
126  {
127  // nothing to do
128  }
129 
130  ~ToMesh(){};
131 
133  bool operator()(const BoundingBox &a, const BoundingBox & b) const
134  {
135  return a.second.id() < b.second.id();
136  }
137  };
138 
139 
140  // Needed for STK Transfer
141  stk::ParallelMachine comm() const {return comm_;}
142 
143  void bounding_boxes (std::vector<BoundingBox> &v) const
144  {
145 
146  const unsigned spatial_dimension = toMetaData_.spatial_dimension();
147 
148  Point center;
149 
150  stk::mesh::Selector s_locally_owned_union = toMetaData_.locally_owned_part()
151  &stk::mesh::selectUnion(toPartVec_);
152 
153  stk::mesh::BucketVector const& node_buckets
154  = toBulkData_.get_buckets( stk::topology::NODE_RANK, s_locally_owned_union );
155  for ( stk::mesh::BucketVector::const_iterator ib = node_buckets.begin();
156  ib != node_buckets.end() ; ++ib ) {
157  stk::mesh::Bucket & b = **ib ;
158 
159  const stk::mesh::Bucket::size_type length = b.size();
160 
161  for ( stk::mesh::Bucket::size_type k = 0 ; k < length ; ++k ) {
162 
163  // get node
164  stk::mesh::Entity node = b[k];
165 
166  double * coord = stk::mesh::field_data(*tocoordinates_, node);
167  for (unsigned i=0; i<spatial_dimension; ++i) {
168  center[i] = coord[i];
169  }
170 
171  // setup ident
172  ToMesh::EntityProc theIdent(toBulkData_.entity_key(node), toBulkData_.parallel_rank());
173 
174  ToMesh::BoundingBox theBox(Sphere(center,radius_), theIdent );
175  v.push_back(theBox);
176  }
177  }
178  std::sort(v.begin(), v.end(), BoundingBoxCompare());
179  }
180 
182  {
183  std::vector<const stk::mesh::FieldBase *> fields(toFieldVec_.begin(), toFieldVec_.end());
184  stk::mesh::copy_owned_to_shared ( toBulkData_, fields);
185  }
186 
187  stk::mesh::MetaData &toMetaData_;
188  stk::mesh::BulkData &toBulkData_;
192  const std::vector< const stk::mesh::FieldBase *> toFieldVec_;
193  const stk::ParallelMachine comm_;
194  const double radius_;
195 
196  typedef std::map<stk::mesh::EntityKey, std::vector<double> > TransferInfo;
197  TransferInfo TransferInfo_;
198 
199 };
200 
201 } // namespace nalu
202 } // namespace Sierra
203 
204 #endif
std::vector< Part * > PartVector
Definition: Algorithm.h:16
Definition: ABLForcingAlgorithm.C:26
stk::search::Sphere< double > Sphere
Definition: Actuator.h:40
TransferInfo TransferInfo_
Definition: ToMesh.h:197
const double radius_
Definition: ToMesh.h:194
void update_values()
Definition: ToMesh.h:181
const stk::mesh::PartVector toPartVec_
Definition: ToMesh.h:191
std::vector< const stk::mesh::FieldBase * > get_fields(const stk::mesh::MetaData &toMetaData, const PairNames &VarPairName)
Definition: ToMesh.h:57
bool operator()(const BoundingBox &a, const BoundingBox &b) const
Definition: ToMesh.h:133
std::vector< std::pair< std::string, std::string > > PairNames
Definition: ToMesh.h:55
std::vector< Entity > EntityVec
Definition: ToMesh.h:43
Definition: Algorithm.h:14
stk::search::Sphere< double > Sphere
Definition: ToMesh.h:50
stk::mesh::Field< double, stk::mesh::Cartesian > VectorFieldType
Definition: FieldTypeDef.h:24
const std::vector< const stk::mesh::FieldBase * > toFieldVec_
Definition: ToMesh.h:192
stk::mesh::BulkData & toBulkData_
Definition: ToMesh.h:188
void bounding_boxes(std::vector< BoundingBox > &v) const
Definition: ToMesh.h:143
stk::mesh::Entity Entity
Definition: ToMesh.h:42
stk::mesh::MetaData & toMetaData_
Definition: ToMesh.h:187
const stk::ParallelMachine comm_
Definition: ToMesh.h:193
stk::search::IdentProc< EntityKey, unsigned > EntityProc
Definition: ToMesh.h:46
stk::search::Point< double > Point
Definition: ToMesh.h:49
std::vector< EntityProc > EntityProcVec
Definition: ToMesh.h:47
std::vector< Bucket * > BucketVector
Definition: PromotedPartHelper.h:21
std::pair< Sphere, EntityProc > BoundingBox
Definition: ToMesh.h:51
std::set< EntityKey > EntityKeySet
Definition: ToMesh.h:45
const VectorFieldType * tocoordinates_
Definition: ToMesh.h:190
stk::mesh::EntityKey EntityKey
Definition: ToMesh.h:44
Definition: Realm.h:82
Definition: ToMesh.h:40
~ToMesh()
Definition: ToMesh.h:130
std::map< stk::mesh::EntityKey, std::vector< double > > TransferInfo
Definition: ToMesh.h:196
Realm & toRealm_
Definition: ToMesh.h:189
ToMesh(stk::mesh::MetaData &toMetaData, stk::mesh::BulkData &toBulkData, Realm &toRealm, const std::string &coordinates_name, const PairNames &VarPairName, const stk::mesh::PartVector &toPartVec, const stk::ParallelMachine comm, const double radius)
Definition: ToMesh.h:109
stk::ParallelMachine comm() const
Definition: ToMesh.h:141