dof_object.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_DOF_OBJECT_H
21 #define LIBMESH_DOF_OBJECT_H
22 
23 // Local includes
24 #include "libmesh/id_types.h"
25 #include "libmesh/libmesh_config.h"
26 #include "libmesh/libmesh_common.h"
27 #include "libmesh/libmesh.h" // libMesh::invalid_uint
29 
30 // C++ includes
31 #include <cstddef>
32 #include <vector>
33 
34 namespace libMesh
35 {
36 
37 // Forward declarations
38 class DofObject;
39 
40 
51 class DofObject : public ReferenceCountedObject<DofObject>
52 {
53 #ifdef LIBMESH_IS_UNIT_TESTING
54 public:
55 #else
56 protected:
57 #endif
58 
63  DofObject ();
64 
69  ~DofObject ();
70 
74  DofObject (const DofObject&);
75 
79  DofObject& operator= (const DofObject& dof_obj);
80 
81 public:
82 
83 #ifdef LIBMESH_ENABLE_AMR
84 
90 
94  void clear_old_dof_object ();
95 
99  void set_old_dof_object ();
100 
101 #endif
102 
107  void clear_dofs ();
108 
112  void invalidate_dofs (const unsigned int sys_num = libMesh::invalid_uint);
113 
117  void invalidate_id ();
118 
122  void invalidate_processor_id ();
123 
127  void invalidate ();
128 
134  unsigned int n_dofs (const unsigned int s,
135  const unsigned int var =
136  libMesh::invalid_uint) const;
137 
141  dof_id_type id () const;
142 
146  dof_id_type & set_id ();
147 
151  unique_id_type unique_id () const;
152 
157 
161  void set_id (const dof_id_type dofid)
162  { this->set_id() = dofid; }
163 
168  bool valid_id () const;
169 
174  bool valid_unique_id () const;
175 
184 
190 
194  void processor_id (const processor_id_type pid);
195 
200  bool valid_processor_id () const;
201 
206  unsigned int n_systems() const;
207 
211  void set_n_systems (const unsigned int s);
212 
216  void add_system ();
217 
222  unsigned int n_var_groups(const unsigned int s) const;
223 
228  unsigned int n_vars(const unsigned int s,
229  const unsigned int vg) const;
230 
235  unsigned int n_vars(const unsigned int s) const;
236 
244  void set_n_vars_per_group(const unsigned int s,
245  const std::vector<unsigned int> &nvpg);
246 
256  unsigned int n_comp(const unsigned int s,
257  const unsigned int var) const;
258 
268  unsigned int n_comp_group(const unsigned int s,
269  const unsigned int vg) const;
270 
275  void set_n_comp(const unsigned int s,
276  const unsigned int var,
277  const unsigned int ncomp);
278 
283  void set_n_comp_group(const unsigned int s,
284  const unsigned int vg,
285  const unsigned int ncomp);
286 
296  dof_id_type dof_number(const unsigned int s,
297  const unsigned int var,
298  const unsigned int comp) const;
299 
304  void set_dof_number(const unsigned int s,
305  const unsigned int var,
306  const unsigned int comp,
307  const dof_id_type dn);
308 
313  bool has_dofs(const unsigned int s=libMesh::invalid_uint) const;
314 
320  void set_vg_dof_base(const unsigned int s,
321  const unsigned int vg,
322  const dof_id_type db);
323 
329  dof_id_type vg_dof_base(const unsigned int s,
330  const unsigned int vg) const;
331 
335  static const dof_id_type invalid_id = static_cast<dof_id_type>(-1);
336 
340  static const unique_id_type invalid_unique_id = static_cast<unique_id_type>(-1);
341 
347 
352  unsigned int packed_indexing_size() const;
353 
358  static unsigned int unpackable_indexing_size
359  (std::vector<largest_id_type>::const_iterator begin);
360 
366  void unpack_indexing(std::vector<largest_id_type>::const_iterator begin);
367 
373  void pack_indexing(std::back_insert_iterator<std::vector<largest_id_type> > target) const;
374 
378  void debug_buffer () const;
379 
380 private:
381 
386  unsigned int var_to_vg (const unsigned int s,
387  const unsigned int var) const;
388 
393  unsigned int system_var_to_vg_var (const unsigned int s,
394  const unsigned int vg,
395  const unsigned int var) const;
396 
401 
402 
406 #ifdef LIBMESH_ENABLE_UNIQUE_ID
408 #endif
409 
420 
464  typedef std::vector<index_t> index_buffer_t;
466 
476  static const index_t ncv_magic = 256; // = 2^8, in case we want to manually bitshift
477 
481  unsigned int start_idx(const unsigned int s) const;
482 
486  unsigned int end_idx(const unsigned int s) const;
487 
488  // methods only available for unit testing
489 #ifdef LIBMESH_IS_UNIT_TESTING
490 public:
491  void set_buffer (const std::vector<dof_id_type> &buf)
492  { _idx_buf = buf; }
493 #endif
494 };
495 
496 
497 
498 //------------------------------------------------------
499 // Inline functions
500 inline
502 #ifdef LIBMESH_ENABLE_AMR
503  old_dof_object(NULL),
504 #endif
505  _id (invalid_id),
506 #ifdef LIBMESH_ENABLE_UNIQUE_ID
507  _unique_id (invalid_unique_id),
508 #endif
509  _processor_id (invalid_processor_id)
510 {
511  this->invalidate();
512 }
513 
514 
515 
516 
517 
518 inline
520 {
521  // Free all memory.
522 #ifdef LIBMESH_ENABLE_AMR
523  this->clear_old_dof_object ();
524 #endif
525  this->clear_dofs ();
526 }
527 
528 
529 
530 inline
531 void DofObject::invalidate_dofs (const unsigned int sys_num)
532 {
533  // If the user does not specify the system number...
534  if (sys_num >= this->n_systems())
535  {
536  for (unsigned int s=0; s<this->n_systems(); s++)
537  for (unsigned int vg=0; vg<this->n_var_groups(s); vg++)
538  if (this->n_comp_group(s,vg))
539  this->set_vg_dof_base(s,vg,invalid_id);
540  }
541  // ...otherwise invalidate the dofs for all systems
542  else
543  for (unsigned int vg=0; vg<this->n_var_groups(sys_num); vg++)
544  if (this->n_comp_group(sys_num,vg))
545  this->set_vg_dof_base(sys_num,vg,invalid_id);
546 }
547 
548 
549 
550 inline
552 {
553  this->set_id (invalid_id);
554 }
555 
556 
557 
558 inline
560 {
562 }
563 
564 
565 
566 inline
568 {
569  this->invalidate_dofs ();
570  this->invalidate_id ();
571  this->invalidate_processor_id ();
572 }
573 
574 
575 
576 inline
578 {
579  // vector swap trick to force deallocation
580  index_buffer_t().swap(_idx_buf);
581 
582  libmesh_assert_equal_to (this->n_systems(), 0);
583  libmesh_assert (_idx_buf.empty());
584 }
585 
586 
587 
588 inline
589 unsigned int DofObject::n_dofs (const unsigned int s,
590  const unsigned int var) const
591 {
592  libmesh_assert_less (s, this->n_systems());
593 
594  unsigned int num = 0;
595 
596  // Count all variables
597  if (var == libMesh::invalid_uint)
598  for (unsigned int v=0; v<this->n_vars(s); v++)
599  num += this->n_comp(s,v);
600 
601  // Only count specified variable
602  else
603  num = this->n_comp(s,var);
604 
605  return num;
606 }
607 
608 
609 
610 inline
612 {
613  libmesh_assert (this->valid_id());
614  return _id;
615 }
616 
617 
618 
619 inline
621 {
622  return _id;
623 }
624 
625 
626 
627 inline
629 {
630 #ifdef LIBMESH_ENABLE_UNIQUE_ID
632  return _unique_id;
633 #else
634  return invalid_unique_id;
635 #endif
636 }
637 
638 
639 
640 inline
642 {
643 #ifdef LIBMESH_ENABLE_UNIQUE_ID
644  return _unique_id;
645 #else
646  libmesh_error();
647 #endif
648 }
649 
650 
651 
652 inline
653 bool DofObject::valid_id () const
654 {
655  return (DofObject::invalid_id != _id);
656 }
657 
658 
659 
660 inline
662 {
663 #ifdef LIBMESH_ENABLE_UNIQUE_ID
665 #else
666  return false;
667 #endif
668 }
669 
670 
671 
672 inline
674 {
675  return _processor_id;
676 }
677 
678 
679 
680 inline
682 {
683  return _processor_id;
684 }
685 
686 
687 
688 inline
690 {
691  this->processor_id() = pid;
692 }
693 
694 
695 
696 inline
698 {
700 }
701 
702 
703 
704 inline
705 unsigned int DofObject::n_systems () const
706 {
707  return _idx_buf.empty() ?
708  0 : libmesh_cast_int<unsigned int>(_idx_buf[0]);
709 }
710 
711 
712 
713 inline
714 unsigned int DofObject::n_var_groups(const unsigned int s) const
715 {
716  libmesh_assert_less (s, this->n_systems());
717 
718  return (this->end_idx(s) - this->start_idx(s)) / 2;
719 }
720 
721 
722 
723 inline
724 unsigned int DofObject::n_vars(const unsigned int s,
725  const unsigned int vg) const
726 {
727  libmesh_assert_less (s, this->n_systems());
728  libmesh_assert_less (vg, this->n_var_groups(s));
729 
730  const unsigned int start_idx_sys = this->start_idx(s);
731 
732  libmesh_assert_less ((start_idx_sys + 2*vg), _idx_buf.size());
733 
734  return (libmesh_cast_int<unsigned int>
735  (_idx_buf[start_idx_sys + 2*vg]) / ncv_magic);
736 }
737 
738 
739 
740 inline
741 unsigned int DofObject::n_vars(const unsigned int s) const
742 {
743  libmesh_assert_less (s, this->n_systems());
744 
745  const unsigned int nvg = this->n_var_groups(s);
746 
747  unsigned int val=0;
748 
749  for (unsigned int vg=0; vg<nvg; vg++)
750  val += this->n_vars(s,vg);
751 
752  return val;
753 }
754 
755 
756 
757 
758 inline
759 unsigned int DofObject::n_comp(const unsigned int s,
760  const unsigned int var) const
761 {
762  libmesh_assert_less (s, this->n_systems());
763  libmesh_assert_less (var, this->n_vars(s));
764 
765  return this->n_comp_group(s,this->var_to_vg(s,var));
766 }
767 
768 
769 
770 
771 inline
772 unsigned int DofObject::n_comp_group(const unsigned int s,
773  const unsigned int vg) const
774 {
775  libmesh_assert_less (s, this->n_systems());
776  libmesh_assert_less (vg, this->n_var_groups(s));
777 
778  const unsigned int
779  start_idx_sys = this->start_idx(s);
780 
781  libmesh_assert_less ((start_idx_sys + 2*vg), _idx_buf.size());
782 
783  return (_idx_buf[start_idx_sys + 2*vg] % ncv_magic);
784 }
785 
786 
787 
788 inline
789 dof_id_type DofObject::dof_number(const unsigned int s,
790  const unsigned int var,
791  const unsigned int comp) const
792 {
793  libmesh_assert_less (s, this->n_systems());
794  libmesh_assert_less (var, this->n_vars(s));
795  libmesh_assert_less (comp, this->n_comp(s,var));
796 
797  const unsigned int
798  vg = this->var_to_vg(s,var),
799  start_idx_sys = this->start_idx(s);
800 
801  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
802 
803  const dof_id_type
804  base_idx = _idx_buf[start_idx_sys + 2*vg + 1];
805 
806  // if the first component is invalid, they
807  // are all invalid
808  if (base_idx == invalid_id)
809  return invalid_id;
810 
811  // otherwise the index is the first component
812  // index augemented by the component number
813  else
814  {
815  const unsigned int
816  ncg = this->n_comp_group(s,vg),
817  vig = this->system_var_to_vg_var(s,vg,var);
818 
819  // std::cout << "base_idx, var, vg, vig, ncg, comp="
820  // << base_idx << " "
821  // << var << " "
822  // << vg << " "
823  // << vig << " "
824  // << ncg << " "
825  // << comp << '\n';
826 
827  return libmesh_cast_int<dof_id_type>(base_idx + vig*ncg + comp);
828  }
829 }
830 
831 
832 
833 inline
834 bool DofObject::has_dofs (const unsigned int sys) const
835 {
836  if (sys == libMesh::invalid_uint)
837  {
838  for (unsigned int s=0; s<this->n_systems(); s++)
839  if (this->n_vars(s))
840  return true;
841  }
842 
843  else
844  {
845  libmesh_assert_less (sys, this->n_systems());
846 
847  if (this->n_vars(sys))
848  return true;
849  }
850 
851  return false;
852 }
853 
854 
855 
856 inline
857 unsigned int DofObject::start_idx (const unsigned int s) const
858 {
859  libmesh_assert_less (s, this->n_systems());
860  libmesh_assert_less (s, _idx_buf.size());
861 
862  return libmesh_cast_int<unsigned int>(_idx_buf[s]);
863 }
864 
865 
866 
867 inline
868 unsigned int DofObject::end_idx (const unsigned int s) const
869 {
870  libmesh_assert_less (s, this->n_systems());
871  libmesh_assert_less (s, _idx_buf.size());
872 
873  return ((s+1) == this->n_systems()) ?
874  libmesh_cast_int<unsigned int>(_idx_buf.size()) :
875  libmesh_cast_int<unsigned int>(_idx_buf[s+1]);
876 }
877 
878 
879 
880 inline
881 void DofObject::set_vg_dof_base(const unsigned int s,
882  const unsigned int vg,
883  const dof_id_type db)
884 {
885  libmesh_assert_less (s, this->n_systems());
886  libmesh_assert_less (vg, this->n_var_groups(s));
887 
888  const unsigned int
889  start_idx_sys = this->start_idx(s);
890 
891  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
892 
893  _idx_buf[start_idx_sys + 2*vg + 1] = db;
894 
895  libmesh_assert_equal_to (this->vg_dof_base(s,vg), db);
896 }
897 
898 
899 
900 inline
901 dof_id_type DofObject::vg_dof_base(const unsigned int s,
902  const unsigned int vg) const
903 {
904  libmesh_assert_less (s, this->n_systems());
905  libmesh_assert_less (vg, this->n_var_groups(s));
906 
907  const unsigned int
908  start_idx_sys = this->start_idx(s);
909 
910  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
911 
912 // #ifdef DEBUG
913 // std::cout << " [ ";
914 // for (unsigned int i=0; i<_idx_buf.size(); i++)
915 // std::cout << _idx_buf[i] << " ";
916 // std::cout << "]\n";
917 // #endif
918 
919  return _idx_buf[start_idx_sys + 2*vg + 1];
920 }
921 
922 
923 
924 inline
925 unsigned int DofObject::var_to_vg (const unsigned int s,
926  const unsigned int var) const
927 {
928  const unsigned int
929  nvg = this->n_var_groups(s);
930 
931  for (unsigned int vg=0, vg_end=0; vg<nvg; vg++)
932  {
933  vg_end += this->n_vars(s,vg);
934  if (var < vg_end) return vg;
935  }
936 
937  // we should never get here
938  libmesh_error();
939  return 0;
940 }
941 
942 
943 
944 inline
945 unsigned int DofObject::system_var_to_vg_var (const unsigned int s,
946  const unsigned int vg,
947  const unsigned int var) const
948 {
949  unsigned int accumulated_sum=0;
950 
951  for (unsigned int vgc=0; vgc<vg; vgc++)
952  accumulated_sum += this->n_vars(s,vgc);
953 
954  libmesh_assert_less_equal (accumulated_sum, var);
955 
956  return (var - accumulated_sum);
957 }
958 
959 
960 } // namespace libMesh
961 
962 
963 #endif // #ifndef LIBMESH_DOF_OBJECT_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:04 UTC

Hosted By:
SourceForge.net Logo