예제 #1
0
def local_mass_matrix_tri(tri,xu_l,yu_l,xs_l,ys_l):
    # tri = numpy array shape (3,2)
    # giving the triangle coords
    tri = np.hstack([tri,np.ones((3,1))]).transpose()
    #area = np.linalg.det(tri)/2
    tri = np.dot(tri,bari_coords)
    eval_p = np.dot(np.matlib.eye(2,3),tri).transpose()
    (dx,dy,phi_u,omega) = basis.tri_p1(xu_l,yu_l,eval_p)
    (dx,dy,psi_s,omega) = basis.tri_p1(xs_l,ys_l,eval_p)
    lm = np.zeros((3,3))
    for i in np.arange(0,3):
        col = np.reshape(phi_u[i,:],(3,1))
        row = np.reshape(psi_s[i,:],(1,3))
        lm += omega/3*np.dot(col,row)
    return lm
예제 #2
0
def calc_gradu_gradv_p1_ieq_partly(topo,x,y,ieq):

    ndofs = max(ieq)+1

    (rows,cols)= la_utils.get_sparsity_pattern_ieq(topo,ieq)

    values = np.zeros(rows.shape)

    for row in topo:
        x_l = x[row]
        y_l = y[row]
        eval_points = np.zeros((0,2))
        (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_points)
        dx_j = phi_dx
        dx_i = phi_dx.transpose()
        dy_j = phi_dy
        dy_i = phi_dy.transpose()
        entries = ieq[row]
        #print x_l
        #print y_l
        #print row
        #print entries
        local_matrix = omega*(np.dot(dx_i,dx_j)+np.dot(dy_i,dy_j))
        values = la_utils.add_local_to_global_coo(rows,cols,values,
                            entries,entries,local_matrix)

    A = sparse.coo_matrix((values,(rows,cols)),shape=(ndofs,ndofs))
    A.tocsr()

    return A
예제 #3
0
def calc_gradu_gradv_p1_partly(topo,x,y):
    """
    Assembling the Laplace operator. The function name resambles the
    operator gradtient of the trial functionctions, multiplied the gradient of
    the test functions. Assuming :math:`P_1` elements on the :math:`K` trinagle we have:

    .. math::
       \int_K \mathrm{grad}(u)_j \cdot \mathrm{grad}(v)_i = \mathrm{Area}(K)\ \mathrm{grad}(u_j) \cdot \mathrm{grad}(v_i)

    In the code snippet, we can see that, by default, derivatives are represented a 1x3 row.
    The ``dx_i`` and ``dy_i`` components are transpose. So we only need the matrix product ``np.dot``
    to have the local stffness matrix. The values for thederivatives are obtained
    form the ``tri_p1`` function in the `basis_func` module, `check its documentation`_.

    .. _check its documentation: ./basis_func.html

    .. code:: python

        dx_j = phi_dx
        dx_i = phi_dx.transpose()
        dy_j = phi_dy
        dy_i = phi_dy.transpose()
        local_matrix = omega*(np.dot(dx_i,dx_j)+np.dot(dy_i,dy_j))


    Input:

    ``x``, ``y``, ``topo`` : the nodes coordinates and the connectivity.

    Output:

    ``A`` : The spasre matrix A representing the discretized operator.\n

    """
    ndofs = max(x.shape)

    (rows,cols)= la_utils.get_sparsity_pattern(topo)

    values = np.zeros(rows.shape)

    for row in topo:
        x_l = x[row]
        y_l = y[row]
        eval_points = np.zeros((0,2))

        (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_points)
        dx_j = phi_dx
        dx_i = phi_dx.transpose()
        dy_j = phi_dy
        dy_i = phi_dy.transpose()
        local_matrix = omega*(np.dot(dx_i,dx_j)+np.dot(dy_i,dy_j))
        values = la_utils.add_local_to_global_coo(rows,cols,values,
                            row,row,local_matrix)

    A = sparse.coo_matrix((values,(rows,cols)),shape=(ndofs,ndofs))
    #plt.spy(A)
    #plt.show()
    A.tocsr()

    return A
예제 #4
0
def u_v_p1_inv_diag(topo,x,y):

    ndofs = max(x.shape)

    A = sparse.csr_matrix((ndofs,ndofs))

    for row in topo:
        x_l = x[row]
        y_l = y[row]
        #local_matrix = local_p1_p1_mass_tri(x_l,y_l)
        eval_points = np.zeros((0,2))
        (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_points)
        local_matrix = omega/3. * np.eye(3)
        [r,c] = np.meshgrid(row,row)
        r = np.concatenate(r)
        c = np.concatenate(c)
        vals = np.concatenate(local_matrix)
        tmp = sparse.coo_matrix((vals, (r,c)), shape=(ndofs,ndofs))
        A = A+tmp

    vals = A.data
    vals = np.power(vals,-1)


    (rows,cols) =  A.nonzero()
    A = sparse.coo_matrix((vals, (rows,cols)), shape=(ndofs,ndofs))
    return A
예제 #5
0
def divu_p_p1_iso_p2_p1(topo_p,x_p,y_p,
           topo_u,x_u,y_u,c2f):

    ndofs_u = max(x_u.shape)
    ndofs_p = max(x_p.shape)

    B1 = sparse.csr_matrix((ndofs_u,ndofs_p))
    B2 = sparse.csr_matrix((ndofs_u,ndofs_p))

    el_id = 0
    for nd_p in topo_p:
        #print("====================")
        #print nd_p
        fine_els = c2f[el_id,:]
        x_lp = x_p[nd_p]
        y_lp = y_p[nd_p]
        local_matrix = np.zeros((3,3))
        for el in fine_els:
            nd_u = topo_u[el,:]
            x_lu = x_u[nd_u]
            y_lu = y_u[nd_u]
            eval_p = np.zeros( (3,2) )
            eval_p[:,0] = x_lu.transpose()
            eval_p[:,1] = y_lu.transpose()
            (u_dx,u_dy,u,omega_u) = basis.tri_p1(x_lu,y_lu,eval_p)
            (p_dx,p_dy,p,omega_p) = basis.tri_p1(x_lp,y_lp,eval_p)
            #print("--------------------")
            #print nd_u
            #print u_dx
            int_q_omega = np.zeros((1,3))
            #print sum(p[:,0])
            for k in range(0,3):
                int_q_omega[0,k] += omega_u/3 * sum(p[:,k])
            #print 12*int_q_omega
            local_matrix = np.dot(u_dx.transpose(),
                                  int_q_omega)
            #print local_matrix
            B1 = la_utils.add_local_to_global(B1,local_matrix,nd_u,nd_p)
            local_matrix = np.dot(u_dy.transpose(),
                                  int_q_omega)
            B2 = la_utils.add_local_to_global(B2,local_matrix,nd_u,nd_p)
            #print B1.todense()
        el_id += 1

    return B1, B2
예제 #6
0
def local_fluid_str_coupling(tri,xu_l,yu_l,s_l,t_l,tri_map):
    # tri = numpy array shape (3,2)
    # giving the triangle coords
    tri = np.hstack([tri,np.ones((3,1))]).transpose()
    #print '***'
    tri_ref = np.dot(tri_map,tri)
    area = np.linalg.det(tri_ref)/2
    #print tri_ref
    tri = np.dot(tri,bari_coords)
    eval_p = np.dot(np.eye(2,3),tri).transpose()
    (dx,dy,phi_u,omega) = basis.tri_p1(xu_l,yu_l,eval_p)
    eval_p = np.hstack([eval_p,np.ones((3,1))]).transpose()
    eval_p = np.dot(tri_map,eval_p)
    eval_p = eval_p[0:2,:].transpose()
    #print eval_p.shape
    (dx,dy,psi_s,omega) = basis.tri_p1(s_l,t_l,eval_p)
    lm = np.zeros((3,3))
    for i in np.arange(0,3):
        col = np.reshape(phi_u[i,:],(3,1))
        row = np.reshape(psi_s[i,:],(1,3))
        lm += area/3*np.dot(col,row)
    return lm
예제 #7
0
def local_p1_p1_mass_tri(x_l,y_l):
    eval_p = np.vstack([
        np.reshape(x_l,(1,3)),
        np.reshape(y_l,(1,3)),
        np.ones((1,3))])
    eval_p = np.dot(eval_p,bari_coords)
    eval_p = eval_p[0:2,:].transpose()
    (dx,dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_p)
    lm = np.zeros((3,3))
    for i in np.arange(0,3):
        col = np.reshape(phi[i,:],(3,1))
        row = np.reshape(phi[i,:],(1,3))
        lm += omega/3*np.dot(col,row)
    return lm
예제 #8
0
def diagonal_mass_matrix(topo_s,s_lgr,t_lgr):
    ndofs = s_lgr.shape[0]
    entries = np.zeros((ndofs))

    for row in topo_s:
        x_l = s_lgr[row]
        y_l = t_lgr[row]
        eval_points = np.zeros((0,2))
        (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_points)
        entries[row] += omega/3.
        #print omega

    rows = np.arange(0,s_lgr.shape[0])

    M = sparse.coo_matrix((entries, (rows,rows)), shape=(ndofs,ndofs))
    return M
예제 #9
0
def calc_u_gradv_w_p1_partly(topo, x, y, u_x, u_y):
    ndofs = max(x.shape)

    u_x = np.reshape(u_x, (ndofs, 1))
    u_y = np.reshape(u_y, (ndofs, 1))

    A11 = sparse.csr_matrix((ndofs,ndofs))

    for row in topo:
        x_l = x[row]
        y_l = y[row]
        local_matrix = np.zeros((3,3))
        local_mass_matrix = local_p1_p1_mass_tri(x_l,y_l)

        eval_points = np.zeros( (3,2) )
        eval_points[:,0] = x_l.transpose()
        eval_points[:,1] = y_l.transpose()

        (v_dx,v_dy,v_l,omega_v) = basis.tri_p1(x_l,y_l,eval_points)

        # local_matrix = np.reshape(np.dot(u_x[row].transpose(), local_mass_matrix), (1,3))
        # local_matrix = np.dot(v_dx.transpose(), local_matrix)
        # A11 = la_utils.add_local_to_global(A11,local_matrix,row,row)
        #
        # local_matrix = np.reshape(np.dot(u_y[row].transpose(), local_mass_matrix), (1,3))
        # local_matrix = np.dot(v_dy.transpose(), local_matrix)
        # A11 = la_utils.add_local_to_global(A11,local_matrix,row,row)

        local_matrix = np.dot(local_mass_matrix, u_x[row])
        local_matrix = np.dot(local_matrix, v_dx)
        A11 = la_utils.add_local_to_global(A11,local_matrix,row,row)

        local_matrix = np.dot(local_mass_matrix, u_y[row])
        local_matrix = np.dot(local_matrix, v_dy)
        A11 = la_utils.add_local_to_global(A11,local_matrix,row,row)

        # local_matrix = np.reshape(np.dot(u_x[row].transpose(), local_mass_matrix), (1,3))
        # local_matrix = np.dot(v_dx.transpose(), local_matrix)
        # A22 = la_utils.add_local_to_global(A22,local_matrix,row,row)
        #
        # local_matrix = np.reshape(np.dot(u_y[row].transpose(), local_mass_matrix), (1,3))
        # local_matrix = np.dot(v_dy.transpose(), local_matrix)
        # A22 = la_utils.add_local_to_global(A22,local_matrix,row,row)

    return A11
예제 #10
0
def ibm_force(XY_str,s_lgr,topo_u,x_u,y_u,point_in_tri):
    node_s = XY_str.shape[0]
    force_x = np.zeros((x_u.shape[0],1))
    force_y = np.zeros((x_u.shape[0],1))

    Xs = XY_str[:,0]
    Ys = XY_str[:,1]
    for cnt in range(0,node_s):
        if cnt == 0:
            backward = node_s-1
            middle = cnt
            forward = cnt+1
            ds = s_lgr[forward]-s_lgr[middle]
        if cnt == node_s-1:
            backward = cnt-1
            middle = cnt
            forward = 0
            ds = s_lgr[middle]-s_lgr[backward]
        else:
            backward = cnt-1
            middle = cnt
            forward = cnt+1
            ds = s_lgr[forward]-s_lgr[middle]
        Ds_X1 = (Xs[forward] - Xs[middle])/ds
        Ds_X0 = (Xs[middle] - Xs[backward])/ds
        Ds_Y1 = (Ys[forward] - Ys[middle])/ds
        Ds_Y0 = (Ys[middle] - Ys[backward])/ds
        stiff_x = Ds_X1-Ds_X0
        stiff_y = Ds_Y1-Ds_Y0
        nds = topo_u[point_in_tri[cnt]]
        x_l = x_u[nds]
        y_l = y_u[nds]
        eval_p = np.zeros((1,2))
        eval_p[0,:] = XY_str[cnt,:]
        (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_l,y_l,eval_p)
        force_x[nds] += stiff_x * phi.transpose()
        force_y[nds] += stiff_y * phi.transpose()
    return force_x, force_y
예제 #11
0
def u_s_p1(topo_u,x_u,y_u,
          topo_s,x_s,y_s,s_lgr,ieq_s,
          str_segments,fluid_id):

   r = max(ieq_s)+1
   #print r

   GT = sparse.csr_matrix((x_u.shape[0],r[0]))

   str_iel = 0
   for str_el in str_segments:
       s_dofs = ieq_s[topo_s[str_iel,:]]
       s_l = s_lgr[topo_s[str_iel,:]]
       el_list = fluid_id[str_iel]
       #print '================='
       #print s_l
       #print el_list
       iel = 0
       for segment in str_el:
           f_id = el_list[iel]
           u_dofs = topo_u[f_id,:]
           l = list(segment.coords)
           sp = geom.get_reference_coords(topo_s,x_s,y_s,s_lgr,str_iel,l)
           (ds_psi,psi,omega) = basis.lin_p1(s_l,sp)
           x_ul = x_u[topo_u[f_id,:]]
           y_ul = y_u[topo_u[f_id,:]]
           p0 = Point(l[0])
           p1 = Point(l[1])
           eval_p = np.zeros((2,2))
           eval_p[0,0] = p0.x
           eval_p[0,1] = p0.y
           eval_p[1,0] = p1.x
           eval_p[1,1] = p1.y
           (phi_dx,phi_dy,phi,omega) = basis.tri_p1(x_ul,y_ul,eval_p)
           #print '-----------------'
           #print f_id
           #print s_dofs
           #print u_dofs
           #print sp
           #print psi
           for i in range(0,2):#loop over quadrature points
               cln = np.zeros((3,1))
               row = np.zeros((1,2))
               cln[:,0] = phi[i,:]
               row[0,:] = psi[i,:]
               local_matrix = .5 * segment.length * np.dot(cln,row)
               GT = la_utils.add_local_to_global(GT,local_matrix,u_dofs,s_dofs)
               #print '***'
               #print row
               #print cln
               #print local_matrix*8
               #print '***'
           #
           #print segment.length
           #print x_ul
           #print f_id
           #print psi
           #print phi
           iel += 1
           #
       str_iel+=1
       #break

   return GT
예제 #12
0
A11 = assemble.gradu_gradv_p1(topo_u, x_u, y_u)
M22 = M11

(BT1, BT2) = assemble.divu_p_p1_iso_p2_p1p0(topo_p, x_p, y_p, topo_u, x_u, y_u,
                                            c2f)
# (BT1,BT2) = assemble.divu_p_p1_iso_p2_p1(topo_p,x_p,y_p,
#            topo_u,x_u,y_u,c2f)
BT = sparse.vstack([BT1, BT2])
B = BT.transpose()

mean_p = np.zeros((1, ndofs_p))
for row in topo_p:
    x_l = x_p[row[0:3]]
    y_l = y_p[row[0:3]]
    eval_p = np.zeros((0, 2))
    (phi_dx, phi_dy, phi, omega) = shp.tri_p1(x_l, y_l, eval_p)
    mean_p[0, row] += omega * np.array([1. / 3., 1. / 3., 1. / 3., 1])
    # mean_p[0,row] += omega * np.array([1./3.,1./3.,1./3.])

mean_pT = mean_p.transpose()

#upper boundary
bc_id = np.where(y_u > y_top - delta_x / 10)
BT1 = la_utils.clear_rows(BT1, bc_id)
BT2 = la_utils.clear_rows(BT2, bc_id)

#lower boundary
bc_id = np.where(y_u < y_bottom + delta_x / 10)
BT1 = la_utils.clear_rows(BT1, bc_id)
BT2 = la_utils.clear_rows(BT2, bc_id)
예제 #13
0
def err_l2(topo, x, y, sol):
    aq = np.array([
        1. / 90., 1. / 90., 1. / 90., 16. / 225., 16. / 225., 16. / 225.,
        (49. / 120.)**2, (49. / 120.)**2, (49. / 120.)**2, 81. / 320.
    ])  # pesi quadratura
    xq = np.array(
        [0., 1., 0., 0.5, 0.5, 0., 1. / 7., 5. / 7., 1. / 7.,
         1. / 3.])  # coord x nodi quadratura riferimento
    yq = np.array(
        [0., 0., 1., 0., 0.5, 0.5, 5. / 7., 1. / 7., 1. / 7.,
         1. / 3.])  # coord y nodi quadratura riferimento
    #    eval_p = np.array([xq,yq])
    #    eval_p = np.reshape ( eval_p, (xq.shape[0],2))
    er_l2 = 0.
    ex_l2 = 0.
    er_derx = 0.
    ex_derx = 0.
    er_dery = 0.
    ex_dery = 0.
    for row in topo:
        A = x[row]
        B = y[row]
        uu = sol[row]
        b11 = A[1] - A[0]
        b12 = A[2] - A[0]
        b21 = B[1] - B[0]
        b22 = B[2] - B[0]
        #det=b11*b22-b12*b21
        xp = A[0] + b11 * xq + b12 * yq
        yp = B[0] + b21 * xq + b22 * yq
        eval_p = np.array([xp, yp])
        eval_p = eval_p.transpose()
        #eval_p = eval_p.transpose
        #       print eval_p.shape[0]

        tmpesatta = esatta.sol_esatta1(xp, yp)
        tmpesatta = np.reshape(tmpesatta, (1, xp.shape[0]))
        #        tmpderx = esatta.dx_sol_esatta(xp,yp)
        (tmpderx, tmpdery) = esatta.der_sol_esatta_1(xp, yp)

        #        print "ciao",tmpderx
        #        tmpderx = np.reshape ( tmpderx, (1,xp.shape[0]))
        #        tmpdery = esatta.dy_sol_esatta(xp,yp)
        #        print "ciao-ciao", tmpdery
        #        tmpdery = np.reshape (tmpdery , (1,xp.shape[0]))
        tmpapprox = np.zeros((1, xp.shape[0]))
        tmphderx = np.zeros((1, xp.shape[0]))
        tmphdery = np.zeros((1, xp.shape[0]))

        (phi_dx, phi_dy, phi, omega) = basis.tri_p1(A, B, eval_p)
        #print phi
        for i in np.array([0, 1, 2]):
            tmpapprox = tmpapprox + uu[i] * phi[:, i]

            tmphderx = tmphderx + uu[i] * phi_dx[:, i]
            tmphdery = tmphdery + uu[i] * phi_dy[:, i]

        tmp = np.sum(aq * (tmpesatta - tmpapprox)**2) * 2 * omega
        tmp2 = np.sum(aq * (tmpesatta)**2) * 2 * omega
        tmpx = np.sum(aq * (tmpderx - tmphderx)**2) * 2 * omega
        tmp2x = np.sum(aq * tmpderx**2) * 2 * omega
        tmpy = np.sum(aq * (tmpdery - tmphdery)**2) * 2 * omega
        tmp2y = np.sum(aq * tmpdery**2) * 2 * omega
        er_derx = er_derx + tmpx
        ex_derx = ex_derx + tmp2x
        er_dery = er_dery + tmpy
        ex_dery = ex_dery + tmp2y

        er_l2 = er_l2 + tmp
        ex_l2 = ex_l2 + tmp2

    er_l2 = np.sqrt(er_l2 / ex_l2)
    er_derx = np.sqrt(er_derx / ex_derx)
    er_dery = np.sqrt(er_dery / ex_dery)

    return er_l2, er_derx, er_dery