OrientedEntityTable.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: OrientedEntityTable.hpp
4//
5// Created: Wed Aug 26 11:13:20 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Bård Skaflestad <bard.skaflestad@sintef.no>
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18 Copyright 2009, 2010 Statoil ASA.
19
20 This file is part of The Open Porous Media project (OPM).
21
22 OPM is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
26
27 OPM is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with OPM. If not, see <http://www.gnu.org/licenses/>.
34*/
35
36#ifndef OPM_ORIENTEDENTITYTABLE_HEADER
37#define OPM_ORIENTEDENTITYTABLE_HEADER
38
40#include <opm/common/ErrorMacros.hpp>
41#include <opm/common/utility/SparseTable.hpp>
42#include <algorithm>
43#include <climits>
44#include <map>
45#include <numeric>
46#include <stdexcept>
47#include <vector>
48
50namespace Dune
51{
52 namespace cpgrid
53 {
54
55
58 template <int codim_to>
59 class OrientedEntityRange : private Opm::SparseTable< EntityRep<codim_to> >::row_type
60 {
61 public:
63 typedef ToType* ToTypePtr;
64 typedef typename Opm::SparseTable<ToType>::row_type R;
65
68 : orientation_(true)
69 {
70 }
74 OrientedEntityRange(const R& r, bool orientation)
75 : R(r), orientation_(orientation)
76 {
77 }
78 int size () const { return R::size(); }
79 using R::empty;
80 using R::begin;
81 using R::end;
85 ToType operator[](int subindex) const
86 {
87 ToType erep = *(this->begin() + subindex);
88 return orientation_ ? erep : erep.opposite();
89 }
90 private:
91 bool orientation_;
92 };
93
94
97 template <int codim_to>
98 class MutableOrientedEntityRange : private Opm::SparseTable< EntityRep<codim_to> >::mutable_row_type
99 {
100 public:
103 typedef typename Opm::SparseTable<ToType>::mutable_row_type R;
104
107 : R(ToTypePtr(0), ToTypePtr(0)), orientation_(true)
108 {
109 }
113 MutableOrientedEntityRange(const R& r, bool orientation)
114 : R(r), orientation_(orientation)
115 {
116 }
117 int size () const { return R::size(); }
118 using R::empty;
119 using R::begin;
120 using R::end;
124 ToType operator[](int subindex) const
125 {
126 ToType erep = R::operator[](subindex);
127 return orientation_ ? erep : erep.opposite();
128 }
129 private:
130 bool orientation_;
131 };
132
133
142 template <int codim_from, int codim_to>
143 class OrientedEntityTable : private Opm::SparseTable< EntityRep<codim_to> >
144 {
145 friend class CpGridData;
146 public:
149 typedef OrientedEntityRange<codim_to> row_type; // ??? doxygen henter doc fra Opm::SparseTable
150 typedef Opm::SparseTable<ToType> super_t;
151 typedef typename super_t::mutable_row_type mutable_row_type;
152
155 {
156 }
157
169 template <typename DataIter, typename IntegerIter>
170 OrientedEntityTable(DataIter data_beg, DataIter data_end,
171 IntegerIter rowsize_beg, IntegerIter rowsize_end)
172 : super_t(data_beg, data_end, rowsize_beg, rowsize_end)
173 {
174 }
175
176 using super_t::empty;
177 using super_t::size;
178 using super_t::dataSize;
179 using super_t::clear;
180 using super_t::appendRow;
181 using super_t::allocate;
182
187 int rowSize(const FromType& e) const
188 {
189 return super_t::rowSize(e.index());
190 }
191
198 {
199 return row_type(super_t::operator[](e.index()), e.orientation());
200 }
201
208 {
209 return super_t::operator[](e.index());
210 }
214 bool operator==(const OrientedEntityTable& other) const
215 {
216 return super_t::operator==(other);
217 }
218
221 {
222 super_t::swap(other);
223 }
224
247 void printSparseRelationMatrix(std::ostream& os) const
248 {
249 for (int i = 0; i < size(); ++i) {
250 const FromType from_ent(i, true);
251 const row_type r = operator[](from_ent);
252 const int rsize = r.size();
253 for (int j = 0; j < rsize; ++j) {
254 os << i << ' ' << r[j].index() << ' ' << (r[j].orientation() ? 1 : -1) << '\n';
255 }
256 }
257 os << std::flush;
258 }
259
277 void printRelationMatrix(std::ostream& os) const
278 {
279 int columns = numberOfColumns();
280 for (int i = 0; i < size(); ++i) {
281 FromType from_ent(i, true);
282 row_type r = operator[](from_ent);
283 int cur_col = 0;
284 int next_ent = 0;
285 ToType to_ent = r[next_ent];
286 int next_print = to_ent.index();
287 while (cur_col < columns) {
288 if (cur_col == next_print) {
289 if (to_ent.orientation()) {
290 os << " 1";
291 } else {
292 os << " -1";
293 }
294 ++next_ent;
295 if (next_ent >= r.size()) {
296 next_print = columns;
297 } else {
298 to_ent = r[next_ent];
299 next_print = to_ent.index();
300 }
301 } else {
302 os << " 0";
303 }
304 ++cur_col;
305 }
306 os << '\n';
307 }
308 }
309
320 {
321 // Find the maximum index used. This will give (one less than) the size
322 // of the table to be created.
323 int maxind = -1;
324 for (int i = 0; i < size(); ++i) {
325 EntityRep<codim_from> from_ent(i, true);
326 row_type r = operator[](from_ent);
327 for (int j = 0; j < r.size(); ++j) {
328 EntityRep<codim_to> to_ent = r[j];
329 if (!to_ent.isValid()) {
330 continue;
331 }
332 int ind = to_ent.index();
333 maxind = std::max(ind, maxind);
334 }
335 }
336 return makeInverseRelation(maxind + 1);
337 }
338
347 {
348 if (num_rows < 0) {
349 OPM_THROW(std::invalid_argument, "Inverse relation row count must be nonnegative");
350 }
351
352 // Build the new_sizes vector and compute datacount.
353 std::vector<int> new_sizes(num_rows);
354 int datacount = 0;
355 for (int i = 0; i < size(); ++i) {
356 EntityRep<codim_from> from_ent(i, true);
357 row_type r = operator[](from_ent);
358 for (int j = 0; j < r.size(); ++j) {
359 EntityRep<codim_to> to_ent = r[j];
360 if (!to_ent.isValid()) {
361 continue;
362 }
363 int ind = to_ent.index();
364 if (ind >= num_rows) {
365 OPM_THROW(std::out_of_range, "Inverse relation destination index exceeds row count");
366 }
367 ++new_sizes[ind];
368 ++datacount;
369 }
370 }
371 // Compute the cumulative sizes.
372 std::vector<int> cumul_sizes(new_sizes.size() + 1);
373 cumul_sizes[0] = 0;
374 std::partial_sum(new_sizes.begin(), new_sizes.end(), cumul_sizes.begin() + 1);
375 // Using the cumulative sizes array as indices, we populate new_data.
376 // Note that cumul_sizes[ind] is not kept constant, but incremented so that
377 // it always gives the correct index for new data corresponding to index ind.
378 std::vector<EntityRep<codim_from> > new_data(datacount);
379 for (int i = 0; i < size(); ++i) {
380 EntityRep<codim_from> from_ent(i, true);
381 row_type r = operator[](from_ent);
382 for (int j = 0; j < r.size(); ++j) {
383 EntityRep<codim_to> to_ent(r[j]);
384 if (!to_ent.isValid()) {
385 continue;
386 }
387 int ind = to_ent.index();
388 int data_ind = cumul_sizes[ind];
389 new_data[data_ind] = to_ent.orientation() ? from_ent : from_ent.opposite();
390 ++cumul_sizes[ind];
391 }
392 }
393 return OrientedEntityTable<codim_to, codim_from>(new_data.begin(),
394 new_data.end(),
395 new_sizes.begin(),
396 new_sizes.end());
397 }
398
399 private:
400 int numberOfColumns() const
401 {
402 int maxind = 0;
403 for (int i = 0; i < size(); ++i) {
404 FromType from_ent(i, true);
405 row_type r = operator[](from_ent);
406 for (int j = 0; j < r.size(); ++j) {
407 maxind = std::max(maxind, r[j].index());
408 }
409 }
410 return maxind + 1;
411 }
412 };
413
414
415 } // namespace cpgrid
416} // namespace Dune
417
418
419
420
421#endif // OPM_ORIENTEDENTITYTABLE_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:144
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:98
bool orientation() const
Returns true if the entity has positive orientation. Not a Dune interface method.
Definition: EntityRep.hpp:148
EntityRep opposite() const
Returns an EntityRep with opposite orientation.
Definition: EntityRep.hpp:155
int index() const
The (positive) index of an entity. Not a Dune interface method.
Definition: EntityRep.hpp:125
bool isValid() const
Whether this representation has a local entity index. InvalidIndex marks absent entities,...
Definition: EntityRep.hpp:134
A class used as a row type for OrientedEntityTable.
Definition: OrientedEntityTable.hpp:99
int size() const
Definition: OrientedEntityTable.hpp:117
EntityRep< codim_to > ToType
Definition: OrientedEntityTable.hpp:101
MutableOrientedEntityRange()
Default constructor yielding an empty range.
Definition: OrientedEntityTable.hpp:106
ToType * ToTypePtr
Definition: OrientedEntityTable.hpp:102
MutableOrientedEntityRange(const R &r, bool orientation)
Constructor taking a row type and an orientation.
Definition: OrientedEntityTable.hpp:113
Opm::SparseTable< ToType >::mutable_row_type R
Definition: OrientedEntityTable.hpp:103
ToType operator[](int subindex) const
Random access operator.
Definition: OrientedEntityTable.hpp:124
A class used as a row type for OrientedEntityTable.
Definition: OrientedEntityTable.hpp:60
OrientedEntityRange(const R &r, bool orientation)
Constructor taking a row type and an orientation.
Definition: OrientedEntityTable.hpp:74
int size() const
Definition: OrientedEntityTable.hpp:78
EntityRep< codim_to > ToType
Definition: OrientedEntityTable.hpp:62
Opm::SparseTable< ToType >::row_type R
Definition: OrientedEntityTable.hpp:64
OrientedEntityRange()
Default constructor yielding an empty range.
Definition: OrientedEntityTable.hpp:67
ToType operator[](int subindex) const
Random access operator.
Definition: OrientedEntityTable.hpp:85
ToType * ToTypePtr
Definition: OrientedEntityTable.hpp:63
Represents the topological relationships between sets of entities, for example cells and faces.
Definition: OrientedEntityTable.hpp:144
EntityRep< codim_to > ToType
Definition: OrientedEntityTable.hpp:148
super_t::mutable_row_type mutable_row_type
Definition: OrientedEntityTable.hpp:151
EntityRep< codim_from > FromType
Definition: OrientedEntityTable.hpp:147
void swap(OrientedEntityTable &other)
Swap contents for other OrientedEntityTable.
Definition: OrientedEntityTable.hpp:220
void printRelationMatrix(std::ostream &os) const
Prints the full relation matrix corresponding to the table.
Definition: OrientedEntityTable.hpp:277
mutable_row_type row(const FromType &e)
Given an entity e of codimension codim_from, returns a row (an indirect container) containing its nei...
Definition: OrientedEntityTable.hpp:207
OrientedEntityTable()
Default constructor.
Definition: OrientedEntityTable.hpp:154
OrientedEntityTable(DataIter data_beg, DataIter data_end, IntegerIter rowsize_beg, IntegerIter rowsize_end)
Constructor taking iterators to a sequence of table data and a sequence of row size data.
Definition: OrientedEntityTable.hpp:170
row_type operator[](const FromType &e) const
Given an entity e of codimension codim_from, returns a row (an indirect container) containing its nei...
Definition: OrientedEntityTable.hpp:197
OrientedEntityTable< codim_to, codim_from > makeInverseRelation() const
Returns the inverse relation, mapping codim_to entities to their codim_from neighbours.
Definition: OrientedEntityTable.hpp:319
OrientedEntityTable< codim_to, codim_from > makeInverseRelation(int num_rows) const
Returns the inverse relation with an explicit row count. Skips the size-inference pass and preserves ...
Definition: OrientedEntityTable.hpp:346
int rowSize(const FromType &e) const
Given an entity e of codimension codim_from, returns the number of neighbours of codimension codim_to...
Definition: OrientedEntityTable.hpp:187
bool operator==(const OrientedEntityTable &other) const
Elementwise equality.
Definition: OrientedEntityTable.hpp:214
OrientedEntityRange< codim_to > row_type
Definition: OrientedEntityTable.hpp:149
Opm::SparseTable< ToType > super_t
Definition: OrientedEntityTable.hpp:150
void printSparseRelationMatrix(std::ostream &os) const
Prints the relation matrix corresponding to the table, sparse format.
Definition: OrientedEntityTable.hpp:247
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10