Пример #1
0
def run_local_multigrid(p, dim, n0, disparity, smoother, smooth_steps,
                        strategy, tol):
    hs = create_example_hspace(p, dim, n0, disparity, num_levels=3)
    dir_dofs = hs.dirichlet_dofs()

    def rhs(*x):
        return 1.0

    params = {'geo': geometry.unit_square(), 'f': rhs}

    # assemble and solve the HB-spline problem
    hdiscr = hierarchical.HDiscretization(hs, vform.stiffness_vf(dim=2),
                                          params)
    A_hb = hdiscr.assemble_matrix()
    f_hb = hdiscr.assemble_rhs()
    P_hb = hs.virtual_hierarchy_prolongators()

    LS_hb = assemble.RestrictedLinearSystem(
        A_hb, f_hb, (dir_dofs, np.zeros_like(dir_dofs)))
    u_hb = scipy.sparse.linalg.spsolve(LS_hb.A, LS_hb.b)
    u_hb0 = LS_hb.complete(u_hb)

    # assemble and solve the THB-spline problem
    hs.truncate = True
    hdiscr = hierarchical.HDiscretization(hs, vform.stiffness_vf(dim=2),
                                          params)
    A_thb = hdiscr.assemble_matrix()
    f_thb = hdiscr.assemble_rhs()
    P_thb = hs.virtual_hierarchy_prolongators()

    LS_thb = assemble.RestrictedLinearSystem(
        A_thb, f_thb, (dir_dofs, np.zeros_like(dir_dofs)))
    u_thb = scipy.sparse.linalg.spsolve(LS_thb.A, LS_thb.b)
    u_thb0 = LS_thb.complete(u_thb)

    # iteration numbers of the local MG method in the (T)HB basis
    inds = hs.indices_to_smooth(strategy)
    iter_hb = num_iterations(solvers.local_mg_step(hs, A_hb, f_hb, P_hb, inds,
                                                   smoother, smooth_steps),
                             u_hb0,
                             tol=tol)
    iter_thb = num_iterations(solvers.local_mg_step(hs, A_thb, f_thb, P_thb,
                                                    inds, smoother,
                                                    smooth_steps),
                              u_thb0,
                              tol=tol)

    winner = "HB" if (iter_hb <= iter_thb) else "THB"
    linestr = f'{strategy} ({smoother}) '.ljust(2 * 22)
    print(linestr + f'{iter_hb:5}    {iter_thb:5}    {winner:6}')
    return (iter_hb, iter_thb)
Пример #2
0
def test_hierarchical_assemble():
    hs = create_example_hspace(p=4, dim=2, n0=4, disparity=1, num_levels=3)
    geo = geometry.bspline_quarter_annulus()
    hdiscr = HDiscretization(hs, vform.stiffness_vf(dim=2), {'geo': geo})
    A = hdiscr.assemble_matrix()
    # compute matrix on the finest level for comparison
    A_fine = assemble.stiffness(hs.knotvectors(-1), geo=geo)
    I_hb = hs.represent_fine()
    A_hb = (I_hb.T @ A_fine @ I_hb)
    assert np.allclose(A.A, A_hb.A)
    #
    A3 = assemble.assemble(vform.stiffness_vf(dim=2), hs, geo=geo)
    assert np.allclose(A.A, A3.A)

    #
    def f(x, y):
        return np.cos(x) * np.exp(y)

    f_hb = assemble.inner_products(
        hs.knotvectors(-1), f, f_physical=True, geo=geo).ravel() @ I_hb
    f2 = assemble.assemble('f * v * dx', hs, f=f, geo=geo)
    assert np.allclose(f_hb, f2)
Пример #3
0
def generate(dim):
    code = backend.CodeGen()

    def gen(vf, classname):
        backend.AsmGenerator(vf, classname, code).generate()

    nD = str(dim) + 'D'
    gen(vform.mass_vf(dim), 'MassAssembler' + nD)
    gen(vform.stiffness_vf(dim), 'StiffnessAssembler' + nD)
    gen(vform.heat_st_vf(dim), 'HeatAssembler_ST' + nD)
    gen(vform.wave_st_vf(dim), 'WaveAssembler_ST' + nD)
    gen(vform.divdiv_vf(dim), 'DivDivAssembler' + nD)
    gen(vform.L2functional_vf(dim), 'L2FunctionalAssembler' + nD)

    return code.result()
Пример #4
0
def test_solve_hmultigrid():
    # test the built-in solve_hmultigrid function in pyiga.solvers
    hs = create_example_hspace(p=3, dim=2, n0=10, disparity=1, num_levels=3)

    for truncate in (False, True):  # test HB- and THB-splines
        hs.truncate = truncate
        # assemble and solve the (T)HB-spline problem
        hdiscr = hierarchical.HDiscretization(hs, vform.stiffness_vf(dim=2), {
            'geo': geometry.unit_square(),
            'f': lambda *x: 1.0
        })
        A_hb = hdiscr.assemble_matrix()
        f_hb = hdiscr.assemble_rhs()

        # solve using a direct solver for comparison
        dir_dofs = hs.dirichlet_dofs()
        LS_hb = assemble.RestrictedLinearSystem(
            A_hb, f_hb, (dir_dofs, np.zeros_like(dir_dofs)))
        u_hb = scipy.sparse.linalg.spsolve(LS_hb.A, LS_hb.b)
        u_hb0 = LS_hb.complete(u_hb)

        # we use default parameters for smoother and strategy
        u_mg, iters = solvers.solve_hmultigrid(hs, A_hb, f_hb, tol=1e-8)
        assert np.allclose(u_hb0, u_mg)
Пример #5
0
def test_codegen_poisson2d():
    code = codegen.CodeGen()
    vf = vform.stiffness_vf(2)
    assert (not vf.vec) and vf.arity == 2
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()