47 #ifndef OPM_CPGRIDDATA_HEADER
48 #define OPM_CPGRIDDATA_HEADER
51 #include <opm/grid/utility/platform_dependent/disable_warnings.h>
53 #include <dune/common/version.hh>
54 #include <dune/common/parallel/mpihelper.hh>
56 #include <dune/istl/owneroverlapcopy.hh>
59 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 7)
60 #include <dune/common/parallel/communication.hh>
62 #include <dune/common/parallel/collectivecommunication.hh>
64 #include <dune/common/parallel/indexset.hh>
65 #include <dune/common/parallel/interface.hh>
66 #include <dune/common/parallel/plocalindex.hh>
67 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 7)
68 #include <dune/common/parallel/variablesizecommunicator.hh>
70 #include <opm/grid/utility/VariableSizeCommunicator.hpp>
72 #include <dune/grid/common/gridenums.hh>
74 #include <opm/grid/utility/platform_dependent/reenable_warnings.h>
77 #include <opm/input/eclipse/EclipseState/Grid/NNC.hpp>
85 #include "OrientedEntityTable.hpp"
86 #include "DefaultGeometryPolicy.hpp"
89 #include <opm/grid/utility/OpmParserIncludes.hpp>
91 #include "Entity2IndexDataHandle.hpp"
92 #include "DataHandleWrappers.hpp"
93 #include "GlobalIdMapping.hpp"
107 class LevelGlobalIdSet;
108 class PartitionTypeIndicator;
109 template<
int,
int>
class Geometry;
110 template<
int>
class Entity;
111 template<
int>
class EntityRep;
115 template<
class T,
int i>
struct Mover;
133 #ifndef MAX_DATA_COMMUNICATED_PER_ENTITY
153 explicit CpGridData(MPIHelper::MPICommunicator comm);
160 int size(
int codim)
const;
163 int size (GeometryType type)
const
166 return size(3 - type.dim());
186 void readEclipseFormat(
const std::string& filename,
bool periodic_extension,
bool turn_normals =
false);
197 void processEclipseFormat(
const Opm::Deck& deck,
bool periodic_extension,
bool turn_normals =
false,
bool clip_z =
false,
198 const std::vector<double>& poreVolume = std::vector<double>());
211 std::vector<std::size_t>
processEclipseFormat(
const Opm::EclipseGrid* ecl_grid, Opm::EclipseState* ecl_state,
212 bool periodic_extension,
bool turn_normals =
false,
bool clip_z =
false,
bool pinchActive =
true);
225 std::array<std::set<std::pair<int, int>>, 2>& nnc,
226 bool remove_ij_boundary,
bool turn_normals,
bool pinchActive);
235 void getIJK(
int c, std::array<int,3>& ijk)
const
237 int gc = global_cell_[c];
238 ijk[0] = gc % logical_cartesian_size_[0]; gc /= logical_cartesian_size_[0];
239 ijk[1] = gc % logical_cartesian_size_[1];
240 ijk[2] = gc / logical_cartesian_size_[1];
244 void computeUniqueBoundaryIds();
251 return use_unique_boundary_ids_;
258 use_unique_boundary_ids_ = uids;
259 if (use_unique_boundary_ids_ && unique_boundary_ids_.empty()) {
260 computeUniqueBoundaryIds();
283 return logical_cartesian_size_;
291 const std::vector<int>& cell_part);
298 template<
class DataHandle>
299 void communicate(DataHandle& data, InterfaceType iftype, CommunicationDirection dir);
302 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 7)
304 using Communicator = VariableSizeCommunicator<>;
307 using Communicator = Opm::VariableSizeCommunicator<>;
311 using InterfaceMap = Communicator::InterfaceMap;
314 using CommunicationType = Dune::OwnerOverlapCopyCommunication<int,int>;
317 using ParallelIndexSet = CommunicationType::ParallelIndexSet;
320 using RemoteIndices = Dune::RemoteIndices<ParallelIndexSet>;
325 CommunicationType& cellCommunication()
333 const CommunicationType& cellCommunication()
const
338 ParallelIndexSet& cellIndexSet()
340 return cellCommunication().indexSet();
343 const ParallelIndexSet& cellIndexSet()
const
345 return cellCommunication().indexSet();
348 RemoteIndices& cellRemoteIndices()
350 return cellCommunication().remoteIndices();
353 const RemoteIndices& cellRemoteIndices()
const
355 return cellCommunication().remoteIndices();
359 #ifdef HAVE_DUNE_ISTL
361 typedef Dune::OwnerOverlapCopyAttributeSet::AttributeSet
AttributeSet;
370 return aquifer_cells_;
376 void populateGlobalCellIndexSet();
385 template<
class DataHandle>
386 void gatherData(DataHandle& data,
CpGridData* global_view,
396 template<
int codim,
class DataHandle>
397 void gatherCodimData(DataHandle& data,
CpGridData* global_data,
406 template<
class DataHandle>
407 void scatterData(DataHandle& data,
CpGridData* global_data,
408 CpGridData* distributed_data,
const InterfaceMap& cell_inf,
409 const InterfaceMap& point_inf);
418 template<
int codim,
class DataHandle>
419 void scatterCodimData(DataHandle& data,
CpGridData* global_data,
430 template<
int codim,
class DataHandle>
432 const Interface& interface);
442 template<
int codim,
class DataHandle>
444 const InterfaceMap& interface);
448 void computeGeometry(
CpGrid& grid,
450 const std::vector<int>& globalAquiferCells,
453 std::vector<int>& aquiferCells,
455 const std::vector< std::array<int,8> >& cell2Points);
471 std::vector< std::array<int,8> > cell_to_point_;
478 std::array<int, 3> logical_cartesian_size_;
485 std::vector<int> global_cell_;
491 typedef FieldVector<double, 3> PointType;
506 typedef MPIHelper::MPICommunicator MPICommunicator;
507 typedef Dune::CollectiveCommunication<MPICommunicator> CollectiveCommunication;
509 CollectiveCommunication ccobj_;
512 bool use_unique_boundary_ids_;
519 std::vector<double> zcorn;
522 std::vector<int> aquifer_cells_;
527 CommunicationType cell_comm_;
530 std::tuple<Interface,Interface,Interface,Interface,Interface> cell_interfaces_;
539 std::tuple<InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap>
552 template<
int>
friend class Entity;
553 template<
int>
friend class EntityRep;
554 template<
int>
friend class EntityPointer;
555 friend class Intersection;
556 friend class PartitionTypeIndicator;
568 T& getInterface(InterfaceType iftype,
569 std::tuple<T,T,T,T,T>& interfaces)
574 return std::get<0>(interfaces);
576 return std::get<1>(interfaces);
578 return std::get<2>(interfaces);
580 return std::get<3>(interfaces);
582 return std::get<4>(interfaces);
584 OPM_THROW(std::runtime_error,
"Invalid Interface type was used during communication");
589 template<
int codim,
class DataHandle>
590 void CpGridData::communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data, CommunicationDirection dir,
591 const Interface& interface)
593 this->
template communicateCodim<codim>(data, dir, interface.interfaces());
596 template<
int codim,
class DataHandle>
597 void CpGridData::communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data_wrapper, CommunicationDirection dir,
598 const InterfaceMap& interface)
600 Communicator comm(ccobj_, interface);
602 if(dir==ForwardCommunication)
603 comm.forward(data_wrapper);
605 comm.backward(data_wrapper);
609 template<
class DataHandle>
611 CommunicationDirection dir)
614 if(data.contains(3,0))
617 communicateCodim<0>(data_wrapper, dir, getInterface(iftype, cell_interfaces_));
619 if(data.contains(3,3))
622 communicateCodim<3>(data_wrapper, dir, getInterface(iftype, point_interfaces_));
634 #include <opm/grid/cpgrid/Entity.hpp>
635 #include <opm/grid/cpgrid/Indexsets.hpp>
649 data=buffer_[index_++];
651 void write(
const T& data)
653 buffer_[index_++]=data;
659 void resize(std::size_t size)
661 buffer_.resize(size);
665 std::vector<T> buffer_;
666 typename std::vector<T>::size_type index_;
668 template<
class DataHandle,
int codim>
673 template<
class DataHandle>
676 BaseMover(DataHandle& data)
680 void moveData(
const E& from,
const E& to)
682 std::size_t size=data_.size(from);
684 data_.gather(buffer, from);
686 data_.scatter(buffer, to, size);
689 MoveBuffer<typename DataHandle::DataType> buffer;
693 template<
class DataHandle>
694 struct Mover<DataHandle,0> :
public BaseMover<DataHandle>
696 Mover<DataHandle,0>(DataHandle& data, CpGridData* gatherView,
697 CpGridData* scatterView)
698 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
701 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
703 Entity<0> from_entity=Entity<0>(*gatherView_, from_cell_index,
true);
704 Entity<0> to_entity=Entity<0>(*scatterView_, to_cell_index,
true);
705 this->moveData(from_entity, to_entity);
707 CpGridData* gatherView_;
708 CpGridData* scatterView_;
711 template<
class DataHandle>
712 struct Mover<DataHandle,1> :
public BaseMover<DataHandle>
714 Mover<DataHandle,1>(DataHandle& data, CpGridData* gatherView,
715 CpGridData* scatterView)
716 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
719 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
721 typedef typename OrientedEntityTable<0,1>::row_type row_type;
722 EntityRep<0> from_cell=EntityRep<0>(from_cell_index,
true);
723 EntityRep<0> to_cell=EntityRep<0>(to_cell_index,
true);
724 OrientedEntityTable<0,1>& table = gatherView_->cell_to_face_;
725 row_type from_faces=table.operator[](from_cell);
726 row_type to_faces=scatterView_->cell_to_face_[to_cell];
728 for(
int i=0; i<from_faces.size(); ++i)
729 this->moveData(from_faces[i], to_faces[i]);
731 CpGridData *gatherView_;
732 CpGridData *scatterView_;
735 template<
class DataHandle>
736 struct Mover<DataHandle,3> :
public BaseMover<DataHandle>
738 Mover<DataHandle,3>(DataHandle& data, CpGridData* gatherView,
739 CpGridData* scatterView)
740 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
742 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
744 const std::array<int,8>& from_cell_points=
745 gatherView_->cell_to_point_[from_cell_index];
746 const std::array<int,8>& to_cell_points=
747 scatterView_->cell_to_point_[to_cell_index];
748 for(std::size_t i=0; i<8; ++i)
750 this->moveData(Entity<3>(*gatherView_, from_cell_points[i],
true),
751 Entity<3>(*scatterView_, to_cell_points[i],
true));
754 CpGridData* gatherView_;
755 CpGridData* scatterView_;
760 template<
class DataHandle>
761 void CpGridData::scatterData(DataHandle& data, CpGridData* global_data,
762 CpGridData* distributed_data,
const InterfaceMap& cell_inf,
763 const InterfaceMap& point_inf)
766 if(data.contains(3,0))
768 Entity2IndexDataHandle<DataHandle, 0> data_wrapper(*global_data, *distributed_data, data);
769 communicateCodim<0>(data_wrapper, ForwardCommunication, cell_inf);
771 if(data.contains(3,3))
773 Entity2IndexDataHandle<DataHandle, 3> data_wrapper(*global_data, *distributed_data, data);
774 communicateCodim<3>(data_wrapper, ForwardCommunication, point_inf);
779 template<
int codim,
class DataHandle>
780 void CpGridData::scatterCodimData(DataHandle& data, CpGridData* global_data,
781 CpGridData* distributed_data)
783 CpGridData *gather_view, *scatter_view;
784 gather_view=global_data;
785 scatter_view=distributed_data;
787 mover::Mover<DataHandle,codim> mover(data, gather_view, scatter_view);
790 for(
auto index=distributed_data->cellIndexSet().begin(),
791 end = distributed_data->cellIndexSet().end();
794 std::size_t from=index->global();
795 std::size_t to=index->local();
803 template<
int codim,
class T,
class F>
804 void visitInterior(CpGridData& distributed_data, T begin, T endit, F& func)
806 for(T it=begin; it!=endit; ++it)
808 Entity<codim> entity(distributed_data, it-begin,
true);
809 PartitionType pt = entity.partitionType();
810 if(pt==Dune::InteriorEntity)
817 template<
class DataHandle>
818 struct GlobalIndexSizeGatherer
820 GlobalIndexSizeGatherer(DataHandle& data_,
821 std::vector<int>& ownedGlobalIndices_,
822 std::vector<int>& ownedSizes_)
823 : data(data_), ownedGlobalIndices(ownedGlobalIndices_), ownedSizes(ownedSizes_)
826 template<
class T,
class E>
827 void operator()(T& i, E& entity)
829 ownedGlobalIndices.push_back(i);
830 ownedSizes.push_back(data.size(entity));
833 std::vector<int>& ownedGlobalIndices;
834 std::vector<int>& ownedSizes;
837 template<
class DataHandle>
840 DataGatherer(mover::MoveBuffer<typename DataHandle::DataType>& buffer_,
842 : buffer(buffer_), data(data_)
845 template<
class T,
class E>
846 void operator()(T& , E& entity)
848 data.gather(buffer, entity);
850 mover::MoveBuffer<typename DataHandle::DataType>& buffer;
856 template<
class DataHandle>
857 void CpGridData::gatherData(DataHandle& data, CpGridData* global_data,
858 CpGridData* distributed_data)
861 if(data.contains(3,0))
862 gatherCodimData<0>(data, global_data, distributed_data);
863 if(data.contains(3,3))
864 gatherCodimData<3>(data, global_data, distributed_data);
868 template<
int codim,
class DataHandle>
869 void CpGridData::gatherCodimData(DataHandle& data, CpGridData* global_data,
870 CpGridData* distributed_data)
874 const std::vector<int>& mapping =
875 distributed_data->global_id_set_->getMapping<codim>();
879 std::vector<int> owned_global_indices;
880 std::vector<int> owned_sizes;
881 owned_global_indices.reserve(mapping.size());
882 owned_sizes.reserve(mapping.size());
884 GlobalIndexSizeGatherer<DataHandle> gisg(data, owned_global_indices, owned_sizes);
885 visitInterior<codim>(*distributed_data, mapping.begin(), mapping.end(), gisg);
888 int no_indices=owned_sizes.size();
891 if ( owned_global_indices.empty() )
892 owned_global_indices.resize(1);
893 if ( owned_sizes.empty() )
894 owned_sizes.resize(1);
895 std::vector<int> no_indices_to_recv(distributed_data->ccobj_.size());
896 distributed_data->ccobj_.allgather(&no_indices, 1, &(no_indices_to_recv[0]));
900 std::vector<int> displ(distributed_data->ccobj_.size()+1, 0);
901 std::transform(displ.begin(), displ.end()-1, no_indices_to_recv.begin(), displ.begin()+1,
903 int global_size=displ[displ.size()-1];
904 std::vector<int> global_indices(global_size);
905 std::vector<int> global_sizes(global_size);
906 MPI_Allgatherv(&(owned_global_indices[0]), no_indices, MPITraits<int>::getType(),
907 &(global_indices[0]), &(no_indices_to_recv[0]), &(displ[0]),
908 MPITraits<int>::getType(),
909 distributed_data->ccobj_);
910 MPI_Allgatherv(&(owned_sizes[0]), no_indices, MPITraits<int>::getType(),
911 &(global_sizes[0]), &(no_indices_to_recv[0]), &(displ[0]),
912 MPITraits<int>::getType(),
913 distributed_data->ccobj_);
914 std::vector<int>().swap(owned_global_indices);
916 std::vector<int> no_data_send(distributed_data->ccobj_.size());
917 for(
typename std::vector<int>::iterator begin=no_data_send.begin(),
918 i=begin, end=no_data_send.end(); i!=end; ++i)
919 *i = std::accumulate(global_sizes.begin()+displ[i-begin],
920 global_sizes.begin()+displ[i-begin+1], std::size_t());
922 std::vector<int>().swap(owned_sizes);
925 std::transform(displ.begin(), displ.end()-1, no_data_send.begin(), displ.begin()+1,
926 std::plus<std::size_t>());
928 int no_data_recv = displ[displ.size()-1];
931 mover::MoveBuffer<typename DataHandle::DataType> local_data_buffer, global_data_buffer;
932 if ( no_data_send[distributed_data->ccobj_.rank()] )
934 local_data_buffer.resize(no_data_send[distributed_data->ccobj_.rank()]);
938 local_data_buffer.resize(1);
940 global_data_buffer.resize(no_data_recv);
942 DataGatherer<DataHandle> gatherer(local_data_buffer, data);
943 visitInterior<codim>(*distributed_data, mapping.begin(), mapping.end(), gatherer);
944 MPI_Allgatherv(&(local_data_buffer.buffer_[0]), no_data_send[distributed_data->ccobj_.rank()],
945 MPITraits<typename DataHandle::DataType>::getType(),
946 &(global_data_buffer.buffer_[0]), &(no_data_send[0]), &(displ[0]),
947 MPITraits<typename DataHandle::DataType>::getType(),
948 distributed_data->ccobj_);
949 Entity2IndexDataHandle<DataHandle, codim> edata(*global_data, data);
951 for(
int i=0; i< codim; ++i)
952 offset+=global_data->size(i);
954 typename std::vector<int>::const_iterator s=global_sizes.begin();
955 for(
typename std::vector<int>::const_iterator i=global_indices.begin(),
956 end=global_indices.end();
959 edata.scatter(global_data_buffer, *i-offset, *s);
[ provides Dune::Grid ]
Definition: CpGrid.hpp:207
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:123
@ MAX_DATA_PER_CELL
The maximum data items allowed per cell (DUNE < 2.5.2)
Definition: CpGridData.hpp:140
int size(GeometryType type) const
number of leaf entities per geometry type in this process
Definition: CpGridData.hpp:163
void communicate(DataHandle &data, InterfaceType iftype, CommunicationDirection dir)
communicate objects for all codims on a given level
Definition: CpGridData.hpp:610
bool uniqueBoundaryIds() const
Is the grid currently using unique boundary ids?
Definition: CpGridData.hpp:249
CpGridData()
Constructor.
Definition: CpGridData.cpp:43
void readSintefLegacyFormat(const std::string &grid_prefix)
Read the Sintef legacy grid format ('topogeom').
Definition: readSintefLegacyFormat.cpp:66
int size(int codim) const
number of leaf entities per codim in this process
Definition: CpGridData.cpp:144
void readEclipseFormat(const std::string &filename, bool periodic_extension, bool turn_normals=false)
Read the Eclipse grid format ('grdecl').
~CpGridData()
Destructor.
Definition: CpGridData.cpp:93
void getIJK(int c, std::array< int, 3 > &ijk) const
Extract Cartesian index triplet (i,j,k) of an active cell.
Definition: CpGridData.hpp:235
const IndexSet & indexSet() const
Get the index set.
Definition: CpGridData.hpp:274
const std::array< int, 3 > & logicalCartesianSize() const
The logical cartesian size of the grid.
Definition: CpGridData.hpp:281
void distributeGlobalGrid(CpGrid &grid, const CpGridData &view_data, const std::vector< int > &cell_part)
Redistribute a global grid.
Definition: CpGridData.cpp:1553
void processEclipseFormat(const grdecl &input_data, Opm::EclipseState *ecl_state, std::array< std::set< std::pair< int, int >>, 2 > &nnc, bool remove_ij_boundary, bool turn_normals, bool pinchActive)
Read the Eclipse grid format ('grdecl').
Definition: processEclipseFormat.cpp:249
AttributeSet
The type of the set of the attributes.
Definition: CpGridData.hpp:364
void setUniqueBoundaryIds(bool uids)
Set whether we want to have unique boundary ids.
Definition: CpGridData.hpp:256
void writeSintefLegacyFormat(const std::string &grid_prefix) const
Write the Sintef legacy grid format ('topogeom').
Definition: writeSintefLegacyFormat.cpp:72
const std::vector< int > & sortedNumAquiferCells() const
Get sorted active cell indices of numerical aquifer.
Definition: CpGridData.hpp:368
const std::vector< double > & zcornData() const
Return the internalized zcorn copy from the grid processing, if no cells were adjusted during the min...
Definition: CpGridData.hpp:267
Definition: DefaultGeometryPolicy.hpp:49
const EntityVariable< cpgrid::Geometry< 3 - codim, 3 >, codim > & geomVector() const
Definition: DefaultGeometryPolicy.hpp:74
Wrapper that turns a data handle suitable for dune-grid into one based on integers instead of entitie...
Definition: Entity2IndexDataHandle.hpp:54
This class encapsulates geometry for both vertices, intersections and cells.
Definition: Geometry.hpp:68
The global id set for Dune.
Definition: Indexsets.hpp:325
Definition: Indexsets.hpp:198
Definition: Indexsets.hpp:54
Definition: Indexsets.hpp:257
Definition: PartitionTypeIndicator.hpp:50
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:29
Low-level corner-point processing routines and supporting data structures.
Definition: CpGridData.hpp:115
Raw corner-point specification of a particular geological model.
Definition: preprocess.h:56