Skip to content
Permalink
Browse files

Merge pull request #13185 from tophmatthews/poly_13184

Added MathUtils::poly
  • Loading branch information...
permcody committed Apr 11, 2019
2 parents bdb5adf + ad18cf8 commit f263fc4935ecd120190be9e78243448a1f264c3a
@@ -0,0 +1,15 @@
# MathUtils Namespace

[MOOSE] includes a number of C++ utility classes and functions that may be useful for developing
applications with mathematical expressions.

### Polynomial evaluations
`MathUtils::poly` evaluates a polynomial for any integer order $n>0$ using the Horner's method of evaluation,
\begin{equation}
p(x) = c_0 + c_1 x + c_2 x^2 + ... + a_n x^n
\end{equation}
\begin{equation}
p(x) = c_0 + x * (a_1 + x(a_2 + ... + x (a_{n-1} + x a_n)))
\end{equation}

The derivative is optionally returned via the `derivative parameter`.
@@ -38,6 +38,7 @@ Real poly4Log(Real x, Real tol, int order);
Real taylorLog(Real x);

Real pow(Real x, int e);
Real poly(std::vector<Real> c, const Real x, const bool deriv);

inline Real
heavyside(Real x)
@@ -106,6 +107,30 @@ dotProduct(const W<T> & a, const W2<T2> & b)
return a.contract(b);
}

template <typename T>
T
poly(std::vector<Real> c, const T x, const bool derivative)
{
const unsigned int size = c.size();
if (size == 0)
return 0.0;

T value = c[0];
if (derivative)
{
value *= size - 1;
for (unsigned int i = 1; i < size - 1; i++)
value = value * x + c[i] * (size - i - 1);
}
else
{
for (unsigned int i = 1; i < size; i++)
value = value * x + c[i];
}

return value;
}

} // namespace MathUtils

#endif // MATHUTILS_H
@@ -0,0 +1,31 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#ifndef POLYTESTFUNCTION_H
#define POLYTESTFUNCTION_H

#include "Function.h"

class PolyTestFunction;

template <>
InputParameters validParams<PolyTestFunction>();

class PolyTestFunction : public Function
{
public:
PolyTestFunction(const InputParameters & parameters);

virtual Real value(Real t, const Point & p);

const std::vector<Real> _coeffs;
const bool _deriv;
};

#endif // POLYTESTFUNCTION_H
@@ -0,0 +1,39 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "PolyTestFunction.h"

#include "MathUtils.h"

registerMooseObject("MooseTestApp", PolyTestFunction);

template <>
InputParameters
validParams<PolyTestFunction>()
{
InputParameters params = validParams<Function>();
params.addRequiredParam<std::vector<Real>>("coefficients",
"Coefficients to use for the polynomial evaluation");
params.addParam<bool>(
"derivative", false, "Flag to calculate the derivative of the polynomial function");
return params;
}

PolyTestFunction::PolyTestFunction(const InputParameters & parameters)
: Function(parameters),
_coeffs(getParam<std::vector<Real>>("coefficients")),
_deriv(getParam<bool>("derivative"))
{
}

Real
PolyTestFunction::value(Real /*t*/, const Point & p)
{
return MathUtils::poly<Real>(_coeffs, p(0), _deriv);
}
@@ -0,0 +1,11 @@
x,y,z,id,constant,constant_exact,quadratic,quadratic_exact,tenth,tenth_exact,tenth_derivative,tenth_derivative_exact
0,0,0,0,1,1,0,0,-0.1,-0.1,0.1,0.1
0.11111111111111,0,0,0.11111111111111,1,1,0.024691358024691,0.024691358024691,-0.091000000031261,-0.091000000031261,0.064799996935629,0.064799996935629
0.22222222222222,0,0,0.22222222222222,1,1,0.098765432098765,0.098765432098765,-0.085124024707371,-0.085124024707371,0.042596740669542,0.042596740669542
0.33333333333333,0,0,0.33333333333333,1,1,0.22222222222222,0.22222222222222,-0.081254551304849,-0.081254551304849,0.027978458568308,0.027978458568308
0.44444444444444,0,0,0.44444444444444,1,1,0.39506172839506,0.39506172839506,-0.078797162801693,-0.078797162801693,0.016058444704508,0.016058444704508
0.55555555555556,0,0,0.55555555555556,1,1,0.61728395061728,0.61728395061728,-0.078105388598703,-0.078105388598703,-0.0085452452670876,-0.0085452452670876
0.66666666666667,0,0,0.66666666666667,1,1,0.88888888888889,0.88888888888889,-0.083352808684313,-0.083352808684313,-0.10945994004979,-0.10945994004979
0.77777777777778,0,0,0.77777777777778,1,1,1.2098765432099,1.2098765432099,-0.11282754273168,-0.11282754273168,-0.50333018886877,-0.50333018886877
0.88888888888889,0,0,0.88888888888889,1,1,1.5802469135802,1.5802469135802,-0.22767435413337,-0.22767435413337,-1.8017824862639,-1.8017824862639
1,0,0,1,1,1,2,2,-0.6,-0.6,-5.5,-5.5
@@ -0,0 +1,63 @@
[Mesh]
type = GeneratedMesh
dim = 1
[]

[Problem]
solve = false
[]

[Functions]
[./constant]
type = PolyTestFunction
coefficients = '1'
[../]
[./constant_exact]
type = ParsedFunction
value = '1'
[../]
[./quadratic]
type = PolyTestFunction
coefficients = '2 0 0'
[../]
[./quadratic_exact]
type = ParsedFunction
value = '2 * x * x'
[../]
[./tenth]
type = PolyTestFunction
coefficients = '-1.0 0.9 -0.8 0.7 -0.6 0.5 -0.4 0.3 -0.2 0.1 -0.1'
[../]
[./tenth_exact]
type = ParsedFunction
value = '-0.1 + 0.1 * x - 0.2 * x^2 + 0.3 * x^3 - 0.4 * x^4 + 0.5 * x^5 - 0.6 * x^6 + 0.7 * x^7 - 0.8 * x^8 + 0.9 * x^9 - 1.0 * x^10'
[../]
[./tenth_derivative]
type = PolyTestFunction
coefficients = '-1.0 0.9 -0.8 0.7 -0.6 0.5 -0.4 0.3 -0.2 0.1 -0.1'
derivative = true
[../]
[./tenth_derivative_exact]
type = ParsedFunction
value = '0.1 - 2.0 * 0.2 * x^1 + 3.0 * 0.3 * x^2 - 4.0 * 0.4 * x^3 + 5.0 * 0.5 * x^4 - 6.0 * 0.6 * x^5 + 7.0 * 0.7 * x^6 - 8.0 * 0.8 * x^7 + 9.0 * 0.9 * x^8 - 10.0 * 1.0 * x^9'
[../]
[]

[VectorPostprocessors]
[./out]
type = LineFunctionSampler
functions = 'constant constant_exact quadratic quadratic_exact tenth tenth_exact tenth_derivative tenth_derivative_exact'
start_point = '0 0 0'
end_point = '1 0 0'
num_points = 10
sort_by = x
[../]
[]

[Executioner]
type = Steady
[]

[Outputs]
csv = true
[]
@@ -0,0 +1,10 @@
[Tests]
[./poly]
type = 'CSVDiff'
input = 'poly.i'
csvdiff = 'poly_out_out_0001.csv'
issues = '#13184'
design = 'MathUtils.md'
requirement = 'Moose shall have a function that evaluates the polynomial and derivative of polyonimal of varying degree and arbitrary coefficients'
[../]
[]

0 comments on commit f263fc4

Please sign in to comment.
You can’t perform that action at this time.