NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
solver.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 <iostream>
6#include <memory>
7#include <type_traits>
8#include <utility>
9#include <concepts>
10
12#include "NeoN/fields/field.hpp"
13#include "NeoN/core/input.hpp"
17
20
21// FIXME
23
24
25namespace NeoN::dsl
26{
27
28/* @brief solve an expression
29 *
30 * @param exp - Expression which is to be solved/updated.
31 * @param solution - Solution field, where the solution will be 'written to'.
32 * @param t - the time at the start of the time step.
33 * @param dt - time step for the temporal integration
34 * @param fvSchemes - Dictionary containing spatial operator and time integration properties
35 * @param fvSolution - Dictionary containing linear solver properties
36 */
37template<typename VectorType>
38void solve(
40 VectorType& solution,
41 scalar t,
42 scalar dt,
43 [[maybe_unused]] const Dictionary& fvSchemes,
44 const Dictionary& fvSolution
45)
46{
47 // TODO:
48 if (exp.temporalOperators().size() == 0 && exp.spatialOperators().size() == 0)
49 {
50 NF_ERROR_EXIT("No temporal or implicit terms to solve.");
51 }
52 exp.build(fvSchemes);
53 if (exp.temporalOperators().size() > 0)
54 {
55 // integrate equations in time
57 fvSchemes.subDict("ddtSchemes"), fvSchemes
58 );
59 timeIntegrator.solve(exp, solution, t, dt);
60 }
61 else
62 {
63 // solve sparse matrix system
64 using ValueType = typename VectorType::ElementType;
65
66 auto sparsity = NeoN::finiteVolume::cellCentred::SparsityPattern(solution.mesh());
68 ValueType,
71
72 exp.implicitOperation(ls);
73 auto expTmp = exp.explicitOperation(solution.mesh().nCells());
74
75 auto [vol, expSource, rhs] = spans(solution.mesh().cellVolumes(), expTmp, ls.rhs());
76
77 // subtract the explicit source term from the rhs
79 solution.exec(),
80 {0, rhs.size()},
81 KOKKOS_LAMBDA(const localIdx i) { rhs[i] -= expSource[i] * vol[i]; }
82 );
83
84 auto solver = NeoN::la::Solver(solution.exec(), fvSolution);
85 solver.solve(ls, solution.internalVector());
86 }
87}
88
89} // namespace dsl
A class representing a dictionary that stores key-value pairs.
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls)
Vector< ValueType > explicitOperation(localIdx nCells) const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
void build(const Dictionary &input)
void solve(Expression &eqn, SolutionVectorType &sol, scalar t, scalar dt)
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:108
void solve(Expression< typename VectorType::ElementType > &exp, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution)
Definition solver.hpp:38
LinearSystem< ValueType, IndexType > createEmptyLinearSystem(const SparsityType &sparsity)
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
int32_t localIdx
Definition label.hpp:30
float scalar
Definition scalar.hpp:14
auto spans(Args &... fields)