Skip to main content

A differentiable spherical harmonic transform for PyTorch.

Project description

torch_harmonics is a differentiable implementation of the Spherical Harmonic transform in PyTorch. It was originally implemented to enable Spherical Fourier Neural Operators (SFNO). It uses quadrature rules to compute the projection onto the associated Legendre polynomials and FFTs for the projection onto the harmonic basis. This algorithm tends to outperform others with better asymptotic scaling for most practical purposes.

torch_harmonics uses PyTorch primitives to implement these operations, making it fully differentiable. Moreover, the quadrature can be distributed onto multiple ranks making it spatially distributed.

torch_harmonics has been used to implement a variety of differentiable PDE solvers which generated the animations below. Moreover, it has enabled the development of Spherical Fourier Neural Operators (SFNOs) [1].

Installation

Download directyly from PyPI:

pip install torch-harmonics

Build in your environment using the Python package:

git clone git@github.com:NVIDIA/torch-harmonics.git
pip install ./torch_harmonics

Alternatively, use the Dockerfile to build your custom container after cloning:

git clone git@github.com:NVIDIA/torch-harmonics.git
cd torch_harmonics
docker build . -t torch_harmonics
docker run --gpus all -it --rm --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 torch_harmonics

Contributors

Implementation

The implementation follows the algorithm as presented in [2].

Spherical harmonic transform

The truncated series expansion of a function $f$ defined on the surface of a sphere can be written as

$$ f(\theta, \lambda) = \sum_{m=-M}^{M} \exp(im\lambda) \sum_{n=|m|}^{M} F_n^m \bar{P}_n^m (\cos \theta), $$

where $\theta$ is the colatitude, $\lambda$ the longitude, $\bar{P}_n^m$ the normalized, associated Legendre polynomials and $F_n^m$, the expansion coefficient associated to the mode $(m,n)$.

A direct spherical harmonic transform can be accomplished by a Fourier transform

$$ F^m(\theta) = \frac{1}{2 \pi} \int_{0}^{2\pi} f(\theta, \lambda) \exp(-im\lambda) \mathrm{d}\lambda $$

in longitude and a Legendre transform

$$ F_n^m = \frac{1}{2} \int_{-1}^1 F^m(\theta) \bar{P}_n^m(\cos \theta) \mathrm{d} \cos \theta $$

in latitude.

Discrete Legendre transform

in order to apply the Legendre transfor, we shall use Gauss-Legendre points in the latitudinal direction. The integral

$$ F_n^m = \int_{0}^\pi F^m(\theta) \bar{P}_n^m(\cos \theta) \sin \theta \mathrm{d} \theta $$

is approximated by the sum

$$ F_n^m = \sum_{j=1}^{N_\theta} F^m(\theta_j) \bar{P}_n^m(\cos \theta_j) w_j $$

Usage

Getting started

The main functionality of torch_harmonics is provided in the form of torch.nn.Modules for composability. A minimum example is given by:

import torch
import torch_harmonics as th

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

nlat = 512
nlon = 2*nlat
batch_size = 32
signal = torch.randn(batch_size, nlat, nlon)

# transform data on an equiangular grid
sht = th.RealSHT(nlat, nlon, grid="equiangular").to(device).float()

coeffs = sht(signal)

torch_harmonics also implements a distributed variant of the SHT located in torch-harmonics.distributed.

Cite us

If you use torch-harmonics in an academic paper, please cite [1]

@misc{bonev2023spherical,
      title={Spherical Fourier Neural Operators: Learning Stable Dynamics on the Sphere}, 
      author={Boris Bonev and Thorsten Kurth and Christian Hundt and Jaideep Pathak and Maximilian Baust and Karthik Kashinath and Anima Anandkumar},
      year={2023},
      eprint={2306.03838},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

References

[1] Bonev B., Kurth T., Hundt C., Pathak, J., Baust M., Kashinath K., Anandkumar A.; Spherical Fourier Neural Operators: Learning Stable Dynamics on the Sphere; arXiv 2306.0383, 2023.

[2] Schaeffer N.; Efficient spherical harmonic transforms aimed at pseudospectral numerical simulations; G3: Geochemistry, Geophysics, Geosystems, 2013.

[3] Wang B., Wang L., Xie Z.; Accurate calculation of spherical and vector spherical harmonic expansions via spectral element grids; Adv Comput Math, 2018.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_harmonics-0.6.1.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

torch_harmonics-0.6.1-py3-none-any.whl (55.6 kB view details)

Uploaded Python 3

File details

Details for the file torch_harmonics-0.6.1.tar.gz.

File metadata

  • Download URL: torch_harmonics-0.6.1.tar.gz
  • Upload date:
  • Size: 36.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for torch_harmonics-0.6.1.tar.gz
Algorithm Hash digest
SHA256 23e8f1e345b47a740ff0304381f57ea5fae3d06b2a180083e817e61073be0918
MD5 ae85ba961acf176819b432a304d87212
BLAKE2b-256 6189a4bb95287c42014e95ecc7dcb1f36d1208e35e3c1671312b9efeffbe2372

See more details on using hashes here.

File details

Details for the file torch_harmonics-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_harmonics-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b3a92d5fa36c3150a5c1b7778d354d99f39ed305cbd6634fb1c18525383e0a6
MD5 b8d44e7cfc723297d841cb8464cfe6a3
BLAKE2b-256 f444fbca829c4242d59fc75782b984b61fd3b8504447495c5571ec0bf646f174

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page