OpenLB 1.8.1
Loading...
Searching...
No Matches
expr.cpp
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#include <iostream>
25
26#include "olbInit.h"
27#include "expr.h"
28
30
31namespace olb {
32
33std::array<std::size_t, static_cast<std::size_t>(Expr::Op::End)> Expr::_stats = { };
34
35void Expr::Symbol::describe(std::stringstream& out) const {
36 out << name;
37}
38
39std::size_t Expr::Symbol::size() const {
40 return 1;
41}
42
43Expr::Constant::Constant():
44 value{} { }
45
46Expr::Constant::Constant(double x):
47 value{x} { }
48
49void Expr::Constant::describe(std::stringstream& out) const {
50 out << std::to_string(value);
51}
52
53std::size_t Expr::Constant::size() const {
54 return 1;
55}
56
57Expr::Binary::Binary(Expr lhs, Op op, Expr rhs):
58 _lhs{std::make_shared<Expr>(lhs)},
59 op{op},
60 _rhs{std::make_shared<Expr>(rhs)} { }
61
62void Expr::Binary::describe(std::stringstream& out) const {
63 switch (op) {
64 case Op::Add: out << "("; _lhs->describe(out); out << ") + ("; _rhs->describe(out); out << ")"; break;
65 case Op::Sub: out << "("; _lhs->describe(out); out << ") - ("; _rhs->describe(out); out << ")"; break;
66 case Op::Mul: out << "("; _lhs->describe(out); out << ") * ("; _rhs->describe(out); out << ")"; break;
67 case Op::Div: out << "("; _lhs->describe(out); out << ") / ("; _rhs->describe(out); out << ")"; break;
68 case Op::Pow: out << "Pow("; _lhs->describe(out); out << ","; _rhs->describe(out); out << ")"; break;
69 default: throw std::invalid_argument("Unsupported binary operation");
70 }
71}
72
73std::size_t Expr::Binary::size() const {
74 return 1 + _lhs->size() + _rhs->size();
75}
76
77Expr::Unary::Unary(Op op, Expr arg):
78 op{op},
79 _arg{std::make_shared<Expr>(arg)} { }
80
81void Expr::Unary::describe(std::stringstream& out) const {
82 switch (op) {
83 case Op::Sqrt: out << "sqrt("; _arg->describe(out); out << ")"; break;
84 case Op::Abs: out << "Abs("; _arg->describe(out); out << ")"; break;
85 case Op::Exp: out << "exp("; _arg->describe(out); out << ")"; break;
86 default: throw std::invalid_argument("Unsupported unary operation");
87 }
88}
89
90std::size_t Expr::Unary::size() const {
91 return 1 + _arg->size();
92}
93
94Expr::Expr():
95 _payload(Constant{}) { }
96
98 _payload(rhs._payload) { }
99
100Expr::Expr(const Expr& rhs):
101 _payload(rhs._payload) { }
102
103Expr::Expr(double v):
104 _payload(Constant{v}) { }
105
106Expr::Expr(std::string name):
107 _payload(Symbol{name}) { }
108
109Expr::Expr(Expr lhs, Op op, Expr rhs):
110 _payload(Binary(lhs, op, rhs)) { }
111
113 _payload(Unary(op, rhs)) { }
114
116 _payload = rhs._payload;
117 return *this;
118}
119
122 _payload = Binary(*this, Op::Add, rhs);
123 return *this;
124}
125
128 _payload = Binary(*this, Op::Sub, rhs);
129 return *this;
130}
131
134 _payload = Binary(*this, Op::Mul, rhs);
135 return *this;
136}
137
140 _payload = Binary(*this, Op::Div, rhs);
141 return *this;
142}
143
144void Expr::describe(std::stringstream& out) const {
145 std::visit([&out](auto& x) {
146 x.describe(out);
147 }, _payload);
148};
149
150std::string Expr::describe() const {
151 std::stringstream out;
152 describe(out);
153 return out.str();
154};
155
156bool Expr::isSymbol(std::string name) const {
157 if (auto symbol = std::get_if<Symbol>(&_payload)) {
158 return (*symbol).name == name;
159 }
160 return false;
161}
162
163std::size_t Expr::size() const {
164 std::size_t size{};
165 std::visit([&size](auto& x) {
166 size = x.size();
167 }, _payload);
168 return size;
169}
170
173 return Expr(lhs, Expr::Op::Add, rhs);
174}
175
178 return Expr(lhs, Expr::Op::Sub, rhs);
179}
180
183 return Expr(lhs, Expr::Op::Mul, rhs);
184}
185
188 return Expr(lhs, Expr::Op::Div, rhs);
189}
190
192 return Expr(-1.) * rhs;
193}
194
195Expr operator%(Expr lhs, int rhs) {
196 throw std::domain_error("Invalid operator for Expr type: '%'");
197}
198
199bool operator==(const Expr& rhs, const Expr& lhs) {
200 throw std::domain_error("Invalid operator for Expr type: '=='");
201}
202
203bool operator!=(const Expr& rhs, const Expr& lhs) {
204 throw std::domain_error("Invalid operator for Expr type: '!='");
205}
206
207bool operator>(const Expr& rhs, const Expr& lhs) {
208 throw std::domain_error("Invalid operator for Expr type: '>'");
209}
210
211bool operator<(const Expr& rhs, const Expr& lhs) {
212 throw std::domain_error("Invalid operator for Expr type: '<'");
213}
214
215bool operator>=(const Expr& rhs, const Expr& lhs) {
216 throw std::domain_error("Invalid operator for Expr type: '>='");
217}
218
219bool operator<=(const Expr& rhs, const Expr& lhs) {
220 throw std::domain_error("Invalid operator for Expr type: '<='");
221}
222
223namespace util {
224
229
234
237 return Expr(base, Expr::Op::Pow, exp);
238}
239
244
246 throw std::domain_error("Invalid operator for Expr type: 'max'");
247}
248
250 throw std::domain_error("Invalid operator for Expr type: 'min'");
251}
252
253}
254
255}
Basic value-substitute enabling extraction of expression trees for code generation.
Definition expr.h:39
std::string describe() const
Returns the serialized expression tree.
Definition expr.cpp:150
Expr & operator=(const Expr &rhs)
Definition expr.cpp:115
Expr & operator*=(Expr rhs)
Definition expr.cpp:132
static void increment(Op op)
Increment FLOP counter.
Definition expr.h:128
std::size_t size() const
Returns expanded size of tree.
Definition expr.cpp:163
Expr & operator-=(Expr rhs)
Definition expr.cpp:126
Expr & operator/=(Expr rhs)
Definition expr.cpp:138
bool isSymbol(std::string name) const
Helper function to quickly check if Expr is a specific symbol.
Definition expr.cpp:156
Expr & operator+=(Expr rhs)
Definition expr.cpp:120
typename std::integral_constant< TYPE, VALUE >::type value
Identity type to wrap non-type template arguments.
Definition meta.h:96
Expr sqrt(Expr x)
Definition expr.cpp:225
Expr min(Expr a, Expr b)
Definition expr.cpp:249
Expr max(Expr a, Expr b)
Definition expr.cpp:245
Expr pow(Expr base, Expr exp)
Definition expr.cpp:235
Expr fabs(Expr x)
Definition expr.cpp:230
Expr exp(Expr x)
Definition expr.cpp:240
Top level namespace for all of OpenLB.
bool operator>=(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:215
Expr operator/(Expr lhs, Expr rhs)
Definition expr.cpp:186
bool operator>(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:207
bool operator<=(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:219
Expr operator*(Expr lhs, Expr rhs)
Definition expr.cpp:181
Expr operator%(Expr lhs, int rhs)
Definition expr.cpp:195
Expr operator+(Expr lhs, Expr rhs)
Definition expr.cpp:171
bool operator!=(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:203
bool operator==(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:199
Expr operator-(Expr lhs, Expr rhs)
Definition expr.cpp:176
bool operator<(const Expr &rhs, const Expr &lhs)
Definition expr.cpp:211
LB initialisation routine – header file.