libMesh
Public Member Functions | Private Types | Private Attributes | Friends | List of all members
libMesh::CouplingMatrix Class Reference

This class defines a coupling matrix. More...

#include <coupling_matrix.h>

Public Member Functions

 CouplingMatrix (const unsigned int n=0)
 Constructor. More...
 
bool operator() (const unsigned int i, const unsigned int j) const
 
CouplingAccessor operator() (const unsigned int i, const unsigned int j)
 
unsigned int size () const
 
void resize (const unsigned int n)
 Resizes the matrix and initializes all entries to be 0. More...
 
void clear ()
 Clears the matrix. More...
 
bool empty () const
 
CouplingMatrixoperator&= (const CouplingMatrix &other)
 

Private Types

typedef std::pair< std::size_t, std::size_t > range_type
 Coupling matrices are typically either full or very sparse, and all values are only zero or one. More...
 
typedef std::vector< range_typerc_type
 

Private Attributes

rc_type _ranges
 
unsigned int _size
 The size of the matrix. More...
 

Friends

class ConstCouplingAccessor
 
class CouplingAccessor
 
class ConstCouplingRow
 
class ConstCouplingRowConstIterator
 

Detailed Description

This class defines a coupling matrix.

A coupling matrix is simply a matrix of ones and zeros describing how different components in a system couple with each other. A coupling matrix is necessarily square but not necessarily symmetric.

Author
Benjamin S. Kirk
Date
2002

Defines the coupling between variables of a System.

Definition at line 54 of file coupling_matrix.h.

Member Typedef Documentation

◆ range_type

typedef std::pair<std::size_t, std::size_t> libMesh::CouplingMatrix::range_type
private

Coupling matrices are typically either full or very sparse, and all values are only zero or one.

We store non-zeros as ranges: the first entry of each range pair is the location of the first non-zero, and the second is the location of the last subsequent non-zero (not the next subsequent zero; we drop empty ranges).

We store locations (i,j) as long integers i*_size+j

Definition at line 119 of file coupling_matrix.h.

◆ rc_type

typedef std::vector<range_type> libMesh::CouplingMatrix::rc_type
private

Definition at line 120 of file coupling_matrix.h.

Constructor & Destructor Documentation

◆ CouplingMatrix()

libMesh::CouplingMatrix::CouplingMatrix ( const unsigned int  n = 0)
inlineexplicit

Constructor.

Definition at line 570 of file coupling_matrix.h.

570  :
571  _ranges(), _size(n)
572 {
573  this->resize(n);
574 }

References resize().

Member Function Documentation

◆ clear()

void libMesh::CouplingMatrix::clear ( )
inline

Clears the matrix.

Definition at line 623 of file coupling_matrix.h.

624 {
625  this->resize(0);
626 }

References resize().

◆ empty()

bool libMesh::CouplingMatrix::empty ( ) const
inline
Returns
true if the matrix is empty.

Definition at line 631 of file coupling_matrix.h.

632 {
633  return (_size == 0);
634 }

References _size.

Referenced by libMesh::DofMap::reinit(), and libMesh::DefaultCoupling::set_dof_coupling().

◆ operator&=()

CouplingMatrix & libMesh::CouplingMatrix::operator&= ( const CouplingMatrix other)

Definition at line 24 of file coupling_matrix.C.

25 {
26  const std::size_t max_size = std::numeric_limits<std::size_t>::max();
27 
28  rc_type::iterator start_range = this->_ranges.begin();
29 
30  rc_type::const_iterator other_range = other._ranges.begin();
31  const rc_type::const_iterator other_end = other._ranges.end();
32 
33  for (; other_range != other_end; ++other_range)
34  {
35  std::size_t other_range_start = other_range->first;
36  std::size_t other_range_end = other_range->second;
37 
38  // Find our range that might contain the start of the other
39  // range.
40  // lower_bound isn't *quite* what we want.
41  // Because the other._ranges is sorted, we can contract this
42  // search as we proceed, beginning with lb rather than at
43  // begin() every time.
44  rc_type::iterator lb =
45  std::upper_bound (start_range, this->_ranges.end(),
46  std::make_pair(other_range_start, max_size));
47  if (lb!=start_range)
48  --lb;
49  else
50  lb=this->_ranges.end();
51 
52  start_range = lb;
53 
54  // If no range might contain the start of the new range then
55  // we can just break out of here and start appending any
56  // remaining ranges.
57  if (lb == this->_ranges.end())
58  break;
59 
60  // We did find a range which might contain the start of the new
61  // range.
62  const std::size_t lastloc = lb->second;
63  libmesh_assert_less_equal(lb->first, lastloc);
64  libmesh_assert_less_equal(lb->first, other_range_start);
65 
66 #ifdef DEBUG
67  {
68  CouplingMatrix::rc_type::const_iterator next = lb;
69  next++;
70  if (next != this->_ranges.end())
71  {
72  // Ranges should be sorted and should not touch
73  libmesh_assert_greater(next->first, lastloc+1);
74  }
75  }
76 #endif
77 
78  CouplingMatrix::rc_type::iterator next = lb;
79  next++;
80 
81  // We might need to extend this range or add a new range.
82  // Merge contiguous ranges
83  if (other_range_start <= lastloc)
84  lb->second = other_range_end;
85  // Or insert a new range. This invalidates existing iterators,
86  // but that's okay; the only iterator we need is for the newly
87  // inserted range.
88  else
89  start_range = lb = this->_ranges.insert
90  (next, std::make_pair(other_range_start, other_range_end));
91 
92  // At this point we have a range lb that may potentially overlap
93  // subsequent existing ranges, in which case we need to merge
94  // some.
95 
96  // First expand our range as necessary while finding ranges
97  // which will be redundant later
98  for (const std::size_t nextloc =
99  (next == this->_ranges.end()) ?
100  std::numeric_limits<std::size_t>::max() : next->first;
101  nextloc <= lb->second; ++next)
102  {
103  // Ranges should be sorted and should not have been touching
104  // initially
105  libmesh_assert_greater(nextloc, lastloc+1);
106 
107  lb->second = std::max(lb->second, next->second);
108  }
109 
110  CouplingMatrix::rc_type::iterator oldnext = lb;
111  oldnext++;
112 
113  // Finally remove the redundant ranges
114  this->_ranges.erase(oldnext, next);
115  }
116 
117  // If we broke out early but we still have more other_ranges then
118  // we can safely just append them to our ranges.
119  for (; other_range != other_end; ++other_range)
120  this->_ranges.push_back(*other_range);
121 
122  // Behave like a standard modification operator
123  return *this;
124 }

References _ranges, and libMesh::MeshTools::Subdivision::next.

◆ operator()() [1/2]

CouplingAccessor libMesh::CouplingMatrix::operator() ( const unsigned int  i,
const unsigned int  j 
)
inline
Returns
The (i,j) entry of the matrix as a smart-reference.

Definition at line 594 of file coupling_matrix.h.

596 {
597  const std::size_t location = std::size_t(i)*_size + j;
598 
599  return CouplingAccessor(location, *this);
600 }

References _size, and CouplingAccessor.

◆ operator()() [2/2]

bool libMesh::CouplingMatrix::operator() ( const unsigned int  i,
const unsigned int  j 
) const
inline
Returns
The (i,j) entry of the matrix.

Definition at line 579 of file coupling_matrix.h.

581 {
582  libmesh_assert_less (i, _size);
583  libmesh_assert_less (j, _size);
584 
585  const std::size_t location = std::size_t(i)*_size + j;
586 
587  return bool(ConstCouplingAccessor(location, *this));
588 }

References _size, and ConstCouplingAccessor.

◆ resize()

void libMesh::CouplingMatrix::resize ( const unsigned int  n)
inline

Resizes the matrix and initializes all entries to be 0.

Definition at line 613 of file coupling_matrix.h.

614 {
615  _size = n;
616 
617  _ranges.clear();
618 }

References _ranges, and _size.

Referenced by clear(), CouplingMatrix(), main(), and CouplingMatrixTest::testSimpleAPI().

◆ size()

unsigned int libMesh::CouplingMatrix::size ( ) const
inline
Returns
The size of the matrix, i.e. N for an NxN matrix.

Definition at line 605 of file coupling_matrix.h.

606 {
607  return _size;
608 }

References _size.

Referenced by libMesh::ConstCouplingAccessor::ConstCouplingAccessor(), libMesh::ConstCouplingRow::ConstCouplingRow(), libMesh::SparsityPattern::Build::operator()(), and libMesh::ConstCouplingRowConstIterator::operator*().

Friends And Related Function Documentation

◆ ConstCouplingAccessor

friend class ConstCouplingAccessor
friend

Definition at line 103 of file coupling_matrix.h.

Referenced by operator()().

◆ ConstCouplingRow

friend class ConstCouplingRow
friend

Definition at line 105 of file coupling_matrix.h.

◆ ConstCouplingRowConstIterator

friend class ConstCouplingRowConstIterator
friend

Definition at line 106 of file coupling_matrix.h.

◆ CouplingAccessor

friend class CouplingAccessor
friend

Definition at line 104 of file coupling_matrix.h.

Referenced by operator()().

Member Data Documentation

◆ _ranges

rc_type libMesh::CouplingMatrix::_ranges
private

◆ _size

unsigned int libMesh::CouplingMatrix::_size
private

The size of the matrix.

Definition at line 126 of file coupling_matrix.h.

Referenced by empty(), operator()(), resize(), and size().


The documentation for this class was generated from the following files:
libMesh::MeshTools::Subdivision::next
static const unsigned int next[3]
A lookup table for the increment modulo 3 operation, for iterating through the three nodes per elemen...
Definition: mesh_subdivision_support.h:102
libMesh::CouplingMatrix::CouplingAccessor
friend class CouplingAccessor
Definition: coupling_matrix.h:104
libMesh::CouplingMatrix::resize
void resize(const unsigned int n)
Resizes the matrix and initializes all entries to be 0.
Definition: coupling_matrix.h:613
libMesh::CouplingMatrix::_ranges
rc_type _ranges
Definition: coupling_matrix.h:121
libMesh::CouplingMatrix::_size
unsigned int _size
The size of the matrix.
Definition: coupling_matrix.h:126
libMesh::CouplingMatrix::ConstCouplingAccessor
friend class ConstCouplingAccessor
Definition: coupling_matrix.h:103