A Fortran example program that performs backward and forward Spherical Harmonic Transforms using SHTns. Compile using:
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include <fftw3.h>
void write_vect(char *fn, double *vec, int N);
void write_mx(char *fn, double *mx, int N1, int N2);
int main()
{
long int lmax,mmax,nlat,nphi,mres, NLM;
complex double *Slm, *Tlm;
double *Sh, *Th;
long int i,im,lm;
double t;
lmax = 5; nlat = 32;
mmax = 3; nphi = 10;
mres = 1;
Sh = (
double *) fftw_malloc(
NSPAT_ALLOC(shtns) *
sizeof(double));
Th = (
double *) fftw_malloc(
NSPAT_ALLOC(shtns) *
sizeof(double));
Slm = (complex double *) fftw_malloc( NLM * sizeof(complex double));
Tlm = (complex double *) fftw_malloc( NLM * sizeof(complex double));
LM_LOOP(shtns, Slm[lm]=0.0; Tlm[lm] = 0.0; )
Slm[
LM(shtns, 2,0)] = 1.0;
write_vect("ylm",(double *) Slm,NLM*2);
write_mx("spat",Sh,nphi,nlat);
double t2;
SHqst_to_point(shtns, Tlm, Tlm, Slm, shtns->
ct[nlat/3], 2.*M_PI/(mres*nphi),&t2,&t2,&t);
printf("check if SH_to_point coincides with SH_to_spat : %f = %f\n",t,Sh[nlat/3]);
printf(
"ct*st = %f\n",shtns->
ct[nlat/3]*shtns->
st[nlat/3]);
for (im=0;im<nphi;im++) {
for (i=0;i<nlat;i++) {
Sh[im*nlat+i] *= Sh[im*nlat+i];
}
}
write_vect("ylm_nl",(double *) Tlm, NLM*2);
LM_LOOP(shtns, Slm[lm]=0.0; Tlm[lm] = 0.0; )
write_mx("spatt",Sh,nphi,nlat);
write_mx("spatp",Th,nphi,nlat);
for (im=0;im<nphi;im++) {
for (i=0;i<nlat;i++) {
Sh[im*nlat+i] = shtns->
ct[i];
}
}
write_vect("ylm_v",(double *) Slm,NLM*2);
}
void write_vect(char *fn, double *vec, int N)
{
FILE *fp;
int i;
fp = fopen(fn,"w");
for (i=0;i<N;i++) {
fprintf(fp,"%.6g ",vec[i]);
}
fclose(fp);
}
void write_mx(char *fn, double *mx, int N1, int N2)
{
FILE *fp;
int i,j;
fp = fopen(fn,"w");
for (i=0;i<N1;i++) {
for(j=0;j<N2;j++) {
fprintf(fp,"%.6g ",mx[i*N2+j]);
}
fprintf(fp,"\n");
}
fclose(fp);
}