SHTns 3.6.1
SHT_example.f90

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 :

make SHT_f90_ex
See also
fortran
Fortran 2003 iso_c_binding
1program sht_example
2 !
3 ! This is a modern Fortran example that shows how to set-up the spherical
4 ! harmonic transforms using SHTns.
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 !-- Set the size of the transforms
30 lmax = 5
31 mmax = 3
32 mres = 1
33 nphi = 10
34 nlat = 64
35
36 !-- Polar optimisation threshold
37 eps_polar = 1.0e-10_dp
38
39 !-- Norm and layout for SHTs
40 norm = sht_orthonormal + sht_no_cs_phase
41 layout = sht_gauss + sht_phi_contiguous
42
43 call shtns_verbose(2)
44 nthreads = shtns_use_threads(0)
45 print*, 'nthreads=', nthreads
46
47 shtns_c = shtns_create(lmax, mmax, mres, norm)
48 call shtns_set_grid(shtns_c, layout, eps_polar, nlat, nphi)
49
50 !-- C/Fortran pointer mapping
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 !-- Get lm index from the (l,m) pair
64 l = 1
65 m = 0
66 lm = shtns_lmidx(shtns_c,l,m)
67 !-- Set the l=1, m=0 mode to 1.0
68 slm(lm)=(1.0_dp,0.0_dp)
69
70 !-- Get (l,m) from lm
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 !-- Spec -> Spat
77 call sh_to_spat(shtns_c, slm, sh)
78
79 print*, 'Y(1,0)', sh(1,:) ! It should be sqrt(3/4/pi) * cos(theta)
80
81 !-- Spat -> Spec, DOES NOT PRESERVE THE CONTENT OF INPUT 'Sh'
82 call spat_to_sh(shtns_c, sh, slm)
83
84 !-- print S(1,0) to check it 1.0 is recovered.
85 print*, 'S(1,0)', slm(lm)
86
87 !-- Legendre only for m=0
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)
94 call shtns_unset_grid(shtns_c)
95 call shtns_destroy(shtns_c)
96
97end program
void shtns_destroy(shtns_cfg)
free memory of given config, which cannot be used afterwards.
Definition sht_init.c:1287
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:1635
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:1575
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:1112
void shtns_unset_grid(shtns_cfg)
unset the grid.
Definition sht_init.c:1277
@ 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