manoharan-lab / holopy

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

MieLens() and Lens(Mie()) produce slightly different answers #350

Open briandleahy opened 4 years ago

briandleahy commented 4 years ago

Currently (as of #333) the MieLens() and Lens(Mie) theories produce slightly different answers, even when the number of quadrature points is large for both theories. The following test, removed from holopy.scattering.tests.test_lens, illustrates this:

    @unittest.skip("known failure")
    def test_raw_fields_exact_mielens(self):
        detector = SMALL_DETECTOR
        scatterer = test_common.sphere
        medium_wavevec = 2 * np.pi / test_common.wavelen
        medium_index = test_common.index
        illum_polarization = detector.illum_polarization

        theory_mielens = MieLens(
            lens_angle=LENS_ANGLE,
            calculator_accuracy_kwargs={'quad_npts': 400})
        theory_lensmie = Lens(
            lens_angle=LENS_ANGLE,
            theory=Mie(False, False),
            quad_npts_theta=400,
            quad_npts_phi=400)

        pos_mielens = theory_mielens._transform_to_desired_coordinates(
            detector, scatterer.center, wavevec=medium_wavevec)
        pos_lensmie = theory_lensmie._transform_to_desired_coordinates(
            detector, scatterer.center, wavevec=medium_wavevec)

        args = (scatterer, medium_wavevec, medium_index, illum_polarization)
        fields_mielens = theory_mielens._raw_fields(pos_mielens, *args)
        fields_lensmie = theory_lensmie._raw_fields(pos_lensmie, *args)
        for fa, fb in zip(fields_mielens, fields_lensmie):
            self.assertTrue(np.allclose(fa, fb, atol=1e-9, rtol=1e-9))
        # The actual difference is in the 6e-5 range...

The difference between the fields is in the 6e-5 range, which is too small to be a physics mistake, but larger than numerical truncation should be. My hunch is that there is a numerical issue / incorrectness / difference in calculating the Mie scattering a_ls and b_ls between the coefficients called in MieLens and those in Mie(), as they are two distinct pieces of code.

This should be figured out, but at the 6e-5 it is unlikely that this will have a serious effect on fitting holograms.

briandleahy commented 3 years ago

This is probably due to a ~1e-7 difference in the Mie scattering matrices between mielensfunctions.MieScatteringMatrix and Mie._scat_coeffs.