Skip to content

Commit

Permalink
Small simplification for defineChildToParentAndIdxInParentCell
Browse files Browse the repository at this point in the history
  • Loading branch information
aritorto committed Jun 20, 2024
1 parent 316700b commit 25327dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
41 changes: 18 additions & 23 deletions opm/grid/cpgrid/CpGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2168,31 +2168,28 @@ CpGrid::defineChildToParentAndIdxInParentCell(std::map<std::array<int,2>,std::ar
const int& startingGridIdx = static_cast<int>(this->data_.size()) -1;
adapted_child_to_parent_cells.resize(cell_count, std::array<int,2>{-1,-1}); // Entry is {-1,-1} when cell has no father, otherwise, {level parent cell, parent cell index}.
adapted_cell_to_idxInParentCell.resize(cell_count, -1);

// Rewrite only the entries of adapted cells that have a parent cell
for (int cell = 0; cell < cell_count; ++cell) {
const auto& elemLgr = adaptedCell_to_elemLgrAndElemLgrCell[cell][0];
// (elemLgr == -1) represents that the adapted cell is equivalent to a cell from the starting grid (current_view_data_)
if (elemLgr != -1) {
// Search for the level where the parent cell was born, and its index in that level grid.
// Notice that elemLgr is a cell index in current_view_data_ (the starting grid where elements got marked for (further) refinement).
const auto& element = Dune::cpgrid::Entity<0>(*current_view_data_, elemLgr, true); // elemLgr == parent cell index in starting grid.
const auto& elemLevel = element.level(); // parent cell level (where the parent cell was born, might not coincide with the starting grid).
const auto& elemLevelIdx = (elemLevel == 0) ? element.getOrigin().index() : element.getLevelElem().index(); // parent cell index in the level it was born.
adapted_child_to_parent_cells[cell] = {elemLevel, elemLevelIdx};
// For the element with index "cell" in the adapted grid (or updated-leaf-grid-view),
// - if "cell" is a new born refined cell (i.e. born in the current adapt-call), we get the parent cell index from the preAdapt-leaf-grid-view ("current_view_data_").
// In this case, "preAdapt_parentElemIdx_or_elemIdx" represents "preAdapt_parentElemIdx".
// - if "cell" is either a coarse cell or a refined cell that was born in a preAdapt-refined-level-grid, we get the index of the equivalent cell in the
// preAdapt-leaf-grid-view ("current_view_data_"). In this case, "preAdapt_parentElemIdx_or_elemIdx" represents "preAdapt_elemIdx".
bool isCellNewBorn = (adaptedCell_to_elemLgrAndElemLgrCell[cell][0] != -1);
const auto& preAdapt_parent_or_elem_idx = isCellNewBorn ? adaptedCell_to_elemLgrAndElemLgrCell[cell][0] : adaptedCell_to_elemLgrAndElemLgrCell[cell][1];
// Get the element of either a parent cell of a new born refined cell with index "cell" or an equivalent cell, in the preAdapt-leaf-grid-view ("current_view_data_").
const auto& preAdapt_parent_or_elem = Dune::cpgrid::Entity<0>(*current_view_data_, preAdapt_parent_or_elem_idx, true);
if (isCellNewBorn) {
adapted_child_to_parent_cells[cell] = {preAdapt_parent_or_elem.level(), preAdapt_parent_or_elem.getEquivLevelElem().index()};
adapted_cell_to_idxInParentCell[cell] = adaptedCell_to_elemLgrAndElemLgrCell[cell][1];
}
else {
// Populate child_to_parent_ relationships from the previous (existing grids before calling adapt).
assert(elemLgr == -1);
const auto& elemIdx = adaptedCell_to_elemLgrAndElemLgrCell[cell][1];
// Search for the level where the parent cell was born, and its index in that level grid.
// Notice that elemLgr is a cell index in current_view_data_ (the starting grid where elements got marked for (further) refinement).
const auto& element = Dune::cpgrid::Entity<0>(*current_view_data_, elemIdx, true); // elemIdx of equivalent cell in the starting grid.
if(element.hasFather()) {
adapted_child_to_parent_cells[cell] = {element.father().level(), element.father().index()};
const auto& elemLevel = element.level(); // parent cell level (level where the parent cell was born, might not coincide with the starting grid).
const auto& elemLevelIdx = (elemLevel == 0) ? element.getOrigin().index() : element.getLevelElem().index(); // parent cell index in the level it was born.
adapted_cell_to_idxInParentCell[cell] = this-> data_[elemLevel]-> cell_to_idxInParentCell_[elemLevelIdx];
// Only populate the entries of refined cells that were born in preAdapt-refined-level-grids.
if(preAdapt_parent_or_elem.hasFather()) {
adapted_child_to_parent_cells[cell] = {preAdapt_parent_or_elem.father().level(), preAdapt_parent_or_elem.father().index() };
adapted_cell_to_idxInParentCell[cell] = this->data_[preAdapt_parent_or_elem.level()]->
cell_to_idxInParentCell_[preAdapt_parent_or_elem.getEquivLevelElem().index()];
}
}
}
Expand All @@ -2209,9 +2206,7 @@ CpGrid::defineChildToParentAndIdxInParentCell(std::map<std::array<int,2>,std::ar
// Search for the level where the parent cell was born, and its index in that level grid.
// Notice that elemLgr is a cell index in current_view_data_ (the starting grid where elements got marked for (further) refinement).
const auto& element = Dune::cpgrid::Entity<0>(*current_view_data_, elemLgr, true); // elemLgr == parent cell index in starting grid.
const auto& elemLevel = element.level(); // parent cell level (level where the parent cell was born, might not coincide with the starting grid).
const auto& elemLevelIdx = (elemLevel == 0) ? element.getOrigin().index() : element.getLevelElem().index(); // parent cell index in the level it was born.
refined_child_to_parent_cells_vec[shiftedLevel][cell] = {elemLevel, elemLevelIdx};
refined_child_to_parent_cells_vec[shiftedLevel][cell] = {element.level(), element.getEquivLevelElem().index()};
refined_cell_to_idxInParentCell_vec[shiftedLevel][cell] = refinedLevelAndRefinedCell_to_elemLgrAndElemLgrCell[{level,cell}][1];
}
}
Expand Down
20 changes: 20 additions & 0 deletions opm/grid/cpgrid/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ class Entity : public EntityRep<codim>
/// \brief To be invoked only for leaf-grid-view entities. Get equivalent element on the level grid the leaf-entity was born.
Entity<0> getLevelElem() const;

/// \brief Get equivalent element on the level grid where the entity was born, if grid = leaf-grid-view. Otherwise, return itself.
Entity<0> getEquivLevelElem() const;

/// \brief Get Cartesian Index in the level grid view where the Entity was born.
int getLevelCartesianIdx() const;

Expand Down Expand Up @@ -569,6 +572,23 @@ Dune::cpgrid::Entity<0> Dune::cpgrid::Entity<codim>::getLevelElem() const
}
}

template<int codim>
Dune::cpgrid::Entity<0> Dune::cpgrid::Entity<codim>::getEquivLevelElem() const
{
// Check if the element belongs to the leaf grid view
// This is needed to get the index of the element in the level it was born.
// leaf_to_level_cells_ [leaf idx] = {level where the entity was born, equivalent cell idx in that level}
if (!(pgrid_ -> leaf_to_level_cells_.empty())) // entity on the LeafGridView
{
const int& entityLevelIdx = pgrid_->leaf_to_level_cells_[this->index()][1];
return Dune::cpgrid::Entity<0>( *((*(pgrid_ -> level_data_ptr_))[this->level()].get()), entityLevelIdx, true);
}
else {
return *this;
}
}


template<int codim>
int Dune::cpgrid::Entity<codim>::getLevelCartesianIdx() const
{
Expand Down

0 comments on commit 25327dc

Please sign in to comment.