From ed73abd931b91128f971a11f4d4e5bd8639fcfd3 Mon Sep 17 00:00:00 2001 From: Hrvoje Jasak Date: Mon, 10 Apr 2017 11:19:39 +0100 Subject: [PATCH] Improved form of BlockCoeffTwoNorm --- .../BlockCoeffTwoNorm/BlockCoeffTwoNorm.C | 20 +++-- .../BlockCoeffTwoNorm/blockCoeffTwoNorms.H | 1 + .../scalarBlockCoeffTwoNorm.H | 80 +++++++++++++++++++ .../tensorBlockCoeffTwoNorm.H | 11 --- 4 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/scalarBlockCoeffTwoNorm.H diff --git a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/BlockCoeffTwoNorm.C b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/BlockCoeffTwoNorm.C index dfdb1f025..1dc865113 100644 --- a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/BlockCoeffTwoNorm.C +++ b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/BlockCoeffTwoNorm.C @@ -61,8 +61,13 @@ Foam::scalar Foam::BlockCoeffTwoNorm::normalize } else if (a.activeType() == Foam::BlockCoeff::SQUARE) { - // Note: for two-norm, use mag - return mag(a.asSquare()); + // Note: for two-norm, use mag of diag + typedef typename BlockCoeff::linearType linearType; + + linearType diag; + contractLinear(diag, a.asSquare()); + + return mag(diag); } else { @@ -94,13 +99,18 @@ void Foam::BlockCoeffTwoNorm::normalize } else if (a.activeType() == Foam::BlockCoeff::LINEAR) { - // Note: for two-norm, use mag + // Note: for two-norm, use mag = sqrt(sum magSqr(cmpt)) b = mag(a.asLinear()); } else if (a.activeType() == Foam::BlockCoeff::SQUARE) { - // Note: for two-norm, use mag - b = mag(a.asSquare()); + // Note: for two-norm, use mag of diag + typedef typename BlockCoeff::linearTypeField linearTypeField; + + linearTypeField diag(a.size()); + contractLinear(diag, a.asSquare()); + + b = mag(diag); } else { diff --git a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/blockCoeffTwoNorms.H b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/blockCoeffTwoNorms.H index fd70487b3..524766733 100644 --- a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/blockCoeffTwoNorms.H +++ b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/blockCoeffTwoNorms.H @@ -38,6 +38,7 @@ SourceFiles #ifndef blockCoeffTwoNorms_H #define blockCoeffTwoNorms_H +#include "scalarBlockCoeffTwoNorm.H" #include "tensorBlockCoeffTwoNorm.H" #include "BlockCoeffTwoNorm.H" diff --git a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/scalarBlockCoeffTwoNorm.H b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/scalarBlockCoeffTwoNorm.H new file mode 100644 index 000000000..3a10881b6 --- /dev/null +++ b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/scalarBlockCoeffTwoNorm.H @@ -0,0 +1,80 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | foam-extend: Open Source CFD + \\ / O peration | Version: 4.0 + \\ / A nd | Web: http://www.foam-extend.org + \\/ M anipulation | For copyright notice see file Copyright +------------------------------------------------------------------------------- +License + This file is part of foam-extend. + + foam-extend is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + foam-extend is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with foam-extend. If not, see . + +Class + BlockCoeffTwoNorm + +Description + Class for two norm of block coeffs. Specilization for scalar. Implemented + to avoid issues with asScalar, asSquare etc. + +Author + Hrvoje Jasak, Wikki Ltd. All rights reserved + +\*---------------------------------------------------------------------------*/ + +#ifndef scalarBlockCoeffTwoNorm_H +#define scalarBlockCoeffTwoNorm_H + +#include "blockCoeffs.H" +#include "blockCoeffNorms.H" +#include "BlockCoeffNorm.H" +#include "BlockCoeffTwoNorm.H" +#include "runTimeSelectionTables.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +template<> +inline scalar BlockCoeffTwoNorm::normalize +( + const BlockCoeff& a +) +{ + // Note: for two-norm, use mag + return mag(a.asScalar()); +} + + +template<> +inline void BlockCoeffTwoNorm::normalize +( + Field& b, + const CoeffField& a +) +{ + b = mag(a.asScalar()); +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/tensorBlockCoeffTwoNorm.H b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/tensorBlockCoeffTwoNorm.H index db2a2bc7d..e765a5a65 100644 --- a/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/tensorBlockCoeffTwoNorm.H +++ b/src/foam/primitives/BlockCoeff/BlockCoeffNorm/BlockCoeffTwoNorm/tensorBlockCoeffTwoNorm.H @@ -33,9 +33,6 @@ Description Author Klas Jareteg, 2013-01-30 -SourceFiles - BlockCoeffTwoNorm.C - \*---------------------------------------------------------------------------*/ #ifndef tensorBlockCoeffTwoNorm_H @@ -52,11 +49,6 @@ SourceFiles namespace Foam { - -/*---------------------------------------------------------------------------*\ - Class BlockCoeffTwoNorm Declaration -\*---------------------------------------------------------------------------*/ - template<> inline scalar BlockCoeffTwoNorm::normalize ( @@ -116,9 +108,6 @@ inline void BlockCoeffTwoNorm::normalize } // End namespace Foam -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif