NeoN
A framework for CFD software
Loading...
Searching...
No Matches
solver.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include "NeoN/core/input.hpp"
10
11namespace NeoN::la
12{
13
21
22/* @brief A helper to collect statistics of the solver */
24{
25 std::vector<SolverStatsEntry> entries;
26
28
29 SolverStats(size_t numIter, scalar initResNorm, scalar finalResNorm, scalar solveTime)
30 : entries({SolverStatsEntry {numIter, initResNorm, finalResNorm, solveTime}})
31 {}
32
34};
35
36/* @class SolverFactory
37**
38*/
40 public RuntimeSelectionFactory<SolverFactory, Parameters<const Executor&, const Dictionary&>>
41{
42public:
43
44 static std::unique_ptr<SolverFactory> create(const Executor& exec, const Dictionary& dict)
45 {
46 auto key = dict.get<std::string>("solver");
47 SolverFactory::keyExistsOrError(key);
48 return SolverFactory::table().at(key)(exec, dict);
49 }
50
51 static std::string name() { return "SolverFactory"; }
52
53 SolverFactory(const Executor& exec) : exec_(exec) {};
54
55 virtual SolverStats
57 const = 0;
58
59 virtual SolverStats
61
62 virtual SolverStats
64 const
65 {
66 NF_THROW("solve(scalar matrix, Vec3 rhs) not implemented for this solver");
67 }
68
69#ifdef NF_WITH_MPI_SUPPORT
70 virtual SolverStats
72 const
73 {
74 NF_THROW("solveDist not implemented for this solver");
75 }
76
77 virtual SolverStats
78 solveDist(const LinearSystem<Vec3, Vec3, CSRMatrix<Vec3, localIdx>>&, Vector<Vec3>&) const
79 {
80 NF_THROW("solveDist not implemented for this solver");
81 }
82
83 virtual SolverStats
84 solveDist(const LinearSystem<scalar, Vec3, CSRMatrix<scalar, localIdx>, COOMatrix<scalar, localIdx>>&, Vector<Vec3>&)
85 const
86 {
87 NF_THROW("solveDist(scalar matrix, Vec3 rhs) not implemented for this solver");
88 }
89#endif
90
91 // Pure virtual function for cloning
92 virtual std::unique_ptr<SolverFactory> clone() const = 0;
93
94protected:
95
97};
98
99class Solver
100{
101
102public:
103
104 Solver(const Solver& solver)
105 : exec_(solver.exec_), solverInstance_(solver.solverInstance_->clone()) {};
106
107 Solver(Solver&& solver)
108 : exec_(solver.exec_), solverInstance_(std::move(solver.solverInstance_)) {};
109
110 Solver(const Executor& exec, std::unique_ptr<SolverFactory> solverInstance)
111 : exec_(exec), solverInstance_(std::move(solverInstance)) {};
112
113 Solver(const Executor& exec, const Dictionary& dict)
114 : exec_(exec), solverInstance_(SolverFactory::create(exec, dict)) {};
115
118 ) const
119 {
120#ifdef NF_WITH_MPI_SUPPORT
121 if (!ls.commPattern().sendCounts.empty()) return solverInstance_->solveDist(ls, field);
122#endif
123 return solverInstance_->solve(ls, field);
124 }
125
128 {
129#ifdef NF_WITH_MPI_SUPPORT
130 if (!ls.commPattern().sendCounts.empty()) return solverInstance_->solveDist(ls, field);
131#endif
132 return solverInstance_->solve(ls, field);
133 }
134
140 ls,
141 Vector<Vec3>& field
142 ) const
143 {
144#ifdef NF_WITH_MPI_SUPPORT
145 if (!ls.commPattern().sendCounts.empty()) return solverInstance_->solveDist(ls, field);
146#endif
147 return solverInstance_->solve(ls, field);
148 }
149
150private:
151
152 const Executor exec_;
153 std::unique_ptr<SolverFactory> solverInstance_;
154};
155
156}
A class representing a dictionary that stores key-value pairs.
T & get(const std::string &key)
Retrieves the value associated with the given key, casting it to the specified type.
A factory class for runtime selection of derived classes.
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:27
A class representing a linear system of equations.
Sparse matrix class with compact storage by row (CSR) format.
Definition matrix.hpp:71
static std::string name()
Definition solver.hpp:51
virtual SolverStats solve(const LinearSystem< scalar, Vec3, CSRMatrix< scalar, localIdx >, COOMatrix< scalar, localIdx > > &, Vector< Vec3 > &) const
Definition solver.hpp:63
virtual std::unique_ptr< SolverFactory > clone() const =0
virtual SolverStats solve(const LinearSystem< scalar, scalar, CSRMatrix< scalar, localIdx > > &, Vector< scalar > &) const =0
virtual SolverStats solve(const LinearSystem< Vec3, Vec3, CSRMatrix< Vec3, localIdx > > &, Vector< Vec3 > &) const =0
SolverFactory(const Executor &exec)
Definition solver.hpp:53
static std::unique_ptr< SolverFactory > create(const Executor &exec, const Dictionary &dict)
Definition solver.hpp:44
const Executor exec_
Definition solver.hpp:96
Solver(const Solver &solver)
Definition solver.hpp:104
Solver(const Executor &exec, std::unique_ptr< SolverFactory > solverInstance)
Definition solver.hpp:110
Solver(const Executor &exec, const Dictionary &dict)
Definition solver.hpp:113
SolverStats solve(const LinearSystem< scalar, Vec3, CSRMatrix< scalar, localIdx >, COOMatrix< scalar, localIdx > > &ls, Vector< Vec3 > &field) const
Solve a system with a scalar matrix and Vec3 right-hand side (segregated vector solve)....
Definition solver.hpp:138
Solver(Solver &&solver)
Definition solver.hpp:107
SolverStats solve(const LinearSystem< Vec3, Vec3, CSRMatrix< Vec3, localIdx > > &ls, Vector< Vec3 > &field) const
Definition solver.hpp:127
SolverStats solve(const LinearSystem< scalar, scalar, CSRMatrix< scalar, localIdx > > &ls, Vector< scalar > &field) const
Definition solver.hpp:116
#define NF_THROW(message)
Macro for throwing a NeoNException with the specified error message.
Definition error.hpp:103
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:20
float scalar
Definition scalar.hpp:17
SolverStats(size_t numIter, scalar initResNorm, scalar finalResNorm, scalar solveTime)
Definition solver.hpp:29
SolverStats(SolverStatsEntry entry)
Definition solver.hpp:33
std::vector< SolverStatsEntry > entries
Definition solver.hpp:25