Esempio n. 1
0
source = [mp.EigenModeSource(src,
                    eig_band = 1,
                    direction=mp.NO_DIRECTION,
                    eig_kpoint=kpoint,
                    size = source_size,
                    center=source_center)]
#----------------------------------------------------------------------
#- geometric objects
#----------------------------------------------------------------------

Nx = 10
Ny = 10

design_region = mp.Volume(center=mp.Vector3(), size=mp.Vector3(1, 1, 0))
rho_vector = 11*np.random.rand(Nx*Ny) + 1
basis = mpa.BilinearInterpolationBasis(volume=design_region,Nx=Nx,Ny=Ny,rho_vector=rho_vector)

geometry = [
    mp.Block(center=mp.Vector3(x=-Sx/4), material=mp.Medium(index=3.45), size=mp.Vector3(Sx/2, 0.5, 0)), # horizontal waveguide
    mp.Block(center=mp.Vector3(y=Sy/4), material=mp.Medium(index=3.45), size=mp.Vector3(0.5, Sy/2, 0)),  # vertical waveguide
    mp.Block(center=design_region.center, size=design_region.size, epsilon_func=basis.func()) # design region
]

sim = mp.Simulation(cell_size=cell_size,
                    boundary_layers=pml_layers,
                    geometry=geometry,
                    sources=source,
                    eps_averaging=False,
                    resolution=resolution)

#----------------------------------------------------------------------
                     eig_band = 1,
                     direction=mp.NO_DIRECTION,
                     eig_kpoint=kpoint,
                     size = source_size,
                     center=source_center)]

#----------------------------------------------------------------------
#- geometric objects
#----------------------------------------------------------------------
geometry = [mp.Block(center=mp.Vector3(), material=mp.Medium(index=3.45), size=mp.Vector3(mp.inf, 0.5, 0) )]

Nx = 10
Ny = 10
design_size   = mp.Vector3(1, 1, 0)
design_region = mpa.Subregion(name='design', center=mp.Vector3(), size=design_size)
basis = mpa.BilinearInterpolationBasis(region=design_region,Nx=Nx,Ny=Ny)

beta_vector = 11*np.random.rand(Nx*Ny) + 1

design_function = basis.parameterized_function(beta_vector)
design_object = [mp.Block(center=design_region.center, size=design_region.size, epsilon_func = design_function.func())]

geometry += design_object

sim = mp.Simulation(cell_size=cell_size,
                    boundary_layers=pml_layers,
                    geometry=geometry,
                    sources=sources,
                    eps_averaging=False,
                    resolution=resolution)
                       direction=mp.NO_DIRECTION,
                       eig_kpoint=kpoint,
                       size=source_size,
                       center=source_center)
]
#----------------------------------------------------------------------
#- geometric objects
#----------------------------------------------------------------------

Nx = 10
Ny = 10

design_region1 = mp.Volume(center=mp.Vector3(-.5), size=mp.Vector3(0.5, 1, 0))
rho_vector1 = 11 * np.random.rand(Nx * Ny) + 1
basis1 = mpa.BilinearInterpolationBasis(volume=design_region1,
                                        Nx=Nx,
                                        Ny=Ny,
                                        rho_vector=rho_vector1)

design_region2 = mp.Volume(center=mp.Vector3(.5), size=mp.Vector3(0.5, 1, 0))
rho_vector2 = 11 * np.random.rand(Nx * Ny) + 1
basis2 = mpa.BilinearInterpolationBasis(volume=design_region2,
                                        Nx=Nx,
                                        Ny=Ny,
                                        rho_vector=rho_vector2)

bases = [basis1, basis2]
geometry = [
    mp.Block(center=mp.Vector3(x=-Sx / 4),
             material=mp.Medium(index=3.45),
             size=mp.Vector3(Sx / 2, 0.5, 0)),  # horizontal waveguide
    mp.Block(center=mp.Vector3(y=Sy / 4),
Esempio n. 4
0
    def run_test(self, resolution, Nx, Ny):
        Sx = 1
        Sy = 1

        z = lambda x, y: 5 * np.abs((x**3 + y**2 + x**2 * y)) + 1

        cell_size = mp.Vector3(Sx, Sy)

        design_size = mp.Vector3(Sx, Sy, 0)
        design_region = mpa.Subregion(name='design',
                                      center=mp.Vector3(),
                                      size=design_size)
        basis = mpa.BilinearInterpolationBasis(region=design_region,
                                               Nx=Nx,
                                               Ny=Ny)

        rho_x = np.linspace(-Sx / 2, Sx / 2, Nx)
        rho_y = np.linspace(-Sy / 2, Sy / 2, Ny)
        rho_X, rho_Y = np.meshgrid(rho_x, rho_y)
        rho = z(rho_X, rho_Y)
        beta_vector = rho.reshape(rho.size, order='C')

        design_function = basis.parameterized_function(beta_vector)
        design_object = [
            mp.Block(center=design_region.center,
                     size=design_region.size,
                     epsilon_func=design_function.func())
        ]

        geometry = design_object

        sim = mp.Simulation(cell_size=cell_size,
                            geometry=geometry,
                            resolution=resolution)
        sim.init_sim()
        (x_interp, y_interp, z_interp,
         w) = sim.get_array_metadata(center=mp.Vector3(), size=cell_size)
        eps_data = sim.get_array(center=mp.Vector3(),
                                 size=cell_size,
                                 component=mp.Dielectric)

        rho_X_interp, rho_Y_interp = np.meshgrid(x_interp, y_interp)
        rho_interp_true = z(rho_X_interp, rho_Y_interp)

        A = mpa.gen_interpolation_matrix(rho_x, rho_y, np.array(x_interp),
                                         np.array(y_interp))
        rho_interp_hat = A * rho.reshape(rho.size, order='C')

        # Calculate errors
        sim_err = np.mean(
            np.abs(
                eps_data.reshape(eps_data.size, order='C') -
                rho_interp_true.reshape(rho_interp_true.size, order='C'))**2)
        A_err = np.mean(
            np.abs(rho_interp_hat -
                   rho_interp_true.reshape(rho_interp_true.size, order='C'))**
            2)
        meep_err = np.mean(
            np.abs(rho_interp_hat -
                   eps_data.reshape(eps_data.size, order='C'))**2)

        print("Simulation Mean Squared Error:                             ",
              sim_err)
        print("Interpolation Matrix Mean Squared Error:                   ",
              A_err)
        print("MSE between interp. matrix and meep's dielectric function: ",
              meep_err)
        '''from matplotlib import pyplot as plt
        plt.figure()
        plt.imshow(eps_data)
        plt.title('Bilinear Interpolation')

        plt.figure()
        plt.imshow(rho_interp_hat.reshape(rho_interp_true.shape,order='C'))
        plt.title('Sparse Matrix Solution')

        plt.figure()
        plt.imshow(rho_interp_true)
        plt.title('Analytic Solution')

        plt.show()'''

        return sim_err, A_err, meep_err