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
18
19namespace NeoN::dsl
20{
21
22template<typename VectorType>
28
29
30template<typename ValueType>
32{
33public:
34
35 Expression(const Executor& exec) : exec_(exec), temporalOperators_(), spatialOperators_() {}
36
38 : exec_(exp.exec_), temporalOperators_(exp.temporalOperators_),
39 spatialOperators_(exp.spatialOperators_)
40 {}
41
42 /* @brief dispatch read call to operator */
43 void read(const Dictionary& input)
44 {
45 for (auto& op : temporalOperators_)
46 {
47 op.read(input);
48 }
49 for (auto& op : spatialOperators_)
50 {
51 op.read(input);
52 }
53 }
54
55 /* @brief perform all explicit operation and accumulate the result */
57 {
58 Vector<ValueType> source(exec_, nCells, zero<ValueType>());
59 return explicitOperation(source);
60 }
61
62 /* @brief perform all explicit operation and accumulate the result */
64 {
65 for (auto& op : spatialOperators_)
66 {
67 if (op.getType() == Operator::Type::Explicit)
68 {
69 op.explicitOperation(source);
70 }
71 }
72 return source;
73 }
74
76 {
77 for (auto& op : temporalOperators_)
78 {
79 if (op.getType() == Operator::Type::Explicit)
80 {
81 op.explicitOperation(source, t, dt);
82 }
83 }
84 return source;
85 }
86
87 /*@brief compute matrix coefficients based on all spatial operators */
89 {
90 for (auto& op : spatialOperators_)
91 {
92 if (op.getType() == Operator::Type::Implicit)
93 {
94 op.implicitOperation(ls);
95 }
96 }
97 }
98
99 /*@brief compute matrix coefficients based on all temporal operators
100 * assemble directly into linear system
101 */
102 void
104 {
105 for (auto& op : temporalOperators_)
106 {
107 if (op.getType() == Operator::Type::Implicit)
108 {
109 op.implicitOperation(ls, t, dt);
110 }
111 }
112 }
113
114 /* @brief construct a linear system and force assembly
115 *
116 * @param ps a vector of functor performing transformation on the created linear system
117 * @return a tuple of the sparsity pattern and the assembled linear system
118 */
119 std::tuple<la::SparsityPattern, la::LinearSystem<ValueType, localIdx>> assemble(
120 const UnstructuredMesh& mesh,
121 scalar t,
122 scalar dt,
123 std::span<const PostAssemblyBase<ValueType>> ps = {}
124 ) const
125 {
126 auto sp = la::SparsityPattern(mesh);
127 auto ls = la::createEmptyLinearSystem<ValueType, localIdx>(mesh, sp);
128 assemble(t, dt, sp, ls, ps);
129 return {sp, ls};
130 };
131
132 /* @brief assemble into a given linear system
133 *
134 * @param ps a vector of functor performing transformation on the created linear system
135 */
137 scalar t,
138 scalar dt,
139 const la::SparsityPattern& sp,
141 std::span<const PostAssemblyBase<ValueType>> ps = {}
142 ) const
143 {
144 assembleSpatialOperator(ls); // add spatial operator
145 assembleTemporalOperator(ls, t, dt); // add temporal operators
146
147 // perform post assembly transformations
148 for (auto p : ps)
149 {
150 p(sp, ls);
151 }
152 };
153
154 void addOperator(const SpatialOperator<ValueType>& oper) { spatialOperators_.push_back(oper); }
155
157 {
158 temporalOperators_.push_back(oper);
159 }
160
161 void addExpression(const Expression& equation)
162 {
163 for (auto& oper : equation.temporalOperators_)
164 {
165 temporalOperators_.push_back(oper);
166 }
167 for (auto& oper : equation.spatialOperators_)
168 {
169 spatialOperators_.push_back(oper);
170 }
171 }
172
173
174 /* @brief getter for the total number of terms in the equation */
176 {
177 return static_cast<localIdx>(temporalOperators_.size() + spatialOperators_.size());
178 }
179
180 // getters
181 const std::vector<TemporalOperator<ValueType>>& temporalOperators() const
182 {
183 return temporalOperators_;
184 }
185
186 const std::vector<SpatialOperator<ValueType>>& spatialOperators() const
187 {
188 return spatialOperators_;
189 }
190
191 std::vector<TemporalOperator<ValueType>>& temporalOperators() { return temporalOperators_; }
192
193 std::vector<SpatialOperator<ValueType>>& spatialOperators() { return spatialOperators_; }
194
195 const Executor& exec() const { return exec_; }
196
197private:
198
199 const Executor exec_;
200
201 std::vector<TemporalOperator<ValueType>> temporalOperators_;
202
203 std::vector<SpatialOperator<ValueType>> spatialOperators_;
204};
205
206template<typename ValueType>
207[[nodiscard]] inline Expression<ValueType>
209{
210 lhs.addExpression(rhs);
211 return lhs;
212}
213
214template<typename ValueType>
215[[nodiscard]] inline Expression<ValueType>
217{
218 lhs.addOperator(rhs);
219 return lhs;
220}
221
222template<typename leftOperator, typename rightOperator>
223[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
224operator+(leftOperator lhs, rightOperator rhs)
225{
226 using ValueType = typename leftOperator::VectorValueType;
227 Expression<ValueType> expr(lhs.exec());
228 expr.addOperator(lhs);
229 expr.addOperator(rhs);
230 return expr;
231}
232
233template<typename ValueType>
234[[nodiscard]] inline Expression<ValueType> operator*(scalar scale, const Expression<ValueType>& es)
235{
236 Expression<ValueType> expr(es.exec());
237 for (const auto& oper : es.temporalOperators())
238 {
239 expr.addOperator(scale * oper);
240 }
241 for (const auto& oper : es.spatialOperators())
242 {
243 expr.addOperator(scale * oper);
244 }
245 return expr;
246}
247
248
249template<typename ValueType>
250[[nodiscard]] inline Expression<ValueType>
252{
253 lhs.addExpression(-1.0 * rhs);
254 return lhs;
255}
256
257template<typename ValueType>
258[[nodiscard]] inline Expression<ValueType>
260{
261 lhs.addOperator(-1.0 * rhs);
262 return lhs;
263}
264
265template<typename leftOperator, typename rightOperator>
266[[nodiscard]] inline Expression<typename leftOperator::VectorValueType>
267operator-(leftOperator lhs, rightOperator rhs)
268{
269 using ValueType = typename leftOperator::VectorValueType;
270 Expression<ValueType> expr(lhs.exec());
271 expr.addOperator(lhs);
272 expr.addOperator(Coeff(-1) * rhs);
273 return expr;
274}
275
276
277} // namespace dsl
A class representing a dictionary that stores key-value pairs.
Represents an unstructured mesh in NeoN.
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
void assembleSpatialOperator(la::LinearSystem< ValueType, localIdx > &ls) const
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
Vector< ValueType > explicitOperation(localIdx nCells) const
void assembleTemporalOperator(la::LinearSystem< ValueType, localIdx > &ls, scalar t, scalar dt) const
Expression(const Executor &exec)
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()
std::tuple< la::SparsityPattern, la::LinearSystem< ValueType, localIdx > > assemble(const UnstructuredMesh &mesh, scalar t, scalar dt, std::span< const PostAssemblyBase< ValueType > > ps={}) const
void addOperator(const SpatialOperator< ValueType > &oper)
Vector< ValueType > explicitOperation(Vector< ValueType > &source) const
void assemble(scalar t, scalar dt, const la::SparsityPattern &sp, la::LinearSystem< ValueType, localIdx > &ls, std::span< const PostAssemblyBase< ValueType > > ps={}) 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
virtual ~PostAssemblyBase()=default
virtual void operator()(const la::SparsityPattern &, la::LinearSystem< VectorType, localIdx > &)