dof_object.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 
71 public:
72 
73 #ifdef LIBMESH_ENABLE_AMR
74 
80 
84  void clear_old_dof_object ();
85 
89  void set_old_dof_object ();
90 
91 #endif
92 
97  void clear_dofs ();
98 
102  void invalidate_dofs (const unsigned int sys_num = libMesh::invalid_uint);
103 
107  void invalidate_id ();
108 
112  void invalidate_processor_id ();
113 
117  void invalidate ();
118 
124  unsigned int n_dofs (const unsigned int s,
125  const unsigned int var =
126  libMesh::invalid_uint) const;
127 
131  dof_id_type id () const;
132 
136  dof_id_type & set_id ();
137 
141  unique_id_type unique_id () const;
142 
147 
151  void set_id (const dof_id_type dofid)
152  { this->set_id() = dofid; }
153 
158  bool valid_id () const;
159 
164  bool valid_unique_id () const;
165 
174 
180 
184  void processor_id (const processor_id_type pid);
185 
190  bool valid_processor_id () const;
191 
196  unsigned int n_systems() const;
197 
201  void set_n_systems (const unsigned int s);
202 
206  void add_system ();
207 
212  unsigned int n_var_groups(const unsigned int s) const;
213 
218  unsigned int n_vars(const unsigned int s,
219  const unsigned int vg) const;
220 
225  unsigned int n_vars(const unsigned int s) const;
226 
234  void set_n_vars_per_group(const unsigned int s,
235  const std::vector<unsigned int> & nvpg);
236 
246  unsigned int n_comp(const unsigned int s,
247  const unsigned int var) const;
248 
258  unsigned int n_comp_group(const unsigned int s,
259  const unsigned int vg) const;
260 
265  void set_n_comp(const unsigned int s,
266  const unsigned int var,
267  const unsigned int ncomp);
268 
273  void set_n_comp_group(const unsigned int s,
274  const unsigned int vg,
275  const unsigned int ncomp);
276 
285  dof_id_type dof_number(const unsigned int s,
286  const unsigned int var,
287  const unsigned int comp) const;
288 
297  dof_id_type dof_number(const unsigned int s,
298  const unsigned int vg,
299  const unsigned int vig,
300  const unsigned int comp,
301  const unsigned int n_comp) const;
302 
308  std::pair<unsigned int, unsigned int>
309  var_to_vg_and_offset(const unsigned int s,
310  const unsigned int var) const;
311 
316  void set_dof_number(const unsigned int s,
317  const unsigned int var,
318  const unsigned int comp,
319  const dof_id_type dn);
320 
325  bool has_dofs(const unsigned int s=libMesh::invalid_uint) const;
326 
332  void set_vg_dof_base(const unsigned int s,
333  const unsigned int vg,
334  const dof_id_type db);
335 
341  dof_id_type vg_dof_base(const unsigned int s,
342  const unsigned int vg) const;
343 
347  static const dof_id_type invalid_id = static_cast<dof_id_type>(-1);
348 
352  static const unique_id_type invalid_unique_id = static_cast<unique_id_type>(-1);
353 
359 
364  unsigned int packed_indexing_size() const;
365 
370  static unsigned int unpackable_indexing_size
371  (std::vector<largest_id_type>::const_iterator begin);
372 
378  void unpack_indexing(std::vector<largest_id_type>::const_iterator begin);
379 
385  void pack_indexing(std::back_insert_iterator<std::vector<largest_id_type>> target) const;
386 
390  void debug_buffer () const;
391 
395  void print_dof_info() const;
396 
397  // Deep copy (or almost-copy) of DofObjects is now deprecated in
398  // derived classes; we keep these methods around solely for a couple
399  // tricky internal uses.
400 #ifndef LIBMESH_ENABLE_DEPRECATED
401 private:
402 #endif
403 
408  DofObject (const DofObject &);
409 
413  DofObject & operator= (const DofObject & dof_obj);
414 
415 private:
416 
421  unsigned int var_to_vg (const unsigned int s,
422  const unsigned int var) const;
423 
428  unsigned int system_var_to_vg_var (const unsigned int s,
429  const unsigned int vg,
430  const unsigned int var) const;
431 
435 #ifdef LIBMESH_ENABLE_UNIQUE_ID
437 #endif
438 
443 
454 
507  typedef std::vector<index_t> index_buffer_t;
509 
519  static const index_t ncv_magic = 256; // = 2^8, in case we want to manually bitshift
520  static const index_t ncv_magic_exp = 8; // Let's manually bitshift
521 
525  unsigned int start_idx(const unsigned int s) const;
526 
530  unsigned int end_idx(const unsigned int s) const;
531 
532  // methods only available for unit testing
533 #ifdef LIBMESH_IS_UNIT_TESTING
534 public:
535  void set_buffer (const std::vector<dof_id_type> & buf)
536  { _idx_buf = buf; }
537 #endif
538 };
539 
540 
541 
542 //------------------------------------------------------
543 // Inline functions
544 inline
546 #ifdef LIBMESH_ENABLE_AMR
547  old_dof_object(nullptr),
548 #endif
549 #ifdef LIBMESH_ENABLE_UNIQUE_ID
550  _unique_id (invalid_unique_id),
551 #endif
552  _id (invalid_id),
553  _processor_id (invalid_processor_id)
554 {
555  this->invalidate();
556 }
557 
558 
559 
560 
561 
562 inline
564 {
565  // Free all memory.
566 #ifdef LIBMESH_ENABLE_AMR
567  this->clear_old_dof_object ();
568 #endif
569  this->clear_dofs ();
570 }
571 
572 
573 
574 inline
575 void DofObject::invalidate_dofs (const unsigned int sys_num)
576 {
577  // If the user does not specify the system number...
578  if (sys_num >= this->n_systems())
579  {
580  for (unsigned int s=0; s<this->n_systems(); s++)
581  for (unsigned int vg=0; vg<this->n_var_groups(s); vg++)
582  if (this->n_comp_group(s,vg))
583  this->set_vg_dof_base(s,vg,invalid_id);
584  }
585  // ...otherwise invalidate the dofs for all systems
586  else
587  for (unsigned int vg=0; vg<this->n_var_groups(sys_num); vg++)
588  if (this->n_comp_group(sys_num,vg))
589  this->set_vg_dof_base(sys_num,vg,invalid_id);
590 }
591 
592 
593 
594 inline
596 {
597  this->set_id (invalid_id);
598 }
599 
600 
601 
602 inline
604 {
606 }
607 
608 
609 
610 inline
612 {
613  this->invalidate_dofs ();
614  this->invalidate_id ();
615  this->invalidate_processor_id ();
616 }
617 
618 
619 
620 inline
622 {
623  // vector swap trick to force deallocation
624  index_buffer_t().swap(_idx_buf);
625 
626  libmesh_assert_equal_to (this->n_systems(), 0);
627  libmesh_assert (_idx_buf.empty());
628 }
629 
630 
631 
632 inline
633 unsigned int DofObject::n_dofs (const unsigned int s,
634  const unsigned int var) const
635 {
636  libmesh_assert_less (s, this->n_systems());
637 
638  unsigned int num = 0;
639 
640  // Count all variables
641  if (var == libMesh::invalid_uint)
642  for (unsigned int v=0; v<this->n_vars(s); v++)
643  num += this->n_comp(s,v);
644 
645  // Only count specified variable
646  else
647  num = this->n_comp(s,var);
648 
649  return num;
650 }
651 
652 
653 
654 inline
656 {
657  libmesh_assert (this->valid_id());
658  return _id;
659 }
660 
661 
662 
663 inline
665 {
666  return _id;
667 }
668 
669 
670 
671 inline
673 {
674 #ifdef LIBMESH_ENABLE_UNIQUE_ID
675  libmesh_assert (this->valid_unique_id());
676  return _unique_id;
677 #else
678  return invalid_unique_id;
679 #endif
680 }
681 
682 
683 
684 inline
686 {
687 #ifdef LIBMESH_ENABLE_UNIQUE_ID
688  return _unique_id;
689 #else
690  libmesh_not_implemented();
691 #endif
692 }
693 
694 
695 
696 inline
697 bool DofObject::valid_id () const
698 {
699  return (DofObject::invalid_id != _id);
700 }
701 
702 
703 
704 inline
706 {
707 #ifdef LIBMESH_ENABLE_UNIQUE_ID
709 #else
710  return false;
711 #endif
712 }
713 
714 
715 
716 inline
718 {
719  return _processor_id;
720 }
721 
722 
723 
724 inline
726 {
727  return _processor_id;
728 }
729 
730 
731 
732 inline
734 {
735  this->processor_id() = pid;
736 }
737 
738 
739 
740 inline
742 {
744 }
745 
746 
747 
748 inline
749 unsigned int DofObject::n_systems () const
750 {
751  return _idx_buf.empty() ?
752  0 : cast_int<unsigned int>(_idx_buf[0]);
753 }
754 
755 
756 
757 inline
758 unsigned int DofObject::n_var_groups(const unsigned int s) const
759 {
760  libmesh_assert_less (s, this->n_systems());
761 
762  return (this->end_idx(s) - this->start_idx(s)) / 2;
763 }
764 
765 
766 
767 inline
768 unsigned int DofObject::n_vars(const unsigned int s,
769  const unsigned int vg) const
770 {
771  libmesh_assert_less (s, this->n_systems());
772  libmesh_assert_less (vg, this->n_var_groups(s));
773 
774  const unsigned int start_idx_sys = this->start_idx(s);
775 
776  libmesh_assert_less ((start_idx_sys + 2*vg), _idx_buf.size());
777 
778  return (cast_int<unsigned int>
779  (_idx_buf[start_idx_sys + 2*vg]) >> ncv_magic_exp);
780 }
781 
782 
783 
784 inline
785 unsigned int DofObject::n_vars(const unsigned int s) const
786 {
787  libmesh_assert_less (s, this->n_systems());
788 
789  const unsigned int nvg = this->n_var_groups(s);
790 
791  unsigned int val=0;
792 
793  for (unsigned int vg=0; vg<nvg; vg++)
794  val += this->n_vars(s,vg);
795 
796  return val;
797 }
798 
799 
800 
801 
802 inline
803 unsigned int DofObject::n_comp(const unsigned int s,
804  const unsigned int var) const
805 {
806  libmesh_assert_less (s, this->n_systems());
807  libmesh_assert_less (var, this->n_vars(s));
808 
809  return this->n_comp_group(s,this->var_to_vg(s,var));
810 }
811 
812 
813 
814 
815 inline
816 unsigned int DofObject::n_comp_group(const unsigned int s,
817  const unsigned int vg) const
818 {
819  libmesh_assert_less (s, this->n_systems());
820  libmesh_assert_less (vg, this->n_var_groups(s));
821 
822  const unsigned int
823  start_idx_sys = this->start_idx(s);
824 
825  libmesh_assert_less ((start_idx_sys + 2*vg), _idx_buf.size());
826 
827  return (_idx_buf[start_idx_sys + 2*vg] % ncv_magic);
828 }
829 
830 
831 
832 inline
833 dof_id_type DofObject::dof_number(const unsigned int s,
834  const unsigned int var,
835  const unsigned int comp) const
836 {
837  libmesh_assert_less (s, this->n_systems());
838  libmesh_assert_less (var, this->n_vars(s));
839  libmesh_assert_less (comp, this->n_comp(s,var));
840 
841  const std::pair<unsigned int, unsigned int>
842  vg_vig = this->var_to_vg_and_offset(s,var);
843 
844  const unsigned int
845  n_comp = this->n_comp_group(s,vg_vig.first);
846 
847  return this->dof_number(s, vg_vig.first, vg_vig.second,
848  comp, n_comp);
849 }
850 
851 
852 
853 inline
854 dof_id_type DofObject::dof_number(const unsigned int s,
855  const unsigned int vg,
856  const unsigned int vig,
857  const unsigned int comp,
858  const unsigned int n_comp) const
859 {
860  libmesh_assert_less (s, this->n_systems());
861  libmesh_assert_less (vg, this->n_var_groups(s));
862  libmesh_assert_less (vig, this->n_vars(s,vg));
863 
864  const unsigned int
865  start_idx_sys = this->start_idx(s);
866 
867  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
868 
869  const dof_id_type
870  base_idx = _idx_buf[start_idx_sys + 2*vg + 1];
871 
872  // if the first component is invalid, they
873  // are all invalid
874  if (base_idx == invalid_id)
875  return invalid_id;
876 
877  // otherwise the index is the first component
878  // index augmented by the component number
879  else
880  return cast_int<dof_id_type>(base_idx + vig*n_comp + comp);
881 }
882 
883 
884 
885 inline
886 std::pair<unsigned int, unsigned int>
887 DofObject::var_to_vg_and_offset(const unsigned int s,
888  const unsigned int var) const
889 {
890  std::pair<unsigned int, unsigned int> returnval(0,0);
891 
892  unsigned int & vg = returnval.first;
893  unsigned int & offset = returnval.second;
894 
895  unsigned int vg_start = 0;
896  for (; ; vg++)
897  {
898  libmesh_assert_less(vg, this->n_var_groups(s));
899 
900  const unsigned int vg_end = vg_start + this->n_vars(s,vg);
901  if (var < vg_end)
902  {
903  offset = var - vg_start;
904  return returnval;
905  }
906  vg_start = vg_end;
907  }
908 }
909 
910 
911 
912 inline
913 bool DofObject::has_dofs (const unsigned int sys) const
914 {
915  if (sys == libMesh::invalid_uint)
916  {
917  for (unsigned int s=0; s<this->n_systems(); s++)
918  if (this->n_vars(s))
919  return true;
920  }
921 
922  else
923  {
924  libmesh_assert_less (sys, this->n_systems());
925 
926  if (this->n_vars(sys))
927  return true;
928  }
929 
930  return false;
931 }
932 
933 
934 
935 inline
936 unsigned int DofObject::start_idx (const unsigned int s) const
937 {
938  libmesh_assert_less (s, this->n_systems());
939  libmesh_assert_less (s, _idx_buf.size());
940 
941  return cast_int<unsigned int>(_idx_buf[s]);
942 }
943 
944 
945 
946 inline
947 unsigned int DofObject::end_idx (const unsigned int s) const
948 {
949  libmesh_assert_less (s, this->n_systems());
950  libmesh_assert_less (s, _idx_buf.size());
951 
952  return ((s+1) == this->n_systems()) ?
953  cast_int<unsigned int>(_idx_buf.size()) :
954  cast_int<unsigned int>(_idx_buf[s+1]);
955 }
956 
957 
958 
959 inline
960 void DofObject::set_vg_dof_base(const unsigned int s,
961  const unsigned int vg,
962  const dof_id_type db)
963 {
964  libmesh_assert_less (s, this->n_systems());
965  libmesh_assert_less (vg, this->n_var_groups(s));
966 
967  const unsigned int
968  start_idx_sys = this->start_idx(s);
969 
970  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
971 
972  _idx_buf[start_idx_sys + 2*vg + 1] = db;
973 
974  libmesh_assert_equal_to (this->vg_dof_base(s,vg), db);
975 }
976 
977 
978 
979 inline
980 dof_id_type DofObject::vg_dof_base(const unsigned int s,
981  const unsigned int vg) const
982 {
983  libmesh_assert_less (s, this->n_systems());
984  libmesh_assert_less (vg, this->n_var_groups(s));
985 
986  const unsigned int
987  start_idx_sys = this->start_idx(s);
988 
989  libmesh_assert_less ((start_idx_sys + 2*vg + 1), _idx_buf.size());
990 
991  // #ifdef DEBUG
992  // std::cout << " [ ";
993  // for (std:size_t i=0; i<_idx_buf.size(); i++)
994  // std::cout << _idx_buf[i] << " ";
995  // std::cout << "]\n";
996  // #endif
997 
998  return _idx_buf[start_idx_sys + 2*vg + 1];
999 }
1000 
1001 
1002 
1003 inline
1004 unsigned int DofObject::var_to_vg (const unsigned int s,
1005  const unsigned int var) const
1006 {
1007  const unsigned int
1008  nvg = this->n_var_groups(s);
1009 
1010  for (unsigned int vg=0, vg_end=0; vg<nvg; vg++)
1011  {
1012  vg_end += this->n_vars(s,vg);
1013  if (var < vg_end) return vg;
1014  }
1015 
1016  libmesh_error_msg("Error: could not map variable " << var << " to variable group.");
1017 }
1018 
1019 
1020 
1021 inline
1022 unsigned int DofObject::system_var_to_vg_var (const unsigned int s,
1023  const unsigned int vg,
1024  const unsigned int var) const
1025 {
1026  unsigned int accumulated_sum=0;
1027 
1028  for (unsigned int vgc=0; vgc<vg; vgc++)
1029  accumulated_sum += this->n_vars(s,vgc);
1030 
1031  libmesh_assert_less_equal (accumulated_sum, var);
1032 
1033  return (var - accumulated_sum);
1034 }
1035 
1036 
1037 } // namespace libMesh
1038 
1039 
1040 #endif // #ifndef LIBMESH_DOF_OBJECT_H
void set_n_comp(const unsigned int s, const unsigned int var, const unsigned int ncomp)
Definition: dof_object.C:340
unsigned int system_var_to_vg_var(const unsigned int s, const unsigned int vg, const unsigned int var) const
Definition: dof_object.h:1022
dof_id_type vg_dof_base(const unsigned int s, const unsigned int vg) const
Definition: dof_object.h:980
unique_id_type & set_unique_id()
Definition: dof_object.h:685
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
Definition: dof_object.h:833
void set_old_dof_object()
Definition: dof_object.C:150
static unsigned int unpackable_indexing_size(std::vector< largest_id_type >::const_iterator begin)
Definition: dof_object.C:461
const unsigned int invalid_uint
Definition: libmesh.h:245
unsigned int n_comp(const unsigned int s, const unsigned int var) const
Definition: dof_object.h:803
unsigned int n_var_groups(const unsigned int s) const
Definition: dof_object.h:758
unsigned int var_to_vg(const unsigned int s, const unsigned int var) const
Definition: dof_object.h:1004
void set_id(const dof_id_type dofid)
Definition: dof_object.h:151
uint8_t processor_id_type
Definition: id_types.h:99
unique_id_type unique_id() const
Definition: dof_object.h:672
dof_id_type _id
Definition: dof_object.h:442
void set_vg_dof_base(const unsigned int s, const unsigned int vg, const dof_id_type db)
Definition: dof_object.h:960
bool has_dofs(const unsigned int s=libMesh::invalid_uint) const
Definition: dof_object.h:913
dof_id_type & set_id()
Definition: dof_object.h:664
dof_id_type index_t
Definition: dof_object.h:506
unique_id_type _unique_id
Definition: dof_object.h:436
unsigned int n_dofs(const unsigned int s, const unsigned int var=libMesh::invalid_uint) const
Definition: dof_object.h:633
unsigned int end_idx(const unsigned int s) const
Definition: dof_object.h:947
void set_buffer(const std::vector< dof_id_type > &buf)
Definition: dof_object.h:535
dof_id_type id() const
Definition: dof_object.h:655
DofObject & operator=(const DofObject &dof_obj)
Definition: dof_object.C:90
static const unique_id_type invalid_unique_id
Definition: dof_object.h:352
static const processor_id_type invalid_processor_id
Definition: dof_object.h:358
void set_dof_number(const unsigned int s, const unsigned int var, const unsigned int comp, const dof_id_type dn)
Definition: dof_object.C:409
unsigned int n_vars(const unsigned int s, const unsigned int vg) const
Definition: dof_object.h:768
unsigned int n_systems() const
Definition: dof_object.h:749
processor_id_type _processor_id
Definition: libmesh.C:236
void print_dof_info() const
Definition: dof_object.C:548
bool valid_processor_id() const
Definition: dof_object.h:741
void clear_old_dof_object()
Definition: dof_object.C:142
static const dof_id_type invalid_id
Definition: dof_object.h:347
bool valid_unique_id() const
Definition: dof_object.h:705
void invalidate_processor_id()
Definition: dof_object.h:603
std::vector< index_t > index_buffer_t
Definition: dof_object.h:507
void set_n_comp_group(const unsigned int s, const unsigned int vg, const unsigned int ncomp)
Definition: dof_object.C:352
void set_n_systems(const unsigned int s)
Definition: dof_object.C:165
void set_n_vars_per_group(const unsigned int s, const std::vector< unsigned int > &nvpg)
Definition: dof_object.C:227
DofObject * old_dof_object
Definition: dof_object.h:79
bool valid_id() const
Definition: dof_object.h:697
unsigned int start_idx(const unsigned int s) const
Definition: dof_object.h:936
unsigned int packed_indexing_size() const
Definition: dof_object.C:445
void pack_indexing(std::back_insert_iterator< std::vector< largest_id_type >> target) const
Definition: dof_object.C:520
void invalidate_dofs(const unsigned int sys_num=libMesh::invalid_uint)
Definition: dof_object.h:575
void unpack_indexing(std::vector< largest_id_type >::const_iterator begin)
Definition: dof_object.C:482
std::pair< unsigned int, unsigned int > var_to_vg_and_offset(const unsigned int s, const unsigned int var) const
Definition: dof_object.h:887
index_buffer_t _idx_buf
Definition: dof_object.h:508
void debug_buffer() const
Definition: dof_object.C:538
unsigned int n_comp_group(const unsigned int s, const unsigned int vg) const
Definition: dof_object.h:816
processor_id_type processor_id() const
Definition: dof_object.h:717
uint8_t unique_id_type
Definition: id_types.h:79
processor_id_type _processor_id
Definition: dof_object.h:453
static const index_t ncv_magic_exp
Definition: dof_object.h:520
uint8_t dof_id_type
Definition: id_types.h:64
static const index_t ncv_magic
Definition: dof_object.h:519