Skip to main content

A python electrodynamical CPU/GPU solver written in PyTorch,With full support of PyTorch's automatic differentiation.Based on the Green's Dyadic Method (GDM, volume integral technique in frequency domain).

Project description

torchgdm logo

TorchGDM

nano-optics full-field solver, written in PyTorch.

TorchGDM is a PyTorch implementation of the Green's dyadic method (GDM), a electro-dynamics full-field volume integral technique. It's main features are mixed simulations combining volume discretized and effective e/m dipole-pair polarizabilities, as well as the general support of torch's automatic differentiation. Please visit also the TorchGDM documentation website.

TorchGDM is originally based on various theoretical works by Christian Girard at CEMES (see e.g. Ch. Girard 2005 Rep. Prog. Phys. 68 1883), with contributions from G. Colas des Francs, A. Arbouet, R. Marty, C. Majorel, A. Patoux, Y. Brûlé and P. R. Wiecha.

If you use TorchGDM for your projects, please cite it.

Getting started

Simulate and plot the scattering cross section spectrum between 550nm and 950nm of a silicon nano-cube (200nm side length), placed in vacuum, illuminated by an x-polarized plane wave:

  import torch
  import matplotlib.pyplot as plt
  import torchgdm as tg

  # --- simulation setup
  # - vacuum environment
  env = tg.env.freespace_3d.EnvHomogeneous3D(
      env_material=tg.materials.MatConstant(1.0),
  )

  # - illumination field(s)
  wavelengths = torch.linspace(550.0, 950.0, 25)
  plane_wave = tg.env.freespace_3d.PlaneWave(e0p=1.0, e0s=0.0)

  # - discretized structure
  structure = tg.struct.StructDiscretizedCubic3D(
      discretization_config=tg.struct.volume.cube(l=8),
      step=30,
      materials=tg.materials.MatDatabase("GaN"),
  )

  # - define and run simulation
  sim = tg.Simulation(
      structures=[structure],
      illumination_fields=[plane_wave],
      environment=env,
      wavelengths=wavelengths,
  )
  sim.run()

  # --- post-processing: cross sections
  cs_results = sim.get_crosssections()

  # - plot
  plt.figure(figsize=(5, 4))
  plt.plot(tg.to_np(wavelengths), tg.to_np(cs_results["scs"]))
  plt.xlabel("wavelength (nm)")
  plt.ylabel("scs (nm^2)")
  plt.show()

GPU support

For GPU support, all you need is a GPU enabled version of pytorch. TorchGDM was tested with CUDA. CUDA usage can be enabled simply by using:

  import torchgdm as tg
  tg.use_cuda(True)

Alternatively, GPU usage can be fine controlled by passing the device="cuda" argument when setting up the simulation class:

  sim = tg.Simulation(..., device="cuda")

Features

List of features

General:

  • pure python
  • run on CPU and GPU, parallelized and vectorized
  • full support of torch's automatic differentiation

Simulations:

  • 3D volume discretizations
  • 3D effective polarizabilities (dipolar electric and magnetic)
  • mix of both 3D types of structures (volume / point-polarizabilities)
  • 2D surface discretization (infinitly long geometries)
  • global (plane wave, focused Gaussian) and local illumination (point emitter)

Post-processing

  • cross sections (extinction, scattering, absorption)
  • nearfields and farfields
  • optical chirality
  • Poynting vector
  • field gradients
  • exact multipole decomposition
  • Green's tensors in complex environments
  • LDOS / decay rate
  • extract ED/MD point-polarizability tensors
  • plotting tools for convenient 2D / 3D visualizations
  • ...

Installing / Requirements

Installation should work via pip on all major operating systems. For now please use the gitlab repository as source, we will publish TorchGDM on PyPi once all main features are implemented and tested.

pip install https://gitlab.com/wiechapeter/torchgdm/-/archive/main/torchgdm-main.zip

TorchGDM was tested under linux and windows with python versions 3.9 to 3.12. It requires following python packages

  • pytorch (v2.0+)

Further optional dependencies

  • numpy (for some tools)
  • scipy (for some tools)
  • pyyaml (support for permittivity data from refractiveindex.info)
  • tqdm (for progress bars)
  • psutil (for automatic memory purge)
  • matplotlib (2d plotting)
  • alphashape (2d contour plotting)
  • pyvista (3d visualization, install via: # pip install 'jupyterlab>=3' ipywidgets 'pyvista[all,trame]')

(install all optional dependencies via pip: pip install numpy scipy pyyaml tqdm psutil matplotlib alphashape pyvista)

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

Links

Licensing

The code in this project is licensed under the GNU GPLv3.

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

torchgdm-0.39.tar.gz (161.9 kB view details)

Uploaded Source

File details

Details for the file torchgdm-0.39.tar.gz.

File metadata

  • Download URL: torchgdm-0.39.tar.gz
  • Upload date:
  • Size: 161.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for torchgdm-0.39.tar.gz
Algorithm Hash digest
SHA256 d95e4a87bac62c268c20f10ab44f5543bb0392428224f88378bff9669e644215
MD5 a534ba572128c4f5a4eeff232899fabe
BLAKE2b-256 7011b260f5f44c565e027bcbd84d659d8547962fe0ec16bb8fb85602f6027ccb

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