CpGridData.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: CpGridData.hpp
4//
5// Created: Sep 17 21:11:41 2013
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Bård Skaflestad <bard.skaflestad@sintef.no>
9// Markus Blatt <markus@dr-blatt.de>
10// Antonella Ritorto <antonella.ritorto@opm-op.com>
11//
12// Comment: Major parts of this file originated in dune/grid/CpGrid.hpp
13// and got transfered here during refactoring for the parallelization.
14//
15// $Date$
16//
17// $Revision$
18//
19//===========================================================================
20
21/*
22 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
23 Copyright 2009, 2010, 2013, 2022-2023 Equinor ASA.
24 Copyright 2013 Dr. Blatt - HPC-Simulation-Software & Services
25
26 This file is part of The Open Porous Media project (OPM).
27
28 OPM is free software: you can redistribute it and/or modify
29 it under the terms of the GNU General Public License as published by
30 the Free Software Foundation, either version 3 of the License, or
31 (at your option) any later version.
32
33 OPM is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 GNU General Public License for more details.
37
38 You should have received a copy of the GNU General Public License
39 along with OPM. If not, see <http://www.gnu.org/licenses/>.
40*/
48#ifndef OPM_CPGRIDDATA_HEADER
49#define OPM_CPGRIDDATA_HEADER
50
51
52#include <dune/common/parallel/mpihelper.hh>
53#ifdef HAVE_DUNE_ISTL
54#include <dune/istl/owneroverlapcopy.hh>
55#endif
56
57#include <dune/common/parallel/communication.hh>
58#include <dune/common/parallel/variablesizecommunicator.hh>
59#include <dune/grid/common/gridenums.hh>
60
61#include <opm/input/eclipse/EclipseState/Grid/EclipseGrid.hpp>
62#include <opm/input/eclipse/EclipseState/Grid/NNC.hpp>
63
65
67#include "CpGridDataTraits.hpp"
68//#include "DataHandleWrappers.hpp"
69//#include "GlobalIdMapping.hpp"
70#include "Geometry.hpp"
71#include "LevelHierarchy.hpp"
72
73#include <array>
74#include <cstdint>
75#include <initializer_list>
76#include <set>
77#include <cstddef>
78#include <vector>
79
80namespace Opm
81{
82class EclipseState;
83namespace Refinement { struct GridStateWriter; struct RetainedCornerPointInput; }
84}
85namespace Dune
86{
87class CpGrid;
88
89namespace cpgrid
90{
91
92class IndexSet;
93class IdSet;
94class LevelGlobalIdSet;
95class PartitionTypeIndicator;
96class Intersection;
97template<int,int> class Geometry;
98template<int> class Entity;
99template<int> class EntityRep;
100}
101}
102
104 const std::array<int, 3>&,
105 bool);
106
107namespace Dune
108{
109namespace cpgrid
110{
117{
120
124
128 std::vector<Opm::NNCdata> pinchNnc;
129};
130} // namespace cpgrid
131
132namespace cpgrid
133{
134namespace mover
135{
136template<class T, int i> struct Mover;
137}
138
144{
145 template<class T, int i> friend struct mover::Mover;
146 friend class GlobalIdSet;
147 friend class HierarchicIterator;
151 friend struct ::Opm::Refinement::GridStateWriter;
152
153 friend
155 const std::array<int, 3>&,
156 bool);
157
158private:
159 CpGridData(const CpGridData& g);
160
161public:
162 enum{
163#ifndef MAX_DATA_COMMUNICATED_PER_ENTITY
171#else
176 MAX_DATA_PER_CELL = MAX_DATA_COMMUNICATED_PER_ENTITY
177#endif
178 };
179
180 CpGridData() = delete;
181
186 explicit CpGridData(MPIHelper::MPICommunicator comm, const std::vector<std::shared_ptr<CpGridData>>& data);
187
188
189
191 explicit CpGridData(const std::vector<std::shared_ptr<CpGridData>>& data);
194
195
196
197
199 int size(int codim) const;
200
202 int size (GeometryType type) const
203 {
204 if (type.isCube()) {
205 return size(3 - type.dim());
206 } else {
207 return 0;
208 }
209 }
210
226 void readEclipseFormat(const std::string& filename,
227 bool periodic_extension,
228 bool turn_normals = false,
229 bool edge_conformal = false);
230
252 void processEclipseFormat(const Opm::Deck& deck,
253 bool periodic_extension,
254 bool turn_normals = false,
255 bool clip_z = false,
256 const std::vector<double>& poreVolume = std::vector<double>{},
257 bool edge_conformal = false);
258
301 std::vector<std::size_t>
302 processEclipseFormat(const Opm::EclipseGrid* ecl_grid,
303 Opm::EclipseState* ecl_state,
304 bool periodic_extension,
305 bool turn_normals = false,
306 bool clip_z = false,
307 bool pinchActive = true,
308 bool edge_conformal = false,
309 ContinuumSlice* continuum = nullptr);
310
339 void processEclipseFormat(const grdecl& input_data,
340 Opm::EclipseState* ecl_state,
341 std::array<std::set<std::pair<int, int>>, 2>& nnc,
342 bool remove_ij_boundary,
343 bool turn_normals,
344 bool pinchActive,
345 double tolerance_unique_points,
346 bool edge_conformal);
347
355 void getIJK(int c, std::array<int,3>& ijk) const;
356
357 int cellFace(int cell, int local_index) const
358 {
359 return cell_to_face_[cpgrid::EntityRep<0>(cell, true)][local_index].index();
360 }
361
362 auto cellToFace(int cellIdx) const
363 {
364 return cell_to_face_[cpgrid::EntityRep<0>(cellIdx, true)];
365 }
366
367 const auto& cellToPoint() const
368 {
369 return cell_to_point_;
370 }
371
372 const auto& cellToPoint(int cellIdx) const
373 {
374 return cell_to_point_[cellIdx];
375 }
376
377 int faceToCellSize(int face) const {
378 Dune::cpgrid::EntityRep<1> faceRep(face, true);
379 return face_to_cell_[faceRep].size();
380 }
381
382 auto faceTag(int faceIdx) const
383 {
384 Dune::cpgrid::EntityRep<1> faceRep(faceIdx, true);
385 return face_tag_[faceRep];
386 }
387
388 auto faceNormals(int faceIdx) const
389 {
390 Dune::cpgrid::EntityRep<1> faceRep(faceIdx, true);
391 return face_normals_[faceRep];
392 }
393
394 auto faceToPoint(int faceIdx) const
395 {
396 return face_to_point_[faceIdx];
397 }
398
399 int numFaces() const
400 {
401 return face_to_cell_.size();
402 }
403
404 auto cornerHistorySize() const
405 {
406 return levels_.corner_history.size();
407 }
408
409 const auto& getCornerHistory(int cornerIdx) const
410 {
411 if(cornerHistorySize()) {
412 return levels_.corner_history[cornerIdx];
413 }
414 else {
415 OPM_THROW(std::logic_error, "Vertex has no history record.\n");
416#if defined(__CUDACC__)
417 __builtin_unreachable();
418#endif
419 }
420 }
421
423 std::vector<std::int64_t> stableCellId() const;
424
431 const std::vector<int>& globalCell() const
432 {
433 return global_cell_;
434 }
435
440 std::size_t continuumCartOffset() const
441 {
442 return continuum_cart_offset_;
443 }
444
447 bool hasNNCs(const std::vector<int>& cellIndices) const;
448
462 bool mark(int refCount, const cpgrid::Entity<0>& element, bool throwOnFailure = false);
463
467 int getMark(const cpgrid::Entity<0>& element) const;
468
478 bool preAdapt();
479
481 bool adapt();
482
484 void postAdapt();
485
486private:
487 std::array<Dune::FieldVector<double,3>,8> getReferenceRefinedCorners(int idx_in_parent_cell, const std::array<int,3>& cells_per_dim) const;
488
489public:
491 int getGridIdx() const {
492 // Not the nicest way of checking if "this" points at the leaf grid view of a mixed grid (with coarse and refined cells).
493 // 1. When the grid has been refined at least onece, level_data_ptr_ ->size() >1. Therefore, there is a chance of "this" pointing at the leaf grid view.
494 // 2. Unfortunately, level_ is default initialized by 0. This implies, in particular, that if someone wants to check the value of
495 // "this->level_" when "this" points at the leaf grid view of a grid that has been refined, this value is - unfortunately - equal to 0.
496 // 3. Due to 2. we need an extra bool value to distinguish between the actual level 0 grid and such a leaf grid view (with incorrect level_ == 0). For this
497 // reason we check if child_to_parent_cells_.empty() [true for actual level 0 grid, false for the leaf grid view].
498 // --- TO BE IMPROVED ---
499 if ((levels_.level_data_ptr ->size() >1) && (levels_.level == 0) && (!levels_.child_to_parent_cells.empty())) {
500 return levels_.level_data_ptr->size() -1;
501 }
502 return levels_.level;
503 }
505 const std::vector<std::shared_ptr<Dune::cpgrid::CpGridData>>& levelData() const
506 {
507 if (levels_.level_data_ptr->empty()) {
508 OPM_THROW(std::logic_error, "Level data has not been initialized\n");
509 }
510 return *levels_.level_data_ptr;
511 }
512
520 const std::tuple<int,std::vector<int>>& getChildrenLevelAndIndexList(int elemIdx) const {
521 return levels_.parent_to_children_cells[elemIdx];
522 }
523
524 const std::vector<std::tuple<int,std::vector<int>>>& getParentToChildren() const {
525 return levels_.parent_to_children_cells;
526 }
527
529 {
530 return geometry_;
531 }
532
533 int getLeafIdxFromLevelIdx(int level_cell_idx) const
534 {
535 if (levels_.level_to_leaf_cells.empty()) {
536 OPM_THROW(std::logic_error, "Grid has no LGRs. No mapping to the leaf.\n");
537 }
538 return levels_.level_to_leaf_cells[level_cell_idx];
539 }
540
562 std::tuple< const std::shared_ptr<CpGridData>,
563 const std::vector<std::array<int,2>>> // parent_to_refined_corners(~boundary_old_to_new_corners)
564 refineSingleCell(const std::array<int,3>& cells_per_dim,
565 const int& parent_idx,
566 std::vector<std::vector<std::pair<int, std::vector<int>>>>& faceInMarkedElemAndRefinedFaces) const;
567
568 // @breif Compute center of an entity/element/cell in the Eclipse way:
569 // - Average of the 4 corners of the bottom face.
570 // - Average of the 4 corners of the top face.
571 // Return average of the previous computations.
572 // @param [in] int Index of a cell.
573 // @return 'eclipse centroid'
574 std::array<double,3> computeEclCentroid(const int idx) const;
575
576 // @breif Compute center of an entity/element/cell in the Eclipse way:
577 // - Average of the 4 corners of the bottom face.
578 // - Average of the 4 corners of the top face.
579 // Return average of the previous computations.
580 // @param [in] Entity<0> Entity
581 // @return 'eclipse centroid'
582 std::array<double,3> computeEclCentroid(const Entity<0>& elem) const;
583
584 // Make unique boundary ids for all intersections.
586
590 bool uniqueBoundaryIds() const
591 {
592 return use_unique_boundary_ids_;
593 }
594
597 void setUniqueBoundaryIds(bool uids)
598 {
599 use_unique_boundary_ids_ = uids;
600 if (use_unique_boundary_ids_ && unique_boundary_ids_.empty()) {
602 }
603 }
604
608 const std::vector<double>& zcornData() const {
609 return zcorn;
610 }
611
628 void appendDisjointGrid(const CpGridData& other);
629
642 void appendNncFaces(const std::vector<std::pair<int, int>>& cell_pairs);
643
644
647 const IndexSet& indexSet() const
648 {
649 return *index_set_;
650 }
651
654 {
655 return *local_id_set_;
656 }
657
660 {
661 return *global_id_set_;
662 }
663
667 const std::array<int, 3>& logicalCartesianSize() const
668 {
669 return logical_cartesian_size_;
670 }
671
676 const CpGridData& view_data,
677 const std::vector<int>& cell_part);
678
684 template<class DataHandle>
685 void communicate(DataHandle& data, InterfaceType iftype, CommunicationDirection dir);
686
688
690
691 void computeCommunicationInterfaces(int noexistingPoints);
692
698
701#if HAVE_MPI
704
707
710
713
716
721 {
722 return cell_comm_;
723 }
724
729 {
730 return cell_comm_;
731 }
732
734 {
735 return cellCommunication().indexSet();
736 }
737
739 {
740 return cellCommunication().indexSet();
741 }
742
744 {
745 return cellCommunication().remoteIndices();
746 }
747
749 {
750 return cellCommunication().remoteIndices();
751 }
752#endif
753
755 const std::vector<int>& sortedNumAquiferCells() const
756 {
757 return aquifer_cells_;
758 }
759
760private:
761
763 void populateGlobalCellIndexSet();
764
765#if HAVE_MPI
766
772 template<class DataHandle>
773 void gatherData(DataHandle& data, CpGridData* global_view,
774 CpGridData* distributed_view);
775
776
783 template<int codim, class DataHandle>
784 void gatherCodimData(DataHandle& data, CpGridData* global_data,
785 CpGridData* distributed_data);
786
793 template<class DataHandle>
794 void scatterData(DataHandle& data, const CpGridData* global_data,
795 const CpGridData* distributed_data, const InterfaceMap& cell_inf,
796 const InterfaceMap& point_inf);
797
805 template<int codim, class DataHandle>
806 void scatterCodimData(DataHandle& data, CpGridData* global_data,
807 CpGridData* distributed_data);
808
817 template<int codim, class DataHandle>
818 void communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data, CommunicationDirection dir,
819 const Interface& interface);
820
829 template<int codim, class DataHandle>
830 void communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data, CommunicationDirection dir,
831 const InterfaceMap& interface);
832
833#endif
834
835 void computeGeometry(const CpGrid& grid,
836 const DefaultGeometryPolicy& globalGeometry,
837 const std::vector<int>& globalAquiferCells,
838 const OrientedEntityTable<0, 1>& globalCell2Faces,
839 DefaultGeometryPolicy& geometry,
840 std::vector<int>& aquiferCells,
842 const std::vector< std::array<int,8> >& cell2Points);
843
844 // Representing the topology
856 Opm::SparseTable<int> face_to_point_;
858 std::vector< std::array<int,8> > cell_to_point_;
865 std::array<int, 3> logical_cartesian_size_{};
872 std::vector<int> global_cell_;
873
877 std::size_t continuum_cart_offset_{0};
879 cpgrid::EntityVariable<enum face_tag, 1> face_tag_;
881 cpgrid::DefaultGeometryPolicy geometry_;
883 typedef FieldVector<double, 3> PointType;
885 cpgrid::SignedEntityVariable<PointType, 1> face_normals_;
887 cpgrid::EntityVariable<int, 1> unique_boundary_ids_;
889 std::unique_ptr<cpgrid::IndexSet> index_set_;
891 std::shared_ptr<const cpgrid::IdSet> local_id_set_;
893 std::shared_ptr<LevelGlobalIdSet> global_id_set_;
895 std::shared_ptr<PartitionTypeIndicator> partition_type_indicator_;
897 LevelHierarchy levels_;
898
900 Communication ccobj_;
901
902 // Boundary information (optional).
903 bool use_unique_boundary_ids_;
904
910 std::vector<double> zcorn;
911
913 std::vector<int> aquifer_cells_;
914
916 std::shared_ptr<const Opm::Refinement::RetainedCornerPointInput> retained_cp_input_;
917 bool retain_cp_input_{false};
918
919#if HAVE_MPI
920
922 CommunicationType cell_comm_;
923
925 std::tuple<Interface,Interface,Interface,Interface,Interface> cell_interfaces_;
926 /*
927 // code deactivated, because users cannot access face indices and therefore
928 // communication on faces makes no sense!
930 std::tuple<InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap>
931 face_interfaces_;
932 */
934 std::tuple<InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap,InterfaceMap>
935 point_interfaces_;
936
937#endif
938
939 // Return the geometry vector corresponding to the given codim.
940 template <int codim>
941 const EntityVariable<Geometry<3 - codim, 3>, codim>& geomVector() const
942 {
943 return geometry_.geomVector<codim>();
944 }
945
946 friend class Dune::CpGrid;
947 template<int> friend class Entity;
948 template<int> friend class EntityRep;
949 friend class Intersection;
951};
952
953
954
955#if HAVE_MPI
956
957namespace
958{
963template<class T>
964T& getInterface(InterfaceType iftype,
965 std::tuple<T,T,T,T,T>& interfaces)
966{
967 switch(iftype)
968 {
969 case 0:
970 return std::get<0>(interfaces);
971 case 1:
972 return std::get<1>(interfaces);
973 case 2:
974 return std::get<2>(interfaces);
975 case 3:
976 return std::get<3>(interfaces);
977 case 4:
978 return std::get<4>(interfaces);
979 }
980 OPM_THROW(std::runtime_error, "Invalid Interface type was used during communication");
981}
982
983} // end unnamed namespace
984
985template<int codim, class DataHandle>
986void CpGridData::communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data, CommunicationDirection dir,
987 const Interface& interface)
988{
989 this->template communicateCodim<codim>(data, dir, interface.interfaces());
990}
991
992template<int codim, class DataHandle>
993void CpGridData::communicateCodim(Entity2IndexDataHandle<DataHandle, codim>& data_wrapper, CommunicationDirection dir,
994 const InterfaceMap& interface)
995{
996 Communicator comm(ccobj_, interface);
997
998 if(dir==ForwardCommunication)
999 comm.forward(data_wrapper);
1000 else
1001 comm.backward(data_wrapper);
1002}
1003#endif
1004
1005template<class DataHandle>
1006void CpGridData::communicate(DataHandle& data, InterfaceType iftype,
1007 CommunicationDirection dir)
1008{
1009#if HAVE_MPI
1010 if(data.contains(3,0))
1011 {
1012 Entity2IndexDataHandle<DataHandle, 0> data_wrapper(*this, data);
1013 communicateCodim<0>(data_wrapper, dir, getInterface(iftype, cell_interfaces_));
1014 }
1015 if(data.contains(3,3))
1016 {
1017 Entity2IndexDataHandle<DataHandle, 3> data_wrapper(*this, data);
1018 communicateCodim<3>(data_wrapper, dir, getInterface(iftype, point_interfaces_));
1019 }
1020#else
1021 // Suppress warnings for unused arguments.
1022 (void) data;
1023 (void) iftype;
1024 (void) dir;
1025#endif
1026}
1027}}
1028
1029#if HAVE_MPI
1032
1033namespace Dune {
1034namespace cpgrid {
1035
1036namespace mover
1037{
1038template<class T>
1040{
1042public:
1043 void read(T& data)
1044 {
1045 data=buffer_[index_++];
1046 }
1047 void write(const T& data)
1048 {
1049 buffer_[index_++]=data;
1050 }
1051 void reset()
1052 {
1053 index_=0;
1054 }
1055 void resize(std::size_t size)
1056 {
1057 buffer_.resize(size);
1058 index_=0;
1059 }
1060private:
1061 std::vector<T> buffer_;
1062 typename std::vector<T>::size_type index_;
1063};
1064template<class DataHandle,int codim>
1065struct Mover
1066{
1067};
1068
1069template<class DataHandle>
1071{
1072 explicit BaseMover(DataHandle& data)
1073 : data_(data)
1074 {}
1075 template<class E>
1076 void moveData(const E& from, const E& to)
1077 {
1078 std::size_t size=data_.size(from);
1079 buffer.resize(size);
1080 data_.gather(buffer, from);
1081 buffer.reset();
1082 data_.scatter(buffer, to, size);
1083 }
1084 DataHandle& data_;
1086};
1087
1088
1089template<class DataHandle>
1091{
1092 Mover(DataHandle& data, CpGridData* gatherView,
1093 CpGridData* scatterView)
1094 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
1095 {}
1096
1097 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
1098 {
1099 Entity<0> from_entity=Entity<0>(*gatherView_, from_cell_index, true);
1100 Entity<0> to_entity=Entity<0>(*scatterView_, to_cell_index, true);
1101 this->moveData(from_entity, to_entity);
1102 }
1105};
1106
1107template<class DataHandle>
1109{
1110 Mover(DataHandle& data, CpGridData* gatherView,
1111 CpGridData* scatterView)
1112 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
1113 {}
1114
1115 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
1116 {
1117 typedef typename OrientedEntityTable<0,1>::row_type row_type;
1118 EntityRep<0> from_cell=EntityRep<0>(from_cell_index, true);
1119 EntityRep<0> to_cell=EntityRep<0>(to_cell_index, true);
1120 const OrientedEntityTable<0,1>& table = gatherView_->cell_to_face_;
1121 row_type from_faces=table.operator[](from_cell);
1122 row_type to_faces=scatterView_->cell_to_face_[to_cell];
1123
1124 for(int i=0; i<from_faces.size(); ++i)
1125 this->moveData(from_faces[i], to_faces[i]);
1126 }
1129};
1130
1131template<class DataHandle>
1133{
1134 Mover(DataHandle& data, CpGridData* gatherView,
1135 CpGridData* scatterView)
1136 : BaseMover<DataHandle>(data), gatherView_(gatherView), scatterView_(scatterView)
1137 {}
1138 void operator()(std::size_t from_cell_index,std::size_t to_cell_index)
1139 {
1140 const std::array<int,8>& from_cell_points=
1141 gatherView_->cell_to_point_[from_cell_index];
1142 const std::array<int,8>& to_cell_points=
1143 scatterView_->cell_to_point_[to_cell_index];
1144 for(std::size_t i=0; i<8; ++i)
1145 {
1146 this->moveData(Entity<3>(*gatherView_, from_cell_points[i], true),
1147 Entity<3>(*scatterView_, to_cell_points[i], true));
1148 }
1149 }
1152};
1153
1154} // end mover namespace
1155
1156template<class DataHandle>
1157void CpGridData::scatterData(DataHandle& data, const CpGridData* global_data,
1158 const CpGridData* distributed_data, const InterfaceMap& cell_inf,
1159 const InterfaceMap& point_inf)
1160{
1161#if HAVE_MPI
1162 if(data.contains(3,0))
1163 {
1164 Entity2IndexDataHandle<DataHandle, 0> data_wrapper(*global_data, *distributed_data, data);
1165 communicateCodim<0>(data_wrapper, ForwardCommunication, cell_inf);
1166 }
1167 if(data.contains(3,3))
1168 {
1169 Entity2IndexDataHandle<DataHandle, 3> data_wrapper(*global_data, *distributed_data, data);
1170 communicateCodim<3>(data_wrapper, ForwardCommunication, point_inf);
1171 }
1172#endif
1173}
1174
1175template<int codim, class DataHandle>
1176void CpGridData::scatterCodimData(DataHandle& data, CpGridData* global_data,
1177 CpGridData* distributed_data)
1178{
1179 CpGridData *gather_view, *scatter_view;
1180 gather_view=global_data;
1181 scatter_view=distributed_data;
1182
1183 mover::Mover<DataHandle,codim> mover(data, gather_view, scatter_view);
1184
1185
1186 for(auto index=distributed_data->cellIndexSet().begin(),
1187 end = distributed_data->cellIndexSet().end();
1188 index!=end; ++index)
1189 {
1190 std::size_t from=index->global();
1191 std::size_t to=index->local();
1192 mover(from,to);
1193 }
1194}
1195
1196namespace
1197{
1198
1199template<int codim, class T, class F>
1200void visitInterior(CpGridData& distributed_data, T begin, T endit, F& func)
1201{
1202 for(T it=begin; it!=endit; ++it)
1203 {
1204 Entity<codim> entity(distributed_data, it-begin, true);
1205 PartitionType pt = entity.partitionType();
1206 if(pt==Dune::InteriorEntity)
1207 {
1208 func(*it, entity);
1209 }
1210 }
1211}
1212
1213template<class DataHandle>
1214struct GlobalIndexSizeGatherer
1215{
1216 GlobalIndexSizeGatherer(DataHandle& data_,
1217 std::vector<int>& ownedGlobalIndices_,
1218 std::vector<int>& ownedSizes_)
1219 : data(data_), ownedGlobalIndices(ownedGlobalIndices_), ownedSizes(ownedSizes_)
1220 {}
1221
1222 template<class T, class E>
1223 void operator()(T& i, E& entity)
1224 {
1225 ownedGlobalIndices.push_back(i);
1226 ownedSizes.push_back(data.size(entity));
1227 }
1228 DataHandle& data;
1229 std::vector<int>& ownedGlobalIndices;
1230 std::vector<int>& ownedSizes;
1231};
1232
1233template<class DataHandle>
1234struct DataGatherer
1235{
1236 DataGatherer(mover::MoveBuffer<typename DataHandle::DataType>& buffer_,
1237 DataHandle& data_)
1238 : buffer(buffer_), data(data_)
1239 {}
1240
1241 template<class T, class E>
1242 void operator()(T& /* it */, E& entity)
1243 {
1244 data.gather(buffer, entity);
1245 }
1246 mover::MoveBuffer<typename DataHandle::DataType>& buffer;
1247 DataHandle& data;
1248};
1249
1250}
1251
1252template<class DataHandle>
1253void CpGridData::gatherData(DataHandle& data, CpGridData* global_data,
1254 CpGridData* distributed_data)
1255{
1256#if HAVE_MPI
1257 if(data.contains(3,0))
1258 gatherCodimData<0>(data, global_data, distributed_data);
1259 if(data.contains(3,3))
1260 gatherCodimData<3>(data, global_data, distributed_data);
1261#endif
1262}
1263
1264template<int codim, class DataHandle>
1265void CpGridData::gatherCodimData(DataHandle& data, CpGridData* global_data,
1266 CpGridData* distributed_data)
1267{
1268#if HAVE_MPI
1269 // Get the mapping to global index from the global id set
1270 const std::vector<int>& mapping =
1271 distributed_data->global_id_set_->getMapping<codim>();
1272
1273 // Get the global indices and data size for the entities whose data is
1274 // to be sent, i.e. the ones that we own.
1275 std::vector<int> owned_global_indices;
1276 std::vector<int> owned_sizes;
1277 owned_global_indices.reserve(mapping.size());
1278 owned_sizes.reserve(mapping.size());
1279
1280 GlobalIndexSizeGatherer<DataHandle> gisg(data, owned_global_indices, owned_sizes);
1281 visitInterior<codim>(*distributed_data, mapping.begin(), mapping.end(), gisg);
1282
1283 // communicate the number of indices that each processor sends
1284 int no_indices=owned_sizes.size();
1285 // We will take the address of the first elemet for MPI_Allgather below.
1286 // Make sure the containers have such an element.
1287 if ( owned_global_indices.empty() )
1288 owned_global_indices.resize(1);
1289 if ( owned_sizes.empty() )
1290 owned_sizes.resize(1);
1291 std::vector<int> no_indices_to_recv(distributed_data->ccobj_.size());
1292 distributed_data->ccobj_.allgather(&no_indices, 1, &(no_indices_to_recv[0]));
1293 // compute size of the vector capable for receiving all indices
1294 // and allgather the global indices and the sizes.
1295 // calculate displacements
1296 std::vector<int> displ(distributed_data->ccobj_.size()+1, 0);
1297 std::transform(displ.begin(), displ.end()-1, no_indices_to_recv.begin(), displ.begin()+1,
1298 std::plus<int>());
1299 int global_size=displ[displ.size()-1];//+no_indices_to_recv[displ.size()-1];
1300 std::vector<int> global_indices(global_size);
1301 std::vector<int> global_sizes(global_size);
1302 MPI_Allgatherv(&(owned_global_indices[0]), no_indices, MPITraits<int>::getType(),
1303 &(global_indices[0]), &(no_indices_to_recv[0]), &(displ[0]),
1304 MPITraits<int>::getType(),
1305 distributed_data->ccobj_);
1306 MPI_Allgatherv(&(owned_sizes[0]), no_indices, MPITraits<int>::getType(),
1307 &(global_sizes[0]), &(no_indices_to_recv[0]), &(displ[0]),
1308 MPITraits<int>::getType(),
1309 distributed_data->ccobj_);
1310 std::vector<int>().swap(owned_global_indices); // free data for reuse.
1311 // Compute the number of data items to send
1312 std::vector<int> no_data_send(distributed_data->ccobj_.size());
1313 for(typename std::vector<int>::iterator begin=no_data_send.begin(),
1314 i=begin, end=no_data_send.end(); i!=end; ++i)
1315 *i = std::accumulate(global_sizes.begin()+displ[i-begin],
1316 global_sizes.begin()+displ[i-begin+1], std::size_t());
1317 // free at least some memory that can be reused.
1318 std::vector<int>().swap(owned_sizes);
1319 // compute the displacements for receiving with allgatherv
1320 displ[0]=0;
1321 std::transform(displ.begin(), displ.end()-1, no_data_send.begin(), displ.begin()+1,
1322 std::plus<std::size_t>());
1323 // Compute the number of data items we will receive
1324 int no_data_recv = displ[displ.size()-1];//+global_sizes[displ.size()-1];
1325
1326 // Collect the data to send, gather it
1327 mover::MoveBuffer<typename DataHandle::DataType> local_data_buffer, global_data_buffer;
1328 if ( no_data_send[distributed_data->ccobj_.rank()] )
1329 {
1330 local_data_buffer.resize(no_data_send[distributed_data->ccobj_.rank()]);
1331 }
1332 else
1333 {
1334 local_data_buffer.resize(1);
1335 }
1336 global_data_buffer.resize(no_data_recv);
1337
1338 DataGatherer<DataHandle> gatherer(local_data_buffer, data);
1339 visitInterior<codim>(*distributed_data, mapping.begin(), mapping.end(), gatherer);
1340 MPI_Allgatherv(&(local_data_buffer.buffer_[0]), no_data_send[distributed_data->ccobj_.rank()],
1341 MPITraits<typename DataHandle::DataType>::getType(),
1342 &(global_data_buffer.buffer_[0]), &(no_data_send[0]), &(displ[0]),
1343 MPITraits<typename DataHandle::DataType>::getType(),
1344 distributed_data->ccobj_);
1345 Entity2IndexDataHandle<DataHandle, codim> edata(*global_data, data);
1346 int offset=0;
1347 for(int i=0; i< codim; ++i)
1348 offset+=global_data->size(i);
1349
1350 typename std::vector<int>::const_iterator s=global_sizes.begin();
1351 for(typename std::vector<int>::const_iterator i=global_indices.begin(),
1352 end=global_indices.end();
1353 i!=end; ++s, ++i)
1354 {
1355 edata.scatter(global_data_buffer, *i-offset, *s);
1356 }
1357#endif
1358}
1359
1360} // end namespace cpgrid
1361} // end namespace Dune
1362
1363#endif
1364
1365#endif
DataHandle & data
Definition: CpGridData.hpp:1228
mover::MoveBuffer< typename DataHandle::DataType > & buffer
Definition: CpGridData.hpp:1246
std::vector< int > & ownedGlobalIndices
Definition: CpGridData.hpp:1229
void refine_and_check(const Dune::cpgrid::Geometry< 3, 3 > &, const std::array< int, 3 > &, bool)
std::vector< int > & ownedSizes
Definition: CpGridData.hpp:1230
[ provides Dune::Grid ]
Definition: CpGrid.hpp:206
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:144
auto faceToPoint(int faceIdx) const
Definition: CpGridData.hpp:394
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, double tolerance_unique_points, bool edge_conformal)
void postAdapt()
Clean up refinement/coarsening markers - set every element to the mark 0 which represents 'doing noth...
const cpgrid::LevelGlobalIdSet & globalIdSet() const
Get the global index set.
Definition: CpGridData.hpp:659
CpGridDataTraits::CommunicationType CommunicationType
type of OwnerOverlap communication for cells
Definition: CpGridData.hpp:709
@ MAX_DATA_PER_CELL
The maximum data items allowed per cell (DUNE < 2.5.2)
Definition: CpGridData.hpp:170
void appendNncFaces(const std::vector< std::pair< int, int > > &cell_pairs)
CpGridDataTraits::ParallelIndexSet ParallelIndexSet
The type of the parallel index set.
Definition: CpGridData.hpp:712
int size(GeometryType type) const
number of leaf entities per geometry type in this process
Definition: CpGridData.hpp:202
const std::array< int, 3 > & logicalCartesianSize() const
Definition: CpGridData.hpp:667
void processEclipseFormat(const Opm::Deck &deck, bool periodic_extension, bool turn_normals=false, bool clip_z=false, const std::vector< double > &poreVolume=std::vector< double >{}, bool edge_conformal=false)
void communicate(DataHandle &data, InterfaceType iftype, CommunicationDirection dir)
communicate objects for all codims on a given level
Definition: CpGridData.hpp:1006
std::size_t continuumCartOffset() const
Definition: CpGridData.hpp:440
auto faceTag(int faceIdx) const
Definition: CpGridData.hpp:382
bool uniqueBoundaryIds() const
Definition: CpGridData.hpp:590
const std::tuple< int, std::vector< int > > & getChildrenLevelAndIndexList(int elemIdx) const
Retrieves the level and child indices of a given parent cell.
Definition: CpGridData.hpp:520
std::array< double, 3 > computeEclCentroid(const Entity< 0 > &elem) const
void appendDisjointGrid(const CpGridData &other)
void computeCommunicationInterfaces(int noexistingPoints)
int getLeafIdxFromLevelIdx(int level_cell_idx) const
Definition: CpGridData.hpp:533
const auto & getCornerHistory(int cornerIdx) const
Definition: CpGridData.hpp:409
RemoteIndices & cellRemoteIndices()
Definition: CpGridData.hpp:743
int size(int codim) const
number of leaf entities per codim in this process
void readEclipseFormat(const std::string &filename, bool periodic_extension, bool turn_normals=false, bool edge_conformal=false)
CpGridDataTraits::CollectiveCommunication CollectiveCommunication
Definition: CpGridData.hpp:697
const std::vector< int > & globalCell() const
Definition: CpGridData.hpp:431
CpGridDataTraits::InterfaceMap InterfaceMap
The type of the map describing communication interfaces.
Definition: CpGridData.hpp:706
CpGridDataTraits::Communication Communication
The type of the collective communication.
Definition: CpGridData.hpp:696
int numFaces() const
Definition: CpGridData.hpp:399
const std::vector< std::tuple< int, std::vector< int > > > & getParentToChildren() const
Definition: CpGridData.hpp:524
CpGridData(const std::vector< std::shared_ptr< CpGridData > > &data)
Constructor.
void getIJK(int c, std::array< int, 3 > &ijk) const
Extract Cartesian index triplet (i,j,k) of an active cell.
const IndexSet & indexSet() const
Definition: CpGridData.hpp:647
CommunicationType & cellCommunication()
Get the owner-overlap-copy communication for cells.
Definition: CpGridData.hpp:720
auto cellToFace(int cellIdx) const
Definition: CpGridData.hpp:362
auto faceNormals(int faceIdx) const
Definition: CpGridData.hpp:388
CpGridDataTraits::RemoteIndices RemoteIndices
The type of the remote indices information.
Definition: CpGridData.hpp:715
auto cornerHistorySize() const
Definition: CpGridData.hpp:404
ParallelIndexSet & cellIndexSet()
Definition: CpGridData.hpp:733
const auto & cellToPoint(int cellIdx) const
Definition: CpGridData.hpp:372
int getMark(const cpgrid::Entity< 0 > &element) const
Return refinement mark for entity.
const ParallelIndexSet & cellIndexSet() const
Definition: CpGridData.hpp:738
CpGridDataTraits::MPICommunicator MPICommunicator
The type of the mpi communicator.
Definition: CpGridData.hpp:694
bool mark(int refCount, const cpgrid::Entity< 0 > &element, bool throwOnFailure=false)
Mark entity for refinement or coarsening.
std::vector< std::int64_t > stableCellId() const
See CpGrid::stableCellId().
void distributeGlobalGrid(CpGrid &grid, const CpGridData &view_data, const std::vector< int > &cell_part)
Redistribute a global grid.
const std::vector< std::shared_ptr< Dune::cpgrid::CpGridData > > & levelData() const
Add doc/or remove method and replace it with better approach.
Definition: CpGridData.hpp:505
const auto & cellToPoint() const
Definition: CpGridData.hpp:367
const std::vector< double > & zcornData() const
Definition: CpGridData.hpp:608
void setUniqueBoundaryIds(bool uids)
Definition: CpGridData.hpp:597
std::vector< std::size_t > processEclipseFormat(const Opm::EclipseGrid *ecl_grid, Opm::EclipseState *ecl_state, bool periodic_extension, bool turn_normals=false, bool clip_z=false, bool pinchActive=true, bool edge_conformal=false, ContinuumSlice *continuum=nullptr)
bool preAdapt()
Set mightVanish flags for elements that will be refined in the next adapt() call Need to be called af...
int getGridIdx() const
Add doc/or remove method and replace it with better approach.
Definition: CpGridData.hpp:491
bool hasNNCs(const std::vector< int > &cellIndices) const
Check all cells selected for refinement have no NNCs (no neighbor connections). Assumption: all grid ...
const CommunicationType & cellCommunication() const
Get the owner-overlap-copy communication for cells.
Definition: CpGridData.hpp:728
int cellFace(int cell, int local_index) const
Definition: CpGridData.hpp:357
const cpgrid::IdSet & localIdSet() const
Get the local index set.
Definition: CpGridData.hpp:653
const cpgrid::DefaultGeometryPolicy getGeometry() const
Definition: CpGridData.hpp:528
bool adapt()
TO DO: Documentation. Triggers the grid refinement process - Currently, returns preAdapt()
int faceToCellSize(int face) const
Definition: CpGridData.hpp:377
CpGridDataTraits::Communicator Communicator
The type of the Communicator.
Definition: CpGridData.hpp:703
std::tuple< const std::shared_ptr< CpGridData >, const std::vector< std::array< int, 2 > > > refineSingleCell(const std::array< int, 3 > &cells_per_dim, const int &parent_idx, std::vector< std::vector< std::pair< int, std::vector< int > > > > &faceInMarkedElemAndRefinedFaces) const
Refine a single cell and return a shared pointer of CpGridData type.
std::array< double, 3 > computeEclCentroid(const int idx) const
CpGridData(MPIHelper::MPICommunicator comm, const std::vector< std::shared_ptr< CpGridData > > &data)
const std::vector< int > & sortedNumAquiferCells() const
Get sorted active cell indices of numerical aquifer.
Definition: CpGridData.hpp:755
const RemoteIndices & cellRemoteIndices() const
Definition: CpGridData.hpp:748
Definition: DefaultGeometryPolicy.hpp:56
Wrapper that turns a data handle suitable for dune-grid into one based on integers instead of entitie...
Definition: Entity2IndexDataHandle.hpp:56
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:98
The global id set for Dune.
Definition: Indexsets.hpp:497
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:118
Definition: Indexsets.hpp:199
Definition: Indexsets.hpp:57
Definition: Intersection.hpp:63
Definition: Indexsets.hpp:378
Definition: PartitionTypeIndicator.hpp:52
Definition: CpGridData.hpp:1040
void write(const T &data)
Definition: CpGridData.hpp:1047
void reset()
Definition: CpGridData.hpp:1051
void read(T &data)
Definition: CpGridData.hpp:1043
void resize(std::size_t size)
Definition: CpGridData.hpp:1055
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
Dune::cpgrid::Cell2FacesContainer cell2Faces(const Dune::CpGrid &grid)
Get the cell to faces mapping of a grid.
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26
Definition: CpGridData.hpp:117
int cartOffset
Cartesian index, in the dual-continuum grid, of the first cell of this continuum.
Definition: CpGridData.hpp:119
bool broadcastSuccess
Definition: CpGridData.hpp:123
std::vector< Opm::NNCdata > pinchNnc
Definition: CpGridData.hpp:128
MPIHelper::MPICommunicator MPICommunicator
The type of the collective communication.
Definition: CpGridDataTraits.hpp:56
Dune::VariableSizeCommunicator<> Communicator
The type of the Communicator.
Definition: CpGridDataTraits.hpp:71
Dune::RemoteIndices< ParallelIndexSet > RemoteIndices
The type of the remote indices information.
Definition: CpGridDataTraits.hpp:83
typename CommunicationType::ParallelIndexSet ParallelIndexSet
The type of the parallel index set.
Definition: CpGridDataTraits.hpp:80
Dune::Communication< MPICommunicator > CollectiveCommunication
Definition: CpGridDataTraits.hpp:59
Dune::OwnerOverlapCopyCommunication< int, int > CommunicationType
type of OwnerOverlap communication for cells
Definition: CpGridDataTraits.hpp:77
Dune::Communication< MPICommunicator > Communication
Definition: CpGridDataTraits.hpp:58
AttributeSet
The type of the set of the attributes.
Definition: CpGridDataTraits.hpp:66
Communicator::InterfaceMap InterfaceMap
The type of the map describing communication interfaces.
Definition: CpGridDataTraits.hpp:74
const std::vector< std::shared_ptr< CpGridData > > * level_data_ptr
Definition: LevelHierarchy.hpp:67
std::vector< std::array< int, 2 > > child_to_parent_cells
Definition: LevelHierarchy.hpp:82
std::vector< int > level_to_leaf_cells
Definition: LevelHierarchy.hpp:70
std::vector< std::tuple< int, std::vector< int > > > parent_to_children_cells
Definition: LevelHierarchy.hpp:72
int level
Definition: LevelHierarchy.hpp:65
std::vector< std::array< int, 2 > > corner_history
Definition: LevelHierarchy.hpp:79
Definition: CpGridData.hpp:1071
BaseMover(DataHandle &data)
Definition: CpGridData.hpp:1072
void moveData(const E &from, const E &to)
Definition: CpGridData.hpp:1076
MoveBuffer< typename DataHandle::DataType > buffer
Definition: CpGridData.hpp:1085
DataHandle & data_
Definition: CpGridData.hpp:1084
Definition: CpGridData.hpp:1091
void operator()(std::size_t from_cell_index, std::size_t to_cell_index)
Definition: CpGridData.hpp:1097
CpGridData * scatterView_
Definition: CpGridData.hpp:1104
CpGridData * gatherView_
Definition: CpGridData.hpp:1103
Mover(DataHandle &data, CpGridData *gatherView, CpGridData *scatterView)
Definition: CpGridData.hpp:1092
Definition: CpGridData.hpp:1109
Mover(DataHandle &data, CpGridData *gatherView, CpGridData *scatterView)
Definition: CpGridData.hpp:1110
CpGridData * gatherView_
Definition: CpGridData.hpp:1127
CpGridData * scatterView_
Definition: CpGridData.hpp:1128
void operator()(std::size_t from_cell_index, std::size_t to_cell_index)
Definition: CpGridData.hpp:1115
Definition: CpGridData.hpp:1133
CpGridData * scatterView_
Definition: CpGridData.hpp:1151
CpGridData * gatherView_
Definition: CpGridData.hpp:1150
Mover(DataHandle &data, CpGridData *gatherView, CpGridData *scatterView)
Definition: CpGridData.hpp:1134
void operator()(std::size_t from_cell_index, std::size_t to_cell_index)
Definition: CpGridData.hpp:1138
Definition: CpGridData.hpp:1066
Definition: GridStateWriter.hpp:44
Definition: RetainedCornerPointInput.hpp:36
Definition: preprocess.h:56