scalar: Added optimised integer powers and roots
This commit is contained in:
3
applications/test/integerPow/Make/files
Normal file
3
applications/test/integerPow/Make/files
Normal file
@ -0,0 +1,3 @@
|
||||
Test-integerPow.C
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/Test-integerPow
|
||||
0
applications/test/integerPow/Make/options
Normal file
0
applications/test/integerPow/Make/options
Normal file
87
applications/test/integerPow/Test-integerPow.C
Normal file
87
applications/test/integerPow/Test-integerPow.C
Normal file
@ -0,0 +1,87 @@
|
||||
#include "cpuTime.H"
|
||||
#include "Pair.H"
|
||||
#include "Random.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
//- Number of calculations to do. Need 10^8-ish to get enough time (~2 seconds)
|
||||
// to meaningfully compare.
|
||||
static const label n = 100000000;
|
||||
|
||||
//- Range of exponents to calculate. From -E to +E. Zero will be omitted so
|
||||
// that we can do roots, too. At small values (<8) integer powers win. At
|
||||
// larger values (>16), scalar powers do better.
|
||||
static const label E = 4;
|
||||
|
||||
//- Run a given power method
|
||||
template<class Pow>
|
||||
Pair<scalar> run(Pow pow)
|
||||
{
|
||||
Random rndGen(0);
|
||||
|
||||
cpuTime time;
|
||||
|
||||
scalar y = 0;
|
||||
|
||||
for (label i = 0; i < n; ++ i)
|
||||
{
|
||||
const scalar x = rndGen.sampleAB<scalar>(rootSmall, rootGreat);
|
||||
const label e =
|
||||
rndGen.sample01<label>()
|
||||
? rndGen.sampleAB<label>(- E, 0)
|
||||
: rndGen.sampleAB<label>(1, E + 1);
|
||||
|
||||
y = y*scalar(i)/(i + 1) + pow(x, e)/(i + 1);
|
||||
}
|
||||
|
||||
return Pair<scalar>(y, time.cpuTimeIncrement());
|
||||
}
|
||||
|
||||
//- Scalar power function
|
||||
scalar scalarPow(const scalar x, const scalar e)
|
||||
{
|
||||
return Foam::pow(x, e);
|
||||
}
|
||||
|
||||
//- Scalar root function
|
||||
scalar scalarRoot(const scalar x, const scalar e)
|
||||
{
|
||||
return Foam::pow(x, 1/e);
|
||||
}
|
||||
|
||||
//- Run tests
|
||||
int main()
|
||||
{
|
||||
Info<< "Averaging " << n << " power/root calculations with exponents "
|
||||
<< "ranging from " << - E << " to " << + E << ":" << endl;
|
||||
|
||||
Info<< incrIndent;
|
||||
|
||||
Info<< indent << " Scalar power: " << flush;
|
||||
const Pair<scalar> scalarPowYTime =
|
||||
run(static_cast<scalar (*)(scalar, scalar)>(&scalarPow));
|
||||
Info<< "Calculated value " << scalarPowYTime.first()
|
||||
<< " in " << scalarPowYTime.second() << " s" << endl;
|
||||
|
||||
Info<< indent << " Integer power: " << flush;
|
||||
const Pair<scalar> integerPowYTime =
|
||||
run(static_cast<scalar (*)(scalar, label)>(&integerPow));
|
||||
Info<< "Calculated value " << integerPowYTime.first()
|
||||
<< " in " << integerPowYTime.second() << " s" << endl;
|
||||
|
||||
Info<< indent << " Scalar root: " << flush;
|
||||
const Pair<scalar> scalarRootYTime =
|
||||
run(static_cast<scalar (*)(scalar, scalar)>(&scalarRoot));
|
||||
Info<< "Calculated value " << scalarRootYTime.first()
|
||||
<< " in " << scalarRootYTime.second() << " s" << endl;
|
||||
|
||||
Info<< indent << " Integer root: " << flush;
|
||||
const Pair<scalar> integerRootYTime =
|
||||
run(static_cast<scalar (*)(scalar, label)>(&integerRoot));
|
||||
Info<< "Calculated value " << integerRootYTime.first()
|
||||
<< " in " << integerRootYTime.second() << " s" << endl;
|
||||
|
||||
Info<< decrIndent;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration | Website: https://openfoam.org
|
||||
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
|
||||
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
@ -50,6 +50,7 @@ void component(scalarField& sf, const UList<scalar>& f, const direction)
|
||||
sf = f;
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
void scalarField::replace(const direction, const UList<scalar>& sf)
|
||||
{
|
||||
@ -117,10 +118,15 @@ BINARY_TYPE_OPERATOR_SF(scalar, scalar, scalar, /, divide)
|
||||
|
||||
BINARY_FUNCTION(scalar, scalar, scalar, pow)
|
||||
BINARY_TYPE_FUNCTION(scalar, scalar, scalar, pow)
|
||||
BINARY_FUNCTION(scalar, scalar, label, integerPow)
|
||||
BINARY_TYPE_FUNCTION_FS(scalar, scalar, label, integerPow)
|
||||
BINARY_FUNCTION(scalar, scalar, label, integerRoot)
|
||||
BINARY_TYPE_FUNCTION_FS(scalar, scalar, label, integerRoot)
|
||||
|
||||
BINARY_FUNCTION(scalar, scalar, scalar, atan2)
|
||||
BINARY_TYPE_FUNCTION(scalar, scalar, scalar, atan2)
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
UNARY_FUNCTION(scalar, scalar, pow3)
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration | Website: https://openfoam.org
|
||||
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
|
||||
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
@ -60,6 +60,7 @@ void component
|
||||
const direction
|
||||
);
|
||||
|
||||
|
||||
template<>
|
||||
void scalarField::replace(const direction, const UList<scalar>& sf);
|
||||
|
||||
@ -68,7 +69,9 @@ void scalarField::replace(const direction, const scalar& s);
|
||||
|
||||
|
||||
void stabilise(scalarField& Res, const UList<scalar>& sf, const scalar s);
|
||||
|
||||
tmp<scalarField> stabilise(const UList<scalar>&, const scalar s);
|
||||
|
||||
tmp<scalarField> stabilise(const tmp<scalarField>&, const scalar s);
|
||||
|
||||
|
||||
@ -90,6 +93,10 @@ BINARY_TYPE_OPERATOR_SF(scalar, scalar, scalar, /, divide)
|
||||
|
||||
BINARY_FUNCTION(scalar, scalar, scalar, pow)
|
||||
BINARY_TYPE_FUNCTION(scalar, scalar, scalar, pow)
|
||||
BINARY_FUNCTION(scalar, scalar, label, integerPow)
|
||||
BINARY_TYPE_FUNCTION_FS(scalar, scalar, label, integerPow)
|
||||
BINARY_FUNCTION(scalar, scalar, label, integerRoot)
|
||||
BINARY_TYPE_FUNCTION_FS(scalar, scalar, label, integerRoot)
|
||||
|
||||
BINARY_FUNCTION(scalar, scalar, scalar, atan2)
|
||||
BINARY_TYPE_FUNCTION(scalar, scalar, scalar, atan2)
|
||||
|
||||
@ -124,6 +124,7 @@ namespace Foam
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
//- Read a single scalar from an input stream
|
||||
scalar readScalar(Istream& is);
|
||||
|
||||
//- Normalised upper incomplete gamma function
|
||||
@ -140,9 +141,19 @@ namespace Foam
|
||||
|
||||
//- Inverse normalised lower incomplete gamma function
|
||||
scalar invIncGammaRatio_P(const scalar a, const scalar P);
|
||||
|
||||
//- Compute the power of the number x to the integer e
|
||||
inline scalar integerPow(const scalar x, const label e);
|
||||
|
||||
//- Compute the power of the number x to the reciprocal integer 1/e
|
||||
inline scalar integerRoot(const scalar x, const label e);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#include "scalarI.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
74
src/OpenFOAM/primitives/Scalar/scalar/scalarI.H
Normal file
74
src/OpenFOAM/primitives/Scalar/scalar/scalarI.H
Normal file
@ -0,0 +1,74 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration | Website: https://openfoam.org
|
||||
\\ / A nd | Copyright (C) 2023 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM 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.
|
||||
|
||||
OpenFOAM 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 OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "scalar.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::scalar Foam::integerPow(const scalar x, const label e)
|
||||
{
|
||||
if (e == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
scalar xx = e > 0 ? x : 1/x;
|
||||
|
||||
scalar y = 1;
|
||||
|
||||
for (label i = e; i != 0; i /= 2)
|
||||
{
|
||||
if (i % 2)
|
||||
{
|
||||
y *= xx;
|
||||
}
|
||||
|
||||
xx *= xx;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::scalar Foam::integerRoot(const scalar x, const label e)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case -4: return 1/pow025(x);
|
||||
case -3: return 1/cbrt(x);
|
||||
case -2: return 1/sqrt(x);
|
||||
case -1: return 1/x;
|
||||
case 0: return NaN;
|
||||
case 1: return x;
|
||||
case 2: return sqrt(x);
|
||||
case 3: return cbrt(x);
|
||||
case 4: return pow025(x);
|
||||
}
|
||||
|
||||
return pow(x, scalar(1)/e);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration | Website: https://openfoam.org
|
||||
\\ / A nd | Copyright (C) 2021 OpenFOAM Foundation
|
||||
\\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
@ -103,28 +103,7 @@ inline Foam::scalar Foam::pow(const scalar x, const specieExponent& e)
|
||||
{
|
||||
if (e.hasIntegerExponent())
|
||||
{
|
||||
if (e.integerExponent_ == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
scalar xx = e.integerExponent_ > 0 ? x : 1/x;
|
||||
|
||||
scalar y = 1;
|
||||
|
||||
for (label i = e.integerExponent_; i != 0; i /= 2)
|
||||
{
|
||||
if (i % 2)
|
||||
{
|
||||
y *= xx;
|
||||
}
|
||||
|
||||
xx *= xx;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
return integerPow(x, e.integerExponent_);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user