def runTest(self):

        m = self.mesh
        e = ElementTriP1()
        basis = InteriorBasis(m, e)

        A = laplace.assemble(basis)
        M = mass.assemble(basis)
        D = m.boundary_nodes()

        assert_almost_equal(enforce(A, D=D).toarray(), np.eye(A.shape[0]))
        assert_almost_equal(enforce(M, D=D, diag=0.).toarray(),
                            np.zeros(M.shape))

        enforce(A, D=D, overwrite=True)
        assert_almost_equal(A.toarray(), np.eye(A.shape[0]))
Beispiel #2
0
def test_simple_cg_solver():

    m = MeshTri().refined(3)
    basis = CellBasis(m, ElementTriP1())

    A0 = laplace.coo_data(basis)
    A1 = laplace.assemble(basis)

    f = unit_load.assemble(basis)

    D = m.boundary_nodes()

    x1 = solve(*condense(A1, f, D=D))

    f[D] = 0

    x0 = A0.solve(f, D=D)

    assert_almost_equal(x0, x1)
Beispiel #3
0
def source(v, w):
    x = w.x - .5
    return v * np.exp(-1e3 * (x[0]**2 + x[1]**2))


# nonperiodic mesh
m = MeshTri.init_symmetric().refined(5)

# create a periodic mesh
Mp = MeshTri1DG.periodic(
    m,
    m.nodes_satisfying(lambda x: x[0] == 1),
    m.nodes_satisfying(lambda x: x[0] == 0),
)

peclet = 1e2

basis = Basis(Mp, ElementTriP2())
A = laplace.assemble(basis) + peclet * advection.assemble(basis)
f = source.assemble(basis)

D = basis.get_dofs()
x = solve(*condense(A, f, D=D))

if __name__ == '__main__':
    from os.path import splitext
    from sys import argv
    from skfem.visuals.matplotlib import plot, savefig
    plot(basis, x, shading='gouraud')
    savefig(splitext(argv[0])[0] + '_solution.png')
Beispiel #4
0
"""Wave equation."""
import numpy as np
from scipy.sparse import bmat, identity
from scipy.sparse.linalg import splu
from skfem import *
from skfem.models import laplace, mass

m = MeshLine().refined(6)
basis = Basis(m, ElementLineP1())

N = basis.N
L = laplace.assemble(basis)
M = mass.assemble(basis)
I = identity(N)
c = 1.

# reduction to first order system in time
A0 = bmat([[I, None], [None, M]], "csc")

B0 = bmat([[None, I], [-c**2 * L, None]], "csc")

# Crank-Nicolson
dt = .01
theta = .5
A = A0 + theta * B0 * dt
B = A0 - (1. - theta) * B0 * dt
backsolve = splu(A).solve


# timestepping
def evolve(t, u):