NeoN
A framework for CFD software
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"
13
15
16namespace la = la;
17
18namespace NeoN::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 /* @brief dispatch read call to operator */
35 void read(const Dictionary& input)
36 {
37 for (auto& op : temporalOperators_)
38 {
39 op.read(input);
40 }
41 for (auto& op : spatialOperators_)
42 {
43 op.read(input);
44 }
45 }
46
47 /* @brief perform all explicit operation and accumulate the result */
49 {
50 Vector<ValueType> source(exec_, nCells, zero<ValueType>());
51 return explicitOperation(source);
52 }
53
54 /* @brief perform all explicit operation and accumulate the result */
56 {
57 for (auto& op : spatialOperators_)
58 {
59 if (op.getType() == Operator::Type::Explicit)
60 {
61 op.explicitOperation(source);
62 }
63 }
64 return source;
65 }
66
68 {
69 for (auto& op : temporalOperators_)
70 {
71 if (op.getType() == Operator::Type::Explicit)
72 {
73 op.explicitOperation(source, t, dt);
74 }
75 }
76 return source;
77 }
78
79 // TODO: rename to assembleMatrixCoefficients ?
80 /* @brief perform all implicit operation and accumulate the result */
82 {
83 for (auto& op : spatialOperators_)
84 {
85 if (op.getType() == Operator::Type::Implicit)
86 {
87 op.implicitOperation(ls);
88 }
89 }
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 */
126 {
127 return static_cast<localIdx>(temporalOperators_.size() + spatialOperators_.size());
128 }
129
130 // getters
131 const std::vector<TemporalOperator<ValueType>>& temporalOperators() const
132 {
133 return temporalOperators_;
134 }
135
136 const std::vector<SpatialOperator<ValueType>>& spatialOperators() const
137 {
138 return spatialOperators_;
139 }
140
141 std::vector<TemporalOperator<ValueType>>& temporalOperators() { return temporalOperators_; }
142
143 std::vector<SpatialOperator<ValueType>>& spatialOperators() { return spatialOperators_; }
144
145 const Executor& exec() const { return exec_; }
146
147private:
148
149 const Executor exec_;
150
151 std::vector<TemporalOperator<ValueType>> temporalOperators_;
152
153 std::vector<SpatialOperator<ValueType>> spatialOperators_;
154};
155
156template<typename ValueType>
157[[nodiscard]] inline Expression<ValueType>
159{
160 lhs.addExpression(rhs);
161 return lhs;
162}
163
164template<typename ValueType>
165[[nodiscard]] inline Expression<ValueType>
167{
168 lhs.addOperator(rhs);
169 return lhs;
170}
171
172template<typename leftOperator, typename rightOperator>
173[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
174operator+(leftOperator lhs, rightOperator rhs)
175{
176 using ValueType = typename leftOperator::VectorValueType;
177 Expression<ValueType> expr(lhs.exec());
178 expr.addOperator(lhs);
179 expr.addOperator(rhs);
180 return expr;
181}
182
183template<typename ValueType>
184[[nodiscard]] inline Expression<ValueType> operator*(scalar scale, const Expression<ValueType>& es)
185{
186 Expression<ValueType> expr(es.exec());
187 for (const auto& oper : es.temporalOperators())
188 {
189 expr.addOperator(scale * oper);
190 }
191 for (const auto& oper : es.spatialOperators())
192 {
193 expr.addOperator(scale * oper);
194 }
195 return expr;
196}
197
198
199template<typename ValueType>
200[[nodiscard]] inline Expression<ValueType>
202{
203 lhs.addExpression(-1.0 * rhs);
204 return lhs;
205}
206
207template<typename ValueType>
208[[nodiscard]] inline Expression<ValueType>
210{
211 lhs.addOperator(-1.0 * rhs);
212 return lhs;
213}
214
215template<typename leftOperator, typename rightOperator>
216[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
217operator-(leftOperator lhs, rightOperator rhs)
218{
219 using ValueType = typename leftOperator::VectorValueType;
220 Expression<ValueType> expr(lhs.exec());
221 expr.addOperator(lhs);
222 expr.addOperator(Coeff(-1) * rhs);
223 return expr;
224}
225
226
227} // 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:28
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
Expression(const Expression &exp)
void read(const Dictionary &input)
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