mirror of
https://github.com/OPM/opm-simulators.git
synced 2024-12-28 18:21:00 -06:00
changed: reduce some code duplication
This commit is contained in:
parent
32ea42006c
commit
3faccb4a17
@ -31,9 +31,10 @@
|
|||||||
namespace Dune
|
namespace Dune
|
||||||
{
|
{
|
||||||
namespace FMatrixHelp {
|
namespace FMatrixHelp {
|
||||||
|
|
||||||
//! invert 4x4 Matrix without changing the original matrix
|
//! invert 4x4 Matrix without changing the original matrix
|
||||||
template <typename K>
|
template <template<class K> class Matrix, typename K>
|
||||||
static inline K invertMatrix(const FieldMatrix<K,4,4>& matrix, FieldMatrix<K,4,4>& inverse)
|
static inline K invertMatrix4(const Matrix<K>& matrix, Matrix<K>& inverse)
|
||||||
{
|
{
|
||||||
inverse[0][0] = matrix[1][1] * matrix[2][2] * matrix[3][3] -
|
inverse[0][0] = matrix[1][1] * matrix[2][2] * matrix[3][3] -
|
||||||
matrix[1][1] * matrix[2][3] * matrix[3][2] -
|
matrix[1][1] * matrix[2][3] * matrix[3][2] -
|
||||||
@ -163,139 +164,24 @@ static inline K invertMatrix(const FieldMatrix<K,4,4>& matrix, FieldMatrix<K,4,4
|
|||||||
return det;
|
return det;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class K> using FMat = FieldMatrix<K,4,4>;
|
||||||
|
|
||||||
|
template <typename K>
|
||||||
|
static inline K invertMatrix(const FieldMatrix<K,4,4>& matrix, FieldMatrix<K,4,4>& inverse)
|
||||||
|
{
|
||||||
|
return invertMatrix4<FMat>(matrix, inverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename K>
|
template <typename K>
|
||||||
static inline K invertMatrix(const DynamicMatrix<K>& matrix, DynamicMatrix<K>& inverse)
|
static inline K invertMatrix(const DynamicMatrix<K>& matrix, DynamicMatrix<K>& inverse)
|
||||||
{
|
{
|
||||||
// this function is only for 4 X 4 matrix
|
// this function is only for 4 X 4 matrix
|
||||||
assert (matrix.rows() == 4);
|
assert (matrix.rows() == 4);
|
||||||
|
|
||||||
inverse[0][0] = matrix[1][1] * matrix[2][2] * matrix[3][3] -
|
return invertMatrix4(matrix, inverse);
|
||||||
matrix[1][1] * matrix[2][3] * matrix[3][2] -
|
|
||||||
matrix[2][1] * matrix[1][2] * matrix[3][3] +
|
|
||||||
matrix[2][1] * matrix[1][3] * matrix[3][2] +
|
|
||||||
matrix[3][1] * matrix[1][2] * matrix[2][3] -
|
|
||||||
matrix[3][1] * matrix[1][3] * matrix[2][2];
|
|
||||||
|
|
||||||
inverse[1][0] = -matrix[1][0] * matrix[2][2] * matrix[3][3] +
|
|
||||||
matrix[1][0] * matrix[2][3] * matrix[3][2] +
|
|
||||||
matrix[2][0] * matrix[1][2] * matrix[3][3] -
|
|
||||||
matrix[2][0] * matrix[1][3] * matrix[3][2] -
|
|
||||||
matrix[3][0] * matrix[1][2] * matrix[2][3] +
|
|
||||||
matrix[3][0] * matrix[1][3] * matrix[2][2];
|
|
||||||
|
|
||||||
inverse[2][0] = matrix[1][0] * matrix[2][1] * matrix[3][3] -
|
|
||||||
matrix[1][0] * matrix[2][3] * matrix[3][1] -
|
|
||||||
matrix[2][0] * matrix[1][1] * matrix[3][3] +
|
|
||||||
matrix[2][0] * matrix[1][3] * matrix[3][1] +
|
|
||||||
matrix[3][0] * matrix[1][1] * matrix[2][3] -
|
|
||||||
matrix[3][0] * matrix[1][3] * matrix[2][1];
|
|
||||||
|
|
||||||
inverse[3][0] = -matrix[1][0] * matrix[2][1] * matrix[3][2] +
|
|
||||||
matrix[1][0] * matrix[2][2] * matrix[3][1] +
|
|
||||||
matrix[2][0] * matrix[1][1] * matrix[3][2] -
|
|
||||||
matrix[2][0] * matrix[1][2] * matrix[3][1] -
|
|
||||||
matrix[3][0] * matrix[1][1] * matrix[2][2] +
|
|
||||||
matrix[3][0] * matrix[1][2] * matrix[2][1];
|
|
||||||
|
|
||||||
inverse[0][1]= -matrix[0][1] * matrix[2][2] * matrix[3][3] +
|
|
||||||
matrix[0][1] * matrix[2][3] * matrix[3][2] +
|
|
||||||
matrix[2][1] * matrix[0][2] * matrix[3][3] -
|
|
||||||
matrix[2][1] * matrix[0][3] * matrix[3][2] -
|
|
||||||
matrix[3][1] * matrix[0][2] * matrix[2][3] +
|
|
||||||
matrix[3][1] * matrix[0][3] * matrix[2][2];
|
|
||||||
|
|
||||||
inverse[1][1] = matrix[0][0] * matrix[2][2] * matrix[3][3] -
|
|
||||||
matrix[0][0] * matrix[2][3] * matrix[3][2] -
|
|
||||||
matrix[2][0] * matrix[0][2] * matrix[3][3] +
|
|
||||||
matrix[2][0] * matrix[0][3] * matrix[3][2] +
|
|
||||||
matrix[3][0] * matrix[0][2] * matrix[2][3] -
|
|
||||||
matrix[3][0] * matrix[0][3] * matrix[2][2];
|
|
||||||
|
|
||||||
inverse[2][1] = -matrix[0][0] * matrix[2][1] * matrix[3][3] +
|
|
||||||
matrix[0][0] * matrix[2][3] * matrix[3][1] +
|
|
||||||
matrix[2][0] * matrix[0][1] * matrix[3][3] -
|
|
||||||
matrix[2][0] * matrix[0][3] * matrix[3][1] -
|
|
||||||
matrix[3][0] * matrix[0][1] * matrix[2][3] +
|
|
||||||
matrix[3][0] * matrix[0][3] * matrix[2][1];
|
|
||||||
|
|
||||||
inverse[3][1] = matrix[0][0] * matrix[2][1] * matrix[3][2] -
|
|
||||||
matrix[0][0] * matrix[2][2] * matrix[3][1] -
|
|
||||||
matrix[2][0] * matrix[0][1] * matrix[3][2] +
|
|
||||||
matrix[2][0] * matrix[0][2] * matrix[3][1] +
|
|
||||||
matrix[3][0] * matrix[0][1] * matrix[2][2] -
|
|
||||||
matrix[3][0] * matrix[0][2] * matrix[2][1];
|
|
||||||
|
|
||||||
inverse[0][2] = matrix[0][1] * matrix[1][2] * matrix[3][3] -
|
|
||||||
matrix[0][1] * matrix[1][3] * matrix[3][2] -
|
|
||||||
matrix[1][1] * matrix[0][2] * matrix[3][3] +
|
|
||||||
matrix[1][1] * matrix[0][3] * matrix[3][2] +
|
|
||||||
matrix[3][1] * matrix[0][2] * matrix[1][3] -
|
|
||||||
matrix[3][1] * matrix[0][3] * matrix[1][2];
|
|
||||||
|
|
||||||
inverse[1][2] = -matrix[0][0] * matrix[1][2] * matrix[3][3] +
|
|
||||||
matrix[0][0] * matrix[1][3] * matrix[3][2] +
|
|
||||||
matrix[1][0] * matrix[0][2] * matrix[3][3] -
|
|
||||||
matrix[1][0] * matrix[0][3] * matrix[3][2] -
|
|
||||||
matrix[3][0] * matrix[0][2] * matrix[1][3] +
|
|
||||||
matrix[3][0] * matrix[0][3] * matrix[1][2];
|
|
||||||
|
|
||||||
inverse[2][2] = matrix[0][0] * matrix[1][1] * matrix[3][3] -
|
|
||||||
matrix[0][0] * matrix[1][3] * matrix[3][1] -
|
|
||||||
matrix[1][0] * matrix[0][1] * matrix[3][3] +
|
|
||||||
matrix[1][0] * matrix[0][3] * matrix[3][1] +
|
|
||||||
matrix[3][0] * matrix[0][1] * matrix[1][3] -
|
|
||||||
matrix[3][0] * matrix[0][3] * matrix[1][1];
|
|
||||||
|
|
||||||
inverse[3][2] = -matrix[0][0] * matrix[1][1] * matrix[3][2] +
|
|
||||||
matrix[0][0] * matrix[1][2] * matrix[3][1] +
|
|
||||||
matrix[1][0] * matrix[0][1] * matrix[3][2] -
|
|
||||||
matrix[1][0] * matrix[0][2] * matrix[3][1] -
|
|
||||||
matrix[3][0] * matrix[0][1] * matrix[1][2] +
|
|
||||||
matrix[3][0] * matrix[0][2] * matrix[1][1];
|
|
||||||
|
|
||||||
inverse[0][3] = -matrix[0][1] * matrix[1][2] * matrix[2][3] +
|
|
||||||
matrix[0][1] * matrix[1][3] * matrix[2][2] +
|
|
||||||
matrix[1][1] * matrix[0][2] * matrix[2][3] -
|
|
||||||
matrix[1][1] * matrix[0][3] * matrix[2][2] -
|
|
||||||
matrix[2][1] * matrix[0][2] * matrix[1][3] +
|
|
||||||
matrix[2][1] * matrix[0][3] * matrix[1][2];
|
|
||||||
|
|
||||||
inverse[1][3] = matrix[0][0] * matrix[1][2] * matrix[2][3] -
|
|
||||||
matrix[0][0] * matrix[1][3] * matrix[2][2] -
|
|
||||||
matrix[1][0] * matrix[0][2] * matrix[2][3] +
|
|
||||||
matrix[1][0] * matrix[0][3] * matrix[2][2] +
|
|
||||||
matrix[2][0] * matrix[0][2] * matrix[1][3] -
|
|
||||||
matrix[2][0] * matrix[0][3] * matrix[1][2];
|
|
||||||
|
|
||||||
inverse[2][3] = -matrix[0][0] * matrix[1][1] * matrix[2][3] +
|
|
||||||
matrix[0][0] * matrix[1][3] * matrix[2][1] +
|
|
||||||
matrix[1][0] * matrix[0][1] * matrix[2][3] -
|
|
||||||
matrix[1][0] * matrix[0][3] * matrix[2][1] -
|
|
||||||
matrix[2][0] * matrix[0][1] * matrix[1][3] +
|
|
||||||
matrix[2][0] * matrix[0][3] * matrix[1][1];
|
|
||||||
|
|
||||||
inverse[3][3] = matrix[0][0] * matrix[1][1] * matrix[2][2] -
|
|
||||||
matrix[0][0] * matrix[1][2] * matrix[2][1] -
|
|
||||||
matrix[1][0] * matrix[0][1] * matrix[2][2] +
|
|
||||||
matrix[1][0] * matrix[0][2] * matrix[2][1] +
|
|
||||||
matrix[2][0] * matrix[0][1] * matrix[1][2] -
|
|
||||||
matrix[2][0] * matrix[0][2] * matrix[1][1];
|
|
||||||
|
|
||||||
K det = matrix[0][0] * inverse[0][0] + matrix[0][1] * inverse[1][0] +
|
|
||||||
matrix[0][2] * inverse[2][0] + matrix[0][3] * inverse[3][0];
|
|
||||||
|
|
||||||
// return identity for singular or nearly singular matrices.
|
|
||||||
if (std::abs(det) < 1e-40) {
|
|
||||||
for (int i = 0; i < 4; ++i){
|
|
||||||
inverse[i][i] = 1.0;
|
|
||||||
}
|
}
|
||||||
return 1.0;
|
|
||||||
}
|
|
||||||
K inv_det = 1.0 / det;
|
|
||||||
inverse *= inv_det;
|
|
||||||
|
|
||||||
return det;
|
|
||||||
}
|
|
||||||
} // end FMatrixHelp
|
} // end FMatrixHelp
|
||||||
|
|
||||||
namespace ISTLUtility {
|
namespace ISTLUtility {
|
||||||
|
Loading…
Reference in New Issue
Block a user