You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompiledExpression.cpp 5.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* -------------------------------------------------------------------------- *
  2. * Lepton *
  3. * -------------------------------------------------------------------------- *
  4. * This is part of the Lepton expression parser originating from *
  5. * Simbios, the NIH National Center for Physics-Based Simulation of *
  6. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  7. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  8. * *
  9. * Portions copyright (c) 2013 Stanford University and the Authors. *
  10. * Authors: Peter Eastman *
  11. * Contributors: *
  12. * *
  13. * Permission is hereby granted, free of charge, to any person obtaining a *
  14. * copy of this software and associated documentation files (the "Software"), *
  15. * to deal in the Software without restriction, including without limitation *
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  17. * and/or sell copies of the Software, and to permit persons to whom the *
  18. * Software is furnished to do so, subject to the following conditions: *
  19. * *
  20. * The above copyright notice and this permission notice shall be included in *
  21. * all copies or substantial portions of the Software. *
  22. * *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  26. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  27. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  28. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  29. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  30. * -------------------------------------------------------------------------- */
  31. #include "CompiledExpression.h"
  32. #include "Operation.h"
  33. #include "ParsedExpression.h"
  34. #include <utility>
  35. #include <cstdint>
  36. using namespace Lepton;
  37. CompiledExpression::CompiledExpression(const ParsedExpression& expression)
  38. {
  39. ParsedExpression expr = expression.optimize(); // Just in case it wasn't already optimized.
  40. std::vector<std::pair<ExpressionTreeNode, int>> temps;
  41. compileExpression(expr.getRootNode(), temps);
  42. }
  43. CompiledExpression& CompiledExpression::operator=(const CompiledExpression& expression)
  44. {
  45. arguments = expression.arguments;
  46. target = expression.target;
  47. variableIndices = expression.variableIndices;
  48. variableNames = expression.variableNames;
  49. workspace.resize(expression.workspace.size());
  50. argValues.resize(expression.argValues.size());
  51. operation.resize(expression.operation.size());
  52. for (size_t i = 0; i < operation.size(); ++i) { operation[i] = expression.operation[i]->clone(); }
  53. return *this;
  54. }
  55. void CompiledExpression::compileExpression(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int>>& temps)
  56. {
  57. if (findTempIndex(node, temps) != -1) { return; } // We have already processed a node identical to this one.
  58. // Process the child nodes.
  59. std::vector<int> args;
  60. for (size_t i = 0; i < node.getChildren().size(); ++i)
  61. {
  62. compileExpression(node.getChildren()[i], temps);
  63. args.push_back(findTempIndex(node.getChildren()[i], temps));
  64. }
  65. // Process this node.
  66. if (node.getOperation().getId() == Operation::VARIABLE)
  67. {
  68. variableIndices[node.getOperation().getName()] = int(workspace.size());
  69. variableNames.insert(node.getOperation().getName());
  70. }
  71. else
  72. {
  73. int stepIndex = int(arguments.size());
  74. arguments.push_back(std::vector<int>());
  75. target.push_back(int(workspace.size()));
  76. operation.push_back(node.getOperation().clone());
  77. if (args.size() == 0) arguments[stepIndex].push_back(0); // The value won't actually be used. We just need something there.
  78. else
  79. {
  80. // If the arguments are sequential, we can just pass a pointer to the first one.
  81. bool sequential = true;
  82. for (size_t i = 1; i < args.size(); ++i) if (args[i] != args[i - 1] + 1) sequential = false;
  83. if (sequential) arguments[stepIndex].push_back(args[0]);
  84. else
  85. {
  86. arguments[stepIndex] = args;
  87. if (args.size() > argValues.size()) argValues.resize(args.size(), 0.0);
  88. }
  89. }
  90. }
  91. temps.push_back(std::make_pair(node, workspace.size()));
  92. workspace.push_back(0.0);
  93. }
  94. int CompiledExpression::findTempIndex(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int>>& temps)
  95. {
  96. for (size_t i = 0; i < temps.size(); ++i) { if (temps[i].first == node) { return int(i); } }
  97. return -1;
  98. }
  99. double& CompiledExpression::getVariableReference(const std::string& name)
  100. {
  101. auto index = variableIndices.find(name);
  102. if (index == variableIndices.end()) throw Exception("getVariableReference: Unknown variable '" + name + "'");
  103. return workspace[index->second];
  104. }
  105. double CompiledExpression::evaluate() const
  106. {
  107. // Loop over the operations and evaluate each one.
  108. for (size_t step = 0; step < operation.size(); ++step)
  109. {
  110. const std::vector<int>& args = arguments[step];
  111. if (args.size() == 1) workspace[target[step]] = operation[step]->evaluate(&workspace[args[0]], dummyVariables);
  112. else
  113. {
  114. for (size_t i = 0; i < args.size(); ++i) argValues[i] = workspace[args[i]];
  115. workspace[target[step]] = operation[step]->evaluate(&argValues[0], dummyVariables);
  116. }
  117. }
  118. return workspace[workspace.size() - 1];
  119. }