size=mp.Vector3(*plane_size), 
        component=mp.Ez)

#%% INITIALIZE

sim = mp.Simulation(resolution=resolution,
                    cell_size=cell_size,
                    boundary_layers=boundary_layers,
                    sources=sources,
                    symmetries=symmetries)

sim.init_sim()

#%% DEFINE SAVE STEP FUNCTIONS

f, save_line = vs.save_slice_generator(sim, file("Lines.h5"), "Ez", get_line)
g, save_plane = vs.save_slice_generator(sim, file("Planes.h5"), "Ez", get_plane)

to_do_while_running = [mp.at_every(period_line, save_line),
                       mp.at_every(period_plane, save_plane)]

#%% RUN!

temp = time()
if time_is_after_source:
    sim.run(*to_do_while_running, until_after_source=run_time)
else:
    sim.run(*to_do_while_running, until=run_time)
del f, g
enlapsed.append(time() - temp)
                         component=mp.Ez)


#%% INITIALIZE

sim = mp.Simulation(resolution=resolution,
                    cell_size=cell_size,
                    boundary_layers=boundary_layers,
                    sources=sources,
                    symmetries=symmetries)

sim.init_sim()

#%% DEFINE SAVE STEP FUNCTIONS

f, save_xz_plane = vs.save_slice_generator(sim, file("XZPlane.h5"), "Ez",
                                           get_xz_plane)
g, save_xy_plane = vs.save_slice_generator(sim, file("XYPlane.h5"), "Ez",
                                           get_xy_plane)

to_do_while_running = [
    mp.at_every(period_planes, save_xz_plane, save_xy_plane)
]

#%% RUN!

temp = time()
sim.run(*to_do_while_running, until=run_time)
del f, g
enlapsed.append(time() - temp)

#%% SAVE METADATA
Exemple #3
0
def main(series, folder, resolution, from_um_factor, r, paper, wlen):
    #%% PARAMETERS

    # Au sphere
    r = r / (from_um_factor * 1e3)  # Radius of sphere now in Meep units
    medium = import_medium("Au", from_um_factor,
                           paper=paper)  # Medium of sphere: gold (Au)

    # Frequency and wavelength
    wlen = wlen / (from_um_factor * 1e3)  # Wavelength now in Meep units

    # Space configuration
    pml_width = 0.38 * wlen  # 0.5 * wlen
    air_width = r / 2  # 2 * r

    # Field Measurements
    period_line = 1
    period_plane = 1
    after_cell_run_time = 10 * wlen

    # Computation time
    enlapsed = []

    # Saving directories
    if series is None:
        series = f"AuSphereField{2*r*from_um_factor*1e3:.0f}WLen{wlen*from_um_factor*1e3:.0f}"
    if folder is None:
        folder = "AuMieSphere/AuSphereField"
    home = vs.get_home()

    #%% GENERAL GEOMETRY SETUP

    air_width = air_width - air_width % (1 / resolution)

    pml_width = pml_width - pml_width % (1 / resolution)
    pml_layers = [mp.PML(thickness=pml_width)]

    symmetries = [mp.Mirror(mp.Y), mp.Mirror(mp.Z, phase=-1)]
    # Cause of symmetry, two mirror planes reduce cell size to 1/4

    cell_width = 2 * (pml_width + air_width + r)
    cell_width = cell_width - cell_width % (1 / resolution)
    cell_size = mp.Vector3(cell_width, cell_width, cell_width)

    source_center = -0.5 * cell_width + pml_width
    print("Resto Source Center: {}".format(source_center % (1 / resolution)))
    sources = [
        mp.Source(mp.ContinuousSource(wavelength=wlen, is_integrated=True),
                  center=mp.Vector3(source_center),
                  size=mp.Vector3(0, cell_width, cell_width),
                  component=mp.Ez)
    ]
    # Ez-polarized monochromatic planewave
    # (its size parameter fills the entire cell in 2d)
    # >> The planewave source extends into the PML
    # ==> is_integrated=True must be specified

    geometry = [mp.Sphere(material=medium, center=mp.Vector3(), radius=r)]
    # Au sphere with frequency-dependant characteristics imported from Meep.

    path = os.path.join(home, folder, series)
    if not os.path.isdir(path): vs.new_dir(path)
    file = lambda f: os.path.join(path, f)

    #%% SAVE GET FUNCTIONS

    def get_line(sim):
        return sim.get_array(center=mp.Vector3(),
                             size=mp.Vector3(cell_width),
                             component=mp.Ez)

    def get_plane(sim):
        return sim.get_array(center=mp.Vector3(),
                             size=mp.Vector3(0, cell_width, cell_width),
                             component=mp.Ez)

    #%% INITIALIZE

    sim = mp.Simulation(resolution=resolution,
                        cell_size=cell_size,
                        boundary_layers=pml_layers,
                        sources=sources,
                        symmetries=symmetries,
                        geometry=geometry)

    temp = time()
    sim.init_sim()
    enlapsed.append(time() - temp)

    #%% DEFINE SAVE STEP FUNCTIONS

    f, save_line = vs.save_slice_generator(sim, file("Lines.h5"), "Ez",
                                           get_line)
    g, save_plane = vs.save_slice_generator(sim, file("Planes.h5"), "Ez",
                                            get_plane)

    to_do_while_running = [
        mp.at_every(period_line, save_line),
        mp.at_every(period_plane, save_plane)
    ]

    #%% RUN!

    temp = time()
    sim.run(*to_do_while_running, until=cell_width + after_cell_run_time)
    del f, g
    enlapsed.append(time() - temp)

    #%% SAVE METADATA

    params = dict(from_um_factor=from_um_factor,
                  resolution=resolution,
                  r=r,
                  paper=paper,
                  pml_width=pml_width,
                  air_width=air_width,
                  cell_width=cell_width,
                  source_center=source_center,
                  wlen=wlen,
                  period_line=period_line,
                  period_plane=period_plane,
                  after_cell_run_time=after_cell_run_time,
                  series=series,
                  folder=folder,
                  home=home,
                  enlapsed=enlapsed)

    f = h5.File(file("Lines.h5"), "r+")
    for a in params:
        f["Ez"].attrs[a] = params[a]
    f.close()
    del f

    g = h5.File(file("Planes.h5"), "r+")
    for a in params:
        g["Ez"].attrs[a] = params[a]
    g.close()
    del g

    sim.reset_meep()
def slice_generator_params(get_slice, center, size):
    if H_field:
        datasets = ["Ez", "Hy"]
        get_slices = [
            lambda sim: get_slice(sim, center, size, mp.Ez),
            lambda sim: get_slice(sim, center, size, mp.Hy)
        ]
    else:
        datasets = "Ez"
        get_slices = lambda sim: get_slice(sim, center, size, mp.Ez)
    return datasets, get_slices


f, save_line = vs.save_slice_generator(
    sim, file("Lines.h5"),
    *slice_generator_params(get_line, [0, 0, 0], [cell_width, 0, 0]))
gx1, save_plane_x1 = vs.save_slice_generator(
    sim, file("Planes_X1.h5"),
    *slice_generator_params(get_line, [-r, 0, 0], [0, cell_width, cell_width]))
gx2, save_plane_x2 = vs.save_slice_generator(
    sim, file("Planes_X2.h5"),
    *slice_generator_params(get_plane, [r, 0, 0], [0, cell_width, cell_width]))
gy1, save_plane_y1 = vs.save_slice_generator(
    sim, file("Planes_Y1.h5"),
    *slice_generator_params(get_plane, [0, -r, 0],
                            [cell_width, 0, cell_width]))
gy2, save_plane_y2 = vs.save_slice_generator(
    sim, file("Planes_Y2.h5"),
    *slice_generator_params(get_plane, [0, r, 0], [cell_width, 0, cell_width]))
gz1, save_plane_z1 = vs.save_slice_generator(
Exemple #5
0
def main(series, resolution, r, paper, wlen_range, meep_flux, H_field):
    
    #%% PARAMETERS
    
    ### MEAN PARAMETERS
    
    # Units: 10 nm as length unit
    from_um_factor = 10e-3 # Conversion of 1 μm to my length unit (=10nm/1μm)
    
    # Sphere
    r = r / ( from_um_factor * 1e3 ) # Now in Meep units
    
    # Frequency and wavelength
    wlen_range = wlen_range / ( from_um_factor * 1e3 ) # Now in Meep units
    nfreq = 100 # Number of frequencies to discretize range
    cutoff = 3.2
    
    # Computation time
    enlapsed = []
    time_factor_cell = 1.2
    until_after_sources = False
    
    # Saving data
    period_line = 1/10 * min(wlen_range)
    period_plane = 1/50 * min(wlen_range)
    
    # Saving directories
    if series is None:
        series = f"TestParallelRes{resolution}"
    folder = "AuMieSphere/AuMieField"
    home = "/home/vall/Documents/Thesis/ThesisResults/"
    
    ### OTHER PARAMETERS
    
    # Frequency and wavelength
    freq_range = 1/wlen_range # Hz range in Meep units from highest to lowest
    freq_center = np.mean(freq_range)
    freq_width = max(freq_range) - min(freq_range)
    
    # Space configuration
    pml_width = 0.38 * max(wlen_range)
    air_width = r/2 # 0.5 * max(wlen_range)
    
    #%% GENERAL GEOMETRY SETUP
    
    air_width = air_width - air_width%(1/resolution)
    
    pml_width = pml_width - pml_width%(1/resolution)
    pml_layers = [mp.PML(thickness=pml_width)]
    
    # symmetries = [mp.Mirror(mp.Y), 
    #               mp.Mirror(mp.Z, phase=-1)]
    # Two mirror planes reduce cell size to 1/4
    # Issue related that lead me to comment this lines:
    # https://github.com/NanoComp/meep/issues/1484
    
    cell_width = 2 * (pml_width + air_width + r)
    cell_width = cell_width - cell_width%(1/resolution)
    cell_size = mp.Vector3(cell_width, cell_width, cell_width)
    
    source_center = -0.5*cell_width + pml_width
    print("Resto Source Center: {}".format(source_center%(1/resolution)))
    sources = [mp.Source(mp.GaussianSource(freq_center,
                                            fwidth=freq_width,
                                            is_integrated=True,
                                            cutoff=cutoff),
                          center=mp.Vector3(source_center),
                          size=mp.Vector3(0, cell_width, cell_width),
                          component=mp.Ez)]
    # Ez-polarized planewave pulse 
    # (its size parameter fills the entire cell in 2d)
    # >> The planewave source extends into the PML 
    # ==> is_integrated=True must be specified
    
    if time_factor_cell is not False:
        until_after_sources = time_factor_cell * cell_width
    else:
        if until_after_sources is False:
            raise ValueError("Either time_factor_cell or until_after_sources must be specified")
        time_factor_cell = until_after_sources/cell_width
    # Enough time for the pulse to pass through all the cell
    # Originally: Aprox 3 periods of lowest frequency, using T=λ/c=λ in Meep units 
    # Now: Aprox 3 periods of highest frequency, using T=λ/c=λ in Meep units 
    
    def get_line(sim, line_center, line_size, component):
        return sim.get_array(
            center=mp.Vector3(*line_center), 
            size=mp.Vector3(*line_size), 
            component=component) # mp.Ez, mp.Hy
    
    def get_plane(sim, plane_center, plane_size, component):
        return sim.get_array(
            center=mp.Vector3(*plane_center), 
            size=mp.Vector3(*plane_size), 
            component=component)
    
    path = os.path.join(home, folder, f"{series}")
    if (not os.path.isdir(path)) and mp.am_master(): 
        vs.new_dir(path)
    file = lambda f : os.path.join(path, f)
    
    #%% INITIALIZE
    
    sim = mp.Simulation(resolution=resolution,
                        cell_size=cell_size,
                        boundary_layers=pml_layers,
                        sources=sources,
                        k_point=mp.Vector3())
                        # symmetries=symmetries)
    
    if meep_flux:
        # Scattered power --> Computed by surrounding it with closed DFT flux box 
        # (its size and orientation are irrelevant because of Poynting's theorem) 
        box_x1 = sim.add_flux(freq_center, freq_width, nfreq, 
                              mp.FluxRegion(center=mp.Vector3(x=-r),
                                            size=mp.Vector3(0,2*r,2*r)))
        box_x2 = sim.add_flux(freq_center, freq_width, nfreq, 
                              mp.FluxRegion(center=mp.Vector3(x=+r),
                                            size=mp.Vector3(0,2*r,2*r)))
        box_y1 = sim.add_flux(freq_center, freq_width, nfreq,
                              mp.FluxRegion(center=mp.Vector3(y=-r),
                                            size=mp.Vector3(2*r,0,2*r)))
        box_y2 = sim.add_flux(freq_center, freq_width, nfreq,
                              mp.FluxRegion(center=mp.Vector3(y=+r),
                                            size=mp.Vector3(2*r,0,2*r)))
        box_z1 = sim.add_flux(freq_center, freq_width, nfreq,
                              mp.FluxRegion(center=mp.Vector3(z=-r),
                                            size=mp.Vector3(2*r,2*r,0)))
        box_z2 = sim.add_flux(freq_center, freq_width, nfreq,
                              mp.FluxRegion(center=mp.Vector3(z=+r),
                                            size=mp.Vector3(2*r,2*r,0)))
    
    temp = time()
    sim.init_sim()
    enlapsed.append( time() - temp )
    
    #%% DEFINE SAVE STEP FUNCTIONS
    
    def slice_generator_params(get_slice, center, size):
        if H_field: 
            datasets = ["Ez", "Hy"]
            get_slices = [lambda sim: get_slice(sim, center, size, mp.Ez),
                          lambda sim: get_slice(sim, center, size, mp.Hy)]
        else: 
            datasets = "Ez"
            get_slices = lambda sim: get_slice(sim, center, size, mp.Ez)
        return datasets, get_slices
    
    f, save_line = vs.save_slice_generator(sim, file("Lines.h5"), 
        *slice_generator_params(get_line, [0, 0, 0], [cell_width, 0, 0]), 
        parallel=True)
    gx1, save_plane_x1 = vs.save_slice_generator(sim, file("Planes_X1.h5"), 
        *slice_generator_params(get_line, [-r, 0, 0], [0, cell_width, cell_width]), 
        parallel=True)
    gx2, save_plane_x2 = vs.save_slice_generator(sim, file("Planes_X2.h5"),
        *slice_generator_params(get_plane, [r, 0, 0], [0, cell_width, cell_width]),
        parallel=True)
    gy1, save_plane_y1 = vs.save_slice_generator(sim, file("Planes_Y1.h5"), 
        *slice_generator_params(get_plane, [0, -r, 0], [cell_width, 0, cell_width]),
        parallel=True)
    gy2, save_plane_y2 = vs.save_slice_generator(sim, file("Planes_Y2.h5"),
        *slice_generator_params(get_plane, [0, r, 0], [cell_width, 0, cell_width]),
        parallel=True)
    gz1, save_plane_z1 = vs.save_slice_generator(sim, file("Planes_Z1.h5"), 
        *slice_generator_params(get_plane, [0, 0, -r], [cell_width, cell_width, 0]),
        parallel=True)
    gz2, save_plane_z2 = vs.save_slice_generator(sim, file("Planes_Z2.h5"),
        *slice_generator_params(get_plane, [0, 0, r], [cell_width, cell_width, 0]),
        parallel=True)
    
    to_do_while_running = [mp.at_every(period_line, save_line),
                           mp.at_every(period_plane, save_plane_x1),
                           mp.at_every(period_plane, save_plane_x2),
                           mp.at_every(period_plane, save_plane_y1),
                           mp.at_every(period_plane, save_plane_y2),
                           mp.at_every(period_plane, save_plane_z1),
                           mp.at_every(period_plane, save_plane_z2)]
       
    #%% RUN!
    
    temp = time()
    sim.run(*to_do_while_running, until_after_sources=until_after_sources)
    enlapsed.append( time() - temp )
    
    # if meep_flux:
    #     freqs = np.asarray(mp.get_flux_freqs(box_x1))
    #     box_x1_flux0 = np.asarray(mp.get_fluxes(box_x1))
    #     box_x2_flux0 = np.asarray(mp.get_fluxes(box_x2))
    #     box_y1_flux0 = np.asarray(mp.get_fluxes(box_y1))
    #     box_y2_flux0 = np.asarray(mp.get_fluxes(box_y2))
    #     box_z1_flux0 = np.asarray(mp.get_fluxes(box_z1))
    #     box_z2_flux0 = np.asarray(mp.get_fluxes(box_z2))
    
    # #%% SAVE METADATA
    
    # params = dict(
    #     from_um_factor=from_um_factor,
    #     resolution=resolution,
    #     r=r,
    #     paper=paper,
    #     wlen_range=wlen_range,
    #     nfreq=nfreq,
    #     cutoff=cutoff,
    #     pml_width=pml_width,
    #     air_width=air_width,
    #     source_center=source_center,
    #     enlapsed=enlapsed,
    #     period_line=period_line,
    #     period_plane=period_plane,
    #     series=series,
    #     folder=folder,
    #     home=home,
    #     until_after_sources=until_after_sources,
    #     time_factor_cell=time_factor_cell,
    #     )
    
    # planes_series = ["X1", "X2", "Y1", "Y2", "Z1", "Z2"]
    # if H_field:
    #     fields_series = ["Ez", "Hy"]
    # else:
    #     fields_series = "Ez"
    
    # f = h5.File(file("Lines.h5"), "r+")
    # for fs in fields_series:
    #     for a in params: f[fs].attrs[a] = params[a]
    f.close()
    # del f
    
    gx1.close()
    gx2.close()
    gy1.close()
    gy2.close()
    gz1.close()
    gz2.close()