LCOV - code coverage report
Current view: top level - lepton - ExpressionProgram.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage (other modules) Lines: 0 52 0.0 %
Date: 2024-10-18 14:00:27 Functions: 0 12 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             : /* -------------------------------------------------------------------------- *
      34             :  *                                   lepton                                   *
      35             :  * -------------------------------------------------------------------------- *
      36             :  * This is part of the lepton expression parser originating from              *
      37             :  * Simbios, the NIH National Center for Physics-Based Simulation of           *
      38             :  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
      39             :  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
      40             :  *                                                                            *
      41             :  * Portions copyright (c) 2009-2018 Stanford University and the Authors.      *
      42             :  * Authors: Peter Eastman                                                     *
      43             :  * Contributors:                                                              *
      44             :  *                                                                            *
      45             :  * Permission is hereby granted, free of charge, to any person obtaining a    *
      46             :  * copy of this software and associated documentation files (the "Software"), *
      47             :  * to deal in the Software without restriction, including without limitation  *
      48             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
      49             :  * and/or sell copies of the Software, and to permit persons to whom the      *
      50             :  * Software is furnished to do so, subject to the following conditions:       *
      51             :  *                                                                            *
      52             :  * The above copyright notice and this permission notice shall be included in *
      53             :  * all copies or substantial portions of the Software.                        *
      54             :  *                                                                            *
      55             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
      56             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
      57             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
      58             :  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
      59             :  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
      60             :  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
      61             :  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
      62             :  * -------------------------------------------------------------------------- */
      63             : 
      64             : #include "ExpressionProgram.h"
      65             : #include "Operation.h"
      66             : #include "ParsedExpression.h"
      67             : 
      68             : namespace PLMD {
      69             : using namespace lepton;
      70             : using namespace std;
      71             : 
      72           0 : ExpressionProgram::ExpressionProgram() : maxArgs(0), stackSize(0) {
      73           0 : }
      74             : 
      75           0 : ExpressionProgram::ExpressionProgram(const ParsedExpression& expression) : maxArgs(0), stackSize(0) {
      76           0 :     buildProgram(expression.getRootNode());
      77             :     int currentStackSize = 0;
      78           0 :     for (int i = 0; i < (int) operations.size(); i++) {
      79           0 :         int args = operations[i]->getNumArguments();
      80           0 :         if (args > maxArgs)
      81           0 :             maxArgs = args;
      82           0 :         currentStackSize += 1-args;
      83           0 :         if (currentStackSize > stackSize)
      84           0 :             stackSize = currentStackSize;
      85             :     }
      86           0 : }
      87             : 
      88           0 : ExpressionProgram::~ExpressionProgram() {
      89           0 :     for (int i = 0; i < (int) operations.size(); i++)
      90           0 :         delete operations[i];
      91           0 : }
      92             : 
      93           0 : ExpressionProgram::ExpressionProgram(const ExpressionProgram& program) {
      94           0 :     *this = program;
      95           0 : }
      96             : 
      97           0 : ExpressionProgram& ExpressionProgram::operator=(const ExpressionProgram& program) {
      98           0 :     maxArgs = program.maxArgs;
      99           0 :     stackSize = program.stackSize;
     100           0 :     operations.resize(program.operations.size());
     101           0 :     for (int i = 0; i < (int) operations.size(); i++)
     102           0 :         operations[i] = program.operations[i]->clone();
     103           0 :     return *this;
     104             : }
     105             : 
     106           0 : void ExpressionProgram::buildProgram(const ExpressionTreeNode& node) {
     107           0 :     for (int i = (int) node.getChildren().size()-1; i >= 0; i--)
     108           0 :         buildProgram(node.getChildren()[i]);
     109           0 :     operations.push_back(node.getOperation().clone());
     110           0 : }
     111             : 
     112           0 : int ExpressionProgram::getNumOperations() const {
     113           0 :     return (int) operations.size();
     114             : }
     115             : 
     116           0 : const Operation& ExpressionProgram::getOperation(int index) const {
     117           0 :     return *operations[index];
     118             : }
     119             : 
     120           0 : void ExpressionProgram::setOperation(int index, Operation* operation) {
     121           0 :     delete operations[index];
     122           0 :     operations[index] = operation;
     123           0 : }
     124             : 
     125           0 : int ExpressionProgram::getStackSize() const {
     126           0 :     return stackSize;
     127             : }
     128             : 
     129           0 : double ExpressionProgram::evaluate() const {
     130           0 :     return evaluate(map<string, double>());
     131             : }
     132             : 
     133           0 : double ExpressionProgram::evaluate(const std::map<std::string, double>& variables) const {
     134           0 :     vector<double> stack(stackSize+1);
     135           0 :     int stackPointer = stackSize;
     136           0 :     for (int i = 0; i < (int) operations.size(); i++) {
     137           0 :         int numArgs = operations[i]->getNumArguments();
     138           0 :         double result = operations[i]->evaluate(&stack[stackPointer], variables);
     139           0 :         stackPointer += numArgs-1;
     140           0 :         stack[stackPointer] = result;
     141             :     }
     142           0 :     return stack[stackSize-1];
     143             : }
     144             : }

Generated by: LCOV version 1.16