Loading...
Searching...
No Matches
axisalignedcubegeometry.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_GEOMETRY_AXISALIGNED_CUBE_GEOMETRY_HH
#define DUNE_GEOMETRY_AXISALIGNED_CUBE_GEOMETRY_HH
#include <bitset>
#include <dune/common/fvector.hh>
#include <dune/common/fmatrix.hh>
#include <dune/common/diagonalmatrix.hh>
#include <dune/geometry/referenceelements.hh>
#include <dune/geometry/type.hh>
namespace Dune {
template <class CoordType, unsigned int dim, unsigned int coorddim>
class AxisAlignedCubeGeometry
{
public:
constexpr static int mydimension = dim;
constexpr static int coorddimension = coorddim;
typedef CoordType ctype;
typedef FieldVector<ctype,dim> LocalCoordinate;
typedef FieldVector<ctype,coorddim> GlobalCoordinate;
typedef ctype Volume;
typedef typename std::conditional<dim==coorddim,
DiagonalMatrix<ctype,dim>,
FieldMatrix<ctype,dim,coorddim> >::type JacobianTransposed;
typedef typename std::conditional<dim==coorddim,
DiagonalMatrix<ctype,dim>,
FieldMatrix<ctype,coorddim,dim> >::type JacobianInverseTransposed;
using Jacobian = std::conditional_t<dim==coorddim, DiagonalMatrix<ctype,dim>, FieldMatrix<ctype,coorddim,dim> >;
using JacobianInverse = std::conditional_t<dim==coorddim, DiagonalMatrix<ctype,dim>, FieldMatrix<ctype,dim,coorddim> >;
AxisAlignedCubeGeometry () = default;
AxisAlignedCubeGeometry(const Dune::FieldVector<ctype,coorddim> lower,
const Dune::FieldVector<ctype,coorddim> upper)
: lower_(lower),
upper_(upper),
axes_()
{
static_assert(dim==coorddim, "Use this constructor only if dim==coorddim!");
// all 'true', but is never actually used
axes_ = (1<<coorddim)-1;
}
AxisAlignedCubeGeometry(const Dune::FieldVector<ctype,coorddim> lower,
const Dune::FieldVector<ctype,coorddim> upper,
const std::bitset<coorddim>& axes)
: lower_(lower),
upper_(upper),
axes_(axes)
{
assert(axes.count()==dim);
for (size_t i=0; i<coorddim; i++)
if (not axes_[i])
upper_[i] = lower_[i];
}
AxisAlignedCubeGeometry(const Dune::FieldVector<ctype,coorddim> lower)
: lower_(lower)
{}
GeometryType type() const
{
return GeometryTypes::cube(dim);
}
GlobalCoordinate global(const LocalCoordinate& local) const
{
GlobalCoordinate result;
if (dim == coorddim) { // fast case
for (size_t i=0; i<coorddim; i++)
result[i] = lower_[i] + local[i]*(upper_[i] - lower_[i]);
} else if (dim == 0) { // a vertex -- the other fast case
result = lower_; // hope for named-return-type-optimization
} else { // slow case
size_t lc=0;
for (size_t i=0; i<coorddim; i++)
result[i] = (axes_[i])
? lower_[i] + local[lc++]*(upper_[i] - lower_[i])
: lower_[i];
}
return result;
}
LocalCoordinate local(const GlobalCoordinate& global) const
{
LocalCoordinate result;
if (dim == coorddim) { // fast case
for (size_t i=0; i<dim; i++)
result[i] = (global[i] - lower_[i]) / (upper_[i] - lower_[i]);
} else if (dim != 0) { // slow case
size_t lc=0;
for (size_t i=0; i<coorddim; i++)
if (axes_[i])
result[lc++] = (global[i] - lower_[i]) / (upper_[i] - lower_[i]);
}
return result;
}
JacobianTransposed jacobianTransposed([[maybe_unused]] const LocalCoordinate& local) const
{
JacobianTransposed result;
// Actually compute the result. Uses different methods depending
// on what kind of matrix JacobianTransposed is.
jacobianTransposed(result);
return result;
}
JacobianInverseTransposed jacobianInverseTransposed([[maybe_unused]] const LocalCoordinate& local) const
{
JacobianInverseTransposed result;
// Actually compute the result. Uses different methods depending
// on what kind of matrix JacobianTransposed is.
jacobianInverseTransposed(result);
return result;
}
Jacobian jacobian([[maybe_unused]] const LocalCoordinate& local) const
{
return jacobianTransposed(local).transposed();
}
JacobianInverse jacobianInverse([[maybe_unused]] const LocalCoordinate& local) const
{
return jacobianInverseTransposed(local).transposed();
}
Volume integrationElement([[maybe_unused]] const LocalCoordinate& local) const
{
return volume();
}
GlobalCoordinate center() const
{
GlobalCoordinate result;
if (dim==0)
result = lower_;
else {
// Since lower_==upper_ for unused coordinates, this always does the right thing
for (size_t i=0; i<coorddim; i++)
result[i] = CoordType(0.5) * (lower_[i] + upper_[i]);
}
return result;
}
int corners() const
{
return 1<<dim;
}
GlobalCoordinate corner(int k) const
{
GlobalCoordinate result;
if (dim == coorddim) { // fast case
for (size_t i=0; i<coorddim; i++)
result[i] = (k & (1<<i)) ? upper_[i] : lower_[i];
} else if (dim == 0) { // vertex
result = lower_; // rely on named return-type optimization
} else { // slow case
unsigned int mask = 1;
for (size_t i=0; i<coorddim; i++) {
if (not axes_[i])
result[i] = lower_[i];
else {
result[i] = (k & mask) ? upper_[i] : lower_[i];
mask = (mask<<1);
}
}
}
return result;
}
Volume volume() const
{
ctype vol = 1;
if (dim == coorddim) { // fast case
for (size_t i=0; i<dim; i++)
vol *= upper_[i] - lower_[i];
// do nothing if dim == 0
} else if (dim != 0) { // slow case
for (size_t i=0; i<coorddim; i++)
if (axes_[i])
vol *= upper_[i] - lower_[i];
}
return vol;
}
bool affine() const
{
return true;
}
friend typename Dune::ReferenceElements< ctype, dim >::ReferenceElement referenceElement ( const AxisAlignedCubeGeometry & /* geometry */ )
{
return ReferenceElements< ctype, dim >::cube();
}
private:
// jacobianTransposed: fast case --> diagonal matrix
void jacobianTransposed ( DiagonalMatrix<ctype,dim> &jacobianTransposed ) const
{
for (size_t i=0; i<dim; i++)
jacobianTransposed.diagonal()[i] = upper_[i] - lower_[i];
}
// jacobianTransposed: slow case --> dense matrix
void jacobianTransposed ( FieldMatrix<ctype,dim,coorddim> &jacobianTransposed ) const
{
if (dim==0)
return;
size_t lc = 0;
for (size_t i=0; i<coorddim; i++)
if (axes_[i])
jacobianTransposed[lc++][i] = upper_[i] - lower_[i];
}
// jacobianInverseTransposed: fast case --> diagonal matrix
void jacobianInverseTransposed ( DiagonalMatrix<ctype,dim> &jacobianInverseTransposed ) const
{
for (size_t i=0; i<dim; i++)
jacobianInverseTransposed.diagonal()[i] = CoordType(1.0) / (upper_[i] - lower_[i]);
}
// jacobianInverseTransposed: slow case --> dense matrix
void jacobianInverseTransposed ( FieldMatrix<ctype,coorddim,dim> &jacobianInverseTransposed ) const
{
if (dim==0)
return;
size_t lc = 0;
for (size_t i=0; i<coorddim; i++)
if (axes_[i])
jacobianInverseTransposed[i][lc++] = CoordType(1.0) / (upper_[i] - lower_[i]);
}
Dune::FieldVector<ctype,coorddim> lower_;
Dune::FieldVector<ctype,coorddim> upper_;
std::bitset<coorddim> axes_;
};
} // namespace Dune
#endif
referenceelements.hh
type.hh
A unique label for each type of element that can occur in a grid.
Dune::GeometryTypes::cube
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition type.hh:462
Dune
Definition affinegeometry.hh:22
Dune::AxisAlignedCubeGeometry::volume
Volume volume() const
Return the element volume.
Definition axisalignedcubegeometry.hh:295
Dune::AxisAlignedCubeGeometry::AxisAlignedCubeGeometry
AxisAlignedCubeGeometry(const Dune::FieldVector< ctype, coorddim > lower, const Dune::FieldVector< ctype, coorddim > upper, const std::bitset< coorddim > &axes)
Constructor from a lower left and an upper right corner.
Definition axisalignedcubegeometry.hh:142
Dune::AxisAlignedCubeGeometry::AxisAlignedCubeGeometry
AxisAlignedCubeGeometry(const Dune::FieldVector< ctype, coorddim > lower, const Dune::FieldVector< ctype, coorddim > upper)
Constructor from a lower left and an upper right corner.
Definition axisalignedcubegeometry.hh:124
Dune::AxisAlignedCubeGeometry::jacobianInverse
JacobianInverse jacobianInverse(const LocalCoordinate &local) const
Inverse Jacobian of the transformation from local to global coordinates.
Definition axisalignedcubegeometry.hh:235
Dune::AxisAlignedCubeGeometry::jacobianInverseTransposed
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local) const
Inverse Jacobian transposed of the transformation from local to global coordinates.
Definition axisalignedcubegeometry.hh:217
Dune::AxisAlignedCubeGeometry::JacobianTransposed
std::conditional< dim==coorddim, DiagonalMatrix< ctype, dim >, FieldMatrix< ctype, dim, coorddim > >::type JacobianTransposed
Return type of jacobianTransposed.
Definition axisalignedcubegeometry.hh:81
Dune::AxisAlignedCubeGeometry::AxisAlignedCubeGeometry
AxisAlignedCubeGeometry(const Dune::FieldVector< ctype, coorddim > lower)
Constructor from a single point only.
Definition axisalignedcubegeometry.hh:159
Dune::AxisAlignedCubeGeometry< CoordType, dimension-codimension, dimension >::mydimension
static constexpr int mydimension
Definition axisalignedcubegeometry.hh:56
Dune::AxisAlignedCubeGeometry::referenceElement
friend Dune::ReferenceElements< ctype, dim >::ReferenceElement referenceElement(const AxisAlignedCubeGeometry &)
Definition axisalignedcubegeometry.hh:316
Dune::AxisAlignedCubeGeometry< CoordType, dimension-codimension, dimension >::coorddimension
static constexpr int coorddimension
Definition axisalignedcubegeometry.hh:59
Dune::AxisAlignedCubeGeometry::corner
GlobalCoordinate corner(int k) const
Return world coordinates of the k-th corner of the element.
Definition axisalignedcubegeometry.hh:269
Dune::AxisAlignedCubeGeometry::Volume
ctype Volume
Type used for volume.
Definition axisalignedcubegeometry.hh:71
Dune::AxisAlignedCubeGeometry::LocalCoordinate
FieldVector< ctype, dim > LocalCoordinate
Type used for a vector of element coordinates.
Definition axisalignedcubegeometry.hh:65
Dune::AxisAlignedCubeGeometry::jacobianTransposed
JacobianTransposed jacobianTransposed(const LocalCoordinate &local) const
Jacobian transposed of the transformation from local to global coordinates.
Definition axisalignedcubegeometry.hh:205
Dune::AxisAlignedCubeGeometry::GlobalCoordinate
FieldVector< ctype, coorddim > GlobalCoordinate
Type used for a vector of world coordinates.
Definition axisalignedcubegeometry.hh:68
Dune::AxisAlignedCubeGeometry< CoordType, dimension-codimension, dimension >::local
LocalCoordinate local(const GlobalCoordinate &global) const
Definition axisalignedcubegeometry.hh:189
Dune::AxisAlignedCubeGeometry::ctype
CoordType ctype
Type used for single coordinate coefficients.
Definition axisalignedcubegeometry.hh:62
Dune::AxisAlignedCubeGeometry::Jacobian
std::conditional_t< dim==coorddim, DiagonalMatrix< ctype, dim >, FieldMatrix< ctype, coorddim, dim > > Jacobian
Return type of jacobian.
Definition axisalignedcubegeometry.hh:100
Dune::AxisAlignedCubeGeometry::type
GeometryType type() const
Type of the cube. Here: a hypercube of the correct dimension.
Definition axisalignedcubegeometry.hh:164
Dune::AxisAlignedCubeGeometry::corners
int corners() const
Return the number of corners of the element.
Definition axisalignedcubegeometry.hh:263
Dune::AxisAlignedCubeGeometry::jacobian
Jacobian jacobian(const LocalCoordinate &local) const
Jacobian of the transformation from local to global coordinates.
Definition axisalignedcubegeometry.hh:229
Dune::AxisAlignedCubeGeometry::JacobianInverseTransposed
std::conditional< dim==coorddim, DiagonalMatrix< ctype, dim >, FieldMatrix< ctype, coorddim, dim > >::type JacobianInverseTransposed
Return type of jacobianInverseTransposed.
Definition axisalignedcubegeometry.hh:91
Dune::AxisAlignedCubeGeometry::integrationElement
Volume integrationElement(const LocalCoordinate &local) const
Return the integration element, i.e., the determinant term in the integral transformation formula.
Definition axisalignedcubegeometry.hh:243
Dune::AxisAlignedCubeGeometry::center
GlobalCoordinate center() const
Return center of mass of the element.
Definition axisalignedcubegeometry.hh:249
Dune::AxisAlignedCubeGeometry::AxisAlignedCubeGeometry
AxisAlignedCubeGeometry()=default
Constructs an empty geometry.
Dune::AxisAlignedCubeGeometry::affine
bool affine() const
Return if the element is affine. Here: yes.
Definition axisalignedcubegeometry.hh:311
Dune::AxisAlignedCubeGeometry::JacobianInverse
std::conditional_t< dim==coorddim, DiagonalMatrix< ctype, dim >, FieldMatrix< ctype, dim, coorddim > > JacobianInverse
Return type of jacobianInverse.
Definition axisalignedcubegeometry.hh:109
Dune::AxisAlignedCubeGeometry::global
GlobalCoordinate global(const LocalCoordinate &local) const
Map a point in local (element) coordinates to world coordinates.
Definition axisalignedcubegeometry.hh:170
Dune::GeometryType
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:114
Dune::ReferenceElements::ReferenceElement
typename Container::ReferenceElement ReferenceElement
The reference element type.
Definition referenceelements.hh:146
Dune::ReferenceElements::cube
static const ReferenceElement & cube()
get hypercube reference elements
Definition referenceelements.hh:168