DNDSR 0.1.0.dev1+gcd065ad
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"
5#include "DNDS/JsonUtil.hpp"
6#include "DNDS/ConfigEnum.hpp"
7
9{
21
24 {
26 {RBFKernelType::Distance, "Distance"},
27 {RBFKernelType::DistanceA1, "DistanceA1"},
28 {RBFKernelType::InversedDistanceA1, "InversedDistanceA1"},
29 {RBFKernelType::InversedDistanceA1Compact, "InversedDistanceA1Compact"},
30 {RBFKernelType::Gaussian, "Gaussian"},
31 {RBFKernelType::CPC2, "CPC2"},
32 {RBFKernelType::CPC0, "CPC0"},
33 })
34 inline bool KernelIsCompact(RBFKernelType t)
35 {
36 return t == CPC0 || t == CPC2 || t == InversedDistanceA1Compact;
37 }
38}
39
40namespace DNDS::Geom::RBF
41{
42
43 inline real
44 GetMaxRij(const tSmallCoords &cent, const tSmallCoords &xs)
45 {
46 MatrixXR RiXj;
47 RiXj.resize(cent.cols(), xs.cols());
48 for (int iC = 0; iC < cent.cols(); iC++)
49 {
50 RiXj(iC, EigenAll) = (xs.colwise() - cent(EigenAll, iC)).colwise().norm();
51 }
52 return RiXj.array().maxCoeff();
53 }
54
55 template <class TIn>
56 inline MatrixXR
57 FRBFBasis(TIn RiXj, RBFKernelType kernel)
58 {
59 using real = DNDS::real;
60 MatrixXR NiXj;
61 switch (kernel)
62 {
63 /*****/ // using (modified) distance Kernel
64 case Distance:
65 NiXj = RiXj.array();
66 break;
67 case DistanceA1:
68 NiXj = 1 + RiXj.array();
69 break;
71 NiXj = (1 + RiXj.array()).inverse();
72 break;
74 NiXj = (0.2 + RiXj.array()).inverse() - 1 / 1.2;
75 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
76 break;
77 case CPC2:
78 /*****/ // using CPC2 Kernel
79 NiXj = (1 - RiXj.array()).square().square() * (RiXj.array() * 4 + 1);
80 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
81 break;
82 case CPC0:
83 /*****/ // using CPC0 Kernel
84 NiXj = (1 - RiXj.array()).square();
85 NiXj.array() *= (RiXj.array() < 1.0).template cast<real>(); // this kernel is bounded
86 break;
87 case Gaussian:
88 /*****/ // using Gaussian kernel
89 NiXj = (-(RiXj.array().square())).exp();
90 break;
91 default:
92 DNDS_assert(false);
93 }
94 return NiXj;
95 }
96
97 template <class Tcent, class Tx>
98 inline MatrixXR // redurn Ni at Xj
99 RBFCPC2(Tcent cent, Tx xs, real R, RBFKernelType kernel = Gaussian)
100 {
101 MatrixXR RiXj;
102 RiXj.resize(cent.cols(), xs.cols());
103 for (int iC = 0; iC < cent.cols(); iC++)
104 {
105 RiXj(iC, EigenAll) = (xs.colwise() - cent(EigenAll, iC)).colwise().norm() * (1. / R);
106 }
107 // MatrixXR NiXj = (1 + RiXj.array().square()).inverse();
108 return FRBFBasis(RiXj, kernel);
109 }
110
111 template <class TF>
112 inline MatrixXR
114 {
115 MatrixXR PT;
116 PT.resize(4, xs.cols());
117 PT(0, EigenAll).setConstant(1);
118 PT({1, 2, 3}, EigenAll) = xs;
119 MatrixXR M = RBFCPC2(xs, xs, R, kernel);
120 MatrixXR A;
121 A.setZero(xs.cols() + 4, xs.cols() + 4);
122 A.topLeftCorner(xs.cols(), xs.cols()) = M;
123 A.bottomLeftCorner(4, xs.cols()) = PT;
124 A.topRightCorner(xs.cols(), 4) = PT.transpose();
125 MatrixXR RHS;
126 RHS.setZero(xs.cols() + 4, fs.cols());
127 DNDS_assert(fs.rows() == xs.cols());
128 RHS.topRows(xs.cols()) = fs;
129 auto LDLT = A.ldlt();
130 MatrixXR ret = LDLT.solve(RHS);
131 return ret;
132 }
133
134 template <class TF>
135 inline MatrixXR
137 {
138 MatrixXR M = RBFCPC2(xs, xs, R, kernel);
139 MatrixXR ret;
140 ret.setZero(xs.cols() + 4, fs.cols());
141 auto LDLTm = M.ldlt();
142 ret.topRows(xs.cols()) = LDLTm.solve(fs);
143
144 return ret;
145 }
146}
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:108
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:205
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105