MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
table.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_TABLE
13 #define MFEM_TABLE
14 
15 // Data types for Table.
16 
17 #include "mem_alloc.hpp"
18 #include "array.hpp"
19 #include "globals.hpp"
20 #include <ostream>
21 #include <istream>
22 
23 namespace mfem
24 {
25 
26 /// Helper struct for defining a connectivity table, see Table::MakeFromList.
27 struct Connection
28 {
29  int from, to;
30  Connection(int from, int to) : from(from), to(to) {}
31 
32  bool operator== (const Connection &rhs) const
33  { return (from == rhs.from) && (to == rhs.to); }
34  bool operator< (const Connection &rhs) const
35  { return (from == rhs.from) ? (to < rhs.to) : (from < rhs.from); }
36 };
37 
38 
39 /** Data type Table. Table stores the connectivity of elements of TYPE I
40  to elements of TYPE II, for example, it may be Element-To-Face
41  connectivity table, etc. */
42 class Table
43 {
44 protected:
45 
46  /// size is the number of TYPE I elements.
47  int size;
48 
49  /** Arrays for the connectivity information in the CSR storage.
50  I is of size "size+1", J is of size the number of connections
51  between TYPE I to TYPE II elements (actually stored I[size]). */
52  int *I, *J;
53 
54 public:
55  /// Creates an empty table
56  Table() { size = -1; I = J = NULL; }
57 
58  /// Copy constructor
59  Table(const Table &);
60 
61  /// Create a table with an upper limit for the number of connections.
62  explicit Table (int dim, int connections_per_row = 3);
63 
64  /** Create a table from a list of connections, see MakeFromList. */
65  Table(int nrows, Array<Connection> &list) : size(-1), I(NULL), J(NULL)
66  { MakeFromList(nrows, list); }
67 
68  /** Create a table with one entry per row with column indices given
69  by 'partitioning'. */
70  Table (int nrows, int *partitioning);
71 
72  /// Next 7 methods are used together with the default constructor
73  void MakeI (int nrows);
74  void AddAColumnInRow (int r) { I[r]++; }
75  void AddColumnsInRow (int r, int ncol) { I[r] += ncol; }
76  void MakeJ();
77  void AddConnection (int r, int c) { J[I[r]++] = c; }
78  void AddConnections (int r, const int *c, int nc);
79  void ShiftUpI();
80 
81  /// Set the size and the number of connections for the table.
82  void SetSize(int dim, int connections_per_row);
83 
84  /** Set the rows and the number of all connections for the table.
85  Does NOT initialize the whole array I ! (I[0]=0 and I[rows]=nnz only) */
86  void SetDims(int rows, int nnz);
87 
88  /// Returns the number of TYPE I elements.
89  inline int Size() const { return size; }
90 
91  /** Returns the number of connections in the table. If Finalize() is
92  not called, it returns the number of possible connections established
93  by the used constructor. Otherwise, it is exactly the number of
94  established connections before calling Finalize(). */
95  inline int Size_of_connections() const { return I[size]; }
96 
97  /** Returns index of the connection between element i of TYPE I and
98  element j of TYPE II. If there is no connection between element i
99  and element j established in the table, then the return value is -1. */
100  int operator() (int i, int j) const;
101 
102  /// Return row i in array row (the Table must be finalized)
103  void GetRow(int i, Array<int> &row) const;
104 
105  int RowSize(int i) const { return I[i+1]-I[i]; }
106 
107  const int *GetRow(int i) const { return J+I[i]; }
108  int *GetRow(int i) { return J+I[i]; }
109 
110  int *GetI() { return I; }
111  int *GetJ() { return J; }
112  const int *GetI() const { return I; }
113  const int *GetJ() const { return J; }
114 
115  /// @brief Sort the column (TYPE II) indices in each row.
116  void SortRows();
117 
118  void SetIJ(int *newI, int *newJ, int newsize = -1);
119 
120  /** Establish connection between element i and element j in the table.
121  The return value is the index of the connection. It returns -1 if it
122  fails to establish the connection. Possibilities are there is not
123  enough memory on row i to establish connection to j, an attempt to
124  establish new connection after calling Finalize(). */
125  int Push( int i, int j );
126 
127  /** Finalize the table initialization. The function may be called
128  only once, after the table has been initialized, in order to compress
129  array J (by getting rid of -1's in array J). Calling this function
130  will "freeze" the table and function Push will work no more.
131  Note: The table is functional even without calling Finalize(). */
132  void Finalize();
133 
134  /** Create the table from a list of connections {(from, to)}, where 'from'
135  is a TYPE I index and 'to' is a TYPE II index. The list is assumed to be
136  sorted and free of duplicities, i.e., you need to call Array::Sort and
137  Array::Unique before calling this method. */
138  void MakeFromList(int nrows, const Array<Connection> &list);
139 
140  /// Returns the number of TYPE II elements (after Finalize() is called).
141  int Width() const;
142 
143  /// Call this if data has been stolen.
144  void LoseData() { size = -1; I = J = NULL; }
145 
146  /// Prints the table to stream out.
147  void Print(std::ostream & out = mfem::out, int width = 4) const;
148  void PrintMatlab(std::ostream & out) const;
149 
150  void Save(std::ostream &out) const;
151  void Load(std::istream &in);
152 
153  void Copy(Table & copy) const;
154  void Swap(Table & other);
155 
156  void Clear();
157 
158  long MemoryUsage() const;
159 
160  /// Destroys Table.
161  ~Table();
162 };
163 
164 /// Specialization of the template function Swap<> for class Table
165 template <> inline void Swap<Table>(Table &a, Table &b)
166 {
167  a.Swap(b);
168 }
169 
170 /// Transpose a Table
171 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
172 Table * Transpose (const Table &A);
173 
174 /// Transpose an Array<int>
175 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
176 
177 /// C = A * B (as boolean matrices)
178 void Mult (const Table &A, const Table &B, Table &C);
179 Table * Mult (const Table &A, const Table &B);
180 
181 
182 /** Data type STable. STable is similar to Table, but it's for symmetric
183  connectivity, i.e. TYPE I is equivalent to TYPE II. In the first
184  dimension we put the elements with smaller index. */
185 
186 class STable : public Table
187 {
188 public:
189  /// Creates table with fixed number of connections.
190  STable (int dim, int connections_per_row = 3);
191 
192  /** Returns index of the connection between element i of TYPE I and
193  element j of TYPE II. If there is no connection between element i
194  and element j established in the table, then the return value is -1. */
195  int operator() (int i, int j) const;
196 
197  /** Establish connection between element i and element j in the table.
198  The return value is the index of the connection. It returns -1 if it
199  fails to establish the connection. Possibilities are there is not
200  enough memory on row i to establish connection to j, an attempt to
201  establish new connection after calling Finalize(). */
202  int Push( int i, int j );
203 
204  /// Destroys STable.
205  ~STable() {}
206 };
207 
208 
209 class DSTable
210 {
211 private:
212  class Node
213  {
214  public:
215  Node *Prev;
216  int Column, Index;
217  };
218 
219  int NumRows, NumEntries;
220  Node **Rows;
221 #ifdef MFEM_USE_MEMALLOC
222  MemAlloc <Node, 1024> NodesMem;
223 #endif
224 
225  int Push_(int r, int c);
226  int Index(int r, int c) const;
227 
228 public:
229  DSTable(int nrows);
230  int NumberOfRows() const { return (NumRows); }
231  int NumberOfEntries() const { return (NumEntries); }
232  int Push(int a, int b)
233  { return ((a <= b) ? Push_(a, b) : Push_(b, a)); }
234  int operator()(int a, int b) const
235  { return ((a <= b) ? Index(a, b) : Index(b, a)); }
236  ~DSTable();
237 
239  {
240  private:
241  Node *n;
242  public:
243  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
244  int operator!() { return (n != NULL); }
245  void operator++() { n = n->Prev; }
246  int Column() { return (n->Column); }
247  int Index() { return (n->Index); }
248  };
249 };
250 
251 }
252 
253 #endif
Table(int nrows, Array< Connection > &list)
Definition: table.hpp:65
int Push(int i, int j)
Definition: table.cpp:208
int * GetJ()
Definition: table.hpp:111
void SetSize(int dim, int connections_per_row)
Set the size and the number of connections for the table.
Definition: table.cpp:116
void AddColumnsInRow(int r, int ncol)
Definition: table.hpp:75
int operator()(int i, int j) const
Definition: table.cpp:157
void MakeI(int nrows)
Next 7 methods are used together with the default constructor.
Definition: table.cpp:74
void Swap< Table >(Table &a, Table &b)
Specialization of the template function Swap&lt;&gt; for class Table.
Definition: table.hpp:165
void SortRows()
Sort the column (TYPE II) indices in each row.
Definition: table.cpp:188
bool operator<(const Connection &rhs) const
Definition: table.hpp:34
void Swap(Table &other)
Definition: table.cpp:389
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
void SetDims(int rows, int nnz)
Definition: table.cpp:132
int Push(int a, int b)
Definition: table.hpp:232
const int * GetI() const
Definition: table.hpp:112
int * J
Definition: table.hpp:52
void GetRow(int i, Array< int > &row) const
Return row i in array row (the Table must be finalized)
Definition: table.cpp:179
int Push(int i, int j)
Definition: table.cpp:562
void Save(std::ostream &out) const
Definition: table.cpp:330
void LoseData()
Call this if data has been stolen.
Definition: table.hpp:144
int Size_of_connections() const
Definition: table.hpp:95
RowIterator(const DSTable &t, int r)
Definition: table.hpp:243
void AddConnections(int r, const int *c, int nc)
Definition: table.cpp:96
int Width() const
Returns the number of TYPE II elements (after Finalize() is called).
Definition: table.cpp:285
int operator()(int i, int j) const
Definition: table.cpp:550
void Print(std::ostream &out=mfem::out, int width=4) const
Prints the table to stream out.
Definition: table.cpp:295
int dim
Definition: ex3.cpp:47
void MakeFromList(int nrows, const Array< Connection > &list)
Definition: table.cpp:264
void Clear()
Definition: table.cpp:363
void AddConnection(int r, int c)
Definition: table.hpp:77
int * I
Definition: table.hpp:52
long MemoryUsage() const
Definition: table.cpp:396
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:408
int Size() const
Returns the number of TYPE I elements.
Definition: table.hpp:89
Connection(int from, int to)
Definition: table.hpp:30
void PrintMatlab(std::ostream &out) const
Definition: table.cpp:317
STable(int dim, int connections_per_row=3)
Creates table with fixed number of connections.
Definition: table.cpp:546
void Finalize()
Definition: table.cpp:229
int size
size is the number of TYPE I elements.
Definition: table.hpp:47
void AddAColumnInRow(int r)
Definition: table.hpp:74
int * GetRow(int i)
Definition: table.hpp:108
Helper struct for defining a connectivity table, see Table::MakeFromList.
Definition: table.hpp:27
void ShiftUpI()
Definition: table.cpp:107
void Load(std::istream &in)
Definition: table.cpp:344
~Table()
Destroys Table.
Definition: table.cpp:402
void SetIJ(int *newI, int *newJ, int newsize=-1)
Definition: table.cpp:196
void MakeJ()
Definition: table.cpp:84
bool operator==(const Connection &rhs) const
Definition: table.hpp:32
const int * GetRow(int i) const
Definition: table.hpp:107
~STable()
Destroys STable.
Definition: table.hpp:205
int operator()(int a, int b) const
Definition: table.hpp:234
int NumberOfEntries() const
Definition: table.hpp:231
int * GetI()
Definition: table.hpp:110
int RowSize(int i) const
Definition: table.hpp:105
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64
int NumberOfRows() const
Definition: table.hpp:230
void Copy(Table &copy) const
Definition: table.cpp:371
DSTable(int nrows)
Definition: table.cpp:575
const int * GetJ() const
Definition: table.hpp:113
Table()
Creates an empty table.
Definition: table.hpp:56