MADNESS  version 0.9
phandler.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32 */
33 
34 #ifndef MADNESS_MISC_PHANDLER_H__INCLUDED
35 #define MADNESS_MISC_PHANDLER_H__INCLUDED
36 
39 
40 /* Example:
41 
42  #include <madness/misc/phandler.h>
43 
44  typedef FunctionFactory<double,3> factoryT;
45  typedef std::shared_ptr< FunctionFunctorInterface<double, 3> > functorT;
46  Function<double,3> pFunc = factoryT(world).functor(functorT(
47  new ParserHandler<double,3>("exp(-abs(r))")));
48 
49  pfunc.compress();
50 
51  ...
52 */
53 
55 #include <string>
56 #include <madness/mra/mra.h>
57 
58 //using namespace madness;
59 //using namespace mu;
60 
61 // T can be double or complex, but muParser results will always be real
62 // NDIM can be 1 to 6
63 // Varables allowed in strings are x,y,z,u,v,w, and r
64 // "r" will always mean magnitude of given vector
65 
66 template <typename T, int NDIM>
68 
69  private:
70  mu::Parser parser; // the string-to-function parser
71  static const int MAX_DIM = 6;
72  mutable double vars[MAX_DIM]; // variables used in expression
73  mutable double r; // distance to origin
75 
76  public:
78  if (NDIM > MAX_DIM) MADNESS_EXCEPTION("too many dim for parser!",0);
79  try {
80  if (NDIM >= 1) parser.DefineVar("x", &vars[0]);
81  if (NDIM >= 2) parser.DefineVar("y", &vars[1]);
82  if (NDIM >= 3) parser.DefineVar("z", &vars[2]);
83  if (NDIM >= 4) parser.DefineVar("u", &vars[3]);
84  if (NDIM >= 5) parser.DefineVar("v", &vars[4]);
85  if (NDIM >= 6) parser.DefineVar("w", &vars[5]);
86  parser.DefineVar("r", &r);
87  parser.SetExpr(expr);
88  } catch (mu::Parser::exception_type &e) {
89  std::cout << "muParser: " << e.GetMsg() << std::endl;
90  }
91  }
92 
93  virtual T operator() (const coordT &vals_in) const {
94  r = 0;
95  for (int i = 0; i < NDIM; ++i) {
96  vars[i] = vals_in[i];
97  r += vals_in[i]*vals_in[i];
98  }
99  r = sqrt(r);
100 
101  try {
102  return parser.Eval();
103  } catch (mu::Parser::exception_type &e) {
104  std::cout << "muParser: " << e.GetMsg() << std::endl;
105  return 0.0;
106  }
107  } // end operator()
108 
109  void changeExpr(const std::string newExpr) {
110  try {
111  parser.SetExpr(newExpr);
112  } catch (mu::Parser::exception_type &e) {
113  std::cout << "muParser: " << e.GetMsg() << std::endl;
114  }
115  }
116 }; // end class parserhandler
117 
118 #endif // MADNESS_MISC_PHANDLER_H__INCLUDED
Main include file for MADNESS and defines Function interface.
void DefineVar(const string_type &a_sName, value_type *a_fVar)
Add a user defined variable.
Definition: muParserBase.cpp:441
const int NDIM
Definition: tdse1.cc:44
::std::string string
Definition: gtest-port.h:872
ParserHandler(std::string expr)
Definition: phandler.h:77
virtual T operator()(const coordT &vals_in) const
You should implement this to return f(x)
Definition: phandler.h:93
Definition of the standard floating point parser.
value_type Eval() const
Calculate the result.
Definition: muParserBase.h:116
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
Error class of the parser.
Definition: muParserError.h:119
void SetExpr(const string_type &a_sExpr)
Set the formula.
Definition: muParserBase.cpp:261
tensorT sqrt(const tensorT &s, double tol=1e-8)
Computes matrix square root (not used any more?)
Definition: DFcode/moldft.cc:446
void changeExpr(const std::string newExpr)
Definition: phandler.h:109
Abstract base class interface required for functors used as input to Functions.
Definition: function_interface.h:58
Mathematical expressions parser.
Definition: muParser.h:52
Definition: phandler.h:67
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
const string_type & GetMsg() const
Returns the message string for this error.
Definition: muParserError.cpp:299