ABLATE Source Documentation  0.12.36
rbf.hpp
1 #ifndef ABLATELIBRARY_RBF_HPP
2 #define ABLATELIBRARY_RBF_HPP
3 #include <petsc.h>
4 #include <petsc/private/hashmapi.h>
5 #include "domain/range.hpp" // For domain::Range
6 #include "domain/subDomain.hpp"
7 
8 #define __RBF_DEFAULT_POLYORDER 3
9 
10 namespace ablate::domain::rbf {
11 
12 class RBF {
13  private:
14  std::shared_ptr<ablate::domain::SubDomain> subDomain = nullptr;
15 
16  // Radial Basis Function type and parameters
17  const int polyOrder = 4;
18 
19  PetscInt nPoly = -1; // The number of polynomial components to include
20  PetscInt minNumberCells = -1; // Minimum number of cells-vertices needed to compute the RBF
21  PetscBool useCells = PETSC_FALSE; // Use vertices or edges/faces when computing neighbor cells/vertices
22  const bool returnNeighborVertices; // If it is true formulates the RBF based on vertices surrounding a cell, otherwise will use cells surrounding a cell
23 
24  // Information from the subDomain cell range
25  PetscInt cStart = 0, cEnd = 0; // The cell range
26  PetscInt *cellList; // List of the cells to compute over. Length of cEnd - cStart
27 
28  // Derivative data
29  const bool hasDerivatives;
30  PetscInt nDer = 0; // Number of derivative stencils which are pre-computed
31  PetscInt *dxyz = nullptr; // The derivatives which have been setup
32  PetscHMapI hash = nullptr; // Hash of the derivative
33  PetscInt *nStencil = nullptr; // Length of each stencil. Needed for both derivatives and interpolation.
34  PetscInt **stencilList = nullptr; // IDs of the points in the stencil. Needed for both derivatives and interpolation.
35  PetscReal **stencilWeights = nullptr; // Weights of the points in the stencil. Needed only for derivatives.
36  PetscReal **stencilXLocs = nullptr; // Locations wrt a cell center. Needed only for interpolation.
37 
38  // The derivative->key map for the hash
39  PetscInt derivativeKey(PetscInt dx, PetscInt dy, PetscInt dz) const { return (100 * dx + 10 * dy + dz); };
40 
41  // Setup the derivative stencil at a point. There is no need for anyone outside of RBF to call this
42  void SetupDerivativeStencils(PetscInt c);
43 
44  const bool hasInterpolation;
45  Mat *RBFMatrix = nullptr;
46 
47  // Compute the LU-decomposition of the augmented RBF matrix given a cell list.
48  void Matrix(const PetscInt c);
49 
50  void CheckField(const ablate::domain::Field *field); // Checks whether the field is SOL or AUX
51 
52  void FreeStencilData();
53 
54  protected:
55  PetscReal DistanceSquared(PetscInt dim, PetscReal x[], PetscReal y[]);
56  PetscReal DistanceSquared(PetscInt dim, PetscReal x[]);
57  void Loc3D(PetscInt dim, PetscReal xIn[], PetscReal x[3]);
58 
59  public:
60  explicit RBF(int polyOrder = 4, bool hasDerivatives = true, bool hasInterpolation = true, bool returnNeighborVertices = false);
61 
62  virtual ~RBF();
63 
65  void Initialize();
66  void Setup(std::shared_ptr<ablate::domain::SubDomain> subDomain);
67 
68  // Derivative stuff
75  void SetDerivatives(PetscInt nDer, PetscInt dx[], PetscInt dy[], PetscInt dz[], PetscBool useCellsLocal);
76 
82  void SetDerivatives(PetscInt nDer, PetscInt dx[], PetscInt dy[], PetscInt dz[]);
83 
87  void SetupDerivativeStencils(); // Setup all derivative stencils. Useful if someone wants to remove setup cost when testing
88 
95  PetscReal EvalDer(const ablate::domain::Field *field, PetscInt c, PetscInt dx, PetscInt dy, PetscInt dz); // Evaluate a derivative
96 
105  PetscReal EvalDer(DM dm, Vec vec, const PetscInt fid, PetscInt c, PetscInt dx, PetscInt dy, PetscInt dz); // Evaluate a derivative
106 
107  // Interpolation stuff
108 
116  PetscReal Interpolate(const ablate::domain::Field *field, Vec f, const PetscInt c, PetscReal xEval[3]);
117 
124  PetscReal Interpolate(const ablate::domain::Field *field, Vec f, PetscReal xEval[3]);
125 
131  [[nodiscard]] inline PetscReal Interpolate(const ablate::domain::Field *field, PetscReal xEval[3]) { return RBF::Interpolate(field, RBF::subDomain->GetVec(*field), xEval); }
132 
133  // These will be overwritten in the derived classes
140  virtual PetscReal RBFVal(PetscInt dim, PetscReal x[], PetscReal y[]) = 0; // Radial function evaluated using the distance between two points
141 
148  virtual PetscReal RBFDer(PetscInt dim, PetscReal x[], PetscInt dx, PetscInt dy, PetscInt dz) = 0; // Derivative of the radial function assuming that the center point is at zero.
149 
153  virtual std::string_view type() const = 0;
154 
158  inline PetscInt GetDimensions() { return RBF::subDomain->GetDimensions(); }
159 };
160 
161 } // namespace ablate::domain::rbf
162 
163 #endif // ABLATELIBRARY_RBF_HPP
Definition: rbf.hpp:12
void Initialize()
Definition: rbf.cpp:701
void SetDerivatives(PetscInt nDer, PetscInt dx[], PetscInt dy[], PetscInt dz[], PetscBool useCellsLocal)
Definition: rbf.cpp:264
virtual std::string_view type() const =0
PetscReal Interpolate(const ablate::domain::Field *field, Vec f, const PetscInt c, PetscReal xEval[3])
Definition: rbf.cpp:465
PetscReal EvalDer(const ablate::domain::Field *field, PetscInt c, PetscInt dx, PetscInt dy, PetscInt dz)
Definition: rbf.cpp:405
virtual PetscReal RBFVal(PetscInt dim, PetscReal x[], PetscReal y[])=0
PetscReal Interpolate(const ablate::domain::Field *field, PetscReal xEval[3])
Definition: rbf.hpp:131
virtual PetscReal RBFDer(PetscInt dim, PetscReal x[], PetscInt dx, PetscInt dy, PetscInt dz)=0
void SetupDerivativeStencils()
Definition: rbf.cpp:399
Definition: field.hpp:17