Octopus
finufft.F90
Go to the documentation of this file.
1!! Copyright (C) 2026 Octopus developers
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19
20#include "global.h"
21
23module finufft_oct_m
24 use debug_oct_m
25 use global_oct_m
26 use, intrinsic :: ieee_exceptions
27 use, intrinsic :: iso_c_binding
30 use parser_oct_m
32 implicit none
33
34 private
35
36 public :: &
37 finufft_t, &
51
52#ifdef HAVE_NFFT
53
56 integer, parameter :: FINUFFT_TYPE2 = 2
57 integer, parameter :: FINUFFT_IFLAG = -1
58#endif
59
60 type finufft_t
61 private
62
63 integer :: N(3)
64 integer :: M(3)
65 integer :: dim
66 real(real64), public :: norm
67
68 real(real64) :: tol
69
70 type(c_ptr) :: plan = c_null_ptr
71
74 real(real64), pointer :: xj(:) => null()
75 real(real64), pointer :: yj(:) => null()
76 real(real64), pointer :: zj(:) => null()
77
78 end type finufft_t
79
80#ifdef HAVE_NFFT
81
82 interface
83
85 type(c_ptr) function oct_finufft_opts_init(nthreads) bind(c, name='oct_finufft_opts_init')
86 import :: c_ptr, c_int
87 integer(c_int), value :: nthreads
88 end function oct_finufft_opts_init
89
90 subroutine oct_finufft_opts_end(opts) bind(c, name='oct_finufft_opts_end')
91 import :: c_ptr
92 type(c_ptr), value :: opts
93 end subroutine oct_finufft_opts_end
94
95 integer(c_int) function finufft_makeplan(type_, dim, n_modes, iflag, n_transf, tol, plan, opts) &
96 bind(c, name='finufft_makeplan')
97 import :: c_int, c_int64_t, c_double, c_ptr
98 integer(c_int), value :: type_
99 integer(c_int), value :: dim
100 integer(c_int64_t), intent(in) :: n_modes(*)
101 integer(c_int), value :: iflag
102 integer(c_int), value :: n_transf
103 real(c_double), value :: tol
104 type(c_ptr), intent(out) :: plan
105 type(c_ptr), value :: opts
106 end function finufft_makeplan
107
108 integer(c_int) function finufft_setpts(plan, M, xj, yj, zj, N, s, t, u) &
109 bind(c, name='finufft_setpts')
110 import :: c_int, c_int64_t, c_ptr
111 type(c_ptr), value :: plan
112 integer(c_int64_t), value :: M
113 type(c_ptr), value :: xj, yj, zj
114 integer(c_int64_t), value :: N
115 type(c_ptr), value :: s, t, u
116 end function finufft_setpts
117
118 integer(c_int) function finufft_execute(plan, cj, fk) bind(c, name='finufft_execute')
119 import :: c_int, c_ptr, c_double_complex
120 type(c_ptr), value :: plan
121 complex(c_double_complex), intent(inout) :: cj(*)
122 complex(c_double_complex), intent(in) :: fk(*)
123 end function finufft_execute
125 integer(c_int) function finufft_execute_adjoint(plan, cj, fk) bind(c, name='finufft_execute_adjoint')
126 import :: c_int, c_ptr, c_double_complex
127 type(c_ptr), value :: plan
128 complex(c_double_complex), intent(in) :: cj(*)
129 complex(c_double_complex), intent(inout) :: fk(*)
130 end function finufft_execute_adjoint
131
132 integer(c_int) function finufft_destroy(plan) bind(c, name='finufft_destroy')
133 import :: c_int, c_ptr
134 type(c_ptr), value :: plan
135 end function finufft_destroy
136
137 end interface
138#endif
141contains
142
143 ! ---------------------------------------------------------
144 subroutine finufft_read_options(finufft, namespace)
145 type(finufft_t), intent(inout) :: finufft
146 type(namespace_t), intent(in) :: namespace
148 push_sub(finufft_read_options)
149
150 !%Variable FINUFFTTolerance
151 !%Type float
152 !%Default 1e-8
153 !%Section Mesh::FFTs
154 !%Description
155 !% Requested relative tolerance of the non-equispaced FFTs. Tighter
156 !% tolerances widen the spreading kernel and are more expensive.
157 !%End
158 call parse_variable(namespace, 'FINUFFTTolerance', 1.0e-8_real64, finufft%tol)
159
160 if (finufft%tol <= m_zero) then
161 call messages_input_error(namespace, 'FINUFFTTolerance')
162 end if
163
164 pop_sub(finufft_read_options)
165 end subroutine finufft_read_options
166
167 ! ---------------------------------------------------------
170 subroutine finufft_set_tolerance(finufft, tol)
171 type(finufft_t), intent(inout) :: finufft
172 real(real64), intent(in) :: tol
174 push_sub(finufft_set_tolerance)
175
176 finufft%tol = tol
177
178 pop_sub(finufft_set_tolerance)
179 end subroutine finufft_set_tolerance
180
181 ! ---------------------------------------------------------
182 subroutine finufft_init(finufft, finufft_options, N, dim, M, nthreads)
183 type(finufft_t), intent(inout) :: finufft
184 type(finufft_t), intent(in) :: finufft_options
185 integer, intent(in) :: n(3)
186 integer, intent(in) :: dim
187 integer, intent(in) :: m(3)
188 integer, intent(in), optional :: nthreads
189
190#ifdef HAVE_NFFT
191 integer(c_int64_t) :: n_modes(3)
192 integer(c_int) :: ier
193 type(c_ptr) :: finufft_opts
194 integer(c_int) :: n_threads
195#endif
196
197 push_sub(finufft_init)
198
199 call profiling_in("FINUFFT_INIT")
200
201 finufft%dim = dim
202 finufft%N(:) = n(:)
203 finufft%M(:) = m(:)
204 ! unused directions hold a single mode and a single node
205 finufft%N(dim+1:3) = 1
206 finufft%M(dim+1:3) = 1
207
208 finufft%tol = finufft_options%tol
209
210#ifdef HAVE_NFFT
211 n_modes(1:3) = int(finufft%N(1:3), c_int64_t)
212 n_threads = optional_default(nthreads, 0_c_int)
213
214 ! Selects the FINUFFT defaults, including the mode ordering relied on here
215 ! (modeord = 0, modes -N/2 .. N/2-1)
216 finufft_opts = oct_finufft_opts_init(n_threads)
217 if (.not. c_associated(finufft_opts)) then
218 message(1) = "Could not allocate the FINUFFT options."
219 call messages_fatal(1)
220 end if
221
222 ier = finufft_makeplan(int(finufft_type2, c_int), int(dim, c_int), n_modes, &
223 int(finufft_iflag, c_int), 1_c_int, real(finufft%tol, c_double), finufft%plan, finufft_opts)
224 call finufft_check(ier, 'finufft_makeplan')
225
226 call oct_finufft_opts_end(finufft_opts)
227#endif
228
229 call profiling_out("FINUFFT_INIT")
230
231 pop_sub(finufft_init)
232 end subroutine finufft_init
233
234 ! ---------------------------------------------------------
240 subroutine finufft_precompute(finufft, X1, X2, X3, period)
241 type(finufft_t), intent(inout) :: finufft
242 real(real64), contiguous, intent(in) :: x1(:)
243 real(real64), contiguous, optional, intent(in) :: x2(:)
244 real(real64), contiguous, optional, intent(in) :: x3(:)
246 real(real64), optional, intent(in) :: period(:)
247
248 real(real64) :: v1(1:finufft%m(1)), v2(1:finufft%m(2)), v3(1:finufft%m(3))
249 real(real64) :: inv_spacing, period_(3)
250 integer(c_int64_t) :: nj
251 integer :: i1, i2, i3, jj
252#ifdef HAVE_NFFT
253 integer(c_int) :: ier
254 type(c_ptr) :: yj_ptr, zj_ptr
255#endif
256
257 push_sub(finufft_precompute)
258
259 call profiling_in("FINUFFT_PRECOMPUTE")
260
261 finufft%norm = m_one
262
263 period_ = m_zero
264 if (present(period)) period_(1:3) = period(1:3)
265
266 assert(size(x1) == finufft%M(1))
267 call finufft_scale_nodes(x1, v1, inv_spacing, period_(1))
268 finufft%norm = finufft%norm * inv_spacing
269
270 v2 = m_zero
271 if (finufft%dim >= 2) then
272 assert(present(x2))
273 assert(size(x2) == finufft%M(2))
274 call finufft_scale_nodes(x2, v2, inv_spacing, period_(2))
275 finufft%norm = finufft%norm * inv_spacing
276 end if
277
278 v3 = m_zero
279 if (finufft%dim >= 3) then
280 assert(present(x3))
281 assert(size(x3) == finufft%M(3))
282 call finufft_scale_nodes(x3, v3, inv_spacing, period_(3))
283 finufft%norm = finufft%norm * inv_spacing
284 end if
285
286 nj = int(finufft%M(1), c_int64_t)*int(finufft%M(2), c_int64_t)*int(finufft%M(3), c_int64_t)
287
288 safe_deallocate_p(finufft%xj)
289 safe_deallocate_p(finufft%yj)
290 safe_deallocate_p(finufft%zj)
291 safe_allocate(finufft%xj(1:nj))
292 if (finufft%dim >= 2) then
293 safe_allocate(finufft%yj(1:nj))
294 end if
295 if (finufft%dim >= 3) then
296 safe_allocate(finufft%zj(1:nj))
297 end if
298
299 ! Fortran order, matching the transform arrays, so execute needs no repacking
300 jj = 0
301 do i3 = 1, finufft%M(3)
302 do i2 = 1, finufft%M(2)
303 do i1 = 1, finufft%M(1)
304 jj = jj + 1
305 finufft%xj(jj) = m_two*m_pi*v1(i1)
306 if (finufft%dim >= 2) finufft%yj(jj) = m_two*m_pi*v2(i2)
307 if (finufft%dim >= 3) finufft%zj(jj) = m_two*m_pi*v3(i3)
308 end do
309 end do
310 end do
311
312#ifdef HAVE_NFFT
313 yj_ptr = c_null_ptr
314 zj_ptr = c_null_ptr
315 if (finufft%dim >= 2) yj_ptr = c_loc(finufft%yj)
316 if (finufft%dim >= 3) zj_ptr = c_loc(finufft%zj)
317
318 ! The trailing arguments only apply to type 3
319 ier = finufft_setpts(finufft%plan, nj, c_loc(finufft%xj), yj_ptr, zj_ptr, &
320 0_c_int64_t, c_null_ptr, c_null_ptr, c_null_ptr)
321 call finufft_check(ier, 'finufft_setpts')
322#endif
323
324 call profiling_out("FINUFFT_PRECOMPUTE")
325
326 write(message(1), '(a)') "Info: FINUFFT plan precomputed."
327 call messages_info(1)
328
329 pop_sub(finufft_precompute)
330 end subroutine finufft_precompute
331
332 ! ---------------------------------------------------------
336 subroutine finufft_set_nodes(finufft, xj, yj, zj, period)
337 type(finufft_t), intent(inout) :: finufft
338 real(real64), contiguous, intent(in) :: xj(:)
339 real(real64), contiguous, intent(in) :: yj(:)
340 real(real64), contiguous, intent(in) :: zj(:)
341 real(real64), contiguous, intent(in) :: period(:)
342
343 integer(c_int64_t) :: nj
344#ifdef HAVE_NFFT
345 integer(c_int) :: ier
346#endif
347
348 push_sub(finufft_set_nodes)
349
350 call profiling_in("FINUFFT_SET_NODES")
351
352 assert(finufft%dim == 3)
353
354 nj = int(finufft%M(1), c_int64_t)
355 assert(size(xj) == nj)
356 assert(size(yj) == nj)
357 assert(size(zj) == nj)
358
359 finufft%norm = m_one
360
361 safe_deallocate_p(finufft%xj)
362 safe_deallocate_p(finufft%yj)
363 safe_deallocate_p(finufft%zj)
364 safe_allocate(finufft%xj(1:nj))
365 safe_allocate(finufft%yj(1:nj))
366 safe_allocate(finufft%zj(1:nj))
367
368 finufft%xj = m_two*m_pi*xj/period(1)
369 finufft%yj = m_two*m_pi*yj/period(2)
370 finufft%zj = m_two*m_pi*zj/period(3)
371
372#ifdef HAVE_NFFT
373 ier = finufft_setpts(finufft%plan, nj, c_loc(finufft%xj), c_loc(finufft%yj), c_loc(finufft%zj), &
374 0_c_int64_t, c_null_ptr, c_null_ptr, c_null_ptr)
375 call finufft_check(ier, 'finufft_setpts')
376#endif
377
378 call profiling_out("FINUFFT_SET_NODES")
379
381 end subroutine finufft_set_nodes
382
383 ! ---------------------------------------------------------
385 subroutine finufft_modes_to_nodes(finufft, modes, values)
386 type(finufft_t), intent(in) :: finufft
387 complex(real64), contiguous, intent(in) :: modes(:,:,:)
388 complex(real64), contiguous, intent(out) :: values(:)
389
390#ifdef HAVE_NFFT
391 integer(c_int) :: ier
392 logical :: halting(size(ieee_all))
393#endif
394
395 push_sub(finufft_modes_to_nodes)
396
397 call profiling_in("FINUFFT_MODES_TO_NODES")
398
399#ifdef HAVE_NFFT
400 call finufft_halting_suspend(halting)
401 ier = finufft_execute(finufft%plan, values, modes)
402 call finufft_halting_restore(halting)
403 call finufft_check(ier, 'finufft_execute')
404#else
405 values = m_z0
406#endif
407
408 call profiling_out("FINUFFT_MODES_TO_NODES")
409
411 end subroutine finufft_modes_to_nodes
412
413 ! ---------------------------------------------------------
415 subroutine finufft_nodes_to_modes(finufft, values, modes)
416 type(finufft_t), intent(in) :: finufft
417 complex(real64), contiguous, intent(in) :: values(:)
418 complex(real64), contiguous, intent(out) :: modes(:,:,:)
419
420#ifdef HAVE_NFFT
421 integer(c_int) :: ier
422 logical :: halting(size(ieee_all))
423#endif
424
425 push_sub(finufft_nodes_to_modes)
426
427 call profiling_in("FINUFFT_NODES_TO_MODES")
428
429#ifdef HAVE_NFFT
430 call finufft_halting_suspend(halting)
431 ier = finufft_execute_adjoint(finufft%plan, values, modes)
432 call finufft_halting_restore(halting)
433 call finufft_check(ier, 'finufft_execute_adjoint')
434#else
435 modes = m_z0
436#endif
437
438 call profiling_out("FINUFFT_NODES_TO_MODES")
439
441 end subroutine finufft_nodes_to_modes
442
443 ! ---------------------------------------------------------
448 subroutine finufft_scale_nodes(X, v, inv_spacing, period)
449 real(real64), contiguous, intent(in) :: X(:)
450 real(real64), contiguous, intent(out) :: v(:)
451 real(real64), intent(out) :: inv_spacing
452 real(real64), intent(in) :: period
453
454 real(real64) :: length, cc
455 real(real64) :: spacing(size(v) - 1)
456 integer :: i
457
458 push_sub(finufft_scale_nodes)
459
460 assert(size(x) == size(v))
461 assert(size(x) > 1)
462
463 if (period > m_zero) then
464 v = x/period
465 inv_spacing = real(size(v), real64)
466 else
467 ! the sample nodes must be in [-1/2, 1/2)
468 length = (maxval(x) - minval(x))*(m_one + m_epsilon)
469 cc = (minval(x) + maxval(x))/m_two
470 v = (x - cc)/length
471
472 spacing = [(v(i+1) - v(i), i = 1, size(v)-1)]
473 inv_spacing = m_one/minval(abs(spacing))
474 end if
475
476 pop_sub(finufft_scale_nodes)
477 end subroutine finufft_scale_nodes
478
479 ! ---------------------------------------------------------
480 subroutine finufft_write_info(finufft)
481 type(finufft_t), intent(in) :: finufft
482
483 integer :: idir
484
485 push_sub(finufft_write_info)
486
487 call messages_write("Info: FINUFFT parameters")
488 call messages_new_line()
489
490 call messages_write(" Fourier coefficients N = ")
491 do idir = 1, finufft%dim
492 call messages_write(finufft%N(idir))
493 if (idir < finufft%dim) call messages_write(" x ")
494 end do
495 call messages_new_line()
496
497 call messages_write(" Spatial nodes M = ")
498 do idir = 1, finufft%dim
499 call messages_write(finufft%M(idir))
500 if (idir < finufft%dim) call messages_write(" x ")
501 end do
502 call messages_new_line()
503
504 call messages_write(" Requested tolerance tol = ")
505 call messages_write(finufft%tol)
506 call messages_new_line()
507
508 call messages_info()
509
510 pop_sub(finufft_write_info)
511 end subroutine finufft_write_info
512
513 ! ---------------------------------------------------------
514 subroutine finufft_end(finufft)
515 type(finufft_t), intent(inout) :: finufft
516
517#ifdef HAVE_NFFT
518 integer(c_int) :: ier
519#endif
520
521 push_sub(finufft_end)
522
523#ifdef HAVE_NFFT
524 if (c_associated(finufft%plan)) then
525 ier = finufft_destroy(finufft%plan)
526 call finufft_check(ier, 'finufft_destroy')
527 finufft%plan = c_null_ptr
528 end if
529#endif
530
531 safe_deallocate_p(finufft%xj)
532 safe_deallocate_p(finufft%yj)
533 safe_deallocate_p(finufft%zj)
534
535 pop_sub(finufft_end)
536 end subroutine finufft_end
537
538 ! ---------------------------------------------------------
539#ifdef HAVE_NFFT
540
541 subroutine finufft_check(ier, routine)
542 integer(c_int), intent(in) :: ier
543 character(*), intent(in) :: routine
544
545 if (ier == 0) return
546
547 write(message(1), '(3a,i0,a)') "FINUFFT routine ", trim(routine), &
548 " failed with error code ", ier, "."
549 call messages_fatal(1)
550 end subroutine finufft_check
551
552 ! ---------------------------------------------------------
558 subroutine finufft_halting_suspend(halting)
559 logical, intent(out) :: halting(:)
560
561#ifndef NDEBUG
562 call ieee_get_halting_mode(ieee_all, halting)
563 !$omp parallel
564 call ieee_set_halting_mode(ieee_all, .false.)
565 !$omp end parallel
566#endif
567 end subroutine finufft_halting_suspend
568
569 ! ---------------------------------------------------------
571 subroutine finufft_halting_restore(halting)
572 logical, intent(in) :: halting(:)
573
574#ifndef NDEBUG
575 !$omp parallel
576 call ieee_set_halting_mode(ieee_all, halting)
577 !$omp end parallel
578#endif
579 end subroutine finufft_halting_restore
580#endif
581
582#include "undef.F90"
583#include "real.F90"
584#include "finufft_inc.F90"
586#include "undef.F90"
587#include "complex.F90"
588#include "finufft_inc.F90"
589
590end module finufft_oct_m
591
592!! Local Variables:
593!! mode: f90
594!! coding: utf-8
595!! End:
Non-equispaced FFTs through the C interface of the FINUFFT library.
Definition: finufft.F90:95
subroutine, public finufft_write_info(finufft)
Definition: finufft.F90:446
subroutine, public finufft_set_nodes(finufft, xj, yj, zj, period)
Hand scattered nodes to FINUFFT, mapping one period of the mode grid onto the [-1/2,...
Definition: finufft.F90:302
subroutine, public zfinufft_forward(finufft, in, out)
Evaluate the Fourier modes on the non-equispaced nodes.
Definition: finufft.F90:678
subroutine, public zfinufft_backward(finufft, in, out)
Project the non-equispaced nodes back onto the Fourier modes.
Definition: finufft.F90:697
subroutine, public finufft_precompute(finufft, X1, X2, X3, period)
Hand the spatial nodes to FINUFFT.
Definition: finufft.F90:218
subroutine, public finufft_read_options(finufft, namespace)
Definition: finufft.F90:148
subroutine, public finufft_init(finufft, finufft_options, N, dim, M, nthreads)
Definition: finufft.F90:186
subroutine, public finufft_end(finufft)
Definition: finufft.F90:480
subroutine, public finufft_nodes_to_modes(finufft, values, modes)
Project the scattered nodes onto the Fourier modes.
Definition: finufft.F90:381
subroutine, public finufft_modes_to_nodes(finufft, modes, values)
Evaluate the Fourier modes on the scattered nodes.
Definition: finufft.F90:351
subroutine finufft_scale_nodes(X, v, inv_spacing, period)
Map the nodes onto [-1/2, 1/2) and return the reciprocal of the smallest scaled spacing....
Definition: finufft.F90:414
subroutine, public dfinufft_backward(finufft, in, out)
Project the non-equispaced nodes back onto the Fourier modes.
Definition: finufft.F90:586
subroutine, public dfinufft_forward(finufft, in, out)
Evaluate the Fourier modes on the non-equispaced nodes.
Definition: finufft.F90:567
subroutine, public finufft_set_tolerance(finufft, tol)
Set the requested tolerance directly, for callers that take it from an input variable of their own ra...
Definition: finufft.F90:174
real(real64), parameter, public m_two
Definition: global.F90:175
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:171
complex(real64), parameter, public m_z0
Definition: global.F90:183
real(real64), parameter, public m_epsilon
Definition: global.F90:189
real(real64), parameter, public m_one
Definition: global.F90:174
subroutine, public messages_new_line()
Definition: messages.F90:1069
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604