Beispiel #1
0
def _assemble_eigen(form, bcs=None):
    if bcs is None:
        bcs = []

    L = EigenMatrix()
    assemble(form, tensor=L)
    for bc in bcs:
        bc.apply(L)
    return L
Beispiel #2
0
def test_readme_images():
    from dolfin import (
        MeshEditor, Mesh, FunctionSpace, assemble, EigenMatrix, dot, grad, dx,
        TrialFunction, TestFunction
        )
    import meshzoo

    points, cells = meshzoo.rectangle(-1.0, 1.0, -1.0, 1.0, 20, 20)

    # Convert points, cells to dolfin mesh
    editor = MeshEditor()
    mesh = Mesh()
    # topological and geometrical dimension 2
    editor.open(mesh, 'triangle', 2, 2, 1)
    editor.init_vertices(len(points))
    editor.init_cells(len(cells))
    for k, point in enumerate(points):
        editor.add_vertex(k, point[:2])
    for k, cell in enumerate(cells.astype(numpy.uintp)):
        editor.add_cell(k, cell)
    editor.close()

    V = FunctionSpace(mesh, 'CG', 1)
    u = TrialFunction(V)
    v = TestFunction(V)
    L = EigenMatrix()
    assemble(dot(grad(u), grad(v)) * dx, tensor=L)
    A = L.sparray()

    # M = A.T.dot(A)
    M = A

    with tempfile.TemporaryDirectory() as temp_dir:
        filepath = os.path.join(temp_dir, 'test.png')
        betterspy.write_png(filepath, M, border_width=2)

    # betterspy.write_png(
    #     'ATA.png', M, border_width=2,
    #     colormap='viridis'
    #     )
    return
Beispiel #3
0
 def _assemble_eigen(form):
     L = EigenMatrix()
     assemble(form, tensor=L)
     return L
Beispiel #4
0
def _assemble_eigen(form, bc=None):
    L = EigenMatrix()
    assemble(form, tensor=L)
    if bc is not None:
        bc.apply(L)
    return L
Beispiel #5
0
    def test_multiplication(self):
        print(
            '== testing multiplication of system matrix for problem of weighted projection ===='
        )

        for dim, pol_order in itertools.product([2, 3], [1, 2]):
            N = 2  # no. of elements
            print('dim={0}, pol_order={1}, N={2}'.format(dim, pol_order, N))

            # creating MESH and defining MATERIAL
            if dim == 2:
                mesh = UnitSquareMesh(N, N)
                m = Expression("1+10*16*x[0]*(1-x[0])*x[1]*(1-x[1])",
                               degree=4)  # material coefficients
            elif dim == 3:
                mesh = UnitCubeMesh(N, N, N)
                m = Expression("1+10*16*x[0]*(1-x[0])*(1-x[1])*x[2]",
                               degree=1)  # material coefficients

            mesh.coordinates()[:] += 0.1 * np.random.random(
                mesh.coordinates().shape)  # mesh perturbation

            V = FunctionSpace(mesh, "CG", pol_order)  # original FEM space
            W = FunctionSpace(mesh, "DG",
                              2 * (pol_order - 1))  # double-grid space

            print('assembling local matrices for DoGIP...')
            Bhat = get_Bhat(
                dim, pol_order, problem=1
            )  # interpolation between V on W on a reference element
            AT_dogip = get_A_T(m, V, W, problem=1)

            dofmapV = V.dofmap()

            def system_multiplication_DoGIP(AT_dogip, Bhat, u_vec):
                # matrix-vector mutliplication in DoGIP
                Au = np.zeros_like(u_vec)
                for ii, cell in enumerate(cells(mesh)):
                    ind = dofmapV.cell_dofs(ii)  # local to global map
                    Bu = Bhat.dot(u_vec[ind])
                    ABu = np.einsum('rsj,sj->rj', AT_dogip[ii], Bu)
                    Au[ind] += np.einsum('rjl,rj->l', Bhat, ABu)
                return Au

            print('assembling system matrix for FEM')
            u, v = TrialFunction(V), TestFunction(V)
            Asp = assemble(m * inner(grad(u), grad(v)) * dx,
                           tensor=EigenMatrix())
            Asp = Asp.sparray()  # sparse FEM matrix

            print('multiplication...')
            ur = Function(V)  # creating random vector
            ur_vec = 10 * np.random.random(V.dim())
            ur.vector().set_local(ur_vec)

            Au_DoGIP = system_multiplication_DoGIP(
                AT_dogip, Bhat, ur_vec)  # DoGIP multiplication
            Auex = Asp.dot(ur_vec)  # FEM multiplication with sparse matrix

            # testing the difference between DoGIP and FEniCS
            self.assertAlmostEqual(0, np.linalg.norm(Auex - Au_DoGIP))
            print('...ok')
Beispiel #6
0
            Amat = Expression("1+10*exp(x[0])*exp(x[1])", degree=2)
        elif dim == 3:
            mesh = UnitCubeMesh(Ne, Ne, Ne)
            Amat = Expression("1+100*exp(x[0])*exp(x[1])*x[2]*x[1]", degree=3)

        mesh.coordinates()[:] += 0.1 * np.random.random(
            mesh.coordinates().shape)  # mesh perturbation

        V = FunctionSpace(mesh, 'CG', p)
        print('V.dim = {0}'.format(V.dim()))

        if calculate in [1, -1]:
            ut, vt = TrialFunction(V), TestFunction(V)
            print('generating matrices A, Ad, A_T')
            if problem == 0:
                AG = assemble(Amat * ut * vt * dx, tensor=EigenMatrix())
                A_T = get_A_T_empty(V, problem=problem, full=False)
            else:
                AG = assemble(inner(Amat * grad(ut), grad(vt)) * dx,
                              tensor=EigenMatrix())
                A_T = get_A_T_empty(V, problem=problem, full=False)

            print('generating matrices B, B0...')
            B0 = get_Bhat(dim=dim, pol_order=p, problem=problem)

            A = AG.sparray()
            if calculate == 1:
                print('saving the matrices...')
                savemat(filen_data,
                        dict(A=A, B0=B0, A_T=A_T),
                        do_compression=True)