Esempio n. 1
0
def test5():
    eps = 1e-12
    func = lambda x: x**2
    mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2))
    mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2))
    mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1))
    mesh4 = Mesh1D((-5, 10), (2, ))
    mesh5 = Mesh1D((-5, 10), (3, ))
    mesh6 = Mesh1D((-5, 10), (1, ))
    f = Function(func, mesh1)
    g = Function(func, mesh2)
    h = Function(func, mesh3)
    l = Function(func, mesh4)

    assert f == g
    assert g == f
    assert f == l
    assert g == l
    assert f != h
    assert h != f
    assert g != h
    assert h != g

    assert f == Function(lambda x: x**2, mesh1)
    assert f != Function(lambda x: x**3, mesh1)
    assert f == Function(lambda x: x**2, mesh2)
    assert f == Function(lambda x: x**2, mesh4)
    assert f == Function(lambda x: x**2, mesh5)
    assert f != Function(lambda x: x**2, mesh6)
Esempio n. 2
0
def test3():
    eps = 1e-12
    func = lambda x: x**2
    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]:
        assert abs(f(x) - func(x)) < eps

    func = lambda x: x**3
    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]:
        assert abs(f(x) - func(x)) < eps

    func = lambda x: x**4
    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]:
        assert abs(f(x) - func(x)) < eps

    func = lambda x: x**5
    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    for x in [-4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3]:
        assert abs(f(x) - func(x)) < eps

    func = lambda x: x**6
    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    x = -1
    assert abs(f(x) - func(x)) > 61.9
    x = 0
    assert abs(f(x) - func(x)) > 61.9
    x = 1
    assert abs(f(x) - func(x)) > 61.6
    x = 2
    assert abs(f(x) - func(x)) > 28.9
Esempio n. 3
0
def test_l2_h1_2():
    eps = 1e-9
    func = lambda x: log(x)
    mesh1 = Mesh1D((1, 1.5, 2, 2.5, e), (20, 20, 20, 20))
    f = Function(func, mesh1)
    l2_norm_exact = sqrt(e-2)
    h1_norm_exact = sqrt(e-1-exp(-1))
    assert abs(f.l2_norm(method="Fekete")-l2_norm_exact) < eps
    assert abs(f.l2_norm(method="FE")-l2_norm_exact) < eps
    assert abs(f.h1_norm()-h1_norm_exact) < eps
Esempio n. 4
0
def test4():
    eps = 1e-12
    func = lambda x: x**2
    orig_mesh = Mesh1D((-5, -4, 3, 10), (1, 5, 1))
    mesh1     = Mesh1D((-5, -4, 3, 10), (1, 1, 1))
    f = Function(func, orig_mesh)
    g = f.project_onto(mesh1)
    h = Function(func, mesh1)
    assert g == Function(func, mesh1)
    assert h == h.project_onto(orig_mesh)
Esempio n. 5
0
def test_l2_h1_2():
    eps = 1e-9
    func = lambda x: log(x)
    mesh1 = Mesh1D((1, 1.5, 2, 2.5, e), (20, 20, 20, 20))
    f = Function(func, mesh1)
    l2_norm_exact = sqrt(e - 2)
    h1_norm_exact = sqrt(e - 1 - exp(-1))
    assert abs(f.l2_norm(method="Fekete") - l2_norm_exact) < eps
    assert abs(f.l2_norm(method="FE") - l2_norm_exact) < eps
    assert abs(f.h1_norm() - h1_norm_exact) < eps
Esempio n. 6
0
def test_power():
    eps = 1e-12
    func = lambda x: x
    mesh1 = Mesh1D((0, 1), (1,))
    mesh2 = Mesh1D((0, 1), (2,))
    mesh3 = Mesh1D((0, 1), (3,))
    f = Function(func, mesh1)
    assert abs(f.l2_norm() - sqrt(1./3)) < eps
    assert f**2 != Function(lambda x: x**2, mesh1)
    assert f**2 == Function(lambda x: x**2, mesh2)
    assert f**2 == Function(lambda x: x**2, mesh3)

    func = lambda x: x
    mesh1 = Mesh1D((5, 6), (1,))
    mesh2 = Mesh1D((5, 6), (2,))
    mesh3 = Mesh1D((5, 6), (3,))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**2, mesh1)
    assert f**2 == Function(lambda x: x**2, mesh2)
    assert f**2 == Function(lambda x: x**2, mesh3)

    func = lambda x: x**3+x
    mesh1 = Mesh1D((5, 6), (3,))
    mesh2 = Mesh1D((5, 6), (5,))
    mesh3 = Mesh1D((5, 6), (6,))
    mesh4 = Mesh1D((5, 6), (9,))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**6+2*x**4+x**2, mesh1)
    assert f**2 != Function(lambda x: x**6+2*x**4+x**2, mesh2)
    assert f**2 == Function(lambda x: x**6+2*x**4+x**2, mesh3)
    assert f**3 == Function(lambda x: x**3 + 3*x**5 + 3*x**7 + x**9, mesh4)

    func = lambda x: x**3+x
    mesh1 = Mesh1D((5, 5.1, 6), (3, 3))
    mesh2 = Mesh1D((5, 5.1, 6), (5, 5))
    mesh3 = Mesh1D((5, 5.1, 6), (6, 6))
    mesh4 = Mesh1D((5, 5.1, 6), (9, 9))
    mesh5 = Mesh1D((5, 5.1, 6), (9, 7))
    mesh6 = Mesh1D((5, 5.1, 6), (6, 5))
    mesh7 = Mesh1D((5, 6), (6,))
    mesh8 = Mesh1D((5, 6), (9,))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**6+2*x**4+x**2, mesh1)
    assert f**2 != Function(lambda x: x**6+2*x**4+x**2, mesh2)
    assert f**2 != Function(lambda x: x**6+2*x**4+x**2, mesh6)
    assert f**2 == Function(lambda x: x**6+2*x**4+x**2, mesh3)
    assert f**3 == Function(lambda x: x**3 + 3*x**5 + 3*x**7 + x**9, mesh4)
    assert f**3 != Function(lambda x: x**3 + 3*x**5 + 3*x**7 + x**9, mesh5)

    assert f**2 == Function(lambda x: x**6+2*x**4+x**2, mesh7)
    assert f**3 == Function(lambda x: x**3 + 3*x**5 + 3*x**7 + x**9, mesh8)
Esempio n. 7
0
def test8():
    eps = 1e-12
    func = lambda x: x**2
    mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2))
    mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2))
    mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1))
    mesh4 = Mesh1D((-5, 10), (2,))
    mesh5 = Mesh1D((-5, 10), (3,))
    mesh6 = Mesh1D((-5, 10), (1,))
    f = Function(func, mesh1)
    g = Function(func, mesh2)
    h = Function(func, mesh3)
    l = Function(func, mesh4)
    zero = Function(lambda x: 0., Mesh1D((-5, 10), (1,)))
    assert zero.l2_norm() < eps
    assert Function(lambda x: 0., mesh1) == zero
    assert Function(lambda x: 0., mesh2) == zero
    assert Function(lambda x: 0., mesh3) == zero
    assert Function(lambda x: 0., mesh4) == zero
    assert Function(lambda x: 0., mesh5) == zero
    assert Function(lambda x: 0., mesh6) == zero

    assert f - g == zero
    assert (f-g).l2_norm() < eps
    assert g - f == zero
    assert (g - f).l2_norm() < eps
    assert f - l == zero
    assert (f - l).l2_norm() < eps
    assert g - l == zero
    assert (g - l).l2_norm() < eps
    assert f - h != zero
    assert (f - h).l2_norm() > eps
    assert h - f != zero
    assert (h - f).l2_norm() > eps
    assert g - h != zero
    assert (g - h).l2_norm() > eps
    assert h - g != zero
    assert (h - g).l2_norm() > eps

    assert f - Function(lambda x: x**2, mesh1) == zero
    assert f - Function(lambda x: x**3, mesh1) != zero
    assert f - Function(lambda x: x**2, mesh2) == zero
    assert f - Function(lambda x: x**2, mesh4) == zero
    assert f - Function(lambda x: x**2, mesh5) == zero
    assert f - Function(lambda x: x**2, mesh6) != zero
Esempio n. 8
0
def test_l2_h1_proj6():
    """
    Tests exact projections.

    Slightly more complicated example.
    """
    f_exact = lambda x: sin(x)*exp(x)
    f_exact_l2 = lambda x: -e*cos(1)/4 + e*sin(1)/4 + exp(-1)*sin(1)/4 + 3*x*(e*sin(1)/2 - exp(-1)*sin(1)/2 - cos(1)*exp(-1))/2 + cos(1)*exp(-1)/4
    pts = [-1, -0.5, 0, 0.5, 1]
    orders = [20]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f = Function(f_exact, m)

    pts = [-1, 1]
    orders = [1]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f_proj_l2 = Function(f_exact_l2, m)
    assert (f.project_onto(m, proj_type="L2") - f_proj_l2).l2_norm() < 0.03
Esempio n. 9
0
def test_l2_h1_proj6():
    """
    Tests exact projections.

    Slightly more complicated example.
    """
    f_exact = lambda x: sin(x)*exp(x)
    f_exact_l2 = lambda x: -e*cos(1)/4 + e*sin(1)/4 + exp(-1)*sin(1)/4 + 3*x*(e*sin(1)/2 - exp(-1)*sin(1)/2 - cos(1)*exp(-1))/2 + cos(1)*exp(-1)/4
    pts = [-1, -0.5, 0, 0.5, 1]
    orders = [20]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f = Function(f_exact, m)

    pts = [-1, 1]
    orders = [1]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f_proj_l2 = Function(f_exact_l2, m)
    assert (f.project_onto(m, proj_type="L2") - f_proj_l2).l2_norm() < 0.03
Esempio n. 10
0
def test4():
    eps = 1e-12
    func = lambda x: x**2
    orig_mesh = Mesh1D((-5, -4, 3, 10), (1, 5, 1))
    mesh1 = Mesh1D((-5, -4, 3, 10), (1, 1, 1))
    f = Function(func, orig_mesh)
    g = f.project_onto(mesh1)
    h = Function(func, mesh1)
    assert g == Function(func, mesh1)
    assert h == h.project_onto(orig_mesh)
Esempio n. 11
0
def test_l2_h1_proj5():
    """
    Tests exact projections.

    The exact results were generated using:

    from sympy import sin, cos, integrate, var, pi, exp, log, E, S
    var("x")
    f = exp(x)
    S(1)/2 * integrate(f, (x, -1, 1)) + S(3)/2*x*integrate(x*f, (x, -1, 1))

    """
    f_exact = lambda x: exp(x)
    f_exact_l2 = lambda x: e/2 - exp(-1)/2 + 3*x*exp(-1)
    # TODO: The constant term here has to be checked:
    f_exact_h1 = lambda x: +3*e/8 + 3*exp(-1)/8 + 3*x*(e + exp(-1))/8
    pts = [-1, -0.5, 0, 0.5, 1]
    orders = [20]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f = Function(f_exact, m)

    pts = [-1, 1]
    orders = [1]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    f_proj_l2_exact = Function(f_exact_l2, m)
    f_proj_l2 = f.project_onto(m, proj_type="L2")
    f_proj_h1_exact = Function(f_exact_h1, m)
    f_proj_h1 = f.project_onto(m, proj_type="H1")
    eps_l2 = 1e-3
    eps_h1 = 0.03
    assert (f_proj_l2 - f_proj_l2_exact).l2_norm() < eps_l2
    assert (f_proj_h1 - f_proj_h1_exact).l2_norm() < eps_h1

    # Make sure that if we exchange the L2 and H1 solutions, then the test
    # fails:
    assert (f_proj_l2 - f_proj_h1_exact).l2_norm() > max(eps_l2, eps_h1)
    assert (f_proj_h1 - f_proj_l2_exact).l2_norm() > max(eps_l2, eps_h1)
Esempio n. 12
0
def test2():
    eps = 1e-12
    func = lambda x: x**2
    f = Function(func, Mesh1D((-5, -4, 3, 10), (2, 5, 2)))
    for x in [
            -5, -4.5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 4, 5, 6, 7, 10
    ]:
        assert abs(f(x) - func(x)) < eps

    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 2)))
    for x in [-5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 4, 5, 6, 7, 10]:
        assert abs(f(x) - func(x)) < eps
    x = -4.9
    assert abs(f(x) - func(x)) > 0.08
    x = -4.5
    assert abs(f(x) - func(x)) > 0.24

    f = Function(func, Mesh1D((-5, -4, 3, 10), (1, 5, 1)))
    for x in [-5, -4, -3, -2, -1, 0, 0.01, 1e-5, 1, 2, 3, 10]:
        assert abs(f(x) - func(x)) < eps
    x = -4.9
    assert abs(f(x) - func(x)) > 0.08
    x = -4.5
    assert abs(f(x) - func(x)) > 0.24
    x = 4
    assert abs(f(x) - func(x)) > 5.9
    x = 5
    assert abs(f(x) - func(x)) > 9.9
    x = 6
    assert abs(f(x) - func(x)) > 11.9
    x = 7
    assert abs(f(x) - func(x)) > 11.9
    x = 8
    assert abs(f(x) - func(x)) > 9.9
    x = 9
    assert abs(f(x) - func(x)) > 5.9
Esempio n. 13
0
def test_l2_h1_proj4():
    """
    Tests conversion to FE basis.
    """
    pts = arange(0, 2*pi, 0.4)
    orders = [2]*(len(pts)-1)
    m = Mesh1D(pts, orders)

    f = Function(lambda x: sin(x), m)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    orders = [3]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    orders = [4]*(len(pts)-1)
    m = Mesh1D(pts, orders)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    pts = arange(0, 2*pi, 3)
    orders = [2]*(len(pts)-1)
    m = Mesh1D(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))

    sol_l2 = f_exact.project_onto(m, proj_type="L2")
    sol_h1 = f_exact.project_onto(m, proj_type="H1")
    assert (sol_l2 - f_exact).l2_norm() < 0.07
    assert (sol_h1 - f_exact).l2_norm() < 0.07
Esempio n. 14
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 = CSCMatrix(n_dof)
    rhs = AVector(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="L2")
    x = solve(A.to_scipy_csc().todense(), rhs.to_numpy())
    sol_l2 = FESolution(m, x).to_discrete_function()
    A = CSCMatrix(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f, projection_type="H1")
    x = solve(A.to_scipy_csc().todense(), rhs.to_numpy())
    sol_h1 = FESolution(m, x).to_discrete_function()
    assert sol_l2 == f
    assert sol_h1 == f
Esempio n. 15
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 = CSCMatrix(n_dof)
    rhs = AVector(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="L2")
    x = solve(A.to_scipy_csc().todense(), rhs.to_numpy())
    sol_l2 = FESolution(m, x).to_discrete_function()
    A = CSCMatrix(n_dof)
    assemble_projection_matrix_rhs(m, A, rhs, f_sin, projection_type="H1")
    x = solve(A.to_scipy_csc().todense(), rhs.to_numpy())
    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. 16
0
def test_l2_h1_proj5():
    """
    Tests exact projections.

    The exact results were generated using:

    from sympy import sin, cos, integrate, var, pi, exp, log, E, S
    var("x")
    f = exp(x)
    S(1)/2 * integrate(f, (x, -1, 1)) + S(3)/2*x*integrate(x*f, (x, -1, 1))

    """
    f_exact = lambda x: exp(x)
    f_exact_l2 = lambda x: e / 2 - exp(-1) / 2 + 3 * x * exp(-1)
    # TODO: The constant term here has to be checked:
    f_exact_h1 = lambda x: +3 * e / 8 + 3 * exp(-1) / 8 + 3 * x * (e + exp(-1)
                                                                   ) / 8
    pts = [-1, -0.5, 0, 0.5, 1]
    orders = [20] * (len(pts) - 1)
    m = Mesh1D(pts, orders)
    f = Function(f_exact, m)

    pts = [-1, 1]
    orders = [1] * (len(pts) - 1)
    m = Mesh1D(pts, orders)
    f_proj_l2_exact = Function(f_exact_l2, m)
    f_proj_l2 = f.project_onto(m, proj_type="L2")
    f_proj_h1_exact = Function(f_exact_h1, m)
    f_proj_h1 = f.project_onto(m, proj_type="H1")
    eps_l2 = 1e-3
    eps_h1 = 0.03
    assert (f_proj_l2 - f_proj_l2_exact).l2_norm() < eps_l2
    assert (f_proj_h1 - f_proj_h1_exact).l2_norm() < eps_h1

    # Make sure that if we exchange the L2 and H1 solutions, then the test
    # fails:
    assert (f_proj_l2 - f_proj_h1_exact).l2_norm() > max(eps_l2, eps_h1)
    assert (f_proj_h1 - f_proj_l2_exact).l2_norm() > max(eps_l2, eps_h1)
Esempio n. 17
0
def test_l2_h1_proj4():
    """
    Tests conversion to FE basis.
    """
    pts = arange(0, 2 * pi, 0.4)
    orders = [2] * (len(pts) - 1)
    m = Mesh1D(pts, orders)

    f = Function(lambda x: sin(x), m)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    orders = [3] * (len(pts) - 1)
    m = Mesh1D(pts, orders)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    orders = [4] * (len(pts) - 1)
    m = Mesh1D(pts, orders)
    assert f.project_onto(m, proj_type="Fekete") == f
    assert f.project_onto(m, proj_type="L2") == f
    assert f.project_onto(m, proj_type="H1") == f

    pts = arange(0, 2 * pi, 3)
    orders = [2] * (len(pts) - 1)
    m = Mesh1D(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))

    sol_l2 = f_exact.project_onto(m, proj_type="L2")
    sol_h1 = f_exact.project_onto(m, proj_type="H1")
    assert (sol_l2 - f_exact).l2_norm() < 0.07
    assert (sol_h1 - f_exact).l2_norm() < 0.07
Esempio n. 18
0
def test_power():
    eps = 1e-12
    func = lambda x: x
    mesh1 = Mesh1D((0, 1), (1, ))
    mesh2 = Mesh1D((0, 1), (2, ))
    mesh3 = Mesh1D((0, 1), (3, ))
    f = Function(func, mesh1)
    assert abs(f.l2_norm() - sqrt(1. / 3)) < eps
    assert f**2 != Function(lambda x: x**2, mesh1)
    assert f**2 == Function(lambda x: x**2, mesh2)
    assert f**2 == Function(lambda x: x**2, mesh3)

    func = lambda x: x
    mesh1 = Mesh1D((5, 6), (1, ))
    mesh2 = Mesh1D((5, 6), (2, ))
    mesh3 = Mesh1D((5, 6), (3, ))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**2, mesh1)
    assert f**2 == Function(lambda x: x**2, mesh2)
    assert f**2 == Function(lambda x: x**2, mesh3)

    func = lambda x: x**3 + x
    mesh1 = Mesh1D((5, 6), (3, ))
    mesh2 = Mesh1D((5, 6), (5, ))
    mesh3 = Mesh1D((5, 6), (6, ))
    mesh4 = Mesh1D((5, 6), (9, ))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh1)
    assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh2)
    assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh3)
    assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh4)

    func = lambda x: x**3 + x
    mesh1 = Mesh1D((5, 5.1, 6), (3, 3))
    mesh2 = Mesh1D((5, 5.1, 6), (5, 5))
    mesh3 = Mesh1D((5, 5.1, 6), (6, 6))
    mesh4 = Mesh1D((5, 5.1, 6), (9, 9))
    mesh5 = Mesh1D((5, 5.1, 6), (9, 7))
    mesh6 = Mesh1D((5, 5.1, 6), (6, 5))
    mesh7 = Mesh1D((5, 6), (6, ))
    mesh8 = Mesh1D((5, 6), (9, ))
    f = Function(func, mesh1)
    assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh1)
    assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh2)
    assert f**2 != Function(lambda x: x**6 + 2 * x**4 + x**2, mesh6)
    assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh3)
    assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh4)
    assert f**3 != Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh5)

    assert f**2 == Function(lambda x: x**6 + 2 * x**4 + x**2, mesh7)
    assert f**3 == Function(lambda x: x**3 + 3 * x**5 + 3 * x**7 + x**9, mesh8)
Esempio n. 19
0
def test_l2_h1_1():
    eps = 1e-12
    eps_low = 1e-10
    func = lambda x: sin(x)
    mesh1 = Mesh1D((0, pi), (20, ))
    mesh2 = Mesh1D((0, pi / 2, pi), (20, 20))
    mesh3 = Mesh1D((0, pi / 2), (20, ))
    f = Function(func, mesh1)
    g = Function(func, mesh2)
    h = Function(func, mesh3)
    assert abs(f.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps
    assert abs(g.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps
    assert abs(h.l2_norm(method="Fekete") - sqrt(pi / 4)) < eps

    assert abs(f.l2_norm(method="FE") - sqrt(pi / 2)) < eps
    assert abs(g.l2_norm(method="FE") - sqrt(pi / 2)) < eps
    assert abs(h.l2_norm(method="FE") - sqrt(pi / 4)) < eps

    assert abs(f.h1_norm() - sqrt(pi)) < eps_low
    assert abs(g.h1_norm() - sqrt(pi)) < eps_low
    assert abs(h.h1_norm() - sqrt(pi / 2)) < eps_low

    func = lambda x: cos(x)
    mesh1 = Mesh1D((0, pi / 4, pi / 2, 3 * pi / 4, pi), (20, 20, 20, 20))
    f = Function(func, mesh1)
    assert abs(f.l2_norm(method="Fekete") - sqrt(pi / 2)) < eps
    assert abs(f.l2_norm(method="FE") - sqrt(pi / 2)) < eps

    assert abs(f.h1_norm() - sqrt(pi)) < eps
Esempio n. 20
0
def test_l2_h1_1():
    eps = 1e-12
    eps_low = 1e-10
    func = lambda x: sin(x)
    mesh1 = Mesh1D((0, pi), (20,))
    mesh2 = Mesh1D((0, pi/2, pi), (20, 20))
    mesh3 = Mesh1D((0, pi/2), (20,))
    f = Function(func, mesh1)
    g = Function(func, mesh2)
    h = Function(func, mesh3)
    assert abs(f.l2_norm(method="Fekete")-sqrt(pi/2)) < eps
    assert abs(g.l2_norm(method="Fekete")-sqrt(pi/2)) < eps
    assert abs(h.l2_norm(method="Fekete")-sqrt(pi/4)) < eps

    assert abs(f.l2_norm(method="FE")-sqrt(pi/2)) < eps
    assert abs(g.l2_norm(method="FE")-sqrt(pi/2)) < eps
    assert abs(h.l2_norm(method="FE")-sqrt(pi/4)) < eps

    assert abs(f.h1_norm()-sqrt(pi)) < eps_low
    assert abs(g.h1_norm()-sqrt(pi)) < eps_low
    assert abs(h.h1_norm()-sqrt(pi/2)) < eps_low

    func = lambda x: cos(x)
    mesh1 = Mesh1D((0, pi/4, pi/2, 3*pi/4, pi), (20, 20, 20, 20))
    f = Function(func, mesh1)
    assert abs(f.l2_norm(method="Fekete")-sqrt(pi/2)) < eps
    assert abs(f.l2_norm(method="FE")-sqrt(pi/2)) < eps

    assert abs(f.h1_norm()-sqrt(pi)) < eps
Esempio n. 21
0
def test8():
    eps = 1e-12
    func = lambda x: x**2
    mesh1 = Mesh1D((-5, -4, 3, 10), (2, 5, 2))
    mesh2 = Mesh1D((-5, -4, 3, 10), (2, 2, 2))
    mesh3 = Mesh1D((-5, -4, 3, 10), (2, 2, 1))
    mesh4 = Mesh1D((-5, 10), (2, ))
    mesh5 = Mesh1D((-5, 10), (3, ))
    mesh6 = Mesh1D((-5, 10), (1, ))
    f = Function(func, mesh1)
    g = Function(func, mesh2)
    h = Function(func, mesh3)
    l = Function(func, mesh4)
    zero = Function(lambda x: 0., Mesh1D((-5, 10), (1, )))
    assert zero.l2_norm() < eps
    assert Function(lambda x: 0., mesh1) == zero
    assert Function(lambda x: 0., mesh2) == zero
    assert Function(lambda x: 0., mesh3) == zero
    assert Function(lambda x: 0., mesh4) == zero
    assert Function(lambda x: 0., mesh5) == zero
    assert Function(lambda x: 0., mesh6) == zero

    assert f - g == zero
    assert (f - g).l2_norm() < eps
    assert g - f == zero
    assert (g - f).l2_norm() < eps
    assert f - l == zero
    assert (f - l).l2_norm() < eps
    assert g - l == zero
    assert (g - l).l2_norm() < eps
    assert f - h != zero
    assert (f - h).l2_norm() > eps
    assert h - f != zero
    assert (h - f).l2_norm() > eps
    assert g - h != zero
    assert (g - h).l2_norm() > eps
    assert h - g != zero
    assert (h - g).l2_norm() > eps

    assert f - Function(lambda x: x**2, mesh1) == zero
    assert f - Function(lambda x: x**3, mesh1) != zero
    assert f - Function(lambda x: x**2, mesh2) == zero
    assert f - Function(lambda x: x**2, mesh4) == zero
    assert f - Function(lambda x: x**2, mesh5) == zero
    assert f - Function(lambda x: x**2, mesh6) != zero