SHTns  2.6.5
shtns.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015 Centre National de la Recherche Scientifique.
3  * written by Nathanael Schaeffer (CNRS, ISTerre, Grenoble, France).
4  *
5  * nathanael.schaeffer@ujf-grenoble.fr
6  *
7  * This software is governed by the CeCILL license under French law and
8  * abiding by the rules of distribution of free software. You can use,
9  * modify and/or redistribute the software under the terms of the CeCILL
10  * license as circulated by CEA, CNRS and INRIA at the following URL
11  * "http://www.cecill.info".
12  *
13  * The fact that you are presently reading this means that you have had
14  * knowledge of the CeCILL license and that you accept its terms.
15  *
16  */
17 
22 #ifdef _COMPLEX_H
23  typedef complex double cplx;
25 #else
26  #ifdef __cplusplus
27  #include <complex>
28  typedef std::complex<double> cplx;
29  #else
30  #include <complex.h>
31  typedef complex double cplx;
32  #endif
33 #endif
34 
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 
41 typedef struct shtns_info* shtns_cfg;
42 
45 enum shtns_norm {
49 };
50 #define SHT_NO_CS_PHASE (256*4)
51 #define SHT_REAL_NORM (256*8)
52 
53 enum shtns_type {
62 };
63 #define SHT_NATIVE_LAYOUT 0
64 #define SHT_THETA_CONTIGUOUS 256
65 #define SHT_PHI_CONTIGUOUS (256*2)
66 #define SHT_SOUTH_POLE_FIRST (256*32)
67 
68 #define SHT_SCALAR_ONLY (256*16)
69 #define SHT_LOAD_SAVE_CFG (256*64)
70 
71 
72 #ifndef SHTNS_PRIVATE
73 struct shtns_info { // allow read-only access to some data (useful for optimization and helper macros)
74  const unsigned int nlm;
75  const unsigned short lmax;
76  const unsigned short mmax;
77  const unsigned short mres;
78  const unsigned short nphi;
79  const unsigned short nlat;
80  const unsigned short nlat_2;
81  const int *const lmidx;
82  const unsigned short *const li;
83  const unsigned short *const mi;
84  const double *const ct;
85  const double *const st;
86  const unsigned int nspat;
87 };
88 #endif
89 
90 // MACROS //
91 //@{
96 #define LiM(shtns, l,im) ( shtns->lmidx[im] + (l) )
97 //#define LiM(shtns, l,im) ( ((im)*(2*shtns->lmax + 2 - ((im)+1)*shtns->mres))>>1 + (l) )
99 #define LM(shtns, l,m) ( shtns->lmidx[((unsigned)(m))/shtns->mres] + (l) )
100 //#define LM(shtns, l,m) ( ((((unsigned)(m))/shtns->mres)*(2*shtns->lmax + 2 - ((m)+shtns->mres)))>>1 + (l) )
103 #define LM_LOOP( shtns, action ) { int lm=0; do { action } while(++lm < shtns->nlm); }
104 #define LM_L_LOOP( shtns, action ) { int lm=0; do { int l=shtns->li[lm]; action } while(++lm < shtns->nlm); }
107 
108 
109 
112 #define NSPAT_ALLOC(shtns) (shtns->nspat)
113 
114 // HELPER MACROS //
115 
117 #define PHI_DEG(shtns, ip) (360./(shtns->nphi*shtns->mres))*(ip)
118 #define PHI_RAD(shtns, ip) (M_PI/(shtns->nphi*shtns->mres))*(2*ip)
120 
121 
122 // FUNCTIONS //
123 
125 long nlm_calc(long lmax, long mmax, long mres);
126 
127 void shtns_verbose(int);
128 void shtns_print_version(void);
129 
130 #ifndef SWIG
131 
132 void shtns_print_cfg(shtns_cfg);
133 
135 
136 shtns_cfg shtns_init(enum shtns_type flags, int lmax, int mmax, int mres, int nlat, int nphi);
139 shtns_cfg shtns_create(int lmax, int mmax, int mres, enum shtns_norm norm);
141 int shtns_set_grid(shtns_cfg, enum shtns_type flags, double eps, int nlat, int nphi);
143 int shtns_set_grid_auto(shtns_cfg, enum shtns_type flags, double eps, int nl_order, int *nlat, int *nphi);
145 shtns_cfg shtns_create_with_grid(shtns_cfg, int mmax, int nofft);
147 int shtns_use_threads(int num_threads);
148 
149 void shtns_reset(void);
150 void shtns_destroy(shtns_cfg);
151 void shtns_unset_grid(shtns_cfg);
152 
154 
156 
157 double sh00_1(shtns_cfg);
158 double sh10_ct(shtns_cfg);
159 double sh11_st(shtns_cfg);
160 double shlm_e1(shtns_cfg, int l, int m);
161 int shtns_gauss_wts(shtns_cfg, double *wts);
163 
165 
167 
168 void SH_Zrotate(shtns_cfg, cplx *Qlm, double alpha, cplx *Rlm);
173 void SH_Yrotate(shtns_cfg, cplx *Qlm, double alpha, cplx *Rlm);
175 void SH_Yrotate90(shtns_cfg, cplx *Qlm, cplx *Rlm);
177 void SH_Xrotate90(shtns_cfg, cplx *Qlm, cplx *Rlm);
179 
181 
182 void mul_ct_matrix(shtns_cfg, double* mx);
187 void st_dt_matrix(shtns_cfg, double* mx);
189 void SH_mul_mx(shtns_cfg, double* mx, cplx *Qlm, cplx *Rlm);
191 
199 
201 
202 void spat_to_SH(shtns_cfg shtns, double *Vr, cplx *Qlm);
211 void SH_to_spat(shtns_cfg shtns, cplx *Qlm, double *Vr);
216 void SH_to_spat_cplx(shtns_cfg shtns, cplx *alm, cplx *z);
221 void spat_cplx_to_SH(shtns_cfg shtns, cplx *z, cplx *alm);
223 
225 
226 void spat_to_SHsphtor(shtns_cfg, double *Vt, double *Vp, cplx *Slm, cplx *Tlm);
229 void SHsphtor_to_spat(shtns_cfg, cplx *Slm, cplx *Tlm, double *Vt, double *Vp);
231 void SHsph_to_spat(shtns_cfg, cplx *Slm, double *Vt, double *Vp);
233 void SHtor_to_spat(shtns_cfg, cplx *Tlm, double *Vt, double *Vp);
235 #define SH_to_grad_spat(shtns, S,Gt,Gp) SHsph_to_spat(shtns, S, Gt, Gp)
237 
239 
240 void spat_to_SHqst(shtns_cfg, double *Vr, double *Vt, double *Vp, cplx *Qlm, cplx *Slm, cplx *Tlm);
245 void SHqst_to_spat(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double *Vr, double *Vt, double *Vp);
247 
250 
251 void spat_to_SH_l(shtns_cfg, double *Vr, cplx *Qlm, int ltr);
252 void SH_to_spat_l(shtns_cfg, cplx *Qlm, double *Vr, int ltr);
253 
254 void SHsphtor_to_spat_l(shtns_cfg, cplx *Slm, cplx *Tlm, double *Vt, double *Vp, int ltr);
255 void SHsph_to_spat_l(shtns_cfg, cplx *Slm, double *Vt, double *Vp, int ltr);
256 void SHtor_to_spat_l(shtns_cfg, cplx *Tlm, double *Vt, double *Vp, int ltr);
257 void spat_to_SHsphtor_l(shtns_cfg, double *Vt, double *Vp, cplx *Slm, cplx *Tlm, int ltr);
258 
259 void spat_to_SHqst_l(shtns_cfg, double *Vr, double *Vt, double *Vp, cplx *Qlm, cplx *Slm, cplx *Tlm, int ltr);
260 void SHqst_to_spat_l(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double *Vr, double *Vt, double *Vp, int ltr);
262 #define SH_to_grad_spat_l(shtns, S,Gt,Gp,ltr) SHsph_to_spat_l(shtns, S, Gt, Gp, ltr)
264 
266 
267 void spat_to_SH_ml(shtns_cfg, int im, cplx *Vr, cplx *Ql, int ltr);
268 void SH_to_spat_ml(shtns_cfg, int im, cplx *Ql, cplx *Vr, int ltr);
269 
270 void spat_to_SHsphtor_ml(shtns_cfg, int im, cplx *Vt, cplx *Vp, cplx *Sl, cplx *Tl, int ltr);
271 void SHsphtor_to_spat_ml(shtns_cfg, int im, cplx *Sl, cplx *Tl, cplx *Vt, cplx *Vp, int ltr);
272 void SHsph_to_spat_ml(shtns_cfg, int im, cplx *Sl, cplx *Vt, cplx *Vp, int ltr);
273 void SHtor_to_spat_ml(shtns_cfg, int im, cplx *Tl, cplx *Vt, cplx *Vp, int ltr);
274 
275 void spat_to_SHqst_ml(shtns_cfg, int im, cplx *Vr, cplx *Vt, cplx *Vp, cplx *Ql, cplx *Sl, cplx *Tl, int ltr);
276 void SHqst_to_spat_ml(shtns_cfg, int im, cplx *Ql, cplx *Sl, cplx *Tl, cplx *Vr, cplx *Vt, cplx *Vp, int ltr);
278 #define SH_to_grad_spat_ml(shtns, im, S,Gt,Gp,ltr) SHsph_to_spat_ml(shtns, im, S, Gt, Gp, ltr)
280 
282 
285 
286 double SH_to_point(shtns_cfg, cplx *Qlm, double cost, double phi);
287 void SH_to_grad_point(shtns_cfg, cplx *DrSlm, cplx *Slm,
288  double cost, double phi, double *vr, double *vt, double *vp);
289 void SHqst_to_point(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm,
290  double cost, double phi, double *vr, double *vt, double *vp);
291 
292 void SH_to_lat(shtns_cfg shtns, cplx *Qlm, double cost,
293  double *vr, int nphi, int ltr, int mtr);
294 void SHqst_to_lat(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double cost,
295  double *vr, double *vt, double *vp, int nphi, int ltr, int mtr);
297 
298 #endif
299 
300 #ifdef __cplusplus
301 }
302 #endif /* __cplusplus */
void spat_cplx_to_SH(shtns_cfg shtns, cplx *z, cplx *alm)
complex scalar analysis.
Definition: sht_func.c:641
struct shtns_info * shtns_cfg
pointer to data structure describing an SHT, returned by shtns_init() or shtns_create().
Definition: shtns.h:41
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:2354
void SH_Yrotate(shtns_cfg, cplx *Qlm, double alpha, cplx *Rlm)
Rotate SH representation around Y axis by alpha (in radians).
Definition: sht_func.c:227
use fastest algorithm, on a regular grid, mixing dct and regular quadrature.
Definition: shtns.h:57
void SH_to_lat(shtns_cfg shtns, cplx *Qlm, double cost, double *vr, int nphi, int ltr, int mtr)
synthesis at a given latitude, on nphi equispaced longitude points.
Definition: sht_func.c:579
long nlm_calc(long lmax, long mmax, long mres)
compute number of spherical harmonics modes (l,m) for given size parameters. Does not require any pre...
Definition: sht_init.c:84
const unsigned short mmax
maximum order (mmax*mres) of spherical harmonics.
Definition: shtns.h:76
void shtns_print_version(void)
print version information to stdout.
Definition: sht_init.c:1742
void SHqst_to_point(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double cost, double phi, double *vr, double *vt, double *vp)
Evaluate vector SH representation Qlm at physical point defined by cost = cos(theta) and phi...
Definition: sht_func.c:431
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:2383
shtns_norm
different Spherical Harmonic normalizations.
Definition: shtns.h:45
void SH_to_spat_cplx(shtns_cfg shtns, cplx *alm, cplx *z)
complex scalar synthesis.
Definition: sht_func.c:693
double shlm_e1(shtns_cfg, int l, int m)
return the l,m SH coefficient corresponding to unit energy.
Definition: sht_init.c:75
int shtns_set_grid_auto(shtns_cfg, enum shtns_type flags, double eps, int nl_order, int *nlat, int *nphi)
Precompute everything and choose the optimal nlat and nphi for a given non-linear order...
Definition: sht_init.c:2111
int shtns_gauss_wts(shtns_cfg, double *wts)
fill the given array with Gauss weights. returns the number of weights written (0 if not a Gauss grid...
Definition: sht_init.c:2399
void spat_to_SHqst(shtns_cfg, double *Vr, double *Vt, double *Vp, cplx *Qlm, cplx *Slm, cplx *Tlm)
3D vector transform from spherical coordinates to radial-spheroidal-toroidal spectral components (see...
double sh00_1(shtns_cfg)
return the spherical harmonic representation of 1 (l=0,m=0)
Definition: sht_init.c:63
use gaussian grid and quadrature.
Definition: shtns.h:55
void SHsphtor_to_spat(shtns_cfg, cplx *Slm, cplx *Tlm, double *Vt, double *Vp)
transform spheroidal-toroidal spherical harmonic coefficients (Slm,Tlm) to the spatial theta and phi ...
const unsigned int nspat
number of real numbers that must be allocated in a spatial field.
Definition: shtns.h:86
Definition: shtns.h:73
Geodesy and spectral analysis : 4.pi normalization.
Definition: shtns.h:47
const unsigned short nlat_2
...and half of it (using (shtns.nlat+1)/2 allows odd shtns.nlat.)
Definition: shtns.h:80
void SHqst_to_lat(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double cost, double *vr, double *vt, double *vp, int nphi, int ltr, int mtr)
synthesis at a given latitude, on nphi equispaced longitude points.
Definition: sht_func.c:504
Schmidt semi-normalized : 4.pi/(2l+1)
Definition: shtns.h:48
void shtns_unset_grid(shtns_cfg)
unset the grid.
Definition: sht_init.c:2057
void spat_to_SH(shtns_cfg shtns, double *Vr, cplx *Qlm)
transform the scalar field Vr into its spherical harmonic representation Qlm.
shtns_type
different SHT types and algorithms
Definition: shtns.h:54
const unsigned short *const mi
order m for given mode index (size nlm) : mi[lm]
Definition: shtns.h:83
void SH_Yrotate90(shtns_cfg, cplx *Qlm, cplx *Rlm)
Rotate SH representation around Y axis by 90 degrees.
Definition: sht_func.c:209
double SH_to_point(shtns_cfg, cplx *Qlm, double cost, double phi)
Evaluate scalar SH representation Qlm at physical point defined by cost = cos(theta) and phi...
Definition: sht_func.c:344
double sh10_ct(shtns_cfg)
return the spherical harmonic representation of cos(theta) (l=1,m=0)
Definition: sht_init.c:67
use a regular grid if dct is faster, otherwise defaults to gauss.
Definition: shtns.h:56
void shtns_verbose(int)
controls output during initialization: 0=no output (default), 1=some output, 2=degug (if compiled in)...
Definition: sht_init.c:48
void SH_to_spat(shtns_cfg shtns, cplx *Qlm, double *Vr)
transform the spherical harmonic coefficients Qlm into its spatial representation Vr...
const unsigned short nphi
number of spatial points in Phi direction (longitude)
Definition: shtns.h:78
void SHtor_to_spat(shtns_cfg, cplx *Tlm, double *Vt, double *Vp)
transform toroidal spherical harmonic coefficients Tlm to the spatial theta and phi components (Vt...
shtns_cfg shtns_init(enum shtns_type flags, int lmax, int mmax, int mres, int nlat, int nphi)
Simple initialization of the spherical harmonic transforms of given size. Calls shtns_create and shtn...
Definition: sht_init.c:2370
void SH_mul_mx(shtns_cfg, double *mx, cplx *Qlm, cplx *Rlm)
Apply a matrix involving l+1 and l-1 to an SH representation Qlm. Result stored in Rlm (must be diffe...
Definition: sht_func.c:312
const int *const lmidx
(virtual) index in SH array of given im (size mmax+1) : LiM(l,im) = lmidx[im] + l ...
Definition: shtns.h:81
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:1904
use a synthesis only algo including poles, not suitable for computations. Useful for vizualisation...
Definition: shtns.h:60
const double *const st
sin(theta) array (size nlat)
Definition: shtns.h:85
gauss grid, with minimum initialization time (useful for pre/post-processing)
Definition: shtns.h:59
const unsigned short lmax
maximum degree (lmax) of spherical harmonics.
Definition: shtns.h:75
void st_dt_matrix(shtns_cfg, double *mx)
compute the matrix (stored in mx, a double array of size 2*NLM) required to apply sin(theta)*d/dtheta...
Definition: sht_func.c:299
use pure dct algorithm, on a regular grid.
Definition: shtns.h:58
void SHsph_to_spat(shtns_cfg, cplx *Slm, double *Vt, double *Vp)
transform spheroidal spherical harmonic coefficients Slm to the spatial theta and phi components (Vt...
void SH_Zrotate(shtns_cfg, cplx *Qlm, double alpha, cplx *Rlm)
Rotate a SH representation Qlm around the z-axis by angle alpha (in radians), which is the same as ro...
Definition: sht_func.c:36
void shtns_print_cfg(shtns_cfg)
print information about given config to stdout.
Definition: sht_init.c:1762
void shtns_destroy(shtns_cfg)
free memory of given config, which cannot be used afterwards.
Definition: sht_init.c:2068
const unsigned short nlat
number of spatial points in Theta direction (latitude) ...
Definition: shtns.h:79
orthonormalized spherical harmonics (default).
Definition: shtns.h:46
void mul_ct_matrix(shtns_cfg, double *mx)
compute the matrix (stored in mx, a double array of size 2*NLM) required to multiply an SH representa...
Definition: sht_func.c:250
legendre polynomials are recomputed on-the-fly for each transform (may be faster on some machines...
Definition: shtns.h:61
double sh11_st(shtns_cfg)
return the spherical harmonic representation of sin(theta)*cos(phi) (l=1,m=1)
Definition: sht_init.c:71
shtns_cfg shtns_create_with_grid(shtns_cfg, int mmax, int nofft)
Copy a given config but allow a different (smaller) mmax and the possibility to enable/disable fft...
Definition: sht_init.c:2016
const double *const ct
cos(theta) array (size nlat)
Definition: shtns.h:84
const unsigned short *const li
degree l for given mode index (size nlm) : li[lm]
Definition: shtns.h:82
void spat_to_SHsphtor(shtns_cfg, double *Vt, double *Vp, cplx *Slm, cplx *Tlm)
transform the theta and phi components (Vt,Vp) of a vector into its spheroidal-toroidal spherical har...
void shtns_reset(void)
destroy all configs, free memory, and go back to initial state.
Definition: sht_init.c:2094
void SHqst_to_spat(shtns_cfg, cplx *Qlm, cplx *Slm, cplx *Tlm, double *Vr, double *Vt, double *Vp)
3D vector transform from spherical coordinates to radial-spheroidal-toroidal spectral components (see...
const unsigned short mres
the periodicity along the phi axis.
Definition: shtns.h:77
void SH_Xrotate90(shtns_cfg, cplx *Qlm, cplx *Rlm)
Rotate SH representation around X axis by 90 degrees.
Definition: sht_func.c:190
const unsigned int nlm
total number of (l,m) spherical harmonics components.
Definition: shtns.h:74