A Fortran 2003 example using iso_c_binding program that performs backward and forward Spherical Harmonic Transforms using SHTns.
A Fortran 2003 example using iso_c_binding program that performs backward and forward Spherical Harmonic Transforms using SHTns. Compile using :
1program sht_example
2
3
4
5
6
7 use iso_c_binding
8 use iso_fortran_env, only: real64
9
10 implicit none
11
12 include 'shtns.f03'
13
14 integer, parameter :: dp=real64
15 real(dp), parameter :: pi=acos(-1.0_dp)
16
17 type(shtns_info), pointer :: shtns
18 real(dp), pointer :: cosTheta(:), sinTheta(:)
19 type(c_ptr) :: shtns_c
20
21 integer :: lmax, mmax, mres, nthreads
22 integer :: nlat, nphi, n_p, lm, l, m, lmStart, lmStop
23 integer :: nlm, norm, nout, layout
24 real(dp) :: eps_polar
25 real(dp), allocatable :: Sh(:,:)
26 complex(dp), allocatable :: Slm(:), ShT(:)
27
28
29
30 lmax = 5
31 mmax = 3
32 mres = 1
33 nphi = 10
34 nlat = 64
35
36
37 eps_polar = 1.0e-10_dp
38
39
42
43 call shtns_verbose(2)
45 print*, 'nthreads=', nthreads
46
49
50
51 call c_f_pointer(cptr=shtns_c, fptr=shtns)
52 call c_f_pointer(cptr=shtns%ct, fptr=costheta, shape=[shtns%nlat])
53 call c_f_pointer(cptr=shtns%st, fptr=sintheta, shape=[shtns%nlat])
54
55 print*, 'cosTheta', costheta
56 print*, 'sinTheta', sintheta
57
58 print*, 'NLM=', shtns%nlm
59
60 allocate( sh(1:shtns%nphi,1:shtns%nlat), slm(1:shtns%nlm), sht(1:shtns%nlat) )
61 slm(:)=0.0_dp
62
63
64 l = 1
65 m = 0
66 lm = shtns_lmidx(shtns_c,l,m)
67
68 slm(lm)=(1.0_dp,0.0_dp)
69
70
71 lm = 17
72 l = shtns_lm2l(shtns_c,lm)
73 m = shtns_lm2m(shtns_c,lm)
74 print*, '(l,m)=', l,m
75
76
77 call sh_to_spat(shtns_c, slm, sh)
78
79 print*, 'Y(1,0)', sh(1,:)
80
81
82 call spat_to_sh(shtns_c, sh, slm)
83
84
85 print*, 'S(1,0)', slm(lm)
86
87
88 lmstart = shtns_lmidx(shtns_c,0,0)
89 lmstop = shtns_lmidx(shtns_c,lmax,0)
90 call sh_to_spat_ml(shtns_c, 0, slm(lmstart:lmstop), sht, lmax)
91 print*, 'Legendre only', sht
92
93 deallocate(sh, slm, sht)
96
97end program
void shtns_destroy(shtns_cfg)
free memory of given config, which cannot be used afterwards.
Definition sht_init.c:1268
int shtns_use_threads(int num_threads)
Enables multi-thread transform using OpenMP with num_threads (if available). Returns number of thread...
Definition sht_init.c:1605
int shtns_set_grid(shtns_cfg, enum shtns_type flags, double eps, int nlat, int nphi)
Precompute everything for a given spatial grid. Use for advanced initialization, after shtns_create.
Definition sht_init.c:1544
shtns_cfg shtns_create(int lmax, int mmax, int mres, enum shtns_norm norm)
Defines the sizes of the spectral description. Use for advanced initialization.
Definition sht_init.c:1100
void shtns_unset_grid(shtns_cfg)
unset the grid.
Definition sht_init.c:1258
@ sht_gauss
use gaussian grid and quadrature. Allows on-the-fly or matrix-based algorithms.
Definition shtns.h:58
@ sht_orthonormal
orthonormalized spherical harmonics (default).
Definition shtns.h:48