NeoN
A framework for CFD software
Loading...
Searching...
No Matches
solver.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 <iostream>
8#include <memory>
9#include <type_traits>
10#include <utility>
11#include <concepts>
12
13#include "NeoN/fields/field.hpp"
15#include "NeoN/core/input.hpp"
19
23
24
25namespace NeoN::dsl
26{
27
28namespace detail
29{
30template<typename VectorType>
33 const la::SparsityPattern& sp,
35 VectorType& solution,
36 scalar t,
37 scalar dt,
38 const Dictionary& fvSchemes,
39 const Dictionary& fvSolution,
41)
42{
43 exp.read(fvSchemes);
44 exp.assemble(t, dt, sp, ls, ps);
45
46 // TODO move that to expression explicit operation or
47 // into functor ?
48 // subtract the explicit source term from the rhs
49 auto expTmp = exp.explicitOperation(solution.mesh().nCells());
50 auto [vol, expSource, rhs] = views(solution.mesh().cellVolumes(), expTmp, ls.rhs());
52 solution.exec(),
53 {0, rhs.size()},
54 KOKKOS_LAMBDA(const localIdx i) { rhs[i] -= expSource[i] * vol[i]; }
55 );
56
57 auto solver = la::Solver(solution.exec(), fvSolution);
58 fence(solution.exec());
59 return solver.solve(ls, solution.internalVector());
60}
61
62template<typename VectorType>
65 VectorType& solution,
66 scalar t,
67 scalar dt,
68 const Dictionary& fvSolution,
70)
71{
72 auto [sparsity, ls] = exp.assemble(solution.mesh(), t, dt, ps);
73
74 // TODO move that to expression explicit operation or
75 // into functor ?
76 // subtract the explicit source term from the rhs
77 auto expTmp = exp.explicitOperation(solution.mesh().nCells());
78 auto [vol, expSource, rhs] = views(solution.mesh().cellVolumes(), expTmp, ls.rhs());
80 solution.exec(),
81 {0, rhs.size()},
82 KOKKOS_LAMBDA(const localIdx i) { rhs[i] -= expSource[i] * vol[i]; }
83 );
84
85 auto solver = la::Solver(solution.exec(), fvSolution);
86 fence(solution.exec());
87 return solver.solve(ls, solution.internalVector());
88}
89}
90
91/* @brief solve an expression
92 *
93 * @param exp - Expression which is to be solved/updated.
94 * @param solution - Solution field, where the solution will be 'written to'.
95 * @param t - the time at the start of the time step.
96 * @param dt - time step for the temporal integration
97 * @param fvSchemes - Dictionary containing spatial operator and time integration properties
98 * @param fvSolution - Dictionary containing linear solver properties
99 * @param p - A chainable functor that performs manipulations on the assembled system
100 */
101template<typename VectorType>
104 VectorType& solution,
105 scalar t,
106 scalar dt,
107 const Dictionary& fvSchemes,
108 const Dictionary& fvSolution,
110)
111{
112 if (exp.temporalOperators().size() == 0 && exp.spatialOperators().size() == 0)
113 {
114 NF_ERROR_EXIT("No temporal or implicit terms to solve.");
115 }
116 exp.read(fvSchemes);
117 auto integrator =
118 timeIntegration::TimeIntegration<VectorType>(fvSchemes.subDict("ddtSchemes"), fvSolution);
119
120 if (exp.temporalOperators().size() > 0 && integrator.explicitIntegration())
121 {
122 // integrate equations in time
123 integrator.solve(exp, solution, t, dt);
124 return {.numIter = -1, .initResNorm = 0, .finalResNorm = 0, .solveTime = 0};
125 }
126 else
127 {
128 return detail::iterativeSolveImpl(exp, solution, t, dt, fvSolution, p);
129 }
130}
131
132} // namespace dsl
A class representing a dictionary that stores key-value pairs.
Dictionary & subDict(const std::string &key)
Retrieves a sub-dictionary associated with the given key.
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
Vector< ValueType > explicitOperation(localIdx nCells) const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
std::tuple< la::SparsityPattern, la::LinearSystem< ValueType, localIdx > > assemble(const UnstructuredMesh &mesh, scalar t, scalar dt, std::span< const PostAssemblyBase< ValueType > > ps={}) const
void read(const Dictionary &input)
A class representing a linear system of equations.
Vector< ValueType > & rhs()
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:110
la::SolverStats iterativeSolveImpl(Expression< typename VectorType::ElementType > &exp, const la::SparsityPattern &sp, la::LinearSystem< typename VectorType::ElementType, localIdx > &ls, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution, std::vector< PostAssemblyBase< typename VectorType::ElementType > > ps)
Definition solver.hpp:31
la::SolverStats solve(Expression< typename VectorType::ElementType > &exp, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution, std::vector< PostAssemblyBase< typename VectorType::ElementType > > p={})
Definition solver.hpp:102
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
void fence(const Executor &exec)
Definition executor.hpp:21
int32_t localIdx
Definition label.hpp:32
float scalar
Definition scalar.hpp:16
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:110