libMesh
Classes | Public Member Functions | Private Attributes | List of all members
FParserAutodiffTest Class Reference
Inheritance diagram for FParserAutodiffTest:
[legend]

Classes

class  ADTest
 

Public Member Functions

 CPPUNIT_TEST_SUITE (FParserAutodiffTest)
 
 CPPUNIT_TEST (runTests)
 
 CPPUNIT_TEST (registerDerivativeTest)
 
 CPPUNIT_TEST (registerDerivativeRepeatTest)
 
 CPPUNIT_TEST_SUITE_END ()
 
virtual void setUp ()
 
void runTests ()
 
void registerDerivativeTest ()
 
void registerDerivativeRepeatTest ()
 

Private Attributes

std::vector< ADTesttests
 

Detailed Description

Definition at line 5 of file autodiff.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/3]

FParserAutodiffTest::CPPUNIT_TEST ( registerDerivativeRepeatTest  )

◆ CPPUNIT_TEST() [2/3]

FParserAutodiffTest::CPPUNIT_TEST ( registerDerivativeTest  )

◆ CPPUNIT_TEST() [3/3]

FParserAutodiffTest::CPPUNIT_TEST ( runTests  )

◆ CPPUNIT_TEST_SUITE()

FParserAutodiffTest::CPPUNIT_TEST_SUITE ( FParserAutodiffTest  )

◆ CPPUNIT_TEST_SUITE_END()

FParserAutodiffTest::CPPUNIT_TEST_SUITE_END ( )

◆ registerDerivativeRepeatTest()

void FParserAutodiffTest::registerDerivativeRepeatTest ( )
inline

Definition at line 172 of file autodiff.C.

173  {
174  // now do the same functional form but with a different mapping to see if the cache
175  // signature was correctly updated
176  FunctionParserAD R;
177  std::string func = "x*a";
178  R.SetADFlags(FunctionParserAD::ADCacheDerivatives, true);
179 
180  // Parse the input expression into bytecode
181  R.Parse(func, "x,a");
182 
183  // add a new variable y but do not map it to the da/dx derivative!
184  R.AddVariable("y");
185  R.RegisterDerivative("a", "x", "a");
186 
187  // parameter vector
188  double p[3];
189  double & x = p[0];
190  double & a = p[1];
191  double & y = p[2];
192 
193  FunctionParserAD dR(R);
194  CPPUNIT_ASSERT_EQUAL (dR.AutoDiff("x"), -1);
195  dR.Optimize();
196  // dR = a + x*a
197 
198  FunctionParserAD d2R(dR);
199  CPPUNIT_ASSERT_EQUAL (d2R.AutoDiff("x"), -1);
200  d2R.Optimize();
201  // d2R = 2*a + x*a
202 
203  // we probe the parsers and check if they agree with the reference solution
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)
207  {
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);
211  }
212  }

◆ registerDerivativeTest()

void FParserAutodiffTest::registerDerivativeTest ( )
inline

Definition at line 132 of file autodiff.C.

133  {
134  FunctionParserAD R;
135  std::string func = "x*a";
136  R.SetADFlags(FunctionParserAD::ADCacheDerivatives, true);
137 
138  // Parse the input expression into bytecode
139  R.Parse(func, "x,a");
140 
141  // add a new variable y and map it to the da/dx derivative
142  R.AddVariable("y");
143  R.RegisterDerivative("a", "x", "y");
144 
145  // parameter vector
146  double p[3];
147  double & x = p[0];
148  double & a = p[1];
149  double & y = p[2];
150 
151  FunctionParserAD dR(R);
152  CPPUNIT_ASSERT_EQUAL (dR.AutoDiff("x"), -1);
153  dR.Optimize();
154  // dR = a+x*y
155 
156  FunctionParserAD d2R(dR);
157  CPPUNIT_ASSERT_EQUAL (d2R.AutoDiff("x"), -1);
158  d2R.Optimize();
159  // d2R = 2*y
160 
161  // we probe the parsers and check if they agree with the reference solution
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)
165  {
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);
169  }
170  }

◆ runTests()

void FParserAutodiffTest::runTests ( )
inline

Definition at line 121 of file autodiff.C.

122  {
123  const unsigned int ntests = tests.size();
124 
125  unsigned int passed = 0;
126  for (unsigned i = 0; i < ntests; ++i)
127  passed += tests[i].run() ? 1 : 0;
128 
129  CPPUNIT_ASSERT_EQUAL (passed, ntests);
130  }

References tests.

◆ setUp()

virtual void FParserAutodiffTest::setUp ( )
inlinevirtual

Definition at line 95 of file autodiff.C.

96  {
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));
102  tests.push_back(ADTest("1/x^2", 0.01, 2.0, 1e-8));
103  tests.push_back(ADTest("sqrt(x*2)", 0.001, 2.0, 1e-6));
104  tests.push_back(ADTest("abs(x*2)", -1.99, 2.0));
105  tests.push_back(ADTest("asin(-x)", -0.99, 0.99));
106  tests.push_back(ADTest("acos(-x)", -0.99, 0.99));
107  tests.push_back(ADTest("atan(-x)", -99, 99));
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));
119  }

References tests.

Member Data Documentation

◆ tests

std::vector<ADTest> FParserAutodiffTest::tests
private

Definition at line 92 of file autodiff.C.

Referenced by runTests(), and setUp().


The documentation for this class was generated from the following file:
FParserAutodiffTest::tests
std::vector< ADTest > tests
Definition: autodiff.C:92