PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
pacman_fortran.f90
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!> @brief Fortran module providing a high-level interface to the PACMAN
7!> interpolation library via ISO C bindings.
8!>
9!> Usage example (RBF-PUM):
10!> @code{.f90}
11!> use pacman_mod
12!> real(8), allocatable :: srcPts(:,:), srcVals(:), tgtPts(:,:), tgtVals(:)
13!> integer :: ierr
14!>
15!> call pacman_kokkos_initialize()
16!> allocate(tgtVals(nTarget))
17!>
18!> call pacman_rbf_interpolate(3, PACMAN_OPENMP, PACMAN_WENDLANDC2, &
19!> srcPts, srcVals, tgtPts, tgtVals, ierr)
20!>
21!> call pacman_kokkos_finalize()
22!> @endcode
23!>
24!> Point arrays must be laid out as @c pts(spaceDimension, nPoints), which
25!> in Fortran column-major storage is identical to C row-major
26!> @c pts[nPoints][spaceDimension].
28 use iso_c_binding
29 implicit none
30 private
31
32 ! -----------------------------------------------------------------
33 ! Public procedures
34 ! -----------------------------------------------------------------
37 public :: pacman_best_execspace
39 public :: pacman_fe_interpolate
42
43 ! -----------------------------------------------------------------
44 ! Execution space constants (match PACMAN::ExecSpaces)
45 ! -----------------------------------------------------------------
46 integer(c_int), parameter, public :: pacman_serial = 0
47 integer(c_int), parameter, public :: pacman_openmp = 1
48 integer(c_int), parameter, public :: pacman_threads = 2
49 integer(c_int), parameter, public :: pacman_cuda = 3
50 integer(c_int), parameter, public :: pacman_hip = 4
51 integer(c_int), parameter, public :: pacman_sycl = 5
52
53 ! -----------------------------------------------------------------
54 ! RBF function constants (match PACMAN::RbfFunctions)
55 ! 0x10 = 16, 0x11 = 17, 0x12 = 18, 0x13 = 19, 0x14 = 20
56 ! -----------------------------------------------------------------
57 integer(c_int), parameter, public :: pacman_wendlandc0 = 16
58 integer(c_int), parameter, public :: pacman_wendlandc2 = 17
59 integer(c_int), parameter, public :: pacman_wendlandc4 = 18
60 integer(c_int), parameter, public :: pacman_wendlandc6 = 19
61 integer(c_int), parameter, public :: pacman_wendlandc8 = 20
62
63 ! -----------------------------------------------------------------
64 ! FE method constants (match PACMAN::TransferMethods)
65 ! 0xF0 = 240, 0xF1 = 241, ..., 0xF4 = 244
66 ! -----------------------------------------------------------------
67 integer(c_int), parameter, public :: pacman_fe_nearest_nearest = 240
68 integer(c_int), parameter, public :: pacman_fe_interp_clamp = 241
69 integer(c_int), parameter, public :: pacman_fe_interp_nearest = 242
70 integer(c_int), parameter, public :: pacman_fe_interp_zerofill = 243
71 integer(c_int), parameter, public :: pacman_fe_interp_extrap = 244
72
73 ! -----------------------------------------------------------------
74 ! MLS constant (match PACMAN::TransferMethods::MLS = 0xF6 = 246)
75 ! -----------------------------------------------------------------
76 integer(c_int), parameter, public :: pacman_mls = 246
77
78 ! -----------------------------------------------------------------
79 ! ISO C interfaces to the plain-C shim (fortran_interface.cpp)
80 ! -----------------------------------------------------------------
81 interface
82 subroutine pacman_kokkos_initialize_c() &
83 bind(c, name='pacman_kokkos_initialize_c')
84 end subroutine
85
86 subroutine pacman_kokkos_finalize_c() &
87 bind(c, name='pacman_kokkos_finalize_c')
88 end subroutine
89
90 function pacman_best_execspace_c() result(es) &
91 bind(c, name='pacman_best_execspace_c')
92 import :: c_int
93 integer(c_int) :: es
94 end function
95
96 function pacman_rbf_interpolate_c( &
97 spaceDimension, execSpace, rbfFunction, &
98 sourcePoints, nSourcePoints, sourceValues, &
99 targetPoints, nTargetPoints, targetValues) result(ierr) &
100 bind(c, name='pacman_rbf_interpolate_c')
101 import :: c_int, c_ptr
102 integer(c_int), value :: spacedimension
103 integer(c_int), value :: execspace
104 integer(c_int), value :: rbffunction
105 type(c_ptr), value :: sourcepoints
106 integer(c_int), value :: nsourcepoints
107 type(c_ptr), value :: sourcevalues
108 type(c_ptr), value :: targetpoints
109 integer(c_int), value :: ntargetpoints
110 type(c_ptr), value :: targetvalues
111 integer(c_int) :: ierr
112 end function
113
114 function pacman_fe_interpolate_c( &
115 spaceDimension, execSpace, method, &
116 sourcePoints, nSourcePoints, sourceValues, &
117 connVal, connValSize, connOff, connOffSize, cellTypes, &
118 targetPoints, nTargetPoints, targetValues, targetStatus) result(ierr) &
119 bind(c, name='pacman_fe_interpolate_c')
120 import :: c_int, c_ptr
121 integer(c_int), value :: spacedimension
122 integer(c_int), value :: execspace
123 integer(c_int), value :: method
124 type(c_ptr), value :: sourcepoints
125 integer(c_int), value :: nsourcepoints
126 type(c_ptr), value :: sourcevalues
127 type(c_ptr), value :: connval
128 integer(c_int), value :: connvalsize
129 type(c_ptr), value :: connoff
130 integer(c_int), value :: connoffsize
131 type(c_ptr), value :: celltypes
132 type(c_ptr), value :: targetpoints
133 integer(c_int), value :: ntargetpoints
134 type(c_ptr), value :: targetvalues
135 type(c_ptr), value :: targetstatus
136 integer(c_int) :: ierr
137 end function
138
139 function pacman_mls_interpolate_c( &
140 spaceDimension, execSpace, &
141 sourcePoints, nSourcePoints, sourceValues, &
142 targetPoints, nTargetPoints, targetValues) result(ierr) &
143 bind(c, name='pacman_mls_interpolate_c')
144 import :: c_int, c_ptr
145 integer(c_int), value :: spacedimension
146 integer(c_int), value :: execspace
147 type(c_ptr), value :: sourcepoints
148 integer(c_int), value :: nsourcepoints
149 type(c_ptr), value :: sourcevalues
150 type(c_ptr), value :: targetpoints
151 integer(c_int), value :: ntargetpoints
152 type(c_ptr), value :: targetvalues
153 integer(c_int) :: ierr
154 end function
155
156 function pacman_vtk_to_pacman_cell_type_c(vtkTypes, pacmanTypes, n) &
157 result(ierr) bind(c, name='pacman_vtk_to_pacman_cell_type_c')
158 import :: c_int, c_ptr
159 type(c_ptr), value :: vtktypes
160 type(c_ptr), value :: pacmantypes
161 integer(c_int), value :: n
162 integer(c_int) :: ierr
163 end function
164 end interface
165
166contains
167
168 ! -----------------------------------------------------------------
169 ! Kokkos lifecycle wrappers
170 ! -----------------------------------------------------------------
171
172 !> @brief Initialize Kokkos with default settings.
173 !> Must be called before any interpolation subroutine.
176 end subroutine
177
178 !> @brief Finalize Kokkos.
179 !> Must be called after all interpolation calls are complete.
182 end subroutine
183
184 !> @brief Return the best execution space available in the current build.
185 !> @return Integer constant from PACMAN_SERIAL / PACMAN_OPENMP / etc.
186 function pacman_best_execspace() result(es)
187 integer(c_int) :: es
189 end function
190
191 ! -----------------------------------------------------------------
192 ! RBF-PUM interpolation
193 ! -----------------------------------------------------------------
194
195 !> @brief Interpolate scalar values from source to target points using
196 !> RBF-PUM (no mesh connectivity required).
197 !>
198 !> @param spaceDimension Geometric dimension (1, 2, or 3).
199 !> @param execSpace Execution-space constant (e.g. PACMAN_OPENMP).
200 !> @param rbfFunction RBF basis-function constant (e.g. PACMAN_WENDLANDC2).
201 !> @param sourcePoints Source coordinates, shape (spaceDimension, nSource).
202 !> @param sourceValues Source scalar values, shape (nSource).
203 !> @param targetPoints Target coordinates, shape (spaceDimension, nTarget).
204 !> @param targetValues Interpolated output, shape (nTarget). Must be
205 !> allocated by the caller before the call.
206 !> @param ierr Optional error code: 0 = success.
207 subroutine pacman_rbf_interpolate(spaceDimension, execSpace, rbfFunction, &
208 sourcePoints, sourceValues, targetPoints, targetValues, ierr)
209 integer, intent(in) :: spacedimension
210 integer(c_int), intent(in) :: execspace
211 integer(c_int), intent(in) :: rbffunction
212 real(c_double), intent(in), contiguous, target :: sourcepoints(:,:)
213 real(c_double), intent(in), contiguous, target :: sourcevalues(:)
214 real(c_double), intent(in), contiguous, target :: targetpoints(:,:)
215 real(c_double), intent(out), contiguous, target :: targetvalues(:)
216 integer(c_int), intent(out), optional :: ierr
217
218 integer(c_int) :: ret, nsrc, ntgt
219
220 nsrc = int(size(sourcevalues), c_int)
221 ntgt = int(size(targetvalues), c_int)
223 int(spacedimension, c_int), execspace, rbffunction, &
224 c_loc(sourcepoints), nsrc, c_loc(sourcevalues), &
225 c_loc(targetpoints), ntgt, c_loc(targetvalues))
226 if (present(ierr)) ierr = ret
227 end subroutine
228
229 ! -----------------------------------------------------------------
230 ! FE interpolation
231 ! -----------------------------------------------------------------
232
233 !> @brief Interpolate scalar values from a source mesh to target points
234 !> using finite-element transfer methods.
235 !>
236 !> @param spaceDimension Geometric dimension (1, 2, or 3).
237 !> @param execSpace Execution-space constant.
238 !> @param method FE method constant (e.g. PACMAN_FE_INTERP_CLAMP).
239 !> @param sourcePoints Source coordinates, shape (spaceDimension, nSource).
240 !> @param sourceValues Source scalar values, shape (nSource).
241 !> @param connVal CSR connectivity values, shape (connValSize).
242 !> @param connOff CSR connectivity offsets, shape (nElems + 1).
243 !> @param cellTypes PACMAN cell-type codes, shape (nElems).
244 !> @param targetPoints Target coordinates, shape (spaceDimension, nTarget).
245 !> @param targetValues Interpolated output, shape (nTarget).
246 !> @param targetStatus Transfer-status codes per target point, shape (nTarget).
247 !> @param ierr Optional error code: 0 = success.
248 subroutine pacman_fe_interpolate(spaceDimension, execSpace, method, &
249 sourcePoints, sourceValues, connVal, connOff, cellTypes, &
250 targetPoints, targetValues, targetStatus, ierr)
251 integer, intent(in) :: spacedimension
252 integer(c_int), intent(in) :: execspace
253 integer(c_int), intent(in) :: method
254 real(c_double), intent(in), contiguous, target :: sourcepoints(:,:)
255 real(c_double), intent(in), contiguous, target :: sourcevalues(:)
256 integer(c_int), intent(in), contiguous, target :: connval(:)
257 integer(c_int), intent(in), contiguous, target :: connoff(:)
258 integer(c_int), intent(in), contiguous, target :: celltypes(:)
259 real(c_double), intent(in), contiguous, target :: targetpoints(:,:)
260 real(c_double), intent(out), contiguous, target :: targetvalues(:)
261 integer(c_int), intent(out), contiguous, target :: targetstatus(:)
262 integer(c_int), intent(out), optional :: ierr
263
264 integer(c_int) :: ret, nsrc, ntgt, nconnval, nconnoff
265 integer :: i
266
267 nsrc = int(size(sourcevalues), c_int)
268 ntgt = int(size(targetvalues), c_int)
269 nconnval = int(size(connval), c_int)
270 nconnoff = int(size(connoff), c_int)
271 write(*,*) 'DEBUG: nsrc=', nsrc, ' ntgt=', ntgt, ' nconnval=', nconnval, ' nconnoff=', nconnoff, &
272 ' size(connVal)=', size(connval), ' size(connOff)=', size(connoff)
273 do i = 1, size(connval)
274 if (connval(i) < 0) then
275 ! LCOV_EXCL_START
276 write(*,'(A,I0,A,I0)') 'ERROR: connVal(', i, ') = ', connval(i)
277 if (present(ierr)) ierr = -1
278 return
279 ! LCOV_EXCL_STOP
280 end if
281 end do
283 int(spacedimension, c_int), execspace, method, &
284 c_loc(sourcepoints), nsrc, c_loc(sourcevalues), &
285 c_loc(connval), nconnval, c_loc(connoff), nconnoff, c_loc(celltypes), &
286 c_loc(targetpoints), ntgt, c_loc(targetvalues), c_loc(targetstatus))
287 if (present(ierr)) ierr = ret
288 end subroutine
289
290 ! -----------------------------------------------------------------
291 ! MLS interpolation
292 ! -----------------------------------------------------------------
293
294 !> @brief Interpolate scalar values from source to target points using
295 !> Moving Least Squares (no mesh connectivity required).
296 !>
297 !> @param spaceDimension Geometric dimension (1, 2, or 3).
298 !> @param execSpace Execution-space constant (e.g. PACMAN_OPENMP).
299 !> @param sourcePoints Source coordinates, shape (spaceDimension, nSource).
300 !> @param sourceValues Source scalar values, shape (nSource).
301 !> @param targetPoints Target coordinates, shape (spaceDimension, nTarget).
302 !> @param targetValues Interpolated output, shape (nTarget). Must be
303 !> allocated by the caller before the call.
304 !> @param ierr Optional error code: 0 = success.
305 subroutine pacman_mls_interpolate(spaceDimension, execSpace, &
306 sourcePoints, sourceValues, targetPoints, targetValues, ierr)
307 integer, intent(in) :: spacedimension
308 integer(c_int), intent(in) :: execspace
309 real(c_double), intent(in), contiguous, target :: sourcepoints(:,:)
310 real(c_double), intent(in), contiguous, target :: sourcevalues(:)
311 real(c_double), intent(in), contiguous, target :: targetpoints(:,:)
312 real(c_double), intent(out), contiguous, target :: targetvalues(:)
313 integer(c_int), intent(out), optional :: ierr
314
315 integer(c_int) :: ret, nsrc, ntgt
316
317 nsrc = int(size(sourcevalues), c_int)
318 ntgt = int(size(targetvalues), c_int)
320 int(spacedimension, c_int), execspace, &
321 c_loc(sourcepoints), nsrc, c_loc(sourcevalues), &
322 c_loc(targetpoints), ntgt, c_loc(targetvalues))
323 if (present(ierr)) ierr = ret
324 end subroutine
325
326 ! -----------------------------------------------------------------
327 ! VTK cell-type helpers
328 ! -----------------------------------------------------------------
329
330 !> @brief Convert VTK cell-type IDs to PACMAN CellType codes.
331 !>
332 !> @param vtkTypes Input VTK cell-type IDs, shape (n).
333 !> @param pacmanTypes Output PACMAN cell-type codes, shape (n).
334 !> @param ierr Optional error code: 0 = success.
335 subroutine pacman_vtk_to_pacman_cell_type(vtkTypes, pacmanTypes, ierr)
336 integer(c_int), intent(in), contiguous, target :: vtktypes(:)
337 integer(c_int), intent(out), contiguous, target :: pacmantypes(:)
338 integer(c_int), intent(out), optional :: ierr
339 integer(c_int) :: ret, n
340 n = int(size(vtktypes), c_int)
341 ret = pacman_vtk_to_pacman_cell_type_c(c_loc(vtktypes), c_loc(pacmantypes), n)
342 if (present(ierr)) ierr = ret
343 end subroutine
344
345end module pacman_mod
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.
Fortran module providing a high-level interface to the PACMAN interpolation library via ISO C binding...
integer(c_int), parameter, public pacman_openmp
integer(c_int), parameter, public pacman_sycl
subroutine, public pacman_mls_interpolate(spacedimension, execspace, sourcepoints, sourcevalues, targetpoints, targetvalues, ierr)
Interpolate scalar values from source to target points using Moving Least Squares (no mesh connectivi...
integer(c_int), parameter, public pacman_wendlandc8
integer(c_int), parameter, public pacman_threads
integer(c_int), parameter, public pacman_wendlandc6
integer(c_int), parameter, public pacman_mls
integer(c_int) function, public pacman_best_execspace()
Return the best execution space available in the current build.
integer(c_int), parameter, public pacman_hip
subroutine, public pacman_vtk_to_pacman_cell_type(vtktypes, pacmantypes, ierr)
Convert VTK cell-type IDs to PACMAN CellType codes.
integer(c_int), parameter, public pacman_fe_nearest_nearest
integer(c_int), parameter, public pacman_fe_interp_clamp
integer(c_int), parameter, public pacman_fe_interp_extrap
subroutine, public pacman_kokkos_initialize()
Initialize Kokkos with default settings.
subroutine, public pacman_kokkos_finalize()
Finalize Kokkos.
integer(c_int), parameter, public pacman_cuda
integer(c_int), parameter, public pacman_wendlandc0
integer(c_int), parameter, public pacman_wendlandc4
integer(c_int), parameter, public pacman_fe_interp_zerofill
integer(c_int), parameter, public pacman_serial
integer(c_int), parameter, public pacman_wendlandc2
integer(c_int), parameter, public pacman_fe_interp_nearest
subroutine, public pacman_rbf_interpolate(spacedimension, execspace, rbffunction, sourcepoints, sourcevalues, targetpoints, targetvalues, ierr)
Interpolate scalar values from source to target points using RBF-PUM (no mesh connectivity required).
subroutine, public pacman_fe_interpolate(spacedimension, execspace, method, sourcepoints, sourcevalues, connval, connoff, celltypes, targetpoints, targetvalues, targetstatus, ierr)
Interpolate scalar values from a source mesh to target points using finite-element transfer methods.