Exemple #1
0
    def init_sim(self, **kwargs):
        """
        Initializes the simulation. This has to be done after adding all structures in order to correctly determine the
        size of the simulation.

        :param kwargs: Parameters which are directly passed to Meep
        """
        z_min = np.min([
            structure['z_min'] for structure in self.structures
            if structure['structure']
        ])
        z_max = np.max([
            structure['z_max'] for structure in self.structures
            if structure['structure']
        ])

        bounds = geometric_union(
            (geometric_union(x['structure']) for x in self.structures)).bounds
        size = np.array(
            (bounds[2] - bounds[0], bounds[3] - bounds[1], (z_max - z_min)))
        self.center = np.round([(bounds[2] + bounds[0]) / 2,
                                (bounds[3] + bounds[1]) / 2,
                                (z_max + z_min) / 2])
        self.size = np.ceil(size + self.padding * 2 + self.pml_thickness * 2)
        if self.reduce_to_2d:
            self.center[2] = self.size[2] = 0

        structures = []
        for structure in self.structures:
            polygon = geometric_union(structure['structure'] + structure['extra_structures']) \
                .buffer(np.finfo(np.float32).eps, resolution=0).simplify(np.finfo(np.float32).eps)
            objs = shapely_collection_to_basic_objs(polygon)

            for obj in objs:
                if obj.is_empty:
                    continue
                for polygon in fracture_intelligently(obj, np.inf, np.inf):
                    structures += [
                        mp.Prism(vertices=[
                            mp.Vector3(
                                *point,
                                0 if self.reduce_to_2d else structure['z_min'])
                            for point in polygon.exterior.coords[:-1]
                        ],
                                 material=structure['material'],
                                 height=structure['z_max'] -
                                 structure['z_min'])
                    ]

        self.sim = mp.Simulation(mp.Vector3(*self.size),
                                 self.resolution,
                                 geometry=structures,
                                 geometry_center=mp.Vector3(*self.center),
                                 sources=self.sources,
                                 boundary_layers=[mp.PML(self.pml_thickness)],
                                 **kwargs)
        self.sim.init_sim()
Exemple #2
0
 def get_fractured_layer_dict(self, max_points=4000, max_line_points=4000):
     from gdshelpers.geometry.shapely_adapter import shapely_collection_to_basic_objs, fracture_intelligently
     fractured_layer_dict = {}
     for layer, geometries in self.layer_dict.items():
         fractured_geometries = []
         for geometry in geometries:
             geometry = geometry.get_shapely_object() if hasattr(geometry, 'get_shapely_object') else geometry
             if type(geometry) in [list, tuple]:
                 geometry = geometric_union(geometry)
             geometry = shapely_collection_to_basic_objs(geometry)
             geometry = itertools.chain(
                 *[fracture_intelligently(geo, max_points, max_line_points) for geo in geometry if not geo.is_empty])
             fractured_geometries.append(geometry)
         fractured_layer_dict[layer] = itertools.chain(*fractured_geometries)
     return fractured_layer_dict