SheafSystem  0.0.0.0
block_connectivity.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class block_connectivity
19 
20 #include "SheafSystem/assert_contract.h"
21 #include "SheafSystem/block_connectivity.h"
22 #include "SheafSystem/std_limits.h"
23 #include "SheafSystem/std_set.h"
24 
25 using namespace std;
26 using namespace fiber_bundle;
27 
28 // ===========================================================
29 // BLOCK_CONNECTIVITY FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  if(_delete_node_ids)
42  {
43  delete [] _node_ids;
44  }
45 
46  // Postconditions:
47 }
48 
51 element_ct() const
52 {
53  return _element_ct;
54 }
55 
58 node_ct() const
59 {
60  return _node_ct;
61 }
62 
66 {
67  return _node_ids;
68 }
69 
72 node_ids() const
73 {
74  return _node_ids;
75 }
76 
77 bool
80 {
81  return _delete_node_ids;
82 }
83 
86 node_id_ct() const
87 {
88  return _node_id_ct;
89 }
90 
94 {
95  return _nodes_per_element;
96 }
97 
100 start_id() const
101 {
102  return _start_id;
103 }
104 
105 void
107 to_stream(std::ostream& os) const
108 {
109  size_type npe = nodes_per_element();
110  size_type nid_ct = node_id_ct();
111  const pod_index_type* nids = node_ids();
112 
113  os << endl;
114  for(size_type i=0; i<nid_ct; i+=npe)
115  {
116  for(size_type j=0; j<npe; ++j)
117  {
118  os << " " << nids[i+j];
119  }
120 
121  os << endl;
122  }
123 }
124 
128 {
129  // Preconditions:
130 
131  // Body:
132 
133  _element_ct = xother._element_ct;
134  _node_ct = xother._node_ct;
135  _node_id_ct = xother._node_id_ct;
136 
137  if(_delete_node_ids)
138  {
139  delete [] _node_ids;
140  }
141 
142  _node_ids = new pod_index_type[xother._node_id_ct];
143 
144  for(pod_index_type i=0; i<_node_id_ct; ++i)
145  {
146  _node_ids[i] = xother._node_ids[i];
147  }
148 
149  _delete_node_ids = true;
150 
151  _nodes_per_element = xother._nodes_per_element;
152  _start_id = xother._start_id;
153 
154  // Postconditions:
155 
156  ensure(invariant());
157  ensure(*this == xother);
158  ensure(node_ids() != xother.node_ids());
159  ensure(delete_node_ids());
160 
161 
162  // Exit:
163 
164  return *this;
165 }
166 
167 
168 
169 bool
171 operator==(const block_connectivity& xother) const
172 {
173  // Preconditions:
174 
175  // Body:
176 
177  bool result;
178 
179  result = (_element_ct == xother._element_ct);
180  result = result && (_node_ct == xother._node_ct);
181  result = result && (_node_id_ct == xother._node_id_ct);
182  for(pod_index_type i=0; result && (i<_node_id_ct); ++i)
183  {
184  result = result && (_node_ids[i] == xother._node_ids[i]);
185  }
186 
187  result = result && (_nodes_per_element == xother._nodes_per_element);
188  result = result && (_start_id == xother._start_id);
189 
190  // Postconditions:
191 
192  // Exit
193 
194  return result;
195 }
196 
197 // PROTECTED MEMBER FUNCTIONS
198 
199 
202 {
203 
204  // Preconditions:
205 
206  // Body:
207 
208  _element_ct = 0;
209  _node_ct = 0;
210  _node_ids = 0;
211  _delete_node_ids = false;
212  _node_id_ct = 0;
213  _nodes_per_element = 0;
214  _start_id = 0;
215 
216  // Postconditions:
217 
218  ensure(element_ct() == 0);
219  ensure(node_ct() == 0);
220  ensure(node_ids() == 0);
221  ensure(!delete_node_ids());
222  ensure(node_id_ct() == 0);
223  ensure(nodes_per_element() == 0);
224  ensure(start_id() == 0);
225 
226 
227 
228 }
229 
232 {
233 
234  // Preconditions:
235 
236  // Body:
237 
238  _element_ct = 0;
239  _node_ct = 0;
240  _node_ids = 0;
241  _delete_node_ids = false;
242  _node_id_ct = 0;
243  _nodes_per_element = 0;
244  _start_id = 0;
245 
246  (*this) == xother;
247 
248  // Postconditions:
249 
250  ensure(invariant());
251  ensure((*this) == xother);
252 
253  // Exit:
254 
255  return;
256 }
257 
260  size_type xnode_id_ct,
261  size_type xnodes_per_element,
262  size_type xnode_ct)
263 {
264 
265  // Preconditions:
266 
267  require(xnode_ids != 0);
268  require(xnode_id_ct > 0);
269  require(xnodes_per_element > 0);
270 
271  // Body:
272 
273  _element_ct = xnode_id_ct/xnodes_per_element;
274 
276  if(xnode_ct > 0)
277  {
278  for(size_type i=0; i<xnode_id_ct; ++i)
279  {
280  pod_index_type lnode_id = xnode_ids[i];
281  lstart_id = (lnode_id < lstart_id) ? lnode_id : lstart_id;
282  }
283  _node_ct = xnode_ct;
284  }
285  else
286  {
287  set<pod_index_type> lnodes;
288  for(size_type i=0; i<xnode_id_ct; ++i)
289  {
290  pod_index_type lnode_id = xnode_ids[i];
291  lstart_id = (lnode_id < lstart_id) ? lnode_id : lstart_id;
292  lnodes.insert(lnode_id);
293  }
294  _node_ct = lnodes.size();
295  }
296 
297  _node_ids = const_cast<pod_index_type*>(xnode_ids);
298  _delete_node_ids = false;
299  _node_id_ct = xnode_id_ct;
300  _nodes_per_element = xnodes_per_element;
301  _start_id = lstart_id;
302 
303  // Postconditions:
304 
305  ensure(element_ct() == xnode_id_ct/xnodes_per_element);
306  ensure(xnode_ct > 0 ? node_ct() == xnode_ct : node_ct() > 0);
307  ensure(node_ids() == xnode_ids);
308  ensure(!delete_node_ids());
309  ensure(node_id_ct() == xnode_id_ct);
310  ensure(nodes_per_element() == xnodes_per_element);
311 
312 }
313 
316 {
317 
318  // Preconditions:
319 
320  // Body:
321 
322  _element_ct = 0;
323  _node_ct = 0;
324  _node_ids = 0;
325  _delete_node_ids = false;
326  _node_id_ct = 0;
327  _nodes_per_element = 0;
328  _start_id = xstart_id;
329 
330  // Postconditions:
331 
332  ensure(node_ids() == 0);
333  ensure(!delete_node_ids());
334  ensure(node_id_ct() == 0);
335  ensure(start_id() == xstart_id);
336 
337 }
338 
339 // ===========================================================
340 // ANY FACET
341 // ===========================================================
342 
343 bool
345 invariant() const
346 {
347  bool result = true;
348 
349  // Preconditions:
350 
351  // Body:
352 
353  if(invariant_check())
354  {
355  // Prevent recursive calls to invariant
356 
357  disable_invariant_check();
358 
359  invariance(block_relation::invariant());
360 
361  // Can't delete node ids if they haven't been allocated.
362 
363  invariance(delete_node_ids() ? node_ids() != 0 : true);
364 
365  // Finished, turn invariant checking back on.
366 
367  enable_invariant_check();
368  }
369 
370  // Postconditions:
371 
372  // Exit
373 
374  return result;
375 }
376 
377 bool
379 is_ancestor_of(const any* other) const
380 {
381 
382  // Preconditions:
383 
384  require(other != 0);
385 
386  // Body:
387 
388  // True if other conforms to this
389 
390  bool result = dynamic_cast<const block_connectivity*>(other) != 0;
391 
392  // Postconditions:
393 
394  return result;
395 }
396 
397 
398 // ===========================================================
399 // NON-MEMBER FUNCTIONS
400 // ===========================================================
401 
402 
403 std::ostream&
405 operator << (std::ostream& xos, const fiber_bundle::block_connectivity& xconn)
406 {
407 
408  xconn.to_stream(xos);
409 
410  return xos;
411 }
pod_index_type * _node_ids
The nodal connectivity array.
size_type _nodes_per_element
The number of nodes per element.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
size_type node_id_ct() const
The number of entries in node_ids().
STL namespace.
Zone to node connectivity relation for a block of zones of a given type.
pod_index_type * node_ids()
The nodal connectivity array.
Abstract base class with useful features for all objects.
Definition: any.h:39
size_type nodes_per_element() const
The number of nodes per element.
size_type _node_id_ct
the number of entyries in _node_ids.
virtual bool invariant() const
Class invariant.
block_connectivity()
Default constructor.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
SHEAF_DLL_SPEC void max(const vd &x0, vd_value_type &xresult, bool xauto_access)
Maximum component of x0, pre-allocated version.
Definition: vd.cc:2097
size_type _element_ct
The number of elements.
virtual bool operator==(const block_connectivity &xother) const
Equality operator.
size_type node_ct() const
The number of distinct nodes.
block_connectivity & operator=(const block_connectivity &xother)
Assignment operator.
size_type element_ct() const
The number of elements.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
size_type _node_ct
The number of distinct nodes.
pod_index_type _start_id
The id given to the first node id generated. Mostly only useful for creating 1 (vs 0) based node numb...
pod_index_type start_id() const
The id given to the first node id generated.
virtual ~block_connectivity()
Destructor.
virtual void to_stream(std::ostream &xos) const
Print to stream xos.
Namespace for the fiber_bundles component of the sheaf system.
bool delete_node_ids() const
True if destructor of this should delete _node_ids.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const binary_index &xbi)
Insert binary_index& xbi into ostream& os.
Definition: binary_index.cc:35