COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-math-integrate.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE
9 
10 #include <iomanip>
11 
12 #include <boost/test/unit_test.hpp>
13 #include <boost/tokenizer.hpp>
14 #include <boost/algorithm/string.hpp>
15 #include "fparser/fparser.hh"
16 
17 #include "common/Log.hpp"
18 
19 #include "math/Integrate.hpp"
20 #include "math/Consts.hpp"
21 
22 using namespace std;
23 using namespace boost;
24 
25 using namespace cf3;
26 using namespace cf3::common;
27 using namespace cf3::math;
28 
29 struct f
30 {
31  Real operator()(Real x) const
32  {
33  return std::sin(x);
34  }
35 };
36 
37 BOOST_AUTO_TEST_SUITE( math_integrate_test_suite )
38 
39 BOOST_AUTO_TEST_CASE( test_integrate )
40 {
41  Integrate integrate;
42  Real result = integrate(f(), 0., 1.);
43  CFinfo << "result = " << result << CFendl;
44  CFinfo << "nb_intervals = " << integrate.nb_intervals << CFendl;
45  CFinfo << "nb_evaluations = " << integrate.nb_kernel_evaluations << CFendl;
46  CFinfo << "error_estimate = " << integrate.error_estimate << CFendl;
47 }
48 
49 
51 
52 BOOST_AUTO_TEST_SUITE_END()
53 
54 
55 #if 0
56 class fparser_integral:
57  public FunctionParser::FunctionWrapper
58 {
59  public:
60 
61  const std::string& name() { return m_name; }
62 
63  fparser_integral(const std::string& name, const std::string& integrand, const std::string& vars) : m_name(name), m_integrand(integrand,vars), integrate() {}
64 
65  struct WrappedIntegrand
66  {
67  WrappedIntegrand(const std::string& integrand, const std::string& vars)
68  {
69  std::vector<std::string> deduced_vars;
70  func.ParseAndDeduceVariables(integrand,deduced_vars);
71 
72  std::cout << "deduced = ";
73  boost_foreach(std::string& deduced_var, deduced_vars)
74  std::cout << deduced_var << " ";
75  std::cout << std::endl;
76 
77  std::stringstream parse_vars;
78  std::vector<std::string> passed_vars; passed_vars.reserve(deduced_vars.size());
79  boost::char_separator<char> sep(",");
80  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
81  tokenizer tok (vars,sep);
82  Uint idx = 0;
83  for(tokenizer::iterator it=tok.begin(); it!=tok.end(); ++it)
84  {
85  if(idx>0) parse_vars << ",";
86  parse_vars << *it;
87 
88  passed_vars.push_back(*it);
89  var_idx[*it] = idx++;
90  }
91 
92  std::vector<std::string> unknown_vars; unknown_vars.reserve(2);
93 
94  boost_foreach(const std::string& deduced_var, deduced_vars)
95  {
96  bool found = false;
97  boost_foreach(const std::string& passed_var, passed_vars)
98  {
99  if ( equals( passed_var, deduced_var) )
100  found = true;
101  }
102  if (found == false)
103  unknown_vars.push_back(deduced_var);
104  }
105 
106  std::cout << "unknown = ";
107  boost_foreach(const std::string& unknown, unknown_vars)
108  std::cout << unknown << " ";
109  std::cout<< std::endl;
110 
111  if(unknown_vars.size() != 2)
112  throw(ParsingFailed(FromHere(),""));
113 
114  std::string* dummy_var;
115  std::string* integral_end;
116  if (unknown_vars[0].size() < unknown_vars[1].size())
117  {
118  if(idx>0) parse_vars << ",";
119  parse_vars << unknown_vars[0];
120  var_idx[unknown_vars[0]] = idx++;
121 
122  if(idx>0) parse_vars << ",";
123  parse_vars << unknown_vars[1];
124  var_idx[unknown_vars[1]] = idx++;
125  }
126  else
127  {
128  if(idx>0) parse_vars << ",";
129  parse_vars << unknown_vars[1];
130  var_idx[unknown_vars[1]] = idx++;
131 
132  if(idx>0) parse_vars << ",";
133  parse_vars << unknown_vars[0];
134  var_idx[unknown_vars[0]] = idx++;
135  }
136 
137  variables.resize(var_idx.size());
138  variables[variables.size()-1] = 1.;
139 
140  std::cout << "parse_vars = " << parse_vars.str() << std::endl;
141  func.Parse(integrand, parse_vars.str());
142  }
143 
144  Real operator()(Real x) const
145  {
146  variables[var_idx["a"]]=1.;
147  variables[variables.size()-2] = x;
148  Real result = func.Eval(&variables[0]);
149  return result;
150  }
151 
152  mutable FunctionParser func;
153  mutable std::vector<Real> variables;
154  mutable std::map<std::string,Uint> var_idx;
155  };
156 
157  virtual double callFunction(const double* values)
158  {
159  // Perform the actual function call here, like:
160  return integrate(m_integrand,values[0],values[1]);
161  }
162 
163 private:
164  std::string m_name;
165  Integrate integrate;
166  WrappedIntegrand m_integrand;
167 };
168 
170 
171 BOOST_AUTO_TEST_SUITE( math_integrate_fparser_test_suite )
172 
173 BOOST_AUTO_TEST_CASE( test_integrate_fparser )
174 {
175  Integrate integrate;
176  Real result = integrate(f(), 0., 1.);
177  CFinfo << "result = " << result << CFendl;
178  CFinfo << "nb_intervals = " << integrate.nb_intervals << CFendl;
179  CFinfo << "nb_evaluations = " << integrate.nb_kernel_evaluations << CFendl;
180  CFinfo << "error_estimate = " << integrate.error_estimate << CFendl;
181 
182  FunctionParser fparser;
183 
184  fparser_integral my_integral("my_integral","sin(a*x)*dx","a");
185  fparser.AddFunctionWrapper(my_integral.name(), my_integral, 2);
186 
187  fparser.Parse("my_integral(0,1)","a");
188 
189  Real variables[1] = {1};
190  Real parsed_result = fparser.Eval(variables);
191 
192  BOOST_CHECK_EQUAL(parsed_result,result);
193 
194 // fparser.Parse("integral(sin(int_x),int_min,int_max)","xmin","xmax","x");
195 
196 }
197 
199 
200 BOOST_AUTO_TEST_SUITE_END()
201 #endif
202 
#define CFinfo
these are always defined
Definition: Log.hpp:104
std::string name(ComponentWrapper &self)
external boost library namespace
Basic Classes for Mathematical applications used by COOLFluiD.
STL namespace.
#define boost_foreach
lowercase version of BOOST_FOREACH
Definition: Foreach.hpp:16
#define CFendl
Definition: Log.hpp:109
Real operator()(Real x) const
BOOST_AUTO_TEST_CASE(test_integrate)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Most basic kernel library.
Definition: Action.cpp:19
#define FromHere()
Send comments to:
COOLFluiD Web Admin