libMesh
Loading...
Searching...
No Matches
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 std::size_t n=0)
 Constructor.
 
bool operator() (const std::size_t i, const std::size_t j) const
 
CouplingAccessor operator() (const std::size_t i, const std::size_t j)
 
std::size_t size () const
 
void resize (const std::size_t n)
 Resizes the matrix and initializes all entries to be 0.
 
void clear ()
 Clears the matrix.
 
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.
 
typedef std::vector< range_typerc_type
 

Private Attributes

rc_type _ranges
 
std::size_t _size
 The size of the matrix.
 

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 std::size_t  n = 0)
inlineexplicit

Constructor.

Definition at line 571 of file coupling_matrix.h.

571 :
572 _ranges(), _size(n)
573{
574 this->resize(n);
575}
std::size_t _size
The size of the matrix.
void resize(const std::size_t n)
Resizes the matrix and initializes all entries to be 0.

References resize().

Member Function Documentation

◆ clear()

void libMesh::CouplingMatrix::clear ( )
inline

Clears the matrix.

Definition at line 624 of file coupling_matrix.h.

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

References resize().

◆ empty()

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

Definition at line 632 of file coupling_matrix.h.

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

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.emplace
90 (next, 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.

◆ operator()() [1/2]

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

Definition at line 595 of file coupling_matrix.h.

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

References _size, and CouplingAccessor.

◆ operator()() [2/2]

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

Definition at line 580 of file coupling_matrix.h.

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

References _size, and ConstCouplingAccessor.

◆ resize()

void libMesh::CouplingMatrix::resize ( const std::size_t  n)
inline

Resizes the matrix and initializes all entries to be 0.

Definition at line 614 of file coupling_matrix.h.

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

References _ranges, and _size.

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

◆ size()

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

Definition at line 606 of file coupling_matrix.h.

607{
608 return _size;
609}

References _size.

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

Friends And Related Symbol 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

std::size_t libMesh::CouplingMatrix::_size
private

The size of the matrix.

Definition at line 126 of file coupling_matrix.h.

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


The documentation for this class was generated from the following files: