3838#define OPM_GEOMETRY_HEADER
3939
4040#include < cmath>
41+ #include < variant>
4142
4243// Warning suppression for Dune includes.
4344#include < opm/grid/utility/platform_dependent/disable_warnings.h>
4445
4546#include < dune/common/version.hh>
47+ #include < dune/common/overloadset.hh>
4648#include < dune/geometry/referenceelements.hh>
4749#include < dune/grid/common/geometry.hh>
4850
@@ -405,6 +407,10 @@ namespace Dune
405407
406408 // / @brief Construct from center, volume (1- and 0-moments) and
407409 // / corners.
410+ // / @warning This constructor does not own the corners or indices,
411+ // / thus, the pointers must remain valid for the lifetime of
412+ // / the Geometry object.
413+ // /
408414 // / @param pos the centroid of the entity
409415 // / @param vol the volume(area) of the entity
410416 // / @param allcorners pointer of all corner positions in the grid
@@ -413,17 +419,34 @@ namespace Dune
413419 // / by (kji), i.e. i running fastest.
414420 Geometry (const GlobalCoordinate& pos,
415421 ctype vol,
416- std::shared_ptr< const EntityVariable<cpgrid::Geometry<0 , 3 >, 3 >> allcorners_ptr ,
422+ EntityVariable<cpgrid::Geometry<0 , 3 >, 3 > const * allcorners_view ,
417423 const int * corner_indices)
418424 : pos_(pos), vol_(vol),
419- allcorners_ (allcorners_ptr ), cor_idx_(corner_indices)
425+ allcorners_ (allcorners_view ), cor_idx_(corner_indices)
420426 {
421- assert (allcorners_ && corner_indices);
427+ assert (allcorners_view && corner_indices);
422428 }
423429
430+ // / @brief Construct from center, volume (1- and 0-moments) and
431+ // / corners.
432+ // /
433+ // / @param pos the centroid of the entity
434+ // / @param vol the volume(area) of the entity
435+ // / @param allcorners pointer of all corner positions in the grid
436+ // / @param corner_indices array of 8 indices into allcorners. The
437+ // / indices must be given in lexicographical order
438+ // / by (kji), i.e. i running fastest.
439+ Geometry (const GlobalCoordinate& pos,
440+ ctype vol,
441+ const EntityVariable<cpgrid::Geometry<0 , 3 >, 3 >& allcorners_storage,
442+ const int * corner_indices)
443+ : pos_(pos), vol_(vol),
444+ allcorners_(allcorners_storage), cor_idx_(corner_indices)
445+ {}
446+
424447 // / Default constructor, giving a non-valid geometry.
425448 Geometry ()
426- : pos_(0.0 ), vol_(0.0 ), allcorners_(0 ), cor_idx_(0 )
449+ : pos_(0.0 ), vol_(0.0 ), allcorners_(nullptr ), cor_idx_(nullptr )
427450 {
428451 }
429452
@@ -511,8 +534,16 @@ namespace Dune
511534 // / @brief Get the cor-th of 8 corners of the hexahedral base cell.
512535 GlobalCoordinate corner (int cor) const
513536 {
514- assert (allcorners_ && cor_idx_);
515- return (allcorners_->data ())[cor_idx_[cor]].center ();
537+ return std::visit (
538+ Dune::overload (
539+ [](const EntityVariable<Geometry<0 , 3 >, 3 >& corners) -> const EntityVariable<Geometry<0 , 3 >, 3 >& {
540+ return corners;
541+ },
542+ [](EntityVariable<Geometry<0 , 3 >, 3 > const * corners) -> const EntityVariable<Geometry<0 , 3 >, 3 >& {
543+ return *corners;
544+ }
545+ ), allcorners_
546+ ).data ()[cor_idx_[cor]].center ();
516547 }
517548
518549 // / Cell volume.
@@ -1022,7 +1053,7 @@ namespace Dune
10221053 refined_cells[refined_cell_idx] =
10231054 Geometry<3 ,cdim>(refined_cell_center,
10241055 refined_cell_volume,
1025- all_geom.geomVector (std::integral_constant<int ,3 >()),
1056+ all_geom.geomVector (std::integral_constant<int ,3 >()). get () ,
10261057 indices_storage_ptr);
10271058 } // end i-for-loop
10281059 } // end j-for-loop
@@ -1043,8 +1074,10 @@ namespace Dune
10431074 private:
10441075 GlobalCoordinate pos_;
10451076 double vol_;
1046- std::shared_ptr<const EntityVariable<Geometry<0 , 3 >,3 >> allcorners_; // For dimension 3 only
1047- const int * cor_idx_; // For dimension 3 only
1077+
1078+ // store either a (view) pointer or the variable itself. For dimension 3 only
1079+ std::variant<EntityVariable<Geometry<0 , 3 >,3 > const *, EntityVariable<Geometry<0 , 3 >,3 >> allcorners_;
1080+ int const * cor_idx_; // For dimension 3 only
10481081
10491082 // / @brief
10501083 // / Auxiliary function to get refined_face information: tag, index, face_to_point_, face_to_cell, face centroid,
0 commit comments