LCOV - code coverage report
Current view: top level - lepton - ParsedExpression.h (source / functions) Hit Total Coverage
Test: plumed test coverage (other modules) Lines: 1 1 100.0 %
Date: 2024-10-11 08:09:49 Functions: 0 0 -

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :  * -------------------------------------------------------------------------- *
       3             :  *                                   Lepton                                   *
       4             :  * -------------------------------------------------------------------------- *
       5             :  * This is part of the Lepton expression parser originating from              *
       6             :  * Simbios, the NIH National Center for Physics-Based Simulation of           *
       7             :  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
       8             :  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
       9             :  *                                                                            *
      10             :  * Portions copyright (c) 2013-2016 Stanford University and the Authors.      *
      11             :  * Authors: Peter Eastman                                                     *
      12             :  * Contributors:                                                              *
      13             :  *                                                                            *
      14             :  * Permission is hereby granted, free of charge, to any person obtaining a    *
      15             :  * copy of this software and associated documentation files (the "Software"), *
      16             :  * to deal in the Software without restriction, including without limitation  *
      17             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
      18             :  * and/or sell copies of the Software, and to permit persons to whom the      *
      19             :  * Software is furnished to do so, subject to the following conditions:       *
      20             :  *                                                                            *
      21             :  * The above copyright notice and this permission notice shall be included in *
      22             :  * all copies or substantial portions of the Software.                        *
      23             :  *                                                                            *
      24             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
      25             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
      26             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
      27             :  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
      28             :  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
      29             :  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
      30             :  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
      31             :  * -------------------------------------------------------------------------- *
      32             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      33             : #ifndef __PLUMED_lepton_ParsedExpression_h
      34             : #define __PLUMED_lepton_ParsedExpression_h
      35             : 
      36             : /* -------------------------------------------------------------------------- *
      37             :  *                                   lepton                                   *
      38             :  * -------------------------------------------------------------------------- *
      39             :  * This is part of the lepton expression parser originating from              *
      40             :  * Simbios, the NIH National Center for Physics-Based Simulation of           *
      41             :  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
      42             :  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
      43             :  *                                                                            *
      44             :  * Portions copyright (c) 2009=2021 Stanford University and the Authors.      *
      45             :  * Authors: Peter Eastman                                                     *
      46             :  * Contributors:                                                              *
      47             :  *                                                                            *
      48             :  * Permission is hereby granted, free of charge, to any person obtaining a    *
      49             :  * copy of this software and associated documentation files (the "Software"), *
      50             :  * to deal in the Software without restriction, including without limitation  *
      51             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
      52             :  * and/or sell copies of the Software, and to permit persons to whom the      *
      53             :  * Software is furnished to do so, subject to the following conditions:       *
      54             :  *                                                                            *
      55             :  * The above copyright notice and this permission notice shall be included in *
      56             :  * all copies or substantial portions of the Software.                        *
      57             :  *                                                                            *
      58             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
      59             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
      60             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
      61             :  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
      62             :  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
      63             :  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
      64             :  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
      65             :  * -------------------------------------------------------------------------- */
      66             : 
      67             : #include "ExpressionTreeNode.h"
      68             : #include "windowsIncludes.h"
      69             : #include <map>
      70             : #include <string>
      71             : 
      72             : namespace PLMD {
      73             : namespace lepton {
      74             : 
      75             : class CompiledExpression;
      76             : class ExpressionProgram;
      77             : 
      78             : /**
      79             :  * This class represents the result of parsing an expression.  It provides methods for working with the
      80             :  * expression in various ways, such as evaluating it, getting the tree representation of the expresson, etc.
      81             :  */
      82             : 
      83     2011410 : class LEPTON_EXPORT ParsedExpression {
      84             : public:
      85             :     /**
      86             :      * Create an uninitialized ParsedExpression.  This exists so that ParsedExpressions can be put in STL containers.
      87             :      * Doing anything with it will produce an exception.
      88             :      */
      89             :     ParsedExpression();
      90             :     /**
      91             :      * Create a ParsedExpression.  Normally you will not call this directly.  Instead, use the Parser class
      92             :      * to parse expression.
      93             :      */
      94             :     ParsedExpression(const ExpressionTreeNode& rootNode);
      95             :     /**
      96             :      * Get the root node of the expression's abstract syntax tree.
      97             :      */
      98             :     const ExpressionTreeNode& getRootNode() const;
      99             :     /**
     100             :      * Evaluate the expression.  If the expression involves any variables, this method will throw an exception.
     101             :      */
     102             :     double evaluate() const;
     103             :     /**
     104             :      * Evaluate the expression.
     105             :      *
     106             :      * @param variables    a map specifying the values of all variables that appear in the expression.  If any
     107             :      *                     variable appears in the expression but is not included in this map, an exception
     108             :      *                     will be thrown.
     109             :      */
     110             :     double evaluate(const std::map<std::string, double>& variables) const;
     111             :     /**
     112             :      * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
     113             :      */
     114             :     ParsedExpression optimize() const;
     115             :     /**
     116             :      * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
     117             :      *
     118             :      * @param variables    a map specifying values for a subset of variables that appear in the expression.
     119             :      *                     All occurrences of these variables in the expression are replaced with the values
     120             :      *                     specified.
     121             :      */
     122             :     ParsedExpression optimize(const std::map<std::string, double>& variables) const;
     123             :     /**
     124             :      * Create a new ParsedExpression which is the analytic derivative of this expression with respect to a
     125             :      * particular variable.
     126             :      *
     127             :      * @param variable     the variable with respect to which the derivate should be taken
     128             :      */
     129             :     ParsedExpression differentiate(const std::string& variable) const;
     130             :     /**
     131             :      * Create an ExpressionProgram that represents the same calculation as this expression.
     132             :      */
     133             :     ExpressionProgram createProgram() const;
     134             :     /**
     135             :      * Create a CompiledExpression that represents the same calculation as this expression.
     136             :      */
     137             :     CompiledExpression createCompiledExpression() const;
     138             :     /**
     139             :      * Create a new ParsedExpression which is identical to this one, except that the names of some
     140             :      * variables have been changed.
     141             :      *
     142             :      * @param replacements    a map whose keys are the names of variables, and whose values are the
     143             :      *                        new names to replace them with
     144             :      */
     145             :     ParsedExpression renameVariables(const std::map<std::string, std::string>& replacements) const;
     146             : private:
     147             :     static double evaluate(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
     148             :     static ExpressionTreeNode preevaluateVariables(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
     149             :     static ExpressionTreeNode precalculateConstantSubexpressions(const ExpressionTreeNode& node, std::map<int, ExpressionTreeNode>& nodeCache);
     150             :     static ExpressionTreeNode substituteSimplerExpression(const ExpressionTreeNode& node, std::map<int, ExpressionTreeNode>& nodeCache);
     151             :     static ExpressionTreeNode differentiate(const ExpressionTreeNode& node, const std::string& variable, std::map<int, ExpressionTreeNode>& nodeCache);
     152             :     static bool isConstant(const ExpressionTreeNode& node);
     153             :     static double getConstantValue(const ExpressionTreeNode& node);
     154             :     static ExpressionTreeNode renameNodeVariables(const ExpressionTreeNode& node, const std::map<std::string, std::string>& replacements);
     155             :     ExpressionTreeNode rootNode;
     156             : };
     157             : 
     158             : LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ExpressionTreeNode& node);
     159             : 
     160             : LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ParsedExpression& exp);
     161             : 
     162             : } // namespace lepton
     163             : } // namespace PLMD
     164             : 
     165             : #endif /*LEPTON_PARSED_EXPRESSION_H_*/

Generated by: LCOV version 1.15