Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FourierFields in cylindrical coordinates error #2195

Closed
ejohlin opened this issue Aug 12, 2022 · 2 comments · Fixed by #2208
Closed

FourierFields in cylindrical coordinates error #2195

ejohlin opened this issue Aug 12, 2022 · 2 comments · Fixed by #2208
Labels

Comments

@ejohlin
Copy link

ejohlin commented Aug 12, 2022

Trying to use the FourierFields adjoint objective when the simulation is in 2D cylindrical coordinates returns an error with respect to the volume argument passed to FourierFields. This was found by modifying the example Adjoint FarField code to use cylindrical coordinates (which runs fine), and then try to change the objective to use FourierFields (which then fails).

Trying to call FourierFields with cylindrical coordinates returns the following error for the Volume (abbreviated):

ValueError                                Traceback (most recent call last)
File ~/miniconda3/envs/mp/lib/python3.10/site-packages/meep/adjoint/objective.py:237, in FourierFields.__init__(self, sim, volume, component, yee_grid, decimation_factor)
    235 def __init__(self, sim, volume, component, yee_grid=False, decimation_factor=0):
    236     super().__init__(sim)
--> 237     self.volume = sim._fit_volume_to_simulation(volume)
    238     self.component = component
    239     self.yee_grid = yee_grid

File ~/miniconda3/envs/mp/lib/python3.10/site-packages/meep/simulation.py:1275, in Simulation._fit_volume_to_simulation(self, vol)
   1274 def _fit_volume_to_simulation(self, vol):
-> 1275     return Volume(vol.center, vol.size, dims=self.dimensions, is_cylindrical=self.is_cylindrical)

File ~/miniconda3/envs/mp/lib/python3.10/site-packages/meep/simulation.py:393, in Volume.__init__(self, center, size, dims, is_cylindrical, vertices)
...
    103     return mp.vec(v3.x, v3.y, v3.z)
    104 else:
--> 105     raise ValueError("Invalid dimensions in Volume: {}".format(dims))

ValueError: Invalid dimensions in Volume: -2

I could not find the reason for the volume dimension to be set to -2.

A MWE for reproducing:

import meep as mp
import meep.adjoint as mpa
pml_layers = [mp.PML(1.0)]
sim = mp.Simulation(
    cell_size=mp.Vector3(3, 0, 3),
    boundary_layers=pml_layers,
    dimensions=mp.CYLINDRICAL,
    m=0,
    default_material=mp.Medium(index=1.0),
    resolution=10
)
Ezvol = mp.Volume(center=mp.Vector3(0, 0, 2.5),
                  size=mp.Vector3(), dims=2, is_cylindrical=True)
Ez = mpa.FourierFields(sim, Ezvol, mp.Ez)

The error is the same regardless of if dims or is_cylindrical are passed to mp.Volume

Interestingly this error occurs in the current *conda version of meep (1.24.0), but the older Ubuntu package manager version (1.17.1) does not produce any error.

@mawc2019
Copy link
Contributor

mawc2019 commented Aug 12, 2022

As the error appears at sim._fit_volume_to_simulation, the MWE can be further reduced as follows, which does not involve any part of the adjoint solver.

import meep as mp
import meep.adjoint as mpa
pml_layers = [mp.PML(1.0)]
sim = mp.Simulation(
    cell_size=mp.Vector3(3, 0, 3),
    boundary_layers=pml_layers,
    dimensions=mp.CYLINDRICAL,
    m=0,
    default_material=mp.Medium(index=1.0),
    resolution=10
)
Ezvol = mp.Volume(center=mp.Vector3(0, 0, 2.5),
                  size=mp.Vector3(), dims=2, is_cylindrical=True)
sim._fit_volume_to_simulation(Ezvol)

Adding the following three lines after this line can fix the bug exposed by the MWE.

        if self.dimensions == mp.CYLINDRICAL:
            self.dimensions = 2
            self.is_cylindrical = True

I could not find the reason for the volume dimension to be set to -2.

When mp.CYLINDRICAL is printed, it gives -2.

Interestingly this error occurs in the current *conda version of meep (1.24.0), but the older Ubuntu package manager version (1.17.1) does not produce any error.

The FourierFields adjoint solver was overhauled before the version 1.23.0. The function _fit_volume_to_simulation was not used in the old version of FourierFields.

@ejohlin
Copy link
Author

ejohlin commented Aug 22, 2022

Adding the following three lines after this line can fix the bug exposed by the MWE.

        if self.dimensions == mp.CYLINDRICAL:
            self.dimensions = 2
            self.is_cylindrical = True

Thank you, I can confirm that this does indeed fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants