NeoN
A framework for CFD software
Loading...
Searching...
No Matches
petsc.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#if NF_WITH_PETSC
8
9#include <Kokkos_Core.hpp>
10#include <petscvec_kokkos.hpp>
11#include <petscmat.h>
12#include <petscksp.h>
13
15#include "NeoN/fields/field.hpp"
21// #include "NeoN/core/database/petscSolverContextCollection.hpp"
22
24
25namespace NeoN::la::petsc
26{
27
28// template<typename ValueType>
29
30class petscSolver : public SolverFactory::template Register<petscSolver>
31{
32
33 using Base = SolverFactory::template Register<petscSolver>;
34
35private:
36
37 Dictionary solverDict_;
38 // Mat Amat_;
39 // KSP ksp_;
40
41 // Vec sol_, rhs_;
42
43 // NeoN::la::petscSolverContext::petscSolverContext<scalar> petsctx_;
44
45 /*
46 NeoN::Database& db_;
47 std::string eqnName_;
48 */
49
50public:
51
52 petscSolver(Executor exec, Dictionary solverDict) : Base(exec), solverDict_(solverDict)
53 //, Amat_(nullptr), ksp_(nullptr), sol_(nullptr),
54 // rhs_(nullptr),petsctx_(exec_)
55 //, db_(db), eqnName_(eqnName)
56 {}
57
58 //- Destructor
59 virtual ~petscSolver()
60 {
61 // MatDestroy(&Amat_);
62 // KSPDestroy(&ksp_);
63 // VecDestroy(&sol_);
64 // VecDestroy(&rhs_);
65 }
66
67 static std::string name() { return "Petsc"; }
68
69 static std::string doc() { return "TBD"; }
70
71 static std::string schema() { return "none"; }
72
73 // TODO why use a smart pointer here?
74 virtual std::unique_ptr<SolverFactory> clone() const final
75 {
76 NF_ERROR_EXIT("Not implemented");
77 return {};
78 }
79
80
81 virtual SolverStats solve(const LinearSystem<scalar>& sys, Vector<scalar>& x) const final
82 {
83
84 Mat Amat;
85 KSP ksp;
86
87 Vec sol, rhs;
88
89 NeoN::la::petscSolverContext::petscSolverContext<scalar> petsctx(exec_);
90
91 sizeType nrows = sys.rhs().size();
92
93 petsctx.initialize(sys);
94
95 Amat = petsctx.AMat();
96 rhs = petsctx.rhs();
97 sol = petsctx.sol();
98 ksp = petsctx.ksp();
99
100 VecSetValuesCOO(rhs, sys.rhs().data(), ADD_VALUES);
101 MatSetValuesCOO(Amat, sys.matrix().values().data(), ADD_VALUES);
102
103 // MatView(Amat_, PETSC_VIEWER_STDOUT_WORLD);
104 // VecView(rhs_, PETSC_VIEWER_STDOUT_WORLD);
105
106
107 // KSPCreate(PETSC_COMM_WORLD, &ksp_);
108 KSPSetOperators(ksp, Amat, Amat);
109 // KSPSetTolerances(ksp, 1.e-9, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
110 // KSPSetFromOptions(ksp_);
111 // KSPSetUp(ksp);
112
113
114 PetscCallVoid(KSPSolve(ksp, rhs, sol));
115
116 auto numIter = 0;
117 KSPGetIterationNumber(ksp, &numIter);
118
119
120 PetscScalar* x_help = static_cast<PetscScalar*>(x.data());
121 VecGetArray(sol, &x_help);
122
123 Vector<scalar> x2(x.exec(), static_cast<scalar*>(x_help), nrows, x.exec());
124 x = x2;
125
126 // TODO residual norms are missing
127 return {numIter, 0.0, 0.0, 0.0};
128 }
129};
130
131}
132
133#endif
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:90
std::optional< la::SolverStats > solve(Expression< typename VectorType::ElementType, IndexType > &exp, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution, std::vector< const PostAssemblyBase< typename VectorType::ElementType, IndexType > * > p={})
Definition solver.hpp:98
float scalar
Definition scalar.hpp:17
std::size_t sizeType
Definition label.hpp:56
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.