manoharan-lab / holopy

Hologram processing and light scattering in python
GNU General Public License v3.0
131 stars 50 forks source link

DDA Memory Error #416

Closed mfreer closed 1 year ago

mfreer commented 1 year ago

Hello,

I'm starting to build a code with the aim to simulate sets of arbitrarily-shaped particles using HoloPy, so I've been proceeding with the assumption that I will eventually use the DDA functionality in HoloPy. Unfortunately, I'm running to an issue when I try to run a test with a sphere (see example code below):

from holopy.scattering import Sphere, Mie, calc_holo, DDA
from holopy import detector_grid 

detector_zoom = detector_grid(shape=(200, 200), spacing=0.50)          
sphere_zoom = Sphere(center=(50, 50, 500), n=1.59, r=50)               

theory = DDA(use_gpu=True)

holo_zoom = calc_holo(
    detector_zoom, sphere_zoom, medium_index=1.33,
    illum_wavelen=0.532, illum_polarization=(1, 0), theory=theory)

Running that code, I get an ArrayMemoryError from Numpy with the following Traceback:

Traceback (most recent call last):
  File "C:\Users\mfreer\cloudsci\phia\test.py", line 12, in <module>
    holo_zoom = calc_holo(
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\interface.py", line 205, in calc_holo
    scattered_field = imageformer.calculate_scattered_field(scatterer, uschema)
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\imageformation.py", line 38, in calculate_scattered_field
    self._calculate_single_color_scattered_field(scatterer, schema))
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\imageformation.py", line 99, in _calculate_single_color_scattered_field
    field = self._get_field_from(scatterer, schema)
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\imageformation.py", line 123, in _get_field_from
    self.scattering_theory.raw_fields(
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\theory\scatteringtheory.py", line 80, in raw_fields
    scat_matr = self.raw_scat_matrs(
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\theory\dda.py", line 214, in raw_scat_matrs
    self._run_adda(
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\theory\dda.py", line 128, in _run_adda
    scat_args = self._adda_discretized(scatterer, medium_wavelen, medium_index, temp_dir)
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\theory\dda.py", line 154, in _adda_discretized
    vox = scatterer.voxelate_domains(spacing)
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\scatterer\scatterer.py", line 228, in voxelate_domains
    return self.in_domain(self._voxel_coords(spacing))
  File "C:\Users\mfreer\anaconda3\lib\site-packages\holopy\scattering\scatterer\scatterer.py", line 204, in _voxel_coords
    grid = np.mgrid[
  File "C:\Users\mfreer\anaconda3\lib\site-packages\numpy\lib\index_tricks.py", line 173, in __getitem__
    nn = _nx.indices(size, typ)
  File "C:\Users\mfreer\anaconda3\lib\site-packages\numpy\core\numeric.py", line 1770, in indices
    res = empty((N,)+dimensions, dtype=dtype)
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 597. GiB for an array with shape (3, 2989, 2989, 2989) and data type float64

Am I running into this issue due to an unreasonably large scatterer or detector grid, or is something else going on?

Thanks in advance for any guidance you can provide!

vnmanoharan commented 1 year ago

The particle size is too large for DDA. ADDA uses 10 dipoles per wavelength inside the particle (see https://github.com/adda-team/adda/blob/master/doc/manual.pdf). So the number of dipoles for a 50 micrometer particle, 0.532 micrometer wavelength, and index 1.59 would be more than 10^9, which is leading to the memory allocation error. If you need to simulate arbitrarily shaped particles this large, you may have to look into an approximate solution.