atomicrex  0.1
An advanced atomistic model building tool
ParsedFunction.h
1 //
3 // Copyright (C) 2017, Alexander Stukowski and Paul Erhart
4 //
5 // This file is part of atomicrex.
6 //
7 // Atomicrex is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Atomicrex is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #pragma once
23 
24 #include "Functions.h"
25 
26 #include <muParser.h>
27 #include <boost/thread/tss.hpp>
28 
29 namespace atomicrex {
30 
31 /************************************************************
32  * A scalar function object that evaluates a user-defined math
33  * expression.
34  *
35  * In addition to the scalar input parameter, the math expression
36  * can include arbitrary parameters, which are exposed as
37  * degrees of freedom by the function class.
38  ************************************************************/
40 {
41 public:
42 
44  ParsedFunction(const FPString& id, FitJob* job) : FunctionBase(id, job, "user-function") {}
45 
47  virtual void parse(XML::Element functionElement) override;
48 
50  virtual void dofValueChanged(DegreeOfFreedom& dof) override;
51 
52 protected:
53 
55  double evaluateInternal(double x) override;
56 
58  double evaluateInternal(double x, double& deriv) override;
59 
60 private:
61 
64  struct ThreadData {
66  double inputValue;
68  mu::Parser parser;
70  mu::Parser derivativeParser;
72  unsigned int parameterSet = 0;
73  };
74 
76  ThreadData* initializeEvaluator();
77 
80  unsigned int _parameterSet = 1;
81 
83  std::vector<ScalarDOF> _params;
84 
86  FPString _expression;
87 
89  FPString _expressionDerivative;
90 
92  FPString _inputVarName;
93 };
94 
95 } // End of namespace
virtual void dofValueChanged(DegreeOfFreedom &dof) override
This function is called by the DOFs of this object each time their value changes. ...
Definition: ParsedFunction.cpp:79
ParsedFunction(const FPString &id, FitJob *job)
Constructor.
Definition: ParsedFunction.h:44
virtual void parse(XML::Element functionElement) override
Parses the XML definition of the object.
Definition: ParsedFunction.cpp:34
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
Definition: ParsedFunction.h:39
double evaluateInternal(double x) override
Evaluates the function at a position x.
Definition: ParsedFunction.cpp:132
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
Definition: XMLUtilities.h:69
Definition: FitJob.h:37
FitJob * job() const
Returns a pointer to the job to which this object belongs.
Definition: FitObject.h:150
Base class for all degrees of freedom a potential or a structure may have.
Definition: DegreeOfFreedom.h:46
Base class for all one-dimensional functions that can be fitted.
Definition: Functions.h:35