DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
RadialBasisFunction.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "DNDS/Defines.hpp"
4#include "Geometric.hpp"
7
9{
21
22 // NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
25 {
27 {RBFKernelType::Distance, "Distance"},
28 {RBFKernelType::DistanceA1, "DistanceA1"},
29 {RBFKernelType::InversedDistanceA1, "InversedDistanceA1"},
30 {RBFKernelType::InversedDistanceA1Compact, "InversedDistanceA1Compact"},
31 {RBFKernelType::Gaussian, "Gaussian"},
32 {RBFKernelType::CPC2, "CPC2"},
33 {RBFKernelType::CPC0, "CPC0"},
34 })
35 // NOLINTEND(cppcoreguidelines-avoid-c-arrays)
36 inline bool KernelIsCompact(RBFKernelType t)
37 {
38 return t == CPC0 || t == CPC2 || t == InversedDistanceA1Compact;
39 }
40}
41
42namespace DNDS::Geom::RBF
43{
44
45 inline real
46 GetMaxRij(const tSmallCoords &cent, const tSmallCoords &xs)
47 {
48 MatrixXR RiXj;
49 RiXj.resize(cent.cols(), xs.cols());
50 for (int iC = 0; iC < cent.cols(); iC++)
51 {
52 RiXj(iC, EigenAll) = (xs.colwise() - cent(EigenAll, iC)).colwise().norm();
53 }
54 return RiXj.array().maxCoeff();
55 }
56
57 template <class TIn>
58 inline MatrixXR
59 FRBFBasis(TIn RiXj, RBFKernelType kernel)
60 {
61 using real = DNDS::real;
62 MatrixXR NiXj;
63 switch (kernel)
64 {
65 /*****/ // using (modified) distance Kernel
66 case Distance:
67 NiXj = RiXj.array();
68 break;
69 case DistanceA1:
70 NiXj = 1 + RiXj.array();
71 break;
73 NiXj = (1 + RiXj.array()).inverse();
74 break;
76 NiXj = (0.2 + RiXj.array()).inverse() - 1 / 1.2;
77 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
78 break;
79 case CPC2:
80 /*****/ // using CPC2 Kernel
81 NiXj = (1 - RiXj.array()).square().square() * (RiXj.array() * 4 + 1);
82 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
83 break;
84 case CPC0:
85 /*****/ // using CPC0 Kernel
86 NiXj = (1 - RiXj.array()).square();
87 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
88 break;
89 case Gaussian:
90 /*****/ // using Gaussian kernel
91 NiXj = (-(RiXj.array().square())).exp();
92 break;
93 default:
94 DNDS_assert(false);
95 }
96 return NiXj;
97 }
98
99 template <class Tcent, class Tx>
100 inline MatrixXR // redurn Ni at Xj
101 RBFCPC2(Tcent cent, Tx xs, real R, RBFKernelType kernel = Gaussian)
102 {
103 MatrixXR RiXj;
104 RiXj.resize(cent.cols(), xs.cols());
105 for (int iC = 0; iC < cent.cols(); iC++)
106 {
107 RiXj(iC, EigenAll) = (xs.colwise() - cent(EigenAll, iC)).colwise().norm() * (1. / R);
108 }
109 // MatrixXR NiXj = (1 + RiXj.array().square()).inverse();
110 return FRBFBasis(RiXj, kernel);
111 }
112
113 template <class TF>
114 inline MatrixXR
116 {
117 MatrixXR PT;
118 PT.resize(4, xs.cols());
119 PT(0, EigenAll).setConstant(1);
120 PT({1, 2, 3}, EigenAll) = xs;
121 MatrixXR M = RBFCPC2(xs, xs, R, kernel);
122 MatrixXR A;
123 A.setZero(xs.cols() + 4, xs.cols() + 4);
124 A.topLeftCorner(xs.cols(), xs.cols()) = M;
125 A.bottomLeftCorner(4, xs.cols()) = PT;
126 A.topRightCorner(xs.cols(), 4) = PT.transpose();
127 MatrixXR RHS;
128 RHS.setZero(xs.cols() + 4, fs.cols());
129 DNDS_assert(fs.rows() == xs.cols());
130 RHS.topRows(xs.cols()) = fs;
131 auto LDLT = A.ldlt();
132 MatrixXR ret = LDLT.solve(RHS);
133 return ret;
134 }
135
136 template <class TF>
137 inline MatrixXR
139 {
140 MatrixXR M = RBFCPC2(xs, xs, R, kernel);
141 MatrixXR ret;
142 ret.setZero(xs.cols() + 4, fs.cols());
143 auto LDLTm = M.ldlt();
144 ret.topRows(xs.cols()) = LDLTm.solve(fs);
145
146 return ret;
147 }
148}
Extended enum-to-JSON serialization macro that also exposes allowed string values for JSON Schema gen...
#define DNDS_DEFINE_ENUM_JSON(EnumType_,...)
Define JSON serialization for an enum AND expose its allowed string values.
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_assert(expr)
Debug-only assertion (compiled out when DNDS_NDEBUG is defined). Prints the expression + file/line + ...
Definition Errors.hpp:112
JSON-to-Eigen conversion utilities and nlohmann_json helper macros.
MatrixXR RBFCPC2(Tcent cent, Tx xs, real R, RBFKernelType kernel=Gaussian)
MatrixXR FRBFBasis(TIn RiXj, RBFKernelType kernel)
MatrixXR RBFInterpolateSolveCoefs(const tSmallCoords &xs, const TF fs, real R, RBFKernelType kernel=Gaussian)
MatrixXR RBFInterpolateSolveCoefsNoPoly(const tSmallCoords &xs, const TF fs, real R, RBFKernelType kernel=Gaussian)
real GetMaxRij(const tSmallCoords &cent, const tSmallCoords &xs)
Eigen::Matrix< real, 3, Eigen::Dynamic > tSmallCoords
Definition Geometric.hpp:50
Eigen::Matrix< real, Eigen::Dynamic, Eigen::Dynamic > MatrixXR
Column-major dynamic Eigen matrix of reals (default layout).
Definition Defines.hpp:210
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:110