MartinPdeS / PyMieSim

Python package for Mie scattering computation and analysis.
MIT License
18 stars 5 forks source link

Unable to initiate PlaneWave object #31

Open thiamanti opened 1 day ago

thiamanti commented 1 day ago

I try to create a PlaneWave using the following script:

wavelength=488e-9
amplitude=1e10
source = PlaneWave(
    wavelength=[wavelength],
    polarization=90.0 ,
    amplitude=[amplitude]
)

And I get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[103], line 1
----> 1 source = PlaneWave(
      2     wavelength=[wavelength],
      3     polarization=90.0 ,
      4     amplitude=[amplitude]
      5 )

    [... skipping hidden 1 frame]

File ~\tutorial_env\Lib\site-packages\PyMieSim\experiment\source.py:37, in BaseSource.__post_init__(self)
     29 self.mapping = {
     30     'wavelength': None,
     31     'polarization': None,
     32     'NA': None,
     33     'optical_power': None
     34 }
     36 self._format_inputs()
---> 37 self._generate_binding()

File ~\tutorial_env\Lib\site-packages\PyMieSim\experiment\source.py:144, in PlaneWave._generate_binding(self)
    131 """
    132 Prepares the keyword arguments for the C++ binding based on the scatterer's properties. This
    133 involves evaluating material indices and organizing them into a dictionary for the C++ interface.
   (...)
    136     None
    137 """
    138 self.binding_kwargs = dict(
    139     wavelength=self.wavelength,
    140     jones_vector=self.polarization.jones_vector,
    141     amplitude=self.amplitude
    142 )
--> 144 self.binding = CppSourceSet(**self.binding_kwargs)

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. PyMieSim.binary.SetsInterface.CppSourceSet(wavelength: list[float], jones_vector: list[list[complex]], NA: list[float], optical_power: list[float])

Invoked with: kwargs: wavelength=array([5.e-07]), jones_vector=array([[6.123234e-17+0.j, 1.000000e+00+0.j]]), amplitude=[1.0]

It seems to me that the CppSourceSet constructor expects arguments that are not defined in the case of the PlaneWave class.

Because in PyMieSim/experiment/source.py when creating a PlaneWave odject, the following code is running:

def _generate_binding(self) -> None:
        """
        Prepares the keyword arguments for the C++ binding based on the scatterer's properties. This
        involves evaluating material indices and organizing them into a dictionary for the C++ interface.

        Returns:
            None
        """
        self.binding_kwargs = dict(
            wavelength=self.wavelength,
            jones_vector=self.polarization.jones_vector,
            amplitude=self.amplitude
        )

        self.binding = CppSourceSet(**self.binding_kwargs)

so it seems like CppSourceSet is not accepting the binding_kwargs that are prepared by the function.

MartinPdeS commented 1 day ago

Hi @thiamanti, thanks for pointing that error to me. I have reproduced the bug on my side and will be working on a patch. I think it is manageable for me to fix this within two weeks. In the meantime, you can use a Gaussian source instead of PlaneWave as follows:

from PyMieSim.experiment.source import Gaussian

source = Gaussian(
    wavelength=1200e-9,
    polarization=90,
    optical_power=1e-3,
    NA=0.2
)