libMesh
parsed_function_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 // libmesh includes
8 #include "libmesh/auto_ptr.h"
9 #include "libmesh/elem.h"
10 #include "libmesh/equation_systems.h"
11 #include "libmesh/mesh.h"
12 #include "libmesh/mesh_generation.h"
13 #include "libmesh/numeric_vector.h"
14 #include "libmesh/parsed_function.h"
15 #include "libmesh/system.h"
16 
17 // test includes
18 #include "test_comm.h"
19 
20 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
21 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
22 // warnings. These can be ignored in GCC as long as we wrap the
23 // offending code in appropriate pragmas. We can't get away with a
24 // single ignore_warnings.h inclusion at the beginning of this file,
25 // since the libmesh headers pull in a restore_warnings.h at some
26 // point. We also don't bother restoring warnings at the end of this
27 // file since it's not a header.
28 #include <libmesh/ignore_warnings.h>
29 
30 using namespace libMesh;
31 
32 class ParsedFunctionTest : public CppUnit::TestCase
33 {
34 public:
35  void setUp() {}
36 
37  void tearDown() {}
38 
39  CPPUNIT_TEST_SUITE(ParsedFunctionTest);
40 
41  CPPUNIT_TEST(testValues);
42  CPPUNIT_TEST(testInlineGetter);
43  CPPUNIT_TEST(testInlineSetter);
44  CPPUNIT_TEST(testTimeDependence);
45 
46  CPPUNIT_TEST_SUITE_END();
47 
48 
49 private:
50 
51  void testValues()
52  {
53  ParsedFunction<Number> x2("x*2");
54 
55  CPPUNIT_ASSERT_DOUBLES_EQUAL
56  (libmesh_real(x2(Point(0.5,1.5,2.5))), 1.0, TOLERANCE*TOLERANCE);
57 
58  ParsedFunction<Number> xy8("x*y*8");
59 
60  CPPUNIT_ASSERT_DOUBLES_EQUAL
61  (libmesh_real(xy8(Point(0.5,1.5,2.5))), 6.0, TOLERANCE*TOLERANCE);
62  }
63 
65  {
66  ParsedFunction<Number> ax2("a:=4.5;a*x*2");
67 
68  CPPUNIT_ASSERT_DOUBLES_EQUAL
69  (libmesh_real(ax2(Point(0.25,0.25,0.25))), 2.25, TOLERANCE*TOLERANCE);
70 
71  CPPUNIT_ASSERT_DOUBLES_EQUAL
72  (libmesh_real(ax2.get_inline_value("a")), 4.5, TOLERANCE*TOLERANCE);
73 
75  ("a := 4 ; b := a/2+1; c:=b-a+3.5; c*x*2*y*4");
76 
77  CPPUNIT_ASSERT_DOUBLES_EQUAL
78  (libmesh_real(cxy8(Point(0.5,0.5,0.5))), 5.0, TOLERANCE*TOLERANCE);
79 
80  CPPUNIT_ASSERT_DOUBLES_EQUAL
81  (libmesh_real(cxy8.get_inline_value("b")), 3.0, TOLERANCE*TOLERANCE);
82 
83  CPPUNIT_ASSERT_DOUBLES_EQUAL
84  (libmesh_real(cxy8.get_inline_value("c")), 2.5, TOLERANCE*TOLERANCE);
85  }
86 
88  {
89  ParsedFunction<Number> ax2("a:=4.5;a*x*2");
90  ax2.set_inline_value("a", 2.5);
91 
92  CPPUNIT_ASSERT_DOUBLES_EQUAL
93  (libmesh_real(ax2(Point(0.25,0.25,0.25))), 1.25, TOLERANCE*TOLERANCE);
94 
95  CPPUNIT_ASSERT_DOUBLES_EQUAL
96  (libmesh_real(ax2.get_inline_value("a")), 2.5, TOLERANCE*TOLERANCE);
97 
99  ("a := 4 ; b := a/2+1; c:=b-a+3.5; c*x*2*y*4");
100  cxy8.set_inline_value("a", 2);
101 
102  CPPUNIT_ASSERT_DOUBLES_EQUAL
103  (libmesh_real(cxy8(Point(0.5,0.5,0.5))), 7.0, TOLERANCE*TOLERANCE);
104 
105  CPPUNIT_ASSERT_DOUBLES_EQUAL
106  (libmesh_real(cxy8.get_inline_value("b")), 2.0, TOLERANCE*TOLERANCE);
107 
108  CPPUNIT_ASSERT_DOUBLES_EQUAL
109  (libmesh_real(cxy8.get_inline_value("c")), 3.5, TOLERANCE*TOLERANCE);
110  }
111 
113  {
114  ParsedFunction<Number> no_t("x*2+y^2-tanh(z)+atan(x-y)");
115  CPPUNIT_ASSERT(!no_t.is_time_dependent());
116 
117  ParsedFunction<Number> xyt("x+y+t");
118  CPPUNIT_ASSERT(xyt.is_time_dependent());
119 
120  ParsedFunction<Number> x2y2t2("x*2+y^2+t^2");
121  CPPUNIT_ASSERT(x2y2t2.is_time_dependent());
122 
123  ParsedFunction<Number> ztanht("z*tanh(t)");
124  CPPUNIT_ASSERT(ztanht.is_time_dependent());
125  }
126 
127 };
128 
T libmesh_real(T a)
Output get_inline_value(const std::string &inline_var_name) const
A Function generated (via FParser) by parsing a mathematical expression.
CPPUNIT_TEST_SUITE_REGISTRATION(ParsedFunctionTest)
static const Real TOLERANCE
The libMesh namespace provides an interface to certain functionality in the library.
bool is_time_dependent() const
void set_inline_value(const std::string &inline_var_name, Output newval)
Changes the value of an inline variable.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38