Entity.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Entity.hpp
4//
5// Created: Fri May 29 20:26:48 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Brd Skaflestad <bard.skaflestad@sintef.no>
9// Antonella Ritorto <antonella.ritorto@opm-op.com>
10//
11// $Date$
12//
13// $Revision$
14//
15//===========================================================================
16
17/*
18 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
19 Copyright 2009, 2010, 2022 Equinor ASA.
20
21 This file is part of The Open Porous Media project (OPM).
22
23 OPM is free software: you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation, either version 3 of the License, or
26 (at your option) any later version.
27
28 OPM is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with OPM. If not, see <http://www.gnu.org/licenses/>.
35*/
36
37#ifndef OPM_ENTITY_HEADER
38#define OPM_ENTITY_HEADER
39
40#include <dune/common/version.hh>
41#include <dune/geometry/type.hh>
42#include <dune/grid/common/gridenums.hh>
43
46
47
48namespace Dune
49{
50namespace cpgrid
51{
52
53template<int,int> class Geometry;
54template<int,PartitionIteratorType> class Iterator;
55class IntersectionIterator;
56class HierarchicIterator;
57class CpGridData;
58class LevelGlobalIdSet;
59
63template <int codim>
64class Entity : public EntityRep<codim>
65{
66 friend class LevelGlobalIdSet;
67 friend class GlobalIdSet;
68 friend class HierarchicIterator;
69 friend class CpGridData;
70
71public:
74 enum { codimension = codim };
75 enum { dimension = 3 };
77 enum { dimensionworld = 3 };
78
79 // the official DUNE names
81
85 template <int cd>
86 struct Codim
87 {
89 };
90
91
92 typedef cpgrid::Geometry<3-codim,3> Geometry;
94
98
99 typedef double ctype;
100
105 // Entity(const CpGridData& grid, int entityrep)
106 // : EntityRep<codim>(entityrep), pgrid_(&grid)
107 // {
108 // }
109
112 : EntityRep<codim>(), pgrid_( 0 )
113 {
114 }
115
117 Entity(const CpGridData& grid, EntityRep<codim> entityrep)
118 : EntityRep<codim>(entityrep), pgrid_(&grid)
119 {
120 }
121
123 Entity(const CpGridData& grid, int index_arg, bool orientation_arg)
124 : EntityRep<codim>(index_arg, orientation_arg), pgrid_(&grid)
125 {
126 }
127
129 Entity(int index_arg, bool orientation_arg)
130 : EntityRep<codim>(index_arg, orientation_arg), pgrid_()
131 {
132 }
133
135 bool operator==(const Entity& other) const
136 {
137 return EntityRep<codim>::operator==(other) && pgrid_ == other.pgrid_;
138 }
139
141 bool operator!=(const Entity& other) const
142 {
143 return !operator==(other);
144 }
145
150 {
151 return EntitySeed( impl() );
152 }
153
155 const Geometry& geometry() const;
156
158 int level() const;
159
165 bool isLeaf() const;
166
168 bool isRegular() const
169 {
170 return true;
171 }
172
175 PartitionType partitionType() const;
176
179 GeometryType type() const
180 {
181 return Dune::GeometryTypes::cube(3 - codim);
182 }
183
185 unsigned int subEntities ( const unsigned int cc ) const;
186
189 template <int cc>
190 typename Codim<cc>::Entity subEntity(int i) const;
191
194
197
200
203
204
207
210
212 bool isNew() const
213 {
214 return false;
215 }
216
218 bool mightVanish() const
219 {
220 return false;
221 }
222
228 bool hasFather() const;
229
234
241
247
248 // Mimic Dune entity wrapper
250 const Entity& impl() const
251 {
252 return *this;
253 }
254
256 {
257 return *this;
258 }
259
262 bool isValid () const;
263
268
271
274
275protected:
277private:
278 // static to not need any extra storage per Enitity. One object used for all instances
279 // constexpr to allow for in-class instantiation
281 static constexpr std::array<int,8> in_father_reference_elem_corner_indices_ = {0,1,2,3,4,5,6,7};
282};
283
284} // namespace cpgrid
285} // namespace Dune
286
287// now we include the Iterators.hh We need to do this here because for hbegin/hend the compiler
288// needs to know the size of hierarchicIterator
289#include "Iterators.hpp"
290#include "Intersection.hpp"
291namespace Dune
292{
293namespace cpgrid
294{
295template<int codim>
297{
298 static_assert(codim == 0, "");
299 return LevelIntersectionIterator(*pgrid_, *this, false);
300}
301
302template<int codim>
304{
305 static_assert(codim == 0, "");
306 return LevelIntersectionIterator(*pgrid_, *this, true);
307}
308
309template<int codim>
311{
312 static_assert(codim == 0, "");
313 return LeafIntersectionIterator(*pgrid_, *this, false);
314}
315
316template<int codim>
318{
319 static_assert(codim == 0, "");
320 return LeafIntersectionIterator(*pgrid_, *this, true);
321}
322
323
324template<int codim>
326{
327 // Creates iterator with first child as target if there is one. Otherwise empty stack and target.
328 return HierarchicIterator(*this, maxLevel);
329}
330
332template<int codim>
334{
335 // Creates iterator with empty stack and target.
336 return HierarchicIterator(maxLevel);
337}
338
339template <int codim>
340PartitionType Entity<codim>::partitionType() const
341{
342 return pgrid_->partition_type_indicator_->getPartitionType(*this);
343}
344} // namespace cpgrid
345} // namespace Dune
346
347
349
350namespace Dune {
351namespace cpgrid {
352
353template<int codim>
354unsigned int Entity<codim>::subEntities ( const unsigned int cc ) const
355{
356 if (cc == codim) {
357 return 1;
358 }
359 else if ( codim == 0 ){ // Cell/element/Entity<0>
360 if ( cc == 3 ) { // Get number of corners of the element.
361 return 8;
362 }
363 }
364 return 0;
365}
366
367template <int codim>
369{
370 return pgrid_->geomVector<codim>()[*this];
371}
372
373template <int codim>
374template <int cc>
376{
377 static_assert(codim == 0, "");
378 if (cc == 0) { // Cell/element/Entity<0>
379 assert(i == 0);
380 typename Codim<cc>::Entity se(*pgrid_, this->index(), this->orientation());
381 return se;
382 } else if (cc == 3) { // Corner/Entity<3>
383 assert(i >= 0 && i < 8);
384 int corner_index = pgrid_->cell_to_point_[this->index()][i];
385 typename Codim<cc>::Entity se(*pgrid_, corner_index, true);
386 return se;
387 }
388 else {
389 OPM_THROW(std::runtime_error,
390 "No subentity exists of codimension " + std::to_string(cc));
391 }
392}
393
394template <int codim>
396{
397 // Copied implementation from EntityDefaultImplementation,
398 // except for not checking LevelIntersectionIterators.
399 typedef LeafIntersectionIterator Iter;
400 Iter end = ileafend();
401 for (Iter it = ileafbegin(); it != end; ++it) {
402 if (it->boundary()) return true;
403 }
404 return false;
405}
406
407template <int codim>
409{
410 return pgrid_ ? EntityRep<codim>::index() < pgrid_->size(codim) : false;
411}
412
413
414// level() It simply returns the level of the entity in the grid hierarchy.
415template <int codim>
417{
418 // if distributed_data_ is not empty, level_data_ptr_ has size 1.
419 if ((*(pgrid_ -> level_data_ptr_)).size() == 1){
420 return 0; // when there is no refinenment, level_ is not automatically instantiated
421 }
422 if (pgrid_ == (*(pgrid_->level_data_ptr_)).back().get()) { // entity on the leafview -> get the level where it was born:
423 return pgrid_ -> leaf_to_level_cells_[this-> index()][0]; // leaf_to_level_cells_ leafIdx -> {level/LGR, cell index in LGR}
424 }
425 else {
426 return pgrid_-> level_;
427 }
428}
429
430
431// isLeaf()
432// - if distributed_data_ is empty: an element is a leaf <-> hbegin and hend return the same iterator. Then,
433// *cells from level 0 (coarse grid) that are not parents, are Leaf.
434// *cells from any LGR are Leaf, since they do not children (nested refinement not supported).
435// - if distrubuted_data_ is NOT empty: there may be children on a different process. Therefore,
436// isLeaf() returns true <-> the element is a leaf entity of the global refinement hierarchy. Equivalently,
437// it can be checked whether parent_to_children_cells_ is empty.
438
439template<int codim>
441{
442 if ((pgrid_ -> parent_to_children_cells_).empty()){ // LGR cells
443 return true;
444 }
445 else {
446 return (std::get<0>((pgrid_ -> parent_to_children_cells_)[this-> index()]) == -1); // Cells from GLOBAL, not involved in any LGR
447 }
448}
449
450template<int codim>
452{
453 if ((pgrid_ -> child_to_parent_cells_.empty()) || (pgrid_ -> child_to_parent_cells_[this->index()][0] == -1)){
454 return false;
455 }
456 else{
457 return true;
458 }
459}
460
461template<int codim>
463{
464 if (this->hasFather()){
465 const int& coarse_level = pgrid_ -> child_to_parent_cells_[this->index()][0]; // currently, always 0
466 const int& parent_index = pgrid_ -> child_to_parent_cells_[this->index()][1];
467 const auto& coarse_grid = (*(pgrid_ -> level_data_ptr_))[coarse_level].get();
468 return Entity<0>( *coarse_grid, parent_index, true);
469 }
470 else{
471 OPM_THROW(std::logic_error, "Entity has no father.");
472 }
473}
474
475template<int codim>
477{
478 if (!(this->hasFather())){
479 OPM_THROW(std::logic_error, "Entity has no father.");
480 }
481 else{
482 // Get IJK index of the entity.
483 std::array<int,3> eIJK;
484 // Get the amount of children cell in each direction of the parent cell of the entity (same for all parents of each LGR)
485 std::array<int,3> cells_per_dim;
486 // Get "child0" IJK in the LGR
487 std::array<int,3> child0_IJK;
488 const auto& level0_grid = (*(pgrid_->level_data_ptr_))[0];
489 const auto& child0_Idx = std::get<1>((*level0_grid).parent_to_children_cells_[this->father().index()])[0];
490 // If pgrid_ is the leafview, go to the LGR where the entity was born to get its IJK index in the LGR and the LGR dimension.
491 if (pgrid_ == (*(pgrid_->level_data_ptr_)).back().get()) // checking if pgrid_ is the LeafView
492 {
493 const auto& lgr_grid = (*(pgrid_->level_data_ptr_))[this -> level()];
494 cells_per_dim = (*lgr_grid).cells_per_dim_;
495
496 const auto& entity_lgrIdx = pgrid_ -> leaf_to_level_cells_[this->index()][1]; // leaf_to_level_cells_[cell] = {level, index}
497 (*lgr_grid).getIJK(entity_lgrIdx, eIJK);
498 (*lgr_grid).getIJK(child0_Idx, child0_IJK);
499 }
500 else // Getting grid dimension and IJK entity index when pgrid_ is an LGR
501 {
502 pgrid_ -> getIJK(this->index(), eIJK);
503 cells_per_dim = pgrid_ -> cells_per_dim_;
504 pgrid_ -> getIJK(child0_Idx, child0_IJK);
505 }
506 // Transform the local coordinates that comes from the refinemnet in such a way that the
507 // reference element of each parent cell is the unit cube. Here, (eIJK[*]-"shift")/cells_per_dim[*]
508 // Get the local coordinates of the entity (in the reference unit cube).
509 FieldVector<double, 3> corners_in_father_reference_elem_temp[8] = {
510 // corner '0'
511 { double(eIJK[0]-child0_IJK[0])/cells_per_dim[0], double(eIJK[1]-child0_IJK[1])/cells_per_dim[1],
512 double(eIJK[2]-child0_IJK[2])/cells_per_dim[2] },
513 // corner '1'
514 { double(eIJK[0]-child0_IJK[0]+1)/cells_per_dim[0], double(eIJK[1]-child0_IJK[1])/cells_per_dim[1],
515 double(eIJK[2]-child0_IJK[2])/cells_per_dim[2] },
516 // corner '2'
517 { double(eIJK[0]-child0_IJK[0])/cells_per_dim[0], double(eIJK[1]-child0_IJK[1]+1)/cells_per_dim[1],
518 double(eIJK[2]-child0_IJK[2])/cells_per_dim[2] },
519 // corner '3'
520 { double(eIJK[0]-child0_IJK[0]+1)/cells_per_dim[0], double(eIJK[1]-child0_IJK[1]+1)/cells_per_dim[1],
521 double(eIJK[2]-child0_IJK[2])/cells_per_dim[2] },
522 // corner '4'
523 { double(eIJK[0]-child0_IJK[0])/cells_per_dim[0], double(eIJK[1]-child0_IJK[1])/cells_per_dim[1],
524 double(eIJK[2]-child0_IJK[2]+1)/cells_per_dim[2] },
525 // corner '5'
526 { double(eIJK[0]-child0_IJK[0]+1)/cells_per_dim[0], double(eIJK[1]-child0_IJK[1])/cells_per_dim[1],
527 double(eIJK[2]-child0_IJK[2]+1)/cells_per_dim[2] },
528 // corner '6'
529 { double(eIJK[0]-child0_IJK[0])/cells_per_dim[0], double(eIJK[1]-child0_IJK[1]+1)/cells_per_dim[1],
530 double(eIJK[2]-child0_IJK[2]+1)/cells_per_dim[2] },
531 // corner '7'
532 { double(eIJK[0]-child0_IJK[0]+1)/cells_per_dim[0], double(eIJK[1]-child0_IJK[1]+1)/cells_per_dim[1],
533 double(eIJK[2]-child0_IJK[2]+1)/cells_per_dim[2] }};
534 auto in_father_reference_elem_corners = std::make_shared<EntityVariable<cpgrid::Geometry<0, 3>, 3>>();
535 EntityVariableBase<cpgrid::Geometry<0, 3>>& mutable_in_father_reference_elem_corners = *in_father_reference_elem_corners;
536 // Assign the corners. Make use of the fact that pointers behave like iterators.
537 mutable_in_father_reference_elem_corners.assign(corners_in_father_reference_elem_temp,
538 corners_in_father_reference_elem_temp + 8);
539 // Compute the center of the 'local-entity'.
540 Dune::FieldVector<double, 3> center_in_father_reference_elem = {0., 0.,0.};
541 for (int corn = 0; corn < 8; ++corn) {
542 for (int c = 0; c < 3; ++c)
543 {
544 center_in_father_reference_elem[c] += corners_in_father_reference_elem_temp[corn][c]/8.;
545 }
546 }
547 // Compute the volume of the 'local-entity'.
548 double volume_in_father_reference_elem = double(1)/(cells_per_dim[0]*cells_per_dim[1]*cells_per_dim[2]);
549 // Construct (and return) the Geometry<3,3> of 'child-cell in the reference element of its father (unit cube)'.
550 return Dune::cpgrid::Geometry<3,3>(center_in_father_reference_elem, volume_in_father_reference_elem,
551 in_father_reference_elem_corners, in_father_reference_elem_corner_indices_.data());
552 }
553}
554
555
556template<int codim>
558{
559 if (hasFather())
560 {
561 return this->father(); // currently, always a level 0 entity.
562 }
563 if (!(pgrid_ -> leaf_to_level_cells_.empty())) // entity on the LeafGridView
564 {
565 const int& entityIdxInLevel0 = pgrid_->leaf_to_level_cells_[this->index()][1]; // leaf_to_level_cells_ [leaf idx] = {0, cell idx}
566 const auto& coarse_grid = (*(pgrid_ -> level_data_ptr_))[0].get();
567 return Dune::cpgrid::Entity<0>( *coarse_grid, entityIdxInLevel0, true);
568 }
569 else
570 {
571 return *this;
572 }
573}
574
575template<int codim>
577{
578 // Check that the element belongs to the leaf grid view
579 // This is needed to get the index of the element in the level it was born.
580 // For cells born in level 0, it is enough to do elem.getOrigin().index().
581 // For cells born in LGRs/levels>0, the level index is stored in leaf_to_level_cells_
582 // leaf_to_level_cells_ [leaf idx] = {level, cell idx}
583 if (!(pgrid_ -> leaf_to_level_cells_.empty())) // entity on the LeafGridView
584 {
585 const auto entityLevel = this -> level();
586 const int& entityLevelIdx = pgrid_->leaf_to_level_cells_[this->index()][1];
587 const auto& level_grid = (*(pgrid_ -> level_data_ptr_))[entityLevel].get();
588 return Dune::cpgrid::Entity<0>( *level_grid, entityLevelIdx, true);
589 }
590 else {
591 throw std::invalid_argument("The entity provided does not belong to the leaf grid view. ");
592 }
593}
594
595template<int codim>
597{
598 const auto entityLevel = this -> level();
599 const auto level = (*(pgrid_ -> level_data_ptr_))[entityLevel].get();
600 const auto& elemInLevel = this->getLevelElem(); // throws when the entity does not belong to the leaf grid view.
601 return level -> global_cell_[elemInLevel.index()];
602}
603
604} // namespace cpgrid
605} // namespace Dune
606
607
608#endif // OPM_ENTITY_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:131
Definition: Entity.hpp:65
bool mightVanish() const
Returns true, if entity might disappear during the next call to adapt(). Dummy.
Definition: Entity.hpp:218
@ codimension
Definition: Entity.hpp:74
Entity< 0 > father() const
ONLY FOR CELLS (Entity<0>). Get the father Entity, in case entity.hasFather() is true.
Definition: Entity.hpp:462
const CpGridData * pgrid_
Definition: Entity.hpp:276
unsigned int subEntities(const unsigned int cc) const
Return the number of all subentities of the entity of a given codimension cc.
Definition: Entity.hpp:354
LeafIntersectionIterator ileafend() const
End leaf-iterator for the cell-cell intersections of this entity.
Definition: Entity.hpp:317
EntitySeed seed() const
Return an entity seed (light-weight entity). EntitySeed objects are used to obtain an Entity back whe...
Definition: Entity.hpp:149
int getLevelCartesianIdx() const
Get Cartesian Index in the level grid view where the Entity was born.
Definition: Entity.hpp:596
HierarchicIterator hend(int) const
Iterator end over the children/beyond last child iterator.
Definition: Entity.hpp:333
const Geometry & geometry() const
Return the geometry of the entity (does not depend on its orientation).
Definition: Entity.hpp:368
cpgrid::IntersectionIterator LeafIntersectionIterator
Definition: Entity.hpp:95
@ dimension
Definition: Entity.hpp:75
double ctype
Definition: Entity.hpp:99
Entity(const CpGridData &grid, int index_arg, bool orientation_arg)
Constructor taking a grid, entity index, and orientation.
Definition: Entity.hpp:123
bool hasFather() const
ONLY FOR CELLS (Entity<0>) Check if the entity comes from an LGR, i.e., it has been created via refin...
Definition: Entity.hpp:451
bool isValid() const
Definition: Entity.hpp:408
Entity< 0 > getOrigin() const
Definition: Entity.hpp:557
Geometry LocalGeometry
Definition: Entity.hpp:93
cpgrid::HierarchicIterator HierarchicIterator
Definition: Entity.hpp:97
bool operator==(const Entity &other) const
Equality.
Definition: Entity.hpp:135
bool isRegular() const
Refinement is not defined for CpGrid.
Definition: Entity.hpp:168
Entity()
Constructor creating empty entity.
Definition: Entity.hpp:111
HierarchicIterator hbegin(int) const
Iterator begin over the children. [If requested, also over descendants more than one generation away....
Definition: Entity.hpp:325
cpgrid::Geometry< 3-codim, 3 > Geometry
Definition: Entity.hpp:92
cpgrid::IntersectionIterator LevelIntersectionIterator
Definition: Entity.hpp:96
const Entity & impl() const
Access the actual implementation class behind Entity interface class.
Definition: Entity.hpp:250
PartitionType partitionType() const
For now, the grid is serial and the only partitionType() is InteriorEntity. Only needed when distribu...
Definition: Entity.hpp:340
Dune::cpgrid::Geometry< 3, 3 > geometryInFather() const
Return LocalGeometry representing the embedding of the entity into its father (when hasFather() is tr...
Definition: Entity.hpp:476
Entity(int index_arg, bool orientation_arg)
Constructor taking a entity index, and orientation.
Definition: Entity.hpp:129
LeafIntersectionIterator ileafbegin() const
Start leaf-iterator for the cell-cell intersections of this entity.
Definition: Entity.hpp:310
Entity(const CpGridData &grid, EntityRep< codim > entityrep)
Constructor taking a grid and an entity representation.
Definition: Entity.hpp:117
@ dimensionworld
Definition: Entity.hpp:77
Entity & impl()
Definition: Entity.hpp:255
LevelIntersectionIterator ilevelend() const
End level-iterator for the cell-cell intersections of this entity.
Definition: Entity.hpp:303
@ mydimension
Definition: Entity.hpp:76
bool isNew() const
Returns true, if the entity has been created during the last call to adapt(). Dummy.
Definition: Entity.hpp:212
Entity< 0 > getLevelElem() const
Get equivalent element on the level grid view for an element on the leaf grid view.
Definition: Entity.hpp:576
LevelIntersectionIterator ilevelbegin() const
Start level-iterator for the cell-cell intersections of this entity.
Definition: Entity.hpp:296
GeometryType type() const
Return marker object (GeometryType object) representing the reference element of the entity....
Definition: Entity.hpp:179
int level() const
Return the level of the entity in the grid hierarchy. Level = 0 represents the coarsest grid.
Definition: Entity.hpp:416
bool isLeaf() const
Check if the entity is in the leafview.
Definition: Entity.hpp:440
bool operator!=(const Entity &other) const
Inequality.
Definition: Entity.hpp:141
Entity EntitySeed
Definition: Entity.hpp:80
bool hasBoundaryIntersections() const
Definition: Entity.hpp:395
Codim< cc >::Entity subEntity(int i) const
Obtain subentity. Example: If cc = 3 and i = 5, it returns the 5th corner/vertex of the entity.
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:99
bool operator==(const EntityRep &other) const
Equality operator.
Definition: EntityRep.hpp:179
int index() const
The (positive) index of an entity. Not a Dune interface method.
Definition: EntityRep.hpp:126
Base class for EntityVariable and SignedEntityVariable. Forwards a restricted subset of the std::vect...
Definition: EntityRep.hpp:219
Definition: Geometry.hpp:75
The global id set for Dune.
Definition: Indexsets.hpp:304
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:108
Definition: Intersection.hpp:278
Definition: Indexsets.hpp:248
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
Definition: Entity.hpp:87
cpgrid::Entity< cd > Entity
Definition: Entity.hpp:88