MFEM  v3.4
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  /// Assignment operator: deep copy
62  Table& operator=(const Table &rhs);
63 
64  /// Create a table with an upper limit for the number of connections.
65  explicit Table (int dim, int connections_per_row = 3);
66 
67  /** Create a table from a list of connections, see MakeFromList(). */
68  Table(int nrows, Array<Connection> &list) : size(-1), I(NULL), J(NULL)
69  { MakeFromList(nrows, list); }
70 
71  /** Create a table with one entry per row with column indices given
72  by 'partitioning'. */
73  Table (int nrows, int *partitioning);
74 
75  /// Next 7 methods are used together with the default constructor
76  void MakeI (int nrows);
77  void AddAColumnInRow (int r) { I[r]++; }
78  void AddColumnsInRow (int r, int ncol) { I[r] += ncol; }
79  void MakeJ();
80  void AddConnection (int r, int c) { J[I[r]++] = c; }
81  void AddConnections (int r, const int *c, int nc);
82  void ShiftUpI();
83 
84  /// Set the size and the number of connections for the table.
85  void SetSize(int dim, int connections_per_row);
86 
87  /** Set the rows and the number of all connections for the table.
88  Does NOT initialize the whole array I ! (I[0]=0 and I[rows]=nnz only) */
89  void SetDims(int rows, int nnz);
90 
91  /// Returns the number of TYPE I elements.
92  inline int Size() const { return size; }
93 
94  /** Returns the number of connections in the table. If Finalize() is
95  not called, it returns the number of possible connections established
96  by the used constructor. Otherwise, it is exactly the number of
97  established connections before calling Finalize(). */
98  inline int Size_of_connections() const { return I[size]; }
99 
100  /** Returns index of the connection between element i of TYPE I and
101  element j of TYPE II. If there is no connection between element i
102  and element j established in the table, then the return value is -1. */
103  int operator() (int i, int j) const;
104 
105  /// Return row i in array row (the Table must be finalized)
106  void GetRow(int i, Array<int> &row) const;
107 
108  int RowSize(int i) const { return I[i+1]-I[i]; }
109 
110  const int *GetRow(int i) const { return J+I[i]; }
111  int *GetRow(int i) { return J+I[i]; }
112 
113  int *GetI() { return I; }
114  int *GetJ() { return J; }
115  const int *GetI() const { return I; }
116  const int *GetJ() const { return J; }
117 
118  /// @brief Sort the column (TYPE II) indices in each row.
119  void SortRows();
120 
121  void SetIJ(int *newI, int *newJ, int newsize = -1);
122 
123  /** Establish connection between element i and element j in the table.
124  The return value is the index of the connection. It returns -1 if it
125  fails to establish the connection. Possibilities are there is not
126  enough memory on row i to establish connection to j, an attempt to
127  establish new connection after calling Finalize(). */
128  int Push( int i, int j );
129 
130  /** Finalize the table initialization. The function may be called
131  only once, after the table has been initialized, in order to compress
132  array J (by getting rid of -1's in array J). Calling this function
133  will "freeze" the table and function Push will work no more.
134  Note: The table is functional even without calling Finalize(). */
135  void Finalize();
136 
137  /** Create the table from a list of connections {(from, to)}, where 'from'
138  is a TYPE I index and 'to' is a TYPE II index. The list is assumed to be
139  sorted and free of duplicities, i.e., you need to call Array::Sort and
140  Array::Unique before calling this method. */
141  void MakeFromList(int nrows, const Array<Connection> &list);
142 
143  /// Returns the number of TYPE II elements (after Finalize() is called).
144  int Width() const;
145 
146  /// Call this if data has been stolen.
147  void LoseData() { size = -1; I = J = NULL; }
148 
149  /// Prints the table to stream out.
150  void Print(std::ostream & out = mfem::out, int width = 4) const;
151  void PrintMatlab(std::ostream & out) const;
152 
153  void Save(std::ostream &out) const;
154  void Load(std::istream &in);
155 
156  void Copy(Table & copy) const;
157  void Swap(Table & other);
158 
159  void Clear();
160 
161  long MemoryUsage() const;
162 
163  /// Destroys Table.
164  ~Table();
165 };
166 
167 /// Specialization of the template function Swap<> for class Table
168 template <> inline void Swap<Table>(Table &a, Table &b)
169 {
170  a.Swap(b);
171 }
172 
173 /// Transpose a Table
174 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
175 Table * Transpose (const Table &A);
176 
177 /// Transpose an Array<int>
178 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
179 
180 /// C = A * B (as boolean matrices)
181 void Mult (const Table &A, const Table &B, Table &C);
182 Table * Mult (const Table &A, const Table &B);
183 
184 
185 /** Data type STable. STable is similar to Table, but it's for symmetric
186  connectivity, i.e. TYPE I is equivalent to TYPE II. In the first
187  dimension we put the elements with smaller index. */
188 
189 class STable : public Table
190 {
191 public:
192  /// Creates table with fixed number of connections.
193  STable (int dim, int connections_per_row = 3);
194 
195  /** Returns index of the connection between element i of TYPE I and
196  element j of TYPE II. If there is no connection between element i
197  and element j established in the table, then the return value is -1. */
198  int operator() (int i, int j) const;
199 
200  /** Establish connection between element i and element j in the table.
201  The return value is the index of the connection. It returns -1 if it
202  fails to establish the connection. Possibilities are there is not
203  enough memory on row i to establish connection to j, an attempt to
204  establish new connection after calling Finalize(). */
205  int Push( int i, int j );
206 
207  /// Destroys STable.
208  ~STable() {}
209 };
210 
211 
212 class DSTable
213 {
214 private:
215  class Node
216  {
217  public:
218  Node *Prev;
219  int Column, Index;
220  };
221 
222  int NumRows, NumEntries;
223  Node **Rows;
224 #ifdef MFEM_USE_MEMALLOC
225  MemAlloc <Node, 1024> NodesMem;
226 #endif
227 
228  int Push_(int r, int c);
229  int Index(int r, int c) const;
230 
231 public:
232  DSTable(int nrows);
233  int NumberOfRows() const { return (NumRows); }
234  int NumberOfEntries() const { return (NumEntries); }
235  int Push(int a, int b)
236  { return ((a <= b) ? Push_(a, b) : Push_(b, a)); }
237  int operator()(int a, int b) const
238  { return ((a <= b) ? Index(a, b) : Index(b, a)); }
239  ~DSTable();
240 
242  {
243  private:
244  Node *n;
245  public:
246  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
247  int operator!() { return (n != NULL); }
248  void operator++() { n = n->Prev; }
249  int Column() { return (n->Column); }
250  int Index() { return (n->Index); }
251  };
252 };
253 
254 }
255 
256 #endif
Table(int nrows, Array< Connection > &list)
Definition: table.hpp:68
int Push(int i, int j)
Definition: table.cpp:218
int * GetJ()
Definition: table.hpp:114
void SetSize(int dim, int connections_per_row)
Set the size and the number of connections for the table.
Definition: table.cpp:126
void AddColumnsInRow(int r, int ncol)
Definition: table.hpp:78
int operator()(int i, int j) const
Definition: table.cpp:167
void MakeI(int nrows)
Next 7 methods are used together with the default constructor.
Definition: table.cpp:84
void Swap< Table >(Table &a, Table &b)
Specialization of the template function Swap&lt;&gt; for class Table.
Definition: table.hpp:168
void SortRows()
Sort the column (TYPE II) indices in each row.
Definition: table.cpp:198
bool operator<(const Connection &rhs) const
Definition: table.hpp:34
void Swap(Table &other)
Definition: table.cpp:399
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:478
void SetDims(int rows, int nnz)
Definition: table.cpp:142
int Push(int a, int b)
Definition: table.hpp:235
const int * GetI() const
Definition: table.hpp:115
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:189
int Push(int i, int j)
Definition: table.cpp:572
void Save(std::ostream &out) const
Definition: table.cpp:340
void LoseData()
Call this if data has been stolen.
Definition: table.hpp:147
int Size_of_connections() const
Definition: table.hpp:98
RowIterator(const DSTable &t, int r)
Definition: table.hpp:246
void AddConnections(int r, const int *c, int nc)
Definition: table.cpp:106
int Width() const
Returns the number of TYPE II elements (after Finalize() is called).
Definition: table.cpp:295
int operator()(int i, int j) const
Definition: table.cpp:560
void Print(std::ostream &out=mfem::out, int width=4) const
Prints the table to stream out.
Definition: table.cpp:305
int dim
Definition: ex3.cpp:47
void MakeFromList(int nrows, const Array< Connection > &list)
Definition: table.cpp:274
Table & operator=(const Table &rhs)
Assignment operator: deep copy.
Definition: table.cpp:43
void Clear()
Definition: table.cpp:373
void AddConnection(int r, int c)
Definition: table.hpp:80
int * I
Definition: table.hpp:52
long MemoryUsage() const
Definition: table.cpp:406
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:418
int Size() const
Returns the number of TYPE I elements.
Definition: table.hpp:92
Connection(int from, int to)
Definition: table.hpp:30
void PrintMatlab(std::ostream &out) const
Definition: table.cpp:327
STable(int dim, int connections_per_row=3)
Creates table with fixed number of connections.
Definition: table.cpp:556
void Finalize()
Definition: table.cpp:239
int size
size is the number of TYPE I elements.
Definition: table.hpp:47
void AddAColumnInRow(int r)
Definition: table.hpp:77
int * GetRow(int i)
Definition: table.hpp:111
Helper struct for defining a connectivity table, see Table::MakeFromList.
Definition: table.hpp:27
void ShiftUpI()
Definition: table.cpp:117
void Load(std::istream &in)
Definition: table.cpp:354
~Table()
Destroys Table.
Definition: table.cpp:412
void SetIJ(int *newI, int *newJ, int newsize=-1)
Definition: table.cpp:206
void MakeJ()
Definition: table.cpp:94
bool operator==(const Connection &rhs) const
Definition: table.hpp:32
const int * GetRow(int i) const
Definition: table.hpp:110
~STable()
Destroys STable.
Definition: table.hpp:208
int operator()(int a, int b) const
Definition: table.hpp:237
int NumberOfEntries() const
Definition: table.hpp:234
int * GetI()
Definition: table.hpp:113
int RowSize(int i) const
Definition: table.hpp:108
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:233
void Copy(Table &copy) const
Definition: table.cpp:381
DSTable(int nrows)
Definition: table.cpp:585
const int * GetJ() const
Definition: table.hpp:116
Table()
Creates an empty table.
Definition: table.hpp:56