From 671a2708ff7c63c44824294828ed06a94a7b4d23 Mon Sep 17 00:00:00 2001 From: Hrvoje Jasak Date: Thu, 6 Aug 2015 09:20:10 +0100 Subject: [PATCH] Added det of TensorN --- .../Fields/VectorNFields/TensorNFields.C | 1 + .../Fields/VectorNFields/TensorNFields.H | 1 + src/foam/primitives/VectorN/TensorNI.H | 315 +++++++++--------- 3 files changed, 152 insertions(+), 165 deletions(-) diff --git a/src/foam/fields/Fields/VectorNFields/TensorNFields.C b/src/foam/fields/Fields/VectorNFields/TensorNFields.C index efb416652..1c8b9d751 100644 --- a/src/foam/fields/Fields/VectorNFields/TensorNFields.C +++ b/src/foam/fields/Fields/VectorNFields/TensorNFields.C @@ -36,6 +36,7 @@ License sphericalTensorType,vectorType,CmptType, \ args...) \ \ +UNARY_FUNCTION(CmptType, tensorType, det) \ UNARY_FUNCTION(tensorType, tensorType, inv) \ UNARY_FUNCTION(diagTensorType, tensorType, diag) \ UNARY_FUNCTION(tensorType, tensorType, negSumDiag) \ diff --git a/src/foam/fields/Fields/VectorNFields/TensorNFields.H b/src/foam/fields/Fields/VectorNFields/TensorNFields.H index f22f8c442..9983c7c71 100644 --- a/src/foam/fields/Fields/VectorNFields/TensorNFields.H +++ b/src/foam/fields/Fields/VectorNFields/TensorNFields.H @@ -47,6 +47,7 @@ SourceFiles sphericalTensorType,vectorType,CmptType, \ args...) \ \ +UNARY_FUNCTION(CmptType, tensorType, det) \ UNARY_FUNCTION(tensorType, tensorType, inv) \ UNARY_FUNCTION(diagTensorType, tensorType, diag) \ UNARY_FUNCTION(tensorType, tensorType, negSumDiag) \ diff --git a/src/foam/primitives/VectorN/TensorNI.H b/src/foam/primitives/VectorN/TensorNI.H index d3e114062..3514c12c0 100644 --- a/src/foam/primitives/VectorN/TensorNI.H +++ b/src/foam/primitives/VectorN/TensorNI.H @@ -89,11 +89,11 @@ inline TensorN TensorN::T() const { TensorN transpose; - int i = 0; - for (int row = 0; row < TensorN::rowLength; row++) + label i = 0; + for (label row = 0; row < TensorN::rowLength; row++) { - int j=row; - for (int col = 0; col < TensorN::rowLength; col++) + label j = row; + for (label col = 0; col < TensorN::rowLength; col++) { transpose.v_[i] = this->v_[j]; i++; @@ -110,8 +110,8 @@ inline DiagTensorN TensorN::diag() const { DiagTensorN dt; - int diagI=0; - for (int i = 0; i < TensorN::rowLength; i++) + label diagI=0; + for (label i = 0; i < TensorN::rowLength; i++) { dt[i] = this->v_[diagI]; diagI += TensorN::rowLength + 1; @@ -127,18 +127,18 @@ inline TensorN TensorN::negSumDiag() const TensorN negsumdiag; // Zero main diagonal - int diagI=0; - for (int i = 0; i < TensorN::rowLength; i++) + label diagI = 0; + for (label i = 0; i < TensorN::rowLength; i++) { negsumdiag.v_[diagI] = 0.0; diagI += TensorN::rowLength + 1; } - int k=0; - for (int i = 0; i < TensorN::rowLength; i++) + label k = 0; + for (label i = 0; i < TensorN::rowLength; i++) { diagI = 0; - for (int j = 0; j < TensorN::rowLength; j++) + for (label j = 0; j < TensorN::rowLength; j++) { if (k != diagI) { @@ -158,8 +158,8 @@ template inline void TensorN::operator=(const SphericalTensorN& st) { - int diag=0; - for (int i = 0; i < TensorN::nComponents; i++) + label diag = 0; + for (label i = 0; i < TensorN::nComponents; i++) { if (i == diag) { @@ -179,11 +179,11 @@ template inline void TensorN::operator=(const DiagTensorN& dt) { - int diag=0; - int k=0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + label k = 0; + for (label i = 0; i < TensorN::rowLength; i++) { - for (int j = 0; j < TensorN::rowLength; j++) + for (label j = 0; j < TensorN::rowLength; j++) { if (j == diag) { @@ -278,17 +278,25 @@ operator&(const TensorN& t1, const TensorN& t2) { TensorN result(TensorN::zero); - int i = 0; - int j = 0; - for (int row = 0; row < TensorN::rowLength; row++) + label i = 0; + label j = 0; + + label m, n; + + for (label row = 0; row < TensorN::rowLength; row++) { - for (int col = 0; col < TensorN::rowLength; col++) + for (label col = 0; col < TensorN::rowLength; col++) { Cmpt& r = result.v_[i]; - int m = j; - int n = col; + m = j; + n = col; - for (int row2=0; row2 < TensorN::rowLength; row2++) + for + ( + label row2 = 0; + row2 < TensorN::rowLength; + row2++ + ) { r += t1.v_[m]*t2.v_[n]; m++; @@ -314,12 +322,12 @@ operator& { TensorN result; - int k=0; - for (int i = 0; i < TensorN::rowLength; i++) + label k = 0; + for (label i = 0; i < TensorN::rowLength; i++) { const Cmpt& xx = dt1.v_[i]; - for (int j = 0; j < TensorN::rowLength; j++) + for (label j = 0; j < TensorN::rowLength; j++) { result.v_[k] = xx*t2.v_[k]; k++; @@ -341,10 +349,10 @@ operator& { TensorN result; - int k=0; - for (int i = 0; i < TensorN::rowLength; i++) + label k = 0; + for (label i = 0; i < TensorN::rowLength; i++) { - for (int j = 0; j < TensorN::rowLength; j++) + for (label j = 0; j < TensorN::rowLength; j++) { result.v_[k] = t1.v_[k]*dt2.v_[j]; k++; @@ -411,12 +419,12 @@ operator&(const TensorN& t, const VectorN& v) { VectorN result(VectorN::zero); - int i=0; - for (int row = 0; row < TensorN::rowLength; row++) + label i = 0; + for (label row = 0; row < TensorN::rowLength; row++) { Cmpt& r = result.v_[row]; - for (int col = 0; col < TensorN::rowLength; col++) + for (label col = 0; col < TensorN::rowLength; col++) { r += t.v_[i]*v.v_[col]; i++; @@ -435,12 +443,12 @@ operator&(const VectorN& v, const TensorN& t) { VectorN result(VectorN::zero); - for (int col = 0; col < TensorN::rowLength; col++) + for (label col = 0; col < TensorN::rowLength; col++) { - int j=col; + label j = col; Cmpt& r = result.v_[col]; - for (int row = 0; row < TensorN::rowLength; row++) + for (label row = 0; row < TensorN::rowLength; row++) { r += v.v_[row]*t.v_[j]; j += TensorN::rowLength; @@ -459,10 +467,10 @@ operator*(const VectorN& v1, const VectorN& v2) { TensorN result(TensorN::zero); - int i=0; - for (int row = 0; row < TensorN::rowLength; row++) + label i = 0; + for (label row = 0; row < TensorN::rowLength; row++) { - for (int col = 0; col < TensorN::rowLength; col++) + for (label col = 0; col < TensorN::rowLength; col++) { result.v_[i] = v1.v_[row]*v2.v_[col]; i++; @@ -498,8 +506,8 @@ operator+(const TensorN& t1, const DiagTensorN& dt2) { TensorN result(t1); - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += dt2.v_[i]; diag += TensorN::rowLength + 1; @@ -516,8 +524,8 @@ operator+(const DiagTensorN& dt1, const TensorN& t2) { TensorN result(t2); - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += dt1.v_[i]; diag += TensorN::rowLength + 1; @@ -539,8 +547,8 @@ operator+ TensorN result(t1); const Cmpt& s = st2.v_[0]; - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += s; diag += TensorN::rowLength + 1; @@ -562,8 +570,8 @@ operator+ TensorN result(t2); const Cmpt& s = st1.v_[0]; - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += s; diag += TensorN::rowLength + 1; @@ -598,8 +606,8 @@ operator-(const TensorN& t1, const DiagTensorN& dt2) { TensorN result(t1); - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] -= dt2.v_[i]; diag += TensorN::rowLength + 1; @@ -616,8 +624,8 @@ operator-(const DiagTensorN& dt1, const TensorN& t2) { TensorN result(-t2); - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += dt1.v_[i]; diag += TensorN::rowLength + 1; @@ -639,8 +647,8 @@ operator- TensorN result(t1); const Cmpt& s = st2.v_[0]; - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] -= s; diag += TensorN::rowLength + 1; @@ -662,8 +670,8 @@ operator- TensorN result(-t2); const Cmpt& s = st1.v_[0]; - int diag = 0; - for (int i = 0; i < TensorN::rowLength; i++) + label diag = 0; + for (label i = 0; i < TensorN::rowLength; i++) { result.v_[diag] += s; diag += TensorN::rowLength + 1; @@ -741,7 +749,7 @@ operator/ TensorN result; const Cmpt& s = st2[0]; - for (int i = 0; i < TensorN::nComponents; i++) + for (label i = 0; i < TensorN::nComponents; i++) { result.v_[i] = t1.v_[i]/s; } @@ -750,109 +758,55 @@ operator/ } -// UNOPTIMIZED VERSION -/* -//- Return the inverse of a tensor give the determinant -// Uses Gauss-Jordan Elimination with full pivoting template -inline TensorN inv(const TensorN& t) +inline Cmpt det(const TensorN& t) { - TensorN result(t); + // Calculate determinant via sub-determinants + Cmpt result = pTraits::zero; - label i, j, k, iRow=0, iCol=0; - Cmpt bigValue, temp, pivotInv; + TensorN subMatrix; - // Lists used for bookkeeping on the pivoting - List