Esempio n. 1
0
    def project_onto(self, mesh, proj_type="Fekete"):
        """
        Projects 'self' onto the 'mesh' using the 'proj_type' projection.

        proj_type == "Fekete"/"L2"/"H1"
        """
        if mesh == self._mesh:
            return self
        if proj_type == "Fekete":
            return Function(self, mesh)
        elif proj_type in ["L2", "H1"]:
            from hermes1d.h1d_wrapper.h1d_wrapper import \
                    (assemble_projection_matrix_rhs, Mesh, FESolution)
            from hermes_common._hermes_common import CooMatrix
            pts, orders = mesh.get_mesh_data()
            m = Mesh(pts, orders)
            n_dof = m.assign_dofs()
            A = CooMatrix(n_dof)
            rhs = empty(n_dof)
            assemble_projection_matrix_rhs(m, A, rhs, self,
                    projection_type=proj_type)
            coeffs = solve(A.to_scipy_coo().todense(), rhs)
            return FESolution(m, coeffs).to_discrete_function()
        else:
            raise ValueError("Unknown projection type")
Esempio n. 2
0
 def calculate_FE_coeffs(self):
     if self._fe_sol is None:
         from hermes1d.h1d_wrapper.h1d_wrapper import \
                 (assemble_projection_matrix_rhs, Mesh, FESolution)
         from hermes_common._hermes_common import CooMatrix
         pts, orders = self._mesh.get_mesh_data()
         m = Mesh(pts, orders)
         n_dof = m.assign_dofs()
         A = CooMatrix(n_dof)
         rhs = empty(n_dof)
         assemble_projection_matrix_rhs(m, A, rhs, self,
                 projection_type="L2")
         coeffs = solve(A.to_scipy_coo().todense(), rhs)
         self._fe_sol = FESolution(m, coeffs)
Esempio n. 3
0
def solve_dirac(mesh, kappa=1, Z=1, eig_num=4):
    """
    Solves the Dirac equation on the given mesh.

    Returns the energies and eigenfunctions.
    """
    mesh.set_bc_left_dirichlet(0, 0);
    mesh.set_bc_right_dirichlet(0, 0);
    N_dof = mesh.assign_dofs()
    A = CooMatrix(N_dof)
    B = CooMatrix(N_dof)
    assemble_dirac(mesh, A, B, kappa=kappa, Z=Z)
    eigs = solve_eig_scipy(A.to_scipy_coo(), B.to_scipy_coo())
    c = 137.03599911
    eigs = [(E, eig) for E, eig in eigs if E > -2*c**2 and E < 0]
    eigs = eigs[:eig_num]
    #assert len(eigs) == eig_num
    energies = [E for E, eig in eigs]
    eigs = [eig for E, eig in eigs]
    return N_dof, array(energies), eigs
Esempio n. 4
0
def test_matrix2():
    m = CooMatrix(5)
    m.add(1, 3, 3.5)
    m.add(2, 3, 4.5)
    m.add(3, 4, 1.5)
    m.add(0, 2, 1.5)
    m.add(2, 3, 1)
    d2 = array([
        [0, 0, 1.5, 0, 0],
        [0, 0, 0, 3.5, 0],
        [0, 0, 0, 5.5, 0],
        [0, 0, 0, 0, 1.5],
        [0, 0, 0, 0, 0],
    ])
    _coo_conversions_test(m, d2)
Esempio n. 5
0
def test_matrix7():
    m = CooMatrix(5, is_complex=True)
    m.add(1, 3, 3.5 + 1j)
    m.add(2, 3, 4.5 + 2j)
    m.add(3, 4, 1.5 + 3j)
    m.add(1, 3, 1.5 + 4j)
    m.add(2, 3, 1 + 4j)
    d2 = array([
        [0, 0, 0, 0, 0],
        [0, 0, 0, 5 + 5j, 0],
        [0, 0, 0, 5.5 + 6j, 0],
        [0, 0, 0, 0, 1.5 + 3j],
        [0, 0, 0, 0, 0],
    ])
    _coo_conversions_test(m, d2)
Esempio n. 6
0
def test_matrix4():
    m = CooMatrix(5, is_complex=True)
    m.add(1, 3, 3.5)
    m.add(2, 3, 4.5)
    m.add(3, 4, 1.5)
    m.add(4, 2, 1.5)
    m.add(2, 3, 1)
    d2 = array([
        [0, 0, 0, 0, 0],
        [0, 0, 0, 3.5, 0],
        [0, 0, 0, 5.5, 0],
        [0, 0, 0, 0, 1.5],
        [0, 0, 1.5, 0, 0],
    ])
    _coo_conversions_test(m, d2)
Esempio n. 7
0
def test_matrix2():
    m = CooMatrix(5)
    m.add(1, 3, 3.5)
    m.add(2, 3, 4.5)
    m.add(3, 4, 1.5)
    m.add(0, 2, 1.5)
    m.add(2, 3, 1)
    d2 = array([
        [0, 0, 1.5, 0, 0],
        [0, 0, 0, 3.5, 0],
        [0, 0, 0, 5.5, 0],
        [0, 0, 0, 0, 1.5],
        [0, 0, 0, 0, 0],
        ])
    _coo_conversions_test(m, d2)
Esempio n. 8
0
def test_matrix7():
    m = CooMatrix(5, is_complex=True)
    m.add(1, 3, 3.5+1j)
    m.add(2, 3, 4.5+2j)
    m.add(3, 4, 1.5+3j)
    m.add(1, 3, 1.5+4j)
    m.add(2, 3, 1+4j)
    d2 = array([
        [0, 0, 0, 0, 0],
        [0, 0, 0, 5+5j, 0],
        [0, 0, 0, 5.5+6j, 0],
        [0, 0, 0, 0, 1.5+3j],
        [0, 0, 0, 0, 0],
        ])
    _coo_conversions_test(m, d2)
Esempio n. 9
0
def test_matrix4():
    m = CooMatrix(5, is_complex=True)
    m.add(1, 3, 3.5)
    m.add(2, 3, 4.5)
    m.add(3, 4, 1.5)
    m.add(4, 2, 1.5)
    m.add(2, 3, 1)
    d2 = array([
        [0, 0, 0, 0, 0],
        [0, 0, 0, 3.5, 0],
        [0, 0, 0, 5.5, 0],
        [0, 0, 0, 0, 1.5],
        [0, 0, 1.5, 0, 0],
        ])
    _coo_conversions_test(m, d2)
Esempio n. 10
0
def solve_dirac(mesh, kappa=1, Z=1, eig_num=4):
    """
    Solves the Dirac equation on the given mesh.

    Returns the energies and eigenfunctions.
    """
    mesh.set_bc_left_dirichlet(0, 0)
    mesh.set_bc_right_dirichlet(0, 0)
    N_dof = mesh.assign_dofs()
    A = CooMatrix(N_dof)
    B = CooMatrix(N_dof)
    assemble_dirac(mesh, A, B, kappa=kappa, Z=Z)
    eigs = solve_eig_scipy(A.to_scipy_coo(), B.to_scipy_coo())
    c = 137.03599911
    eigs = [(E, eig) for E, eig in eigs if E > -2 * c**2 and E < 0]
    eigs = eigs[:eig_num]
    #assert len(eigs) == eig_num
    energies = [E for E, eig in eigs]
    eigs = [eig for E, eig in eigs]
    return N_dof, array(energies), eigs
Esempio n. 11
0
def test_l2_h1_proj_run():
    """
    Test that the projections run.

    It doesn't test if it's correct.
    """
    pts = arange(0, 2*pi, 1)
    orders = [3]*(len(pts)-1)
    m = Mesh(pts, orders)
    n_dof = m.assign_dofs()
    A = CooMatrix(n_dof)
    rhs = empty(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="L2")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_l2 = FESolution(m, x).to_discrete_function()
    A = CooMatrix(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="H1")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_h1 = FESolution(m, x).to_discrete_function()
    sol_l2.plot(False)
    sol_h1.plot(False)
Esempio n. 12
0
def test_l2_h1_proj3():
    """
    Tests conversion to FE basis.
    """
    pts = arange(0, 2*pi, 0.1)
    orders = [2]*(len(pts)-1)
    m = Mesh(pts, orders)

    f = Function(lambda x: sin(x), Mesh1D(pts, orders))

    n_dof = m.assign_dofs()
    A = CooMatrix(n_dof)
    rhs = empty(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="L2")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_l2 = FESolution(m, x).to_discrete_function()
    A = CooMatrix(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="H1")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_h1 = FESolution(m, x).to_discrete_function()
    assert sol_l2 == f
    assert sol_h1 == f
Esempio n. 13
0
def test_l2_h1_proj2():
    """
    Tests the correctness of the projections.
    """
    pts = arange(0, 2*pi, 3)
    orders = [4]*(len(pts)-1)
    m = Mesh(pts, orders)

    pts = array(list(arange(0, pts[-1], 0.1)) + [pts[-1]])
    orders = [6]*(len(pts)-1)
    f_exact = Function(lambda x: sin(x), Mesh1D(pts, orders))

    n_dof = m.assign_dofs()
    A = CooMatrix(n_dof)
    rhs = empty(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="L2")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_l2 = FESolution(m, x).to_discrete_function()
    A = CooMatrix(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="H1")
    x = solve(A.to_scipy_coo().todense(), rhs)
    sol_h1 = FESolution(m, x).to_discrete_function()
    assert (sol_l2 - f_exact).l2_norm() < 0.002
    assert (sol_h1 - f_exact).l2_norm() < 0.002
Esempio n. 14
0
def test_matrix3():
    m = CooMatrix(5)
    m.add(0, 0, 2)
    m.add(0, 1, 3)
    m.add(1, 0, 3)
    m.add(1, 2, 4)
    m.add(1, 4, 6)
    m.add(2, 1, -1)
    m.add(2, 2, -3)
    m.add(2, 3, 2)
    m.add(3, 2, 1)
    m.add(4, 1, 4)
    m.add(4, 2, 2)
    m.add(4, 4, 1)
    d2 = array([
        [2, 3, 0, 0, 0],
        [3, 0, 4, 0, 6],
        [0,-1,-3, 2, 0],
        [0, 0, 1, 0, 0],
        [0, 4, 2, 0, 1],
        ])
    _coo_conversions_test(m, d2)
    # CSR test:
    m = CSRMatrix(m)
    assert _eq(m.IA, [0, 2, 5, 8, 9, 12])
    assert _eq(m.JA, [0, 1, 0, 2, 4, 1, 2, 3, 2, 1, 2, 4])
    assert _eq(m.A, [2, 3, 3, 4, 6, -1, -3, 2, 1, 4, 2, 1])

    # CSC test:
    m = CSCMatrix(m)
    assert _eq(m.JA, [0, 2, 5, 9, 10, 12])
    assert _eq(m.IA, [0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4])
    assert _eq(m.A, [2, 3, 3, -1, 4, 4, -3, 1, 2, 2, 6, 1])
Esempio n. 15
0
def test_matrix3():
    m = CooMatrix(5)
    m.add(0, 0, 2)
    m.add(0, 1, 3)
    m.add(1, 0, 3)
    m.add(1, 2, 4)
    m.add(1, 4, 6)
    m.add(2, 1, -1)
    m.add(2, 2, -3)
    m.add(2, 3, 2)
    m.add(3, 2, 1)
    m.add(4, 1, 4)
    m.add(4, 2, 2)
    m.add(4, 4, 1)
    d2 = array([
        [2, 3, 0, 0, 0],
        [3, 0, 4, 0, 6],
        [0, -1, -3, 2, 0],
        [0, 0, 1, 0, 0],
        [0, 4, 2, 0, 1],
    ])
    _coo_conversions_test(m, d2)
    # CSR test:
    m = CSRMatrix(m)
    assert _eq(m.IA, [0, 2, 5, 8, 9, 12])
    assert _eq(m.JA, [0, 1, 0, 2, 4, 1, 2, 3, 2, 1, 2, 4])
    assert _eq(m.A, [2, 3, 3, 4, 6, -1, -3, 2, 1, 4, 2, 1])

    # CSC test:
    m = CSCMatrix(m)
    assert _eq(m.JA, [0, 2, 5, 9, 10, 12])
    assert _eq(m.IA, [0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4])
    assert _eq(m.A, [2, 3, 3, -1, 4, 4, -3, 1, 2, 2, 6, 1])