Пример #1
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)
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
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
Пример #6
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
Пример #7
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
Пример #8
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