NeoFOAM
WIP Prototype of a modern OpenFOAM core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Concepts
expression.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023-2024 NeoFOAM authors
3#pragma once
4
5#include <memory>
6#include <vector>
7#include <utility>
8
15
16namespace la = NeoFOAM::la;
17
18namespace NeoFOAM::dsl
19{
20
21
22template<typename ValueType>
24{
25public:
26
27 Expression(const Executor& exec) : exec_(exec), temporalOperators_(), spatialOperators_() {}
28
30 : exec_(exp.exec_), temporalOperators_(exp.temporalOperators_),
31 spatialOperators_(exp.spatialOperators_)
32 {}
33
34 void build(const NeoFOAM::Dictionary& input)
35 {
36 for (auto& op : temporalOperators_)
37 {
38 op.build(input);
39 }
40 for (auto& op : spatialOperators_)
41 {
42 op.build(input);
43 }
44 }
45
46 /* @brief perform all explicit operation and accumulate the result */
48 {
49 Field<ValueType> source(exec_, nCells, zero<ValueType>());
50 return explicitOperation(source);
51 }
52
53 /* @brief perform all explicit operation and accumulate the result */
55 {
56 for (auto& op : spatialOperators_)
57 {
58 if (op.getType() == Operator::Type::Explicit)
59 {
60 op.explicitOperation(source);
61 }
62 }
63 return source;
64 }
65
67 {
68 for (auto& op : temporalOperators_)
69 {
70 if (op.getType() == Operator::Type::Explicit)
71 {
72 op.explicitOperation(source, t, dt);
73 }
74 }
75 return source;
76 }
77
78 /* @brief perform all implicit operation and accumulate the result */
80 {
81 auto ls = spatialOperators_[0].createEmptyLinearSystem();
82 for (auto& op : spatialOperators_)
83 {
84 if (op.getType() == Operator::Type::Implicit)
85 {
86 op.implicitOperation(ls);
87 }
88 }
89 return ls;
90 }
91
93 {
94 for (auto& op : temporalOperators_)
95 {
96 if (op.getType() == Operator::Type::Implicit)
97 {
98 op.implicitOperation(ls, t, dt);
99 }
100 }
101 }
102
103
104 void addOperator(const SpatialOperator<ValueType>& oper) { spatialOperators_.push_back(oper); }
105
107 {
108 temporalOperators_.push_back(oper);
109 }
110
111 void addExpression(const Expression& equation)
112 {
113 for (auto& oper : equation.temporalOperators_)
114 {
115 temporalOperators_.push_back(oper);
116 }
117 for (auto& oper : equation.spatialOperators_)
118 {
119 spatialOperators_.push_back(oper);
120 }
121 }
122
123
124 /* @brief getter for the total number of terms in the equation */
125 size_t size() const { return temporalOperators_.size() + spatialOperators_.size(); }
126
127 // getters
128 const std::vector<TemporalOperator<ValueType>>& temporalOperators() const
129 {
130 return temporalOperators_;
131 }
132
133 const std::vector<SpatialOperator<ValueType>>& spatialOperators() const
134 {
135 return spatialOperators_;
136 }
137
138 std::vector<TemporalOperator<ValueType>>& temporalOperators() { return temporalOperators_; }
139
140 std::vector<SpatialOperator<ValueType>>& spatialOperators() { return spatialOperators_; }
141
142 const Executor& exec() const { return exec_; }
143
144private:
145
146 const Executor exec_;
147
148 std::vector<TemporalOperator<ValueType>> temporalOperators_;
149
150 std::vector<SpatialOperator<ValueType>> spatialOperators_;
151};
152
153template<typename ValueType>
154[[nodiscard]] inline Expression<ValueType>
156{
157 lhs.addExpression(rhs);
158 return lhs;
159}
160
161template<typename ValueType>
162[[nodiscard]] inline Expression<ValueType>
164{
165 lhs.addOperator(rhs);
166 return lhs;
167}
168
169template<typename leftOperator, typename rightOperator>
170[[nodiscard]] inline Expression<typename leftOperator::FieldValueType>
171operator+(leftOperator lhs, rightOperator rhs)
172{
173 using ValueType = typename leftOperator::FieldValueType;
174 Expression<ValueType> expr(lhs.exec());
175 expr.addOperator(lhs);
176 expr.addOperator(rhs);
177 return expr;
178}
179
180// [[nodiscard]] inline Expression operator+(const SpatialOperator& lhs, const SpatialOperator& rhs)
181// {
182// Expression expr(lhs.exec());
183// expr.addOperator(lhs);
184// expr.addOperator(rhs);
185// return expr;
186// }
187
188template<typename ValueType>
189[[nodiscard]] inline Expression<ValueType> operator*(scalar scale, const Expression<ValueType>& es)
190{
191 Expression<ValueType> expr(es.exec());
192 for (const auto& oper : es.temporalOperators())
193 {
194 expr.addOperator(scale * oper);
195 }
196 for (const auto& oper : es.spatialOperators())
197 {
198 expr.addOperator(scale * oper);
199 }
200 return expr;
201}
202
203
204template<typename ValueType>
205[[nodiscard]] inline Expression<ValueType>
207{
208 lhs.addExpression(-1.0 * rhs);
209 return lhs;
210}
211
212template<typename ValueType>
213[[nodiscard]] inline Expression<ValueType>
215{
216 lhs.addOperator(-1.0 * rhs);
217 return lhs;
218}
219
220template<typename leftOperator, typename rightOperator>
221[[nodiscard]] inline Expression<typename leftOperator::FieldValueType>
222operator-(leftOperator lhs, rightOperator rhs)
223{
224 using ValueType = typename leftOperator::FieldValueType;
225 Expression<ValueType> expr(lhs.exec());
226 expr.addOperator(lhs);
227 expr.addOperator(Coeff(-1) * rhs);
228 return expr;
229}
230
231// [[nodiscard]] inline Expression operator-(const SpatialOperator& lhs, const SpatialOperator& rhs)
232// {
233// Expression expr(lhs.exec());
234// expr.addOperator(lhs);
235// expr.addOperator(Coeff(-1) * rhs);
236// return expr;
237// }
238
239
240} // namespace NeoFOAM::dsl
A class representing a dictionary that stores key-value pairs.
A class to contain the data and executors for a field and define some basic operations.
Definition field.hpp:49
A class that represents a coefficient for the NeoFOAM dsl.
Definition coeff.hpp:22
Field< ValueType > explicitOperation(Field< ValueType > &source, scalar t, scalar dt)
void addOperator(const SpatialOperator< ValueType > &oper)
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
const Executor & exec() const
la::LinearSystem< ValueType, localIdx > implicitOperation()
void addOperator(const TemporalOperator< ValueType > &oper)
std::vector< TemporalOperator< ValueType > > & temporalOperators()
Expression(const Executor &exec)
Field< ValueType > explicitOperation(Field< ValueType > &source)
void addExpression(const Expression &equation)
Expression(const Expression &exp)
Field< ValueType > explicitOperation(size_t nCells)
void build(const NeoFOAM::Dictionary &input)
std::vector< SpatialOperator< ValueType > > & spatialOperators()
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls, scalar t, scalar dt)
A class representing a linear system of equations.
Expression< ValueType > operator+(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
Expression< ValueType > operator-(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
Coeff operator*(const Coeff &lhs, const Coeff &rhs)
Definition coeff.hpp:57
float scalar
Definition scalar.hpp:14
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16