LifeV
lifev/core/testsuite/parser/main.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  * @file
29  * @brief File containing the parser test
30  *
31  * @date 22-04-2009
32  * @author Cristiano Malossi <cristiano.malossi@epfl.ch>
33  *
34  * @maintainer Cristiano Malossi <cristiano.malossi@epfl.ch>
35  *
36  * This is a test to verify that the parser performs correct computations.
37  * Note that the parser works only with boost v1.41+.
38  */
39 
40 
41 #include <iomanip>
42 #include <string>
43 
44 #include <Epetra_ConfigDefs.h>
45 #ifdef EPETRA_MPI
46 #include <mpi.h>
47 #endif
48 
49 
50 // LifeV includes
51 #include <lifev/core/LifeV.hpp>
52 #include <lifev/core/util/StringUtility.hpp>
53 #include <lifev/core/util/LifeChrono.hpp>
54 
55 #include <lifev/core/util/Parser.hpp>
56 
57 using namespace LifeV;
58 
59 bool EXIT_FLAG = EXIT_SUCCESS;
60 std::string success = "OK ";
61 std::string failed = "FAILED ";
62 
63 inline std::string
64 check ( const bool& expression )
65 {
66  if ( expression )
67  {
68  EXIT_FLAG = EXIT_FAILURE;
69  return failed;
70  }
71 
72  return success;
73 }
74 
75 Int
76 main ( Int argc, char** argv )
77 {
78 #ifdef HAVE_MPI
79  std::cout << "MPI Initialization" << std::endl;
80  MPI_Init ( &argc, &argv );
81 #endif
82 
83  std::string expression;
84  Real result;
85  Real tolerance = 1e-15;
86 
87  std::cout << std::setprecision (30) << std::endl;
88 
89  // Initialization of the parser
90  Parser parser;
91 
92  std::cout << "READY TO TEST WITH 10+ EXPRESSIONS:" << std::endl << std::endl;
93 
94  // TEST 0:
95  expression = "-sqrt(4)+1*2"; // = 0
96  parser.setString (expression);
97  result = parser.evaluate();
98  std::cout << "TEST 0: " << check ( std::fabs (result - 0) > tolerance )
99  << expression << " = " << result << std::endl;
100 
101  // TEST 1:
102  expression = "(1+1)/(2+2)"; // = 0.5
103  parser.setString (expression);
104  result = parser.evaluate();
105 
106  std::cout << "TEST 1: " << check ( std::abs (result - 0.5) > tolerance )
107  << expression << " = " << result << std::endl;
108 
109  // TEST 2:
110  expression = "1-2-3+4*5-6-7+8*9+1"; // = 76
111  parser.setString (expression);
112  result = parser.evaluate();
113  std::cout << "TEST 2: " << check ( std::abs (result - 76) > tolerance )
114  << expression << " = " << result << std::endl;;
115 
116  // TEST 3:
117  expression = "-(1+1)+(250+250+(-2))"; // = 496
118  parser.setString (expression);
119  result = parser.evaluate();
120  std::cout << "TEST 3: " << check ( std::abs (result - 496) > tolerance )
121  << expression << " = " << result << std::endl;
122 
123  // TEST 4:
124  expression = "(0.8 > 0.9)"; // = 0
125  parser.setString (expression);
126  result = parser.evaluate();
127  std::cout << "TEST 4: " << check ( std::abs (result - 0) > tolerance )
128  << expression << " = " << result << std::endl;
129 
130  // TEST 5:
131  expression = "(0.800000000000000 <= 0.8)"; // = 1
132  parser.setString (expression);
133  result = parser.evaluate();
134  std::cout << "TEST 5: " << check ( std::abs (result - 1) > tolerance )
135  << expression << " = " << result << std::endl;
136 
137  // TEST 6:
138  expression = "sin(3/4*pi) * -sin(3/4*pi) + -(cos(3/4*pi))^2"; // = -1
139  parser.setString (expression);
140  result = parser.evaluate();
141  std::cout << "TEST 6: " << check ( std::abs (result - -1) > tolerance )
142  << expression << " = " << result << std::endl;
143 
144  // TEST 7:
145  expression = "144^0.5 * sqrt(144)"; // = 144
146  parser.setString (expression);
147  result = parser.evaluate();
148  std::cout << "TEST 7: " << check ( std::abs (result - 144) > tolerance )
149  << expression << " = " << result << std::endl;
150 
151  std::cout << std::endl << "TEST ENDS SUCCESFULLY" << std::endl;
152 
153  // TEST 8:
154 
155  expression = "abc = -2^3^-3; abc"; // = -0.001953125
156  parser.setString (expression);
157  result = parser.evaluate();
158  std::cout << "TEST 8: " << check ( std::abs (result - -0.001953125) > tolerance )
159  << expression << " = " << result << std::endl;
160 
161  // TEST 9:
162 
163  expression = "c=2; [0., c, c*c, c*c*c]"; // (0, 2, 4, 8)
164  parser.setString (expression);
165  std::cout << "TEST 9: " << check ( std::abs ( parser.evaluate (0) - 0 ) > tolerance
166  || std::abs ( parser.evaluate (1) - 2 ) > tolerance
167  || std::abs ( parser.evaluate (2) - 4 ) > tolerance
168  || std::abs ( parser.evaluate (3) - 8 ) > tolerance )
169  << expression << " = [" << parser.evaluate (0) << ", "
170  << parser.evaluate (1) << ", "
171  << parser.evaluate (2) << ", "
172  << parser.evaluate (3) << "]" << std::endl;
173 
174  // TEST 10:
175 
176  expression = "[0, 0, -(x^2)+y^2]";
177  parser.setString (expression);
178  parser.setVariable ("x", 1);
179  parser.setVariable ("y", 2); // (0, 0, -5)
180  std::cout << "TEST 10a: " << check ( std::abs ( parser.evaluate (0) - 0 ) > tolerance ||
181  std::abs ( parser.evaluate (1) - 0 ) > tolerance ||
182  std::abs ( parser.evaluate (2) - 3 ) > tolerance )
183  << "x = " << 1 << ", y = " << 2 << " ==> "
184  << expression << " = [" << parser.evaluate (0) << ", "
185  << parser.evaluate (1) << ", "
186  << parser.evaluate (2) << "]" << std::endl;
187 
188  parser.setString (expression);
189  parser.setVariable ("x", 4);
190  parser.setVariable ("y", 5); // (0, 0, -41)
191  std::cout << "TEST 10b: " << check ( std::abs ( parser.evaluate (0) - 0 ) > tolerance ||
192  std::abs ( parser.evaluate (1) - 0 ) > tolerance ||
193  std::abs ( parser.evaluate (2) - 9 ) > tolerance )
194  << "x = " << 4 << ", y = " << 5 << " ==> "
195  << expression << " = [" << parser.evaluate (0) << ", "
196  << parser.evaluate (1) << ", "
197  << parser.evaluate (2) << "]" << std::endl;
198 
199 
200  // PERFORMANCE TEST
201 // LifeChrono chronoParser;
202 // LifeChrono chronoReference;
203 //
204 // expression = "sqrt(((index+_pi)*2)^3)+sin(3/4*_pi)"; //We test ONE complex expression containing different operations
205 // parser.setString(expression);
206 //
207 // UInt nEvaluations = 1000000; // 1 Million
208 // Real solution;
209 //
210 // chronoParser.start();
211 // for (UInt i = 0 ; i < nEvaluations ; ++i)
212 // {
213 // parser.setVariable("index", i);
214 // solution = parser.evaluate();
215 // }
216 // chronoParser.stop();
217 //
218 // chronoReference.start();
219 // for (UInt i = 0 ; i < nEvaluations ; ++i)
220 // {
221 // solution = std::sqrt( std::pow( ( i + M_PI )*2, 3 ) );
222 // }
223 // chronoReference.stop();
224 //
225 // std::cout << std::endl << "Total time for " << nEvaluations << " evaluations of expression f=" << expression << " --> " << chronoParser.diff() << " s" << std::endl;
226 // std::cout << std::endl << "Reference time " << chronoReference.diff() << " s" << std::endl;
227 
228 #ifdef HAVE_MPI
229  std::cout << std::endl << "MPI Finalization" << std::endl;
230  MPI_Finalize();
231 #endif
232 
233  return ( EXIT_FLAG );
234 }
std::string success
std::string check(const bool &expression)
std::string failed
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void updateInverseJacobian(const UInt &iQuadPt)
Parser - A string parser for algebraic expressions.
Definition: Parser.hpp:77
int main(int argc, char **argv)
Definition: dummy.cpp:5
double Real
Generic real data.
Definition: LifeV.hpp:175
const Real & evaluate(const ID &id=0)
Evaluate the expression.
Definition: Parser.cpp:107