CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
mathStatementGenLogic.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <map>
38#include <type_traits>
39#include <utility>
40#include <vector>
41
42#include "../../../config.h"
44#include "../../../traits/expressionTraits.hpp"
45#include "../traversalLogic.hpp"
46#include "forEachLeafLogic.hpp"
47
49namespace codi {
50
55 template<typename Identifier>
56 struct MathStatementGenLogic : public ForEachLeafLogic<MathStatementGenLogic<Identifier>> {
57 public:
58 Identifier passiveThreshold;
59
62
64 template<typename Node>
65 CODI_INLINE void eval(NodeInterface<Node> const& node, std::string& mathRep) {
66 std::vector<std::string> topNodeRep;
67 this->toNode(node.cast(), topNodeRep);
68
69 // Check for unnecessary brackets at the start of the statement.
70 // If found, remove first and last bracket.
71 if (topNodeRep[0].find("(") == 0) {
72 topNodeRep[0] = topNodeRep[0].substr(1, topNodeRep[0].size() - 2);
73 }
74
75 mathRep = topNodeRep[0];
76 }
77
78 /*******************************************************************************/
81
83 template<typename Node>
84 CODI_INLINE void node(Node const& node, std::vector<std::string>& nodeRep) {
85 std::vector<std::string> linkRep;
86 std::string stmtOperator = node.getMathRep();
87
88 this->toLinks(node, linkRep);
89
90 if (linkRep.size() == 2) {
91 // Determine if the operator comes before the two args.
92 // This is indicated by the addition of "()" at the end of the stmtOperator.
93 if (stmtOperator.find("()") != std::string::npos) {
94 // Remove ')'.
95 stmtOperator.pop_back();
96 nodeRep.push_back(stmtOperator + linkRep[0] + ", " + linkRep[1] + ")");
97 } else {
98 nodeRep.push_back("(" + linkRep[0] + " " + stmtOperator + " " + linkRep[1] + ")");
99 }
100 } else {
101 nodeRep.push_back(stmtOperator + "(" + linkRep[0] + ")");
102 }
103 }
104
106 template<typename Node>
107 void handleActive(Node const& node, std::vector<std::string>& linkRep) {
108 // Check for passive values
109 if (node.getIdentifier() <= passiveThreshold) {
110 linkRep.push_back("p(" + std::to_string(node.getValue()) + ")");
111 } else {
112 linkRep.push_back("x" + std::to_string(node.getIdentifier()));
113 }
114 }
115
117 template<typename Node>
118 void handleConstant(Node const& node, std::vector<std::string>& linkRep) {
119 linkRep.push_back("c(" + convert_value(node.getValue()) + ")");
120 }
121
122 private:
123
124 template<typename T>
125 std::string convert_value(T const& v) {
126 return std::to_string(v);
127
128 }
129
130 template<typename T>
131 std::string convert_value(T* const& v) {
132 return "p" + std::to_string(reinterpret_cast<size_t>(v));
133 }
134 };
135}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
Implement logic for leaf nodes only.
Definition forEachLeafLogic.hpp:60
Creates a math representation from a given rhs in the form of a string.
Definition mathStatementGenLogic.hpp:56
Identifier passiveThreshold
The identifiers that are allocated for passive values.
Definition mathStatementGenLogic.hpp:58
void eval(NodeInterface< Node > const &node, std::string &mathRep)
Produces a math representation string for a given statement.
Definition mathStatementGenLogic.hpp:65
void node(Node const &node, std::vector< std::string > &nodeRep)
Links two nodes with a math operation.
Definition mathStatementGenLogic.hpp:84
void handleConstant(Node const &node, std::vector< std::string > &linkRep)
Called for leaf nodes which implement ConstantExpression.
Definition mathStatementGenLogic.hpp:118
MathStatementGenLogic(Identifier passiveThreshold=0)
Constructor.
Definition mathStatementGenLogic.hpp:61
void handleActive(Node const &node, std::vector< std::string > &linkRep)
Called for leaf nodes which implement LhsExpressionInterface.
Definition mathStatementGenLogic.hpp:107
Node side interface for the traversal of expressions.
Definition nodeInterface.hpp:56
Impl const & cast() const
Cast to the implementation.
Definition nodeInterface.hpp:62
void toNode(Node const &node, Args &&... args)
Helper method to distinguish between leaf nodes and normal nodes.
Definition traversalLogic.hpp:142
void toLinks(Node const &node, Args &&... args)
Helper method which calls forEachLink on the node.
Definition traversalLogic.hpp:148