libMesh
Loading...
Searching...
No Matches
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 18 of file autodiff.C.

Constructor & Destructor Documentation

◆ ADTest()

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 
)
inline

Definition at line 20 of file autodiff.C.

22 :
23 func(_func),
24 min(_min),
25 max(_max),
26 dx(_dx),
27 reltol(_reltol),
28 abstol(_abstol),
29 steps(_steps)
30 {
31 CPPUNIT_ASSERT_MESSAGE ("Failed to parse test function", F.Parse(func, "x") == -1);
32 dF.Parse(func, "x");
33 dFopt.Parse(func, "x");
34 dFaopt.Parse(func, "x");
35
36 CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of function", dF.AutoDiff("x") == -1);
37
38 dFopt.Optimize();
39 CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of optimized function", dFopt.AutoDiff("x") == -1);
40
41 dFaopt.SetADFlags(FunctionParserAD::ADAutoOptimize, true);
42 CPPUNIT_ASSERT_MESSAGE ("Failed to take derivative of auto-optimized function", dFaopt.AutoDiff("x") == -1);
43 }
FunctionParserAD dF
Definition autodiff.C:89
FunctionParserAD dFopt
Definition autodiff.C:89
FunctionParserAD dFaopt
Definition autodiff.C:89
FunctionParserAD F
Definition autodiff.C:89

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

Member Function Documentation

◆ run()

bool FParserAutodiffTest::ADTest::run ( )
inline

Definition at line 45 of file autodiff.C.

46 {
47 double x1, x2, vdF, vF1, vF2, fd;
48 for (double x = min; x <= max; x += (max-min) / double(steps))
49 {
50 x1 = x - dx/2.0;
51 x2 = x + dx/2.0;
52
53 vF1 = F.Eval(&x1);
54 vF2 = F.Eval(&x2);
55 fd = (vF2-vF1) / dx;
56
57 // CPPUNIT_ASSERT(std::abs(fd - vdF) > tol)
58 // CPPUNIT_ASSERT(std::abs(fd - vdFopt) > tol)
59 vdF = dF.Eval(&x);
60 if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
61 {
62 std::cout << "Error in " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
63 return false;
64 }
65
66 vdF = dFopt.Eval(&x);
67 if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
68 {
69 std::cout << "Error in opt " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
70 return false;
71 }
72
73 vdF = dFaopt.Eval(&x);
74 if (std::abs(vdF) > abstol && std::abs((fd - vdF)/vdF) > reltol && std::abs(fd - vdF)> abstol)
75 {
76 std::cout << "Error in auto opt " << func << ": " << fd << "!=" << vdF << " at x=" << x << '\n';
77 return false;
78 }
79 }
80
81 return true;
82 }

References abstol, dF, dFaopt, dFopt, dx, F, func, max, min, reltol, and steps.

Member Data Documentation

◆ abstol

double FParserAutodiffTest::ADTest::abstol
private

Definition at line 86 of file autodiff.C.

Referenced by run().

◆ dF

FunctionParserAD FParserAutodiffTest::ADTest::dF
private

Definition at line 89 of file autodiff.C.

Referenced by ADTest(), and run().

◆ dFaopt

FunctionParserAD FParserAutodiffTest::ADTest::dFaopt
private

Definition at line 89 of file autodiff.C.

Referenced by ADTest(), and run().

◆ dFopt

FunctionParserAD FParserAutodiffTest::ADTest::dFopt
private

Definition at line 89 of file autodiff.C.

Referenced by ADTest(), and run().

◆ dx

double FParserAutodiffTest::ADTest::dx
private

Definition at line 86 of file autodiff.C.

Referenced by run().

◆ F

FunctionParserAD FParserAutodiffTest::ADTest::F
private

Definition at line 89 of file autodiff.C.

Referenced by ADTest(), and run().

◆ func

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

Definition at line 85 of file autodiff.C.

Referenced by ADTest(), and run().

◆ max

double FParserAutodiffTest::ADTest::max
private

Definition at line 86 of file autodiff.C.

Referenced by run().

◆ min

double FParserAutodiffTest::ADTest::min
private

Definition at line 86 of file autodiff.C.

Referenced by run().

◆ reltol

double FParserAutodiffTest::ADTest::reltol
private

Definition at line 86 of file autodiff.C.

Referenced by run().

◆ steps

int FParserAutodiffTest::ADTest::steps
private

Definition at line 87 of file autodiff.C.

Referenced by run().


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