Пример #1
0
def test_inner_products():
    kvs = [bspline.make_knots(p, 0.0, 1.0, 8+p) for p in range(3,6)]
    def f(x,y,z): return np.cos(x) * np.exp(y) * np.sin(z)
    inp = inner_products(kvs, f)
    assert inp.shape == tuple(kv.numdofs for kv in kvs)
    inp2 = inner_products(kvs, f, geo=geometry.unit_cube())
    assert np.allclose(inp, inp2)
Пример #2
0
def test_stiffness_3d():
    kvs = (bspline.make_knots(3, 0.0, 1.0, 4),
           bspline.make_knots(3, 0.0, 1.0, 5),
           bspline.make_knots(3, 0.0, 1.0, 6))
    assert(np.allclose(
        bsp_stiffness_3d(kvs, geo=None).A,
        bsp_stiffness_3d(kvs, geo=geometry.unit_cube()).A,
        rtol=0, atol=1e-14)
    )
Пример #3
0
def test_integrate():
    def one(*X): return 1.0

    geo = geometry.unit_square()
    assert abs(1.0 - integrate(geo.kvs, one)) < 1e-8
    assert abs(1.0 - integrate(geo.kvs, one, f_physical=True, geo=geo)) < 1e-8

    geo = geometry.unit_cube()
    assert abs(1.0 - integrate(geo.kvs, one)) < 1e-8
    assert abs(1.0 - integrate(geo.kvs, one, f_physical=True, geo=geo)) < 1e-8

    kvs = 2 * (bspline.make_knots(3, 0.0, 1.0, 10),)
    geo = geometry.quarter_annulus()
    assert abs(3*np.pi/4 - integrate(kvs, one, geo=geo)) < 1e-8
Пример #4
0
def test_wave_st_2d():
    T_end = 2.0
    geo = geometry.unit_cube(dim=1).extrude(0.0, T_end, support=(0.0,T_end))
    kv_t = bspline.make_knots(2, 0.0, T_end, 6)    # time axis
    kv   = bspline.make_knots(3, 0.0,   1.0, 8)    # space axis
    kvs = (kv_t, kv)

    M = mass(kv)
    DxDx = stiffness(kv)
    D0Dt = bsp_mixed_deriv_biform_1d(kv_t, 0, 1)
    DttDt = bsp_mixed_deriv_biform_1d(kv_t, 2, 1)
    A_ref = (spkron(DttDt, M) + spkron(D0Dt, DxDx)).tocsr()

    A = assemble(assemblers.WaveAssembler_ST2D(kvs, geo))
    assert abs(A_ref - A).max() < 1e-14
Пример #5
0
def test_heat_st_2d():
    T_end = 2.0
    geo = geometry.unit_cube(dim=1).cylinderize(0.0, T_end, support=(0.0,T_end))
    kv_t = bspline.make_knots(2, 0.0, T_end, 6)    # time axis
    kv   = bspline.make_knots(3, 0.0,   1.0, 8)    # space axis
    kvs = (kv_t, kv)

    M = mass(kv)
    M_t = mass(kv_t)
    DxDx = stiffness(kv)
    DtD0 = bsp_mixed_deriv_biform_1d(kv_t, 1, 0)
    A_ref = (spkron(DtD0, M) + spkron(M_t, DxDx)).tocsr()

    A = assemble_entries(assemblers.HeatAssembler_ST2D(kvs, geo))
    assert abs(A_ref - A).max() < 1e-14
Пример #6
0
def test_assemble_string():
    kvs = 2 * (bspline.make_knots(2, 0.0, 1.0, 10),)
    geo = geometry.quarter_annulus()
    A1 = assemble('inner(grad(u), grad(v)) * dx', kvs, geo=geo)
    A2 = stiffness(kvs, geo)
    assert np.allclose(A1.A, A2.A)

    # use Assembler class
    asm = Assembler('inner(grad(u), grad(v)) * dx', kvs, geo=geo, symmetric=True, updatable=['geo'])
    A3 = asm.assemble()
    assert np.allclose(A2.A, A3.A)

    with unittest.TestCase().assertRaises(RuntimeError):
        asm.assemble(f=geo)       # not an updatable field
    with unittest.TestCase().assertRaises(ValueError):
        asm = Assembler('inner(grad(u), grad(v)) * dx', kvs, geo=geo, updatable=['f'])  # f is not an input

    f = lambda x, y: x * y**2
    f1 = assemble('f * v * dx', kvs, geo=geo, f=f)
    f2 = inner_products(kvs, f, geo=geo, f_physical=True)
    assert np.allclose(f1, f2)

    # vector-valued problems
    f1 = assemble('f * div(v) * dx', kvs, bfuns=[('v',2)], geo=geo, f=f, layout='packed')
    f2 = assemble('f * div(v) * dx', kvs, bfuns=[('v',2)], geo=geo, f=f, layout='blocked')
    assert np.allclose(f1.transpose(2, 0, 1), f2)

    # 1D problem - matrix
    geo = geometry.unit_cube(dim=1)
    A1 = assemble('inner(grad(u), grad(v)) * dx', kvs[:1], geo=geo)
    A2 = stiffness(kvs[0])
    assert np.allclose(A1.A, A2.A)
    # 1D problem - rhs
    f = lambda x: 1 + x**2
    f1 = assemble('f * v * dx', kvs[:1], geo=geo, f=f)
    f2 = inner_products(kvs[0], f=f, f_physical=True, geo=geo)
    assert np.allclose(f1, f2)

    # test error handling
    with unittest.TestCase().assertRaises(ValueError) as cm:
        assemble('inner(grad(u), grad(v)) * dx', kvs)
    assert str(cm.exception) == "required input parameter 'geo' missing"
Пример #7
0
def test_inner_products():
    kvs = [bspline.make_knots(p, 0.0, 1.0, 8+p) for p in range(3,6)]
    def f(x,y,z): return np.cos(x) * np.exp(y) * np.sin(z)
    inp = inner_products(kvs, f)
    assert inp.shape == tuple(kv.numdofs for kv in kvs)
    inp2 = inner_products(kvs, f, geo=geometry.unit_cube())
    assert np.allclose(inp, inp2)

    # compare to autogenerated assemblers when using geometry
    geo = geometry.twisted_box()
    import pyiga.assemblers

    # physical=False
    asm = pyiga.assemblers.L2FunctionalAssembler3D(kvs, geo, f=f)
    inp = asm.assemble_vector()
    inp2 = inner_products(kvs, f, geo=geo)
    assert np.allclose(inp, inp2)

    # physical=True
    asm = pyiga.assemblers.L2FunctionalAssemblerPhys3D(kvs, geo, f=f)
    inp = asm.assemble_vector()
    inp2 = inner_products(kvs, f, f_physical=True, geo=geo)
    assert np.allclose(inp, inp2)
Пример #8
0
def test_interpolate_physical():
    f = lambda x,y,z: np.cos(x)*np.sin(y)*np.exp(z)
    kvs = 3 * (bspline.make_knots(3, 0.0, 1.0, 10),)
    x1 = interpolate(kvs, f)
    x2 = interpolate(kvs, f, geo=geometry.unit_cube())
    assert np.allclose(x1, x2)
Пример #9
0
def test_project_L2_geo():
    f = lambda x,y,z: np.cos(x)*np.sin(y)*np.exp(z)
    kvs = 3 * (bspline.make_knots(3, 0.0, 1.0, 10),)
    x1 = project_L2(kvs, f)
    x2 = project_L2(kvs, f, geo=geometry.unit_cube())
    assert np.allclose(x1, x2)