Geometry.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Geometry.hpp
4//
5// Created: Fri May 29 23:29:24 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// B�rd Skaflestad <bard.skaflestad@sintef.no>
9// Antonella Ritorto <antonella.ritorto@opm-op.com>
10//
11// $Date$
12//
13// $Revision$
14//
15//===========================================================================
16
17/*
18 Copyright 2009, 2010, 2011 SINTEF ICT, Applied Mathematics.
19 Copyright 2009, 2010, 2011, 2022 Equinor ASA.
20
21 This file is part of the Open Porous Media project (OPM).
22
23 OPM is free software: you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation, either version 3 of the License, or
26 (at your option) any later version.
27
28 OPM is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with OPM. If not, see <http://www.gnu.org/licenses/>.
35*/
36
37#ifndef OPM_GEOMETRY_HEADER
38#define OPM_GEOMETRY_HEADER
39
40#include <cmath>
41
42// Warning suppression for Dune includes.
44
45#include <dune/common/version.hh>
46#include <dune/geometry/referenceelements.hh>
47#include <dune/grid/common/geometry.hh>
48
49#include <dune/geometry/type.hh>
50
57#include <opm/common/utility/SparseTable.hpp>
58
60
61namespace Dune
62{
63 namespace cpgrid
64 {
65
74 template <int mydim, int cdim>
76 {
77 };
78
79
80
81
83 template <int cdim> // GridImp arg never used
84 class Geometry<0, cdim>
85 {
86 static_assert(cdim == 3, "");
87 public:
89 enum { dimension = 3 };
91 enum { mydimension = 0};
93 enum { coorddimension = cdim };
95 enum { dimensionworld = 3 };
96
98 using ctype = double;
100 using Volume = ctype;
101
103 typedef FieldVector<ctype, mydimension> LocalCoordinate;
105 typedef FieldVector<ctype, coorddimension> GlobalCoordinate;
106
108 typedef FieldMatrix< ctype, coorddimension, mydimension > Jacobian;
110 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse;
112 typedef FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed;
114 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed;
115
116
120 : pos_(pos)
121 {
122 }
123
126 : pos_(0.0)
127 {
128 }
129
132 {
133 return pos_;
134 }
135
138 {
139 // return 0 to make the geometry check happy.
140 return LocalCoordinate(0.0);
141 }
142
145 {
146 return volume();
147 }
148
150 GeometryType type() const
151 {
152 return Dune::GeometryTypes::cube(mydimension);
153 }
154
156 int corners() const
157 {
158 return 1;
159 }
160
163 {
164 static_cast<void>(cor);
165 assert(cor == 0);
166 return pos_;
167 }
168
171 {
172 return 1.0;
173 }
174
177 {
178 return pos_;
179 }
180
182 JacobianTransposed
183 jacobianTransposed(const LocalCoordinate& /* local */) const
184 {
185
186 // Meaningless to call jacobianTransposed() on singular geometries. But we need to make DUNE happy.
187 return {};
188 }
189
191 JacobianInverseTransposed
193 {
194 // Meaningless to call jacobianInverseTransposed() on singular geometries. But we need to make DUNE happy.
195 return {};
196 }
197
199 Jacobian
200 jacobian(const LocalCoordinate& /*local*/) const
201 {
202 return {};
203 }
204
206 JacobianInverse
207 jacobianInverse(const LocalCoordinate& /*local*/) const
208 {
209 return {};
210 }
211
213 bool affine() const
214 {
215 return true;
216 }
217
218 private:
219 GlobalCoordinate pos_;
220 }; // class Geometry<0,cdim>
221
222
223
224
227 template <int cdim> // GridImp arg never used
228 class Geometry<2, cdim>
229 {
230 static_assert(cdim == 3, "");
231 public:
233 enum { dimension = 3 };
235 enum { mydimension = 2 };
237 enum { coorddimension = cdim };
239 enum { dimensionworld = 3 };
240
242 using ctype = double;
244 using Volume = ctype;
245
247 typedef FieldVector<ctype, mydimension> LocalCoordinate;
249 typedef FieldVector<ctype, coorddimension> GlobalCoordinate;
250
252 typedef FieldMatrix< ctype, coorddimension, mydimension > Jacobian;
254 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse;
256 typedef FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed;
258 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed;
259
264 ctype vol)
265 : pos_(pos), vol_(vol)
266 {
267 }
268
271 : pos_(0.0), vol_(0.0)
272 {
273 }
274
277 {
278 OPM_THROW(std::runtime_error, "Geometry::global() meaningless on singular geometry.");
279 }
280
283 {
284 OPM_THROW(std::runtime_error, "Geometry::local() meaningless on singular geometry.");
285 }
286
290 {
291 return vol_;
292 }
293
295 GeometryType type() const
296 {
297 return Dune::GeometryTypes::none(mydimension);
298 }
299
302 int corners() const
303 {
304 return 0;
305 }
306
308 GlobalCoordinate corner(int /* cor */) const
309 {
310 // Meaningless call to cpgrid::Geometry::corner(int):
311 //"singular geometry has no corners.
312 // But the DUNE tests assume at least one corner.
313 return GlobalCoordinate( 0.0 );
314 }
315
318 {
319 return vol_;
320 }
321
324 {
325 return pos_;
326 }
327
329 const FieldMatrix<ctype, mydimension, coorddimension>&
330 jacobianTransposed(const LocalCoordinate& /* local */) const
331 {
332 OPM_THROW(std::runtime_error, "Meaningless to call jacobianTransposed() on singular geometries.");
333 }
334
336 const FieldMatrix<ctype, coorddimension, mydimension>&
338 {
339 OPM_THROW(std::runtime_error, "Meaningless to call jacobianInverseTransposed() on singular geometries.");
340 }
341
343 Jacobian
344 jacobian(const LocalCoordinate& /*local*/) const
345 {
346 return jacobianTransposed({}).transposed();
347 }
348
350 JacobianInverse
351 jacobianInverse(const LocalCoordinate& /*local*/) const
352 {
353 return jacobianInverseTransposed({}).transposed();
354 }
355
357 bool affine() const
358 {
359 return true;
360 }
361
362 private:
363 GlobalCoordinate pos_;
364 ctype vol_;
365 };
366
367
368
369
370
372 template <int cdim>
373 class Geometry<3, cdim>
374 {
375 static_assert(cdim == 3, "");
376 public:
378 enum { dimension = 3 };
380 enum { mydimension = 3 };
382 enum { coorddimension = cdim };
384 enum { dimensionworld = 3 };
385
387 using ctype = double;
389 using Volume = ctype;
390
392 typedef FieldVector<ctype, mydimension> LocalCoordinate;
394 typedef FieldVector<ctype, coorddimension> GlobalCoordinate;
395
397 typedef FieldMatrix< ctype, coorddimension, mydimension > Jacobian;
399 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse;
401 typedef FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed;
403 typedef FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed;
404
405 typedef Dune::Impl::FieldMatrixHelper< double > MatrixHelperType;
406
416 ctype vol,
417 std::shared_ptr<const EntityVariable<cpgrid::Geometry<0, 3>, 3>> allcorners_ptr,
418 const int* corner_indices)
419 : pos_(pos), vol_(vol),
420 allcorners_(allcorners_ptr), cor_idx_(corner_indices)
421 {
422 assert(allcorners_ && corner_indices);
423 }
424
427 : pos_(0.0), vol_(0.0), allcorners_(0), cor_idx_(0)
428 {
429 }
430
436 GlobalCoordinate global(const LocalCoordinate& local_coord) const
437 {
438 static_assert(mydimension == 3, "");
439 static_assert(coorddimension == 3, "");
440 // uvw = { (1-u, 1-v, 1-w), (u, v, w) }
441 LocalCoordinate uvw[2] = { LocalCoordinate(1.0), local_coord };
442 uvw[0] -= local_coord;
443 // Access pattern for uvw matching ordering of corners.
444 const int pat[8][3] = { { 0, 0, 0 },
445 { 1, 0, 0 },
446 { 0, 1, 0 },
447 { 1, 1, 0 },
448 { 0, 0, 1 },
449 { 1, 0, 1 },
450 { 0, 1, 1 },
451 { 1, 1, 1 } };
452 GlobalCoordinate xyz(0.0);
453 for (int i = 0; i < 8; ++i) {
454 GlobalCoordinate corner_contrib = corner(i);
455 double factor = 1.0;
456 for (int j = 0; j < 3; ++j) {
457 factor *= uvw[pat[i][j]][j];
458 }
459 corner_contrib *= factor;
460 xyz += corner_contrib;
461 }
462 return xyz;
463 }
464
468 {
469 static_assert(mydimension == 3, "");
470 static_assert(coorddimension == 3, "");
471 // This code is modified from dune/grid/genericgeometry/mapping.hh
472 // \todo: Implement direct computation.
473 const ctype epsilon = 1e-12;
474 auto refElement = Dune::ReferenceElements<ctype, 3>::cube();
475 LocalCoordinate x = refElement.position(0,0);
477 do {
478 // DF^n dx^n = F^n, x^{n+1} -= dx^n
479 JacobianTransposed JT = jacobianTransposed(x);
480 GlobalCoordinate z = global(x);
481 z -= y;
482 MatrixHelperType::template xTRightInvA<3, 3>(JT, z, dx );
483 x -= dx;
484 } while (dx.two_norm2() > epsilon*epsilon);
485 return x;
486 }
487
492 Volume integrationElement(const LocalCoordinate& local_coord) const
493 {
494 JacobianTransposed Jt = jacobianTransposed(local_coord);
495 return MatrixHelperType::template sqrtDetAAT<3, 3>(Jt);
496 }
497
500 GeometryType type() const
501 {
502 return Dune::GeometryTypes::cube(mydimension);
503 }
504
507 int corners() const
508 {
509 return 8;
510 }
511
514 {
515 assert(allcorners_ && cor_idx_);
516 return (allcorners_->data())[cor_idx_[cor]].center();
517 }
518
521 {
522 return vol_;
523 }
524
526 vol_ = volume;
527 }
528
531 {
532 return pos_;
533 }
534
541 const JacobianTransposed
542 jacobianTransposed(const LocalCoordinate& local_coord) const
543 {
544 static_assert(mydimension == 3, "");
545 static_assert(coorddimension == 3, "");
546
547 // uvw = { (1-u, 1-v, 1-w), (u, v, w) }
548 LocalCoordinate uvw[2] = { LocalCoordinate(1.0), local_coord };
549 uvw[0] -= local_coord;
550 // Access pattern for uvw matching ordering of corners.
551 const int pat[8][3] = { { 0, 0, 0 },
552 { 1, 0, 0 },
553 { 0, 1, 0 },
554 { 1, 1, 0 },
555 { 0, 0, 1 },
556 { 1, 0, 1 },
557 { 0, 1, 1 },
558 { 1, 1, 1 } };
559 JacobianTransposed Jt(0.0);
560 for (int i = 0; i < 8; ++i) {
561 for (int deriv = 0; deriv < 3; ++deriv) {
562 // This part contributing to dg/du_{deriv}
563 double factor = 1.0;
564 for (int j = 0; j < 3; ++j) {
565 factor *= (j != deriv) ? uvw[pat[i][j]][j]
566 : (pat[i][j] == 0 ? -1.0 : 1.0);
567 }
568 GlobalCoordinate corner_contrib = corner(i);
569 corner_contrib *= factor;
570 Jt[deriv] += corner_contrib; // using FieldMatrix row access.
571 }
572 }
573 return Jt;
574 }
575
577 const JacobianInverseTransposed
579 {
580 JacobianInverseTransposed Jti = jacobianTransposed(local_coord);
581 Jti.invert();
582 return Jti;
583 }
584
586 Jacobian
587 jacobian(const LocalCoordinate& local_coord) const
588 {
589 return jacobianTransposed(local_coord).transposed();
590 }
591
593 JacobianInverse
594 jacobianInverse(const LocalCoordinate& local_coord) const
595 {
596 return jacobianInverseTransposed(local_coord).transposed();
597 }
598
600 bool affine() const
601 {
602 return false;
603 }
604
623 typedef Dune::FieldVector<double,3> PointType;
624 void refineCellifiedPatch(const std::array<int,3>& cells_per_dim,
625 DefaultGeometryPolicy& all_geom,
626 std::vector<std::array<int,8>>& refined_cell_to_point,
627 cpgrid::OrientedEntityTable<0,1>& refined_cell_to_face,
628 Opm::SparseTable<int>& refined_face_to_point,
629 cpgrid::OrientedEntityTable<1,0>& refined_face_to_cell,
631 cpgrid::SignedEntityVariable<PointType, 1>& refined_face_normals,
632 const std::array<int,3>& patch_dim,
633 const std::vector<double>& widthsX,
634 const std::vector<double>& lengthsY,
635 const std::vector<double>& heightsZ) const;
636
637 private:
638 GlobalCoordinate pos_;
639 double vol_;
640 std::shared_ptr<const EntityVariable<Geometry<0, 3>,3>> allcorners_; // For dimension 3 only
641 const int* cor_idx_; // For dimension 3 only
642
660 const std::tuple< enum face_tag, int,
661 std::array<int, 4>, std::vector<cpgrid::EntityRep<0>>,
663 getIndicesFace(int l, int m, int n, int constant_direction, const std::array<int, 3>& cells_per_dim) const;
664 };
665
666 // Reference element for a given geometry
667 template< int mydim, int cdim >
668 auto referenceElement(const cpgrid::Geometry<mydim,cdim>& geo) -> decltype(referenceElement<double,mydim>(geo.type()))
669 {
670 return referenceElement<double,mydim>(geo.type());
671 }
672
673 } // namespace cpgrid
674} // namespace Dune
675
677
678#endif // OPM_GEOMETRY_HEADER
Definition: DefaultGeometryPolicy.hpp:53
A class design to hold a variable with a value for each entity of the given codimension,...
Definition: EntityRep.hpp:271
FieldVector< ctype, coorddimension > GlobalCoordinate
Range type of.
Definition: Geometry.hpp:105
bool affine() const
The mapping implemented by this geometry is constant, therefore affine.
Definition: Geometry.hpp:213
FieldMatrix< ctype, coorddimension, mydimension > Jacobian
Type of Jacobian matrix.
Definition: Geometry.hpp:108
JacobianInverse jacobianInverse(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:207
Volume volume() const
Volume of vertex is arbitrarily set to 1.
Definition: Geometry.hpp:170
FieldVector< ctype, mydimension > LocalCoordinate
Domain type of.
Definition: Geometry.hpp:103
Jacobian jacobian(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:200
double ctype
Coordinate element type.
Definition: Geometry.hpp:98
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed
Type of the inverse of the transposed Jacobian matrix.
Definition: Geometry.hpp:114
Geometry(const GlobalCoordinate &pos)
Construct from vertex position.
Definition: Geometry.hpp:119
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse
Type of inverse of Jacobian matrix.
Definition: Geometry.hpp:110
ctype Volume
Number type used for the geometry volume.
Definition: Geometry.hpp:100
JacobianTransposed jacobianTransposed(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:183
GeometryType type() const
Using the cube type for vertices.
Definition: Geometry.hpp:150
FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed
Type of transposed Jacobian matrix.
Definition: Geometry.hpp:112
GlobalCoordinate corner(int cor) const
Returns the single corner: the vertex itself.
Definition: Geometry.hpp:162
int corners() const
A vertex is defined by a single corner.
Definition: Geometry.hpp:156
const GlobalCoordinate & center() const
Returns the centroid of the geometry.
Definition: Geometry.hpp:176
Geometry()
Default constructor, giving a non-valid geometry.
Definition: Geometry.hpp:125
const GlobalCoordinate & global(const LocalCoordinate &) const
Returns the position of the vertex.
Definition: Geometry.hpp:131
Volume integrationElement(const LocalCoordinate &) const
Returns 1 for the vertex geometry.
Definition: Geometry.hpp:144
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:192
LocalCoordinate local(const GlobalCoordinate &) const
Meaningless for the vertex geometry.
Definition: Geometry.hpp:137
JacobianInverse jacobianInverse(const LocalCoordinate &) const
The inverse of the jacobian.
Definition: Geometry.hpp:351
Volume volume() const
Volume (area, actually) of intersection.
Definition: Geometry.hpp:317
const FieldMatrix< ctype, mydimension, coorddimension > & jacobianTransposed(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:330
const GlobalCoordinate & center() const
Returns the centroid of the geometry.
Definition: Geometry.hpp:323
Volume integrationElement(const LocalCoordinate &) const
Definition: Geometry.hpp:289
int corners() const
Definition: Geometry.hpp:302
LocalCoordinate local(const GlobalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:282
double ctype
Coordinate element type.
Definition: Geometry.hpp:242
FieldMatrix< ctype, coorddimension, mydimension > Jacobian
Type of Jacobian matrix.
Definition: Geometry.hpp:252
bool affine() const
Since integrationElement() is constant, returns true.
Definition: Geometry.hpp:357
ctype Volume
Number type used for the geometry volume.
Definition: Geometry.hpp:244
FieldVector< ctype, mydimension > LocalCoordinate
Domain type of.
Definition: Geometry.hpp:247
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed
Type of the inverse of the transposed Jacobian matrix.
Definition: Geometry.hpp:258
const FieldMatrix< ctype, coorddimension, mydimension > & jacobianInverseTransposed(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:337
GeometryType type() const
We use the singular type (None) for intersections.
Definition: Geometry.hpp:295
FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed
Type of transposed Jacobian matrix.
Definition: Geometry.hpp:256
Geometry()
Default constructor, giving a non-valid geometry.
Definition: Geometry.hpp:270
const GlobalCoordinate & global(const LocalCoordinate &) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:276
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse
Type of inverse of Jacobian matrix.
Definition: Geometry.hpp:254
Geometry(const GlobalCoordinate &pos, ctype vol)
Construct from centroid and volume (1- and 0-moments).
Definition: Geometry.hpp:263
Jacobian jacobian(const LocalCoordinate &) const
The jacobian.
Definition: Geometry.hpp:344
FieldVector< ctype, coorddimension > GlobalCoordinate
Range type of.
Definition: Geometry.hpp:249
GlobalCoordinate corner(int) const
This method is meaningless for singular geometries.
Definition: Geometry.hpp:308
const JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local_coord) const
Inverse of Jacobian transposed.
Definition: Geometry.hpp:578
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverseTransposed
Type of the inverse of the transposed Jacobian matrix.
Definition: Geometry.hpp:403
bool affine() const
The mapping implemented by this geometry is not generally affine.
Definition: Geometry.hpp:600
void set_volume(ctype volume)
Definition: Geometry.hpp:525
Volume volume() const
Cell volume.
Definition: Geometry.hpp:520
FieldVector< ctype, coorddimension > GlobalCoordinate
Range type of.
Definition: Geometry.hpp:394
GlobalCoordinate corner(int cor) const
Get the cor-th of 8 corners of the hexahedral base cell.
Definition: Geometry.hpp:513
FieldMatrix< ctype, mydimension, coorddimension > JacobianTransposed
Type of transposed Jacobian matrix.
Definition: Geometry.hpp:401
Geometry(const GlobalCoordinate &pos, ctype vol, std::shared_ptr< const EntityVariable< cpgrid::Geometry< 0, 3 >, 3 > > allcorners_ptr, const int *corner_indices)
Construct from center, volume (1- and 0-moments) and corners.
Definition: Geometry.hpp:415
GeometryType type() const
Definition: Geometry.hpp:500
const JacobianTransposed jacobianTransposed(const LocalCoordinate &local_coord) const
Jacobian transposed. J^T_{ij} = (dg_j/du_i) where g is the mapping from the reference domain,...
Definition: Geometry.hpp:542
GlobalCoordinate global(const LocalCoordinate &local_coord) const
Definition: Geometry.hpp:436
Geometry()
Default constructor, giving a non-valid geometry.
Definition: Geometry.hpp:426
FieldMatrix< ctype, coorddimension, mydimension > JacobianInverse
Type of inverse of Jacobian matrix.
Definition: Geometry.hpp:399
ctype Volume
Number type used for the geometry volume.
Definition: Geometry.hpp:389
LocalCoordinate local(const GlobalCoordinate &y) const
Definition: Geometry.hpp:467
int corners() const
Definition: Geometry.hpp:507
const GlobalCoordinate & center() const
Returns the centroid of the geometry.
Definition: Geometry.hpp:530
Jacobian jacobian(const LocalCoordinate &local_coord) const
The jacobian.
Definition: Geometry.hpp:587
Dune::FieldVector< double, 3 > PointType
Refine a single cell considering different widths, lengths, and heights.
Definition: Geometry.hpp:623
FieldVector< ctype, mydimension > LocalCoordinate
Domain type of.
Definition: Geometry.hpp:392
Dune::Impl::FieldMatrixHelper< double > MatrixHelperType
Definition: Geometry.hpp:405
Volume integrationElement(const LocalCoordinate &local_coord) const
Definition: Geometry.hpp:492
JacobianInverse jacobianInverse(const LocalCoordinate &local_coord) const
The inverse of the jacobian.
Definition: Geometry.hpp:594
FieldMatrix< ctype, coorddimension, mydimension > Jacobian
Type of Jacobian matrix.
Definition: Geometry.hpp:397
double ctype
Coordinate element type.
Definition: Geometry.hpp:387
Definition: Geometry.hpp:76
auto referenceElement(const Entity< codim > &)
Definition: Entity.hpp:334
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
T volume(const Point< T, 3 > *c)
Computes the volume of a 3D simplex (embedded i 3D space).
Definition: Volumes.hpp:138
face_tag
Definition: preprocess.h:66