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_SUITE_END ()
 
virtual void setUp ()
 
void runTests ()
 
void registerDerivativeTest ()
 

Private Attributes

std::vector< ADTesttests
 

Detailed Description

Definition at line 19 of file autodiff.C.

Member Function Documentation

FParserAutodiffTest::CPPUNIT_TEST ( runTests  )
FParserAutodiffTest::CPPUNIT_TEST ( registerDerivativeTest  )
FParserAutodiffTest::CPPUNIT_TEST_SUITE ( FParserAutodiffTest  )
FParserAutodiffTest::CPPUNIT_TEST_SUITE_END ( )
void FParserAutodiffTest::registerDerivativeTest ( )

Definition at line 145 of file autodiff.C.

References CPPUNIT_TEST_SUITE_REGISTRATION(), FParserAutodiffTest::ADTest::func, and libMesh::x.

146  {
147  FunctionParserAD R;
148  std::string func = "x*a";
149 
150  // Parse the input expression into bytecode
151  R.Parse(func, "x,a");
152 
153  // add a new variable y and map it to the da/dx derivative
154  R.AddVariable("y");
155  R.RegisterDerivative("a", "x", "y");
156 
157  // parameter vector
158  double p[3];
159  double & x = p[0];
160  double & a = p[1];
161  double & y = p[2];
162 
163  FunctionParserAD dR(R);
164  CPPUNIT_ASSERT_EQUAL (dR.AutoDiff("x"), -1);
165  dR.Optimize();
166  // dR = a+x*y
167 
168  FunctionParserAD d2R(dR);
169  CPPUNIT_ASSERT_EQUAL (d2R.AutoDiff("x"), -1);
170  d2R.Optimize();
171  // d2R = 2*y
172 
173  // we probe the parsers and check if they agree with the reference solution
174  for (x = -1.0; x < 1.0; x+=0.3726)
175  for (a = -1.0; a < 1.0; a+=0.2642)
176  for (y = -1.0; y < 1.0; y+=0.3156)
177  {
178  CPPUNIT_ASSERT_DOUBLES_EQUAL(R.Eval(p), x*a, 1.e-12);
179  CPPUNIT_ASSERT_DOUBLES_EQUAL(dR.Eval(p), a+x*y, 1.e-12);
180  CPPUNIT_ASSERT_DOUBLES_EQUAL(d2R.Eval(p), 2*y, 1.e-12);
181  }
182  }
PetscErrorCode Vec x
void FParserAutodiffTest::runTests ( )

Definition at line 134 of file autodiff.C.

References FParserAutodiffTest::ADTest::run().

135  {
136  const unsigned int ntests = tests.size();
137 
138  unsigned int passed = 0;
139  for (unsigned i = 0; i < ntests; ++i)
140  passed += tests[i].run() ? 1 : 0;
141 
142  CPPUNIT_ASSERT_EQUAL (passed, ntests);
143  }
std::vector< ADTest > tests
Definition: autodiff.C:105
virtual void FParserAutodiffTest::setUp ( )
virtual

Definition at line 108 of file autodiff.C.

References FParserAutodiffTest::ADTest::ADTest().

109  {
110  tests.push_back(ADTest("log(x*x) + log2(2*x) + log10(4*x)", 0.1, 3.0));
111  tests.push_back(ADTest("sin(-x) + cos(2*x) + tan(4*x)", -5.0, 5.0, 1e-7, 1e-5, 100));
112  tests.push_back(ADTest("sinh(-x) + cosh(x/2) + tanh(x/3)", -4.0, 4.0, 0.0001, 1e-5, 100));
113  tests.push_back(ADTest("plog(-x,0.01)", 0.001, 0.05, 0.00001, 1e-5, 100));
114  tests.push_back(ADTest("2 + 4*x + 8*x^2 + 16*x^3 + 32*x^4", -5.0, 5.0, 1e-5,1e-4));
115  tests.push_back(ADTest("1/x^2", 0.01, 2.0, 1e-8));
116  tests.push_back(ADTest("sqrt(x*2)", 0.001, 2.0, 1e-6));
117  tests.push_back(ADTest("abs(x*2)", -1.99, 2.0));
118  tests.push_back(ADTest("asin(-x)", -0.99, 0.99));
119  tests.push_back(ADTest("acos(-x)", -0.99, 0.99));
120  tests.push_back(ADTest("atan(-x)", -99, 99));
121  tests.push_back(ADTest("x*sin(-x)*log(x)*tanh(x)", 0.001, 5, 1e-8));
122  tests.push_back(ADTest("exp(-x) + 2*exp2(x)", -1.0, 2.0));
123  tests.push_back(ADTest("hypot(2*x,1) - hypot(1,4*x)", -10, 10.0));
124  tests.push_back(ADTest("if(x<0, (-x)^3, x^3)", -1.0, 1.0));
125  tests.push_back(ADTest("max(x^2-0.5,0)", -1.5, 1.5));
126  tests.push_back(ADTest("min(x^2-0.5,0)", -1.5, 1.5));
127  tests.push_back(ADTest("atan2(x,1) + atan2(2,x)", -0.99, 0.99));
128  tests.push_back(ADTest("0.767^sin(x)", -1.5, 1.5));
129  tests.push_back(ADTest("A := sin(x) + tanh(x); A + sqrt(A) - x", -1.5, 1.5));
130  tests.push_back(ADTest("3*sin(2*x)*sin(2*x)", -5.0, 5.0, 1e-7, 1e-5, 100));
131  tests.push_back(ADTest("erf(0.5*x)", -2., 2., 1e-6, 1e-5, 100));
132  }
std::vector< ADTest > tests
Definition: autodiff.C:105

Member Data Documentation

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

Definition at line 105 of file autodiff.C.


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