SheafSystem  0.0.0.0
section_eval_iterator.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 section_eval_iterator
19 
20 #include "SheafSystem/assert_contract.h"
21 #include "SheafSystem/block.h"
22 #include "SheafSystem/eval_family.h"
23 #include "SheafSystem/poset_state_handle.h"
24 #include "SheafSystem/postorder_iterator.h"
25 #include "SheafSystem/section_eval_iterator.h"
26 #include "SheafSystem/section_evaluator.h"
27 #include "SheafSystem/section_space_schema_member.h"
28 #include "SheafSystem/section_space_schema_poset.h"
29 
30 using namespace std;
31 using namespace fiber_bundle; // Workaround for MS C++ bug.
32 
36 {
37 
38  // Preconditions:
39 
40  require(is_ancestor_of(&xother));
41  require(xother.components_done());
42  require(xother.evaluators_done());
43 
44  // Body:
45 
46  section_eval_iterator& lother =
47  const_cast<section_eval_iterator&>(xother);
48 
49  if(is_initialized())
50  {
51  _anchor->detach_from_state();
52  delete _anchor;
53  _anchor = 0;
54  }
55 
56  if(lother._anchor != 0)
57  {
58  _anchor = lother._anchor->clone();
59  _anchor->attach_to_state(lother._anchor);
60  }
61 
62  _fiber_schema_itr = lother._fiber_schema_itr;
63 
64  _evaluation_itr = lother._evaluation_itr;
65 
66  // Postconditions:
67 
68  ensure(invariant());
69  ensure(is_initialized() == xother.is_initialized());
70  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
71 
72  // Exit
73 
74  return *this;
75 }
76 
77 
80 {
81 
82  // Preconditions:
83 
84  // Body:
85 
86  if(_anchor != 0)
87  {
88  _anchor->detach_from_state();
89  delete _anchor;
90  }
91 
92  // Postconditions:
93 
94  // Exit:
95 
96  return;
97 }
98 
99 
100 bool
102 is_ancestor_of(const any* xother) const
103 {
104  // Preconditions:
105 
106  // Body:
107 
108  bool result = dynamic_cast<const section_eval_iterator*>(xother) != 0;
109 
110  // Postconditions:
111 
112  // Exit
113 
114  return result;
115 }
116 
117 
118 bool
120 invariant() const
121 {
122  bool result = true;
123 
124  // Preconditions:
125 
126  // Body:
127 
128  invariance(any::invariant());
129 
130  if(invariant_check())
131  {
132  disable_invariant_check();
133 
134  invariance( is_initialized() == (_anchor != 0) );
135  invariance( is_initialized() == (_fiber_schema_itr.is_initialized()) );
136  invariance( is_initialized() == (_evaluation_itr.is_initialized()) );
137 
138  // Finished, turn invariant checking back on.
139 
140  enable_invariant_check();
141  }
142 
143  // Postconditions:
144 
145  // Exit
146 
147  return result;
148 }
149 
150 
151 
152 // ITERATOR FACET
153 
154 bool
157 {
158  // Preconditions:
159 
160  // Body:
161 
162  bool result = _anchor != 0;
163 
164  // Postconditions:
165 
166  // Exit
167 
168  return result;
169 }
170 
174 {
175  // Preconditions:
176 
177  require(is_initialized());
178 
179  // Body:
180 
181  section_space_schema_member& result = *_anchor;
182 
183  // Postconditions:
184 
185  // Exit
186 
187  return result;
188 }
189 
192 anchor() const
193 {
194  // Preconditions:
195 
196  require(is_initialized());
197 
198  // Body:
199 
200  section_space_schema_member& result = *_anchor;
201 
202  // Postconditions:
203 
204  // Exit
205 
206  return result;
207 }
208 
209 bool
212 {
213  // Preconditions:
214 
215  require(is_initialized());
216 
217  // Body:
218 
219  // Always true in this class;
220  // intended to be redefined in descendants.
221 
222  bool result = true;
223 
224  // Postconditions:
225 
226  // Exit
227 
228  return result;
229 }
230 
231 
232 void
234 reset(bool xreset_markers)
235 {
236  // Preconditions:
237 
238  require(is_initialized());
239  require(anchor().state_is_read_accessible());
240 
241  // Body:
242 
243  _evaluation_itr.reset(xreset_markers);
244  _fiber_schema_itr.reset(xreset_markers);
245 
246  // Postconditions:
247 
248  ensure(invariant());
249  ensure(is_initialized());
250 
251  // Exit
252 
253  return;
254 }
255 
256 void
258 reset(pod_index_type xanchor_index, bool xreset_markers)
259 {
260  // Preconditions:
261 
262  require(is_initialized());
263  require(anchor().state_is_read_accessible());
264  require(anchor().host()->contains_member(xanchor_index));
265 
266  // Body:
267 
268  // Reset the anchor.
269 
270  _anchor->attach_to_state(xanchor_index);
271 
272  // Reset the evaluation iterator.
273 
274  _evaluation_itr.put_schema_anchor(xanchor_index);
275  _evaluation_itr.reset(xreset_markers);
276 
277  // Anchor of fiber_schema_iterator is an ordinary poset, so we need
278  // the id of the fiber schema component of anchor.
279 
280  _fiber_schema_itr.put_anchor(anchor().fiber_schema_id());
281  _fiber_schema_itr.reset(xreset_markers);
282 
283  // Postconditions:
284 
285  ensure(invariant());
286  ensure(is_initialized());
287  ensure(anchor().index() == xanchor_index);
288 
289  // Exit
290 
291  return;
292 }
293 
294 void
296 reset(const scoped_index& xanchor_index, bool xreset_markers)
297 {
298  // Preconditions:
299 
300  require(is_initialized());
301  require(anchor().state_is_read_accessible());
302  require(anchor().host()->contains_member(xanchor_index));
303 
304  // Body:
305 
306  reset(xanchor_index.hub_pod(), xreset_markers);
307 
308  // Postconditions:
309 
310  ensure(invariant());
311  ensure(is_initialized());
312  ensure(anchor().index() ==~ xanchor_index);
313 
314  // Exit
315 
316  return;
317 }
318 
319 void
322 {
323 
324  // Preconditions:
325 
326  require(is_initialized() ? anchor_is_ancestor_of(xanchor) : true);
327  require(xanchor.state_is_read_accessible());
328  require(xanchor.host()->is_schematized(false));
329 
330  // Body:
331 
332  reset_anchor(xanchor);
333 
334  _evaluation_itr.put_schema_anchor(xanchor);
335  _evaluation_itr.reset();
336 
337  // Reset the fiber schema component iterator.
338  // Set the filter to the dof subposet of top so that we can reset the anchor
339  // without changing the filter. See reset(int xanchor_index, bool xreset_markers).
340  // Also, we don't currently support versions of fiber schema.
341 
342  string ldof_sp_name = schema_poset_member::dof_subposet_name("top", false);
343 
344  _fiber_schema_itr.put_anchor(&(xanchor.fiber_schema()));
345  _fiber_schema_itr.put_filter(ldof_sp_name);
346  _fiber_schema_itr.reset();
347 
348  // Postconditions:
349 
350  ensure(invariant());
351  ensure(is_initialized());
352  ensure(anchor().is_same_state(&xanchor));
353  ensure(anchor().is_same_type(&xanchor));
354 
355  // Exit:
356 
357  return;
358 }
359 
360 int
362 ct(bool xreset)
363 {
364  // Preconditions:
365 
366  require(is_initialized());
367  require(xreset ? anchor().state_is_read_accessible(): true);
368 
369  // Body:
370 
371  int result = _evaluation_itr.ct(xreset)*_fiber_schema_itr.ct(xreset);
372 
373  // Postconditions:
374 
375  ensure(result >= 0);
376  ensure(evaluators_done());
377  ensure(components_done());
378 
379  // Exit
380 
381  return result;
382 }
383 
384 
385 bool
388 {
389  // Preconditions:
390 
391  require(is_initialized());
392  require(anchor().state_is_read_accessible());
393  require(anchor().host()->contains_member(xhub_id));
394 
395  // Body:
396 
397  bool result =
398  _evaluation_itr.has_visited(anchor().host()->get_base_space_id_from_index(xhub_id)) &&
399  _fiber_schema_itr.has_visited(anchor().host()->get_fiber_schema_id_from_index(xhub_id));
400 
401  // Postconditions:
402 
403  // Exit:
404 
405  return result;
406 }
407 
408 bool
410 has_visited(const scoped_index& xid) const
411 {
412  // Preconditions:
413 
414  require(is_initialized());
415  require(anchor().state_is_read_accessible());
416  require(anchor().host()->contains_member(xid));
417 
418  // Body:
419 
420  return has_visited(xid.hub_pod());
421 }
422 
423 bool
426 {
427  // Preconditions:
428 
429  require(is_initialized());
430  require(anchor().state_is_read_accessible());
431  require(xmbr.is_attached());
432  require(anchor().host()->is_same_state(xmbr.host()));
433 
434  // Body:
435 
436  bool result = _evaluation_itr.has_visited(xmbr.base_space_id()) &&
437  _fiber_schema_itr.has_visited(xmbr.fiber_schema_id());
438 
439  // Postconditions:
440 
441  // Exit:
442 
443  return result;
444 }
445 
446 void
448 put_has_visited(pod_index_type xhub_id, bool xvalue)
449 {
450 
451  // Preconditions:
452 
453  require(is_initialized());
454  require(anchor().state_is_read_accessible());
455  require(anchor().host()->contains_member(xhub_id));
456 
457  // Body:
458 
459  _evaluation_itr.put_has_visited(anchor().host()->get_base_space_id_from_index(xhub_id), xvalue);
460  _fiber_schema_itr.put_has_visited(anchor().host()->get_fiber_schema_id_from_index(xhub_id), xvalue);
461 
462  // Postconditions:
463 
464  ensure(has_visited(xhub_id) == xvalue);
465 
466  // Exit:
467 
468  return;
469 }
470 
471 void
473 put_has_visited(const scoped_index& xid, bool xvalue)
474 {
475 
476  // Preconditions:
477 
478  require(is_initialized());
479  require(anchor().state_is_read_accessible());
480  require(anchor().host()->contains_member(xid));
481 
482  // Body:
483 
484  put_has_visited(xid.hub_pod(), xvalue);
485 
486  // Postconditions:
487 
488  ensure(has_visited(xid) == xvalue);
489 
490  // Exit:
491 
492  return;
493 }
494 
495 const sheaf::scoped_index&
498 {
499  // Preconditions:
500 
501  require(is_initialized());
502 
503  require(!evaluators_done());
504 
505  // Body:
506 
507  const scoped_index& result = _evaluation_itr.index();
508 
509  // Postconditions:
510 
511  // Exit
512 
513  return result;
514 }
515 
519 {
520  // Preconditions:
521 
522  require(is_initialized());
523 
524  require(!evaluators_done());
525 
526  // Body:
527 
528  section_evaluator& result = _evaluation_itr.evaluator();
529 
530  // Postconditions:
531 
532  // Exit
533 
534  return result;
535 }
536 
537 
540 evaluator() const
541 {
542  // Preconditions:
543 
544  require(is_initialized());
545 
546  require(!evaluators_done());
547 
548  // Body:
549 
550  const section_evaluator& result = _evaluation_itr.evaluator();
551 
552  // Postconditions:
553 
554  // Exit
555 
556  return result;
557 }
558 
559 bool
562 {
563  bool result;
564 
565  // Preconditions:
566 
567  require(is_initialized());
568 
569  // Body:
570 
571  result = _evaluation_itr.is_done();
572 
573  // Postconditions:
574 
575  // Exit
576 
577  return result;
578 }
579 
580 void
583 {
584 
585  // Preconditions:
586 
587  require(is_initialized());
588  require(!evaluators_done());
589 
590  // Body:
591 
592  _evaluation_itr.next(false);
593 
594  // Postconditions:
595 
596  ensure(invariant());
597 
598  // Exit
599 
600  return;
601 }
602 
603 
604 bool
607 {
608  bool result;
609 
610  // Preconditions:
611 
612  require(is_initialized());
613 
614  // Body:
615 
616  result = _fiber_schema_itr.is_done();
617 
618  // Postconditions:
619 
620  // Exit
621 
622  return result;
623 }
624 
625 void
628 {
629 
630  // Preconditions:
631 
632  require(is_initialized());
633  require(!components_done());
634 
635  // Body:
636 
637  _fiber_schema_itr.next();
638 
639  // if(!_fiber_schema_itr.is_done())
640  // _fiber_id = _fiber_schema_itr.index();
641  // else
642  // _fiber_id.invalidate();
643 
644  // Postconditions:
645 
646  ensure(invariant());
647 
648  // Exit
649 
650  return;
651 }
652 
656 {
657 
658  // Preconditions:
659 
660  require(!evaluators_done());
661 
662  // Body:
663 
664  block<scoped_index>& result = _evaluation_itr.discretization_members();
665 
666  // Postconditions:
667 
668  // Exit
669 
670  return result;
671 }
672 
676 {
677 
678  // Preconditions:
679 
680  require(!evaluators_done());
681 
682  // Body:
683 
684  const block<scoped_index>& result = _evaluation_itr.discretization_members();
685 
686  // Postconditions:
687 
688  // Exit
689 
690  return result;
691 }
692 
693 const sheaf::scoped_index&
695 fiber_id() const
696 {
697 
698  // Preconditions:
699 
700  // require(!evaluators_done());
701  require(!components_done());
702 
703  // Body:
704 
705  const scoped_index& result = _fiber_schema_itr.index();
706 
707  // Postconditions:
708 
709  // Exit
710 
711  return result;
712 }
713 
714 
715 
716 // PROTECTED MEMBER FUNCTIONS
717 
720  _anchor(0)
721 {
722 
723  // Preconditions:
724 
725  // Body:
726 
727  // Postconditions:
728 
729  ensure(invariant());
730  ensure(!is_initialized());
731 
732  // Exit:
733 
734  return;
735 }
736 
737 void
740 {
741  // Preconditions:
742 
743  require(!evaluators_done());
744  require(xsec.state_is_read_accessible());
745 
746  // Body:
747 
749 
750  size_type ldisc_mbr_ct = ldisc_mbrs.ct();
751  int ldf = xsec.schema().df();
752  size_type ldof_ct = ldf*ldisc_mbr_ct;
753 
754  size_type old_xdofs_ct = xdofs.ct();
755  size_type lnew_xdofs_ct = old_xdofs_ct + ldof_ct;
756  xdofs.reserve(lnew_xdofs_ct);
757  xdofs.set_ct(lnew_xdofs_ct);
758 
759  // Gather the dofs by discretization member.
760 
761  size_type ldof_index = old_xdofs_ct;
762  for(int i=0; i<ldisc_mbr_ct; ++i)
763  {
764  xsec.get_fiber(ldisc_mbrs[i], &xdofs[ldof_index],
765  ldf*sizeof(sec_vd::dof_type), false);
766  ldof_index += ldf;
767  }
768 
769  // Postconditions:
770 
771  ensure(xdofs.ct() ==
772  (old_xdofs_ct + xsec.schema().df()*discretization_members().ct()));
773 
774  // Exit:
775 
776  return;
777 }
778 
779 
780 void
783 {
784  // Preconditions:
785 
786  require(!evaluators_done());
787  require(xsec.state_is_read_write_accessible());
788  require(xdofs.ub() >= xhub_id + xsec.schema().df()*discretization_members().ct());
789 
790 
791  // Body:
792 
794 
795  size_type ldisc_mbr_ct = ldisc_mbrs.ct();
796  int ldf = xsec.schema().df();
797  size_type ldof_ct = ldf*ldisc_mbr_ct;
798 
799  size_type ldof_index = xhub_id;
800 
801  // Gather the dofs by discretization member.
802 
803  for(int i=0; i<ldisc_mbr_ct; ++i)
804  {
805  xsec.put_fiber(ldisc_mbrs[i], &xdofs[ldof_index],
806  ldf*sizeof(sec_vd::dof_type), false);
807  ldof_index += ldf;
808  }
809 
810  // Postconditions:
811 
812 
813  // Exit:
814 
815  return;
816 }
817 
818 
819 // ===========================================================
820 // PROTECTED MEMBER FUNCTIONS
821 // ===========================================================
822 
825 {
826 
827  // Preconditions:
828 
829  // Body:
830 
831  _fiber_schema_itr = reinterpret_cast<const section_eval_iterator&>(xother)._fiber_schema_itr;
832 
833  // Postconditions:
834 
835  ensure(invariant());
836  ensure(is_initialized() == xother.is_initialized());
837  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
838 
839  // Exit
840 
841  return;
842 }
843 
844 void
847 {
848  // Preconditions:
849 
850  require(is_initialized() ? anchor_is_ancestor_of(xanchor) : true);
851  require(xanchor.state_is_read_accessible());
852 
853  // Body:
854 
855  if(_anchor == 0)
856  {
857  // Create the anchor handle
858 
859  _anchor = xanchor.clone();
860  }
861 
862  // Attach the handle.
863 
864  _anchor->attach_to_state(&xanchor);
865 
866  // Postconditions:
867 
868  ensure(anchor().is_attached());
869  ensure(anchor().is_same_state(&xanchor));
870  ensure(anchor().is_same_type(&xanchor));
871 
872  // Exit:
873 
874  return;
875 }
876 
879  _anchor(0)
880 {
881 
882  // Preconditions:
883 
884  require(xanchor.state_is_read_accessible());
885  require(xanchor.host()->is_schematized(false));
886 
887  // Body:
888 
893 
894  _anchor = xanchor.clone();
895 
896  reset(xanchor);
897 
898  // Postconditions:
899 
900  ensure(invariant());
901  ensure(is_initialized());
902  ensure(anchor().is_same_state(&xanchor));
903  ensure(anchor().is_same_type(&xanchor));
904 
905  // Exit:
906 
907  return;
908 }
909 
910 void
913 {
914  // Preconditions:
915 
916  require(is_initialized());
917 
918  // Body:
919 
921 
922  // Postconditions:
923 
924  ensure(invariant());
925 
926  // Exit
927 }
928 
931 clone() const
932 {
933 
934  // Preconditions:
935 
936  // Body:
937 
938  not_implemented();
939 
940  // Initialize result anyway, to suppress compiler warnings.
941 
942  section_eval_iterator* result = 0;
943 
944  // Postconditions:
945 
946  ensure(result != 0);
947  ensure(is_same_type(result));
948 
949  // Exit
950 
951  return result;
952 }
bool components_done() const
True if iteration over fiber components is finished.
virtual int ct(bool xreset=false)
The number of members of the iteration set, from the current member to the end, inclusive. If xreset, reset before computing the count.
section_space_schema_poset * host() const
The poset which this is a handle to a component of.
bool evaluators_done() const
True if iteration over the evaluation set is finished.
index_type ub() const
The upper bound on the storage array. The number of items current allocated in the storage array...
size_type ct() const
The number of items currently in use.
section_space_schema_member & anchor()
The schema member whose downset is being iterated over; the top member of the domain of iteration (mu...
schema_poset_member & fiber_schema()
The fiber schema component of this (mutable version).
virtual void next_component()
Makes the next fiber component the current fiber component.
const scoped_index & fiber_id() const
The id of the fiber schema member associated with the current component.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
void get_fiber(pod_index_type xdisc_id, vd_lite &xfiber) const
Sets xfiber to the fiber referred to by discretization id xdisc_id.
Definition: sec_vd.cc:1087
virtual bool is_schematized(bool xauto_access) const
True if this poset has been prepared for use as a schema, that is, if the top member has been schemat...
section_space_schema_member * _anchor
The schema member whose downset is being iterated over; the top member of the domain of iteration...
STL namespace.
void reserve(index_type xub)
Makes ub() at least xub; if new storage is allocated, it is uninitialized.
virtual bool anchor_is_ancestor_of(const section_space_schema_member &xmbr) const
True if xmbr conforms to the type of item of this.
virtual void reset_components()
Restarts iteration at first component of current evaluator.
virtual bool has_visited(pod_index_type xhub_id) const
True if this has already visited hub id xhub_id.
Abstract base class with useful features for all objects.
Definition: any.h:39
pod_index_type fiber_schema_id() const
The member id of the fiber schema component of this.
bool state_is_read_write_accessible() const
True if this is attached and if the state is accessible for read and write or access control is disab...
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
virtual bool is_attached() const
True if this handle is attached to a non-void state.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
section_evaluator & evaluator()
The evaluator (local section) associated with the current evaluation member (mutable version)...
bool invariant() const
The class invariant.
virtual section_eval_iterator * clone() const
Make a new instance of the same type as this.
An abstract local section evaluator; a map from {local coordinates x dofs} to section value...
A section of a fiber bundle with a d-dimensional vector space fiber.
Definition: sec_vd.h:54
Iterates over the evaluation members of a section space schema anchor; gathers the dof ids for each e...
void scatter_dofs(sec_vd &xsec, const block< sec_vd::dof_type > &xdofs, size_type xindex)
Scatters the dofs for the current evalaution member from the location xindex and following of xdofs i...
block< scoped_index > & discretization_members()
The ids of the discretization members in the down set of the current evaluation member (mutable versi...
virtual void reset(bool xreset_markers=true)
Restarts the iteration over the down set of anchor().
void reset_anchor(const section_space_schema_member &xanchor)
Creates anchor if needed and attaches it to xanchor.
virtual void next_evaluator()
Makes the next member of the evaluation set the current evaluation member.
void put_fiber(pod_index_type xdisc_id, const vd_lite &xfiber)
Sets the fiber referred to by discretization id xdisc_id to xfiber.
Definition: sec_vd.cc:1144
postorder_iterator _fiber_schema_itr
The iterator for the fiber schema component.
virtual section_space_schema_member & schema()
The restricted schema for this (mutable version).
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
const scoped_index & evaluator_id() const
The id of the current evaluation member.
sec_vd_dof_type dof_type
The type of degree of freedom.
Definition: sec_vd.h:91
virtual section_eval_iterator & operator=(const section_eval_iterator &xother)
Assignment operator.
A client handle for a poset member which has been prepared for use as a schema for a section space...
int df() const
The dimension of the fiber space component.
section_eval_iterator()
Default constructor; creates an unattached iterator.
virtual bool is_ancestor_of(const any *xother) const
True if other conforms to this.
void gather_dofs(const sec_vd &xsec, block< sec_vd::dof_type > &xdofs)
Gathers the dofs for the current evalaution member from section xsec and appends them to the back of ...
virtual void reset(bool xreset_markers=RESET)
Restarts the iteration over the down set of anchor() If xreset_markers, set !has_visited for all memb...
virtual void put_has_visited(pod_index_type xhub_id, bool xvalue)
Set the visited marker for hub id xhub_id to xvalue. Intended for use reseting iterator without havin...
bool is_initialized() const
True if this has been initialized for iteration with respect to a specific anchor.
Namespace for the fiber_bundles component of the sheaf system.
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
virtual void attach_to_state(pod_index_type xbase_space_id, pod_index_type xfiber_schema_id)=0
Attach to the state in host() with component ids xbase_id and xfiber_schema_id.
section_space_schema_member * clone(bool xnew_state, bool xauto_access=true) const
Make a new handle instance of current. Attach the new instance to a new state if xnew_state is true...
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710
pod_index_type base_space_id() const
The member id of the base space component of this.