MADNESS 0.10.1
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#ifndef MADNESS_MISC_PHANDLER_H__INCLUDED
33#define MADNESS_MISC_PHANDLER_H__INCLUDED
34
35/// \file misc/phandler.h
36/// \brief Interface for the muParser library for turning user-defined functions into bytecode.
37
38/* Example:
39
40 #include <madness/misc/phandler.h>
41
42 typedef FunctionFactory<double,3> factoryT;
43 typedef std::shared_ptr< FunctionFunctorInterface<double, 3> > functorT;
44 Function<double,3> pFunc = factoryT(world).functor(functorT(
45 new ParserHandler<double,3>("exp(-abs(r))")));
46
47 pfunc.compress();
48
49 ...
50*/
51
52#include <madness/external/muParser/muParser.h>
53#include <string>
54#include <madness/mra/mra.h>
55
56// T can be double or complex, but muParser results will always be real
57// NDIM can be 1 to 6
58// Varables allowed in strings are x,y,z,u,v,w, and r
59// "r" will always mean magnitude of given vector
60
61template <typename T, int NDIM>
63
64 private:
65 mu::Parser parser; // the string-to-function parser
66 static const int MAX_DIM = 6;
67 mutable double vars[MAX_DIM]; // variables used in expression
68 mutable double r; // distance to origin
70
71 public:
72 ParserHandler(std::string expr) {
73 if (NDIM > MAX_DIM) MADNESS_EXCEPTION("too many dim for parser!",0);
74 try {
75 if (NDIM >= 1) parser.DefineVar("x", &vars[0]);
76 if (NDIM >= 2) parser.DefineVar("y", &vars[1]);
77 if (NDIM >= 3) parser.DefineVar("z", &vars[2]);
78 if (NDIM >= 4) parser.DefineVar("u", &vars[3]);
79 if (NDIM >= 5) parser.DefineVar("v", &vars[4]);
80 if (NDIM >= 6) parser.DefineVar("w", &vars[5]);
81 parser.DefineVar("r", &r);
82 parser.SetExpr(expr);
83 } catch (mu::Parser::exception_type &e) {
84 std::cout << "muParser: " << e.GetMsg() << std::endl;
85 }
86 }
87
88 virtual T operator() (const coordT &vals_in) const {
89 r = 0;
90 for (int i = 0; i < NDIM; ++i) {
91 vars[i] = vals_in[i];
92 r += vals_in[i]*vals_in[i];
93 }
94 r = sqrt(r);
95
96 try {
97 return parser.Eval();
98 } catch (mu::Parser::exception_type &e) {
99 std::cout << "muParser: " << e.GetMsg() << std::endl;
100 return 0.0;
101 }
102 } // end operator()
103
104 void changeExpr(const std::string newExpr) {
105 try {
106 parser.SetExpr(newExpr);
107 } catch (mu::Parser::exception_type &e) {
108 std::cout << "muParser: " << e.GetMsg() << std::endl;
109 }
110 }
111}; // end class parserhandler
112
113#endif // MADNESS_MISC_PHANDLER_H__INCLUDED
Definition phandler.h:62
double r
Definition phandler.h:68
ParserHandler(std::string expr)
Definition phandler.h:72
double vars[MAX_DIM]
Definition phandler.h:67
mu::Parser parser
Definition phandler.h:65
static const int MAX_DIM
Definition phandler.h:66
void changeExpr(const std::string newExpr)
Definition phandler.h:104
madness::Vector< double, NDIM > coordT
Definition phandler.h:69
virtual T operator()(const coordT &vals_in) const
You should implement this to return f(x)
Definition phandler.h:88
Abstract base class interface required for functors used as input to Functions.
Definition function_interface.h:68
A simple, fixed dimension vector.
Definition vector.h:64
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Main include file for MADNESS and defines Function interface.
void e()
Definition test_sig.cc:75
static const std::size_t NDIM
Definition testpdiff.cc:42