NeoN
WIP Prototype of a modern OpenFOAM core
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 virtual std::unique_ptr<SolverFactory> clone() const final
72 {
73 // FIXME
74 return {};
75 }
76
77
78 virtual void solve(const LinearSystem<scalar, localIdx>& sys, Vector<scalar>& x) const final
79 {
80
81 Mat Amat;
82 KSP ksp;
83
84 Vec sol, rhs;
85
86 NeoN::la::petscSolverContext::petscSolverContext<scalar> petsctx(exec_);
87
88 std::size_t nrows = sys.rhs().size();
89
90 petsctx.initialize(sys);
91
92 Amat = petsctx.AMat();
93 rhs = petsctx.rhs();
94 sol = petsctx.sol();
95 ksp = petsctx.ksp();
96
97 VecSetValuesCOO(rhs, sys.rhs().data(), ADD_VALUES);
98 MatSetValuesCOO(Amat, sys.matrix().values().data(), ADD_VALUES);
99
100 // MatView(Amat_, PETSC_VIEWER_STDOUT_WORLD);
101 // VecView(rhs_, PETSC_VIEWER_STDOUT_WORLD);
102
103
104 // KSPCreate(PETSC_COMM_WORLD, &ksp_);
105 KSPSetOperators(ksp, Amat, Amat);
106 // KSPSetTolerances(ksp, 1.e-9, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT);
107 // KSPSetFromOptions(ksp_);
108 // KSPSetUp(ksp);
109
110
111 PetscCallVoid(KSPSolve(ksp, rhs, sol));
112
113
114 PetscScalar* x_help = static_cast<PetscScalar*>(x.data());
115 VecGetArray(sol, &x_help);
116
117 NeoN::Vector<NeoN::scalar> x2(x.exec(), static_cast<scalar*>(x_help), nrows, x.exec());
118 x = x2;
119 }
120};
121
122}
123
124#endif
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
A template class for registering derived classes with a base class.
void solve(Expression< typename VectorType::ElementType > &exp, VectorType &solution, scalar t, scalar dt, const Dictionary &fvSchemes, const Dictionary &fvSolution)
Definition solver.hpp:38
float scalar
Definition scalar.hpp:14
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.