NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
expression.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023-2024 NeoN authors
3#pragma once
4
5#include <vector>
6
7#include "NeoN/core/error.hpp"
14
15namespace la = la;
16
17namespace NeoN::dsl
18{
19
20
21template<typename ValueType>
23{
24public:
25
26 Expression(const Executor& exec) : exec_(exec), temporalOperators_(), spatialOperators_() {}
27
29 : exec_(exp.exec_), temporalOperators_(exp.temporalOperators_),
30 spatialOperators_(exp.spatialOperators_)
31 {}
32
33 void build(const Dictionary& input)
34 {
35 for (auto& op : temporalOperators_)
36 {
37 op.build(input);
38 }
39 for (auto& op : spatialOperators_)
40 {
41 op.build(input);
42 }
43 }
44
45 /* @brief perform all explicit operation and accumulate the result */
47 {
48 Vector<ValueType> source(exec_, nCells, zero<ValueType>());
49 return explicitOperation(source);
50 }
51
52 /* @brief perform all explicit operation and accumulate the result */
54 {
55 for (auto& op : spatialOperators_)
56 {
57 if (op.getType() == Operator::Type::Explicit)
58 {
59 op.explicitOperation(source);
60 }
61 }
62 return source;
63 }
64
66 {
67 for (auto& op : temporalOperators_)
68 {
69 if (op.getType() == Operator::Type::Explicit)
70 {
71 op.explicitOperation(source, t, dt);
72 }
73 }
74 return source;
75 }
76
77 // TODO: rename to assembleMatrixCoefficients ?
78 /* @brief perform all implicit operation and accumulate the result */
80 {
81 for (auto& op : spatialOperators_)
82 {
83 if (op.getType() == Operator::Type::Implicit)
84 {
85 op.implicitOperation(ls);
86 }
87 }
88 }
89
91 {
92 for (auto& op : temporalOperators_)
93 {
94 if (op.getType() == Operator::Type::Implicit)
95 {
96 op.implicitOperation(ls, t, dt);
97 }
98 }
99 }
100
101
102 void addOperator(const SpatialOperator<ValueType>& oper) { spatialOperators_.push_back(oper); }
103
105 {
106 temporalOperators_.push_back(oper);
107 }
108
109 void addExpression(const Expression& equation)
110 {
111 for (auto& oper : equation.temporalOperators_)
112 {
113 temporalOperators_.push_back(oper);
114 }
115 for (auto& oper : equation.spatialOperators_)
116 {
117 spatialOperators_.push_back(oper);
118 }
119 }
120
121
122 /* @brief getter for the total number of terms in the equation */
124 {
125 return static_cast<localIdx>(temporalOperators_.size() + spatialOperators_.size());
126 }
127
128 // getters
129 const std::vector<TemporalOperator<ValueType>>& temporalOperators() const
130 {
131 return temporalOperators_;
132 }
133
134 const std::vector<SpatialOperator<ValueType>>& spatialOperators() const
135 {
136 return spatialOperators_;
137 }
138
139 std::vector<TemporalOperator<ValueType>>& temporalOperators() { return temporalOperators_; }
140
141 std::vector<SpatialOperator<ValueType>>& spatialOperators() { return spatialOperators_; }
142
143 const Executor& exec() const { return exec_; }
144
145private:
146
147 const Executor exec_;
148
149 std::vector<TemporalOperator<ValueType>> temporalOperators_;
150
151 std::vector<SpatialOperator<ValueType>> spatialOperators_;
152};
153
154template<typename ValueType>
155[[nodiscard]] inline Expression<ValueType>
157{
158 lhs.addExpression(rhs);
159 return lhs;
160}
161
162template<typename ValueType>
163[[nodiscard]] inline Expression<ValueType>
165{
166 lhs.addOperator(rhs);
167 return lhs;
168}
169
170template<typename leftOperator, typename rightOperator>
171[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
172operator+(leftOperator lhs, rightOperator rhs)
173{
174 using ValueType = typename leftOperator::VectorValueType;
175 Expression<ValueType> expr(lhs.exec());
176 expr.addOperator(lhs);
177 expr.addOperator(rhs);
178 return expr;
179}
180
181template<typename ValueType>
182[[nodiscard]] inline Expression<ValueType> operator*(scalar scale, const Expression<ValueType>& es)
183{
184 Expression<ValueType> expr(es.exec());
185 for (const auto& oper : es.temporalOperators())
186 {
187 expr.addOperator(scale * oper);
188 }
189 for (const auto& oper : es.spatialOperators())
190 {
191 expr.addOperator(scale * oper);
192 }
193 return expr;
194}
195
196
197template<typename ValueType>
198[[nodiscard]] inline Expression<ValueType>
200{
201 lhs.addExpression(-1.0 * rhs);
202 return lhs;
203}
204
205template<typename ValueType>
206[[nodiscard]] inline Expression<ValueType>
208{
209 lhs.addOperator(-1.0 * rhs);
210 return lhs;
211}
212
213template<typename leftOperator, typename rightOperator>
214[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
215operator-(leftOperator lhs, rightOperator rhs)
216{
217 using ValueType = typename leftOperator::VectorValueType;
218 Expression<ValueType> expr(lhs.exec());
219 expr.addOperator(lhs);
220 expr.addOperator(Coeff(-1) * rhs);
221 return expr;
222}
223
224
225} // namespace 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 vector.hpp:53
A class that represents a coefficient for the NeoN dsl.
Definition coeff.hpp:22
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls)
Vector< ValueType > explicitOperation(localIdx nCells) const
Expression(const Executor &exec)
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls, scalar t, scalar dt)
void addOperator(const TemporalOperator< ValueType > &oper)
void addExpression(const Expression &equation)
localIdx size() const
Vector< ValueType > explicitOperation(Vector< ValueType > &source, scalar t, scalar dt) const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
std::vector< SpatialOperator< ValueType > > & spatialOperators()
void addOperator(const SpatialOperator< ValueType > &oper)
Vector< ValueType > explicitOperation(Vector< ValueType > &source) const
void build(const Dictionary &input)
Expression(const Expression &exp)
const Executor & exec() const
std::vector< TemporalOperator< ValueType > > & temporalOperators()
A class representing a linear system of equations.
Expression< ValueType > operator+(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
Coeff operator*(const Coeff &lhs, const Coeff &rhs)
Definition coeff.hpp:57
Expression< ValueType > operator-(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
int32_t localIdx
Definition label.hpp:30
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
float scalar
Definition scalar.hpp:14