BB = np.kron(B.T,np.identity(dpn)) matDT = constitutive(material, dpn) k += gauss.wgt[i] * j * np.dot(np.dot(BB.T, matDT), BB) # assemble global K matrix K[eft[:, np.newaxis], eft] += k # derive body force vector f bodyf = grule(p+3) for i, xi in enumerate(bodyf.xi): phi, dphi = np.array(shapes(xi,p)) Xxi = np.dot(X.T,[phi[0],phi[1]]) #Xxi = mapping(xi,e) j = h/2 matDT = constitutive(material, dpn) f += bodyf.wgt[i] * j * phi * BodyF(matDT, Xxi) # assemble global body force vector F[eft] += f #-- Applying boundary conditions... zero = bcs[0] F -= K[:, zero] * bcs[1] K[:, zero] = 0; K[zero, :] = 0; K[zero, zero] = 1 F[zero] = bcs[1] # apply loads F[load[0]] += load[1]
# assemble global K matrix K[eft_0[:, np.newaxis], eft_0] += k_0 K[eft_er[:, np.newaxis], eft_er] += k_er K[eft_0[:, np.newaxis], eft_er] += k_0_er K[eft_er[:, np.newaxis], eft_0] += k_er_0 print(K.toarray()) # derive body force vector f bodyf = grule(p + 3) for i, xi in enumerate(bodyf.xi): N_0, dN_0 = fns_l2(xi, X) x = np.dot(X.T, [N_0[0], N_0[1]]) j = h / 2 matDT = 1 f_0 += bodyf.wgt[i] * j * N_0 * BodyF(matDT, x, 1) f_er += bodyf.wgt[i] * j * N_er * BodyF(matDT, x, 1) # assemble global body force vector F[eft_0] += f_0 F[eft_er] += f_er print('F:', F) K_0 = K[0:nnodes, 0:nnodes] K_er = K[nnodes:, nnodes:] K_0_er = K[0:nnodes, nnodes:] K_er_0 = K[nnodes:, 0:nnodes] print(K_0.toarray()) print(K_er.toarray()) print(K_0_er.toarray()) print(K_er_0.toarray())
Jinv, j = jacobian(X, dN) B = np.dot(dN, Jinv) BB = np.kron(B.T, np.identity(dpn)) matDT = constitutive(material, dpn) k += gauss.wgt[i] * j * np.dot(np.dot(BB.T, matDT), BB) print('k', k) # assemble global K matrix K[eft[:, np.newaxis], eft] += k bodyforce = quadrature(etype, 4) for i, xi in enumerate(bodyforce.xi): N, dN = eval('fns_{}'.format(etype))(xi, X) Jinv, j = jacobian(X, dN) matDT = constitutive(material, dpn) f += bodyforce.wgt[i] * j * N * BodyF(matDT, xi) # assemble global body force vector F[eft] += f print('F', F) print('K', K.toarray()) print("-- Applying boundary conditions...") zero = bcs[0] # array of rows/columns which are to be zeroed out F -= K[:, zero] * bcs[1] # modify right hand side with prescribed values K[:, zero] = 0 K[zero, :] = 0 # zero-out rows/columns K[zero, zero] = 1 # add 1 in the diagonal F[zero] = bcs[1] # prescribed values
Jinv, j = jacobian(X, dN) B = np.dot(dN, Jinv) BB = np.kron(B.T, np.identity(dpn)) matDT = constitutive(material, dpn) k += gauss.wgt[i] * j * np.dot(np.dot(BB.T, matDT), BB) # assemble global K matrix K[eft[:, np.newaxis], eft] += k bodyf = quadrature(etype, 3) for i, xi in enumerate(bodyf.xi): N, dN = eval('fns_{}'.format(etype))(xi, X) Xxi = np.dot(X.T,N) #map xi back to X and then sustitude into the bodyforce!!!! Jinv, j = jacobian(X, dN) matDT = constitutive(material, dpn) f += bodyf.wgt[i] * j * N * BodyF(matDT,Xxi,1) # assemble global body force vector F[eft] += f #print('F', F) print('K', K.toarray()) print("-- Applying boundary conditions...") zero = bcs[0] # array of rows/columns which are to be zeroed out F -= K[:, zero] * bcs[1] # modify right hand side with prescribed values K[:, zero] = 0; K[zero, :] = 0; # zero-out rows/columns K[zero, zero] = 1 # add 1 in the diagonal F[zero] = bcs[1] # prescribed values
def pFEMsol2(p, case): h = 0.2 nodes = np.array([[0.0], [0.2], [0.4], [0.6], [0.8], [1.0]]) elems = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]) bcs = [[0, 4 * p + 1], [0.0, 0.0]] load = [[0, 4 * p + 1], [0, 0]] nnodes, dpn = nodes.shape # node count and dofs per node dofs = dpn * nnodes + 5 * (p - 1) # total number of dofs material = tuple([1, 1]) gauss = grule(p + 1) K = sp.lil_matrix((dofs, dofs)) F = np.zeros(dofs) #-- Assembling stiffness matrix and force vector... for e, conn in enumerate(elems): # coordinate array for the element X = nodes[conn] ldofs = dpn * len(conn) + (p - 1) k = np.zeros((ldofs, ldofs)) f = np.zeros(ldofs) # element degree of freedom if p == 1: eft = np.array([dpn * n + i for n in conn for i in range(dpn)]) if p > 1: if e == 0: eft = np.array([dpn * n for n in range(ldofs)]) elif e > 0: eft_0 = np.array([1 + p * (e - 1), 1 + p * e]) eft_1 = np.array([1 + p * e + (n + 1) for n in range(p - 1)]) eft = np.append(eft_0, eft_1) # derive element k matrix for i, xi in enumerate(gauss.xi): phi, dphi = shapes(xi, p) j = h / 2 Jinv = 1 / j B = np.dot(dphi, Jinv) BB = np.kron(B.T, np.identity(dpn)) matDT = constitutive(material, dpn) k += gauss.wgt[i] * j * np.dot(np.dot(BB.T, matDT), BB) # assemble global K matrix K[eft[:, np.newaxis], eft] += k # derive body force vector f bodyf = grule(p + 3) for i, xi in enumerate(bodyf.xi): phi, dphi = np.array(shapes(xi, p)) Xxi = np.dot(X.T, [phi[0], phi[1]]) j = h / 2 matDT = constitutive(material, dpn) f += bodyf.wgt[i] * j * phi * BodyF(matDT, Xxi, 2) # assemble global body force vector F[eft] += f #print('F', F) #-- Applying boundary conditions... zero = bcs[0] F -= K[:, zero] * bcs[1] K[:, zero] = 0 K[zero, :] = 0 K[zero, zero] = 1 F[zero] = bcs[1] # apply loads F[load[0]] += load[1] #-- Solving system of equations... u = spsolve(K.tocsr(), F) #-- Calculating strain energy... U = 0.5 * np.dot((u.T * K), u) return dofs, U