31#include <dune/common/typetraits.hh>
32#include <dune/common/fvector.hh>
33#include <dune/common/fmatrix.hh>
34#include <dune/common/dynmatrix.hh>
35#include <dune/common/float_cmp.hh>
48template <
class Scalar>
49constexpr Scalar
arithmeticMean(Scalar x, Scalar y, Scalar wx = 1.0, Scalar wy = 1.0) noexcept
51 static_assert(Dune::IsNumber<Scalar>::value,
"The arguments x, y, wx, and wy have to be numbers!");
55 return (x * wx + y * wy)/(wx + wy);
67template <
class Scalar>
68constexpr Scalar
harmonicMean(Scalar x, Scalar y, Scalar wx = 1.0, Scalar wy = 1.0) noexcept
70 static_assert(Dune::IsNumber<Scalar>::value,
"The arguments x, y, wx, and wy have to be numbers!");
74 return (wx + wy) * x * y / (wy * x + wx * y);
85template <
class Scalar>
88 static_assert(Dune::IsNumber<Scalar>::value,
"The arguments x and y have to be numbers!");
93 return sqrt(x*y)*
sign(x);
107template <
class Scalar,
int m,
int n>
109 const Dune::FieldMatrix<Scalar, m, n> &Ki,
110 const Dune::FieldMatrix<Scalar, m, n> &Kj)
112 for (
int rowIdx=0; rowIdx < m; rowIdx++){
113 for (
int colIdx=0; colIdx< n; colIdx++){
114 if (Dune::FloatCmp::ne<Scalar>(Ki[rowIdx][colIdx], Kj[rowIdx][colIdx])) {
120 K[rowIdx][colIdx] = Ki[rowIdx][colIdx];
139template <
class Scalar,
class SolContainer>
167template <
class Scalar,
class SolContainer>
178 Scalar Delta = b*b - 4*a*c;
184 sol[0] = (- b + Delta)/(2*a);
185 sol[1] = (- b - Delta)/(2*a);
191 swap(sol[0], sol[1]);
197template <
class Scalar,
class SolContainer>
198void invertCubicPolynomialPostProcess_(SolContainer &sol,
207 for (
int i = 0; i < numSol; ++i) {
209 Scalar fOld = d + x*(c + x*(b + x*a));
211 Scalar fPrime = c + x*(2*b + x*3*a);
216 Scalar fNew = d + x*(c + x*(b + x*a));
218 if (abs(fNew) < abs(fOld))
241template <
class Scalar,
class SolContainer>
259 Scalar p = c - b*b/3;
260 Scalar q = d + (2*b*b*b - 9*b*c)/27;
264 if (p == 0.0 && q == 0.0) {
266 sol[0] = sol[1] = sol[2] = 0.0 - b/3;
269 else if (p == 0.0 && q != 0.0) {
279 else if (p != 0.0 && q == 0.0) {
290 sol[0] = -sqrt(-p) - b/3;
292 sol[2] = sqrt(-p) - b/3;
327 Scalar wDisc = q*q/4 + p*p*p/27;
332 Scalar u = cbrt(-q/2 + sqrt(wDisc));
336 sol[0] = u - p/(3*u) - b/3;
340 invertCubicPolynomialPostProcess_(sol, 1, a, b, c, d);
344 Scalar uCubedRe = - q/2;
346 Scalar uCubedIm = sqrt(-wDisc);
349 Scalar uAbs = cbrt(sqrt(uCubedRe*uCubedRe + uCubedIm*uCubedIm));
351 Scalar phi = atan2(uCubedIm, uCubedRe)/3;
392 for (
int i = 0; i < 3; ++i) {
394 sol[i] = cos(phi)*(uAbs - p/(3*uAbs)) - b/3;
400 invertCubicPolynomialPostProcess_(sol, 3, a, b, c, d);
425template <
class Scalar,
int dim>
426bool isLarger(
const Dune::FieldVector<Scalar, dim> &pos,
427 const Dune::FieldVector<Scalar, dim> &smallerVec)
429 for (
int i=0; i < dim; i++)
431 if (pos[i]<= smallerVec[i])
451template <
class Scalar,
int dim>
452bool isSmaller(
const Dune::FieldVector<Scalar, dim> &pos,
453 const Dune::FieldVector<Scalar, dim> &largerVec)
455 for (
int i=0; i < dim; i++)
457 if (pos[i]>= largerVec[i])
481template <
class Scalar,
int dim>
482bool isBetween(
const Dune::FieldVector<Scalar, dim> &pos,
483 const Dune::FieldVector<Scalar, dim> &smallerVec,
484 const Dune::FieldVector<Scalar, dim> &largerVec)
495namespace InterpolationPolicy {
struct Linear; }
503template <
class Policy = InterpolationPolicy::Linear,
class Scalar,
class Parameter>
511namespace InterpolationPolicy {
524 template<
class Scalar>
525 static constexpr Scalar
interpolate(Scalar ip,
const std::array<Scalar, 2>& params)
527 return params[0]*(1.0 - ip) + params[1]*ip;
543 template<
class Scalar,
class RandomAccessContainer>
544 static constexpr Scalar
interpolate(Scalar ip,
const std::pair<RandomAccessContainer, RandomAccessContainer>& table)
546 const auto& range = table.first;
547 const auto& values = table.second;
550 if (ip > range.back())
return values.back();
551 if (ip < range[0])
return values[0];
554 const auto lookUpIndex = std::distance(range.begin(), std::lower_bound(range.begin(), range.end(), ip));
555 if (lookUpIndex == 0)
558 const auto ipLinear = (ip - range[lookUpIndex-1])/(range[lookUpIndex] - range[lookUpIndex-1]);
559 return Dumux::interpolate<Linear>(ipLinear, std::array<Scalar, 2>{{values[lookUpIndex-1], values[lookUpIndex]}});
573template <
class Scalar>
574std::vector<Scalar>
linspace(
const Scalar begin,
const Scalar end, std::size_t samples)
577 samples = max(std::size_t{2}, samples);
578 const Scalar delta = (end-begin)/
static_cast<Scalar
>(samples-1);
579 std::vector<Scalar> vec(samples);
580 for (std::size_t i = 0; i < samples; ++i)
581 vec[i] = begin + i*delta;
598template <
class Scalar>
604 const Scalar ln10 = 2.3025850929940459;
617template<
class ValueType>
618constexpr int sign(
const ValueType& value)
noexcept
620 return (ValueType(0) < value) - (value < ValueType(0));
630template <
class Scalar>
631Dune::FieldVector<Scalar, 3>
crossProduct(
const Dune::FieldVector<Scalar, 3> &vec1,
632 const Dune::FieldVector<Scalar, 3> &vec2)
634 return {vec1[1]*vec2[2]-vec1[2]*vec2[1],
635 vec1[2]*vec2[0]-vec1[0]*vec2[2],
636 vec1[0]*vec2[1]-vec1[1]*vec2[0]};
646template <
class Scalar>
648 const Dune::FieldVector<Scalar, 2> &vec2)
649{
return vec1[0]*vec2[1]-vec1[1]*vec2[0]; }
659template <
class Scalar>
661 const Dune::FieldVector<Scalar, 3> &vec2,
662 const Dune::FieldVector<Scalar, 3> &vec3)
663{
return crossProduct<Scalar>(vec1, vec2)*vec3; }
671template <
class Scalar,
int m,
int n>
672Dune::FieldMatrix<Scalar, n, m>
getTransposed(
const Dune::FieldMatrix<Scalar, m, n>& M)
674 Dune::FieldMatrix<Scalar, n, m> T;
675 for (std::size_t i = 0; i < m; ++i)
676 for (std::size_t j = 0; j < n; ++j)
688template <
class Scalar>
689Dune::DynamicMatrix<Scalar>
getTransposed(
const Dune::DynamicMatrix<Scalar>& M)
691 std::size_t rows_T = M.M();
692 std::size_t cols_T = M.N();
694 Dune::DynamicMatrix<Scalar> M_T(rows_T, cols_T, 0.0);
696 for (std::size_t i = 0; i < rows_T; ++i)
697 for (std::size_t j = 0; j < cols_T; ++j)
710template <
class Scalar>
712 const Dune::DynamicMatrix<Scalar> &M2)
714 using size_type =
typename Dune::DynamicMatrix<Scalar>::size_type;
715 const size_type rows = M1.N();
716 const size_type cols = M2.M();
718 DUNE_ASSERT_BOUNDS(M1.M() == M2.N());
720 Dune::DynamicMatrix<Scalar> result(rows, cols, 0.0);
721 for (size_type i = 0; i < rows; i++)
722 for (size_type j = 0; j < cols; j++)
723 for (size_type k = 0; k < M1.M(); k++)
724 result[i][j] += M1[i][k]*M2[k][j];
736template <
class Scalar,
int rows1,
int cols1,
int cols2>
737Dune::FieldMatrix<Scalar, rows1, cols2>
multiplyMatrices(
const Dune::FieldMatrix<Scalar, rows1, cols1> &M1,
738 const Dune::FieldMatrix<Scalar, cols1, cols2> &M2)
740 using size_type =
typename Dune::FieldMatrix<Scalar, rows1, cols2>::size_type;
742 Dune::FieldMatrix<Scalar, rows1, cols2> result(0.0);
743 for (size_type i = 0; i < rows1; i++)
744 for (size_type j = 0; j < cols2; j++)
745 for (size_type k = 0; k < cols1; k++)
746 result[i][j] += M1[i][k]*M2[k][j];
758template <
class MatrixType>
759typename Dune::DenseMatrix<MatrixType>::field_type
760trace(
const Dune::DenseMatrix<MatrixType>& M)
762 const auto rows = M.N();
763 DUNE_ASSERT_BOUNDS(rows == M.M());
765 using MatType = Dune::DenseMatrix<MatrixType>;
766 typename MatType::field_type
trace = 0.0;
768 for (
typename MatType::size_type i = 0; i < rows; ++i)
787template <
class MAT,
class V>
788typename Dune::DenseVector<V>::derived_type
789mv(
const Dune::DenseMatrix<MAT>& M,
790 const Dune::DenseVector<V>& v)
792 typename Dune::DenseVector<V>::derived_type res(v);
814template <
class FieldScalar,
class V>
815typename std::enable_if_t<Dune::IsNumber<FieldScalar>::value,
816 typename Dune::DenseVector<V>::derived_type>
817mv(
const FieldScalar m,
const Dune::DenseVector<V>& v)
819 typename Dune::DenseVector<V>::derived_type res(v);
838template <
class V1,
class MAT,
class V2>
839typename Dune::DenseMatrix<MAT>::value_type
840vtmv(
const Dune::DenseVector<V1>& v1,
841 const Dune::DenseMatrix<MAT>& M,
842 const Dune::DenseVector<V2>& v2)
866template <
class V1,
class FieldScalar,
class V2>
867typename std::enable_if_t<Dune::IsNumber<FieldScalar>::value, FieldScalar>
868vtmv(
const Dune::DenseVector<V1>& v1,
870 const Dune::DenseVector<V2>& v2)
std::vector< Scalar > linspace(const Scalar begin, const Scalar end, std::size_t samples)
Generates linearly spaced vectors.
Definition: math.hh:574
constexpr Scalar harmonicMean(Scalar x, Scalar y, Scalar wx=1.0, Scalar wy=1.0) noexcept
Calculate the (weighted) harmonic mean of two scalar values.
Definition: math.hh:68
Dune::DenseMatrix< MAT >::value_type vtmv(const Dune::DenseVector< V1 > &v1, const Dune::DenseMatrix< MAT > &M, const Dune::DenseVector< V2 > &v2)
Evaluates the scalar product of a vector v2, projected by a matrix M, with a vector v1.
Definition: math.hh:840
Dune::DynamicMatrix< Scalar > multiplyMatrices(const Dune::DynamicMatrix< Scalar > &M1, const Dune::DynamicMatrix< Scalar > &M2)
Multiply two dynamic matrices.
Definition: math.hh:711
Scalar geometricMean(Scalar x, Scalar y) noexcept
Calculate the geometric mean of two scalar values.
Definition: math.hh:86
int invertLinearPolynomial(SolContainer &sol, Scalar a, Scalar b)
Invert a linear polynomial analytically.
Definition: math.hh:140
bool isBetween(const Dune::FieldVector< Scalar, dim > &pos, const Dune::FieldVector< Scalar, dim > &smallerVec, const Dune::FieldVector< Scalar, dim > &largerVec)
Comparison of three position vectors.
Definition: math.hh:482
void harmonicMeanMatrix(Dune::FieldMatrix< Scalar, m, n > &K, const Dune::FieldMatrix< Scalar, m, n > &Ki, const Dune::FieldMatrix< Scalar, m, n > &Kj)
Calculate the harmonic mean of a fixed-size matrix.
Definition: math.hh:108
Dune::FieldVector< Scalar, 3 > crossProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2)
Cross product of two vectors in three-dimensional Euclidean space.
Definition: math.hh:631
int invertCubicPolynomial(SolContainer *sol, Scalar a, Scalar b, Scalar c, Scalar d)
Invert a cubic polynomial analytically.
Definition: math.hh:242
bool isSmaller(const Dune::FieldVector< Scalar, dim > &pos, const Dune::FieldVector< Scalar, dim > &largerVec)
Comparison of two position vectors.
Definition: math.hh:452
Dune::DenseVector< V >::derived_type mv(const Dune::DenseMatrix< MAT > &M, const Dune::DenseVector< V > &v)
Returns the result of the projection of a vector v with a Matrix M.
Definition: math.hh:789
Dune::DenseMatrix< MatrixType >::field_type trace(const Dune::DenseMatrix< MatrixType > &M)
Trace of a dense matrix.
Definition: math.hh:760
Dune::FieldMatrix< Scalar, n, m > getTransposed(const Dune::FieldMatrix< Scalar, m, n > &M)
Transpose a FieldMatrix.
Definition: math.hh:672
bool isLarger(const Dune::FieldVector< Scalar, dim > &pos, const Dune::FieldVector< Scalar, dim > &smallerVec)
Comparison of two position vectors.
Definition: math.hh:426
int invertQuadraticPolynomial(SolContainer &sol, Scalar a, Scalar b, Scalar c)
Invert a quadratic polynomial analytically.
Definition: math.hh:168
Scalar interpolate(Scalar ip, Parameter &¶ms)
a generic function to interpolate given a set of parameters and an interpolation point
Definition: math.hh:504
Scalar tripleProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2, const Dune::FieldVector< Scalar, 3 > &vec3)
Triple product of three vectors in three-dimensional Euclidean space retuning scalar.
Definition: math.hh:660
constexpr int sign(const ValueType &value) noexcept
Sign or signum function.
Definition: math.hh:618
Scalar antoine(Scalar temperature, Scalar A, Scalar B, Scalar C)
Evaluates the Antoine equation used to calculate the vapour pressure of various liquids.
Definition: math.hh:599
constexpr Scalar arithmeticMean(Scalar x, Scalar y, Scalar wx=1.0, Scalar wy=1.0) noexcept
Calculate the (weighted) arithmetic mean of two scalar values.
Definition: math.hh:49
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
std::string temperature() noexcept
I/O name of temperature for equilibrium models.
Definition: name.hh:51
interpolate linearly between two given values
Definition: math.hh:518
static constexpr Scalar interpolate(Scalar ip, const std::array< Scalar, 2 > ¶ms)
interpolate linearly between two given values
Definition: math.hh:525
interpolate linearly in a piecewise linear function (tabularized function)
Definition: math.hh:536
static constexpr Scalar interpolate(Scalar ip, const std::pair< RandomAccessContainer, RandomAccessContainer > &table)
interpolate linearly in a piecewise linear function (tabularized function)
Definition: math.hh:544