7 #include <shared_mutex> 11 #include <dune/common/hybridutilities.hh> 12 #include <dune/common/timer.hh> 13 #include <dune/common/version.hh> 14 #include <dune/common/parallel/mpihelper.hh> 16 #include <dune/geometry/type.hh> 17 #include <dune/grid/common/backuprestore.hh> 18 #include <dune/grid/common/capabilities.hh> 19 #include <dune/grid/common/grid.hh> 20 #include <dune/grid/utility/structuredgridfactory.hh> 22 #include <amdis/common/Concepts.hpp> 23 #include <amdis/common/DefaultGridView.hpp> 24 #include <amdis/common/SharedPtr.hpp> 25 #include <amdis/Observer.hpp> 26 #include <amdis/Output.hpp> 31 template <
class HostGr
id>
45 :
public Dune::GridDefaultImplementation<
46 HG::dimension, HG::dimensionworld, typename HG::ctype, AdaptiveGridFamily<HG> >
47 ,
public Notifier<event::preAdapt, event::adapt, event::postAdapt>
56 using Element =
typename Traits::template Codim<0>::Entity;
61 template <
class HostGrid_,
62 Dune::disableCopyMove<Self, HostGrid_> = 0>
68 std::shared_ptr<HostGrid>
const&
hostGrid()
const {
return hostGrid_; }
77 template <
int codim, Dune::PartitionIteratorType pt = Dune::All_Partition>
78 auto lbegin(
int level)
const {
return hostGrid_->levelGridView(level).template begin<codim,pt>(); }
81 template <
int codim, Dune::PartitionIteratorType pt = Dune::All_Partition>
82 auto lend(
int level)
const {
return hostGrid_->levelGridView(level).template end<codim,pt>(); }
86 template <
int codim, Dune::PartitionIteratorType pt = Dune::All_Partition>
87 auto leafbegin()
const {
return hostGrid_->leafGridView().template begin<codim,pt>(); }
90 template <
int codim, Dune::PartitionIteratorType pt = Dune::All_Partition>
91 auto leafend()
const {
return hostGrid_->leafGridView().template end<codim,pt>(); }
95 auto ilevelbegin(Element
const& e)
const {
return hostGrid_->levelGridView(e.level()).ibegin(e); }
98 auto ilevelend(Element
const& e)
const {
return hostGrid_->levelGridView(e.level()).iend(e); }
101 auto ileafbegin(Element
const& e)
const {
return hostGrid_->leafGridView().ibegin(e); }
104 auto ileafend(Element
const& e)
const {
return hostGrid_->leafGridView().iend(e); }
113 int maxLevel()
const {
return hostGrid_->maxLevel(); }
116 int size(
int level,
int codim)
const {
return hostGrid_->size(level, codim); }
119 int size(
int codim)
const {
return hostGrid_->size(codim); }
122 int size(
int level, Dune::GeometryType type)
const {
return hostGrid_->size(level, type); }
125 int size(Dune::GeometryType type)
const {
return hostGrid_->size(type); }
137 auto const&
globalIdSet()
const {
return hostGrid_->globalIdSet(); }
140 auto const&
localIdSet()
const {
return hostGrid_->localIdSet(); }
143 auto const&
levelIndexSet(
int level)
const {
return hostGrid_->levelIndexSet(level); }
157 for (
int i = 0; i < refCount; ++i) {
159 for (
const auto& element : elements(hostGrid_->leafGridView()))
160 hostGrid_->mark(1, element);
168 bool mark(
int refCount, Element
const& e) {
return hostGrid_->mark(refCount, e); }
171 int getMark(Element
const& e)
const {
return hostGrid_->getMark(e); }
177 mightCoarsen_ = hostGrid_->preAdapt();
178 Dune::MPIHelper::getCollectiveCommunication().max(&mightCoarsen_, 1);
180 info(2,
"AdaptiveGrid::preAdapt needed {} seconds", t.elapsed());
181 return mightCoarsen_;
188 refined_ = hostGrid_->adapt();
189 Dune::MPIHelper::getCollectiveCommunication().max(&refined_, 1);
191 info(2,
"AdaptiveGrid::adapt needed {} seconds", t.elapsed());
199 hostGrid_->postAdapt();
202 info(2,
"AdaptiveGrid::postAdapt needed {} seconds", t.elapsed());
224 auto const&
comm()
const {
return hostGrid_->comm(); }
227 template <
class DataHandle>
229 Dune::CommunicationDirection dir,
int level)
const 231 hostGrid_->levelGridView(level).communicate(handle,iftype,dir);
235 template <
class DataHandle>
237 Dune::CommunicationDirection dir)
const 239 hostGrid_->leafGridView().communicate(handle,iftype,dir);
250 if constexpr (std::is_convertible_v<decltype(std::declval<HG>().
loadBalance()),
bool>)
251 return hostGrid_->loadBalance();
253 hostGrid_->loadBalance();
266 template <
class DataHandle>
269 if constexpr (std::is_convertible_v<decltype(std::declval<HG>().
loadBalance(handle)),
bool>)
270 return hostGrid_->loadBalance(handle);
272 hostGrid_->loadBalance(handle);
279 int overlapSize(
int level,
int codim)
const {
return hostGrid_->levelGridView(level).overlapSize(codim); }
282 int overlapSize(
int codim)
const {
return hostGrid_->leafGridView().overlapSize(codim); }
285 int ghostSize(
int level,
int codim)
const {
return hostGrid_->levelGridView(level).ghostSize(codim); }
288 int ghostSize(
int codim)
const {
return hostGrid_->leafGridView().ghostSize(codim); }
294 template <
class EntitySeed>
295 auto entity(EntitySeed
const& seed)
const {
return hostGrid_->entity(seed); }
301 std::shared_ptr<HostGrid> hostGrid_;
305 bool mightCoarsen_ =
false;
308 bool refined_ =
false;
312 unsigned long changeIndex_ = 0;
316 template <
class HostGr
id>
321 template <
class HostGr
id>
328 using LeafGridView = Dune::GridView< AMDiS::DefaultLeafGridViewTraits<const Grid> >;
329 using LevelGridView = Dune::GridView< AMDiS::DefaultLevelGridViewTraits<const Grid> >;
335 template <
class HostGr
id>
342 template <
class HostGr
id>
351 template <
class HostGr
id>
352 struct AdaptiveGridImpl
357 template <
class HostGr
id>
358 struct AdaptiveGridImpl<AdaptiveGrid<HostGrid>>
366 template <
class HostGr
id>
377 template <
class HostGr
id>
381 using Self = GridFactory;
384 using HostGridFactory = GridFactory<HostGrid>;
388 using ctype =
typename GridType::ctype;
390 enum { dim = GridType::dimension };
391 enum { dimworld = GridType::dimensionworld };
393 template <
class... Args,
394 Dune::disableCopyMove<Self, Args...> = 0>
395 GridFactory(Args&&... args)
396 : hostFactory_(FWD(args)...)
400 void insertVertex(FieldVector<ctype,dimworld>
const& pos)
override 402 hostFactory_.insertVertex(pos);
405 template <
class F,
class... Args>
406 using HasInsertElement = decltype(std::declval<F>().insertElement(std::declval<Args>()...));
409 void insertElement(GeometryType
const& type,
410 std::vector<unsigned int>
const& vertices)
override 412 hostFactory_.insertElement(type, vertices);
415 #if DUNE_VERSION_LT(DUNE_GRID,2,8) 416 using ElementParametrizationType = std::shared_ptr<VirtualFunction<FieldVector<ctype,dim>,FieldVector<ctype,dimworld> > >;
419 void insertElement(GeometryType
const& type,
420 std::vector<unsigned int>
const& vertices,
421 ElementParametrizationType
const& elementParametrization)
override 423 using ElementParametrizationType
424 = std::function<FieldVector<ctype,dimworld>(FieldVector<ctype,dim>)>;
427 void insertElement(GeometryType
const& type,
428 std::vector<unsigned int>
const& vertices,
429 ElementParametrizationType elementParametrization)
override 432 using A0 = GeometryType;
433 using A1 = std::vector<unsigned int>;
434 using A2 = ElementParametrizationType;
435 if constexpr (Std::is_detected<HasInsertElement, HostGridFactory, A0,A1,A2>::value)
436 hostFactory_.insertElement(type, vertices, elementParametrization);
440 using Super::insertElement;
442 template <
class F,
class... Args>
443 using HasInsertBoundarySegment = decltype(std::declval<F>().insertBoundarySegment(std::declval<Args>()...));
446 void insertBoundarySegment(std::vector<unsigned int>
const& vertices)
override 448 hostFactory_.insertBoundarySegment(vertices);
451 using BoundarySegmentType = std::shared_ptr<BoundarySegment<dim,dimworld> >;
454 void insertBoundarySegment(std::vector<unsigned int>
const& vertices,
455 BoundarySegmentType
const& boundarySegment)
override 457 using A0 = std::vector<unsigned int>;
458 using A1 = BoundarySegmentType;
459 if constexpr (Std::is_detected<HasInsertBoundarySegment, HostGridFactory, A0,A1>::value)
460 hostFactory_.insertBoundarySegment(vertices, boundarySegment);
466 #if DUNE_VERSION_LT(DUNE_GRID,2,7) 469 std::unique_ptr<HostGrid>
hostGrid(hostFactory_.createGrid());
473 ToUniquePtr<GridType> createGrid()
override 475 std::unique_ptr<HostGrid>
hostGrid(hostFactory_.createGrid());
476 return makeToUnique<GridType>(std::move(
hostGrid));
481 HostGridFactory hostFactory_;
487 template <class HostGrid, bool = Dune::Capabilities::hasBackupRestoreFacilities<HostGrid>::v>
488 class BackupRestoreFacilityImpl {};
490 template <
class HostGr
id>
491 class BackupRestoreFacilityImpl<HostGrid,true>
494 using HostBackupRestoreFacility = BackupRestoreFacility<HostGrid>;
499 template <
class Output>
500 static void backup(Grid
const& grid, Output
const& filename_or_stream)
502 HostBackupRestoreFacility::backup(*grid.hostGrid(), filename_or_stream);
506 template <
class Input>
507 static Grid* restore(Input
const& filename_or_stream)
509 std::unique_ptr<HostGrid>
hostGrid(HostBackupRestoreFacility::restore(filename_or_stream));
510 return new Grid(std::move(hostGrid));
519 template <
class HostGr
id>
520 class BackupRestoreFacility<AMDiS::
AdaptiveGrid<HostGrid>>
521 :
public Impl::BackupRestoreFacilityImpl<HostGrid>
524 using Impl::BackupRestoreFacilityImpl<HostGrid>::BackupRestoreFacilityImpl;
528 namespace Capabilities
530 template <
class HostGr
id,
int codim>
534 template <
class HostGr
id,
int codim>
538 template <
class HostGr
id>
542 template <
class HostGr
id>
546 template <
class HostGr
id>
550 template <
class HostGr
id,
int codim >
554 template <
class HostGr
id>
558 template <
class HostGr
id>
562 template <
class HostGr
id>
void postAdapt()
Perform cleanup after grid adaptation and notify observers of the postAdapt event.
Definition: AdaptiveGrid.hpp:196
auto const & globalIdSet() const
Return const reference to the grids global id set.
Definition: AdaptiveGrid.hpp:137
auto ileafbegin(Element const &e) const
Obtain begin intersection iterator with respect to the leaf GridView.
Definition: AdaptiveGrid.hpp:101
int overlapSize(int level, int codim) const
Return size of the overlap region for a given codim on the level grid view.
Definition: AdaptiveGrid.hpp:279
bool adapt()
Adapt the grid and notify observers of the adapt event.
Definition: AdaptiveGrid.hpp:185
auto leafbegin() const
Iterator to first leaf entity of given codim.
Definition: AdaptiveGrid.hpp:87
auto lend(int level) const
one past the end on this level
Definition: AdaptiveGrid.hpp:82
Definition: AdaptiveGrid.hpp:32
Definition: AdaptiveGrid.hpp:373
void update()
Update all registered dependent objects if grid is changed manually.
Definition: AdaptiveGrid.hpp:206
void error_exit(std::string const &str, Args &&... args)
print a message and exit
Definition: Output.hpp:142
auto entity(EntitySeed const &seed) const
Obtain Entity from EntitySeed of the HostGrid.
Definition: AdaptiveGrid.hpp:295
typename Impl::AdaptiveGridImpl< HostGrid >::type AdaptiveGrid_t
Definition: AdaptiveGrid.hpp:367
auto ileafend(Element const &e) const
Obtain end intersection iterator with respect to the leaf GridView.
Definition: AdaptiveGrid.hpp:104
void communicate(DataHandle &handle, Dune::InterfaceType iftype, Dune::CommunicationDirection dir, int level) const
Communicate data of level gridView.
Definition: AdaptiveGrid.hpp:228
int size(int codim) const
Return number of leaf entities of a given codim in this process.
Definition: AdaptiveGrid.hpp:119
int overlapSize(int codim) const
Return size of the overlap region for a given codim on the leaf grid view.
Definition: AdaptiveGrid.hpp:282
Definition: AdaptiveGrid.hpp:325
bool mark(int refCount, Element const &e)
Marks an entity to be refined/coarsened in a subsequent adapt.
Definition: AdaptiveGrid.hpp:168
auto const & leafIndexSet() const
Return const reference to the host grids leaf index set.
Definition: AdaptiveGrid.hpp:146
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
auto ilevelbegin(Element const &e) const
Obtain begin intersection iterator with respect to the level GridView.
Definition: AdaptiveGrid.hpp:95
int getMark(Element const &e) const
Return refinement mark for entity.
Definition: AdaptiveGrid.hpp:171
bool loadBalance(DataHandle &handle)
Calls loadBalance(handle) on the underlying grid.
Definition: AdaptiveGrid.hpp:267
int ghostSize(int codim) const
Return size of the ghost region for a given codim on the leaf grid view.
Definition: AdaptiveGrid.hpp:288
std::size_t numBoundarySegments() const
Returns the number of boundary segments within the macro grid.
Definition: AdaptiveGrid.hpp:128
auto leafend() const
One past the end of the sequence of leaf entities.
Definition: AdaptiveGrid.hpp:91
Definition: Observer.hpp:25
auto const & comm() const
Return const reference to a collective communication object.
Definition: AdaptiveGrid.hpp:224
bool loadBalance()
Calls loadBalance on the underlying grid.
Definition: AdaptiveGrid.hpp:248
auto const & levelIndexSet(int level) const
Return const reference to the host grids level index set for level level.
Definition: AdaptiveGrid.hpp:143
auto const & localIdSet() const
Return const reference to the host grids local id set.
Definition: AdaptiveGrid.hpp:140
int maxLevel() const
Return maximum level defined in this grid.
Definition: AdaptiveGrid.hpp:113
int size(Dune::GeometryType type) const
Return number of leaf entities per geometry type in this process.
Definition: AdaptiveGrid.hpp:125
Definition: Observer.hpp:30
auto ilevelend(Element const &e) const
Obtain end intersection iterator with respect to the level GridView.
Definition: AdaptiveGrid.hpp:98
unsigned long changeIndex() const
Returns the grid change index, see changeIndex.
Definition: AdaptiveGrid.hpp:212
void info(int noInfoLevel, std::string const &str, Args &&... args)
prints a message, if Environment::infoLevel() >= noInfoLevel
Definition: Output.hpp:105
Mixin for signaling of certain events.
Definition: Observer.hpp:54
int size(int level, Dune::GeometryType type) const
Return number of entities per level and geometry type in this process.
Definition: AdaptiveGrid.hpp:122
int ghostSize(int level, int codim) const
Return size of the ghost region for a given codim on the level grid view.
Definition: AdaptiveGrid.hpp:285
bool preAdapt()
Prepare the grid for adaptation and notify observers of the preAdapt event.
Definition: AdaptiveGrid.hpp:174
Wrapper class for Dune-grids that allows automatic signalling of events during grid adaptation...
Definition: AdaptiveGrid.hpp:44
std::shared_ptr< T > wrap_or_share(T &t)
Definition: SharedPtr.hpp:22
void globalRefine(int refCount)
Refines all grid elements refCount times.
Definition: AdaptiveGrid.hpp:155
auto lbegin(int level) const
Iterator to first entity of given codim on level.
Definition: AdaptiveGrid.hpp:78
Definition: Observer.hpp:19
void communicate(DataHandle &handle, Dune::InterfaceType iftype, Dune::CommunicationDirection dir) const
Communicate data of leaf gridView.
Definition: AdaptiveGrid.hpp:236
std::shared_ptr< HostGrid > const & hostGrid() const
Return the underlying grid.
Definition: AdaptiveGrid.hpp:68
int size(int level, int codim) const
Number of grid entities per level and codim.
Definition: AdaptiveGrid.hpp:116