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