Octopus
splines.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
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#include "global.h"
20
21!/*----------------------------------------------------------------------------
22! This module contains a data type (spline_t) to contain 1D functions,
23! along with a series of procedures to manage them (to define them, to obtain
24! its values, to operate on them, etc). The internal representation of the
25! functions is done through cubic splines, handled by the GSL library. For
26! the user of the module, this internal representation is hidden; one just
27! works with what are called hereafter "spline functions".
28!
29! To define a function, one must supply a set {x(i),y(i)} of pairs of values
30! -- the abscissa and the value of the function.
31!
32! [*] DATA TYPE:
33! To define a spline function:
34! type(spline_t) :: f
35!
36! [1] INITIALIZATION:
37! Before using any function, one should initialize it:
38!
39! Interface:
40! subroutine spline_init(spl)
41! type(spline_t), intent(out) :: spl [or spl(:) or spl(:, :)]
42! end subroutine spline_init
43!
44! Usage:
45! call spline_init(f)
46!
47! [2] FINALIZATION:
48! To empty any allocated space, one should finalize the function:
49!
50! Interface:
51! subroutine spline_end(spl)
52! type(spline_t), intent(inout) :: spl [or spl(:) or spl(:, :)]
53! end subroutine spline_end
54!
55! Usage
56! type(spline_t) :: f
57! call spline_end(f)
58!
59! [3] TO DEFINE A FUNCTION:
60! To "fill" an initialized function f,use spline_fit
61!
62! Interface:
63! subroutine spline_fit(n, x, y, spl, threshold)
64! integer, intent(in) :: nrc
65! real(X), intent(in) :: x(n), y(n)
66! type(spline_t), intent(out) :: spl
67! real(real64), optional, intent(in) :: threshold
68! end subroutine spline_fit
69!
70! (X may be 4 or eight, for single or double precision)
71!
72! Usage:
73! call spline_fit(n, x, y, f)
74! n is the number of values that are supplied, x the abscissas, and y
75! the value of the function to represent at each point.
76! The optional argument threshold is used to find the radius associated with the spline.
77!
78! [4] FUNCTION VALUES:
79! To retrieve the value of a function at a given point:
80!
81! Interface:
82!
83! real(real64) function spline_eval(spl, x)
84! type(spline_t), intent(in) :: spl
85! real(real64), intent(in) :: x
86! end function spline_eval
87!
88! Usage:
89! To retrieve the value of function f at point x, and place it into
90! real value val:
91! val = spline_eval(f, x)
92!
93! [5] MULTIPLICATION BY A SCALAR
94! You may multiply a given spline-represented spline by a real number:
95!
96! Interface:
97! subroutine spline_times(a, spl)
98! type(spline_t), intent(inout) :: spl
99! real(real64), intent(in) :: a
100! real(real64), optional, intent(in) :: threshold
101! end subroutine spline_times
102!
103! Usage:
104! call spline_init(f)
105! call spline_fit(npoints, x, y, f) ! Fill f with y values at x points
106! call spline_times(a, f) ! Now f contains a*y values at x points.
107!
108! [6] INTEGRAL:
109! Given a defined function, the function spline_integral returns its
110! integral. The interval of integration may or may not be supplied.
111!
112! Interface:
113! real(real64) function spline_integral(spl [,a,b])
114! type(spline_integral), intent(in) :: spl
115! real(real64), intent(in), optional :: a, b
116! end function spline_integral
117!
118! Note: The following routines, spline_3dft, spline_cut and spline_filter,
119! assume that the spline functions are the radial part of a 3 dimensional function with
120! spherical symmetry. This is why the Fourier transform of F(\vec{r}) = f(r), is:
121! F(\vec{g}) = f(g) = \frac{4\pi}{g} \int_{0}^{\infty} { r*sin(g*r)*f(r) }
122! which coincides with the inverse Fourier transform, except that the inverse Fourier
123! transform should be multiplied by a (2*\pi)^{-3} factor.
124!
125! [7] FOURIER TRANSFORM:
126! If a spline function f is representing the radial part of a spherically
127! symmetric function F(\vec{r}), its Fourier transform is:
128! F(\vec{g}) = f(g) = \frac{4\pi}{g} \int_{0}^{\infty} { r*sin(g*r)*f(r) }
129! It is assumed that f is defined in some interval [0,rmax], and that it is
130! null at rmax and beyond. One may obtain f(g) by using spline_3dft.
131! The result is placed on the spline data type splw. This has to be initialized,
132! and may or may not be filled. If it is filled, the abscissas that it has
133! are used to define the function. If it is not filled, an equally spaced
134! grid is constructed to define the function, in the interval [0, gmax], where
135! gmax has to be supplied by the caller.
136!
137! Interface:
138! subroutine spline_3dft(spl, splw, gmax)
139! type(spline_t), intent(in) :: spl
140! type(spline_t), intent(inout) :: splw
141! real(real64), intent(in), optional :: gmax
142! end subroutine spline_3dft
143!
144! [8] BESSEL TRANSFORM:
145!
146!
147! [9] CUTTING A FUNCTION:
148! spline_cut multiplies a given function by a cutoff-function, which
149! is defined to be one in [0, cutoff], and \exp\{-beta*(x/cutoff-1)^2\}
150!
151! Interface:
152! subroutine spline_cut(spl, cutoff, beta)
153! type(spline_t), intent(in) :: spl
154! real(real64), intent(in) :: cutoff, beta
155! end subroutine spline_cut
156!
157! [10] PRINTING A FUNCTION:
158! It prints to a file the (x,y) values that were used to define a function.
159! The file is pointed to by its Fortran unit given by argument iunit.
160!
161! Interface:
162! subroutine spline_print(spl, iunit)
163! type(spline_t), intent(in) :: spl [ or spl(:) or spl(:, :)]
164! integer, intent(in) :: iunit
165! end subroutine spline_print
166!
167!----------------------------------------------------------------------------*/!
168module splines_oct_m
169 use debug_oct_m
170 use global_oct_m
171 use iso_c_binding
172 use, intrinsic :: iso_fortran_env
176
177 implicit none
178
179 ! Define which routines can be seen from the outside.
180 private
181 public :: &
182 spline_t, & ! [*]
183 spline_init, & ! [1]
184 spline_end, & ! [2]
185 spline_fit, & ! [3]
186 spline_eval, & ! [4]
190 spline_same, &
191 spline_eval_vec, & ! [4]
192 spline_times, & ! [5]
193 spline_integral, & ! [6]
194 spline_3dft, & ! [7]
195 spline_besselft, & ! [8]
196 spline_cut, & ! [9]
197 spline_print, & ! [10]
198 spline_der, &
199 spline_der2, &
200 spline_div, &
201 spline_mult, &
207
209 type spline_t
210 private
211 real(real64) :: x_limit(2)
212 type(c_ptr) :: spl, acc
213 real(real64), public :: x_threshold
215 end type spline_t
216
219 interface spline_eval_vec
220 module procedure spline_eval8_array
221 module procedure spline_evalz_array
222 end interface spline_eval_vec
223
226 interface spline_integral
227 module procedure spline_integral_full
228 module procedure spline_integral_limits
229 end interface spline_integral
230
232 interface spline_init
233 module procedure spline_init_0
234 module procedure spline_init_1
235 module procedure spline_init_2
236 end interface spline_init
237
238 interface spline_end
239 module procedure spline_end_0
240 module procedure spline_end_1
241 module procedure spline_end_2
242 end interface spline_end
243
244 interface spline_print
245 module procedure spline_print_0
246 module procedure spline_print_1
247 module procedure spline_print_2
248 end interface spline_print
249
250 ! These are interfaces to functions defined in oct_gsl_f.c, which in turn
251 ! take care of calling the GSL library.
252 interface
253 subroutine oct_spline_end(spl, acc)
254 use iso_c_binding
255 implicit none
256 type(c_ptr), intent(inout) :: spl, acc
257 end subroutine oct_spline_end
258
259 subroutine oct_spline_fit(nrc, x, y, spl, acc)
260 use iso_c_binding
261 use, intrinsic :: iso_fortran_env
262 implicit none
263 integer, intent(in) :: nrc
264 real(real64), intent(in) :: x
265 real(real64), intent(in) :: y
266 type(c_ptr), intent(inout) :: spl
267 type(c_ptr), intent(inout) :: acc
268 end subroutine oct_spline_fit
269
270 subroutine oct_spline_accel_alloc(acc)
271 use iso_c_binding
272 implicit none
273 type(c_ptr), intent(out) :: acc
274 end subroutine oct_spline_accel_alloc
275
276 subroutine oct_spline_accel_free(acc)
277 use iso_c_binding
278 implicit none
279 type(c_ptr), intent(inout) :: acc
280 end subroutine oct_spline_accel_free
282 real(real64) function oct_spline_eval(x, spl, acc)
283 use iso_c_binding
284 use, intrinsic :: iso_fortran_env
285 implicit none
286 real(real64), intent(in) :: x
287 type(c_ptr), intent(in) :: spl
288 type(c_ptr), intent(in) :: acc
289 end function oct_spline_eval
290
291 subroutine oct_spline_eval_array(nn, xf, spl, acc)
292 use iso_c_binding
293 use, intrinsic :: iso_fortran_env
294 implicit none
295 integer, intent(in) :: nn
296 real(real64), intent(inout) :: xf
297 type(c_ptr), intent(in) :: spl
298 type(c_ptr), intent(in) :: acc
299 end subroutine oct_spline_eval_array
300
301 subroutine oct_spline_eval_arrayz(nn, xf, spl, acc)
302 use iso_c_binding
303 use, intrinsic :: iso_fortran_env
304 implicit none
305 integer, intent(in) :: nn
306 complex(real64), intent(inout) :: xf
307 type(c_ptr), intent(in) :: spl
308 type(c_ptr), intent(in) :: acc
309 end subroutine oct_spline_eval_arrayz
311 real(real64) function oct_spline_eval_der(x, spl, acc)
312 use iso_c_binding
313 use, intrinsic :: iso_fortran_env
314 implicit none
315 real(real64), intent(in) :: x
316 type(c_ptr), intent(in) :: spl
317 type(c_ptr), intent(in) :: acc
318 end function oct_spline_eval_der
319
320 real(real64) function oct_spline_eval_der2(x, spl, acc)
321 use iso_c_binding
322 use, intrinsic :: iso_fortran_env
323 implicit none
324 real(real64), intent(in) :: x
325 type(c_ptr), intent(in) :: spl
326 type(c_ptr), intent(in) :: acc
327 end function oct_spline_eval_der2
328
329 integer pure function oct_spline_npoints(spl, acc)
330 use iso_c_binding
331 implicit none
332 type(c_ptr), intent(in) :: spl
333 type(c_ptr), intent(in) :: acc
334 end function oct_spline_npoints
335
336 pure subroutine oct_spline_x(spl, x)
337 use iso_c_binding
338 use, intrinsic :: iso_fortran_env
339 implicit none
340 type(c_ptr), intent(in) :: spl
341 real(real64), intent(out) :: x
342 end subroutine oct_spline_x
343
344 subroutine oct_spline_y(spl, y)
345 use iso_c_binding
346 use, intrinsic :: iso_fortran_env
347 implicit none
348 type(c_ptr), intent(in) :: spl
349 real(real64), intent(out) :: y
350 end subroutine oct_spline_y
351
352 real(real64) pure function oct_spline_eval_integ(spl, a, b, acc)
353 use iso_c_binding
354 use, intrinsic :: iso_fortran_env
355 implicit none
356 type(c_ptr), intent(in) :: spl
357 real(real64), intent(in) :: a
358 real(real64), intent(in) :: b
359 type(c_ptr), intent(in) :: acc
360 end function oct_spline_eval_integ
361
362 real(real64) pure function oct_spline_eval_integ_full(spl, acc)
363 use iso_c_binding
364 use, intrinsic :: iso_fortran_env
365 implicit none
366 type(c_ptr), intent(in) :: spl
367 type(c_ptr), intent(in) :: acc
368 end function oct_spline_eval_integ_full
369 end interface
370
371 real(real64), parameter :: tol_x = 1e-14_real64
372
373contains
374
375 !------------------------------------------------------------
376 subroutine spline_init_0(spl)
377 type(spline_t), intent(out) :: spl
378
379 ! No PUSH SUB, called too often
380
381 spl%spl = c_null_ptr
382 spl%acc = c_null_ptr
384 ! deliberately illegal values, for checking
385 spl%x_limit(1) = -1_real64
386 spl%x_limit(2) = -2_real64
387
388 spl%x_threshold = m_zero
389
390 end subroutine spline_init_0
391
393 !------------------------------------------------------------
394 subroutine spline_init_1(spl)
395 type(spline_t), intent(out) :: spl(:)
396
397 integer :: i
398
399 push_sub(spline_init_1)
400
401 do i = 1, size(spl)
402 call spline_init_0(spl(i))
403 end do
404
405 pop_sub(spline_init_1)
406 end subroutine spline_init_1
407
409 !------------------------------------------------------------
410 subroutine spline_init_2(spl)
411 type(spline_t), intent(out) :: spl(:, :)
412
413 integer :: i, j
414
415 push_sub(spline_init_2)
417 do i = 1, size(spl, 1)
418 do j = 1, size(spl, 2)
419 call spline_init_0(spl(i, j))
420 end do
421 end do
422
423 pop_sub(spline_init_2)
424 end subroutine spline_init_2
425
426
427 !------------------------------------------------------------
428 subroutine spline_end_0(spl)
429 type(spline_t), intent(inout) :: spl
430
431 ! No PUSH SUB, called too often.
432
433 if (c_associated(spl%spl) .and. c_associated(spl%acc)) then
434 call oct_spline_end(spl%spl, spl%acc)
435 end if
436 spl%spl = c_null_ptr
437 spl%acc = c_null_ptr
438
439 end subroutine spline_end_0
440
441
442 !------------------------------------------------------------
443 subroutine spline_end_1(spl)
444 type(spline_t), intent(inout) :: spl(:)
445
446 integer :: i
447
448 push_sub(spline_end_1)
449
450 do i = 1, size(spl)
451 call spline_end_0(spl(i))
452 end do
453
454 pop_sub(spline_end_1)
455 end subroutine spline_end_1
456
457
458 !------------------------------------------------------------
459 subroutine spline_end_2(spl)
460 type(spline_t), intent(inout) :: spl(:, :)
461
462 integer :: i, j
463
464 push_sub(spline_end_2)
465
466 do i = 1, size(spl, 1)
467 do j = 1, size(spl, 2)
468 call spline_end_0(spl(i, j))
469 end do
470 end do
471
472 pop_sub(spline_end_2)
473 end subroutine spline_end_2
474
475
476 !------------------------------------------------------------
477 subroutine spline_fit(nrc, rofi, ffit, spl, threshold)
478 integer, intent(in) :: nrc
479 real(real64), contiguous, intent(in) :: rofi(:)
480 real(real64), contiguous, intent(in) :: ffit(:)
481 type(spline_t), intent(inout) :: spl
482 real(real64), optional, intent(in) :: threshold
483
484 !No PUSH SUB, called too often
485
486 assert(nrc > 0)
487
488 spl%x_limit(1) = rofi(1)
489 spl%x_limit(2) = rofi(nrc)
490 call oct_spline_fit(nrc, rofi(1), ffit(1), spl%spl, spl%acc)
491
492 if (present(threshold)) then
493 if (threshold > m_zero) then
494 spl%x_threshold = spline_x_threshold(spl, threshold)
495 else
496 spl%x_threshold = spl%x_limit(2)
497 end if
498 else
499 spl%x_threshold = m_zero
500 end if
501
502 end subroutine spline_fit
503
504 !------------------------------------------------------------
505 real(real64) function spline_eval(spl, x)
506 type(spline_t), intent(in) :: spl
507 real(real64), intent(in) :: x
508
509 spline_eval = oct_spline_eval(x, spl%spl, spl%acc)
510 end function spline_eval
511
512 !------------------------------------------------------------
516 subroutine spline_accel_init(acc)
517 type(c_ptr), intent(out) :: acc
518
519 call oct_spline_accel_alloc(acc)
520 end subroutine spline_accel_init
521
522 !------------------------------------------------------------
523 subroutine spline_accel_end(acc)
524 type(c_ptr), intent(inout) :: acc
525
526 call oct_spline_accel_free(acc)
527 acc = c_null_ptr
528 end subroutine spline_accel_end
529
530 !------------------------------------------------------------
532 real(real64) function spline_eval_accel(spl, x, acc)
533 type(spline_t), intent(in) :: spl
534 real(real64), intent(in) :: x
535 type(c_ptr), intent(in) :: acc
536
537 spline_eval_accel = oct_spline_eval(x, spl%spl, acc)
538 end function spline_eval_accel
539
540 !------------------------------------------------------------
542 logical function spline_same(spl1, spl2)
543 type(spline_t), intent(in) :: spl1, spl2
544
545 spline_same = c_associated(spl1%spl, spl2%spl)
546 end function spline_same
547
548
549 !------------------------------------------------------------
550 subroutine spline_eval8_array(spl, nn, xf)
551 type(spline_t), intent(in) :: spl
552 integer, intent(in) :: nn
553 real(real64), contiguous, intent(inout) :: xf(:)
554
555 assert(nn > 0)
556
557 call oct_spline_eval_array(nn, xf(1), spl%spl, spl%acc)
558 end subroutine spline_eval8_array
559
560
561 !------------------------------------------------------------
562 subroutine spline_evalz_array(spl, nn, xf)
563 type(spline_t), intent(in) :: spl
564 integer, intent(in) :: nn
565 complex(real64), contiguous, intent(inout) :: xf(:)
566
567 assert(nn > 0)
568
569 call oct_spline_eval_arrayz(nn, xf(1), spl%spl, spl%acc)
570 end subroutine spline_evalz_array
571
572 !------------------------------------------------------------
573 subroutine spline_times(a, spl, threshold)
574 real(real64), intent(in) :: a
575 type(spline_t), intent(inout) :: spl
576 real(real64), intent(in) :: threshold
578 integer :: npoints, i
579 real(real64), allocatable :: x(:), y(:)
580
581 push_sub(spline_times)
582
583 npoints = oct_spline_npoints(spl%spl, spl%acc)
584 assert(npoints > 0)
585
586 safe_allocate(x(1:npoints))
587 safe_allocate(y(1:npoints))
589 call oct_spline_x(spl%spl, x(1))
590 call oct_spline_y(spl%spl, y(1))
591 call oct_spline_end(spl%spl, spl%acc)
592 !$omp parallel do simd
593 do i = 1, npoints
594 y(i) = a*y(i)
595 end do
596 call spline_fit(npoints, x, y, spl, threshold)
597
598 safe_deallocate_a(x)
599 safe_deallocate_a(y)
600
601 pop_sub(spline_times)
602 end subroutine spline_times
603
605 !------------------------------------------------------------
606 real(real64) function spline_integral_full(spl) result(res)
607 type(spline_t), intent(in) :: spl
608
609 push_sub(spline_integral_full)
610
611 res = oct_spline_eval_integ_full(spl%spl, spl%acc)
612
613 pop_sub(spline_integral_full)
615
616
617 !------------------------------------------------------------
618 real(real64) pure function spline_integral_limits(spl, a, b) result(res)
619 type(spline_t), intent(in) :: spl
620 real(real64), intent(in) :: a
621 real(real64), intent(in) :: b
623 res = oct_spline_eval_integ(spl%spl, a, b, spl%acc)
624 end function spline_integral_limits
625
626 !------------------------------------------------------------
627 subroutine spline_3dft(spl, splw, threshold, gmax)
628 type(spline_t), intent(in) :: spl
629 type(spline_t), intent(inout) :: splw
630 real(real64), intent(in) :: threshold
631 real(real64), optional, intent(in) :: gmax
632
633 type(spline_t) :: aux
634 real(real64) :: g, dg
635 integer :: np
636 integer :: npoints, i, j
637 real(real64), allocatable :: x(:), y(:), y2(:), xw(:), yw(:)
638
639 push_sub(spline_3dft)
640
641 npoints = oct_spline_npoints(spl%spl, spl%acc)
642 assert(npoints > 0)
643
644 safe_allocate( x(1:npoints))
645 safe_allocate( y(1:npoints))
646 safe_allocate(y2(1:npoints))
647
648 call oct_spline_x(spl%spl, x(1))
649 call oct_spline_y(spl%spl, y(1))
650
651 ! Check whether splw comes with a defined grid, or else build it.
652 if (c_associated(splw%spl)) then
653 np = oct_spline_npoints(splw%spl, splw%acc)
654 safe_allocate(xw(1:np))
655 safe_allocate(yw(1:np))
656 call oct_spline_x(splw%spl, xw(1))
657 ! But now we need to kill the input:
658 call spline_end(splw)
659 else
660 assert(present(gmax))
661 np = 200 ! hard coded value
662 dg = gmax/(np-1)
663 safe_allocate(xw(1:np))
664 safe_allocate(yw(1:np))
665 do i = 1, np
666 g = (i-1)*dg
667 xw(i) = g
668 end do
669 end if
670
671 ! The first point, xw(1) = 0.0 and it has to be treated separately.
672 !$omp parallel do simd
673 do j = 1, npoints
674 y2(j) = m_four*m_pi*y(j)*x(j)**2
675 end do
676 call spline_init(aux)
677 call spline_fit(npoints, x, y2, aux, m_zero)
678 yw(1) = oct_spline_eval_integ_full(aux%spl, aux%acc)
679 call spline_end(aux)
680
681 do i = 2, np
682 !$omp parallel do simd
683 do j = 1, npoints
684 y2(j) = (m_four*m_pi/xw(i))*y(j)*x(j)*sin(xw(i)*x(j))
685 end do
686 call spline_init(aux)
687 call spline_fit(npoints, x, y2, aux, m_zero)
688 yw(i) = oct_spline_eval_integ_full(aux%spl, aux%acc)
689 call spline_end(aux)
690 end do
691
692 call spline_init(splw)
693 call spline_fit(np, xw, yw, splw, threshold)
694
695 safe_deallocate_a(x)
696 safe_deallocate_a(y)
697 safe_deallocate_a(y2)
698 safe_deallocate_a(xw)
699 safe_deallocate_a(yw)
700
701 pop_sub(spline_3dft)
702 end subroutine spline_3dft
703
704
705 !------------------------------------------------------------
706 subroutine spline_besselft(spl, splw, l, threshold, gmax)
707 type(spline_t), intent(in) :: spl
708 type(spline_t), intent(inout) :: splw
709 integer, intent(in) :: l
710 real(real64), optional, intent(in) :: threshold
711 real(real64), optional, intent(in) :: gmax
712
713 type(spline_t) :: aux
714 real(real64) :: dg
715 integer :: np
716 integer :: npoints, i, j
717 real(real64), allocatable :: x(:), y(:), y2(:), xw(:), yw(:)
718
719 push_sub(spline_besselft)
720
721 npoints = oct_spline_npoints(spl%spl, spl%acc)
722 assert(npoints > 0)
723
724 safe_allocate( x(1:npoints))
725 safe_allocate( y(1:npoints))
726 safe_allocate(y2(1:npoints))
727
728 call oct_spline_x(spl%spl, x(1))
729 call oct_spline_y(spl%spl, y(1))
730
731 ! Check whether splw comes with a defined grid, or else build it.
732 if (c_associated(splw%spl)) then
733 np = oct_spline_npoints(splw%spl, splw%acc)
734 safe_allocate(xw(1:np))
735 safe_allocate(yw(1:np))
736 call oct_spline_x(splw%spl, xw(1))
737 ! But now we need to kill the input:
738 call spline_end(splw)
739 else
740 assert(present(gmax))
741 np = 1000 ! hard coded value
742 dg = gmax/(np-1)
743 safe_allocate(xw(1:np))
744 safe_allocate(yw(1:np))
745 !$omp parallel do
746 do i = 1, np
747 xw(i) = (i-1)*dg
748 end do
749 end if
750
751 do i = 1, np
752 !$omp parallel do
753 do j = 1, npoints
754 y2(j) = y(j) * x(j)**2 * loct_sph_bessel(l, x(j)*xw(i))
755 end do
756 call spline_init(aux)
757 call spline_fit(npoints, x, y2, aux)
758 yw(i) = sqrt(m_two/m_pi)*oct_spline_eval_integ_full(aux%spl, aux%acc)
759
760 call spline_end(aux)
761 end do
762
763 call spline_init(splw)
764 call spline_fit(np, xw, yw, splw, threshold)
765
766 safe_deallocate_a(x)
767 safe_deallocate_a(y)
768 safe_deallocate_a(y2)
769 safe_deallocate_a(xw)
770 safe_deallocate_a(yw)
771
772 pop_sub(spline_besselft)
773 end subroutine spline_besselft
774
775
776 !------------------------------------------------------------
777 subroutine spline_cut(spl, cutoff, beta, threshold)
778 type(spline_t), intent(inout) :: spl
779 real(real64), intent(in) :: cutoff
780 real(real64), intent(in) :: beta
781 real(real64), optional,intent(in) :: threshold
782
783 integer :: npoints, i
784 real(real64), allocatable :: x(:), y(:)
785 real(real64) :: exp_arg
786
787 push_sub(spline_cut)
788
789 npoints = oct_spline_npoints(spl%spl, spl%acc)
790 assert(npoints > 0)
791
792 safe_allocate(x(1:npoints))
793 safe_allocate(y(1:npoints))
794
795 call oct_spline_x(spl%spl, x(1))
796 call oct_spline_y(spl%spl, y(1))
797 call oct_spline_end(spl%spl, spl%acc)
798
799 do i = npoints, 1, -1
800 if (x(i)<cutoff) then
801 exit
802 end if
803
804 exp_arg = -beta*(x(i)/cutoff - m_one)**2
805 !To avoid underflows
806 if (exp_arg > m_min_exp_arg) then
807 y(i) = y(i) * exp(exp_arg)
808 else
809 y(i) = m_zero
810 end if
811 end do
812 call spline_fit(npoints, x, y, spl, threshold)
813
814 safe_deallocate_a(x)
815 safe_deallocate_a(y)
816
817 pop_sub(spline_cut)
818 end subroutine spline_cut
819
820
821 !------------------------------------------------------------
826 subroutine spline_div(spla, splb, threshold)
827 type(spline_t), intent(inout) :: spla
828 type(spline_t), intent(in) :: splb
829 real(real64), optional, intent(in) :: threshold
830
831 integer :: npoints, i, new_npoints
832 real(real64), allocatable :: x(:), y(:)
833 real(real64) :: aa
834
835 push_sub(spline_div)
836
837 npoints = oct_spline_npoints(spla%spl, spla%acc)
838 assert(npoints > 0)
839
840 safe_allocate(x(1:npoints))
841 safe_allocate(y(1:npoints))
842
843 call oct_spline_x(spla%spl, x(1))
844 call oct_spline_y(spla%spl, y(1))
845 call oct_spline_end(spla%spl, spla%acc)
846
847 assert(splb%x_limit(2) >= splb%x_limit(1))
848
849 ! As an output, the new spline only contains the new number of points
850 ! that goes up to the cutoff of the splb spline
851 new_npoints = 0
852 do i = npoints, 1, -1
853 if (x(i) > splb%x_limit(2)-tol_x) cycle
854 aa = spline_eval(splb, x(i))
855 y(i) = y(i)/aa
856 new_npoints = new_npoints + 1
857 end do
858 assert(new_npoints > 0)
859
860 call spline_fit(new_npoints, x, y, spla, threshold)
861
862 safe_deallocate_a(x)
863 safe_deallocate_a(y)
864
865 pop_sub(spline_div)
866 end subroutine spline_div
867
868 !------------------------------------------------------------
869 !Force all the values of the spline to be positive
870 !------------------------------------------------------------
871 subroutine spline_force_pos(spl, threshold)
872 type(spline_t), intent(inout) :: spl
873 real(real64), intent(in) :: threshold
874
875 integer :: npoints, i
876 real(real64), allocatable :: x(:), y(:)
877
878 push_sub(spline_force_pos)
879
880 npoints = oct_spline_npoints(spl%spl, spl%acc)
881 assert(npoints > 0)
882
883 safe_allocate(x(1:npoints))
884 safe_allocate(y(1:npoints))
885
886 call oct_spline_x(spl%spl, x(1))
887 call oct_spline_y(spl%spl, y(1))
888 call oct_spline_end(spl%spl, spl%acc)
889
890 !$omp parallel do simd
891 do i = npoints, 1, -1
892 y(i) = max(y(i),m_zero)
893 end do
894
895 call spline_fit(npoints, x, y, spl, threshold)
896
897 safe_deallocate_a(x)
898 safe_deallocate_a(y)
899
900 pop_sub(spline_force_pos)
901 end subroutine spline_force_pos
902
903
904 !------------------------------------------------------------
905 subroutine spline_mult(spla, splb, threshold)
906 type(spline_t), intent(inout) :: spla
907 type(spline_t), intent(in) :: splb
908 real(real64), intent(in) :: threshold
909
910 integer :: npoints, i, new_npoints
911 real(real64), allocatable :: x(:), y(:)
912 real(real64) :: aa
913
914 push_sub(spline_mult)
915
916 npoints = oct_spline_npoints(spla%spl, spla%acc)
917 if(npoints <= 0) then
918 pop_sub(spline_mult)
919 return
920 end if
921
922 safe_allocate(x(1:npoints))
923 safe_allocate(y(1:npoints))
924
925 call oct_spline_x(spla%spl, x(1))
926 call oct_spline_y(spla%spl, y(1))
927 call oct_spline_end(spla%spl, spla%acc)
928
929 assert(splb%x_limit(2) >= splb%x_limit(1))
930
931 ! As an output, the new spline only contains the new number of points
932 ! that goes up to the cutoff of the splb spline
933 new_npoints = 0
934 do i = npoints, 1, -1
935 if (x(i) > splb%x_limit(2) - tol_x) cycle
936 aa = spline_eval(splb, x(i))
937 y(i) = y(i)*aa
938 new_npoints = new_npoints + 1
939 end do
940
941 call spline_fit(new_npoints, x, y, spla, threshold)
942
943 safe_deallocate_a(x)
944 safe_deallocate_a(y)
945
946 pop_sub(spline_mult)
947 end subroutine spline_mult
948
949
950 !------------------------------------------------------------
951 subroutine spline_der(spl, dspl, threshold)
952 type(spline_t), intent(in) :: spl
953 type(spline_t), intent(inout) :: dspl
954 real(real64), intent(in) :: threshold
955
956 integer :: npoints, i
957 real(real64), allocatable :: x(:), y(:)
958
959 push_sub(spline_der)
960
961 ! Use the grid of dspl if it is present, otherwise use the same one of spl.
962 if (.not. c_associated(dspl%spl)) then ! use the grid of spl
963 npoints = oct_spline_npoints(spl%spl, spl%acc)
964 assert(npoints > 0)
965 safe_allocate(x(1:npoints))
966 safe_allocate(y(1:npoints))
967 call oct_spline_x(spl%spl, x(1))
968 else ! use the grid of dspl
969 npoints = oct_spline_npoints(dspl%spl, dspl%acc)
970 assert(npoints > 0)
971 safe_allocate(x(1:npoints))
972 safe_allocate(y(1:npoints))
973 call oct_spline_x(dspl%spl, x(1))
974 end if
975 do i = 1, npoints
976 y(i) = oct_spline_eval_der(x(i), spl%spl, spl%acc)
977 end do
978 call spline_fit(npoints, x, y, dspl, threshold)
979
980 safe_deallocate_a(x)
981 safe_deallocate_a(y)
982
983 pop_sub(spline_der)
984 end subroutine spline_der
985
986
987 !------------------------------------------------------------
989 subroutine spline_der2(spl, dspl, threshold, grid)
990 type(spline_t), intent(in) :: spl
991 type(spline_t), intent(inout) :: dspl
992 real(real64), intent(in) :: threshold
993 real(real64), contiguous, optional, intent(in) :: grid(:)
994
995 integer :: npoints, i
996 real(real64), allocatable :: x(:), y(:)
997
998 push_sub(spline_der2)
999
1000 ! Use the grid of dspl if it is present, otherwise use the same one of spl.
1001 if (.not. c_associated(dspl%spl)) then ! use the grid of spl
1002 npoints = oct_spline_npoints(spl%spl, spl%acc)
1003 assert(npoints > 0)
1004 safe_allocate(x(1:npoints))
1005 safe_allocate(y(1:npoints))
1006 if (present(grid)) then
1007 x(:) = grid(:)
1008 else
1009 call oct_spline_x(spl%spl, x(1))
1010 end if
1011 else ! use the grid of dspl
1012 npoints = oct_spline_npoints(dspl%spl, dspl%acc)
1013 assert(npoints > 0)
1014 safe_allocate(x(1:npoints))
1015 safe_allocate(y(1:npoints))
1016 call oct_spline_x(dspl%spl, x(1))
1017 end if
1018 do i = 1, npoints
1019 y(i) = oct_spline_eval_der2(x(i), spl%spl, spl%acc)
1020 end do
1021 call spline_fit(npoints, x, y, dspl, threshold)
1022
1023 safe_deallocate_a(x)
1024 safe_deallocate_a(y)
1025
1026 pop_sub(spline_der2)
1027 end subroutine spline_der2
1028
1029
1030 !------------------------------------------------------------
1031 subroutine spline_print_0(spl, iunit)
1032 type(spline_t), intent(in) :: spl
1033 integer, intent(in) :: iunit
1034
1035 integer :: np, i
1036 real(real64), allocatable :: x(:), y(:)
1037
1038 push_sub(spline_print_0)
1039
1040 np = oct_spline_npoints(spl%spl, spl%acc)
1041 assert(np > 0)
1042 safe_allocate(x(1:np))
1043 safe_allocate(y(1:np))
1044
1045 call oct_spline_x(spl%spl, x(1))
1046 call oct_spline_y(spl%spl, y(1))
1047 do i = 1, np
1048 write(iunit, '(2es18.8E3)') x(i), y(i)
1049 end do
1050
1051 safe_deallocate_a(x)
1052 safe_deallocate_a(y)
1053
1054 pop_sub(spline_print_0)
1055 end subroutine spline_print_0
1056
1057
1058 !------------------------------------------------------------
1060 subroutine spline_print_1(spl, iunit)
1061 type(spline_t), intent(in) :: spl(:)
1062 integer, intent(in) :: iunit
1063
1064 integer :: np, i, n, j
1065 real(real64), allocatable :: x(:)
1066 real(real64) :: x_limit
1067 integer :: max_x_index
1068
1069 push_sub(spline_print_1)
1070
1071 n = size(spl)
1072 if (n <= 0) then
1073 pop_sub(spline_print_1)
1074 return
1075 end if
1076
1077 x_limit = spl(1)%x_limit(2)
1078 max_x_index = 1
1079
1080 ! Not all splines have the same number of points
1081 do i = 2, n
1082 if (x_limit < spl(i)%x_limit(2)) then
1083 x_limit = spl(i)%x_limit(2)
1084 max_x_index = i
1085 end if
1086 end do
1087
1088 np = oct_spline_npoints(spl(max_x_index)%spl, spl(max_x_index)%acc)
1089 safe_allocate(x(1:np))
1090 call oct_spline_x(spl(max_x_index)%spl, x(1))
1091
1092 do i = 1, np
1093 write(iunit, '(es18.8E3)', advance='no') x(i)
1094 do j = 1, n
1095 if (x(i) <= spl(j)%x_limit(2)) then
1096 write(iunit, '(es18.8E3)', advance='no') spline_eval(spl(j), x(i))
1097 else
1098 write(iunit, '(es18.8E3)', advance='no') m_zero
1099 end if
1100 end do
1101 write(iunit, '(a)', advance='yes') ''
1102 end do
1104 safe_deallocate_a(x)
1105
1106 pop_sub(spline_print_1)
1107 end subroutine spline_print_1
1108
1109
1110 !------------------------------------------------------------
1112 subroutine spline_print_2(spl, iunit)
1113 type(spline_t), intent(in) :: spl(:, :)
1114 integer, intent(in) :: iunit
1115
1116 integer :: np, i, n1, n2, j, k
1117 real(real64), allocatable :: x(:)
1118 real(real64) :: x_limit
1119 integer :: max_x_index(2)
1120
1121 push_sub(spline_print_2)
1122
1123 n1 = size(spl, 1)
1124 n2 = size(spl, 2)
1125 if (n1 * n2 <= 0) then
1126 pop_sub(spline_print_2)
1127 return
1128 end if
1129
1130 ! Not all splines have the same number of points
1131 max_x_index = 1
1132 x_limit = spl(1,1)%x_limit(2)
1133 do i = 1, n1
1134 do j = 1, n2
1135 if (x_limit < spl(i,j)%x_limit(2)) then
1136 x_limit = spl(i,j)%x_limit(2)
1137 max_x_index = (/i,j/)
1138 end if
1139 end do
1140 end do
1141
1142 np = oct_spline_npoints(spl(max_x_index(1), max_x_index(2))%spl, spl(max_x_index(1), max_x_index(2))%acc)
1143 safe_allocate(x(1:np))
1144
1145 call oct_spline_x(spl(max_x_index(1), max_x_index(2))%spl, x(1))
1146
1147 do i = 1, np
1148 write(iunit, '(es18.8E3)', advance='no') x(i)
1149 do k = 1, n2
1150 do j = 1, n1
1151 if (x(i) <= spl(j,k)%x_limit(2)) then
1152 write(iunit, '(es18.8E3)', advance='no') spline_eval(spl(j, k), x(i))
1153 else
1154 write(iunit, '(es18.8E3)', advance='no') m_zero
1155 end if
1156 end do
1157 end do
1158 write(iunit, '(a)', advance='yes') ''
1159 end do
1160
1161 safe_deallocate_a(x)
1162
1163 pop_sub(spline_print_2)
1164 end subroutine spline_print_2
1165
1166
1167 !------------------------------------------------------------
1170 real(real64) function spline_x_threshold(spl, threshold) result(r)
1171 type(spline_t), intent(in) :: spl
1172 real(real64), intent(in) :: threshold
1173
1174 integer :: ii, jj
1175 real(real64), parameter :: dx = 0.01_real64
1176
1177 ! No PUSH SUB, called too often.
1178 assert(spl%x_limit(2) >= spl%x_limit(1))
1179
1180 call profiling_in('SPLINE_CUTOFF_RADIUS')
1181
1182 jj = floor(spl%x_limit(2)/dx) + 1
1183 ! Numerical errors can lead to jj to be overestimated by 1
1184 ! This test prevents this to occur
1185 do while(dx*(jj-1) > spl%x_limit(2) - m_half * dx)
1186 jj = jj-1
1187 end do
1188
1189 do ii = jj, 1, -1
1190 r = dx*(ii-1)
1191 if (abs(spline_eval(spl, r)) > threshold) exit
1192 end do
1193
1194 call profiling_out('SPLINE_CUTOFF_RADIUS')
1195
1196 end function spline_x_threshold
1197
1198 ! -------------------------------------------------------
1199
1200 real(real64) pure function spline_range_min(this) result(range)
1201 type(spline_t), intent(in) :: this
1202
1203 range = this%x_limit(1)
1204
1205 end function spline_range_min
1206
1207 ! -------------------------------------------------------
1208
1209 real(real64) pure function spline_range_max(this) result(range)
1210 type(spline_t), intent(in) :: this
1211
1212 range = this%x_limit(2)
1213
1214 end function spline_range_max
1215
1216 ! -------------------------------------------------------
1217 subroutine spline_generate_shifted_grid(this, x_shifted)
1218 type(spline_t), intent(in) :: this
1219 real(real64), allocatable, intent(inout) :: x_shifted(:)
1220
1221 real(real64), allocatable :: x(:)
1222 integer :: npoints, i
1223
1225
1226 ! We shift the grid by half of a spacing to force interpolation of the function
1227 ! This is useful for getting back the derivative on the same grid
1228 npoints = oct_spline_npoints(this%spl, this%acc)
1229 safe_allocate(x(1:npoints))
1230 safe_allocate(x_shifted(1:npoints))
1231 call oct_spline_x(this%spl, x(1))
1232 do i = 1, npoints -1
1233 x_shifted(i) = m_half*(x(i) + x(i+1))
1234 end do
1235 x_shifted(npoints) = x(npoints)
1236 safe_deallocate_a(x)
1237
1239 end subroutine spline_generate_shifted_grid
1240
1241end module splines_oct_m
1243!! Local Variables:
1244!! mode: f90
1245!! coding: utf-8
1246!! End:
Both the filling of the function, and the retrieval of the values may be done using single- or double...
Definition: splines.F90:291
Some operations may be done for one spline-function, or for an array of them.
Definition: splines.F90:304
The integral may be done with or without integration limits, but we want the interface to be common.
Definition: splines.F90:298
double exp(double __x) __attribute__((__nothrow__
double sin(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
double floor(double __x) __attribute__((__nothrow__
/* 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 spline_der(spl, dspl, threshold)
Definition: splines.F90:1024
subroutine, public spline_accel_init(acc)
Allocate a GSL accelerator owned by the caller. Used with spline_eval_accel (one accelerator per thre...
Definition: splines.F90:589
subroutine spline_end_0(spl)
Definition: splines.F90:501
subroutine, public spline_force_pos(spl, threshold)
Definition: splines.F90:944
real(real64) pure function spline_integral_limits(spl, a, b)
Definition: splines.F90:691
subroutine, public spline_fit(nrc, rofi, ffit, spl, threshold)
Definition: splines.F90:550
real(real64) function, public spline_x_threshold(spl, threshold)
Determines the largest value of x for which the spline values are above the threshold.
Definition: splines.F90:1243
subroutine, public spline_3dft(spl, splw, threshold, gmax)
Definition: splines.F90:700
subroutine, public spline_der2(spl, dspl, threshold, grid)
Returns a spline that contains the second derivative of the original spline.
Definition: splines.F90:1062
subroutine spline_end_2(spl)
Definition: splines.F90:532
real(real64), parameter tol_x
Tolerance for checking if x is inside or outside the limits of the splines.
Definition: splines.F90:443
subroutine, public spline_besselft(spl, splw, l, threshold, gmax)
Definition: splines.F90:779
logical function, public spline_same(spl1, spl2)
True if both splines refer to the same underlying GSL spline.
Definition: splines.F90:615
real(real64) pure function, public spline_range_min(this)
Definition: splines.F90:1273
subroutine spline_end_1(spl)
Definition: splines.F90:516
subroutine spline_print_1(spl, iunit)
Print debug information for a 1D array of splines.
Definition: splines.F90:1133
real(real64) function spline_integral_full(spl)
Definition: splines.F90:679
real(real64) function, public spline_eval(spl, x)
Definition: splines.F90:578
subroutine spline_init_0(spl)
Definition: splines.F90:449
subroutine spline_print_0(spl, iunit)
Definition: splines.F90:1104
subroutine spline_print_2(spl, iunit)
Print debug information for a 2D array of splines.
Definition: splines.F90:1185
subroutine spline_eval8_array(spl, nn, xf)
Definition: splines.F90:623
subroutine spline_init_1(spl)
Definition: splines.F90:467
subroutine, public spline_cut(spl, cutoff, beta, threshold)
Definition: splines.F90:850
subroutine, public spline_div(spla, splb, threshold)
Returns the values of spla divided by the values of splb.
Definition: splines.F90:899
real(real64) pure function, public spline_range_max(this)
Definition: splines.F90:1282
real(real64) function, public spline_eval_accel(spl, x, acc)
Same as spline_eval, with a caller-owned accelerator (see spline_accel_init).
Definition: splines.F90:605
subroutine, public spline_times(a, spl, threshold)
Definition: splines.F90:646
subroutine spline_init_2(spl)
Definition: splines.F90:483
subroutine, public spline_accel_end(acc)
Definition: splines.F90:596
subroutine, public spline_mult(spla, splb, threshold)
Definition: splines.F90:978
subroutine spline_evalz_array(spl, nn, xf)
Definition: splines.F90:635
subroutine, public spline_generate_shifted_grid(this, x_shifted)
Definition: splines.F90:1290
the basic spline datatype
Definition: splines.F90:281