Пример #1
0
def test_srf_ev(PLOT=0):
    p = 2
    q = 1
    U = np.asarray([0,0,0, 1,1,1], dtype=float)
    V = np.asarray([0,0,     1,1], dtype=float)
    n = len(U)-1-(p+1)
    m = len(V)-1-(q+1)
    Pw = np.zeros((n+1,m+1,3))
    Pw[0,0,:] = [0.0, 0.5, 1.0]
    Pw[1,0,:] = [0.5, 0.5, 1.0]
    Pw[2,0,:] = [0.5, 0.0, 1.0]
    Pw[0,1,:] = [0.0, 1.0, 1.0]
    Pw[1,1,:] = [1.0, 1.0, 1.0]
    Pw[2,1,:] = [1.0, 0.0, 1.0]
    Pw[1,:,:] *= np.sqrt(2)/2

    u = np.linspace(U[0], U[-1], 31)
    v = np.linspace(V[0], V[-1], 10)

    Cw = bsp.Evaluate2(p,U,q,V,Pw,u,v)
    Dw = bsp.Evaluate2(p,U,q,V,Pw,U,V)

    P = Pw[:,:,:2] / Pw[:,:,2, None]
    C = Cw[:,:,:2] / Cw[:,:,2, None]
    D = Dw[:,:,:2] / Dw[:,:,2, None]

    if not PLOT: return
    plt.figure()
    plt.title("Surface - Evaluation")

    x1 = C[:,:,0]
    y1 = C[:,:,1]
    plt.plot(x1,y1,'.b')

    x2 = D[:,:,0]
    y2 = D[:,:,1]
    plt.plot(x2,y2,'og')

    x0 = P[:,:,0]
    y0 = P[:,:,1]
    plt.plot(x0,y0,'sr')

    t = np.linspace(0,np.pi/2,100)
    a = np.cos(t)
    b = np.sin(t)
    plt.plot(1.0*a,1.0*b,'-k')
    plt.plot(0.5*a,0.5*b,'-k')

    plt.axis("equal")
Пример #2
0
def test_srf_ki(PLOT=0):
    p = 2
    q = 1
    U = np.asarray([0,0,0, 1,1,1], dtype=float)
    V = np.asarray([0,0,     1,1], dtype=float)
    n = len(U)-1-(p+1)
    m = len(V)-1-(q+1)
    Pw = np.zeros((n+1,m+1,4))
    Pw[0,0,:] = [0.0, 0.5, 0.0, 1.0]
    Pw[1,0,:] = [0.5, 0.5, 0.0, 1.0]
    Pw[2,0,:] = [0.5, 0.0, 0.0, 1.0]
    Pw[0,1,:] = [0.0, 1.0, 0.0, 1.0]
    Pw[1,1,:] = [1.0, 1.0, 0.0, 1.0]
    Pw[2,1,:] = [1.0, 0.0, 0.0, 1.0]
    Pw[1,:,:] *= np.sqrt(2)/2
    nurbs = NURBS([U,V],Pw)

    X = np.asarray([])
    Y = np.asarray([])
    nrb = nurbs.copy().refine(0,X).refine(1,Y)
    (Ubar, Vbar), Qw = nrb.knots, nrb.control
    assert np.allclose(U,  Ubar)
    assert np.allclose(V,  Vbar)
    assert np.allclose(Pw, Qw)

    X = np.asarray([.25, 0.5])#;X = np.asarray([])
    Y = np.asarray([0.5, .75])#;Y = np.asarray([])
    nrb = nurbs.refine(0,X).refine(1,Y)
    (Ubar, Vbar), Qw = nrb.knots, nrb.control

    u = np.linspace(Ubar[0], Ubar[-1], 31)
    v = np.linspace(Vbar[0], Vbar[-1], 10)
    Cw = bsp.Evaluate2(p,Ubar,q,Vbar,Qw,u,v)

    Q = Qw[:,:,:2] / Qw[:,:,3,None]
    C = Cw[:,:,:2] / Cw[:,:,3,None]

    if not PLOT: return
    plt.figure()
    plt.title("Surface - Knot Insertion")

    x = Q[:,:,0]
    y = Q[:,:,1]
    plt.plot(x,y,'sr')

    x = C[:,:,0]
    y = C[:,:,1]
    plt.plot(x,y,'.b')

    t = np.linspace(0,np.pi/2,100)
    a = np.cos(t)
    b = np.sin(t)
    plt.plot(1.0*a,1.0*b,'-k')
    plt.plot(0.5*a,0.5*b,'-k')

    plt.axis("equal")
Пример #3
0
def test_srf_de(PLOT=0):
    p = 2
    q = 1
    U = np.asarray([0,0,0, 1,1,1], dtype=float)
    V = np.asarray([0,0,     1,1], dtype=float)
    n = len(U)-1-(p+1)
    m = len(V)-1-(q+1)
    Pw = np.zeros((n+1,m+1,4))
    Pw[0,0,:] = [0.0, 0.5, 0.0, 1.0]
    Pw[1,0,:] = [0.5, 0.5, 0.0, 1.0]
    Pw[2,0,:] = [0.5, 0.0, 0.0, 1.0]
    Pw[0,1,:] = [0.0, 1.0, 0.0, 1.0]
    Pw[1,1,:] = [1.0, 1.0, 0.0, 1.0]
    Pw[2,1,:] = [1.0, 0.0, 0.0, 1.0]
    Pw[1,:,:] *= np.sqrt(2)/2
    nurbs = NURBS([U,V],Pw)

    nrb = nurbs.copy().elevate(0,0).elevate(1,0)
    (Uh, Vh), Qw = nrb.knots, nrb.control
    nrb = nurbs.copy().elevate(0,1).elevate(1,0)
    (Uh, Vh), Qw = nrb.knots, nrb.control
    nrb = nurbs.copy().elevate(0,0).elevate(1,1)
    (Uh, Vh), Qw = nrb.knots, nrb.control

    r, s = 1, 2
    nrb = nurbs.copy().elevate(0,r).elevate(1,s)
    (Uh, Vh), Qw = nrb.knots, nrb.control
    ph = p+r; qh = q+s;
    u = np.linspace(Uh[0], Uh[-1], 31)
    v = np.linspace(Vh[0], Vh[-1], 10)
    Cw = bsp.Evaluate2(ph,Uh,qh,Vh,Qw,u,v)

    Q = Qw[:,:,:2] / Qw[:,:,3, None]
    C = Cw[:,:,:2] / Cw[:,:,3, None]

    if not PLOT: return
    plt.figure()
    plt.title("Surface - Degree Elevation")

    x = Q[:,:,0]
    y = Q[:,:,1]
    plt.plot(x,y,'sr')

    x = C[:,:,0]
    y = C[:,:,1]
    plt.plot(x,y,'.b')

    t = np.linspace(0,np.pi/2,100)
    a = np.cos(t)
    b = np.sin(t)
    plt.plot(1.0*a,1.0*b,'-k')
    plt.plot(0.5*a,0.5*b,'-k')

    plt.axis("equal")
Пример #4
0
def test_fem_2d(VERB=0, PLOT=0):
    """
     -Laplacian(u(x,y)) = f(x,y)  in  0 < x,y < 1
                u(x,y)  = 0       on  x,y = 0,1
    """
    def forcing(x, y):
        return x**2 + y**2

    # geometry, order & continuity
    nelx = 5
    px = 2
    Cx = px - 1
    nely = 5
    py = 2
    Cy = py - 1
    Ux = iga.KnotVector(nelx, px, Cx)
    Uy = iga.KnotVector(nely, py, Cy)
    nx = len(Ux) - (px + 1)
    ny = len(Uy) - (py + 1)

    # quadrature and basis data
    nqpx = 3
    nqpy = 3
    Ox, Jx, Wx, X, Nx = iga.BasisData(px, Ux, d=1, q=nqpx)
    Oy, Jy, Wy, Y, Ny = iga.BasisData(py, Uy, d=1, q=nqpy)

    # global matrix and vector
    K = np.zeros((nx, ny, nx, ny))
    F = np.zeros((nx, ny))

    # element loop
    for ex in range(nelx):
        for ey in range(nely):
            Ke = np.zeros((px + 1, py + 1, px + 1, py + 1))
            Fe = np.zeros((px + 1, py + 1))
            # quadrature loop
            for qx in range(nqpx):
                for qy in range(nqpy):
                    # basis functions
                    N0 = np.zeros((px + 1, py + 1))
                    N1 = np.zeros((px + 1, py + 1, 2))
                    for ax in range(px + 1):
                        for ay in range(py + 1):
                            N0[ax, ay] = Nx[ex, qx, ax, 0] * Ny[ey, qy, ay, 0]
                            N1[ax, ay,
                               0] = Nx[ex, qx, ax, 1] * Ny[ey, qy, ay, 0]
                            N1[ax, ay,
                               1] = Nx[ex, qx, ax, 0] * Ny[ey, qy, ay, 1]
                    # stiffness matrix
                    Kab = np.zeros((px + 1, py + 1, px + 1, py + 1))
                    for ax in range(px + 1):
                        for ay in range(py + 1):
                            Na_x, Na_y = N1[ax, ay]
                            for bx in range(px + 1):
                                for by in range(py + 1):
                                    Nb_x, Nb_y = N1[bx, by]
                                    Kab[ax, ay, bx,
                                        by] = Na_x * Nb_x + Na_y * Nb_y
                    # force vector
                    x, y = X[ex, qx], Y[ey, qy]
                    Fa = np.zeros((px + 1, py + 1))
                    for ax in range(px + 1):
                        for ay in range(py + 1):
                            Na = N0[ax, ay]
                            Fa[ax, ay] = Na * forcing(x, y)
                    #
                    J = Jx[ex] * Jy[ey]
                    W = Wx[qx] * Wy[qy]
                    Ke += Kab * J * W
                    Fe += Fa * J * W
            # global matrix and vector assembly
            ox = Ox[ex]
            oy = Oy[ey]
            for ax in range(px + 1):
                for ay in range(py + 1):
                    Ax = ox + ax
                    Ay = oy + ay
                    for bx in range(px + 1):
                        for by in range(py + 1):
                            Bx = ox + bx
                            By = oy + by
                            K[Ax, Ay, Bx, By] += Ke[ax, ay, bx, by]
                    F[Ax, Ay] += Fe[ax, ay]

    # boundary conditions (homogeneous)
    for Ax in (0, -1):
        for Ay in range(ny):
            K[Ax, Ay, :, :] = 0
            K[:, :, Ax, Ay] = 0
            K[Ax, Ay, Ax, Ay] = 1
            F[Ax, Ay] = 0
    for Ay in (0, -1):
        for Ax in range(nx):
            K[Ax, Ay, :, :] = 0
            K[:, :, Ax, Ay] = 0
            K[Ax, Ay, Ax, Ay] = 1
            F[Ax, Ay] = 0

    # solve linear system
    K.shape = (nx * ny, nx * ny)
    F.shape = (nx * ny, )
    X = np.linalg.solve(K, F)
    X.shape = (nx, ny)

    # interpolate solution
    x = np.linspace(Ux[0], Ux[-1], 25)
    y = np.linspace(Uy[0], Uy[-1], 25)
    z = bsp.Evaluate2(px, Ux, py, Uy, X, x, y)
    x, y = np.meshgrid(x, y)
    z.shape = z.shape[:-1]

    # surface plot solution
    if not PLOT: return
    from matplotlib import cm
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    plt.title("Poisson 2D (FEM)")
    plt.xlabel('X')
    plt.ylabel('Y')
    ax.plot_surface(x,
                    y,
                    z,
                    rstride=1,
                    cstride=1,
                    cmap=cm.jet,
                    antialiased=True)
Пример #5
0
def test_col_2d(VERB=0, PLOT=0):
    """
     -Laplacian(u(x,y)) = f(x,y)  in  0 < x,y < 1
                u(x,y)  = 0       on  x,y = 0,1
    """
    def forcing(x, y):
        #eturn 1
        return x**2 + y**2

    # geometry, order & continuity
    nelx = 5
    px = 2
    Cx = px - 1
    nely = 5
    py = 2
    Cy = py - 1
    Ux = iga.KnotVector(
        nelx,
        px,
        Cx,
    )
    Uy = iga.KnotVector(
        nely,
        py,
        Cy,
    )
    nx = len(Ux) - (px + 1)
    ny = len(Uy) - (py + 1)

    # basis functions
    X = iga.Greville(px, Ux)
    Y = iga.Greville(px, Uy)
    Ox, Nx = iga.BasisDataCol(px, Ux, X, d=2)
    Oy, Ny = iga.BasisDataCol(py, Uy, Y, d=2)

    # global matrix and vector
    K = np.zeros((nx, ny, nx, ny))
    F = np.zeros((nx, ny))

    # point loop
    for Ax in range(nx):
        for Ay in range(ny):
            ox = Ox[Ax]
            oy = Oy[Ay]
            for bx in range(px + 1):
                for by in range(py + 1):
                    N_xx = Nx[Ax, bx, 2] * Ny[Ay, by, 0]
                    N_yy = Nx[Ax, bx, 0] * Ny[Ay, by, 2]

                    Bx = ox + bx
                    By = oy + by
                    K[Ax, Ay, Bx, By] += -(N_xx + N_yy)

            x = X[Ax]
            y = Y[Ay]
            f = forcing(x, y)
            F[Ax, Ay] = f

    # boundary conditions (homogeneous)
    for Ax in (0, -1):
        for Ay in range(ny):
            K[Ax, Ay, :, :] = 0
            K[:, :, Ax, Ay] = 0
            K[Ax, Ay, Ax, Ay] = 1
            F[Ax, Ay] = 0
    for Ay in (0, -1):
        for Ax in range(nx):
            K[Ax, Ay, :, :] = 0
            K[:, :, Ax, Ay] = 0
            K[Ax, Ay, Ax, Ay] = 1
            F[Ax, Ay] = 0

    # solve linear system
    K.shape = (nx * ny, nx * ny)
    F.shape = (nx * ny, )
    X = np.linalg.solve(K, F)
    X.shape = (nx, ny)

    # interpolate solution
    x = np.linspace(Ux[0], Ux[-1], 25)
    y = np.linspace(Uy[0], Uy[-1], 25)
    z = bsp.Evaluate2(px, Ux, py, Uy, X, x, y)
    x, y = np.meshgrid(x, y)
    z.shape = z.shape[:-1]

    # surface plot solution
    if not PLOT: return
    from matplotlib import cm
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    plt.title("Poisson 2D (Collocation)")
    plt.xlabel('X')
    plt.ylabel('Y')
    ax.plot_surface(x,
                    y,
                    z,
                    rstride=1,
                    cstride=1,
                    cmap=cm.jet,
                    antialiased=True)