Sierra Toolkit  Version of the Day
Selector.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #include <stdexcept>
11 #include <sstream>
12 #include <iostream>
13 
14 #include <stk_mesh/base/Selector.hpp>
15 #include <stk_mesh/base/Bucket.hpp>
16 #include <stk_mesh/base/MetaData.hpp>
17 #include <stk_mesh/base/BulkData.hpp>
18 #include <stk_mesh/base/Types.hpp>
19 
20 namespace stk_classic {
21 namespace mesh {
22 
24  : m_mesh_meta_data(0), m_op()
25 {
26  compoundAll();
27 }
28 
29 
31  : m_mesh_meta_data( & MetaData::get(p) ) , m_op()
32 {
33  m_op.push_back( OpType( p.mesh_meta_data_ordinal() , 0 , 0 ) );
34 }
35 
37 {
38  m_op.insert( m_op.begin(), OpType( 0, 0, m_op.size()+1 ) );
39 }
40 
41 
43 {
44  bool singlePart = (m_op.size() == 1);
45  bool fullCompoundPart = (m_op[0].m_count == m_op.size());
46 
47  if ( !(singlePart || fullCompoundPart) ) {
48  // Turn into a compound
49  compoundAll();
50  }
51  // Flip the bit
52  m_op[0].m_unary ^= 1;
53  return *this;
54 }
55 
56 bool Selector::operator()( const Part & part ) const
57 {
58  unsigned part_ord = part.mesh_meta_data_ordinal();
59  std::pair<const unsigned *, const unsigned *> part_ords(&part_ord, &part_ord+1);
60  return apply(part_ords, PartOrdLess());
61 }
62 
63 bool Selector::operator()( const Bucket & candidate ) const
64 { return apply( m_op.begin() , m_op.end() , candidate.superset_part_ordinals(), PartOrdLess() ); }
65 
66 bool Selector::operator()( const Bucket * candidate ) const{
67  return operator()(*candidate);
68 }
69 
70 bool Selector::operator()( const Entity & candidate ) const
71 {
72  const Bucket & b = candidate.bucket();
73  return this->operator()(b);
74 }
75 
77 {
78  if (m_mesh_meta_data == 0) {
79  m_mesh_meta_data = B.m_mesh_meta_data;
80  }
81 
82  m_op.insert( m_op.end() , B.m_op.begin() , B.m_op.end() );
83  return *this;
84 }
85 
86 
88 {
89  if (m_mesh_meta_data == 0) {
90  m_mesh_meta_data = B.m_mesh_meta_data;
91  }
92 
93  Selector notB = B; notB.complement();
94 
95  const size_t original_size = m_op.size();
96 
97  if ( 1 == original_size &&
98  m_op.front().m_count == 1 &&
99  m_op.front().m_unary == 0 ) {
100  // this == empty ; therefore,
101  // this UNION B == B
102  m_op = B.m_op ;
103  }
104  else if ( m_op.front().m_count == original_size &&
105  m_op.front().m_unary != 0 ) {
106  // This is a full-compound complement.
107  // Simply add notB to the end and increase the size of the compound
108 
109  // this == ! A ; therefore,
110  // this UNION B == ! ( ! ( ! A ) & ! B )
111  // this UNION B == ! ( A & ! B )
112 
113  m_op.insert(
114  m_op.end(),
115  notB.m_op.begin(),
116  notB.m_op.end() );
117 
118  m_op.front().m_count = m_op.size();
119  }
120  else {
121  // this UNION B == ! ( ! this & ! B )
122 
123  this->complement(); // ( ! (this) )
124 
125  const unsigned finalSize = 1 + m_op.size() + notB.m_op.size();
126 
127  m_op.insert(
128  m_op.end(),
129  notB.m_op.begin(),
130  notB.m_op.end() ); // ! ( ! (this) & !B )
131  m_op.insert(
132  m_op.begin(),
133  OpType( 0 , 1 , finalSize ) ); // ! ( ! (this) & ? )
134  }
135 
136  return *this;
137 }
138 
139 Selector operator & ( const Part & A , const Part & B )
140 {
141  Selector S( A );
142  S &= Selector( B );
143  return S;
144 }
145 
146 
147 Selector operator & ( const Part & A , const Selector & B )
148 {
149  Selector S( A );
150  S &= B;
151  return S;
152 }
153 
154 Selector operator & ( const Selector & A, const Part & B )
155 {
156  Selector S( A );
157  S &= Selector(B);
158  return S;
159 }
160 
161 Selector operator & ( const Selector & A, const Selector & B )
162 {
163  Selector S( A );
164  S &= Selector(B);
165  return S;
166 }
167 
168 Selector operator | ( const Part & A , const Part & B )
169 {
170  Selector S( A );
171  S |= Selector( B );
172  return S;
173 }
174 
175 
176 Selector operator | ( const Part & A , const Selector & B )
177 {
178  Selector S( A );
179  S |= B;
180  return S;
181 }
182 
183 Selector operator | ( const Selector & A, const Part & B )
184 {
185  Selector S( A );
186  S |= Selector(B);
187  return S;
188 }
189 
190 Selector operator | ( const Selector & A, const Selector & B )
191 {
192  Selector S( A );
193  S |= Selector(B);
194  return S;
195 }
196 
197 
198 
199 
200 Selector operator ! ( const Part & A )
201 {
202  Selector S(A);
203  return S.complement();
204 }
205 
206 
207 std::ostream & operator<<( std::ostream & out, const Selector & selector)
208 {
209  out << selector.printExpression(selector.m_op.begin(),selector.m_op.end());
210  return out;
211 }
212 
213 std::string Selector::printExpression(
214  const std::vector<OpType>::const_iterator start,
215  const std::vector<OpType>::const_iterator finish
216  ) const
217 {
218  std::ostringstream outS;
219 
220  std::vector<OpType>::const_iterator start_it = start;
221  std::vector<OpType>::const_iterator finish_it = finish;
222 
223  const OpType & op = *start_it;
224  if (op.m_count > 0) { // Compound
225  if (op.m_unary != 0) { // Complement
226  outS << "!";
227  }
228  outS << "(";
229  if (op.m_count == 1) {
230  outS << ")";
231  }
232  else {
233  finish_it = start_it;
234  for (int i=0 ; i < op.m_count ; ++i) {
235  ++finish_it;
236  }
237  ++start_it;
238  outS << printExpression(start_it,finish_it) << ")";
239  start_it = finish_it;
240  --start_it; // back up one
241  }
242  }
243  else { // Part
244  if (m_mesh_meta_data != NULL) {
245  Part & part = m_mesh_meta_data->get_part(op.m_part_id);
246  if (op.m_unary != 0) { // Complement
247  outS << "!";
248  }
249  outS << part.name();
250  }
251  }
252  ++start_it;
253  if (start_it != finish) {
254  outS << " AND " << printExpression(start_it,finish);
255  }
256  return outS.str();
257 }
258 
259 
260 Selector selectUnion( const PartVector& union_part_vector )
261 {
262  Selector selector;
263  if (union_part_vector.size() > 0) {
264  selector = *union_part_vector[0];
265  for (unsigned i = 1 ; i < union_part_vector.size() ; ++i) {
266  selector |= *union_part_vector[i];
267  }
268  }
269  return selector;
270 }
271 
272 
273 Selector selectIntersection( const PartVector& intersection_part_vector )
274 {
275  Selector selector;
276  if (intersection_part_vector.size() > 0) {
277  selector = *intersection_part_vector[0];
278  for (unsigned i = 1 ; i < intersection_part_vector.size() ; ++i) {
279  selector &= *intersection_part_vector[i];
280  }
281  }
282  return selector;
283 }
284 
285 Selector selectField( const FieldBase& field )
286 {
287  Selector selector;
288  const MetaData& meta = MetaData::get(field);
289  const FieldRestrictionVector& rvec = field.restrictions();
290 
291  for(size_t i=0; i<rvec.size(); ++i) {
292  selector |= meta.get_part(rvec[i].part_ordinal());
293  }
294 
295  const FieldRestrictionVector& sel_rvec = field.selector_restrictions();
296  for(size_t i=0; i<sel_rvec.size(); ++i) {
297  selector |= sel_rvec[i].selector();
298  }
299 
300  return selector;
301 }
302 
303 
304 
305 } // namespace mesh
306 } // namespace stk_classic
307 
308 
Field base class with an anonymous data type and anonymous multi-dimension.
Definition: FieldBase.hpp:53
The manager of an integrated collection of parts and fields.
Definition: MetaData.hpp:56
Selector & operator&=(const Selector &selector)
Intersection: this = this INTERSECT ( expression )
Selector & complement()
Complement: this = !(this) Postcondition: this is a compound expression.
Definition: Selector.cpp:42
Bucket & bucket() const
The bucket which holds this mesh entity&#39;s field data.
Definition: Entity.hpp:141
This is a class for selecting buckets based on a set of meshparts and set logic.
Definition: Selector.hpp:112
Part * get_part(const std::string &p_name, const char *required_by=NULL) const
Get an existing part by its application-defined text name.
Definition: MetaData.cpp:185
std::pair< const unsigned *, const unsigned * > superset_part_ordinals() const
Definition: Bucket.hpp:188
Selector()
A default Selector selects nothing.
Definition: Selector.cpp:23
An application-defined subset of a problem domain.
Definition: Part.hpp:49
std::ostream & operator<<(std::ostream &s, const Bucket &k)
Print the part names for which this bucket is a subset.
Definition: Bucket.cpp:239
const std::string & name() const
Application-defined text name of this part.
Definition: Part.hpp:67
unsigned mesh_meta_data_ordinal() const
Internally generated ordinal of this part that is unique within the owning meta data manager...
Definition: Part.hpp:72
Selector & operator|=(const Selector &selector)
Union: this = this UNION ( expression )
Definition: Selector.cpp:87
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
Sierra Toolkit.
bool apply(const std::pair< PartIterator, PartIterator > &part_range, Compare comp) const
Is the intersection of the &#39;part_ords&#39; parts a member of the set defined by the selector expression...
Definition: Selector.hpp:165
void compoundAll()
Turn the entire expression into a compound.
Definition: Selector.cpp:36
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94
bool operator()(const Part &part) const
Is this part a member of the set defined by the selector expression.
Definition: Selector.cpp:56