-
Notifications
You must be signed in to change notification settings - Fork 47
Fix meep #587
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
Conversation
Reviewer's GuideThis PR refactors Meep geometry extraction to support layer exclusion and robust layer handling, optimizes simulation setup by flattening on a component copy, updates the example notebook with margin parameters and kernel metadata, and modernizes the adjoint optimizer’s algorithm parameter to use string identifiers. Sequence Diagram: Updated Meep Geometry Extraction with Layer ExclusionsequenceDiagram
participant Caller
participant GMFC as "get_meep_geometry_from_component()"
participant Component
participant LayerStack
Caller->>+GMFC: Call(component, layer_stack, exclude_layers, ...)
GMFC->>Component: component.get_polygons_points(merge=True)
Component-->>GMFC: polygons_per_layer
GMFC->>LayerStack: Access layer_stack.layers.values()
LayerStack-->>GMFC: levels (iterator/list)
loop For each 'level' in levels
GMFC->>GMFC: current_layer_tuple = Determine from level.layer / level.derived_layer.layer
GMFC->>GMFC: current_layer_index = get_layer(current_layer_tuple)
alt layer_index in exclude_layers OR layer_index not in polygons_per_layer
GMFC->>GMFC: Skip layer processing (continue loop)
else Process this layer
GMFC->>GMFC: zmin = level.zmin
GMFC->>GMFC: height = level.thickness
GMFC->>GMFC: polygons = polygons_per_layer[current_layer_index]
GMFC->>LayerStack: Get material_name for level.layer
LayerStack-->>GMFC: material_name
loop For each polygon in polygons
GMFC->>GMFC: Create Meep vertices (using zmin)
GMFC->>GMFC: Create mp.GeometricObject
GMFC->>GMFC: Add to results list
end
end
end
GMFC-->>-Caller: List of mp.GeometricObject
Class Diagram: Modified Function Signatures in gplugins.gmeep ModulesclassDiagram
class GetMeepGeometryModule {
<<gplugins.gmeep.get_meep_geometry>>
+get_meep_geometry_from_component(component: any, wavelength: float, is_3d: bool, dispersive: bool, exclude_layers: LayerSpecs | None, **kwargs) list~mp.GeometricObject~
}
class MeepAdjointOptimizationModule {
<<gplugins.gmeep.meep_adjoint_optimization>>
+run_meep_adjoint_optimizer(..., algorithm: str, ...) any
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @joamatab - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 5 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
wavelength: float = 1.55, | ||
is_3d: bool = False, | ||
dispersive: bool = False, | ||
exclude_layers: LayerSpecs | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Clarify exclude_layers
type and usage
Consider renaming the parameter to exclude_layer_indices: Sequence[int]
or converting it to set[int]
for clearer intent and improved performance.
Suggested implementation:
exclude_layer_indices: Sequence[int] | None = None,
) -> list[mp.GeometricObject]:
"""Returns Meep geometry from a gdsfactory component.
Args:
exclude_layer_indices: Sequence of layer indices to exclude from geometry generation.
kwargs: settings.
"""
component = gf.get_component(component=component, **kwargs)
polygons_per_layer = component.get_polygons_points(merge=True)
layer_stack = layer_stack or get_layer_stack()
layer_to_thickness = layer_stack.get_layer_to_thickness()
# Convert exclude_layer_indices to set for fast lookup, if provided
exclude_layer_indices_set = set(exclude_layer_indices) if exclude_layer_indices is not None else set()
- You will need to update all logic in the function that previously checked for excluded layers using
exclude_layers
to now useexclude_layer_indices_set
. For example, if you have code likeif layer in exclude_layers:
, change it toif layer in exclude_layer_indices_set:
. - Make sure to update any function calls or references to the old parameter name in the rest of the file and in any calling code.
- If the function is part of a public API, update any relevant documentation or type hints elsewhere in the codebase.
|
||
geometry = [] | ||
exclude_layers = exclude_layers or [] | ||
layer_to_polygons = component_with_booleans.get_polygons_points() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Remove unused layer_to_polygons
assignment
Since layer_to_polygons
is not used after introducing polygons_per_layer
, removing it will make the code cleaner.
for polygon in polygons: | ||
p = shapely.geometry.Polygon(polygon) | ||
vertices = [mp.Vector3(p[0], p[1], zmin_um) for p in polygon] | ||
material_name = layer_to_material[layer] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Unnecessary shapely Polygon construction
Since p
is not used, consider removing its instantiation to avoid unnecessary computation.
for polygon in polygons: | |
p = shapely.geometry.Polygon(polygon) | |
vertices = [mp.Vector3(p[0], p[1], zmin_um) for p in polygon] | |
material_name = layer_to_material[layer] | |
for polygon in polygons: | |
vertices = [mp.Vector3(p[0], p[1], zmin_um) for p in polygon] | |
material_name = layer_to_material[layer] |
|
||
layer_index = int(get_layer(layer_tuple)) | ||
|
||
if layer_index in exclude_layers: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Optimize exclude_layers
membership checks
Consider converting exclude_layers
to a set before the loop to improve membership test performance for large collections.
Suggested implementation:
layer_index = int(get_layer(layer_tuple))
# Convert exclude_layers to a set for faster membership checks
if not isinstance(exclude_layers, set):
exclude_layers_set = set(exclude_layers) if exclude_layers is not None else set()
else:
exclude_layers_set = exclude_layers
if layer_index in exclude_layers_set:
continue
You should ensure that the initialization of exclude_layers_set
happens outside the loop that contains this code, so the conversion to a set is not repeated on every iteration. If this code is inside a loop, move the conversion to just before the loop starts, and use exclude_layers_set
in the loop body.
update_variable: np.ndarray, | ||
maximize_cost_function: bool = True, | ||
algorithm: int = nlopt.LD_MMA, | ||
algorithm: str = "LD_MMA", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Rename algorithm
parameter to reflect new string usage
Consider renaming the parameter to algorithm_name
to clarify that it now expects a string, not an algorithm object.
Suggested implementation:
algorithm_name: str = "LD_MMA",
algorithm = getattr(nlopt, algorithm_name)
solver = nlopt.opt(algorithm, number_of_params)
Fixes meep plugin
fixes #575
@Arthaj-Octopus
@vvahidd
@threaddy
@joelslaby
@jan-david-fischbach
Summary by Sourcery
Improve the Meep plugin by adding layer exclusion and dynamic algorithm selection, streamlining geometry processing and simulation setup, and updating example notebooks.
New Features:
Enhancements:
Documentation: