Example #1
0
def test_empty_array():
    "Confirm that an empty array has the expected dimensions and lookup"
    sim = openmodes.Simulation()
    name = "SRR"
    mesh_tol = 1e-3

    sim = openmodes.Simulation(name=name)
    mesh = sim.load_mesh(osp.join(openmodes.geometry_dir, name+'.geo'),
                         mesh_tol=mesh_tol)
    part1 = sim.place_part(mesh)
    part2 = sim.place_part(mesh, location=[0, 0, 5e-3])

    vec = sim.empty_array()

    s = 2j*np.pi*1e9
    Z = sim.impedance(s)
    pw = PlaneWaveSource([0, 1, 0], [0, 0, 1])
    V = sim.source_vector(pw, 0)
    I = Z.solve(V)

    assert(vec.shape == I.shape)
    assert(vec.lookup == I.lookup)

    vec2 = sim.empty_array(part2)
    assert(vec2.shape == I[:, part2].shape)
    assert(vec2.lookup == I[:, part2].lookup)
Example #2
0
    def save():
        name = "SRR"
        mesh_tol = 1e-3

        sim = openmodes.Simulation(name=name)
        mesh = sim.load_mesh(osp.join(openmodes.geometry_dir, name+'.geo'),
                             mesh_tol=mesh_tol)
        parent_part = sim.place_part()
        sim.place_part(mesh, parent=parent_part)
        sim.place_part(mesh, parent=parent_part, location=[10, 10, 10])
        sim.place_part(mesh, location=[10, 10, 10])

        parents_dict = {}
        for part in sim.parts.iter_all():
            print("Original part", part)
            if part.parent_ref is None:
                parents_dict[str(part.id)] = 'None'
            else:
                parents_dict[str(part.id)] = str(part.parent_ref().id)

        pw = PlaneWaveSource([0, 1, 0], [0, 0, 1])
        V = sim.source_vector(pw, 0)

        with tempfile.NamedTemporaryFile(delete=False) as output_file:
            file_name = output_file.name
            pickle.dump(V, output_file, protocol=0)

        return file_name, parents_dict
Example #3
0
def test_extinction(plot_extinction=False,
                    skip_asserts=False,
                    write_reference=False):
    "Test extinction of a horseshoe"
    sim = openmodes.Simulation(name='horseshoe_extinction',
                               basis_class=openmodes.basis.LoopStarBasis)

    shoe = sim.load_mesh(osp.join(mesh_dir, 'horseshoe_rect.msh'))
    sim.place_part(shoe)

    num_freqs = 101
    freqs = np.linspace(1e8, 20e9, num_freqs)

    extinction = np.empty(num_freqs, np.complex128)

    e_inc = np.array([1, 0, 0], dtype=np.complex128)
    k_hat = np.array([0, 0, 1], dtype=np.complex128)
    pw = PlaneWaveSource(e_inc, k_hat)

    for freq_count, s in sim.iter_freqs(freqs):
        Z = sim.impedance(s)
        V = sim.source_vector(pw, s)
        extinction[freq_count] = np.vdot(V, Z.solve(V))

    if write_reference:
        # generate the reference extinction solution
        write_1d_complex(osp.join(reference_dir, 'extinction.txt'), extinction)

    extinction_ref = read_1d_complex(osp.join(reference_dir, 'extinction.txt'))

    if not skip_asserts:
        assert_allclose(extinction, extinction_ref, rtol=1e-3)

    if plot_extinction:
        # to plot the generated and reference solutions
        plt.figure()
        plt.plot(freqs * 1e-9, extinction.real)
        plt.plot(freqs * 1e-9, extinction_ref.real, '--')
        plt.plot(freqs * 1e-9, extinction.imag)
        plt.plot(freqs * 1e-9, extinction_ref.imag, '--')
        plt.xlabel('f (GHz)')
        plt.show()
Example #4
0
def geometry_extinction_modes(name, freqs, num_modes, mesh_tol,
                              plot_currents=False, plot_admittance=True,
                              parameters={}, model_class=ScalarModelLeastSq):
    """Load a geometry file, calculate its modes by searching for
    singularities, and plot them in 3D. Then use the modes to calculate
    extinction, and compare with exact calculation
    """

    sim = openmodes.Simulation(name=name,
                               basis_class=openmodes.basis.LoopStarBasis)
    mesh = sim.load_mesh(osp.join(openmodes.geometry_dir, name+'.geo'),
                         mesh_tol=mesh_tol, parameters=parameters)
    part = sim.place_part(mesh)

    s_start = 2j*np.pi*0.5*(freqs[0]+freqs[-1])

    mode_s, mode_j = sim.singularities(s_start, num_modes, part)

    if plot_currents:
        for mode in range(num_modes):
            current = sim.empty_vector()
            current[:] = mode_j[:, mode]
            sim.plot_3d(solution=current, output_format='mayavi',
                        compress_scalars=1)

    if not plot_admittance:
        return

    models = sim.construct_models(mode_s, mode_j, part,
                                  model_class=model_class)

    num_freqs = len(freqs)

    extinction = np.empty(num_freqs, np.complex128)
    extinction_sem = np.empty((num_freqs, num_modes), np.complex128)
    extinction_eem = np.empty((num_freqs, num_modes), np.complex128)

    e_inc = np.array([1, 1, 0], dtype=np.complex128)/np.sqrt(2)
    k_hat = np.array([0, 0, 1], dtype=np.complex128)
    pw = PlaneWaveSource(e_inc, k_hat)

    z_sem = np.empty((num_freqs, num_modes), np.complex128)
    z_eem = np.empty((num_freqs, num_modes), np.complex128)
#    z_eem_direct = np.empty((num_freqs, num_modes), np.complex128)

    for freq_count, s in sim.iter_freqs(freqs):
        Z = sim.impedance(s)
        V = sim.source_vector(pw, s)
        I = Z.solve(V)
        extinction[freq_count] = np.vdot(V[part], I)

        z_sem[freq_count] = [model.scalar_impedance(s) for model in models]
        extinction_sem[freq_count] = [np.vdot(V, model.solve(s, V))
                                      for model in models]

#        z_eem_direct[freq_count], _ = Z.eigenmodes(num_modes, use_gram=False)
        z_eem[freq_count], j_eem = Z.eigenmodes(start_j=mode_j, use_gram=True)
        extinction_eem[freq_count] = [np.vdot(V, j_eem[:, mode]) *
                                      V.dot(j_eem[:, mode]) / z_eem[freq_count, mode]
                                      for mode in range(num_modes)]

    plt.figure(figsize=(10, 5))
    plt.subplot(121)
    plt.plot(freqs*1e-9, extinction.real)
    plt.plot(freqs*1e-9, np.sum(extinction_sem.real, axis=1), '--')
    #plt.plot(freqs*1e-9, np.sum(extinction_eem.real, axis=1), '-.')
    plt.xlabel('f (GHz)')
    plt.subplot(122)
    plt.plot(freqs*1e-9, extinction.imag)
    plt.plot(freqs*1e-9, np.sum(extinction_sem.imag, axis=1), '--')
    #plt.plot(freqs*1e-9, np.sum(extinction_eem.imag, axis=1), '-.')
    plt.suptitle("Extinction")
    plt.show()

#    plt.figure(figsize=(10,5))
#    plt.subplot(121)
#    plt.plot(freqs*1e-9, z_eem_direct.real)
#    #plt.ylim(0, 80)
#    plt.xlabel('f (GHz)')
#    plt.subplot(122)
#    plt.plot(freqs*1e-9, z_eem_direct.imag)
#    plt.plot([freqs[0]*1e-9, freqs[-1]*1e-9], [0, 0], 'k')
#    plt.suptitle("EEM impedance without Gram matrix")
#    plt.show()

    y_sem = 1/z_sem
    y_eem = 1/z_eem

    z_sem *= 2j*np.pi*freqs[:, None]
    z_eem *= 2j*np.pi*freqs[:, None]

    plt.figure(figsize=(10, 5))
    plt.subplot(121)
    plt.plot(freqs*1e-9, z_eem.real)
    plt.plot(freqs*1e-9, z_sem.real, '--')
    plt.xlabel('f (GHz)')
    plt.subplot(122)
    plt.plot(freqs*1e-9, z_eem.imag)
    plt.plot(freqs*1e-9, z_sem.imag, '--')
    plt.suptitle("SEM and EEM impedance")
    plt.show()

    plt.figure(figsize=(10, 5))
    plt.subplot(121)
    plt.plot(freqs*1e-9, y_eem.real)
    plt.plot(freqs*1e-9, y_sem.real, '--')
    plt.xlabel('f (GHz)')
    plt.subplot(122)
    plt.plot(freqs*1e-9, y_eem.imag)
    plt.plot(freqs*1e-9, y_sem.imag, '--')
    plt.xlabel('f (GHz)')
    plt.suptitle("SEM and EEM admittance")
    plt.show()
Example #5
0
def pec_sphere_multipoles(plot=False):
    "Multipole expansion of a PEC sphere"
    sim = openmodes.Simulation(name='pec_sphere_multipoles')
    mesh = sim.load_mesh(meshfile)
    sim.place_part(mesh)

    k0r = np.linspace(0.1, 3, 50)
    freqs = k0r * c / (2 * np.pi)
    pw = PlaneWaveSource([1, 0, 0], [0, 0, 1], p_inc=1.0)

    multipole_order = 4
    extinction = np.empty(len(freqs), dtype=np.complex128)

    a_e = {}
    a_m = {}
    for l in range(multipole_order + 1):
        for m in range(-l, l + 1):
            a_e[l, m] = np.empty(len(freqs), dtype=np.complex128)
            a_m[l, m] = np.empty(len(freqs), dtype=np.complex128)

    for freq_count, s in sim.iter_freqs(freqs):
        Z = sim.impedance(s)
        V = sim.source_vector(pw, s)
        V_E = sim.source_vector(pw, s, extinction_field=True)
        I = Z.solve(V)
        extinction[freq_count] = np.vdot(V_E, I)

        a_en, a_mn = sim.multipole_decomposition(I, multipole_order, s)
        for l in range(multipole_order + 1):
            for m in range(-l, l + 1):
                a_e[l, m][freq_count] = a_en[l, m]
                a_m[l, m][freq_count] = a_mn[l, m]

    if plot:
        plt.figure()
        plt.plot(k0r, extinction.real)
        plt.plot(
            k0r, np.pi / k0r**2 * sum(
                sum((np.abs(a_e[l, m])**2 + np.abs(a_m[l, m])**2)
                    for m in range(-l, l + 1))
                for l in range(1, multipole_order + 1)), 'x')
        plt.plot(
            k0r, np.pi / k0r**2 * sum(
                sum(
                    np.sqrt(2 * l + 1) * (-m * a_e[l, m].real - a_m[l, m].real)
                    for m in range(-l, l + 1))
                for l in range(1, multipole_order + 1)), '+')
        plt.xlabel('$k_{0}r$')
        plt.title("Total extinction vs multipole extinction and scattering")
        plt.show()

        plt.figure()
        for l in range(1, multipole_order + 1):
            for m in range(-l, l + 1):
                plt.plot(k0r, np.pi / k0r**2 * np.abs(a_e[l, m])**2)
                plt.plot(k0r, np.pi / k0r**2 * np.abs(a_m[l, m])**2, '--')
        plt.title("Multipole contributions to scattering")
        plt.xlabel('$k_{0}r$')
        plt.show()

        plt.figure()
        for l in range(1, multipole_order + 1):
            for m in (-1, 1):
                plt.plot(
                    k0r,
                    -np.pi / k0r**2 * np.sqrt(2 * l + 1) * m * a_e[l, m].real)
                plt.plot(k0r,
                         -np.pi / k0r**2 * np.sqrt(2 * l + 1) * a_m[l, m].real,
                         '--')
        plt.title("Multipole contributions to extinction")
        plt.xlabel('$k_{0}r$')
        plt.show()

    else:
        return {
            'name': 'pec_sphere_multipoles',
            'results': {
                'k0r': k0r,
                'extinction': extinction,
                'a_e': a_e,
                'a_m': a_m
            },
            'rtol': {
                'a_e': 1e-6,
                'a_m': 1e-6
            }
        }
Example #6
0
def test_extinction_all(plot_extinction=False, skip_asserts=False,
                        write_reference=False):
    "Extinction of a PEC sphere with EFIE, MFIE, CFIE"

    tests = (("EFIE", EfieOperator, 'extinction_efie.npy'),
             ("MFIE", MfieOperator, 'extinction_mfie.npy'),
             ("CFIE", CfieOperator, 'extinction_cfie.npy'))

    for operator_name, operator_class, reference_filename in tests:

        sim = openmodes.Simulation(name='horseshoe_extinction',
                                   basis_class=DivRwgBasis,
                                   operator_class=operator_class)

        radius = 5e-3
        sphere = sim.load_mesh(osp.join(mesh_dir, 'sphere.msh'))
        sim.place_part(sphere)

        num_freqs = 101
        freqs = np.linspace(1e8, 20e9, num_freqs)

        extinction = np.empty(num_freqs, np.complex128)

        e_inc = np.array([1, 0, 0], dtype=np.complex128)
        k_hat = np.array([0, 0, 1], dtype=np.complex128)
        pw = PlaneWaveSource(e_inc, k_hat)

        for freq_count, s in sim.iter_freqs(freqs):
            Z = sim.impedance(s)
            V = sim.source_vector(pw, s)
            V_E = sim.source_vector(pw, s, extinction_field=True)
            extinction[freq_count] = np.vdot(V_E, Z.solve(V))

        extinction_filename = osp.join(reference_dir, reference_filename)

        if write_reference:
            # generate the reference extinction solution
            write_1d_complex(extinction_filename, extinction)

        extinction_ref = read_1d_complex(extinction_filename)

        if not skip_asserts:
            assert_allclose(extinction, extinction_ref, rtol=1e-3)

        if plot_extinction:
            # to plot the generated and reference solutions

            # calculate analytically
            extinction_analytical = sphere_extinction_analytical(freqs, radius)
            plt.figure(figsize=(8, 6))
            plt.plot(freqs*1e-9, extinction.real)
            plt.plot(freqs*1e-9, extinction_ref.real, '--')
            plt.plot(freqs*1e-9, extinction_analytical, 'x')
            plt.plot(freqs*1e-9, extinction.imag)
            plt.plot(freqs*1e-9, extinction_ref.imag, '--')
            plt.xlabel('f (GHz)')
            plt.legend(('Calculated (Re)', 'Reference (Re)', 'Analytical (Re)',
                        'Calculated (Im)', 'Reference (Im)'), loc='right')
            plt.title('Extinction with operator %s' % operator_name)
            plt.ylim(ymin=0)
            plt.show()