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>
29void
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::int_t nComponents, PACMAN::fp_t *pTargetValues,
37 PACMAN::int_t *pTargetStatus) {
38 PACMAN::Transfer<ExecSpace, Dim> transfer(method);
39 PACMAN::SetupTransferClass(transfer, nSourcePoints, connValSize, connOffSize,
40 nTargetPoints, nComponents, pSourcePoints,
41 pSourceValues,
42 pConnVal, pConnOff, pCellTypes, pTargetPoints, fortranIndexing);
43 PACMAN::Interpolate(transfer);
44
45 auto unmanaged_host_ts = Kokkos::View<
46 PACMAN::TransferStatus *, Kokkos::DefaultHostExecutionSpace::memory_space,
47 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
48 reinterpret_cast<PACMAN::TransferStatus *>(pTargetStatus), nTargetPoints);
49
50 PACMAN::CopyToRowMajorHost(transfer.targetValues, pTargetValues);
51 Kokkos::deep_copy(unmanaged_host_ts, transfer.targetStatus);
52}
53
54template <PACMAN::int_t Dim>
55requires PACMAN::IsValidDim<Dim> void
56Dispatch(unsigned char execSpace, PACMAN::method_t method,
57 PACMAN::coordinates_t *pSourcePoints, PACMAN::int_t nSourcePoints,
58 PACMAN::fp_t *pSourceValues, PACMAN::int_t *pConnVal,
59 PACMAN::int_t connValSize, PACMAN::offset_t *pConnOff,
60 PACMAN::int_t connOffSize, PACMAN::cell_t *pCellTypes,
61 PACMAN::coordinates_t *pTargetPoints, PACMAN::int_t nTargetPoints,
62 bool fortranIndexing, PACMAN::int_t nComponents,
63 PACMAN::fp_t *pTargetValues, PACMAN::int_t *pTargetStatus) {
64 std::visit(
65 [&](auto execSpaceObj) {
66 RunInterpolate<decltype(execSpaceObj), Dim>(
67 method, pSourcePoints, nSourcePoints, pSourceValues, pConnVal,
68 connValSize, pConnOff, connOffSize, pCellTypes, pTargetPoints,
69 nTargetPoints, fortranIndexing, nComponents, pTargetValues,
70 pTargetStatus);
71 },
72 PACMAN::MakeExecSpace(execSpace));
73}
74
75} // anonymous namespace
76
77// ---------------------------------------------------------------------------
78// Public API implementation
79// ---------------------------------------------------------------------------
80
81namespace PACMAN {
82
83void
84fe_interpolate(int_t spaceDimension, unsigned char execSpace, method_t method,
85 coordinates_t *sourcePoints, int_t nSourcePoints,
86 fp_t *sourceValues, int_t *connVal, int_t connValSize,
87 offset_t *connOff, int_t connOffSize, cell_t *cellTypes,
88 coordinates_t *targetPoints, int_t nTargetPoints,
89 fp_t *targetValues, int_t *targetStatus, bool fortranIndexing,
90 int_t nComponents) {
91 const std::string _region_name = "PACMAN::fe_interpolate";
92 const Kokkos::Profiling::ScopedRegion region(_region_name);
93
94 // LCOV_EXCL_START
95 if (sourcePoints == nullptr && nSourcePoints > 0)
96 throw std::invalid_argument("sourcePoints is null but nSourcePoints > 0");
97 if (targetPoints == nullptr && nTargetPoints > 0)
98 throw std::invalid_argument("targetPoints is null but nTargetPoints > 0");
99 if (sourceValues == nullptr && nSourcePoints > 0)
100 throw std::invalid_argument("sourceValues is null but nSourcePoints > 0");
101 if (targetValues == nullptr && nTargetPoints > 0)
102 throw std::invalid_argument("targetValues is null but nTargetPoints > 0");
103 if (targetStatus == nullptr && nTargetPoints > 0)
104 throw std::invalid_argument("targetStatus is null but nTargetPoints > 0");
105 if (nComponents < 1)
106 throw std::invalid_argument("nComponents must be greater than zero");
107 // LCOV_EXCL_STOP
108
109 switch (spaceDimension) {
110 case 1:
111 return Dispatch<1>(execSpace, method, sourcePoints, nSourcePoints,
112 sourceValues, connVal, connValSize, connOff, connOffSize,
113 cellTypes, targetPoints, nTargetPoints, fortranIndexing,
114 nComponents, targetValues, targetStatus);
115 case 2:
116 return Dispatch<2>(execSpace, method, sourcePoints, nSourcePoints,
117 sourceValues, connVal, connValSize, connOff, connOffSize,
118 cellTypes, targetPoints, nTargetPoints, fortranIndexing,
119 nComponents, targetValues, targetStatus);
120 case 3:
121 return Dispatch<3>(execSpace, method, sourcePoints, nSourcePoints,
122 sourceValues, connVal, connValSize, connOff, connOffSize,
123 cellTypes, targetPoints, nTargetPoints, fortranIndexing,
124 nComponents, targetValues, targetStatus);
125 // LCOV_EXCL_START
126 default:
127 throw std::runtime_error(
128 "The dimension of the points can only be: 1, 2 or 3.\n");
129 // LCOV_EXCL_STOP
130 }
131}
132
133void vtk_to_pacman_cell_type(const int_t *vtkTypes, cell_t *pacmanTypes,
134 int_t n) {
135 static const std::unordered_map<int_t, CellType> vtk_to_pacman = {
136 // 0D
138 // 1D
141 // 2D
146 // 3D
155 };
156 for (int_t i = 0; i < n; ++i) {
157 auto it = vtk_to_pacman.find(vtkTypes[i]);
158 // LCOV_EXCL_START
159 if (it == vtk_to_pacman.end()) {
160 throw std::runtime_error("Unsupported VTK cell type: " +
161 std::to_string(vtkTypes[i]));
162 }
163 // LCOV_EXCL_STOP
164 pacmanTypes[i] = static_cast<cell_t>(it->second);
165 }
166}
167
168} // namespace PACMAN
Generic C++ entry point for PACMAN interpolation methods.
double fp_t
Definition types.hpp:15
double coordinates_t
Definition types.hpp:16
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.
void 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, fp_t *targetValues, int_t *targetStatus, bool fortranIndexing, int_t nComponents)
C++ interface for finite-elements interpolation.
Definition interface.cpp:84
int32_t method_t
Definition types.hpp:24
int32_t int_t
Definition types.hpp:18
@ VTK_QUADRATIC_HEXAHEDRON
Definition types.hpp:79
@ VTK_QUADRATIC_PYRAMID
Definition types.hpp:81
@ VTK_QUADRATIC_TRIANGLE
Definition types.hpp:71