1 #include "libmesh/fparser_ad.hh" 
   21            double _min, 
double _max, 
double _dx = 1e-6,
 
   22            double _reltol = 1e-5, 
int _steps = 20, 
double _abstol = 1e-10) :
 
   31       CPPUNIT_ASSERT_MESSAGE (
"Failed to parse test function", 
F.Parse(
func, 
"x") == -1);
 
   36       CPPUNIT_ASSERT_MESSAGE (
"Failed to take derivative of function", 
dF.AutoDiff(
"x") == -1);
 
   39       CPPUNIT_ASSERT_MESSAGE (
"Failed to take derivative of optimized function", 
dFopt.AutoDiff(
"x") == -1);
 
   41       dFaopt.SetADFlags(FunctionParserAD::ADAutoOptimize, 
true);
 
   42       CPPUNIT_ASSERT_MESSAGE (
"Failed to take derivative of auto-optimized function", 
dFaopt.AutoDiff(
"x") == -1);
 
   47       double x1, x2, vdF, vF1, vF2, fd;
 
   62               std::cout << 
"Error in " << 
func << 
": " << fd << 
"!=" << vdF << 
" at x=" << x << 
'\n';
 
   69               std::cout << 
"Error in opt " << 
func << 
": " << fd << 
"!=" << vdF << 
" at x=" << x << 
'\n';
 
   76               std::cout << 
"Error in auto opt " << 
func << 
": " << fd << 
"!=" << vdF << 
" at x=" << x << 
'\n';
 
   97     tests.push_back(
ADTest(
"log(x*x) + log2(2*x) + log10(4*x)", 0.1, 3.0));
 
   98     tests.push_back(
ADTest(
"sin(-x) + cos(2*x) + tan(4*x)", -5.0, 5.0, 1e-7, 1e-5, 100));
 
   99     tests.push_back(
ADTest(
"sinh(-x) + cosh(x/2) + tanh(x/3)", -4.0, 4.0, 0.0001, 1e-5, 100));
 
  100     tests.push_back(
ADTest(
"plog(-x,0.01)", 0.001, 0.05, 0.00001, 1e-5, 100));
 
  101     tests.push_back(
ADTest(
"2 + 4*x + 8*x^2 + 16*x^3 + 32*x^4", -5.0, 5.0, 1e-5,1e-4));
 
  103     tests.push_back(
ADTest(
"sqrt(x*2)", 0.001, 2.0, 1e-6));
 
  108     tests.push_back(
ADTest(
"x*sin(-x)*log(x)*tanh(x)", 0.001, 5, 1e-8));
 
  109     tests.push_back(
ADTest(
"exp(-x) + 2*exp2(x)", -1.0, 2.0));
 
  110     tests.push_back(
ADTest(
"hypot(2*x,1) - hypot(1,4*x)", -10, 10.0));
 
  111     tests.push_back(
ADTest(
"if(x<0, (-x)^3, x^3)", -1.0, 1.0));
 
  112     tests.push_back(
ADTest(
"max(x^2-0.5,0)", -1.5, 1.5));
 
  113     tests.push_back(
ADTest(
"min(x^2-0.5,0)", -1.5, 1.5));
 
  114     tests.push_back(
ADTest(
"atan2(x,1) + atan2(2,x)", -0.99, 0.99));
 
  115     tests.push_back(
ADTest(
"0.767^sin(x)", -1.5, 1.5));
 
  116     tests.push_back(
ADTest(
"A := sin(x) + tanh(x); A + sqrt(A) - x", -1.5, 1.5));
 
  117     tests.push_back(
ADTest(
"3*sin(2*x)*sin(2*x)", -5.0, 5.0, 1e-7, 1e-5, 100));
 
  118     tests.push_back(
ADTest(
"erf(0.5*x)", -2., 2., 1e-6, 1e-5, 100));
 
  123     const unsigned int ntests = 
tests.size();
 
  125     unsigned int passed = 0;
 
  126     for (
unsigned i = 0; i < ntests; ++i)
 
  127       passed += 
tests[i].run() ? 1 : 0;
 
  129     CPPUNIT_ASSERT_EQUAL (passed, ntests);
 
  135     std::string func = 
"x*a";
 
  136     R.SetADFlags(FunctionParserAD::ADCacheDerivatives, 
true);
 
  139     R.Parse(func, 
"x,a");
 
  143     R.RegisterDerivative(
"a", 
"x", 
"y");
 
  151     FunctionParserAD dR(R);
 
  152     CPPUNIT_ASSERT_EQUAL (dR.AutoDiff(
"x"), -1);
 
  156     FunctionParserAD d2R(dR);
 
  157     CPPUNIT_ASSERT_EQUAL (d2R.AutoDiff(
"x"), -1);
 
  162     for (x = -1.0; x < 1.0; x+=0.3726)
 
  163       for (a = -1.0; a < 1.0; a+=0.2642)
 
  164         for (y = -1.0; y < 1.0; y+=0.3156)
 
  166             LIBMESH_ASSERT_FP_EQUAL(R.Eval(p), x*a, 1.e-12);
 
  167             LIBMESH_ASSERT_FP_EQUAL(dR.Eval(p), a+x*y, 1.e-12);
 
  168             LIBMESH_ASSERT_FP_EQUAL(d2R.Eval(p), 2*y, 1.e-12);
 
  177     std::string func = 
"x*a";
 
  178     R.SetADFlags(FunctionParserAD::ADCacheDerivatives, 
true);
 
  181     R.Parse(func, 
"x,a");
 
  185     R.RegisterDerivative(
"a", 
"x", 
"a");
 
  193     FunctionParserAD dR(R);
 
  194     CPPUNIT_ASSERT_EQUAL (dR.AutoDiff(
"x"), -1);
 
  198     FunctionParserAD d2R(dR);
 
  199     CPPUNIT_ASSERT_EQUAL (d2R.AutoDiff(
"x"), -1);
 
  204     for (x = -1.0; x < 1.0; x+=0.3726)
 
  205       for (a = -1.0; a < 1.0; a+=0.2642)
 
  206         for (y = -1.0; y < 1.0; y+=0.3156)
 
  208             LIBMESH_ASSERT_FP_EQUAL(R.Eval(p), x*a, 1.e-12);
 
  209             LIBMESH_ASSERT_FP_EQUAL(dR.Eval(p), a+x*a, 1.e-12);
 
  210             LIBMESH_ASSERT_FP_EQUAL(d2R.Eval(p), 2*a+x*a, 1.e-12);