Skip to content

Commit 7613f03

Browse files
committed
update documentation
1 parent aa64465 commit 7613f03

File tree

9 files changed

+312
-33
lines changed

9 files changed

+312
-33
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,45 @@
88

99
A Julia wrapper around the [SHTns](https://bitbucket.org/nschaeff/shtns) spherical harmonic transform library.
1010

11-
# This is WORK IN PROGRESS and will certainly have BREAKING changes.
12-
13-
For the moment not a lot is tested. There are wrappers generated by Clang.jl to call most functions exported by the C library. The *more Julian* API is under development. The documentation will come, for the time being I refer to the [documentation of the C library](https://nschaeff.bitbucket.io/shtns/).
11+
> [!WARNING]
12+
> This is WORK IN PROGRESS and will certainly have BREAKING changes.
1413
1514
# Installation
1615

1716
```julia
1817
import Pkg; Pkg.add("SHTns")
1918
```
2019

20+
# Example usage
21+
```julia
22+
using SHTns
23+
24+
cfg = SHTnsCfg(64) #define configuration
25+
26+
θ,ϕ = SHTns.grid(cfg) #get (co-)latitude and longitude
27+
d = @. 0.3*sin') * sin(θ) #create some spatial data (0.3 Y₁¹)
28+
29+
q = SHTns.analys(cfg,d) #transform to spectral space
30+
q[SHTns.LM(cfg, 1,1)] 0.3*sqrt(2π/3)*im #true
31+
32+
d2 = SHTns.synth(cfg,q) #transform back to spatial space
33+
d2 d #true
34+
```
35+
36+
Or we can do the same on a CUDA-enabled GPU!
37+
```julia
38+
using CUDA
39+
40+
cfg_gpu = SHTnsCfg(64; shtype=SHTns.QuickInit(;gpu=true))
41+
42+
d_gpu = CuArray(d)
43+
44+
q_gpu = SHTns.analys(cfg_gpu,d_gpu)
45+
Array(q_gpu)[SHTns.LM(cfg, 1,1)] 0.3*sqrt(2π/3)*im
46+
47+
d2_gpu = SHTns.synth(cfg_gpu,q_gpu)
48+
(d_gpu d2_gpu) && (Array(d2_gpu) d) #true
49+
```
2150

2251
# License
2352

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
SHTns = "5dc4ea12-f621-4038-8458-a22eae100703"

docs/make.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ makedocs(;
55
# format=Documenter.HTML(),
66
pages=[
77
"Home" => "index.md",
8+
"Quickstart" => "quickstart.md",
9+
"Advanced usage" => "advanced.md",
810
"API" => "api.md",
911
],
1012
repo="https://github.com/fgerick/SHTns.jl/blob/{commit}{path}#L{line}",

docs/src/advanced.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Advanced usage
2+
3+
## Configuring the transform
4+
5+
The [`SHTnsCfg`](@ref) configuration object takes several keywords allowing the user to tweak the spherical harmonic transform.
6+
7+
The simplest configuration takes only the maximum spherical harmonic degree `lmax` as an argument
8+
```@example advanced
9+
using SHTns
10+
11+
lmax = 100
12+
cfg = SHTnsCfg(lmax)
13+
```
14+
15+
We can also define the grid resolution (`nlat` and `nphi`) first, and then `lmax`, as well as the maximum azimuthal degree `mmax`, as well as the _resolution_ in ``m`` through `mres`.
16+
```@example advanced
17+
nlat = 100
18+
nphi = 2nlat+1
19+
lmax = 2nlat÷3
20+
mmax = 5
21+
mres = 1
22+
cfg = SHTnsCfg(lmax,mmax,mres,nlat,nphi)
23+
```
24+
25+
The transform type [`SHTnsType`](@ref) can be varied through the `shtype` keyword
26+
```@example advanced
27+
cfg = SHTnsCfg(lmax; shtype = SHTns.Gauss())
28+
```
29+
30+
See also the documentation of the C library on [shtns_type](https://nschaeff.bitbucket.io/shtns/shtns_8h.html#abdccbb8fbce176dbe189d494c94f0f7b) and [grid layouts](https://nschaeff.bitbucket.io/shtns/spat.html).
31+
32+
We can also change the normalization of the spherical harmonics, for example to the Schmidt semi-normalization without [Condon-Shortley phase](https://mathworld.wolfram.com/Condon-ShortleyPhase.html) (default enabled)
33+
```@example advanced
34+
cfg = SHTnsCfg(lmax; norm = SHTns.Schmidt(; cs_phase=false))
35+
```
36+
See also [Spherical Harmonics storage and normalization](https://nschaeff.bitbucket.io/shtns/spec.html) of the C library.
37+
38+
39+
`SHTns` also supports complex to complex transforms (instead of real spatial space to complex spectral space, which is the default).
40+
41+
```@example advanced
42+
cfg = SHTnsCfg(lmax; transform=Complex)
43+
```
44+
45+
## GPU
46+
47+
Using `SHTns` on a CUDA-enabled GPU is straightforward.
48+
Simply install and import [`CUDA.jl`](https://github.com/JuliaGPU/CUDA.jl):
49+
50+
```julia
51+
import Pkg; Pkg.add("CUDA")
52+
using CUDA
53+
```
54+
55+
Then, configure your [`SHTnsCfg`](@ref) using an [`SHTnsType`](@ref) with the keyword `gpu=true`.
56+
```julia
57+
cfg_gpu = SHTnsCfg(64; shtype=SHTns.QuickInit(;gpu=true))
58+
```
59+
60+
The rest remains almost identical to the CPU transform
61+
```julia
62+
θ,ϕ = SHTns.grid(cfg) #get (co-)latitude and longitude
63+
d = @. 0.3*sin') * sin(θ) #create some spatial data (0.3 Y₁¹) - on CPU!
64+
65+
d_gpu = CuArray(d) #send spatial data to GPU
66+
67+
q_gpu = SHTns.analys(cfg_gpu,d_gpu) #transform to spectral space
68+
69+
#scalar indexing not allowed on GPU arrays, therefore call Array(q_gpu) to send back to CPU
70+
y11_coeff = Array(q_gpu)[SHTns.LM(cfg, 1,1)]
71+
y11_coeff 0.3*sqrt(2π/3)*im #true
72+
73+
d2_gpu = SHTns.synth(cfg_gpu,q_gpu) #transform backto spatial space
74+
d2_gpu d_gpu #true
75+
```
76+
77+
78+
> [!NOTE]
79+
> ROCm GPUs may be supported in future releases of SHTns.jl (already available in the C library).
80+
81+
## Batched transform
82+
83+
`SHTns` supports batched transforms, i.e. multiple spherical harmonic transforms together. For example transforms on spherical surfaces at different radii at the same time.
84+
85+
One can exploit this feature using the keyword `howmany`, to give the configuration the number of transforms to be performed.
86+
87+
```@example advanced
88+
howmany = 10
89+
cfg = SHTnsCfg(lmax; howmany)
90+
91+
θ,ϕ = SHTns.grid(cfg)
92+
93+
# create spatial array (nlat_padded x nphi x howmany)
94+
d = zeros(cfg.nlat_padded, cfg.nphi, howmany)
95+
d1 = @. 0.3*sin(ϕ') * sin(θ)
96+
for i=1:howmany
97+
@. d[:,:,i] = i*d1
98+
end
99+
100+
q = SHTns.analys(cfg,d)
101+
d2 = SHTns.synth(cfg,q)
102+
d2 ≈ d #true
103+
```
104+
105+
> [!NOTE]
106+
> Complex to complex transforms are currently not supported in the batched transform.
107+
108+

docs/src/api.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
11
# API
22

3-
```@index
3+
4+
## Configuration
5+
6+
```@docs
7+
SHTns.SHTnsCfg
8+
```
9+
### Transform types (SHTnsType)
10+
11+
```@docs
12+
SHTns.SHTnsType
13+
```
14+
15+
```@docs
16+
SHTns.Gauss
17+
SHTns.GaussFly
18+
SHTns.QuickInit
19+
SHTns.RegDCT
20+
SHTns.RegFast
21+
SHTns.RegPoles
22+
```
23+
24+
### Spherical harmonic normalizations (SHTnsNorm)
25+
26+
```@docs
27+
SHTns.ForRotations
28+
SHTns.FourPi
29+
SHTns.Orthonormal
30+
SHTns.Schmidt
431
```
532

6-
```@autodocs
7-
Modules = [SHTns]
33+
## Transforms
34+
35+
The transforms from spatial space to spectral space, also called _analysis_, are available through [`SHTns.analys`](@ref) and [`SHTns.analys!`](@ref).
36+
The transforms from spectral space to spatial space, also called _synthesis_, are available through [`SHTns.synth`](@ref) and [`SHTns.synth!`](@ref).
37+
38+
```@docs
39+
SHTns.analys
40+
SHTns.analys!
41+
SHTns.synth
42+
SHTns.synth!
43+
```
44+
45+
## Miscellaneous
46+
47+
```@docs
48+
SHTns.LM
49+
SHTns.gauss_weights
50+
SHTns.grid
851
```

docs/src/quickstart.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Quickstart
2+
3+
Load the package
4+
```@example quickstart
5+
using SHTns
6+
```
7+
8+
Define your [`SHTnsCfg`](@ref) configuration for some maximum spherical harmonic degree `lmax`
9+
```@example quickstart
10+
lmax = 64
11+
cfg = SHTnsCfg(lmax)
12+
```
13+
14+
15+
Create some spatial data `d`, using the latitude and longitude from [`SHTns.grid`](@ref)
16+
```@example quickstart
17+
function spat_func_test(theta, phi)
18+
return 1.0 + 0.01*cos(theta) + 0.1*(3cos(theta)*cos(theta) - 1.0) + #// Y00, Y10, Y20
19+
(cos(phi) + 0.3*sin(phi)) * sin(theta) + #// Y11
20+
(cos(2phi) + 0.1*sin(2phi)) * sin(theta)*sin(theta) * (7.0* cos(theta)*cos(theta) - 1.0) * 3/8 #// Y42
21+
end
22+
23+
θ,ϕ = SHTns.grid(cfg)
24+
d = spat_func_test.(θ, ϕ')
25+
nothing #hide
26+
```
27+
28+
Transform the data `d` to the spectral coefficients `q` (i.e. perform an analysis)
29+
```@example quickstart
30+
q = SHTns.analys(cfg, d)
31+
nothing #hide
32+
```
33+
34+
Transform back to spatial data (i.e. perform a synthesis)
35+
```@example quickstart
36+
d2 = SHTns.synth(cfg, q)
37+
d2 ≈ d
38+
```
39+
40+
These transforms can also be performed in place
41+
```@example quickstart
42+
q2 = similar(q)
43+
SHTns.analys!(cfg, d2, q2)
44+
q2 ≈ q
45+
```
46+
!!! warning
47+
`d2` is overwritten during [`SHTns.analys!`](@ref).
48+
49+
```@example quickstart
50+
SHTns.synth!(cfg, q2, d2)
51+
d2 ≈ d
52+
```
53+
54+

src/SHTns.jl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ for (type, enumtype) in [(:Orthonormal, :sht_orthonormal), (:FourPi, :sht_fourpi
7373
end
7474
end
7575

76+
"""
77+
abstract type SHTnsType
78+
79+
SHTnsType is an abstract type for the spherical harmonic transform types. All subtypes contain the following keyword arguments:
80+
81+
- `contiguous_lat::Bool=true`
82+
- `contiguous_phi::Bool=false`
83+
- `padding::Bool=false`
84+
- `gpu::Bool=false`
85+
- `southpolefirst::Bool=false`
86+
- `float32::Bool=false`
87+
"""
7688
abstract type SHTnsType end
7789

7890
for (type, enumtype) in [(:Gauss, :sht_gauss), (:RegFast, :sht_reg_fast), (:RegDCT, :sht_reg_dct), (:QuickInit, :sht_quick_init), (:RegPoles, :sht_reg_poles), (:GaussFly, :sht_gauss_fly) ] #(:Auto, :sht_auto),
@@ -117,7 +129,27 @@ function _init_checks(shtype, lmax, mmax, mres, nlat, nphi)
117129
end
118130

119131
"""
120-
mutable struct SHTnsCfg{N<:SHTnsNorm, T<:SHTnsType}
132+
mutable struct SHTnsCfg{TR<:Union{Real,Complex}, N<:SHTnsNorm, T<:SHTnsType}
133+
134+
- `cfg::Ptr{shtns_info}`
135+
- `norm::N`
136+
- `shtype::T`
137+
- `robert_form::Bool`
138+
- `nlm::Int`
139+
- `lmax::Int`
140+
- `mmax::Int`
141+
- `mres::Int`
142+
- `nlat_2::Int`
143+
- `nlat::Int`
144+
- `nphi::Int`
145+
- `nspat::Int`
146+
- `li::Vector{Int}`
147+
- `mi::Vector{Int}`
148+
- `ct::Vector{Float64}`
149+
- `st::Vector{Float64}`
150+
- `nlat_padded::Int`
151+
- `nlm_cplx::Int`
152+
- `howmany::Int`
121153
122154
Configuration of spherical harmonic transform.
123155
"""

src/analys.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,24 @@ function analys!(cfg::SHTnsCfg{Complex,T,N}, ur::Tv, utheta::Tv, uphi::Tv, qlm,
7070
return spat_cplx_to_SHqst(cfg.cfg, ur, utheta, uphi, qlm, slm, tlm)
7171
end
7272

73+
"""
74+
analys!(cfg::SHTnsCfg, v, qlm)
75+
analys!(cfg::SHTnsCfg, utheta, uphi, slm, tlm)
76+
analys!(cfg::SHTnsCfg, ur, utheta, uphi, qlm, slm, tlm)
77+
78+
In-place transforms of the spatial data into spherical harmonics coefficients for scalar, 2D or 3D fields.
79+
!!! warning
80+
This function modifies the input arrays `v`, `ur`, `utheta` and `uphi`.
81+
"""
82+
function analys! end
83+
84+
"""
85+
analys(cfg::SHTnsCfg, v)
86+
analys(cfg::SHTnsCfg, utheta, uphi)
87+
analys(cfg::SHTnsCfg, ur, utheta, uphi)
88+
89+
Transforms the spatial data into spherical harmonics coefficients `qlm`; `slm` and `tlm`; `qlm`, `slm` and `tlm` for scalar; 2D; 3D fields, respectively.
90+
"""
91+
function analys end
7392

7493
export analys, analys!

0 commit comments

Comments
 (0)