LookUpData.hh
Go to the documentation of this file.
1//===========================================================================
2//
3// File: LookUpData.hh
4//
5// Created: Tue May 23 14:44:00 2023
6//
7// Author(s): Antonella Ritorto <antonella.ritorto@opm-op.com>
8//
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17 Copyright 2023 Equinor ASA.
18
19 This file is part of The Open Porous Media project (OPM).
20
21 OPM is free software: you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation, either version 3 of the License, or
24 (at your option) any later version.
25
26 OPM is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
30
31 You should have received a copy of the GNU General Public License
32 along with OPM. If not, see <http://www.gnu.org/licenses/>.
33*/
34#ifndef OPM_LOOKUPDATA_HH
35#define OPM_LOOKUPDATA_HH
36
37#include <dune/grid/common/mcmgmapper.hh>
38
39#include <opm/input/eclipse/EclipseState/Grid/FieldPropsManager.hpp>
41
42#include <functional>
43#include <string>
44#include <type_traits>
45#include <vector>
46
47namespace Dune
48{
49class CpGrid;
50}
51
52namespace Opm
53{
54
61template <typename Grid, typename GridView>
63{
64public:
71 explicit LookUpData(const GridView& gridView, bool isFieldPropInLgr = false) :
72 gridView_(gridView),
73 elemMapper_(gridView, Dune::mcmgElementLayout()),
74 isFieldPropInLgr_(isFieldPropInLgr)
75 {
76 }
77
83 template<typename FieldPropType>
84 FieldPropType operator()(const int& elemIdx, const std::vector<FieldPropType>& fieldProp) const;
85
91 template<typename EntityType, typename FieldPropType>
92 typename std::enable_if_t<!std::is_same_v<EntityType, unsigned int>, FieldPropType>
93 operator()(const EntityType& elem, const std::vector<FieldPropType>& fieldProp) const;
94
96 std::vector<double> assignFieldPropsDoubleOnLeaf(const FieldPropsManager& fieldPropsManager,
97 const std::string& propString) const;
98
100 template<typename IntType>
101 std::vector<IntType> assignFieldPropsIntOnLeaf(const FieldPropsManager& fieldPropsManager,
102 const std::string& propString,
103 const bool& needsTranslation,
104 std::function<void(IntType, int)> valueCheck = [](IntType, int){}) const;
105
107 template<typename ElemOrIndex>
108 double fieldPropDouble(const FieldPropsManager& fieldPropsManager,
109 const std::string& propString,
110 const ElemOrIndex& elemOrIndex) const;
111
113 template<typename ElemOrIndex>
114 int fieldPropInt(const FieldPropsManager& fieldPropsManager,
115 const std::string& propString,
116 const ElemOrIndex& elemOrIndex) const;
117
122 template<typename EntityType, typename GridType = Grid>
123 typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
124 getFieldPropIdx(const EntityType& elem) const;
125
134 template<typename EntityType, typename GridType = Grid>
135 typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
136 getFieldPropIdx(const EntityType& elem) const;
137
142 template<typename GridType>
143 typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid>,int>
144 getFieldPropIdx(const int& elemIdx) const;
145
154 template<typename GridType = Grid>
155 typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid>,int>
156 getFieldPropIdx(const int& elemIdx) const;
157
158
159protected:
160 const GridView& gridView_;
161 Dune::MultipleCodimMultipleGeomTypeMapper<GridView> elemMapper_;
163}; // end LookUpData class
164
171template<typename Grid, typename GridView>
173{
174public:
183 explicit LookUpCartesianData(const GridView& gridView,
185 bool isFieldPropInLgr = false) :
186 gridView_(gridView),
187 elemMapper_(gridView, Dune::mcmgElementLayout()),
188 cartMapper_(&mapper),
189 isFieldPropInLgr_(isFieldPropInLgr)
190 {
191 }
192
198 template<typename FieldPropType>
199 FieldPropType operator()(const int& elemIdx, const std::vector<FieldPropType>& fieldProp) const;
200
206 template<typename EntityType, typename FieldPropType>
207 typename std::enable_if_t<!std::is_same_v<EntityType, unsigned int>, FieldPropType>
208 operator()(const EntityType& elem,const std::vector<FieldPropType>& fieldProp) const;
209
211 std::vector<double> assignFieldPropsDoubleOnLeaf(const FieldPropsManager& fieldPropsManager,
212 const std::string& propString) const;
213
215 template<typename IntType>
216 std::vector<IntType> assignFieldPropsIntOnLeaf(const FieldPropsManager& fieldPropsManager,
217 const std::string& propString,
218 const bool& needsTranslation,
219 std::function<void(IntType, int)> valueCheck = [](IntType, int){}) const;
220
222 template<typename ElemOrIndex>
223 double fieldPropDouble(const FieldPropsManager& fieldPropsManager,
224 const std::string& propString,
225 const ElemOrIndex& elemOrIndex) const;
226
228 template<typename ElemOrIndex>
229 int fieldPropInt(const FieldPropsManager& fieldPropsManager,
230 const std::string& propString,
231 const ElemOrIndex& elemOrIndex) const;
232
237 template<typename EntityType, typename GridType = Grid>
238 typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
239 getFieldPropCartesianIdx(const EntityType& elem) const;
240
248 template<typename EntityType, typename GridType = Grid>
249 typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
250 getFieldPropCartesianIdx(const EntityType& elem) const;
251
256 template<typename GridType = Grid>
257 typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid>,int>
258 getFieldPropCartesianIdx(const int& elemIdx) const;
259
267 template<typename GridType = Grid>
268 typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid>,int>
269 getFieldPropCartesianIdx(const int& elemIdx) const;
270
271protected:
272 const GridView& gridView_;
273 Dune::MultipleCodimMultipleGeomTypeMapper<GridView> elemMapper_;
276}; // end LookUpCartesianData class
277}
278// end namespace Opm
279
280
281
283
284template<typename Grid, typename GridView>
285template<typename FieldPropType>
286FieldPropType Opm::LookUpData<Grid,GridView>::operator()(const int& elemIdx,
287 const std::vector<FieldPropType>& fieldProp) const
288{
289 const auto& fieldPropIdx = this->getFieldPropIdx<Grid>(elemIdx);
290 assert(0 <= fieldPropIdx && static_cast<int>(fieldProp.size()) > fieldPropIdx);
291 return fieldProp[fieldPropIdx];
292}
293
294template<typename Grid, typename GridView>
295template<typename EntityType, typename FieldPropType>
296typename std::enable_if_t<!std::is_same_v<EntityType, unsigned int>,FieldPropType>
298 const std::vector<FieldPropType>& fieldProp) const
299{
300 const auto& fieldPropIdx = this->getFieldPropIdx<EntityType,Grid>(elem);
301 assert( (0 <= fieldPropIdx) && (static_cast<int>(fieldProp.size()) > fieldPropIdx));
302 return fieldProp[fieldPropIdx];
303}
304
305template<typename Grid, typename GridView>
306std::vector<double> Opm::LookUpData<Grid,GridView>::assignFieldPropsDoubleOnLeaf(const FieldPropsManager& fieldPropsManager,
307 const std::string& propString) const
308{
309 std::vector<double> fieldPropOnLeaf;
310 unsigned int numElements = gridView_.size(0);
311 fieldPropOnLeaf.resize(numElements);
312 const auto& fieldProp = fieldPropsManager.get_double(propString);
313 if ( (propString == "PORV") && (gridView_.grid().maxLevel() > 0)) {
314 // PORV poreVolume. LGRs supported (so far) only for CpGrid.
315 // For CpGrid with LGRs, poreVolume of a cell on the leaf grid view which has a parent cell on level 0,
316 // is computed as porv[parent] * leafCellVolume / parentCellVolume. In this way, the sum of the pore
317 // volume of a parent cell coincides with the sum of the pore volume of its children.
318 for (const auto& element : elements(gridView_)) {
319 const auto& elemIdx = this-> elemMapper_.index(element);
320 const auto& fieldPropIdx = this->getFieldPropIdx<Grid>(elemIdx); // gets parentIdx (or (lgr)levelIdx) for CpGrid with LGRs
321 if (element.hasFather()) {
322 const auto fatherVolume = element.father().geometry().volume();
323 const auto& elemVolume = element.geometry().volume();
324 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropIdx] * elemVolume / fatherVolume;
325 }
326 else {
327 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropIdx];
328 }
329 }
330 }
331 else {
332 for (const auto& element : elements(gridView_)) {
333 const auto& elemIdx = this-> elemMapper_.index(element);
334 const auto& fieldPropIdx = this->getFieldPropIdx<Grid>(elemIdx); // gets parentIdx (or (lgr)levelIdx) for CpGrid with LGRs
335 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropIdx];
336 }
337 }
338 return fieldPropOnLeaf;
339}
340
341template<typename Grid, typename GridView>
342template<typename IntType>
343std::vector<IntType> Opm::LookUpData<Grid,GridView>::assignFieldPropsIntOnLeaf(const FieldPropsManager& fieldPropsManager,
344 const std::string& propString,
345 const bool& needsTranslation,
346 std::function<void(IntType, int)> valueCheck) const
347{
348 std::vector<IntType> fieldPropOnLeaf;
349 unsigned int numElements = gridView_.size(0);
350 fieldPropOnLeaf.resize(numElements);
351 const auto& fieldProp = fieldPropsManager.get_int(propString);
352 for (const auto& element : elements(gridView_)) {
353 const auto& elemIdx = this-> elemMapper_.index(element);
354 const auto& fieldPropIdx = this->getFieldPropIdx<Grid>(elemIdx); // gets parentIdx (or (lgr)levelIdx) for CpGrid with LGRs
355 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropIdx] - needsTranslation;
356 valueCheck(fieldProp[fieldPropIdx], fieldPropIdx);
357 }
358 return fieldPropOnLeaf;
359}
360
361template<typename Grid, typename GridView>
362template<typename ElemOrIndex>
363double Opm::LookUpData<Grid,GridView>::fieldPropDouble(const FieldPropsManager& fieldPropsManager,
364 const std::string& propString,
365 const ElemOrIndex& elemOrIndex) const
366{
367 const auto& fieldPropVec = fieldPropsManager.get_double(propString);
368 return this ->operator()(elemOrIndex,fieldPropVec);
369}
370
371template<typename Grid, typename GridView>
372template<typename ElemOrIndex>
373int Opm::LookUpData<Grid,GridView>::fieldPropInt(const FieldPropsManager& fieldPropsManager,
374 const std::string& propString,
375 const ElemOrIndex& elemOrIndex) const
376{
377 const auto& fieldPropVec = fieldPropsManager.get_int(propString);
378 return this ->operator()(elemOrIndex,fieldPropVec);
379}
380
381template<typename Grid, typename GridView>
382template<typename EntityType, typename GridType>
383typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
385{
386 static_assert(std::is_same_v<Grid,GridType>);
387 assert(elem.level() == 0); // LGRs (level>0) only supported for CpGrid.
388 return this-> elemMapper_.index(elem);
389}
390
391template<typename Grid, typename GridView>
392template<typename EntityType,typename GridType>
393typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
395{
396 static_assert(std::is_same_v<Grid,GridType>);
397 static_assert(std::is_same_v<EntityType,Dune::cpgrid::Entity<0>>);
398 if (isFieldPropInLgr_ && elem.level()) { // level > 0 == true ; level == 0 == false
399 // In case some LGRs do not have refined field properties, the next line need to be modified.
400 return elem.getLevelElem().index();
401 }
402 else {
403 return elem.getOrigin().index();
404 }
405}
406
407template<typename Grid, typename GridView>
408template<typename GridType>
409typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid>,int>
411{
412 static_assert(std::is_same_v<Grid,GridType>);
413 // Check there are no LGRs. LGRs (level>0) only supported for CpGrid.
414 assert(gridView_.grid().maxLevel() == 0);
415 return elemIdx;
416}
417
418template<typename Grid, typename GridView>
419template<typename GridType>
420typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid>,int>
422{
423 static_assert(std::is_same_v<Grid,GridType>);
424 const auto& elem = Dune::cpgrid::Entity<0>(*(gridView_.grid().current_view_data_), elemIdx, true);
425 if (isFieldPropInLgr_ && elem.level()) { // level > 0 == true ; level == 0 == false
426 // In case some LGRs do not have refined field properties, the next line need to be modified.
427 return elem.getLevelElem().index();
428 }
429 else {
430 return elem.getOrigin().index();
431 }
432}
433
434
435
437
438template<typename Grid, typename GridView>
439template<typename FieldPropType>
441 const std::vector<FieldPropType>& fieldProp) const
442{
443 assert(cartMapper_);
444 const auto fieldPropCartIdx = this->getFieldPropCartesianIdx<Grid>(elemIdx);
445 assert(0 <= fieldPropCartIdx && (static_cast<int>(fieldProp.size()) > fieldPropCartIdx));
446 return fieldProp[fieldPropCartIdx];
447}
448
449template<typename Grid, typename GridView>
450template<typename EntityType, typename FieldPropType>
451typename std::enable_if_t<!std::is_same_v<EntityType, unsigned int>,FieldPropType>
453 const std::vector<FieldPropType>& fieldProp) const
454{
455 assert(cartMapper_);
456 const auto fieldPropCartIdx = this->getFieldPropCartesianIdx<EntityType,Grid>(elem);
457 assert( (0 <= fieldPropCartIdx) && (static_cast<int>(fieldProp.size()) > fieldPropCartIdx) );
458 return fieldProp[fieldPropCartIdx];
459}
460
461template<typename Grid, typename GridView>
462std::vector<double> Opm::LookUpCartesianData<Grid,GridView>::assignFieldPropsDoubleOnLeaf(const FieldPropsManager& fieldPropsManager,
463 const std::string& propString) const
464{
465 std::vector<double> fieldPropOnLeaf;
466 unsigned int numElements = gridView_.size(0);
467 fieldPropOnLeaf.resize(numElements);
468 const auto& fieldProp = fieldPropsManager.get_double(propString);
469 for (unsigned int elemIdx = 0; elemIdx < numElements; ++elemIdx) {
470 const auto fieldPropCartIdx = this->getFieldPropCartesianIdx<Grid>(elemIdx);
471 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropCartIdx];
472 }
473 return fieldPropOnLeaf;
474}
475
476template<typename Grid, typename GridView>
477template<typename IntType>
478std::vector<IntType> Opm::LookUpCartesianData<Grid,GridView>::assignFieldPropsIntOnLeaf(const FieldPropsManager& fieldPropsManager,
479 const std::string& propString,
480 const bool& needsTranslation,
481 std::function<void(IntType, int)> valueCheck) const
482{
483 std::vector<IntType> fieldPropOnLeaf;
484 unsigned int numElements = gridView_.size(0);
485 fieldPropOnLeaf.resize(numElements);
486 const auto& fieldProp = fieldPropsManager.get_int(propString);
487 for (unsigned elemIdx = 0; elemIdx < numElements; ++elemIdx) {
488 const auto fieldPropCartIdx = this->getFieldPropCartesianIdx<Grid>(elemIdx);
489 fieldPropOnLeaf[elemIdx] = fieldProp[fieldPropCartIdx] - needsTranslation;
490 valueCheck(fieldProp[fieldPropCartIdx], fieldPropCartIdx);
491 }
492 return fieldPropOnLeaf;
493}
494
495template<typename Grid, typename GridView>
496template<typename ElemOrIndex>
497double Opm::LookUpCartesianData<Grid,GridView>::fieldPropDouble(const FieldPropsManager& fieldPropsManager,
498 const std::string& propString,
499 const ElemOrIndex& elemOrIndex) const
500{
501 const auto& fieldPropVec = fieldPropsManager.get_double(propString);
502 return this ->operator()(elemOrIndex,fieldPropVec);
503}
504
505template<typename Grid, typename GridView>
506template<typename ElemOrIndex>
507int Opm::LookUpCartesianData<Grid,GridView>::fieldPropInt(const FieldPropsManager& fieldPropsManager,
508 const std::string& propString,
509 const ElemOrIndex& elemOrIndex) const
510{
511 const auto& fieldPropVec = fieldPropsManager.get_int(propString);
512 return this ->operator()(elemOrIndex,fieldPropVec);
513}
514
515template<typename Grid, typename GridView>
516template<typename EntityType, typename GridType>
517typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
519{
520 static_assert(std::is_same_v<Grid,GridType>);
521 // Check there are no LGRs. LGRs (level>0) only supported for CpGrid.
522 assert(gridView_.grid().maxLevel() == 0);
523 return cartMapper_-> cartesianIndex(this->elemMapper_.index(elem));
524}
525
526template<typename Grid, typename GridView>
527template<typename EntityType, typename GridType>
528typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid> && !std::is_same_v<EntityType, unsigned int>,int>
530{
531 static_assert(std::is_same_v<Grid,GridType>);
532 if (isFieldPropInLgr_ && elem.level()) { // level == 0 false; level > 0 true
533 return elem.getLevelCartesianIdx();
534 }
535 else {
536 return cartMapper_-> cartesianIndex(this->elemMapper_.index(elem));
537 }
538}
539
540template<typename Grid, typename GridView>
541template<typename GridType>
542typename std::enable_if_t<!std::is_same_v<GridType,Dune::CpGrid>,int>
544{
545 static_assert(std::is_same_v<Grid,GridType>);
546 return cartMapper_-> cartesianIndex(elemIdx);
547}
548
549template<typename Grid, typename GridView>
550template<typename GridType>
551typename std::enable_if_t<std::is_same_v<GridType,Dune::CpGrid>,int>
553{
554 static_assert(std::is_same_v<Grid,GridType>);
555 const auto& elem = Dune::cpgrid::Entity<0>(*(gridView_.grid().current_view_data_), elemIdx, true);
556 return this -> getFieldPropCartesianIdx<Dune::cpgrid::Entity<0>,Dune::CpGrid>(elem);
557}
558
559#endif
Interface class to access the logical Cartesian grid as used in industry standard simulator decks.
Definition: common/CartesianIndexMapper.hpp:16
[ provides Dune::Grid ]
Definition: CpGrid.hpp:238
Definition: LookUpData.hh:173
FieldPropType operator()(const int &elemIdx, const std::vector< FieldPropType > &fieldProp) const
: Get field property for an element in the leaf grid view, from a vector, via Cartesian Index.
Definition: LookUpData.hh:440
const GridView & gridView_
Definition: LookUpData.hh:272
std::vector< double > assignFieldPropsDoubleOnLeaf(const FieldPropsManager &fieldPropsManager, const std::string &propString) const
: Get field property of type double from field properties manager by name.
Definition: LookUpData.hh:462
int fieldPropInt(const FieldPropsManager &fieldPropsManager, const std::string &propString, const ElemOrIndex &elemOrIndex) const
: Get property of type int from field properties manager by name, via element or its index.
Definition: LookUpData.hh:507
LookUpCartesianData(const GridView &gridView, const Dune::CartesianIndexMapper< Grid > &mapper, bool isFieldPropInLgr=false)
: Constructor taking a GridView, a CartesianIndexMapper, and a bool
Definition: LookUpData.hh:183
double fieldPropDouble(const FieldPropsManager &fieldPropsManager, const std::string &propString, const ElemOrIndex &elemOrIndex) const
: Get property of type double from field properties manager by name, via element or its index.
Definition: LookUpData.hh:497
std::enable_if_t<!std::is_same_v< GridType, Dune::CpGrid > &&!std::is_same_v< EntityType, unsigned int >, int > getFieldPropCartesianIdx(const EntityType &elem) const
: Return the same element index for all grids different from CpGrid.
Definition: LookUpData.hh:518
const Dune::CartesianIndexMapper< Grid > * cartMapper_
Definition: LookUpData.hh:274
std::vector< IntType > assignFieldPropsIntOnLeaf(const FieldPropsManager &fieldPropsManager, const std::string &propString, const bool &needsTranslation, std::function< void(IntType, int)> valueCheck=[](IntType, int){}) const
: Get field property of type int from field properties manager by name.
Definition: LookUpData.hh:478
bool isFieldPropInLgr_
Definition: LookUpData.hh:275
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > elemMapper_
Definition: LookUpData.hh:273
Definition: LookUpData.hh:63
bool isFieldPropInLgr_
Definition: LookUpData.hh:162
std::vector< double > assignFieldPropsDoubleOnLeaf(const FieldPropsManager &fieldPropsManager, const std::string &propString) const
: Get field property of type double from field properties manager by name.
Definition: LookUpData.hh:306
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > elemMapper_
Definition: LookUpData.hh:161
const GridView & gridView_
Definition: LookUpData.hh:160
LookUpData(const GridView &gridView, bool isFieldPropInLgr=false)
: Constructor taking a GridView and a bool
Definition: LookUpData.hh:71
std::vector< IntType > assignFieldPropsIntOnLeaf(const FieldPropsManager &fieldPropsManager, const std::string &propString, const bool &needsTranslation, std::function< void(IntType, int)> valueCheck=[](IntType, int){}) const
: Get field property of type int from field properties manager by name.
Definition: LookUpData.hh:343
std::enable_if_t<!std::is_same_v< GridType, Dune::CpGrid > &&!std::is_same_v< EntityType, unsigned int >, int > getFieldPropIdx(const EntityType &elem) const
: Return the same element index for all grids different from CpGrid.
Definition: LookUpData.hh:384
int fieldPropInt(const FieldPropsManager &fieldPropsManager, const std::string &propString, const ElemOrIndex &elemOrIndex) const
: Get property of type int from field properties manager by name, via element.
Definition: LookUpData.hh:373
double fieldPropDouble(const FieldPropsManager &fieldPropsManager, const std::string &propString, const ElemOrIndex &elemOrIndex) const
: Get property of type double from field properties manager by name, via element or its index.
Definition: LookUpData.hh:363
FieldPropType operator()(const int &elemIdx, const std::vector< FieldPropType > &fieldProp) const
: Get field propertry for an element in the leaf grid view, from a vector and element index.
Definition: LookUpData.hh:286
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26