1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 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/>.
Application
PolynomialTest
Description
Test application for the templated Polynomial class
\*---------------------------------------------------------------------------*/
#include "IStringStream.H"
#include "Polynomial.H"
#include "polynomialFunction.H"
#include "Random.H"
#include "cpuTime.H"
using namespace Foam;
const int PolySize = 8;
const scalar coeff[] = { 0.11, 0.45, -0.94, 1.58, -2.58, 0.08, 3.15, -4.78 };
const char* polyDef = "(0.11 0.45 -0.94 1.58 -2.58 0.08 3.15 -4.78)";
scalar polyValue(const scalar x)
{
// Hard-coded polynomial 8 coeff (7th order)
return
coeff[0]
+ coeff[1]*x
+ coeff[2]*sqr(x)
+ coeff[3]*pow3(x)
+ coeff[4]*pow4(x)
+ coeff[5]*pow5(x)
+ coeff[6]*pow6(x)
+ coeff[7]*x*pow6(x);
}
scalar intPolyValue(const scalar x)
{
// Hard-coded integral form of above polynomial
return
coeff[0]*x
+ coeff[1]/2.0*sqr(x)
+ coeff[2]/3.0*pow3(x)
+ coeff[3]/4.0*pow4(x)
+ coeff[4]/5.0*pow5(x)
+ coeff[5]/6.0*pow6(x)
+ coeff[6]/7.0*x*pow6(x)
+ coeff[7]/8.0*x*x*pow6(x);
}
scalar polyValue1(const scalar x)
{
// "normal" evaluation using pow()
scalar value = coeff[0];
for (int i=1; i < PolySize; ++i)
{
value += coeff[i]*pow(x, i);
}
return value;
}
scalar polyValue2(const scalar x)
{
// calculation avoiding pow()
scalar value = coeff[0];
scalar powX = x;
for (int i=1; i < PolySize; ++i)
{
value += coeff[i] * powX;
powX *= x;
}
return value;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
const label n = 10000;
const label nIters = 1000;
scalar sum = 0.0;
Info<< "null poly = " << (Polynomial<8>()) << nl
<< "null poly = " << (polynomialFunction(8)) << nl
<< endl;
Polynomial<8> poly(coeff);
Polynomial<9> intPoly(poly.integral(0.0));
IStringStream is(polyDef);
polynomialFunction polyfunc(is);
Info<< "poly = " << poly << nl
<< "intPoly = " << intPoly << nl
<< endl;
Info<< "2*poly = " << 2*poly << nl
<< "poly+poly = " << poly + poly << nl
<< "3*poly = " << 3*poly << nl
<< "poly+poly+poly = " << poly + poly + poly << nl
<< "3*poly - 2*poly = " << 3*poly - 2*poly << nl
<< endl;
Info<< nl << "--- as polynomialFunction" << nl << endl;
Info<< "polyf = " << polyfunc << nl
<< "intPoly = " << poly.integral(0.0) << nl
<< endl;
Info<< "2*polyf = " << 2*polyfunc << nl
<< "polyf+polyf = " << polyfunc + polyfunc << nl
<< "3*polyf = " << 3*polyfunc << nl
<< "polyf+polyf+polyf = " << polyfunc + polyfunc + polyfunc << nl
<< "3*polyf - 2*polyf = " << 3*polyfunc - 2*polyfunc << nl
<< endl;
Polynomial<8> polyCopy = poly;
Info<< "poly, polyCopy = " << poly << ", " << polyCopy << nl << endl;
polyCopy = 2.5*poly;
Info<< "2.5*poly = " << polyCopy << nl << endl;
Random rnd(123456);
for (int i=0; i<10; i++)
{
scalar x = rnd.scalar01()*100;
scalar px = polyValue(x);
scalar ipx = intPolyValue(x);
scalar pxTest = poly.value(x);
scalar ipxTest = intPoly.value(x);
Info<<"\nx = " << x << endl;
Info<< " px, pxTest = " << px << ", " << pxTest << endl;
Info<< " ipx, ipxTest = " << ipx << ", " << ipxTest << endl;
if (mag(px - pxTest) > SMALL)
{
Info<< " *** WARNING: px != pxTest: " << px - pxTest << endl;
}
if (mag(ipx - ipxTest) > SMALL)
{
Info<< " *** WARNING: ipx != ipxTest: " << ipx - ipxTest << endl;
}
Info<< endl;
}
//
// test speed of Polynomial:
//
Info<< "start timing loops" << nl
<< "~~~~~~~~~~~~~~~~~~" << endl;
cpuTime timer;
for (int loop = 0; loop < n; ++loop)
{
sum = 0.0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += poly.value(loop+iter);
}
}
Info<< "value: " << sum
<< " in " << timer.cpuTimeIncrement() << " s\n";
for (int loop = 0; loop < n; ++loop)
{
sum = 0.0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += polyfunc.value(loop+iter);
}
}
Info<< "via function: " << sum
<< " in " << timer.cpuTimeIncrement() << " s\n";
for (int loop = 0; loop < n; ++loop)
{
sum = 0.0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += polyValue(loop+iter);
}
}
Info<< "hard-coded 0: " << sum
<< " in " << timer.cpuTimeIncrement() << " s\n";
for (int loop = 0; loop < n; ++loop)
{
sum = 0.0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += polyValue1(loop+iter);
}
}
Info<< "hard-coded 1: " << sum
<< " in " << timer.cpuTimeIncrement() << " s\n";
for (int loop = 0; loop < n; ++loop)
{
sum = 0.0;
for (label iter = 0; iter < nIters; ++iter)
{
sum += polyValue2(loop+iter);
}
}
Info<< "hard-coded 2: " << sum
<< " in " << timer.cpuTimeIncrement() << " s\n";
Info<< nl << "Done." << endl;
return 0;
}
// ************************************************************************* //
|