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.0.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for torch_harmonics-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0ec4337da608cfcea25d3e47d7a44d78e6a4a0d05797b493dd2175311ced2c0c
MD5 7b52c973336703865ec1fe121f0faa06
BLAKE2b-256 674be0a75bb1a78845ab90d514aec7c055188aa7cc9ce25ea3879d60ecaae017

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torch_harmonics-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0033f07df5ff62cc2493260a0a990157eeb367d4e1b6913369e4150b8b933a2b
MD5 5e4519952d278d739152ec498d191812
BLAKE2b-256 9c27ef574cb1477be2a52a378bcc1a3b3ef63e6c8dc47413d257bff1656fc2f3

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