1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
28 Test application for the templated Polynomial class
30 \*---------------------------------------------------------------------------*/
32 #include "IStringStream.H"
33 #include "Polynomial.H"
34 #include "polynomialFunction.H"
40 const int PolySize = 8;
41 const scalar coeff[] = { 0.11, 0.45, -0.94, 1.58, -2.58, 0.08, 3.15, -4.78 };
42 const char* polyDef = "(0.11 0.45 -0.94 1.58 -2.58 0.08 3.15 -4.78)";
45 scalar polyValue(const scalar x)
47 // Hard-coded polynomial 8 coeff (7th order)
60 scalar intPolyValue(const scalar x)
62 // Hard-coded integral form of above polynomial
66 + coeff[2]/3.0*pow3(x)
67 + coeff[3]/4.0*pow4(x)
68 + coeff[4]/5.0*pow5(x)
69 + coeff[5]/6.0*pow6(x)
70 + coeff[6]/7.0*x*pow6(x)
71 + coeff[7]/8.0*x*x*pow6(x);
75 scalar polyValue1(const scalar x)
77 // "normal" evaluation using pow()
78 scalar value = coeff[0];
80 for (int i=1; i < PolySize; ++i)
82 value += coeff[i]*pow(x, i);
89 scalar polyValue2(const scalar x)
91 // calculation avoiding pow()
92 scalar value = coeff[0];
95 for (int i=1; i < PolySize; ++i)
97 value += coeff[i] * powX;
105 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
107 int main(int argc, char *argv[])
109 const label n = 10000;
110 const label nIters = 1000;
113 Info<< "null poly = " << (Polynomial<8>()) << nl
114 << "null poly = " << (polynomialFunction(8)) << nl
117 Polynomial<8> poly(coeff);
118 Polynomial<9> intPoly(poly.integral(0.0));
120 IStringStream is(polyDef);
121 polynomialFunction polyfunc(is);
123 Info<< "poly = " << poly << nl
124 << "intPoly = " << intPoly << nl
127 Info<< "2*poly = " << 2*poly << nl
128 << "poly+poly = " << poly + poly << nl
129 << "3*poly = " << 3*poly << nl
130 << "poly+poly+poly = " << poly + poly + poly << nl
131 << "3*poly - 2*poly = " << 3*poly - 2*poly << nl
134 Info<< nl << "--- as polynomialFunction" << nl << endl;
135 Info<< "polyf = " << polyfunc << nl
136 << "intPoly = " << poly.integral(0.0) << nl
139 Info<< "2*polyf = " << 2*polyfunc << nl
140 << "polyf+polyf = " << polyfunc + polyfunc << nl
141 << "3*polyf = " << 3*polyfunc << nl
142 << "polyf+polyf+polyf = " << polyfunc + polyfunc + polyfunc << nl
143 << "3*polyf - 2*polyf = " << 3*polyfunc - 2*polyfunc << nl
147 Polynomial<8> polyCopy = poly;
148 Info<< "poly, polyCopy = " << poly << ", " << polyCopy << nl << endl;
150 Info<< "2.5*poly = " << polyCopy << nl << endl;
153 for (int i=0; i<10; i++)
155 scalar x = rnd.scalar01()*100;
157 scalar px = polyValue(x);
158 scalar ipx = intPolyValue(x);
160 scalar pxTest = poly.value(x);
161 scalar ipxTest = intPoly.value(x);
163 Info<<"\nx = " << x << endl;
164 Info<< " px, pxTest = " << px << ", " << pxTest << endl;
165 Info<< " ipx, ipxTest = " << ipx << ", " << ipxTest << endl;
167 if (mag(px - pxTest) > SMALL)
169 Info<< " *** WARNING: px != pxTest: " << px - pxTest << endl;
172 if (mag(ipx - ipxTest) > SMALL)
174 Info<< " *** WARNING: ipx != ipxTest: " << ipx - ipxTest << endl;
182 // test speed of Polynomial:
184 Info<< "start timing loops" << nl
185 << "~~~~~~~~~~~~~~~~~~" << endl;
189 for (int loop = 0; loop < n; ++loop)
192 for (label iter = 0; iter < nIters; ++iter)
194 sum += poly.value(loop+iter);
197 Info<< "value: " << sum
198 << " in " << timer.cpuTimeIncrement() << " s\n";
200 for (int loop = 0; loop < n; ++loop)
203 for (label iter = 0; iter < nIters; ++iter)
205 sum += polyfunc.value(loop+iter);
208 Info<< "via function: " << sum
209 << " in " << timer.cpuTimeIncrement() << " s\n";
212 for (int loop = 0; loop < n; ++loop)
215 for (label iter = 0; iter < nIters; ++iter)
217 sum += polyValue(loop+iter);
220 Info<< "hard-coded 0: " << sum
221 << " in " << timer.cpuTimeIncrement() << " s\n";
224 for (int loop = 0; loop < n; ++loop)
227 for (label iter = 0; iter < nIters; ++iter)
229 sum += polyValue1(loop+iter);
232 Info<< "hard-coded 1: " << sum
233 << " in " << timer.cpuTimeIncrement() << " s\n";
235 for (int loop = 0; loop < n; ++loop)
238 for (label iter = 0; iter < nIters; ++iter)
240 sum += polyValue2(loop+iter);
243 Info<< "hard-coded 2: " << sum
244 << " in " << timer.cpuTimeIncrement() << " s\n";
247 Info<< nl << "Done." << endl;
253 // ************************************************************************* //