PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
interface.cpp
Go to the documentation of this file.
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6// Define before including interpolate.hpp so the extern-template suppression
7// block is skipped and this TU can provide explicit instantiations.
8#define PACMAN_FE_ETI_COMPILATION
9
10#include "interface.hpp"
11
12#include <Kokkos_Core.hpp>
13#include <Kokkos_Profiling_ScopedRegion.hpp>
14#include <stdexcept>
15#include <string>
16#include <unordered_map>
17
18#include "common/concepts.hpp"
19#include "common/transfer.hxx"
20#include "interpolate.hpp"
21
22// ---------------------------------------------------------------------------
23// Internal helpers (anonymous namespace)
24// ---------------------------------------------------------------------------
25
26namespace {
27
28template <typename ExecSpace, PACMAN::int_t Dim>
30RunInterpolate(PACMAN::method_t method, PACMAN::coordinates_t *pSourcePoints,
31 PACMAN::int_t nSourcePoints, PACMAN::fp_t *pSourceValues,
32 PACMAN::int_t *pConnVal, PACMAN::int_t connValSize,
33 PACMAN::offset_t *pConnOff, PACMAN::int_t connOffSize,
34 PACMAN::cell_t *pCellTypes, PACMAN::coordinates_t *pTargetPoints,
35 PACMAN::int_t nTargetPoints, bool fortranIndexing) {
36 PACMAN::Transfer<ExecSpace, Dim> transfer(method);
37 PACMAN::SetupTransferClass(transfer, nSourcePoints, connValSize, connOffSize,
38 nTargetPoints, pSourcePoints, pSourceValues,
39 pConnVal, pConnOff, pCellTypes, pTargetPoints, fortranIndexing);
40 PACMAN::Interpolate(transfer);
41
43 result.targetValues.resize(nTargetPoints);
44 result.targetStatus.resize(nTargetPoints);
45
46 auto unmanaged_host_tv =
47 Kokkos::View<PACMAN::fp_t *,
48 Kokkos::DefaultHostExecutionSpace::memory_space,
49 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
50 result.targetValues.data(), nTargetPoints);
51 auto unmanaged_host_ts = Kokkos::View<
52 PACMAN::TransferStatus *, Kokkos::DefaultHostExecutionSpace::memory_space,
53 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
54 reinterpret_cast<PACMAN::TransferStatus *>(result.targetStatus.data()),
55 nTargetPoints);
56
57 Kokkos::deep_copy(unmanaged_host_tv, transfer.targetValues);
58 Kokkos::deep_copy(unmanaged_host_ts, transfer.targetStatus);
59 return result;
60}
61
62template <PACMAN::int_t Dim>
64Dispatch(unsigned char execSpace, PACMAN::method_t method,
65 PACMAN::coordinates_t *pSourcePoints, PACMAN::int_t nSourcePoints,
66 PACMAN::fp_t *pSourceValues, PACMAN::int_t *pConnVal,
67 PACMAN::int_t connValSize, PACMAN::offset_t *pConnOff,
68 PACMAN::int_t connOffSize, PACMAN::cell_t *pCellTypes,
69 PACMAN::coordinates_t *pTargetPoints, PACMAN::int_t nTargetPoints, bool fortranIndexing) {
70 return std::visit(
71 [&](auto execSpaceObj) {
72 return RunInterpolate<decltype(execSpaceObj), Dim>(
73 method, pSourcePoints, nSourcePoints, pSourceValues, pConnVal,
74 connValSize, pConnOff, connOffSize, pCellTypes, pTargetPoints,
75 nTargetPoints, fortranIndexing);
76 },
77 PACMAN::MakeExecSpace(execSpace));
78}
79
80} // anonymous namespace
81
82// ---------------------------------------------------------------------------
83// Public API implementation
84// ---------------------------------------------------------------------------
85
86namespace PACMAN {
87
88FeInterpolateResult
91 fp_t *sourceValues, int_t *connVal, int_t connValSize,
94 const std::string _region_name = "PACMAN::fe_interpolate";
95 const Kokkos::Profiling::ScopedRegion region(_region_name);
96
97 if (sourcePoints == nullptr && nSourcePoints > 0)
98 throw std::invalid_argument("sourcePoints is null but nSourcePoints > 0");
99 if (targetPoints == nullptr && nTargetPoints > 0)
100 throw std::invalid_argument("targetPoints is null but nTargetPoints > 0");
101 if (sourceValues == nullptr && nSourcePoints > 0)
102 throw std::invalid_argument("sourceValues is null but nSourcePoints > 0");
103
104 switch (spaceDimension) {
105 case 1:
107 sourceValues, connVal, connValSize, connOff, connOffSize,
109 case 2:
111 sourceValues, connVal, connValSize, connOff, connOffSize,
113 case 3:
115 sourceValues, connVal, connValSize, connOff, connOffSize,
117 default:
118 throw std::runtime_error(
119 "The dimension of the points can only be: 1, 2 or 3.\n");
120 }
121}
122
124 int_t n) {
125 static const std::unordered_map<int_t, CellType> vtk_to_pacman = {
126 // 0D
128 // 1D
131 // 2D
136 // 3D
145 };
146 for (int_t i = 0; i < n; ++i) {
147 auto it = vtk_to_pacman.find(vtkTypes[i]);
148 if (it == vtk_to_pacman.end())
149 throw std::runtime_error("Unsupported VTK cell type: " +
150 std::to_string(vtkTypes[i]));
151 pacmanTypes[i] = static_cast<cell_t>(it->second);
152 }
153}
154
156 static const std::unordered_map<int_t, int_t> vtk_dim = {
157 // 0D
158 {1, 0},
159 // 1D
160 {3, 1},
161 {21, 1},
162 // 2D
163 {5, 2},
164 {9, 2},
165 {22, 2},
166 {23, 2},
167 // 3D
168 {10, 3},
169 {12, 3},
170 {13, 3},
171 {14, 3},
172 {24, 3},
173 {25, 3},
174 {26, 3},
175 {27, 3},
176 };
177 auto it = vtk_dim.find(vtkCellType);
178 if (it == vtk_dim.end())
179 throw std::runtime_error("Unsupported VTK cell type: " +
180 std::to_string(vtkCellType));
181 return it->second;
182}
183
184} // namespace PACMAN
Generic C++ entry point for PACMAN interpolation methods.
double fp_t
Definition types.hpp:15
double coordinates_t
Definition types.hpp:16
FeInterpolateResult fe_interpolate(int_t spaceDimension, unsigned char execSpace, method_t method, coordinates_t *sourcePoints, int_t nSourcePoints, fp_t *sourceValues, int_t *connVal, int_t connValSize, offset_t *connOff, int_t connOffSize, cell_t *cellTypes, coordinates_t *targetPoints, int_t nTargetPoints, bool fortranIndexing)
C++ interface for finite-elements interpolation.
Definition interface.cpp:89
TransferStatus
Definition types.hpp:39
int32_t cell_t
Definition types.hpp:25
void vtk_to_pacman_cell_type(const int_t *vtkTypes, cell_t *pacmanTypes, int_t n)
Convert an array of VTK cell type IDs to PACMAN CellType enum values.
int32_t offset_t
Definition types.hpp:21
static AvailableExecSpaces MakeExecSpace(const unsigned char s)
Converts an execution-space selector byte to a variant type.
void Interpolate(Transfer< ExecSpace, Dim > &transfer)
Generic C++ interface to PACMAN interpolation methods.
int32_t method_t
Definition types.hpp:24
int32_t int_t
Definition types.hpp:18
int_t vtk_cell_dim(int_t vtkCellType)
Return the topological dimension for a VTK cell type identifier.
Result of a finite-elements interpolation call.
Definition interface.hpp:34
std::vector< int_t > targetStatus
TransferStatus code per target point (same underlying int_t as TransferStatus enum).
Definition interface.hpp:38
std::vector< fp_t > targetValues
Interpolated scalar value per target point.
Definition interface.hpp:36