NeoN
A framework for CFD software
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
11#include "NeoN/fields/field.hpp"
13#include "NeoN/core/input.hpp"
17
21
22
23namespace NeoN::dsl
24{
25
26/* @brief solve an expression
27 *
28 * @param exp - Expression which is to be solved/updated.
29 * @param solution - Solution field, where the solution will be 'written to'.
30 * @param t - the time at the start of the time step.
31 * @param dt - time step for the temporal integration
32 * @param fvSchemes - Dictionary containing spatial operator and time integration properties
33 * @param fvSolution - Dictionary containing linear solver properties
34 */
35template<typename VectorType>
36void solve(
38 VectorType& solution,
39 scalar t,
40 scalar dt,
41 const Dictionary& fvSchemes,
42 const Dictionary& fvSolution
43)
44{
45 // TODO:
46 if (exp.temporalOperators().size() == 0 && exp.spatialOperators().size() == 0)
47 {
48 NF_ERROR_EXIT("No temporal or implicit terms to solve.");
49 }
50 exp.read(fvSchemes);
51 if (exp.temporalOperators().size() > 0)
52 {
53 // integrate equations in time
55 fvSchemes.subDict("ddtSchemes"), fvSolution
56 );
57 timeIntegrator.solve(exp, solution, t, dt);
58 }
59 else
60 {
61 // solve sparse matrix system
62 using ValueType = typename VectorType::ElementType;
63
64 auto sparsity = la::SparsityPattern(solution.mesh());
65 auto ls = la::createEmptyLinearSystem<ValueType, localIdx>(solution.mesh(), sparsity);
66
67 exp.implicitOperation(ls);
68 auto expTmp = exp.explicitOperation(solution.mesh().nCells());
69
70 auto [vol, expSource, rhs] = views(solution.mesh().cellVolumes(), expTmp, ls.rhs());
71
72 // subtract the explicit source term from the rhs
74 solution.exec(),
75 {0, rhs.size()},
76 KOKKOS_LAMBDA(const localIdx i) { rhs[i] -= expSource[i] * vol[i]; }
77 );
78
79 auto solver = la::Solver(solution.exec(), fvSolution);
80 solver.solve(ls, solution.internalVector());
81 }
82}
83
84} // 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
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls)
Vector< ValueType > explicitOperation(localIdx nCells) const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
void read(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:36
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 views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:108