PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
fortran_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
15#include "fortran_interface.h"
16#include "interface.hpp"
17
18#include <Kokkos_Core.hpp>
19#include <cstddef>
20#include <iostream>
21#include <stdexcept>
22
23extern "C" {
24
25// ---------------------------------------------------------------------------
26// Kokkos lifecycle
27// ---------------------------------------------------------------------------
28
30 if (!Kokkos::is_initialized()) {
31 Kokkos::initialize();
32 }
33}
34
36 if (Kokkos::is_initialized()) {
37 Kokkos::finalize();
38 }
39}
40
42#if defined(KOKKOS_ENABLE_HIP)
43 return static_cast<int>(PACMAN::ExecSpaces::HIP);
44#elif defined(KOKKOS_ENABLE_CUDA)
45 return static_cast<int>(PACMAN::ExecSpaces::CUDA);
46#elif defined(KOKKOS_ENABLE_SYCL)
47 return static_cast<int>(PACMAN::ExecSpaces::SYCL);
48#elif defined(KOKKOS_ENABLE_OPENMP)
49 return static_cast<int>(PACMAN::ExecSpaces::OPENMP);
50#elif defined(KOKKOS_ENABLE_THREADS)
51 return static_cast<int>(PACMAN::ExecSpaces::THREADS);
52#elif defined(KOKKOS_ENABLE_SERIAL)
53 return static_cast<int>(PACMAN::ExecSpaces::SERIAL);
54#else
55 return -1;
56#endif
57}
58
59// ---------------------------------------------------------------------------
60// RBF-PUM interpolation
61// ---------------------------------------------------------------------------
62
63int pacman_rbf_interpolate_c(int spaceDimension, int execSpace, int rbfFunction,
64 const double *sourcePoints, int nSourcePoints,
65 const double *sourceValues,
66 const double *targetPoints, int nTargetPoints,
67 double *targetValues) {
68 try {
70 static_cast<PACMAN::int_t>(spaceDimension),
71 static_cast<unsigned char>(execSpace),
72 static_cast<unsigned char>(rbfFunction),
73 // The C++ API takes non-const pointers but does not modify the data.
74 const_cast<PACMAN::coordinates_t *>(sourcePoints),
75 static_cast<PACMAN::int_t>(nSourcePoints),
76 const_cast<PACMAN::fp_t *>(sourceValues),
77 const_cast<PACMAN::coordinates_t *>(targetPoints),
78 static_cast<PACMAN::int_t>(nTargetPoints));
79
80 for (int i = 0; i < nTargetPoints; ++i)
81 targetValues[i] = result.targetValues[static_cast<std::size_t>(i)];
82
83 return 0;
84 } catch (const std::exception &e) {
85 std::cerr << "pacman_rbf_interpolate_c: " << e.what() << "\n";
86 return -1;
87 } catch (...) {
88 std::cerr << "pacman_rbf_interpolate_c: unknown exception\n";
89 return -2;
90 }
91}
92
93// ---------------------------------------------------------------------------
94// FE interpolation
95// ---------------------------------------------------------------------------
96
97int pacman_fe_interpolate_c(int spaceDimension, int execSpace, int method,
98 const double *sourcePoints, int nSourcePoints,
99 const double *sourceValues, const int *connVal,
100 int connValSize, const int *connOff,
101 int connOffSize, const int *cellTypes,
102 const double *targetPoints, int nTargetPoints,
103 double *targetValues, int *targetStatus) {
104 try {
106 static_cast<PACMAN::int_t>(spaceDimension),
107 static_cast<unsigned char>(execSpace),
108 static_cast<PACMAN::method_t>(method),
109 const_cast<PACMAN::coordinates_t *>(sourcePoints),
110 static_cast<PACMAN::int_t>(nSourcePoints),
111 const_cast<PACMAN::fp_t *>(sourceValues),
112 const_cast<PACMAN::int_t *>(connVal),
113 static_cast<PACMAN::int_t>(connValSize),
114 const_cast<PACMAN::offset_t *>(connOff),
115 static_cast<PACMAN::int_t>(connOffSize),
116 const_cast<PACMAN::cell_t *>(cellTypes),
117 const_cast<PACMAN::coordinates_t *>(targetPoints),
118 static_cast<PACMAN::int_t>(nTargetPoints), true);
119
120 for (int i = 0; i < nTargetPoints; ++i) {
121 targetValues[i] = result.targetValues[static_cast<std::size_t>(i)];
122 targetStatus[i] = result.targetStatus[static_cast<std::size_t>(i)];
123 }
124 return 0;
125 } catch (const std::exception &e) {
126 std::cerr << "pacman_fe_interpolate_c: " << e.what() << "\n";
127 return -1;
128 } catch (...) {
129 std::cerr << "pacman_fe_interpolate_c: unknown exception\n";
130 return -2;
131 }
132}
133
134// ---------------------------------------------------------------------------
135// MLS interpolation
136// ---------------------------------------------------------------------------
137
138int pacman_mls_interpolate_c(int spaceDimension, int execSpace,
139 const double *sourcePoints, int nSourcePoints,
140 const double *sourceValues,
141 const double *targetPoints, int nTargetPoints,
142 double *targetValues) {
143
144 try {
146 static_cast<PACMAN::int_t>(spaceDimension),
147 static_cast<unsigned char>(execSpace),
148 const_cast<PACMAN::coordinates_t *>(sourcePoints),
149 static_cast<PACMAN::int_t>(nSourcePoints),
150 const_cast<PACMAN::fp_t *>(sourceValues),
151 const_cast<PACMAN::coordinates_t *>(targetPoints),
152 static_cast<PACMAN::int_t>(nTargetPoints));
153
154 for (int i = 0; i < nTargetPoints; ++i)
155 targetValues[i] = result.targetValues[static_cast<std::size_t>(i)];
156
157 return 0;
158 } catch (const std::exception &e) {
159 std::cerr << "pacman_mls_interpolate_c: " << e.what() << "\n";
160 return -1;
161 } catch (...) {
162 std::cerr << "pacman_mls_interpolate_c: unknown exception\n";
163 return -2;
164 }
165}
166
167// ---------------------------------------------------------------------------
168// Cell-type helpers
169// ---------------------------------------------------------------------------
170
171int pacman_vtk_to_pacman_cell_type_c(const int *vtkTypes, int *pacmanTypes,
172 int n) {
173 try {
175 reinterpret_cast<const PACMAN::int_t *>(vtkTypes),
176 reinterpret_cast<PACMAN::cell_t *>(pacmanTypes),
177 static_cast<PACMAN::int_t>(n));
178 return 0;
179 } catch (const std::exception &e) {
180 std::cerr << "pacman_vtk_to_pacman_cell_type_c: " << e.what() << "\n";
181 return -1;
182 } catch (...) {
183 std::cerr << "pacman_vtk_to_pacman_cell_type_c: unknown exception\n";
184 return -2;
185 }
186}
187
188int pacman_vtk_cell_dim_c(int vtkCellType) {
189 try {
190 return static_cast<int>(
191 PACMAN::vtk_cell_dim(static_cast<PACMAN::int_t>(vtkCellType)));
192 } catch (const std::exception &e) {
193 std::cerr << "pacman_vtk_cell_dim_c: " << e.what() << "\n";
194 return -1;
195 } catch (...) {
196 std::cerr << "pacman_vtk_cell_dim_c: unknown exception\n";
197 return -2;
198 }
199}
200
201} // extern "C"
void pacman_kokkos_initialize_c(void)
Initialize Kokkos with default settings.
int pacman_rbf_interpolate_c(int spaceDimension, int execSpace, int rbfFunction, const double *sourcePoints, int nSourcePoints, const double *sourceValues, const double *targetPoints, int nTargetPoints, double *targetValues)
C shim for PACMAN::rbf_interpolate.
int pacman_mls_interpolate_c(int spaceDimension, int execSpace, const double *sourcePoints, int nSourcePoints, const double *sourceValues, const double *targetPoints, int nTargetPoints, double *targetValues)
C shim for PACMAN::mls_interpolate.
int pacman_best_execspace_c(void)
Return the best execution space available in the current build.
int pacman_fe_interpolate_c(int spaceDimension, int execSpace, int method, const double *sourcePoints, int nSourcePoints, const double *sourceValues, const int *connVal, int connValSize, const int *connOff, int connOffSize, const int *cellTypes, const double *targetPoints, int nTargetPoints, double *targetValues, int *targetStatus)
C shim for PACMAN::fe_interpolate.
void pacman_kokkos_finalize_c(void)
Finalize Kokkos.
int pacman_vtk_to_pacman_cell_type_c(const int *vtkTypes, int *pacmanTypes, int n)
Convert an array of VTK cell-type IDs to PACMAN CellType values.
int pacman_vtk_cell_dim_c(int vtkCellType)
Return the topological dimension for a VTK cell-type ID.
Plain-C shim declarations for the Fortran ISO C bindings interface.
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
RbfInterpolateResult rbf_interpolate(int_t spaceDimension, unsigned char execSpace, unsigned char rbfFunction, coordinates_t *sourcePoints, int_t nSourcePoints, fp_t *sourceValues, coordinates_t *targetPoints, int_t nTargetPoints)
C++ interface for RBF-PUM interpolation.
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.
MLSInterpolateResult MLS_interpolate(int_t spaceDimension, unsigned char execSpace, coordinates_t *sourcePoints, int_t nSourcePoints, fp_t *sourceValues, coordinates_t *targetPoints, int_t nTargetPoints)
C++ interface for Moving Least Squares (MLS) interpolation.
int_t vtk_cell_dim(int_t vtkCellType)
Return the topological dimension for a VTK cell type identifier.
static constexpr const unsigned char OPENMP
static constexpr const unsigned char SERIAL
static constexpr const unsigned char SYCL
static constexpr const unsigned char THREADS
static constexpr const unsigned char HIP
static constexpr const unsigned char CUDA
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
Result of a Moving Least Squares interpolation call.
std::vector< fp_t > targetValues
Interpolated scalar value per target point.
Result of an RBF-PUM interpolation call.
std::vector< fp_t > targetValues
Interpolated scalar value per target point.