Exemplo n.º 1
0
def visualize():
    from skfem.visuals.matplotlib import plot, draw
    ax = plot(basis[0], y, Nrefs=4, colorbar=True)
    draw(basis[0], ax=ax)
    plot(basis[1], y, ax=ax, Nrefs=4)
    draw(basis[1], ax=ax)
    return ax
Exemplo n.º 2
0
 def runTest(self):
     m = self.mesh_type()
     draw(m,
          aspect=1.1,
          facet_numbering=True,
          node_numbering=True,
          element_numbering=True)
Exemplo n.º 3
0
def visualize():
    from skfem.visuals.matplotlib import plot, draw
    ax = plot(ib_dg,
              vonmises1,
              shading='gouraud',
              colorbar='$\sigma_{\mathrm{mises}}$')
    draw(mdefo, ax=ax)
    plot(Ib_dg, vonmises2, ax=ax, Nrefs=3, shading='gouraud')
    draw(Mdefo, ax=ax)
    return ax
Exemplo n.º 4
0
def triangulate(
    corner_points,
    max_refloops=20,
    theta=0.8,
    smooth_steps=50,
    quality=0.9,
    verbose=False,
    **params,
):
    from .criterion import avg_quality
    from .initial import cdt
    from .mark import adaptive_theta
    from .refine import rgb
    from .smooth import cpt
    from .solve import laplace

    m = process(
        initial=cdt,
        solve=laplace,
        mark=adaptive_theta,
        refine=rgb,
        smooth=cpt,
        criterion=avg_quality,
        corner_points=corner_points,
        max_refloops=max_refloops,
        theta=theta,
        smooth_steps=smooth_steps,
        quality=quality,
        verbose=verbose,
        **params,
    )
    m.draw = types.MethodType(lambda self, **kwargs: draw(self, **kwargs), m)
    m.show = types.MethodType(lambda self, **kwargs: show(self, **kwargs), m)

    return m
Exemplo n.º 5
0
 def draw(self, *args, **kwargs):
     """Convenience wrapper for :func:`skfem.visuals.matplotlib.draw`."""
     from skfem.visuals.matplotlib import draw, show
     from skfem.assembly import CellBasis
     ax = draw(CellBasis(self, self.elem()), *args, **kwargs)
     ax.show = show
     return ax
Exemplo n.º 6
0
def visualize():
    from skfem.visuals.matplotlib import plot, draw
    M = MeshQuad(np.array(m.p + .5 * y[gb.nodal_dofs]), m.t)
    ax = draw(M)
    return plot(M,
                sigma[sgb.nodal_dofs[0]],
                ax=ax,
                colorbar='$\sigma_{xx}$',
                shading='gouraud')
Exemplo n.º 7
0
def visualize():
    import matplotlib.pyplot as plt
    from skfem.visuals.matplotlib import plot, draw
    f, axes = plt.subplots(3, 3)

    ixs = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0)]
    i = 0

    for itr in ib.nodal_dofs[:, 4]:
        axi = axes[ixs[i]]
        axi.set_axis_off()
        X = ib.zeros()
        X[itr] = 1.0
        plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)
        axi.set_aspect('equal')
        i += 1

    axi = axes[(1, 1)]
    axi.set_axis_off()
    draw(m, ax=axi)
    axi.set_aspect('equal')

    axi = axes[(2, 1)]
    axi.set_axis_off()
    X = ib.zeros()
    X[np.array([56, 59, 64, 66])] = 1.0
    plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)
    axi.set_aspect('equal')

    axi = axes[(2, 2)]
    axi.set_axis_off()
    X = ib.zeros()
    X[np.array([58, 61, 63, 65])] = 1.0
    plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)
    axi.set_aspect('equal')

    return axi
Exemplo n.º 8
0
def process(initial=None,
            solve=None,
            mark=None,
            refine=None,
            smooth=None,
            criterion=None,
            max_refloops=6,
            verbose=False,
            **params):

    if initial is not None:
        if callable(initial):
            mesh = initial(**params)
        else:
            mesh = initial
    else:
        raise Exception("The initial mesh not given.")

    if verbose:
        draw(mesh)

    for itr in range(max_refloops):
        estimators = solve(mesh, **params)
        elements = mark(mesh, estimators, **params)
        mesh = refine(mesh, elements, **params)
        if verbose:
            draw(mesh)
        if smooth is not None:
            mesh = smooth(mesh, **params)
            if verbose:
                draw(mesh)
        if criterion is not None:
            if criterion(mesh, **params):
                break

    if itr == max_refloops - 1:
        warnings.warn("Criterion not satisfied in {} refinement loops.".format(
            max_refloops))

    return mesh
Exemplo n.º 9
0
def visualize():
    from skfem.visuals.matplotlib import plot, draw
    ax = draw(M, boundaries_only=True)
    return plot(M, X, shading="gouraud", ax=ax, colorbar=True)
Exemplo n.º 10
0
        dw1 = grad(w['u1'])
        dw2 = grad(w['u2'])
        return h * ((dw1[0] - dw2[0]) * n[0] + (dw1[1] - dw2[1]) * n[1])**2

    eta_E = edge_jump.elemental(fbasis[0], **w)

    tmp = np.zeros(m.facets.shape[1])
    np.add.at(tmp, fbasis[0].find, eta_E)
    eta_E = np.sum(.5 * tmp[m.t2f], axis=0)

    return eta_K + eta_E


if __name__ == "__main__":
    from skfem.visuals.matplotlib import draw, plot, show
    draw(m)

for itr in reversed(range(9)):

    basis = Basis(m, e)

    K = asm(laplace, basis)
    M = asm(mass, basis)
    #f = asm(load, basis)

    I = m.interior_nodes()
    Lh, eigen_uh = solve(*condense(K, M, I=I),
                         solver=solver_eigen_scipy_sym(sigma=20, k=1))
    eigv = Lh[0]
    print(eigv)
    eigen_uh = eigen_uh.flatten()
Exemplo n.º 11
0
rotf = asm(unit_load, ib)

psi = solve(*condense(stokes, rotf, D=ib.find_dofs()))
psi0, = ib.interpolator(psi)(np.zeros((2, 1)))

if __name__ == "__main__":
    from os.path import splitext
    from sys import argv
    from skfem.visuals.matplotlib import draw
    from matplotlib.tri import Triangulation

    print('psi0 = {} (cf. exact = 1/64 = {})'.format(psi0, 1 / 64))

    M, Psi = ib.refinterp(psi, 3)

    ax = draw(mesh)
    ax.tricontour(Triangulation(*M.p, M.t.T), Psi)
    name = splitext(argv[0])[0]
    ax.get_figure().savefig(f'{name}_stream-lines.png')

    refbasis = InteriorBasis(M, ElementTriP1())
    velocity = np.vstack([
        derivative(Psi, refbasis, refbasis, 1),
        -derivative(Psi, refbasis, refbasis, 0)
    ])
    ax = draw(mesh)
    sparsity_factor = 2**3  # subsample the arrows
    vector_factor = 2**3  # lengthen the arrows
    x = M.p[:, ::sparsity_factor]
    u = vector_factor * velocity[:, ::sparsity_factor]
    ax.quiver(*x, *u, x[0])
Exemplo n.º 12
0
from pathlib import Path

m = MeshTet.load(Path(__file__).parent / 'meshes' / 'beams.msh')
e1 = ElementTetP2()
e = ElementVectorH1(e1)

ib = Basis(m, e)

K = asm(linear_elasticity(*lame_parameters(200.0e9, 0.3)), ib)

rho = 8050.0


@BilinearForm
def mass(u, v, w):
    from skfem.helpers import dot
    return dot(rho * u, v)

M = asm(mass, ib)

L, x = solve(
    *condense(K, M, D=ib.find_dofs()["fixed"]), solver=solver_eigen_scipy_sym()
)

if __name__ == "__main__":
    from skfem.visuals.matplotlib import draw, show
    sf = 2.0
    draw(MeshTet(np.array(m.p + sf * x[ib.nodal_dofs, 0]), m.t))
    show()
Exemplo n.º 13
0
    f, axes = plt.subplots(3, 3)

    ixs = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0)]
    i = 0

    for itr in ib.nodal_dofs[:, 4]:
        axi = axes[ixs[i]]
        axi.set_axis_off()
        X = np.zeros(ib.N)
        X[itr] = 1.0
        plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)
        i += 1

    axi = axes[(1, 1)]
    axi.set_axis_off()
    draw(m, ax=axi)

    axi = axes[(2, 1)]
    axi.set_axis_off()
    X = np.zeros(ib.N)
    X[np.array([56, 59, 64, 66])] = 1.0
    plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)

    axi = axes[(2, 2)]
    axi.set_axis_off()
    X = np.zeros(ib.N)
    X[np.array([58, 61, 63, 65])] = 1.0
    plot(ib, X, Nrefs=5, shading='gouraud', ax=axi)

    plt.axis('off')
    plt.show()
Exemplo n.º 14
0
M = asm(mass, gb)

D = gb.find_dofs({'left': m.facets_satisfying(lambda x: x[0] == 0.)})
y = gb.zeros()

I = gb.complement_dofs(D)

L, x = solve(*condense(K, M, I=I),
             solver=solver_eigen_scipy_sym(k=6, sigma=0.0))

y = x[:, 4]

# calculate stress
sgb = gb.with_element(ElementVector(e))
C = linear_stress(lam, mu)
yi = gb.interpolate(y)

@LinearForm
def proj(v, _):
    return ddot(C(sym_grad(yi)), v)

sigma = projection(proj, sgb, gb)

if __name__ == "__main__":
    from skfem.visuals.matplotlib import plot, draw, show
    M = MeshQuad(np.array(m.p + y[gb.nodal_dofs]), m.t)
    ax = draw(M)
    plot(M, sigma[sgb.nodal_dofs[0]], ax=ax, colorbar=True)
    show()
Exemplo n.º 15
0
 def runTest(self):
     m = self.mesh_type()
     draw(m)
Exemplo n.º 16
0
mapping = MappingIsoparametric(m, e1)

e = ElementVectorH1(e1)

gb = InteriorBasis(m, e, mapping, 2)

K = asm(linear_elasticity(1.0, 1.0), gb)


@BilinearForm
def mass(u, v, w):
    return dot(u, v)


M = asm(mass, gb)

D = gb.get_dofs(lambda x: x[0] == 0.0).all()
y = np.zeros(gb.N)

I = gb.complement_dofs(D)

L, x = eigsh(K[I].T[I].T, k=6, M=M[I].T[I].T, which='SM')

y[I] = x[:, 4]

if __name__ == "__main__":
    from skfem.visuals.matplotlib import draw, show
    M = MeshQuad(np.array(m.p + y[gb.nodal_dofs]), m.t)
    draw(M)
    show()
Exemplo n.º 17
0
 def _repr_svg_(self) -> str:
     from skfem.visuals.svg import draw
     return draw(self, nrefs=2, boundaries_only=True).svg
Exemplo n.º 18
0
def visualize():
    from skfem.visuals.matplotlib import draw, plot
    ax = draw(m)
    return plot(m, u, ax=ax, shading='gouraud', colorbar=True)
Exemplo n.º 19
0
elements that have nodal degrees-of-freedom.  Support for higher-order elements
on mixed meshes is work-in-progress.

"""
from skfem import *
from skfem.models import laplace, unit_load

fname = 'docs/examples/meshes/mixedtriquad.msh'
out = ['cell_sets_dict']  # read boundary nodes from meshio
m = [
    Mesh.load(fname, force_meshio_type='triangle'),
    Mesh.load(fname, force_meshio_type='quad', out=out),
]
e = [ElementTriP1(), ElementQuad1()]
basis = list(map(Basis, m, e))

A = asm(laplace, basis)
f = asm(unit_load, basis)

y = solve(*condense(A, f, D=out[0]['boundary']['line']))

if __name__ == '__main__':
    from os.path import splitext
    from sys import argv
    from skfem.visuals.matplotlib import plot, draw, savefig
    ax = plot(basis[0], y, Nrefs=4)
    draw(basis[0], ax=ax)
    plot(basis[1], y, ax=ax, Nrefs=4)
    draw(basis[1], ax=ax)
    savefig(splitext(argv[0])[0] + '_solution.png')