libMesh::CouplingMatrix Class Reference

Defines the coupling between variables of a System. More...

#include <coupling_matrix.h>

Public Member Functions

 CouplingMatrix (const unsigned int n=0)
 
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)
 
void clear ()
 
bool empty () const
 
CouplingMatrixoperator&= (const CouplingMatrix &other)
 

Private Types

typedef std::pair< std::size_t, std::size_t > range_type
 
typedef std::vector< range_typerc_type
 

Private Attributes

rc_type _ranges
 
unsigned int _size
 

Friends

class ConstCouplingAccessor
 
class CouplingAccessor
 
class ConstCouplingRow
 
class ConstCouplingRowConstIterator
 

Detailed Description

Defines the coupling between variables of a System.

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

Definition at line 54 of file coupling_matrix.h.

Member Typedef Documentation

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.

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

Definition at line 120 of file coupling_matrix.h.

Constructor & Destructor Documentation

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

Constructor.

Definition at line 555 of file coupling_matrix.h.

References resize().

555  :
556  _ranges(), _size(n)
557 {
558  this->resize(n);
559 }
void resize(const unsigned int n)

Member Function Documentation

void libMesh::CouplingMatrix::clear ( )
inline

Clears the matrix.

Definition at line 608 of file coupling_matrix.h.

References resize().

609 {
610  this->resize(0);
611 }
void resize(const unsigned int n)
bool libMesh::CouplingMatrix::empty ( ) const
inline
Returns
true if the matrix is empty.

Definition at line 616 of file coupling_matrix.h.

References _size.

Referenced by libMesh::DefaultCoupling::set_dof_coupling().

617 {
618  return (_size == 0);
619 }
CouplingMatrix & libMesh::CouplingMatrix::operator&= ( const CouplingMatrix other)

Definition at line 24 of file coupling_matrix.C.

References _ranges, std::max(), and libMesh::MeshTools::Subdivision::next.

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()) ?
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 }
long double max(long double a, double b)
static const unsigned int next[3]
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 564 of file coupling_matrix.h.

References _size, and ConstCouplingAccessor.

566 {
567  libmesh_assert_less (i, _size);
568  libmesh_assert_less (j, _size);
569 
570  const std::size_t location = std::size_t(i)*_size + j;
571 
572  return bool(ConstCouplingAccessor(location, *this));
573 }
friend class ConstCouplingAccessor
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 579 of file coupling_matrix.h.

References _size, and CouplingAccessor.

581 {
582  const std::size_t location = std::size_t(i)*_size + j;
583 
584  return CouplingAccessor(location, *this);
585 }
void libMesh::CouplingMatrix::resize ( const unsigned int  n)
inline

Resizes the matrix and initializes all entries to be 0.

Definition at line 598 of file coupling_matrix.h.

References _ranges, and _size.

Referenced by clear(), and CouplingMatrix().

599 {
600  _size = n;
601 
602  _ranges.clear();
603 }
unsigned int libMesh::CouplingMatrix::size ( ) const
inline
Returns
the size of the matrix, i.e. N for an NxN matrix.

Definition at line 590 of file coupling_matrix.h.

References _size.

Referenced by libMesh::SparsityPattern::Build::operator()().

591 {
592  return _size;
593 }

Friends And Related Function Documentation

friend class ConstCouplingAccessor
friend

Definition at line 103 of file coupling_matrix.h.

Referenced by operator()().

friend class ConstCouplingRow
friend

Definition at line 105 of file coupling_matrix.h.

friend class ConstCouplingRowConstIterator
friend

Definition at line 106 of file coupling_matrix.h.

friend class CouplingAccessor
friend

Definition at line 104 of file coupling_matrix.h.

Referenced by operator()().

Member Data Documentation

rc_type libMesh::CouplingMatrix::_ranges
private

Definition at line 121 of file coupling_matrix.h.

Referenced by operator&=(), and resize().

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: