Example #1
0
    def test_with_prefix(self):
        sim = self.init_simple_simulation()
        sim.use_output_directory(self.temp_dir)
        sim.run(mp.with_prefix('test_prefix-', mp.at_end(mp.output_efield_z)), until=200)

        fname = os.path.join(self.temp_dir, 'test_prefix-simulation-ez-000200.00.h5')
        self.assertTrue(os.path.exists(fname))
Example #2
0
 def get_step_funcs(self):
     sf = {self._get_meep_output_comp(f) for f in self.static_fields}
     sf = (mp.at_beginning(f) for f in sf)
     df = {self._get_meep_output_comp(f) for f in self.dynamic_fields}
     df = (mp.at_every(self.dt, f) for f in df)
     volume = mp.in_volume(mp.Volume(center=self.size / 2, size=self.size))
     fields = mp.with_prefix(f'{results_dir}/', *sf, *df)
     return volume, fields
Example #3
0
    def test_with_prefix(self):
        sim = self.init_simple_simulation()
        sim.run(mp.with_prefix('test_prefix-', mp.at_end(mp.output_efield_z)), until=200)

        fname = 'test_prefix-simulation-ez-000200.00.h5'
        self.assertTrue(os.path.exists(fname))

        mp.all_wait()
        if mp.am_master():
            os.remove(fname)
Example #4
0
    def test_with_prefix(self):
        sim = self.init_simple_simulation()
        sim.run(mp.with_prefix('test_prefix-', mp.at_end(mp.output_efield_z)), until=200)

        fname = 'test_prefix-simulation-ez-000200.00.h5'
        self.assertTrue(os.path.exists(fname))

        mp.all_wait()
        if mp.am_master():
            os.remove(fname)
Example #5
0
def main(args):
    """
    Args:
       * **fields** (boolean): If true, outputs the fields at the relevant waveguide cross-sections (top-down and side-view)
       * **output_directory** (string): Name of the output directory (for storing the fields)
       * **eps_input_file** (string): Name of the hdf5 file that defines the geometry through prisms
       * **input_pol** (string): Either "TE", or "TM", corresponding to the desired input mode.  Defaults to "TE"
       * **res** (int): Resolution of the MEEP simulation
       * **nfreq** (int): The number of wavelength points to record in the transmission/reflection spectra
       * **input_direction** (1 or -1): Direction of propagation for the input eigenmode.  If +1, goes in +x, else if -1, goes in -x.  Defaults to +1.
       * **dpml** (float): Length (in microns) of the perfectly-matched layer (PML) at simulation boundaries.  Defaults to 0.5 um.
       * **wl_center** (float): Center wavelength (in microns)
       * **wl_span** (float): Wavelength span (determines the pulse width)
       * **port_vcenter** (float): Vertical center of the waveguide
       * **port_height** (float): Height of the port cross-section (flux plane)
       * **port_width** (float): Width of the port cross-section (flux plane)
       * **source_offset** (float): Offset (in x-direction) between reflection monitor and source.  Defaults to 0.1 um.
       * **center_x** (float): x-coordinate of the center of the simulation region
       * **center_y** (float): y-coordinate of the center of the simulation region
       * **center_z** (float): z-coordinate of the center of the simulation region
       * **sx** (float): Size of the simulation region in x-direction
       * **sx** (float): Size of the simulation region in y-direction
       * **sz** (float): Size of the simulation region in z-direction
       * **port_coords** (list): List of the port coordinates (variable length), in the format [x1, y1, x2, y2, x3, y3, ...] (*must* be even)
    """
    #Boolean inputs
    fields = args.fields

    #String inputs
    output_directory = args.output_directory
    eps_input_file = args.eps_input_file
    input_pol = args.input_pol

    #Int inputs
    res = args.res
    nfreq = args.nfreq
    input_direction = args.input_direction

    #Float inputs
    dpml = args.dpml
    wl_center = args.wl_center
    wl_span = args.wl_span
    port_vcenter = args.port_vcenter
    port_height = args.port_height
    port_width = args.port_width
    source_offset = args.source_offset
    center_x, center_y, center_z = args.center_x, args.center_y, args.center_z
    sx, sy, sz = args.sx, args.sy, args.sz

    #List of floats
    port_coords = [float(x) for x in args.port_coords[0].split(" ")]
    ports = [(port_coords[2 * i], port_coords[2 * i + 1])
             for i in range(int(len(port_coords) / 2))]

    if input_pol == "TE":
        parity = mp.ODD_Z
    elif input_pol == "TM":
        parity = mp.EVEN_Z
    else:
        raise ValueError(
            "Warning! Improper value of 'input_pol' was passed to mcts.py (input_pol given ="
            + str(input_pol) + ")")

    if len(port_coords) % 2 != 0:
        raise ValueError(
            "Warning! Improper port_coords was passed to `meep_compute_transmission_spectra`.  Must be even number of port_coords in [x1, y1, x2, y2, ..] format."
        )

    # Setup the simulation geometries

    prism_objects = get_prism_objects(eps_input_file)
    geometry = []
    for p in prism_objects:
        #        print('vertices='+str(p['vlist']))
        #        print('axis = '+str(mp.Vector3(0,1,0)))
        #        print('height = '+str(p['height']))
        print('material = ' + str(p['eps']))
        #        print('\n')
        geometry.append(
            mp.Prism(p['vlist'],
                     axis=mp.Vector3(0, 1, 0),
                     height=p['height'],
                     material=mp.Medium(epsilon=p['eps'])))

    # Setup the simulation sources
    fmax = 1.0 / (wl_center - 0.5 * wl_span)
    fmin = 1.0 / (wl_center + 0.5 * wl_span)
    fcen = (fmax + fmin) / 2.0
    df = fmax - fmin
    if abs(abs(input_direction) - 1) > 1E-6:
        print(input_direction)
        raise ValueError("Warning! input_direction is not +1 or -1.")

    # Use first port in 'ports' as the location of the eigenmode source
    sources = [
        mp.EigenModeSource(
            src=mp.GaussianSource(fcen, fwidth=df, cutoff=30),
            component=mp.ALL_COMPONENTS,
            size=mp.Vector3(0, 3 * float(port_height), 3 * float(port_width)),
            center=mp.Vector3(ports[0][0] + source_offset - center_x,
                              float(port_vcenter) - center_y,
                              ports[0][1] - center_z),
            eig_match_freq=True,
            eig_parity=parity,
            eig_kpoint=mp.Vector3(float(input_direction) * wl_center, 0, 0),
            eig_resolution=2 * res if res > 16 else 32,
        )
    ]

    # Setup the simulation
    sim = mp.Simulation(cell_size=mp.Vector3(sx, sy, sz),
                        boundary_layers=[mp.PML(dpml)],
                        geometry=geometry,
                        sources=sources,
                        dimensions=3,
                        resolution=res,
                        filename_prefix=False)
    """ Add power flux monitors """
    print("ADDING FLUX MONITORS")
    flux_plane_objects = []

    for port in ports:
        flux_region = mp.FluxRegion(size=mp.Vector3(0, float(port_height),
                                                    float(port_width)),
                                    center=mp.Vector3(
                                        float(port[0]) - center_x,
                                        float(port_vcenter) - center_y,
                                        float(port[1]) - center_z))
        fpo = sim.add_flux(fcen, df, nfreq, flux_region)
        flux_plane_objects.append(fpo)

    sim.use_output_directory(str(output_directory))
    """ Run the simulation """
    """ Monitor the amplitude in the center of the structure """
    decay_pt = mp.Vector3(0, port_vcenter, 0)

    sv = mp.Volume(size=mp.Vector3(sx, sy, 0), center=mp.Vector3(0, 0, 0))
    tv = mp.Volume(size=mp.Vector3(sx, 0, sz),
                   center=mp.Vector3(0, port_vcenter, 0))

    print("RUNNING SIMULATION")

    if fields:
        sim.run(mp.at_beginning(mp.output_epsilon),
                mp.at_beginning(
                    mp.with_prefix(str("sideview-"),
                                   mp.in_volume(sv, mp.output_epsilon))),
                mp.at_beginning(
                    mp.with_prefix(str("topview-"),
                                   mp.in_volume(tv, mp.output_epsilon))),
                mp.at_every(
                    1.0,
                    mp.to_appended(str("ez-sideview"),
                                   mp.in_volume(sv, mp.output_efield_z))),
                mp.at_every(
                    1.0,
                    mp.to_appended(str("ez-topview"),
                                   mp.in_volume(tv, mp.output_efield_z))),
                until_after_sources=mp.stop_when_fields_decayed(
                    20, mp.Ez, decay_pt, 1e-4))
    else:
        sim.run(until_after_sources=mp.stop_when_fields_decayed(
            20, mp.Ez, decay_pt, 1e-4))

    sim.display_fluxes(*flux_plane_objects)

    print("FINISHED SIMULATION")