2015-10-20 09:41:21 -05:00
|
|
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
// vi: set et ts=4 sw=4 sts=4:
|
|
|
|
/*
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2016-03-14 07:21:47 -05:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2015-10-20 09:41:21 -05:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-14 07:21:47 -05:00
|
|
|
|
|
|
|
Consult the COPYING file in the top-level source directory of this
|
|
|
|
module for the precise wording of the license and the list of
|
|
|
|
copyright holders.
|
2015-10-20 09:41:21 -05:00
|
|
|
*/
|
2019-02-01 10:33:30 -06:00
|
|
|
/*!
|
|
|
|
* \file
|
|
|
|
*
|
2019-09-05 10:04:39 -05:00
|
|
|
* \copydoc Opm::AluCartesianIndexMapper
|
2019-02-01 10:33:30 -06:00
|
|
|
*/
|
|
|
|
#ifndef EWOMS_ALU_CARTESIAN_INDEX_MAPPER_HH
|
|
|
|
#define EWOMS_ALU_CARTESIAN_INDEX_MAPPER_HH
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2021-12-01 07:00:21 -06:00
|
|
|
#include <dune/alugrid/grid.hh>
|
|
|
|
#include <dune/alugrid/3d/gridview.hh>
|
|
|
|
#include <opm/grid/common/CartesianIndexMapper.hpp>
|
2023-08-02 04:21:09 -05:00
|
|
|
#include <dune/grid/common/datahandleif.hh>
|
|
|
|
#include <dune/grid/utility/persistentcontainer.hh>
|
2016-01-17 14:15:27 -06:00
|
|
|
|
2015-09-10 08:55:54 -05:00
|
|
|
#include <array>
|
2023-08-02 04:21:09 -05:00
|
|
|
#include <cassert>
|
2023-08-15 02:32:10 -05:00
|
|
|
#include <cstddef>
|
2019-02-01 10:33:30 -06:00
|
|
|
#include <memory>
|
2023-08-02 04:21:09 -05:00
|
|
|
#include <stdexcept>
|
2016-02-22 11:43:13 -06:00
|
|
|
#include <vector>
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2021-12-01 07:00:21 -06:00
|
|
|
namespace Dune {
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
/*!
|
|
|
|
* \brief Interface class to access the logical Cartesian grid as used in industry
|
|
|
|
* standard simulator decks.
|
|
|
|
*/
|
2021-12-01 07:00:21 -06:00
|
|
|
//#if HAVE_MPI
|
|
|
|
template <>
|
|
|
|
class CartesianIndexMapper<Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming>>
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
public:
|
2021-12-01 07:00:21 -06:00
|
|
|
|
|
|
|
#if HAVE_MPI
|
|
|
|
using Grid = Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming, Dune::ALUGridMPIComm>;
|
|
|
|
#else
|
|
|
|
using Grid = Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming, Dune::ALUGridNoComm>;
|
|
|
|
#endif //HAVE_MPI
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
// data handle for communicating global ids during load balance and communication
|
|
|
|
template <class GridView>
|
|
|
|
class GlobalIndexDataHandle : public Dune::CommDataHandleIF<GlobalIndexDataHandle<GridView>, int>
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
// global id
|
|
|
|
class GlobalCellIndex
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
|
|
|
public:
|
2019-02-01 10:33:30 -06:00
|
|
|
GlobalCellIndex()
|
|
|
|
: idx_(-1)
|
|
|
|
{}
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
GlobalCellIndex& operator=(const int index)
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
idx_ = index;
|
|
|
|
return *this;
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
int index() const
|
|
|
|
{ return idx_; }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
private:
|
|
|
|
int idx_;
|
|
|
|
};
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2022-08-10 05:29:49 -05:00
|
|
|
using GlobalIndexContainer = typename Dune::PersistentContainer<Grid, GlobalCellIndex>;
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
public:
|
|
|
|
// constructor copying cartesian index to persistent container
|
|
|
|
GlobalIndexDataHandle(const GridView& gridView,
|
|
|
|
std::vector<int>& cartesianIndex)
|
|
|
|
: gridView_(gridView)
|
|
|
|
, globalIndex_(gridView.grid(), 0)
|
|
|
|
, cartesianIndex_(cartesianIndex)
|
|
|
|
{
|
|
|
|
globalIndex_.resize();
|
|
|
|
initialize();
|
|
|
|
}
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
// constructor copying cartesian index to persistent container
|
|
|
|
GlobalIndexDataHandle(const GlobalIndexDataHandle& other) = delete ;
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
// destrcutor writing load balanced cartesian index back to vector
|
|
|
|
~GlobalIndexDataHandle()
|
|
|
|
{ finalize(); }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2021-12-01 07:00:21 -06:00
|
|
|
bool contains(int /* dim */, int codim) const
|
2019-02-01 10:33:30 -06:00
|
|
|
{ return codim == 0; }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2021-12-01 07:00:21 -06:00
|
|
|
bool fixedsize(int /* dim */, int /* codim */) const
|
2019-02-01 10:33:30 -06:00
|
|
|
{ return true; }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
//! \brief loop over all internal data handlers and call gather for
|
|
|
|
//! given entity
|
|
|
|
template<class MessageBufferImp, class EntityType>
|
|
|
|
void gather(MessageBufferImp& buff, const EntityType& element) const
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
int globalIdx = globalIndex_[element].index();
|
|
|
|
buff.write(globalIdx);
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
//! \brief loop over all internal data handlers and call scatter for
|
|
|
|
//! given entity
|
|
|
|
template<class MessageBufferImp, class EntityType>
|
2023-08-15 02:32:10 -05:00
|
|
|
void scatter(MessageBufferImp& buff, const EntityType& element, std::size_t /* n */)
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
int globalIdx = -1;
|
|
|
|
buff.read(globalIdx);
|
|
|
|
if (globalIdx >= 0)
|
|
|
|
{
|
|
|
|
globalIndex_.resize();
|
|
|
|
globalIndex_[element] = globalIdx;
|
|
|
|
}
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
//! \brief loop over all internal data handlers and return sum of data
|
|
|
|
//! size of given entity
|
|
|
|
template<class EntityType>
|
2023-08-15 02:32:10 -05:00
|
|
|
std::size_t size(const EntityType& /* en */) const
|
2019-02-01 10:33:30 -06:00
|
|
|
{ return 1; }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
protected:
|
|
|
|
// initialize persistent container from given vector
|
|
|
|
void initialize()
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
auto idx = cartesianIndex_.begin();
|
|
|
|
auto it = gridView_.template begin<0>();
|
|
|
|
const auto& endIt = gridView_.template end<0>();
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
for (; it != endIt; ++it, ++idx)
|
|
|
|
globalIndex_[*it] = *idx;
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
// update vector from given persistent container
|
|
|
|
void finalize()
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
std::vector<int> newIndex ;
|
|
|
|
newIndex.reserve(gridView_.indexSet().size(0));
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
auto it = gridView_.template begin<0>();
|
|
|
|
const auto& endIt = gridView_.template end<0>();
|
|
|
|
for (; it != endIt; ++it)
|
|
|
|
newIndex.push_back(globalIndex_[*it].index()) ;
|
|
|
|
|
|
|
|
cartesianIndex_.swap(newIndex);
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
GridView gridView_;
|
|
|
|
GlobalIndexContainer globalIndex_;
|
|
|
|
std::vector<int>& cartesianIndex_;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** \brief dimension of the grid */
|
2022-08-10 05:29:49 -05:00
|
|
|
static constexpr int dimension = Grid::dimension ;
|
2019-02-01 10:33:30 -06:00
|
|
|
|
|
|
|
/** \brief constructor taking grid */
|
2021-12-01 07:00:21 -06:00
|
|
|
CartesianIndexMapper(const Grid& grid,
|
|
|
|
const std::array<int, dimension>& cartDims,
|
|
|
|
const std::vector<int>& cartesianIndex)
|
2019-02-01 10:33:30 -06:00
|
|
|
: grid_(grid)
|
|
|
|
, cartesianDimensions_(cartDims)
|
|
|
|
, cartesianIndex_(cartesianIndex)
|
|
|
|
, cartesianSize_(computeCartesianSize())
|
|
|
|
{}
|
|
|
|
|
|
|
|
/** \brief return Cartesian dimensions, i.e. number of cells in each direction */
|
|
|
|
const std::array<int, dimension>& cartesianDimensions() const
|
|
|
|
{ return cartesianDimensions_; }
|
|
|
|
|
|
|
|
/** \brief return total number of cells in the logical Cartesian grid */
|
|
|
|
int cartesianSize() const
|
|
|
|
{ return cartesianSize_; }
|
|
|
|
|
|
|
|
/** \brief return number of cells in the active grid */
|
|
|
|
int compressedSize() const
|
|
|
|
{ return cartesianIndex_.size(); }
|
|
|
|
|
2023-11-20 08:56:17 -06:00
|
|
|
/** \brief return number of cells in the active grid. Only for unifying calls with CpGrid and PolyhedralGrid specializations. */
|
|
|
|
int compressedLevelZeroSize() const
|
|
|
|
{ return cartesianIndex_.size(); }
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
/** \brief return index of the cells in the logical Cartesian grid */
|
|
|
|
int cartesianIndex(const int compressedElementIndex) const
|
|
|
|
{
|
|
|
|
assert(compressedElementIndex < compressedSize());
|
|
|
|
return cartesianIndex_[compressedElementIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief return index of the cells in the logical Cartesian grid */
|
|
|
|
int cartesianIndex(const std::array<int, dimension>& coords) const
|
|
|
|
{
|
|
|
|
int cartIndex = coords[0];
|
|
|
|
int factor = cartesianDimensions()[0];
|
|
|
|
for (int i=1; i < dimension; ++i) {
|
|
|
|
cartIndex += coords[i] * factor;
|
|
|
|
factor *= cartesianDimensions()[i];
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
return cartIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief return Cartesian coordinate, i.e. IJK, for a given cell */
|
|
|
|
void cartesianCoordinate(const int compressedElementIndex, std::array<int, dimension>& coords) const
|
|
|
|
{
|
|
|
|
int gc = cartesianIndex(compressedElementIndex);
|
2022-08-10 05:29:49 -05:00
|
|
|
if constexpr (dimension == 3) {
|
2019-02-01 10:33:30 -06:00
|
|
|
coords[0] = gc % cartesianDimensions()[0];
|
|
|
|
gc /= cartesianDimensions()[0];
|
|
|
|
coords[1] = gc % cartesianDimensions()[1];
|
|
|
|
coords[2] = gc / cartesianDimensions()[1];
|
2015-09-10 08:55:54 -05:00
|
|
|
}
|
2022-08-10 05:29:49 -05:00
|
|
|
else if constexpr (dimension == 2) {
|
2019-02-01 10:33:30 -06:00
|
|
|
coords[0] = gc % cartesianDimensions()[0];
|
|
|
|
coords[1] = gc / cartesianDimensions()[0];
|
|
|
|
}
|
2022-08-10 05:29:49 -05:00
|
|
|
else if constexpr (dimension == 1)
|
2019-02-01 10:33:30 -06:00
|
|
|
coords[0] = gc ;
|
|
|
|
else
|
|
|
|
throw std::invalid_argument("cartesianCoordinate not implemented for dimension " + std::to_string(dimension));
|
|
|
|
}
|
|
|
|
|
2023-11-27 08:12:40 -06:00
|
|
|
/** \brief Only for unifying calls with CartesianIndexMapper<CpGrid> where levels are relevant */
|
|
|
|
void cartesianCoordinateLevel(const int compressedElementIndex, std::array<int, dimension>& coords, int level) const
|
|
|
|
{
|
|
|
|
if (level) {
|
|
|
|
throw std::invalid_argument("Invalid level.\n");
|
|
|
|
}
|
|
|
|
cartesianCoordinate(compressedElementIndex, coords);
|
|
|
|
}
|
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
template <class GridView>
|
|
|
|
std::unique_ptr<GlobalIndexDataHandle<GridView> > dataHandle(const GridView& gridView)
|
|
|
|
{
|
2022-08-10 05:29:49 -05:00
|
|
|
using DataHandle = GlobalIndexDataHandle<GridView>;
|
2019-02-01 10:33:30 -06:00
|
|
|
assert(&grid_ == &gridView.grid());
|
2020-09-02 08:35:39 -05:00
|
|
|
return std::make_unique<DataHandle>(gridView, cartesianIndex_);
|
2019-02-01 10:33:30 -06:00
|
|
|
}
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
protected:
|
|
|
|
int computeCartesianSize() const
|
|
|
|
{
|
|
|
|
int size = cartesianDimensions()[0];
|
|
|
|
for (int d=1; d<dimension; ++d)
|
|
|
|
size *= cartesianDimensions()[d];
|
|
|
|
return size ;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Grid& grid_;
|
|
|
|
const std::array<int, dimension> cartesianDimensions_;
|
|
|
|
std::vector<int> cartesianIndex_;
|
|
|
|
const int cartesianSize_ ;
|
|
|
|
};
|
|
|
|
|
2021-12-01 07:00:21 -06:00
|
|
|
} // end namespace Dune
|
2015-09-10 08:55:54 -05:00
|
|
|
|
|
|
|
#endif
|