My Project
DataHandleWrappers.hpp
1//===========================================================================
2//
3// File: DataHandleWrappers.hpp
4//
5// Created: Mon Nov 4 2013
6//
7// Author(s): Markus Blatt <markus@dr-blatt.de>
8//
9// $Date$
10//
11// $Revision$
12//
13//===========================================================================
32#ifndef OPM_DATAHANDLEWRAPPERS_HEADER
33#define OPM_DATAHANDLEWRAPPERS_HEADER
34
35#include <array>
36#include <vector>
37#include <iostream>
38
39#include "OrientedEntityTable.hpp"
40#include "EntityRep.hpp"
41
42#include <dune/common/version.hh>
43
44namespace Dune
45{
46namespace cpgrid
47{
48
60template<class Handle>
62{
63 using DataType = typename Handle::DataType;
65
72 const C2FTable& c2fGather,
73 const C2FTable& c2f)
74 : handle_(handle), c2fGather_(c2fGather), c2f_(c2f)
75 {}
76
77#if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 7)
78 bool fixedSize(int, int)
79#else
80 bool fixedsize(int, int)
81#endif
82 {
83 return false; // as the faces per cell differ
84 }
85 template<class T>
86 typename std::enable_if<T::codimension != 0, std::size_t>::type
87 size(const T&)
88 {
89 OPM_THROW(std::logic_error, "This should never throw! We only know sizes for cells");
90 return 1;
91 }
92 std::size_t size(const EntityRep<0>& t)
93 {
94 const auto& faces = c2fGather_[t];
95 std::size_t size{};
96 for (const auto& face : faces)
97 {
98 size += handle_.size(face);
99 }
100 return size;
101 }
102 bool contains(std::size_t dim, std::size_t codim)
103 {
104 return dim==3 && codim == 0;
105 }
106 template<class B, class T>
107 typename std::enable_if<T::codimension != 0, void>::type
108 gather(B&, const T&)
109 {
110 OPM_THROW(std::logic_error, "This should never throw! We can only gather cell values and indicate that");
111 }
112 template<class B>
113 void gather(B& buffer, const EntityRep<0>& t)
114 {
115 const auto& faces = c2fGather_[t];
116 for (const auto& face : faces)
117 {
118 handle_.gather(buffer, face);
119 }
120 }
121 template<class B, class T>
122 typename std::enable_if<T::codimension != 0, void>::type
123 scatter(B&, const T&, std::size_t)
124 {
125 OPM_THROW(std::logic_error, "This should never throw! We can only gather cell values and indicate that");
126 }
127 template<class B>
128 void scatter(B& buffer, const EntityRep<0>& t, std::size_t)
129 {
130 const auto& faces = c2f_[t];
131 for (const auto& face : faces)
132 {
133 // Note that the size (last parameter) is not correct here.
134 // Therefore this handle needs to know how many data items
135 // to expect. Not usable outside of CpGrid.
136 handle_.scatter(buffer, face, 1);
137 }
138 }
139private:
140 Handle& handle_;
141 const C2FTable& c2fGather_, c2f_;
142};
143
145{
146 static bool printWarn;
147 void warn()
148 {
149 if (printWarn)
150 {
151 std::cerr << "Communication of variable data attached to points is "
152 << "not fully supported. Your code/handle must not use the"
153 << "last parameter of "
154 << "DataHandle::scatter(B& buffer, E& entity, int size) "
155 << "as it will not be correct!";
156 printWarn =false;
157 }
158 }
159};
160
170template<class Handle>
172{
173 using DataType = typename Handle::DataType;
174 using C2PTable = std::vector< std::array<int,8> >;
175
182 const C2PTable& c2pGather,
183 const C2PTable& c2p)
184 : handle_(handle), c2pGather_(c2pGather), c2p_(c2p)
185 {}
186#if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 7)
187 bool fixedSize(int i, int j)
188 {
189 if( ! handle_.fixedSize(i, j))
190 {
191 this->warn();
192 }
193 return handle_.fixedSize(i, j);
194 }
195#else
196 bool fixedsize(int i, int j)
197 {
198 if( ! handle_.fixedsize(i, j))
199 {
200 this->warn();
201 }
202 return handle_.fixedsize(i, j);
203 }
204#endif
205 template<class T>
206 typename std::enable_if<T::codimension != 0, std::size_t>::type
207 size(const T&)
208 {
209 OPM_THROW(std::logic_error, "This should never throw! We only know sizes for cells");
210 return 1;
211 }
212 std::size_t size(const EntityRep<0>& t)
213 {
214 const auto& points = c2pGather_[t.index()];
215 std::size_t size{};
216 for (const auto& point : points)
217 {
218 size += handle_.size(EntityRep<3>(point, true));
219 }
220 return size;
221 }
222 bool contains(std::size_t dim, std::size_t codim)
223 {
224 return dim==3 && codim == 0;
225 }
226 template<class B, class T>
227 typename std::enable_if<T::codimension != 0, void>::type
228 gather(B&, const T&)
229 {
230 OPM_THROW(std::logic_error, "This should never throw! We can only gather cell values and indicate that");
231 }
232 template<class B>
233 void gather(B& buffer, const EntityRep<0>& t)
234 {
235 const auto& points = c2pGather_[t.index()];
236 for (const auto& point : points)
237 {
238 handle_.gather(buffer, EntityRep<3>(point, true));
239 }
240 }
241 template<class B, class T>
242 typename std::enable_if<T::codimension != 0, void>::type
243 scatter(B&, const T&, std::size_t)
244 {
245 OPM_THROW(std::logic_error, "This should never throw! We can only gather cell values and indicate that");
246 }
247 template<class B>
248 void scatter(B& buffer, const EntityRep<0>& t, std::size_t s)
249 {
250 const auto& points = c2p_[t.index()];
251 for (const auto& point : points)
252 {
253 handle_.scatter(buffer, EntityRep<3>(point, true), s/8);
254 }
255 }
256private:
257 Handle& handle_;
258 const C2PTable& c2pGather_, c2p_;
259};
260
261} // end namespace cpgrid
262} // end namespace Dune
263#endif
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10
A data handle to send data attached to faces via cell communication.
Definition: DataHandleWrappers.hpp:62
FaceViaCellHandleWrapper(Handle &handle, const C2FTable &c2fGather, const C2FTable &c2f)
Constructs the data handle.
Definition: DataHandleWrappers.hpp:71
A data handle to send data attached to points via cell communication.
Definition: DataHandleWrappers.hpp:172
PointViaCellHandleWrapper(Handle &handle, const C2PTable &c2pGather, const C2PTable &c2p)
Constructs the data handle.
Definition: DataHandleWrappers.hpp:181
Definition: DataHandleWrappers.hpp:145