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
|
|
|
|
2019-02-01 10:33:30 -06: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>
|
2019-02-01 10:33:30 -06:00
|
|
|
#include <memory>
|
2019-03-26 07:17:54 -05:00
|
|
|
#include <exception>
|
2016-02-22 11:43:13 -06:00
|
|
|
#include <vector>
|
2015-09-10 08:55:54 -05:00
|
|
|
#include <cassert>
|
|
|
|
|
2019-09-05 10:04:39 -05:00
|
|
|
namespace Opm {
|
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.
|
|
|
|
*/
|
|
|
|
template <class Grid>
|
|
|
|
class AluCartesianIndexMapper
|
2015-09-10 08:55:54 -05:00
|
|
|
{
|
2019-02-01 10:33:30 -06:00
|
|
|
public:
|
|
|
|
// 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
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
typedef typename Dune::PersistentContainer<Grid, GlobalCellIndex> GlobalIndexContainer;
|
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
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
bool contains(int dim, int codim) const
|
|
|
|
{ return codim == 0; }
|
2015-09-10 08:55:54 -05:00
|
|
|
|
2019-02-01 10:33:30 -06:00
|
|
|
bool fixedsize(int dim, int codim) const
|
|
|
|
{ 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>
|
|
|
|
void scatter(MessageBufferImp& buff, const EntityType& element, 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>
|
|
|
|
size_t size(const EntityType& en) const
|
|
|
|
{ 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 */
|
|
|
|
static const int dimension = Grid::dimension ;
|
|
|
|
|
|
|
|
/** \brief constructor taking grid */
|
|
|
|
AluCartesianIndexMapper(const Grid& grid,
|
|
|
|
const std::array<int, dimension>& cartDims,
|
|
|
|
const std::vector<int>& cartesianIndex)
|
|
|
|
: 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(); }
|
|
|
|
|
|
|
|
/** \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);
|
|
|
|
if (dimension == 3) {
|
|
|
|
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
|
|
|
}
|
2019-02-01 10:33:30 -06:00
|
|
|
else if (dimension == 2) {
|
|
|
|
coords[0] = gc % cartesianDimensions()[0];
|
|
|
|
coords[1] = gc / cartesianDimensions()[0];
|
|
|
|
}
|
|
|
|
else if (dimension == 1)
|
|
|
|
coords[0] = gc ;
|
|
|
|
else
|
|
|
|
throw std::invalid_argument("cartesianCoordinate not implemented for dimension " + std::to_string(dimension));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class GridView>
|
|
|
|
std::unique_ptr<GlobalIndexDataHandle<GridView> > dataHandle(const GridView& gridView)
|
|
|
|
{
|
|
|
|
typedef GlobalIndexDataHandle<GridView> DataHandle ;
|
|
|
|
assert(&grid_ == &gridView.grid());
|
|
|
|
return std::unique_ptr<DataHandle>(new DataHandle(gridView, cartesianIndex_));
|
|
|
|
}
|
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_ ;
|
|
|
|
};
|
|
|
|
|
2019-09-05 10:04:39 -05:00
|
|
|
} // end namespace Opm
|
2015-09-10 08:55:54 -05:00
|
|
|
|
|
|
|
#endif
|