libMesh
Public Member Functions | Private Attributes | List of all members
FParserAutodiffTest::ADTest Class Reference

Public Member Functions

 ADTest (const std::string &_func, double _min, double _max, double _dx=1e-6, double _reltol=1e-5, int _steps=20, double _abstol=1e-10)
 
bool run ()
 

Private Attributes

std::string func
 
double min
 
double max
 
double dx
 
double reltol
 
double abstol
 
int steps
 
FunctionParserAD F
 
FunctionParserAD dF
 
FunctionParserAD dFopt
 
FunctionParserAD dFaopt
 

Detailed Description

Definition at line 31 of file autodiff.C.

Constructor & Destructor Documentation

FParserAutodiffTest::ADTest::ADTest ( const std::string &  _func,
double  _min,
double  _max,
double  _dx = 1e-6,
double  _reltol = 1e-5,
int  _steps = 20,
double  _abstol = 1e-10 
)

Definition at line 33 of file autodiff.C.

References dF, dFaopt, dFopt, F, and func.

Referenced by FParserAutodiffTest::setUp().

35  :
36  func(_func),
37  min(_min),
38  max(_max),
39  dx(_dx),
40  reltol(_reltol),
41  abstol(_abstol),
42  steps(_steps)
43  {
44  CPPUNIT_ASSERT_MESSAGE ("Failed to parse test function", F.Parse(func, "x") == -1);
45  dF.Parse(func, "x");
46  dFopt.Parse(func, "x");
47  dFaopt.Parse(func, "x");
48 
49  CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of function", dF.AutoDiff("x") == -1);
50 
51  dFopt.Optimize();
52  CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of optimized function", dFopt.AutoDiff("x") == -1);
53 
54  dFaopt.SetADFlags(FunctionParserAD::ADAutoOptimize, true);
55  CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of auto-optimized function", dFaopt.AutoDiff("x") == -1);
56  }
FunctionParserAD F
Definition: autodiff.C:102
FunctionParserAD dF
Definition: autodiff.C:102
FunctionParserAD dFopt
Definition: autodiff.C:102
FunctionParserAD dFaopt
Definition: autodiff.C:102

Member Function Documentation

bool FParserAutodiffTest::ADTest::run ( )

Definition at line 58 of file autodiff.C.

References std::abs(), abstol, dF, dFaopt, dFopt, dx, F, func, max, min, reltol, steps, and libMesh::x.

Referenced by FParserAutodiffTest::runTests().

59  {
60  double x1, x2, vdF, vF1, vF2, fd;
61  for (double x = min; x <= max; x += (max-min) / double(steps))
62  {
63  x1 = x - dx/2.0;
64  x2 = x + dx/2.0;
65 
66  vF1 = F.Eval(&x1);
67  vF2 = F.Eval(&x2);
68  fd = (vF2-vF1) / dx;
69 
70  // CPPUNIT_ASSERT(std::abs(fd - vdF) > tol)
71  // CPPUNIT_ASSERT(std::abs(fd - vdFopt) > tol)
72  vdF = dF.Eval(&x);
73  if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
74  {
75  std::cout << "Error in " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
76  return false;
77  }
78 
79  vdF = dFopt.Eval(&x);
80  if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
81  {
82  std::cout << "Error in opt " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
83  return false;
84  }
85 
86  vdF = dFaopt.Eval(&x);
87  if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
88  {
89  std::cout << "Error in auto opt " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
90  return false;
91  }
92  }
93 
94  return true;
95  }
FunctionParserAD F
Definition: autodiff.C:102
double abs(double a)
PetscErrorCode Vec x
FunctionParserAD dF
Definition: autodiff.C:102
FunctionParserAD dFopt
Definition: autodiff.C:102
FunctionParserAD dFaopt
Definition: autodiff.C:102

Member Data Documentation

double FParserAutodiffTest::ADTest::abstol
private

Definition at line 99 of file autodiff.C.

Referenced by run().

FunctionParserAD FParserAutodiffTest::ADTest::dF
private

Definition at line 102 of file autodiff.C.

Referenced by ADTest(), and run().

FunctionParserAD FParserAutodiffTest::ADTest::dFaopt
private

Definition at line 102 of file autodiff.C.

Referenced by ADTest(), and run().

FunctionParserAD FParserAutodiffTest::ADTest::dFopt
private

Definition at line 102 of file autodiff.C.

Referenced by ADTest(), and run().

double FParserAutodiffTest::ADTest::dx
private

Definition at line 99 of file autodiff.C.

Referenced by run().

FunctionParserAD FParserAutodiffTest::ADTest::F
private

Definition at line 102 of file autodiff.C.

Referenced by ADTest(), and run().

std::string FParserAutodiffTest::ADTest::func
private

Definition at line 98 of file autodiff.C.

Referenced by ADTest(), FParserAutodiffTest::registerDerivativeTest(), and run().

double FParserAutodiffTest::ADTest::max
private

Definition at line 99 of file autodiff.C.

Referenced by run().

double FParserAutodiffTest::ADTest::min
private

Definition at line 99 of file autodiff.C.

Referenced by run().

double FParserAutodiffTest::ADTest::reltol
private

Definition at line 99 of file autodiff.C.

Referenced by run().

int FParserAutodiffTest::ADTest::steps
private

Definition at line 100 of file autodiff.C.

Referenced by run().


The documentation for this class was generated from the following file: