FacePropertiesTPSA_impl.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 Copyright 2025 NORCE AS
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20
21 Consult the COPYING file in the top-level source directory of this
22 module for the precise wording of the license and the list of
23 copyright holders.
24*/
25#ifndef TPSA_FACE_PROPERTIES_IMPL_HPP
26#define TPSA_FACE_PROPERTIES_IMPL_HPP
27
28#ifndef TPSA_FACE_PROPERTIES_HPP
29#include <config.h>
31#endif
32
33#include <dune/grid/common/mcmgmapper.hh>
34
35#include <opm/common/utility/ThreadSafeMapBuilder.hpp>
36
37#include <opm/grid/utility/ElementChunks.hpp>
38
39#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
40
42
43#include <algorithm>
44#include <stdexcept>
45#include <utility>
46
47
48namespace Opm {
49
50// Copied from Opm::Transmissibility class
51namespace details {
52 constexpr unsigned elemIdxShift = 32; // bits
53
54 std::uint64_t isIdTPSA(std::uint32_t elemIdx1, std::uint32_t elemIdx2)
55 {
56 const std::uint32_t elemAIdx = std::min(elemIdx1, elemIdx2);
57 const std::uint64_t elemBIdx = std::max(elemIdx1, elemIdx2);
58
59 return (elemBIdx << elemIdxShift) + elemAIdx;
60 }
61} // namespace Opm::details
62
63// /////
64// Public functions
65// ////
75template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
77FacePropertiesTPSA(const EclipseState& eclState,
78 const GridView& gridView,
79 const CartesianIndexMapper& cartMapper,
80 const Grid& grid,
81 std::function<std::array<double,dimWorld>(int)> centroids)
82 : eclState_(eclState)
83 , gridView_(gridView)
84 , cartMapper_(cartMapper)
85 , grid_(grid)
86 , centroids_(centroids)
87 , lookUpData_(gridView)
88 , lookUpCartesianData_(gridView, cartMapper)
89{ }
90
94template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
97{
98 update();
99}
100
104template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
106update()
107{
108 // Number of elements
109 ElementMapper elemMapper(gridView_, Dune::mcmgElementLayout());
110 unsigned numElements = elemMapper.size();
111
112 // Extract shear modulus (Lame's sec. params)
113 extractSModulus_();
114
115 // Init. containers
116 // Note (from Transmissibility::update): Reserving some space in the hashmap upfront saves quite a bit of time
117 // because resizes are costly for hashmaps and there would be quite a few of them if we would not have a rough idea
118 // of how large the final map will be (the rough idea is a conforming Cartesian grid).
119 const int num_threads = ThreadManager::maxThreads();
120 weightsAvg_.clear();
121 weightsProd_.clear();
122 distance_.clear();
123 faceNormal_.clear();
124 if (num_threads == 1) {
125 weightsAvg_.reserve(numElements * 3 * 1.05);
126 weightsProd_.reserve(numElements * 3 * 1.05);
127 distance_.reserve(numElements * 3 * 1.05);
128 faceNormal_.reserve(numElements * 3 * 1.05);
129 }
130 weightsAvgBoundary_.clear();
131 weightsProdBoundary_.clear();
132 distanceBoundary_.clear();
133 faceNormalBoundary_.clear();
134
135 // Fill the centroids cache to avoid repeated calculations in loops below
136 centroids_cache_.resize(gridView_.size(0));
137 for (const auto& elem : elements(gridView_)) {
138 const unsigned elemIdx = elemMapper.index(elem);
139 centroids_cache_[elemIdx] = centroids_(elemIdx);
140 }
141
142 // Initialize thread safe insert_or_assign for face properties in the grid and separate for boundaries
143 ThreadSafeMapBuilder weightsAvgMap(weightsAvg_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
144 ThreadSafeMapBuilder weightsProdMap(weightsProd_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
145 ThreadSafeMapBuilder distanceMap(distance_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
146 ThreadSafeMapBuilder faceNormalMap(faceNormal_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
147
148 ThreadSafeMapBuilder weightsAvgBoundaryMap(weightsAvgBoundary_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
149 ThreadSafeMapBuilder weightsProdBoundaryMap(weightsProdBoundary_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
150 ThreadSafeMapBuilder distanceBoundaryMap(distanceBoundary_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
151 ThreadSafeMapBuilder faceNormalBoundaryMap(faceNormalBoundary_, num_threads, MapBuilderInsertionMode::Insert_Or_Assign);
152
153 // Loop over grid elements and compute face properties
154 const auto element_chunks = ElementChunks(gridView_, Dune::Partitions::all, num_threads);
155#ifdef _OPENMP
156#pragma omp parallel for
157#endif
158 for (std::size_t ci = 0; ci < element_chunks.size(); ++ci) {
159 const auto& chunk = element_chunks[ci];
160 for (const auto& elem : chunk) {
161 // Init. face info for inside/outside cells
162 FaceInfo inside;
163 FaceInfo outside;
164 DimVector faceNormal;
165
166 // Set inside info
167 inside.elemIdx = elemMapper.index(elem);
168 inside.cartElemIdx = lookUpCartesianData_.
169 template getFieldPropCartesianIdx<Grid>(inside.elemIdx);
170
171 // Loop over intersection to neighboring cells
172 unsigned boundaryIsIdx = 0;
173 for (const auto& intersection : intersections(gridView_, elem)) {
174 // Handle grid boundary
175 if (intersection.boundary()) {
176 // One-sided cell properties
177 const auto& geometry = intersection.geometry();
178 inside.faceCenter = geometry.center();
179 faceNormal = intersection.centerUnitOuterNormal();
180
181 // Face properties on boundary
182 const auto index_pair = std::make_pair(inside.elemIdx, boundaryIsIdx);
183 Scalar distBound = computeDistance_(distanceVector_(inside.faceCenter, inside.elemIdx), faceNormal);
184 distanceBoundaryMap.insert_or_assign(index_pair, distBound);
185
186 Scalar weightsAvgBound = 1.0; // w_j = 0 -> w_avg = wi / (wi + 0)
187 Scalar weightsProdBound = 0.0; // w_j = 0 -> w_prod = wi * 0
188 weightsAvgBoundaryMap.insert_or_assign(index_pair, weightsAvgBound);
189 weightsProdBoundaryMap.insert_or_assign(index_pair, weightsProdBound);
190
191 faceNormalBoundaryMap.insert_or_assign(index_pair, faceNormal);
192
193 ++boundaryIsIdx;
194 continue;
195 }
196
197 // Handle intersection on process boundary (i.e., neighbor on different rank)
198 if (!intersection.neighbor()) {
199 ++boundaryIsIdx;
200 continue;
201 }
202
203 // Set outside info
204 const auto& outsideElem = intersection.outside();
205 outside.elemIdx = elemMapper.index(outsideElem);
206 outside.cartElemIdx = lookUpCartesianData_.
207 template getFieldPropCartesianIdx<Grid>(outside.elemIdx);
208
209 // Skip intersections that have already been processed
210 if (std::tie(inside.cartElemIdx, inside.elemIdx) > std::tie(outside.cartElemIdx, outside.elemIdx)) {
211 continue;
212 }
213
214 // Face indices for this intersection
215 inside.faceIdx = intersection.indexInInside();
216 outside.faceIdx = intersection.indexInOutside();
217
218 // Set NNC face properties to zero
219 if (inside.faceIdx == -1) {
220 const auto id = details::isIdTPSA(inside.elemIdx, outside.elemIdx);
221 weightsAvgMap.insert_or_assign(id, 0.0);
222 weightsProdMap.insert_or_assign(id, 0.0);
223 distanceMap.insert_or_assign(id, 1.0);
224 faceNormalMap.insert_or_assign(id, DimVector{0.0, 0.0, 0.0});
225
226 continue;
227 }
228
229 // Compute cell properties from grid
230 typename std::is_same<Grid, Dune::CpGrid>::type isCpGrid;
231 computeCellProperties(intersection,
232 inside,
233 outside,
234 faceNormal,
235 isCpGrid);
236
237 // Compute face properties
238 const auto id = details::isIdTPSA(inside.elemIdx, outside.elemIdx);
239 Scalar dist_in = computeDistance_(distanceVector_(inside.faceCenter, inside.elemIdx), faceNormal);
240 Scalar dist_out = computeDistance_(distanceVector_(outside.faceCenter, outside.elemIdx), faceNormal);
241 distanceMap.insert_or_assign(id, dist_in + dist_out);
242
243 Scalar weight_in = computeWeight_(dist_in, sModulus_[inside.elemIdx]);
244 Scalar weight_out = computeWeight_(dist_out, sModulus_[outside.elemIdx]);
245 Scalar weightsAvg = weight_in / (weight_in + weight_out);
246 Scalar weightsProd = weight_in * weight_out;
247 weightsAvgMap.insert_or_assign(id, weightsAvg);
248 weightsProdMap.insert_or_assign(id, weightsProd);
249
250 faceNormalMap.insert_or_assign(id, faceNormal);
251 }
252 }
253 }
254
255 // Clear centroid cache
256 centroids_cache_.clear();
257
258#ifdef _OPENMP
259#pragma omp parallel sections
260#endif
261 {
262#ifdef _OPENMP
263#pragma omp section
264#endif
265 weightsAvgMap.finalize();
266#ifdef _OPENMP
267#pragma omp section
268#endif
269 weightsProdMap.finalize();
270#ifdef _OPENMP
271#pragma omp section
272#endif
273 distanceMap.finalize();
274#ifdef _OPENMP
275#pragma omp section
276#endif
277 faceNormalMap.finalize();
278#ifdef _OPENMP
279#pragma omp section
280#endif
281 weightsAvgBoundaryMap.finalize();
282#ifdef _OPENMP
283#pragma omp section
284#endif
285 weightsProdBoundaryMap.finalize();
286#ifdef _OPENMP
287#pragma omp section
288#endif
289 distanceBoundaryMap.finalize();
290#ifdef _OPENMP
291#pragma omp section
292#endif
293 faceNormalBoundaryMap.finalize();
294 }
295}
296
304template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
306weightAverage(unsigned elemIdx1, unsigned elemIdx2) const
307{
308 auto tmp_whgt = weightsAvg_.at(details::isIdTPSA(elemIdx1, elemIdx2));
309
310 auto cartIdx1 = lookUpCartesianData_.
311 template getFieldPropCartesianIdx<Grid>(elemIdx1);
312 auto cartIdx2 = lookUpCartesianData_.
313 template getFieldPropCartesianIdx<Grid>(elemIdx2);
314 if (cartIdx1 < cartIdx2) {
315 return tmp_whgt;
316 }
317 else {
318 return 1.0 - tmp_whgt;
319 }
320}
321
329template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
331weightAverageBoundary(unsigned elemIdx, unsigned boundaryFaceIdx) const
332{
333 return weightsAvgBoundary_.at(std::make_pair(elemIdx, boundaryFaceIdx));
334}
335
343template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
345weightProduct(unsigned elemIdx1, unsigned elemIdx2) const
346{
347 return weightsProd_.at(details::isIdTPSA(elemIdx1, elemIdx2));
348}
349
357template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
359weightProductBoundary(unsigned elemIdx, unsigned boundaryFaceIdx) const
360{
361 return weightsProdBoundary_.at(std::make_pair(elemIdx, boundaryFaceIdx));
362}
363
371template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
373normalDistance(unsigned elemIdx1, unsigned elemIdx2) const
374{
375 return distance_.at(details::isIdTPSA(elemIdx1, elemIdx2));
376}
377
385template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
387normalDistanceBoundary(unsigned elemIdx, unsigned boundaryFaceIdx) const
388{
389 return distanceBoundary_.at(std::make_pair(elemIdx, boundaryFaceIdx));
390}
391
401template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
404cellFaceNormal(unsigned elemIdx1, unsigned elemIdx2)
405{
406 auto cartIdx1 = lookUpCartesianData_.
407 template getFieldPropCartesianIdx<Grid>(elemIdx1);
408 auto cartIdx2 = lookUpCartesianData_.
409 template getFieldPropCartesianIdx<Grid>(elemIdx2);
410 int sign = (cartIdx1 < cartIdx2) ? 1 : -1;
411 return sign * faceNormal_.at(details::isIdTPSA(elemIdx1, elemIdx2));
412}
413
423template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
426cellFaceNormalBoundary(unsigned elemIdx, unsigned boundaryFaceIdx) const
427{
428 return faceNormalBoundary_.at(std::make_pair(elemIdx, boundaryFaceIdx));
429}
430
431// /////
432// Protected functions
433// ////
441template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
443computeDistance_(const DimVector& distVec, const DimVector& faceNormal)
444{
445 return std::abs(Dune::dot(faceNormal, distVec));
446}
447
458template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
459template<class Intersection>
461computeCellProperties([[maybe_unused]] const Intersection& intersection,
462 [[maybe_unused]] FaceInfo& inside,
463 [[maybe_unused]] FaceInfo& outside,
464 [[maybe_unused]] DimVector& faceNormal,
465 /*isCpGrid=*/std::false_type) const
466{
467 throw std::runtime_error("TPSA not implemented for DUNE grid types other than CpGrid!");
468}
469
480template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
481template<class Intersection>
483computeCellProperties(const Intersection& intersection,
484 FaceInfo& inside,
485 FaceInfo& outside,
486 DimVector& faceNormal,
487 /*isCpGrid=*/std::true_type) const
488{
489 int faceIdx = intersection.id();
490 if (grid_.maxLevel() == 0) {
491 // Face center coordinates
492 inside.faceCenter = grid_.faceCenterEcl(inside.elemIdx, inside.faceIdx, intersection);
493 outside.faceCenter = grid_.faceCenterEcl(outside.elemIdx, outside.faceIdx, intersection);
494
495 // Face normal, ensuring it points from cell with lower to higher (global grid) index
496 faceNormal = grid_.faceNormal(faceIdx);
497 auto cartFaceCell0 = lookUpCartesianData_.
498 template getFieldPropCartesianIdx<Grid>(grid_.faceCell(faceIdx, 0));
499 auto cartFaceCell1 = lookUpCartesianData_.
500 template getFieldPropCartesianIdx<Grid>(grid_.faceCell(faceIdx, 1));
501 if (cartFaceCell0 > cartFaceCell1) {
502 faceNormal *= -1;
503 }
504 }
505 else {
506 throw std::runtime_error("TPSA not implemented with LGR");
507 }
508
509}
510
518template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
520computeWeight_(const Scalar distance, const Scalar smod)
521{
522 return distance / smod;
523}
524
532template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
535distanceVector_(const DimVector& faceCenter, const unsigned& cellIdx) const
536{
537 const auto& cellCenter = centroids_cache_.empty() ? centroids_(cellIdx)
538 : centroids_cache_[cellIdx];
539 DimVector x = faceCenter;
540 for (unsigned dimIdx = 0; dimIdx < dimWorld; ++dimIdx) {
541 x[dimIdx] -= cellCenter[dimIdx];
542 }
543
544 return x;
545}
546
554template<class Grid, class GridView, class ElementMapper, class CartesianIndexMapper, class Scalar>
557{
558 unsigned numElem = gridView_.size(/*codim=*/0);
559 sModulus_.resize(numElem);
560
561 const auto& fp = eclState_.fieldProps();
562 std::vector<double> sModulusData;
563 if (fp.has_double("SMODULUS")) {
564 sModulusData = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "SMODULUS");
565 }
566 else if (fp.has_double("YMODULE") && fp.has_double("LAME")) {
567 // Convert from Young's modulus and Lame's first parameter
568 const std::vector<double>& ymodulus = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "YMODULE");
569 const std::vector<double>& lameParam = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "LAME");
570 sModulusData.resize(numElem);
571 for (std::size_t i = 0; i < sModulusData.size(); ++i) {
572 const double r = std::sqrt(ymodulus[i] * ymodulus[i] + 9 * lameParam[i] * lameParam[i]
573 + 2 * ymodulus[i] * lameParam[i]);
574 sModulusData[i] = (ymodulus[i] - 3 * lameParam[i] + r) / 4.0;
575 }
576 }
577 else if (fp.has_double("YMODULE") && fp.has_double("PRATIO")) {
578 const std::vector<double>& ymodulus = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "YMODULE");
579 const std::vector<double>& pratio = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "PRATIO");
580 sModulusData.resize(numElem);
581 for (std::size_t i = 0; i < sModulusData.size(); ++i) {
582 sModulusData[i] = ymodulus[i] / (2 * (1 + pratio[i]));
583 }
584 }
585 else if (fp.has_double("LAME") && fp.has_double("PRATIO")) {
586 const std::vector<double>& lameParam = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "LAME");
587 const std::vector<double>& pratio = this->lookUpData_.assignFieldPropsDoubleOnLeaf(fp, "PRATIO");
588 sModulusData.resize(numElem);
589 for (std::size_t i = 0; i < sModulusData.size(); ++i) {
590 sModulusData[i] = lameParam[i] * (1 - 2 * pratio[i]) / (2 * pratio[i]);
591 }
592 }
593 else {
594 throw std::logic_error("Cannot read shear modulus data from ecl state, SMODULUS keyword missing, "
595 "and one of the following keyword pairs are missing for conversion: "
596 "(YMODULE, LAME), (YMODULE, PRATIO) and (LAME, PRATIO)!");
597 }
598
599 // Assign shear modulus
600 for (std::size_t dofIdx = 0; dofIdx < numElem; ++ dofIdx) {
601 sModulus_[dofIdx] = sModulusData[dofIdx];
602 }
603}
604
605} // namespace Opm
606
607#endif
void extractSModulus_()
Extract shear modulus from eclState.
Definition: FacePropertiesTPSA_impl.hpp:556
void finishInit()
Compute TPSA face properties.
Definition: FacePropertiesTPSA_impl.hpp:96
void computeCellProperties(const Intersection &intersection, FaceInfo &inside, FaceInfo &outside, DimVector &faceNormal, std::false_type) const
Compute face properties from general DUNE grid.
Definition: FacePropertiesTPSA_impl.hpp:461
Scalar weightProduct(unsigned elemIdx1, unsigned elemIdx2) const
Product of weights at interface between two elements.
Definition: FacePropertiesTPSA_impl.hpp:345
DimVector cellFaceNormal(unsigned elemIdx1, unsigned elemIdx2)
Cell face normal at interface between two elements.
Definition: FacePropertiesTPSA_impl.hpp:404
Dune::FieldVector< Scalar, dimWorld > DimVector
Definition: FacePropertiesTPSA.hpp:53
Scalar weightAverage(unsigned elemIdx1, unsigned elemIdx2) const
Average (half-)weight at interface between two elements.
Definition: FacePropertiesTPSA_impl.hpp:306
Scalar normalDistanceBoundary(unsigned elemIdx1, unsigned boundaryFaceIdx) const
Distance to boundary interface.
Definition: FacePropertiesTPSA_impl.hpp:387
Scalar weightAverageBoundary(unsigned elemIdx1, unsigned boundaryFaceIdx) const
Average (half-)weight at boundary interface.
Definition: FacePropertiesTPSA_impl.hpp:331
const DimVector & cellFaceNormalBoundary(unsigned elemIdx1, unsigned boundaryFaceIdx) const
Cell face normal of boundary interface.
Definition: FacePropertiesTPSA_impl.hpp:426
Scalar computeWeight_(const Scalar distance, const Scalar smod)
Compute weight ratio between distance and shear modulus.
Definition: FacePropertiesTPSA_impl.hpp:520
void update()
Compute TPSA face properties.
Definition: FacePropertiesTPSA_impl.hpp:106
Scalar computeDistance_(const DimVector &distVec, const DimVector &faceNormal)
Compute normal distance from cell center to face center.
Definition: FacePropertiesTPSA_impl.hpp:443
DimVector distanceVector_(const DimVector &faceCenter, const unsigned &cellIdx) const
Distance vector from cell center to face center.
Definition: FacePropertiesTPSA_impl.hpp:535
Scalar normalDistance(unsigned elemIdx1, unsigned elemIdx2) const
Distance between two elements.
Definition: FacePropertiesTPSA_impl.hpp:373
FacePropertiesTPSA(const EclipseState &eclState, const GridView &gridView, const CartesianIndexMapper &cartMapper, const Grid &grid, std::function< std::array< double, dimWorld >(int)> centroids)
Constructor.
Definition: FacePropertiesTPSA_impl.hpp:77
Scalar weightProductBoundary(unsigned elemIdx1, unsigned boundaryFaceIdx) const
Product of weights at boundary interface.
Definition: FacePropertiesTPSA_impl.hpp:359
static unsigned maxThreads()
Return the maximum number of threads of the current process.
Definition: threadmanager.hpp:66
constexpr unsigned elemIdxShift
Definition: FacePropertiesTPSA_impl.hpp:52
std::uint64_t isIdTPSA(std::uint32_t elemIdx1, std::uint32_t elemIdx2)
Definition: FacePropertiesTPSA_impl.hpp:54
Definition: blackoilbioeffectsmodules.hh:45
Definition: FacePropertiesTPSA.hpp:83
int faceIdx
Definition: FacePropertiesTPSA.hpp:85
unsigned cartElemIdx
Definition: FacePropertiesTPSA.hpp:87
unsigned elemIdx
Definition: FacePropertiesTPSA.hpp:86
DimVector faceCenter
Definition: FacePropertiesTPSA.hpp:84