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