Commit c9ad15d7 authored by Martin Nolte's avatar Martin Nolte

Merge branch 'cherry-pick-2e965c69' into 'releases/2.6'

Merge branch 'bugfix/copy_geogridview' into 'master'

See merge request !257
parents f3a4bcb7 5f557e10
Pipeline #6293 passed with stage
in 48 minutes and 19 seconds
......@@ -390,7 +390,7 @@ namespace Dune
const LeafIndexSet &leafIndexSet () const
{
if( !leafIndexSet_ )
leafIndexSet_ = LeafIndexSet( hostGrid().leafIndexSet() );
leafIndexSet_.reset( hostGrid().leafIndexSet() );
assert( leafIndexSet_ );
return leafIndexSet_;
}
......
......@@ -112,10 +112,35 @@ namespace Dune
static const bool conforming = Traits::conforming;
GridView ( const Grid &grid, const HostGridView &hostGridView )
: grid_( &grid ),
hostGridView_( hostGridView )
: grid_( &grid ), hostGridView_( hostGridView )
{}
GridView ( const This &other )
: grid_( other.grid_ ), hostGridView_( other.hostGridView_ )
{}
GridView ( This &&other )
: grid_( other.grid_ ), hostGridView_( std::move( other.hostGridView_ ) )
{}
This &operator= ( const This &other )
{
grid_ = other.grid_;
hostGridView_ = other.hostGridView_;
if( indexSet_ )
indexSet_.reset( hostGridView().indexSet() );
return *this;
}
This &operator= ( This &&other )
{
grid_ = other.grid_;
hostGridView_ = std::move( other.hostGridView_ );
if( indexSet_ )
indexSet_.reset( hostGridView().indexSet() );
return *this;
}
const Grid &grid () const
{
assert( grid_ );
......@@ -124,8 +149,7 @@ namespace Dune
const IndexSet &indexSet () const
{
if( !indexSet_ )
indexSet_ = IndexSet( hostGridView().indexSet() );
indexSet_.reset( hostGridView().indexSet() );
return indexSet_;
}
......
......@@ -39,23 +39,18 @@ namespace Dune
typedef typename Base::Types Types;
IndexSet ()
: hostIndexSet_( 0 )
{}
IndexSet () = default;
explicit IndexSet ( const HostIndexSet &hostIndexSet )
: hostIndexSet_( &hostIndexSet )
{}
IndexSet ( const This &other )
: hostIndexSet_( other.hostIndexSet_ )
{}
// The index set contains a pointer to the host index set, so copying or assigning this can be dangerous.
IndexSet ( const This & ) = delete;
IndexSet ( This && ) = delete;
const This &operator= ( const This &other )
{
hostIndexSet_ = other.hostIndexSet_;
return *this;
}
IndexSet &operator= ( const This & ) = delete;
IndexSet &operator= ( This && ) = delete;
using Base::index;
using Base::subIndex;
......@@ -97,6 +92,9 @@ namespace Dune
explicit operator bool () const { return bool( hostIndexSet_ ); }
void reset () { hostIndexSet_ = nullptr; }
void reset ( const HostIndexSet &hostIndexSet ) { hostIndexSet_ = &hostIndexSet; }
private:
const HostIndexSet &hostIndexSet () const
{
......@@ -104,7 +102,7 @@ namespace Dune
return *hostIndexSet_;
}
const HostIndexSet *hostIndexSet_;
const HostIndexSet *hostIndexSet_ = nullptr;
};
} // namespace GeoGrid
......
......@@ -123,6 +123,43 @@ void test(const std::string& gridfile)
}
void testNestedGeometryGrid(const std::string& gridfile) {
using NestedGeometryGrid = Dune::GeometryGrid< GeometryGrid, Dune::IdenticalCoordFunction< Grid::ctype, Grid::dimensionworld > >;
using NestedGgLeafView = NestedGeometryGrid::LeafGridView;
Dune::GridPtr< NestedGeometryGrid > pgeogrid(gridfile);
NestedGeometryGrid &geogrid = *pgeogrid;
geogrid.globalRefine( 1 );
// creating different variables for storing grid views
NestedGgLeafView gv1 = geogrid.leafGridView();
std::shared_ptr<NestedGgLeafView> pgv2;
NestedGgLeafView gv3 = geogrid.leafGridView();
// use geometry grid views copy constructor and copy assignment operator
{
NestedGgLeafView tmpGv = geogrid.leafGridView();
// copying/assigning temporary grid view would be dangerous with default implementation of copy constructor or copy assignment operator from geomrtry grid view
pgv2 = std::make_shared<NestedGgLeafView> (tmpGv);
gv3 = tmpGv;
}
// The following test would now be guaranteed to fail with the default implementation of geometry grid views copy constructor
// assert(&(pgv2->indexSet().hostIndexSet()) == &(pgv2->impl().hostGridView().indexSet()))
// but this would not compile since the member functions impl() and hostIndexSet() are protected/private.
// So we do the following
// get index sets and do some arbitrary test to check whether they work correctly
auto const& is1 = gv1.indexSet();
auto const& is2 = pgv2->indexSet();
auto const& is3 = gv3.indexSet();
for(auto const& e : elements(gv1)) {
bool idxCorrect = (is1.index(e)==is2.index(e));
idxCorrect &= (is1.index(e)==is3.index(e));
assert(idxCorrect);
}
}
int main ( int argc, char **argv )
try
{
......@@ -140,6 +177,10 @@ try
test<GeometryGrid>(gridfile);
std::cout << "=== GeometryGrid took " << watch.elapsed() << " seconds\n";
watch.reset();
testNestedGeometryGrid(gridfile);
std::cout << "=== NestedGeometryGrid took " << watch.elapsed() << " seconds\n";
// compile, but do not actually call, because it is not working yet
if (false)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment